summaryrefslogtreecommitdiff
path: root/src/core/rasterizer.c
blob: c5745ba2fbfce68902627f949bee198aa9f03127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "rasterizer.h"
#include "device.h"
#include "shader.h"
#include "../math/math.h"
#include "../util/assert.h"

/*directly put line onto screen*/
/*Bresenham's line algorithm*/
void ssrR_putline(int x0, int y0, int x1, int y1, Color color) {
	int steep = 0;
	if (abs(x0 - x1) < abs(y0 - y1)) {
		swapi(x0, y0);
		swapi(x1, y1);
		steep = 1;
	}
	if (x0 > x1) {
		swapi(x0, x1);
		swapi(y0, y1);
	}
	int dx = x1 - x0;
	int dy = y1 - y0;
	int derror2 = abs(dy) << 1;
	int error2 = 0;
	int y = y0;
	for (int x = x0; x <= x1; ++x) {
		if (steep) {
			ssr_putpoint(y, x, color);
		}
		else {
			ssr_putpoint(x, y, color);
		}
		error2 += derror2;
		if (error2 > dx) {
			y += (y1 > y0 ? 1 : -1);
			error2 -= (dx << 1);
		}
	}
}

/*使用辅助梯形计算三角形面积,《3D数学基础》P245*/
float ssrR_area(Vec2* v1, Vec2* v2, Vec2* v3) {
	ssr_assert(v1 && v2 && v3);
	float area  = 0.5f * ((v1->y - v2->y) * (v3->x - v2->x) + (v3->y - v2->y) * (v2->x - v1->x));
	return area;
}

/*from https://github.com/ssloy/tinyrenderer */
bool ssrR_barycentric(Vec2* A, Vec2* B, Vec2* C, Vec2* p, Vec3* out) {
	ssr_assert(A && B && C && p && out);
	Vec3 s[2], u;
	s[0].x = C->x - A->x; s[0].y = B->x - A->x; s[0].z = A->x - p->x;
	s[1].x = C->y - A->y; s[1].y = B->y - A->y; s[1].z = A->y - p->y;
	internal_vec3_cross(&s[0], &s[1], &u);
	if (compare(u.z, 0)) {
		return 0;
	} else {
		float uz = 1.f / u.z;
		out->x = 1 - (u.x + u.y) * uz;
		out->y = u.y * uz;
		out->z = u.x * uz;
		return 1;
	}
}

bool ssrR_ispointintriangle(Vec2* A, Vec2* B, Vec2* C, Vec2* p) {
	ssr_assert(A && B && C && p);
	Vec3 c; ssrR_barycentric(A, B, C, p, &c);
	return c.x >= 0 && c.y >= 0 && c.z >= 0;
}

void ssrR_center(Vec2* A, Vec2* B, Vec2* C, Vec2* out) {
	ssr_assert(A && B && C && out);
	float k = 1 / 3.f;
	out->x = k * (A->x + B->x + C->x);
	out->y = k * (A->y + B->y + C->y);
}

static void puttriangle(Vec2* A, Vec2* B, Vec2* C, Color c) {
	ssr_assert(A && B && C);
	ssrR_putline(A->x, A->y, B->x, B->y, c);
	ssrR_putline(A->x, A->y, C->x, C->y, c);
	ssrR_putline(C->x, C->y, B->x, B->y, c);
}

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);

	/*late back face culling*/
	if (!early_culled && ssr_isenable(ENABLE_BACKFACECULL)) {
		float w0 = 1 / CA->w, w1 = 1 / CB->w, w2 = 1 / CC->w;
		Vec2 ab, ac;
		ab.x = CB->x * w1 - CA->x * w0;
		ab.y = CB->y * w1 - CA->y * w0;
		ac.x = CC->x * w2 - CA->x * w0;
		ac.y = CC->y * w2 - CA->y * w0;
		if (ab.x * ac.y - ab.y * ac.x <= 0) {
			return;
		}
	}

	Vec4 SA, SB, SC;
	internal_vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA);
	internal_vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB);
	internal_vec4_dividewnoz(CC, &SC); ssrU_viewport(&SC, &SC);

	//puttriangle(&SA, &SB, &SC, 0xffff0000);
	//return;

	Vec4 *sa = &SA, *sb = &SB, *sc = &SC, *tmp; 
	Vec4 *v4tmp; 
	uint itmp;
#define swap(t, a, b) {t = a; a = b; b = t;} /*sort in y axis*/
	if (sb->y < sa->y) { swap(tmp, sa, sb); swap(v4tmp, CA, CB); swap(itmp, IA, IB); }
	if (sc->y < sb->y) { swap(tmp, sb, sc); swap(v4tmp, CB, CC); swap(itmp, IB, IC); }
	if (sb->y < sa->y) { swap(tmp, sa, sb); swap(v4tmp, CA, CB); swap(itmp, IA, IB); }
