summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-07-18 03:43:19 +0800
committerchai <chaifix@163.com>2020-07-18 03:43:19 +0800
commit4deff343b5fd928ae9475eedcf9b6add34b31ae2 (patch)
treed9108f5fedc9f0dd57bf964763edc508983c80c5
parent4984946caf0397a13da7605bc1ee406ea7354628 (diff)
-uniform collection
-rw-r--r--Release/SoftShadeRoom.exebin131072 -> 130560 bytes
-rw-r--r--src/core/device.c24
-rw-r--r--src/core/rasterizer.c24
-rw-r--r--src/core/rasterizer.h6
-rw-r--r--src/core/shader.h20
-rw-r--r--src/example/04_bloom/postprocess.c4
-rw-r--r--src/example/04_bloom/preprocess.c5
-rw-r--r--src/gizmo/gizmo.c4
-rw-r--r--src/shaders/common/core.h10
-rw-r--r--src/shaders/default.c5
-rw-r--r--src/shaders/pbr.c4
-rw-r--r--src/shaders/unlit.c5
12 files changed, 59 insertions, 52 deletions
diff --git a/Release/SoftShadeRoom.exe b/Release/SoftShadeRoom.exe
index a158108..280d7e2 100644
--- a/Release/SoftShadeRoom.exe
+++ b/Release/SoftShadeRoom.exe
Binary files differ
diff --git a/src/core/device.c b/src/core/device.c
index 8284315..ce13c72 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -61,6 +61,8 @@ static struct {
} state;
+UniformCollection* g_uniforms = &state.uniforms;
+
#define MATRIXTOP state.matrix_top[state.matrix_mode]
#define MATRIX state.matrices[state.matrix_mode][MATRIXTOP]
#define MATRIXSTACK state.matrices[state.matrix_mode]
@@ -538,7 +540,7 @@ static void render_prims_triangle(uint varying_flag) {
reset_active_reg = FALSE;
ssrS_setactiveregr();
}
- ssrR_triangle(c0, c1, c2, i0, i1, i2, state.program, &state.uniforms, early_culled);
+ ssrR_triangle(c0, c1, c2, i0, i1, i2, state.program, early_culled);
}
else {
if (!reset_active_reg) {
@@ -552,7 +554,7 @@ static void render_prims_triangle(uint varying_flag) {
vt2 = &clip_buffer.vertices[i + 1];
c0 = &vt0->clip_coord; c1 = &vt1->clip_coord; c2 = &vt2->clip_coord;
i0 = vt0->index; i1 = vt1->index; i2 = vt2->index;
- ssrR_triangle(c0, c1, c2, i0, i1, i2, state.program, &state.uniforms, early_culled);
+ ssrR_triangle(c0, c1, c2, i0, i1, i2, state.program, early_culled);
}
}
}
@@ -578,7 +580,7 @@ static void render_prims_line(uint varying_flag) {
reset_active_reg = FALSE;
ssrS_setactiveregr();
}
- ssrR_line(c0, c1, i0, i1, state.program, &state.uniforms);
+ ssrR_line(c0, c1, i0, i1, state.program);
}
else {
if (!reset_active_reg) {
@@ -592,7 +594,7 @@ static void render_prims_line(uint varying_flag) {
vt1 = &clip_buffer.vertices[i + 1];
c0 = &vt0->clip_coord; c1 = &vt1->clip_coord;
i0 = vt0->index; i1 = vt1->index;
- ssrR_line(c0, c1, i0, i1, state.program, &state.uniforms);
+ ssrR_line(c0, c1, i0, i1, state.program);
}
}
}
@@ -612,7 +614,7 @@ static void render_prims_point(uint varying_flag) {
clipped = clip_point(c0, i0, varying_flag, &clip_buffer);
if (!clipped) {
- ssrR_point(c0, i0, state.program, &state.uniforms);
+ ssrR_point(c0, i0, state.program);
}
else {
/*clipped*/
@@ -628,11 +630,11 @@ void ssr_draw(ssr_PrimitiveType primitive) {
/*set built-in uniforms*/
ssr_getmvp(&mvp);
ssr_getmv(&mv);
- state.uniforms.model = &GETMATRIX(MATRIX_MODEL);
- state.uniforms.view = &GETMATRIX(MATRIX_VIEW);
- state.uniforms.projection = &GETMATRIX(MATRIX_PROJECTION);
- state.uniforms.mvp = &mvp;
- state.uniforms.mv = &mv;
+ g_uniforms->model = &GETMATRIX(MATRIX_MODEL);
+ g_uniforms->view = &GETMATRIX(MATRIX_VIEW);
+ g_uniforms->projection = &GETMATRIX(MATRIX_PROJECTION);
+ g_uniforms->mvp = &mvp;
+ g_uniforms->mv = &mv;
uint varying_flag = state.program->varying_flag;
bool use_extra_varyings = (varying_flag & VARYING_ANY) != 0;
@@ -663,7 +665,7 @@ void ssr_draw(ssr_PrimitiveType primitive) {
if (use_extra_varyings) {
ssrS_setupregisterpointers(vert->index);
}
- vert_shader(&state.uniforms, vert, &clip_coords.coords[i]);
+ vert_shader(vert, &clip_coords.coords[i]);
}
/*set register pointer to frag-in*/
diff --git a/src/core/rasterizer.c b/src/core/rasterizer.c
index c5745ba..55d4ad3 100644
--- a/src/core/rasterizer.c
+++ b/src/core/rasterizer.c
@@ -82,11 +82,12 @@ static void puttriangle(Vec2* A, Vec2* B, Vec2* C, Color c) {
ssrR_putline(C->x, C->y, B->x, B->y, c);
}
+extern UniformCollection* g_uniforms ;
+
void ssrR_triangle(
Vec4* CA, Vec4* CB, Vec4* CC,
uint IA, uint IB, uint IC,
Program* program,
- UniformCollection* uniforms,
bool early_culled
) {
ssr_assert(CA && CB && CC && program);
@@ -104,6 +105,8 @@ void ssrR_triangle(
}
}
+ UniformCollection* uniforms = g_uniforms;
+
Vec4 SA, SB, SC;
internal_vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA);
internal_vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB);
@@ -186,7 +189,7 @@ void ssrR_triangle(
/*interpolate varying variables*/ \
ssrS_solveregsbcp(&bc, IA, IB, IC); \
/*enter fragment shader*/ \
- discard = !frag_shader(uniforms, out_color[0]); \
+ discard = !frag_shader(out_color[0]); \
discardif(discard); \
/*put point*/ \
ssr_blendandputpoint(p.x, p.y, blend); \
@@ -242,10 +245,9 @@ void ssrR_triangle(
void ssrR_line(
Vec4* CA, Vec4* CB,
uint IA, uint IB,
- Program* program,
- UniformCollection* uniforms
+ Program* program
) {
- ssr_assert(CA && CB && program && uniforms);
+ ssr_assert(CA && CB && program);
Vec4 SA, SB;
internal_vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA);
@@ -253,6 +255,8 @@ void ssrR_line(
FragmentShader frag_shader = program->fragmentshader;
+ UniformCollection* uniforms = g_uniforms;
+
int x0 = SA.x, y0 = SA.y;
int x1 = SB.x, y1 = SB.y;
float wA = SA.w, wB = SB.w;
@@ -318,7 +322,7 @@ void ssrR_line(
discardif(!pass_depth_test || !pass_stencil_test);
/*solve registers with lerp*/
ssrS_solveregslerp(t, IA, IB);
- discard = !frag_shader(uniforms, out_color[0]);
+ discard = !frag_shader(out_color[0]);
discardif(discard);
/*put point*/
ssr_blendandputpoint(px, py, blend);
@@ -333,11 +337,13 @@ void ssrR_line(
#undef discardif
}
-void ssrR_point(Vec4* CA, uint IA, Program* program, UniformCollection* uniforms) {
- ssr_assert(CA && program && uniforms);
+void ssrR_point(Vec4* CA, uint IA, Program* program) {
+ ssr_assert(CA && program);
FragmentShader frag_shader = program->fragmentshader;
+ UniformCollection* uniforms = g_uniforms;
+
Vec3 SA;
internal_vec4_dividew(CA, &SA); ssrU_viewport(&SA, &SA);
@@ -364,7 +370,7 @@ void ssrR_point(Vec4* CA, uint IA, Program* program, UniformCollection* uniforms
}
discardif(!pass_depth_test || !pass_stencil_test);
ssrS_solveregscopy(IA);
- discard = !frag_shader(uniforms, out_color[0]);
+ discard = !frag_shader(out_color[0]);
discardif(discard);
/*put point*/
ssr_blendandputpoint(px, py, blend);
diff --git a/src/core/rasterizer.h b/src/core/rasterizer.h
index a566329..fce8376 100644
--- a/src/core/rasterizer.h
+++ b/src/core/rasterizer.h
@@ -13,8 +13,8 @@ bool ssrR_barycentric(Vec2* A, Vec2* B, Vec2* C, Vec2* p, Vec3* out); /*计算重心
void ssrR_center(Vec2* A, Vec2* B, Vec2* C, Vec2* out); /*获得重心*/
bool ssrR_ispointintriangle(Vec2* A, Vec2* B, Vec2* C, Vec2* p);
-void ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, uint IA, uint IB, uint IC, Program* program, UniformCollection* uniforms);
-void ssrR_line(Vec4* CA, Vec4* CB, uint IA, uint IB, Program* program, UniformCollection* uniforms);
-void ssrR_point(Vec4* CA, uint IA, Program* program, UniformCollection* uniforms);
+void ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, uint IA, uint IB, uint IC, Program* program, bool early_called);
+void ssrR_line(Vec4* CA, Vec4* CB, uint IA, uint IB, Program* program);
+void ssrR_point(Vec4* CA, uint IA, Program* program);
#endif \ No newline at end of file
diff --git a/src/core/shader.h b/src/core/shader.h
index 043ba1e..be3819f 100644
--- a/src/core/shader.h
+++ b/src/core/shader.h
@@ -24,13 +24,15 @@ typedef struct {
void* userdata;
} UniformCollection;
-#define UTEX(i) (uniforms->var_tex[i])
-#define UM4(i) (&uniforms->var_mat4[i])
-#define UV2(i) (&uniforms->var_vec2[i])
-#define UV3(i) (&uniforms->var_vec3[i])
-#define UV4(i) (&uniforms->var_vec4[i])
-#define UN(i) (&uniforms->var_num[i])
-#define UU (uniforms->userdata)
+extern UniformCollection* g_uniforms;
+
+#define UTEX(i) (g_uniforms->var_tex[i])
+#define UM4(i) (&g_uniforms->var_mat4[i])
+#define UV2(i) (&g_uniforms->var_vec2[i])
+#define UV3(i) (&g_uniforms->var_vec3[i])
+#define UV4(i) (&g_uniforms->var_vec4[i])
+#define UN(i) (&g_uniforms->var_num[i])
+#define UU (g_uniforms->userdata)
#define REG_TOTAL 32
#define REG_NUM_COUNT 4
@@ -42,7 +44,7 @@ typedef struct {
// Vertex* vertex;
//} Vertex;
-typedef void(*VertexShader)(UniformCollection* uniforms, Vertex* in, Vec4* homocoord);
+typedef void(*VertexShader)(Vertex* in, Vec4* homocoord);
typedef struct {
float num[REG_NUM_COUNT];
@@ -51,7 +53,7 @@ typedef struct {
Vec4 v4[REG_V4_COUNT];
} FragmentShaderIn;
-typedef bool(*FragmentShader)(UniformCollection* uniforms, Color32* color);
+typedef bool(*FragmentShader)(Color32* color);
typedef struct {
VertexShader vertexshader;
diff --git a/src/example/04_bloom/postprocess.c b/src/example/04_bloom/postprocess.c
index fe58b96..1d476d0 100644
--- a/src/example/04_bloom/postprocess.c
+++ b/src/example/04_bloom/postprocess.c
@@ -9,13 +9,13 @@
static float weight[5] = { 0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162 };
-static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
+static void vert( Vertex* in, Vec4* clipcoord) {
clipcoord->xyz = in->position;
clipcoord->w = 1;
*_texcoord = in->texcoord;
}
-static bool frag(UniformCollection* uniforms, Color32* color) {
+static bool frag( Color32* color) {
float off = 1 / 600.f;
//blur
*color = tex2d(_frag_tex, _texcoord);
diff --git a/src/example/04_bloom/preprocess.c b/src/example/04_bloom/preprocess.c
index f02195f..0956c59 100644
--- a/src/example/04_bloom/preprocess.c
+++ b/src/example/04_bloom/preprocess.c
@@ -5,9 +5,8 @@
/*varyings*/
#define _color reg_v4_00
-static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
+static void vert( Vertex* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->position; p.w = 1;
- internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord);
color_tocolor32(in->color, _color);
}
@@ -15,7 +14,7 @@ static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
#define _frag_color out_color[0]
#define _bright_color out_color[1]
-static bool frag(UniformCollection* uniforms, Color32* color) {
+static bool frag( Color32* color) {
//_frag_color->r = 1;
//_frag_color->g = 1;
//_frag_color->b = 1;
diff --git a/src/gizmo/gizmo.c b/src/gizmo/gizmo.c
index adf5e43..30638ee 100644
--- a/src/gizmo/gizmo.c
+++ b/src/gizmo/gizmo.c
@@ -47,12 +47,12 @@ void gizmo_ondraw() {
// varying
#define _color reg_v4_00
-static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
+static void vert( Vertex* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->position; p.w = 1;
object2clip(&p, clipcoord);
color_tocolor32(in->color, _color);
}
-static bool frag(UniformCollection* uniforms, Color32* color) {
+static bool frag( Color32* color) {
*color = *_color;
return 1;
}
diff --git a/src/shaders/common/core.h b/src/shaders/common/core.h
index 2e895c5..ef58897 100644
--- a/src/shaders/common/core.h
+++ b/src/shaders/common/core.h
@@ -7,10 +7,10 @@
/* variables */
/************************************************************************/
-#define _model_matrix (uniforms->model)
-#define _view_matrix (uniforms->view)
-#define _proj_matrix (uniforms->projection)
-#define _mvp_matrix (uniforms->mvp)
+#define _model_matrix (g_uniforms->model)
+#define _view_matrix (g_uniforms->view)
+#define _proj_matrix (g_uniforms->projection)
+#define _mvp_matrix (g_uniforms->mvp)
#define _it_model_matrix /*inverse-transpose model matrix if needed*/
// near
@@ -51,7 +51,7 @@ if(cond) return 0; \
#define MVP_PROCESS \
do{ \
static Vec4 p; p.xyz = in->position; p.w = 1; \
-internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord); \
+internal_mat4_mulvec4(g_uniforms->mvp, &p, clipcoord); \
}while(0)
#define object2clip(pos, out) internal_mat4_mulvec4(_mvp_matrix, pos, out);
diff --git a/src/shaders/default.c b/src/shaders/default.c
index efd9f25..46b4b6b 100644
--- a/src/shaders/default.c
+++ b/src/shaders/default.c
@@ -11,9 +11,8 @@
/*varyings*/
#define rough reg_num_00
-static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
+static void vert( Vertex* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->position; p.w = 1;
- internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord);
Vec4 normal = {
in->normal.x,
in->normal.y,
@@ -25,7 +24,7 @@ static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
*rough = 1 - internal_vec3_dot(&worldnormal, light);
}
-static bool frag(UniformCollection* uniforms, Color32* color) {
+static bool frag( Color32* color) {
internal_vec3_normalize(light, light);
float roughness = *rough;
//(*color).r = 1;
diff --git a/src/shaders/pbr.c b/src/shaders/pbr.c
index 1c74feb..8a47051 100644
--- a/src/shaders/pbr.c
+++ b/src/shaders/pbr.c
@@ -19,7 +19,7 @@
#define _world_tangent reg_v3_03
#define _world_bitangent reg_v3_04
-static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
+static void vert( Vertex* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->position; p.w = 1;
object2clip(&p, clipcoord);
Vec3 worldnormal = mat4_mulvec3(*_object2world, in->normal);
@@ -31,7 +31,7 @@ static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
_clip_pos->y = clipcoord->w;
}
-static bool frag(UniformCollection* uniforms, Vec4* color) {
+static bool frag( Vec4* color) {
//internal_vec3_normalize(light, light);
//internal_vec3_normalize(vnormal, vnormal);
//float roughness = *rough;
diff --git a/src/shaders/unlit.c b/src/shaders/unlit.c
index c6a258b..cc31443 100644
--- a/src/shaders/unlit.c
+++ b/src/shaders/unlit.c
@@ -8,9 +8,8 @@
/*varyings*/
#define rough reg_num_00
-static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
+static void vert( Vertex* in, Vec4* clipcoord) {
static Vec4 p; p.xyz = in->position; p.w = 1;
- internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord);
Vec4 normal = {
in->normal.x,
in->normal.y,
@@ -22,7 +21,7 @@ static void vert(UniformCollection* uniforms, Vertex* in, Vec4* clipcoord) {
*reg_num_00 = 1 - internal_vec3_dot(&worldnormal, light);
}
-static bool frag(UniformCollection* uniforms, Color32* color) {
+static bool frag( Color32* color) {
internal_vec3_normalize(light, light);
return 1;
}