Compare commits
No commits in common. "9160c8ce351543f018de4e7d703b3177eff7ce18" and "a571d2d999ea11ec5b62cc4314a6b74b9cbbe7a9" have entirely different histories.
9160c8ce35
...
a571d2d999
162
src/k3.c
162
src/k3.c
@ -17,8 +17,6 @@
|
||||
|
||||
#include"ssort.h"
|
||||
|
||||
#include<stb_image_resize.h>
|
||||
|
||||
#define RAST_DIFF 0
|
||||
#define RAST_NORM 1
|
||||
#define RAST_DISP 2
|
||||
@ -497,20 +495,6 @@ struct k3Tex *k3TexCreate(enum k3TexType type) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int k3TexSetParameter(struct k3Tex *tex, enum k3TexParam param, void *value) {
|
||||
switch(param) {
|
||||
case k3_TEX_SET_MIPMAPPING_DISABLED:
|
||||
tex->mipmapForceDisabled = *(bool*) value;
|
||||
return 0;
|
||||
|
||||
case k3_TEX_SET_MIPMAP_ALPHA:
|
||||
tex->mipmapAlphaMode = *(int*) value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void k3TexUpdate(struct k3Tex *tex, enum k3TexType type, int index, uint16_t width, uint16_t height, void *data) {
|
||||
GLenum target = tex->cubemap ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
|
||||
|
||||
@ -534,28 +518,22 @@ void k3TexUpdate(struct k3Tex *tex, enum k3TexType type, int index, uint16_t wid
|
||||
GLint internalFmt, externalFmt, intype;
|
||||
int linearFilter, mipmap;
|
||||
int compressed = 0;
|
||||
int channels;
|
||||
bool gamma;
|
||||
|
||||
switch(type) {
|
||||
case k3_RAWCOLOR:
|
||||
internalFmt = GLAD_GL_ARB_texture_float ? GL_RGBA32F_ARB : GL_RGB10_A2;
|
||||
externalFmt = GL_RGBA;
|
||||
channels = 4;
|
||||
intype = GL_UNSIGNED_BYTE;
|
||||
linearFilter = 1;
|
||||
mipmap = 0;
|
||||
gamma = false;
|
||||
mipmap = 1;
|
||||
break;
|
||||
case k3_NORMAL:
|
||||
compressed = TextureCompressionEnabled;
|
||||
internalFmt = compressed ? TextureCompressionRGBA : GL_RGBA;
|
||||
externalFmt = GL_RGBA;
|
||||
channels = 4;
|
||||
intype = GL_UNSIGNED_BYTE;
|
||||
linearFilter = 1;
|
||||
mipmap = 1;
|
||||
gamma = false;
|
||||
break;
|
||||
case k3_DIFFUSE:
|
||||
case k3_EMISSION:
|
||||
@ -564,47 +542,39 @@ void k3TexUpdate(struct k3Tex *tex, enum k3TexType type, int index, uint16_t wid
|
||||
? (GLAD_GL_EXT_texture_sRGB ? TextureCompressionSRGBA : TextureCompressionRGBA)
|
||||
: (GLAD_GL_EXT_texture_sRGB ? GL_SRGB8_ALPHA8_EXT : GL_RGBA8);
|
||||
externalFmt = GL_RGBA;
|
||||
channels = 4;
|
||||
intype = GL_UNSIGNED_BYTE;
|
||||
linearFilter = 1;
|
||||
mipmap = 1;
|
||||
gamma = true;
|
||||
break;
|
||||
case k3_DISPLACEMENT:
|
||||
case k3_ROUGHNESS:
|
||||
internalFmt = !k3IsCore ? GL_LUMINANCE8 : GL_RED;
|
||||
externalFmt = GL_RED;
|
||||
channels = 1;
|
||||
intype = GL_UNSIGNED_BYTE;
|
||||
linearFilter = 1;
|
||||
mipmap = 1;
|
||||
gamma = false;
|
||||
break;
|
||||
case k3_ALPHA:
|
||||
internalFmt = !k3IsCore ? GL_ALPHA8 : GL_RED;
|
||||
externalFmt = !k3IsCore ? GL_ALPHA : GL_RED;
|
||||
channels = 1;
|
||||
intype = GL_UNSIGNED_BYTE;
|
||||
linearFilter = 1;
|
||||
mipmap = 1;
|
||||
gamma = false;
|
||||
break;
|
||||
case k3_DEPTH:
|
||||
internalFmt = GL_DEPTH_COMPONENT24;
|
||||
internalFmt = GL_DEPTH_COMPONENT32F;
|
||||
externalFmt = GL_DEPTH_COMPONENT;
|
||||
intype = GL_UNSIGNED_INT;
|
||||
channels = 4;
|
||||
if(!k3IsCore) glTexParameteri(target, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
|
||||
linearFilter = 1;
|
||||
mipmap = 0;
|
||||
gamma = false;
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
break;
|
||||
}
|
||||
|
||||
if(!MipmappingEnabled || tex->mipmapForceDisabled) {
|
||||
if(!MipmappingEnabled) {
|
||||
mipmap = 0;
|
||||
}
|
||||
|
||||
@ -617,6 +587,10 @@ void k3TexUpdate(struct k3Tex *tex, enum k3TexType type, int index, uint16_t wid
|
||||
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
} else {
|
||||
if(!k3IsCore && mipmap) {
|
||||
glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
if(type == k3_DEPTH) {
|
||||
@ -649,6 +623,23 @@ void k3TexUpdate(struct k3Tex *tex, enum k3TexType type, int index, uint16_t wid
|
||||
k3Log(k3_WARN, "Non-zero index passed for non-cubemap texture");
|
||||
}
|
||||
|
||||
if(compressed && TextureOfflineCompressor) {
|
||||
k3Log(k3_DEBUG, "Compressing texture...");
|
||||
|
||||
size_t len;
|
||||
data = TextureOfflineCompressor(data, width, height, externalFmt, intype, &len);
|
||||
|
||||
glCompressedTexImage2D(target, 0, internalFmt, width, height, 0, len, data);
|
||||
|
||||
free(data);
|
||||
} else {
|
||||
glTexImage2D(target, 0, internalFmt, width, height, 0, externalFmt, intype, data);
|
||||
}
|
||||
|
||||
if(!tex->cubemap && k3IsCore && mipmap) {
|
||||
glGenerateMipmap(target);
|
||||
}
|
||||
|
||||
tex->szX = width;
|
||||
tex->szY = height;
|
||||
tex->szZ = 0;
|
||||
@ -656,65 +647,6 @@ void k3TexUpdate(struct k3Tex *tex, enum k3TexType type, int index, uint16_t wid
|
||||
tex->glInternalFormat = internalFmt;
|
||||
tex->glExternalFormat = externalFmt;
|
||||
tex->glInType = intype;
|
||||
|
||||
if(data) {
|
||||
char *resized = malloc(width * height * channels);
|
||||
memcpy(resized, data, width * height * channels);
|
||||
|
||||
size_t level;
|
||||
for(level = 0;; level++) {
|
||||
if(compressed && TextureOfflineCompressor) {
|
||||
k3Log(k3_DEBUG, "Compressing level %i...", level);
|
||||
|
||||
size_t len;
|
||||
void *compressedData = TextureOfflineCompressor(data, width, height, externalFmt, intype, &len);
|
||||
|
||||
glCompressedTexImage2D(target, level, internalFmt, width, height, 0, len, compressedData);
|
||||
|
||||
free(compressedData);
|
||||
} else {
|
||||
glTexImage2D(target, level, internalFmt, width, height, 0, externalFmt, intype, data);
|
||||
}
|
||||
|
||||
if(!mipmap || tex->cubemap || (width <= 1 && height <= 1)) {
|
||||
break;
|
||||
}
|
||||
|
||||
size_t resizedWidth = width >> 1;
|
||||
if(resizedWidth == 0) {
|
||||
resizedWidth = 1;
|
||||
}
|
||||
|
||||
size_t resizedHeight = height >> 1;
|
||||
if(resizedHeight == 0) {
|
||||
resizedHeight = 1;
|
||||
}
|
||||
|
||||
stbir_resize_uint8_generic(data, width, height, 0, resized, resizedWidth, resizedHeight, 0, channels, channels == 4 ? 3 : STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, gamma ? STBIR_COLORSPACE_SRGB : STBIR_COLORSPACE_LINEAR, NULL);
|
||||
|
||||
data = resized;
|
||||
width = resizedWidth;
|
||||
height = resizedHeight;
|
||||
}
|
||||
|
||||
glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, 0);
|
||||
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, level);
|
||||
|
||||
free(resized);
|
||||
} else {
|
||||
glTexImage2D(target, 0, internalFmt, width, height, 0, externalFmt, intype, NULL);
|
||||
}
|
||||
}
|
||||
void k3TexUpdateSub(struct k3Tex *this, int index, uint16_t x, uint16_t y, uint16_t width, uint16_t height, void *data) {
|
||||
GLenum target = GL_TEXTURE_2D;
|
||||
if(this->cubemap) {
|
||||
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + index;
|
||||
} else if(index) {
|
||||
k3Log(k3_WARN, "Non-zero index passed for non-cubemap texture");
|
||||
}
|
||||
|
||||
glBindTexture(target, this->tex);
|
||||
glTexSubImage2D(target, index, x, y, width, height, this->glExternalFormat, this->glInType, data);
|
||||
}
|
||||
uint32_t k3TexSzX(struct k3Tex *this) {
|
||||
return this->szX;
|
||||
@ -1640,7 +1572,7 @@ static void forward_subpass(mat4 projection, mat4 view, int transparent, int lig
|
||||
if(!k3IsCore) {
|
||||
if(mat->passes[0].alphatest) {
|
||||
glEnable(GL_ALPHA_TEST);
|
||||
glAlphaFunc(GL_GREATER, mat->passes[0].alphatest);
|
||||
glAlphaFunc(GL_GREATER, 0.9f);
|
||||
} else {
|
||||
glDisable(GL_ALPHA_TEST);
|
||||
}
|
||||
@ -1705,7 +1637,7 @@ void k3PassForward(mat4 projection, mat4 cam) {
|
||||
if(GLAD_GL_EXT_framebuffer_sRGB) {
|
||||
glEnable(GL_FRAMEBUFFER_SRGB_EXT);
|
||||
}
|
||||
glDisable(GL_BLEND);
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
glFrontFace(GL_CCW);
|
||||
@ -1727,7 +1659,6 @@ void k3PassForward(mat4 projection, mat4 cam) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
// Horribly inefficient.
|
||||
for(size_t rble = 0; rble < renderQueueSize; rble++) {
|
||||
@ -1781,8 +1712,12 @@ void k3PassDepthOnly(mat4 projection, mat4 cam, int clear, int cull) {
|
||||
if(GLAD_GL_ARB_vertex_program)
|
||||
glDisable(GL_VERTEX_PROGRAM_ARB);
|
||||
|
||||
glEnable(GL_CULL_FACE);
|
||||
glFrontFace(GL_CCW);
|
||||
if(cull) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
} else {
|
||||
glDisable(GL_CULL_FACE);
|
||||
}
|
||||
|
||||
GLuint lastVP = 0;
|
||||
struct k3GLSLP *lastGLSLP = NULL;
|
||||
@ -1857,7 +1792,7 @@ void k3PassDepthOnly(mat4 projection, mat4 cam, int clear, int cull) {
|
||||
if(!k3IsCore) {
|
||||
if(mat->passes[0].alphatest) {
|
||||
glEnable(GL_ALPHA_TEST);
|
||||
glAlphaFunc(GL_GREATER, mat->passes[0].alphatest);
|
||||
glAlphaFunc(GL_GREATER, 0.9f);
|
||||
} else {
|
||||
glDisable(GL_ALPHA_TEST);
|
||||
}
|
||||
@ -1945,11 +1880,7 @@ static size_t compute_light_views(struct LightView lv[]) {
|
||||
glm_frustum_center(corners, viewcenter);
|
||||
|
||||
mat4 lightview;
|
||||
if(glm_vec3_dot(l->dir.direction, (vec3) {0, 1, 0}) >= 0.99) {
|
||||
glm_look(viewcenter, l->dir.direction, (vec3) {1, 0, 0}, lightview);
|
||||
} else {
|
||||
glm_look(viewcenter, l->dir.direction, (vec3) {0, 1, 0}, lightview);
|
||||
}
|
||||
glm_look_anyup(viewcenter, l->dir.direction, lightview);
|
||||
|
||||
vec4 minaabb = {+HUGE_VALF, +HUGE_VALF, +HUGE_VALF};
|
||||
vec4 maxaabb = {-HUGE_VALF, -HUGE_VALF, -HUGE_VALF};
|
||||
@ -1962,10 +1893,7 @@ static size_t compute_light_views(struct LightView lv[]) {
|
||||
glm_vec4_maxv(maxaabb, lightspaceCorner, maxaabb);
|
||||
}
|
||||
|
||||
// TODO: automate
|
||||
float minz = 50;
|
||||
|
||||
glm_ortho(minaabb[0], maxaabb[0], minaabb[1], maxaabb[1], minaabb[2] - minz, maxaabb[2], lv[i].p[cascade]);
|
||||
glm_ortho(minaabb[0], maxaabb[0], minaabb[1], maxaabb[1], minaabb[2] - 50, maxaabb[2], lv[i].p[cascade]);
|
||||
glm_mat4_inv_fast(lightview, lv[i].c[cascade]);
|
||||
}
|
||||
} else if(l->type == k3_SPOT || l->type == k3_HALF_OMNI) {
|
||||
@ -2266,7 +2194,7 @@ void k3PassIrregular(struct k3Offscreen *mainview, mat4 mainproj, mat4 maincam)
|
||||
#endif
|
||||
|
||||
// Constructs shadowmap atlas, saves `offscr` for own use
|
||||
void k3PassShadowmap(mat4 projection, mat4 cam, struct k3Offscreen *offscr, int minimumSubdivision) {
|
||||
void k3PassShadowmap(mat4 projection, mat4 cam, struct k3Offscreen *offscr, float cellSizeLimit) {
|
||||
struct k3Timer timer = k3StartTimer("k3PassShadowmap");
|
||||
|
||||
glm_mat4_copy(projection, ProjMat);
|
||||
@ -2309,17 +2237,12 @@ void k3PassShadowmap(mat4 projection, mat4 cam, struct k3Offscreen *offscr, int
|
||||
cellsPerDimension = cellsTotalSqrt;
|
||||
}
|
||||
|
||||
if(cellsPerDimension < minimumSubdivision) {
|
||||
cellsPerDimension = minimumSubdivision;
|
||||
}
|
||||
|
||||
uint16_t sz = k3TexSzX(offscr->depth);
|
||||
float cellSz = (float) sz / cellsPerDimension;
|
||||
|
||||
#ifdef SHADOWSKIP
|
||||
static size_t shadowskipI = 0;
|
||||
shadowskipI++;
|
||||
#endif
|
||||
if(cellSizeLimit > 0) {
|
||||
cellSz = fminf(cellSizeLimit, cellSz);
|
||||
}
|
||||
|
||||
k3BeginOffscreen(offscr);
|
||||
size_t i = 0;
|
||||
@ -2351,18 +2274,9 @@ void k3PassShadowmap(mat4 projection, mat4 cam, struct k3Offscreen *offscr, int
|
||||
glm_vec4_copy((vec4) {i, (float) cellSz / sz, 0, 0}, LightShadows[li].atlasSegment);
|
||||
}
|
||||
|
||||
#ifdef SHADOWSKIP
|
||||
// Skip every second light
|
||||
if((li + shadowskipI) % 2) {
|
||||
#endif
|
||||
|
||||
glViewport(vp[0], vp[1], vp[2], vp[3]);
|
||||
k3PassDepthOnly(proj, lv->c[camIdx], i == 0, false);
|
||||
|
||||
#ifdef SHADOWSKIP
|
||||
}
|
||||
#endif
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
14
src/k3.h
14
src/k3.h
@ -22,18 +22,9 @@ enum k3TexType {
|
||||
k3_RAWCOLOR,
|
||||
};
|
||||
|
||||
enum k3TexParam {
|
||||
k3_TEX_SET_MIPMAP_ALPHA,
|
||||
k3_TEX_SET_MIPMAPPING_DISABLED,
|
||||
};
|
||||
#define k3_PARAM_STANDARD 0
|
||||
#define k3_PARAM_MAX_DOWNSAMPLE 1
|
||||
|
||||
struct k3Tex;
|
||||
struct k3Tex *k3TexCreate(enum k3TexType type);
|
||||
int k3TexSetParameter(struct k3Tex*, enum k3TexParam, void*);
|
||||
void k3TexUpdate(struct k3Tex*, enum k3TexType type, int index, uint16_t width, uint16_t height, void *data);
|
||||
void k3TexUpdateSub(struct k3Tex*, int index, uint16_t x, uint16_t y, uint16_t width, uint16_t height, void *data);
|
||||
uint32_t k3TexSzX(struct k3Tex*);
|
||||
uint32_t k3TexSzY(struct k3Tex*);
|
||||
uint32_t k3TexSzZ(struct k3Tex*);
|
||||
@ -106,7 +97,7 @@ struct k3Mat {
|
||||
char additive;
|
||||
char transparent;
|
||||
char nocull;
|
||||
float alphatest;
|
||||
char alphatest;
|
||||
char depthwrite;
|
||||
} passes[1];
|
||||
};
|
||||
@ -168,7 +159,6 @@ void k3StorageUnref(struct k3Storage*);
|
||||
struct k3Mdl;
|
||||
struct k3Mdl *k3MdlCreate(size_t verts, size_t indices, size_t boneCount, vec3 *pos, uint8_t *nrm, float *uvs, uint8_t *cols, uint8_t *boneids, uint16_t *boneweights, uint16_t *inds, mat4 *invBind, uint8_t *boneParents);
|
||||
void k3MdlUpdatePos(struct k3Mdl *mdl, vec3 *pos);
|
||||
void k3MdlUpdateNrm(struct k3Mdl *mdl, uint8_t *nrm);
|
||||
void k3MdlAddMesh(struct k3Mdl*, struct k3Mat*, uint32_t idxStart, uint32_t idxNumber);
|
||||
struct k3Mesh *k3MdlGetMeshes(struct k3Mdl*, size_t *count);
|
||||
void k3MdlAddAnim(struct k3Mdl*, struct k3AnimationFountain*);
|
||||
@ -195,7 +185,7 @@ void k3BatchClear();
|
||||
void k3PassForward(mat4 projection, mat4 cam);
|
||||
void k3PassDepthOnly(mat4 projection, mat4 cam, int clear, int cull);
|
||||
|
||||
void k3PassShadowmap(mat4 projection, mat4 cam, struct k3Offscreen *offscr, int minimumSubdivision);
|
||||
void k3PassShadowmap(mat4 projection, mat4 cam, struct k3Offscreen *offscr, float cellSizeLimit);
|
||||
|
||||
struct k3Offscreen;
|
||||
struct k3Offscreen *k3OffscreenCreateMultisampled(struct k3Tex *diffuse, struct k3Tex *depth, uint8_t samples);
|
||||
|
||||
@ -28,8 +28,10 @@ struct k3Tex {
|
||||
GLuint glInType;
|
||||
bool mipmap;
|
||||
|
||||
bool mipmapForceDisabled;
|
||||
int mipmapAlphaMode;
|
||||
// Asynchronous decoding
|
||||
uint8_t deferredRemaining;
|
||||
uint8_t *deferredData[6];
|
||||
struct k3Tex *deferredNext;
|
||||
};
|
||||
|
||||
struct k3Storage {
|
||||
@ -103,7 +105,7 @@ static inline struct k3Timer k3StartTimer(char *name) {
|
||||
struct k3Timer t = {};
|
||||
|
||||
if(!GLAD_GL_ARB_timer_query) {
|
||||
return t;
|
||||
return;
|
||||
}
|
||||
|
||||
glGenQueries(2, (GLuint*) &t);
|
||||
|
||||
@ -10,8 +10,6 @@ struct k3RectF {
|
||||
float h;
|
||||
};
|
||||
|
||||
void k3BatchInit();
|
||||
|
||||
void k3BatchAdd(struct k3Tex *tex, struct k3RectF src, struct k3RectF dst, float rot, vec4 color, float borderRadius, float minAlpha);
|
||||
void k3BatchFlush();
|
||||
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
#include<string.h>
|
||||
#include"gl.h"
|
||||
#include<freetype/freetype.h>
|
||||
#include<freetype/ftmodapi.h>
|
||||
|
||||
#define GLCA_IMPLEMENTATION
|
||||
#include"glyphcache/glca.h"
|
||||
@ -49,9 +48,6 @@ int k3FontLoad(struct k3Font *this, const char *fn) {
|
||||
this->atlasSize = k3TexSzMax() / 4;
|
||||
this->atlas = k3TexCreate(k3_ALPHA);
|
||||
this->atlasCPU = malloc(this->atlasSize * this->atlasSize);
|
||||
|
||||
k3TexSetParameter(this->atlas, k3_TEX_SET_MIPMAPPING_DISABLED, &(bool) {true});
|
||||
|
||||
k3TexUpdate(this->atlas, k3_ALPHA, 0, this->atlasSize, this->atlasSize, NULL);
|
||||
|
||||
this->fontPixelSize = this->atlasSize / 16;
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
struct k3Tex;
|
||||
typedef struct k3Tex *k3MImageData;
|
||||
Loading…
Reference in New Issue
Block a user