#undef swap

	Vec2 AB = {sb->x - sa->x, sb->y - sa->y}, AC = { sc->x - sa->x, sc->y - sa->y };
	int order = (AB.x * AC.y - AC.x * AB.y) > 0 ? 1 : -1;

	float invkAC = (sc->x - sa->x) / (sc->y - sa->y + EPSILON);
	float invkAB = (sb->x - sa->x) / (sb->y - sa->y + EPSILON);
	float invkBC = (sc->x - sb->x) / (sc->y - sb->y + EPSILON);
	
	float from, to;
	float depth;
	float CAw = 1.f / CA->w, CBw = 1.f / CB->w, CCw = 1.f / CC->w;
	
	bool depth_test    = ssr_isenable(ENABLE_DEPTHTEST);
	bool blend         = ssr_isenable(ENABLE_BLEND);
	bool write_depth   = ssr_isenable(ENABLE_WRITEDEPTH);
	bool stencil_test  = ssr_isenable(ENABLE_STENCILTEST);
	bool write_stencil = ssr_iswritesencil();

	bool pass_depth_test = TRUE;
	bool pass_stencil_test = TRUE;
	bool discard = FALSE;

	FragmentShader frag_shader = program->fragmentshader;

	Vec3 s[2], u; /*edge of triangle*/

	s[0].x = sc->x - sa->x; s[0].y = sb->x - sa->x;
	s[1].x = sc->y - sa->y; s[1].y = sb->y - sa->y;

	Vec2 p;
	Vec3 bc;

#define discardif(condition) if(condition) continue

#define RENDER_TRIANGLE \
	for (p.y = FROM->y; p.y < TO->y + OFFSET; ++p.y) {							      \
		SET_FROM_AND_TO	                                                    \
		s[1].z = sa->y - p.y;																			          \
		for (p.x = from; order * (p.x - to) <= 0; p.x += order) {	          \
			/*calculate barycentric coordinate*/															\
			s[0].z = sa->x - p.x;																		          \
			internal_vec3_cross(&s[0], &s[1], &u);													            \
			discardif(compare(u.z, 0));														            \
			u.z = 1.f / u.z;																			            \
			bc.x = 1.f - (u.x + u.y) * u.z;											              \
			bc.y = u.y * u.z;																		              \
			bc.z = u.x * u.z;																		              \
			discardif(bc.x < 0 || bc.y < 0 || bc.z < 0);				              \
			/*perspective correction*/																	      \
			bc.x *= CAw; bc.y *= CBw; bc.z *= CCw;							              \
			internal_vec3_scale(&bc, 1.f / (bc.x + bc.y + bc.z), &bc);                 \
			/*early depth testing*/																			      \
			if(depth_test){											                              \
				depth = bc.x*sa->z + bc.y*sb->z + bc.z*sc->z;										\
				depth /= bc.x*sa->w + bc.y*sb->w + bc.z*sc->w;									\
				/*depth = bc.x*sa->z+bc.y*sb->z+bc.z*sc->z;*//*wrong*/          \
				pass_depth_test = ssr_testdepth(p.x, p.y, depth);					      \
			}																														      \
			/*early stencil testing*/																			    \
			if(stencil_test){																							    \
				pass_stencil_test = ssr_teststencil(p.x, p.y, pass_depth_test); \
			}																															    \
			discardif(!pass_depth_test || !pass_stencil_test);						    \
			/*interpolate varying variables*/															    \
			ssrS_solveregsbcp(&bc, IA, IB, IC);                               \
			/*enter fragment shader*/																			    \
			discard = !frag_shader(uniforms, out_color[0]);                   \
			discardif(discard);																								\
			/*put point*/																										  \
			ssr_blendandputpoint(p.x, p.y, blend);													  \
			/*write depth and stencil*/																			  \
			if(write_depth) {																						      \
				ssr_writedepth(p.x, p.y, depth);													      \
			}																														      \
			if(write_stencil) {																							  \
				ssr_writestencil(p.x, p.y, pass_depth_test, pass_stencil_test); \
			}																																  \
		}																													          \
	}

#define FROM    sa 
#define TO      sb 
#define FROMK   invkAC 
#define TOK     invkAB
#define OFFSET  0
#define SET_FROM_AND_TO \
	from = (int)(invkAC * (p.y - sa->y) + sa->x); \
	to = (int)(invkAB * (p.y - sa->y) + sa->x);   \

	RENDER_TRIANGLE

#define FROM    sb 
#define TO      sc
#define FROMK   invkAC 
#define TOK     invkBC
#define OFFSET  1
#define SET_FROM_AND_TO \
	if (p.y == sb->y && sb->y == sc->y) {					  \
		from = sc->x;																  \
		to = sb->x;																	  \
	}																							  \
	else {																				  \
		from = (int)(invkAC * (p.y - sa->y) + sa->x); \
		to = (int)(invkBC * (p.y - sb->y) + sb->x);	  \
	}

	RENDER_TRIANGLE
		
