summaryrefslogtreecommitdiff
path: root/src/core/device.c
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-12-15 00:39:18 +0800
committerchai <chaifix@163.com>2019-12-15 00:39:18 +0800
commit749bbc6a54e50c297ab49d9e515a3679651d1461 (patch)
tree097bbe044332e816aa481db1a4e325b8d3f63b0d /src/core/device.c
parent3f44877edfe4c301b258d522bcb4e8d9b6e92382 (diff)
*misc
Diffstat (limited to 'src/core/device.c')
-rw-r--r--src/core/device.c118
1 files changed, 87 insertions, 31 deletions
diff --git a/src/core/device.c b/src/core/device.c
index cf9fee2..3566448 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -21,7 +21,7 @@ typedef struct VertexAttr {
/*
** 状态结构
*/
-struct {
+static struct {
/* MVP矩阵栈 */
Mat4 matrices[3][MATRIXDEPTH];
uint matrixtop[3];
@@ -33,7 +33,7 @@ struct {
uint buffersize; /*framebuffer size in bytess*/
/* zbuffer 深度会被映射到0~1的非线性区间*/
- uint* zbuffer;
+ float* zbuffer;
/* 顶点数据 */
Vert* verts; uint nverts;
@@ -43,12 +43,19 @@ struct {
Program* program;
UniformCollection uniforms;
- /*texture relavent*/
+ /* texture relavent */
FilterMode filtermode;
WrapMode wrapmode;
+ //FrameBuffer* framebuffer;
+
uint enable;
+ struct {
+ ssr_BlendFactor src;
+ ssr_BlendFactor dst;
+ } blendfactor;
+
} state;
/*
@@ -75,11 +82,14 @@ void ssr_init(ssr_Config* conf) {
state.framebuffer = conf->target;
}
state.buffersize = sizeof(Color) * config.width * config.height;
- state.zbuffer = ssrM_newvector(uint, config.width * config.height);
- memset(state.zbuffer, 0xff, sizeof(uint)*config.width*config.height);
+ state.zbuffer = ssrM_newvector(float, config.width * config.height);
+ //memset(state.zbuffer, 0xff, sizeof(uint)*config.width*config.height);
state.filtermode = FILTERMODE_POINT;
state.wrapmode = WRAPMODE_CLAMP;
+
+ state.blendfactor.src = BLENDFACTOR_SRC_ALPHA;
+ state.blendfactor.dst = BLENDFACTOR_ONE_MINUS_SRC_ALPHA;
}
float ssr_getaspect() {
@@ -110,6 +120,19 @@ WrapMode ssr_getwrapmode() {
return state.wrapmode;
}
+void ssr_bindframebuffer(FrameBuffer* fbo) {
+
+}
+
+void ssr_unbindframebuffer() {
+
+}
+
+void ssr_setblendfunc(ssr_BlendFactor sfactor, ssr_BlendFactor dfactor) {
+ state.blendfactor.src = sfactor;
+ state.blendfactor.dst = dfactor;
+}
+
void ssr_matrixmode(ssr_MatrixMode mode) {
state.matrixmode = mode;
}
@@ -181,6 +204,11 @@ void ssr_getmvp(Mat4* out) {
mat4_multiply(&GETMATRIX(MATRIX_PROJECTION), out, out);
}
+void ssr_getm(Mat4* out) {
+ ssr_assert(out);
+ *out = GETMATRIX(MATRIX_MODEL);
+}
+
void ssr_getmv(Mat4* out) {
ssr_assert(out);
mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out);
@@ -240,7 +268,10 @@ void ssr_clearcolor(Color color) {
}
void ssr_cleardepth() {
- memset(state.zbuffer, 0xff, sizeof(uint)*config.width*config.height);
+ //memset(state.zbuffer, 0xff, sizeof(uint)*config.width*config.height);
+ for (int i = 0; i < config.width * config.height; ++i) {
+ state.zbuffer[i] = 1;
+ }
}
void ssr_putpoint(uint screenx, uint screeny, Color color) {
@@ -249,24 +280,24 @@ void ssr_putpoint(uint screenx, uint screeny, Color color) {
BUFFER[screeny * config.width + screenx] = color;
}
+void ssr_putpoint32(uint screenx, uint screeny, Color32* c32) {
+ Color c = color32_tocolor(c32);
+ ssr_putpoint(screenx, screeny, c);
+}
+
Color ssr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
unsigned int c = (a << 24) | (r << 16) | (g << 8) | b;
return c;
}
-bool ssr_testdepth(uint x, uint y, uint depth) {
- if (!contains(x, y, 0, config.width - 1, 0, config.height - 1))
- return 0;
+bool ssr_testdepth(uint x, uint y, float depth){
uint off = x + y * config.width;
- if (state.zbuffer[off] < depth)
- return 0;
- state.zbuffer[off] = depth;
- return 1;
+ return state.zbuffer[off] >= depth;
}
-bool ssr_testdepthf(uint x, uint y, float depth) {
- uint d = UINT_MAX * (double)depth;
- return ssr_testdepth(x, y, d);
+void ssr_writedepth(uint x, uint y, float depth) {
+ uint off = x + y * config.width;
+ state.zbuffer[off] = depth;
}
void ssrU_viewport(Vec2* p, Vec2* out) {
@@ -278,6 +309,26 @@ void ssrU_viewport(Vec2* p, Vec2* out) {
out->y = (int)round((1 - p->y) * (halfh - 0.5f));
}
+void ssr_blend(Color32* src, Color32* dst, Color32* out) {
+ ssrU_blend(state.blendfactor.src, state.blendfactor.dst, src, dst, out);
+}
+
+void ssrU_blend(ssr_BlendFactor sfactor
+ , ssr_BlendFactor dfactor
+ , Color32* src
+ , Color32* dst
+ , Color32* out
+) {
+ ssr_assert(src && dst && out);
+
+}
+
+Color32 ssr_getfbocolor(uint x, uint y) {
+ Color c = state.framebuffer[x + y * config.width];
+ Color32 c32; color_tocolor32(c, &c32);
+ return c32;
+}
+
void ssr_bindvertices(Vert* verts, int nverts, uint* indices, int nprims) {
ssr_assert(verts && indices);
state.verts = verts;
@@ -301,7 +352,7 @@ FragmentShaderIn ssr_frag_in;
static struct {
Vec4* coords;
uint length;
-} clipping_buffer;
+} clip_coords;
static ClippedBuffer clipped_buffer; /*clipping result*/
@@ -310,6 +361,7 @@ void ssr_draw(ssr_PrimitiveType primitive) {
static Mat4 mvp, mv;
+ /*set built-in uniforms*/
ssr_getmvp(&mvp);
ssr_getmv(&mv);
state.uniforms.model = &GETMATRIX(MATRIX_MODEL);
@@ -323,15 +375,17 @@ void ssr_draw(ssr_PrimitiveType primitive) {
/*prepare registers if necessary*/
if (use_extra_varyings) {
- ssrS_setregisters(varying_flag, state.nverts);
+ ssrS_openregs(varying_flag);
+ ssrS_setregisters(state.nverts);
+ ssrS_setactiveregr();
}
/*resize clipping coords buffer*/
- if (clipping_buffer.length < state.nverts) {
+ if (clip_coords.length < state.nverts) {
ssrM_resizevector(
Vec4,
- clipping_buffer.coords,
- clipping_buffer.length,
+ clip_coords.coords,
+ clip_coords.length,
state.nverts,
FALSE
);
@@ -343,9 +397,9 @@ void ssr_draw(ssr_PrimitiveType primitive) {
ssr_vert_in.vertex = vert;
/*set register pointers*/
if (use_extra_varyings) {
- ssrS_setupregisterpoints(varying_flag, vert->index);
+ ssrS_setupregisterpoints(vert->index);
}
- state.program->vertexshader(&state.uniforms, &ssr_vert_in, &clipping_buffer.coords[i]);
+ state.program->vertexshader(&state.uniforms, &ssr_vert_in, &clip_coords.coords[i]);
}
/*set register pointer to frag-in*/
@@ -363,9 +417,9 @@ void ssr_draw(ssr_PrimitiveType primitive) {
i1 = state.indices[i * 3 + 1];
i2 = state.indices[i * 3 + 2];
- h0 = &clipping_buffer.coords[i0],
- h1 = &clipping_buffer.coords[i1],
- h2 = &clipping_buffer.coords[i2];
+ h0 = &clip_coords.coords[i0],
+ h1 = &clip_coords.coords[i1],
+ h2 = &clip_coords.coords[i2];
v0 = &state.verts[i0];
v1 = &state.verts[i1];
@@ -379,8 +433,9 @@ void ssr_draw(ssr_PrimitiveType primitive) {
ab.y = h1->y * w1 - h0->y * w0;
ac.x = h2->x * w2 - h0->x * w0;
ac.y = h2->y * w2 - h0->y * w0;
- if (ab.x * ac.y - ab.y * ac.x <= 0)
+ if (ab.x * ac.y - ab.y * ac.x <= 0) {
continue; /*cull*/
+ }
}
/*clipping*/
@@ -388,20 +443,21 @@ void ssr_draw(ssr_PrimitiveType primitive) {
/*rasterization*/
if (!clipped) {
- ssrR_triangle(h0, h1, h2, v0, v1, v2, TRUE, NULL, NULL, NULL, state.program, &state.uniforms);
+ /*ssrS_setactiveregr();*/
+ ssrR_triangle(h0, h1, h2, v0, v1, v2, state.program, &state.uniforms);
} else {
+ ssrS_setactiveregc();
if (clipped_buffer.count >= 3) {
ClippedVert* vt0 = &clipped_buffer.vertices[0], *vt1, *vt2;
for (int i = 1; i <= clipped_buffer.count - 2; ++i) {
vt1 = &clipped_buffer.vertices[i];
vt2 = &clipped_buffer.vertices[i + 1];
h0 = &vt0->clip_coord; h1 = &vt1->clip_coord; h2 = &vt2->clip_coord;
- v0 = &vt0->vertex; v1 = &vt1->vertex; v2 = &vt2->vertex;
- ssrR_triangle(h0, h1, h2, v0, v1, v2, FALSE, vt0, vt1, vt2, state.program, &state.uniforms);
+ v0 = &vt0->vertex; v1 = &vt1->vertex; v2 = &vt2->vertex;
+ ssrR_triangle(h0, h1, h2, v0, v1, v2, state.program, &state.uniforms);
}
}
}
-
}
} else if (primitive == PRIMITIVE_POINT) {