#undef discardif

#undef FROM 
#undef TO
#undef FROMK
#undef TOK
#undef OFFSET
#undef SET_FROM_AND_TO
#undef RENDER_TRIANGLE
}

void ssrR_line(
	Vec4* CA, Vec4* CB, 
	uint IA, uint IB,
	Program* program, 
	UniformCollection* uniforms
) {
	ssr_assert(CA && CB && program && uniforms);

	Vec4 SA, SB;
	internal_vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA);
	internal_vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB);

	FragmentShader frag_shader = program->fragmentshader;
	
	int x0 = SA.x, y0 = SA.y;
	int x1 = SB.x, y1 = SB.y;
	float wA = SA.w, wB = SB.w;
	float zA = SA.z, zB = SB.z;
	
	bool steep = FALSE;
	if (abs(x0 - x1) < abs(y0 - y1)) { 
		swapi(x0, y0);
		swapi(x1, y1);
		steep = 1;
	}
	if (x0 > x1) { 
		swapi(x0, x1);
		swapi(y0, y1);
		swapi(IA, IB);
		swapf(wA, wB);
		swapf(zA, zB);
	}

	float inv_wA = 1 / wA, inv_wB = 1 / wB;

	bool depth_test = ssr_isenable(ENABLE_DEPTHTEST);
	bool blend = ssr_isenable(ENABLE_BLEND);
	bool write_depth = ssr_isenable(ENABLE_WRITEDEPTH);
	bool stencil_test = ssr_isenable(ENABLE_STENCILTEST);
	bool write_stencil = ssr_iswritesencil();

	bool pass_depth_test = TRUE;
	bool pass_stencil_test = TRUE;
	bool discard = FALSE;

#define discardif(condition) if(condition) continue

	float t, z, w, depth;
	Vec2 c;
	int x, y, px, py;
	for (x = x0; x <= x1; ++x) {
		t = (x - x0) / (float)(x1 - x0);
		y = y0 + (y1 - y0)*t;
		/*caculate center*/
		c.x = (1 - t) * inv_wA;
		c.y = t * inv_wB;
		discardif(compare(c.x+c.y, 0));
		c.x /= (c.x + c.y);
		t = 1 - c.x;
		if (steep) {
			px = y; 
			py = x; 
		}
		else { 
			px = x; 
			py = y; 
		}
		if (depth_test) {
			ssrS_lerpnum(t, &zA, &zB, &z);
			ssrS_lerpnum(t, &wA, &wB, &w);
			depth = z / w;
			pass_depth_test = ssr_testdepth(px, py, depth);
		}
		if (stencil_test) {
			pass_stencil_test = ssr_teststencil(px, py, pass_depth_test);
		}
		discardif(!pass_depth_test || !pass_stencil_test);
		/*solve registers with lerp*/
		ssrS_solveregslerp(t, IA, IB);
		discard = !frag_shader(uniforms, out_color[0]);     
		discardif(discard);																								
		/*put point*/																										  
		ssr_blendandputpoint(px, py, blend);													  
		/*write depth and stencil*/																			  
		if (write_depth) {
			ssr_writedepth(px, py, depth);													     
		}																														      
		if (write_stencil) {
			ssr_writestencil(px, py, pass_depth_test, pass_stencil_test); 
		}																																  
	}
#undef discardif
}

void ssrR_point(Vec4* CA, uint IA, Program* program, UniformCollection* uniforms) {
	ssr_assert(CA && program && uniforms);

	FragmentShader frag_shader = program->fragmentshader;

	Vec3 SA; 
	internal_vec4_dividew(CA, &SA); ssrU_viewport(&SA, &SA);

	bool depth_test = ssr_isenable(ENABLE_DEPTHTEST);
	bool blend = ssr_isenable(ENABLE_BLEND);
	bool write_depth = ssr_isenable(ENABLE_WRITEDEPTH);
	bool stencil_test = ssr_isenable(ENABLE_STENCILTEST);
	bool write_stencil = ssr_iswritesencil();

	bool pass_depth_test = TRUE;
	bool pass_stencil_test = TRUE;
	bool discard = FALSE;

#define discardif(condition) if(condition) return ;

	int px = SA.x, py = SA.y;
	float depth = SA.z;

	if (depth_test) {
		pass_depth_test = ssr_testdepth(px, py, depth);
	}
	if (stencil_test) {
		pass_stencil_test = ssr_teststencil(px, py, pass_depth_test);
	}
	discardif(!pass_depth_test || !pass_stencil_test);
	ssrS_solveregscopy(IA);
	discard = !frag_shader(uniforms, out_color[0]);
	discardif(discard);
	/*put point*/
	ssr_blendandputpoint(px, py, blend);
	/*write depth and stencil*/
	if (write_depth) {
		ssr_writedepth(px, py, depth);
	}
	if (write_stencil) {
		ssr_writestencil(px, py, pass_depth_test, pass_stencil_test);
	}
}