97fd8a54553966b80b7f94ac210c6e4001ef5857
[csRadar.git] / csrDraw.h
1 // Copyright (C) 2021 Harry Godden (hgn)
2
3 // Extremely simple software renderer. Only supports orthographic
4 //=======================================================================================================================
5
6 typedef struct csr_target csr_target;
7 typedef struct csr_filter csr_filter;
8 typedef struct csr_shader csr_shader;
9 typedef enum EMSAA EMSAA;
10
11 typedef void (* csr_frag_program)( void *, vmf_vert[3], float, float, float );
12 typedef void (* csr_frag_clear)( void * );
13
14 // API
15 //=======================================================================================================================
16
17 // Create a render target. Resolution, msaa, and shader must be known at this point!
18 void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa, csr_shader *shader );
19 void csr_rt_clear( csr_target *rt );
20 void csr_rt_free( csr_target *rt );
21
22 // Refit bounds so that it is square, and crops to center with padding
23 void csr_auto_fit( csr_target *rt, float padding );
24
25 // Run this after bounds have been adjusted on the RT to update the size of the msaa
26 // Auto fit will call this.
27 void csr_update_subsamples( csr_target *rt );
28
29 // Write CS:GO radar txt
30 void csr_write_txt( char const *path, const char *name, csr_target *rt );
31
32 // Render calls
33 // ------------
34
35 // Render a finalzied triangle into target. Coordinates are world space
36 void simple_raster( csr_target *rt, vmf_vert tri[3] );
37
38 // Draw a batch of triangles with an affine world transformation
39 void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform );
40
41 // Draw VMF with filtering options. Will automatically branch into instances
42 // You should call this with the last two recursive arguments (prev,inst), set to NULL
43 //
44 // Filter is optional, it can be st to NULL to just render everything.
45 void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst );
46
47 // Obsolete
48 void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname );
49
50 // Implementation
51 //=======================================================================================================================
52
53 struct csr_shader
54 {
55 u32 stride;
56 csr_frag_program frag;
57 csr_frag_clear clear;
58 };
59
60 struct csr_target
61 {
62 void *colour;
63 float *depth;
64
65 u32 x, y;
66 boxf bounds;
67 float scale;
68
69 v2f subsamples[ 8 ];
70 int num_samples;
71 v2f *sample_src;
72
73 csr_shader *shader;
74 };
75
76 struct csr_filter
77 {
78 const char *visgroup; // Limit to this visgroup only
79 const char *classname; // Limit to this exact classname. will not draw world
80
81 int compute_bounds_only;
82 };
83
84 enum EMSAA
85 {
86 k_EMSAA_none,
87 k_EMSAA_2x2,
88 k_EMSAA_RGSS,
89 k_EMSAA_8R
90 };
91
92 #ifdef CSR_EXECUTABLE
93
94 // MSAA patterns
95 v2f csr_msaa_1[] =
96 {
97 {0.f, 0.f}
98 };
99
100 // XX
101 // XX
102 v2f csr_msaa_2x2[] =
103 {
104 { 0x0.4p0f, 0x0.4p0f },
105 { 0x0.4p0f, -0x0.4p0f },
106 { -0x0.4p0f, -0x0.4p0f },
107 { -0x0.4p0f, 0x0.4p0f }
108 };
109
110 // X
111 // X
112 // X
113 // X
114 v2f csr_msaa_2x2rgss[] =
115 {
116 { 0x0.2p0f, 0x0.6p0f },
117 { -0x0.6p0f, 0x0.2p0f },
118 { -0x0.2p0f, -0x0.6p0f },
119 { 0x0.6p0f, -0x0.2p0f }
120 };
121
122 // X
123 // X
124 // X
125 // X
126 // X
127 // X
128 // X
129 // X
130 v2f csr_msaa_8rook[] =
131 {
132 { 0x0.1p0f, 0x0.7p0f },
133 { 0x0.5p0f, 0x0.1p0f },
134 { 0x0.7p0f, -0x0.3p0f },
135 { 0x0.3p0f, -0x0.5p0f },
136 { -0x0.1p0f, -0x0.7p0f },
137 { -0x0.5p0f, -0x0.1p0f },
138 { -0x0.7p0f, 0x0.3p0f },
139 { -0x0.3p0f, 0x0.5p0f }
140 };
141
142
143 void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa, csr_shader *shader )
144 {
145 rt->x = x;
146 rt->y = y;
147
148 switch( aa )
149 {
150 default:
151 case k_EMSAA_none:
152 rt->num_samples = 1;
153 rt->sample_src = csr_msaa_1;
154 break;
155
156 case k_EMSAA_2x2:
157 rt->num_samples = 4;
158 rt->sample_src = csr_msaa_2x2;
159 break;
160
161 case k_EMSAA_RGSS:
162 rt->num_samples = 4;
163 rt->sample_src = csr_msaa_2x2rgss;
164 break;
165
166 case k_EMSAA_8R:
167 rt->num_samples = 8;
168 rt->sample_src = csr_msaa_8rook;
169 break;
170 }
171
172 rt->shader = shader;
173 rt->depth = (float *)csr_malloc( x*y*rt->num_samples * sizeof(float) );
174 rt->colour = csr_malloc( x * y * rt->shader->stride * rt->num_samples );
175
176 v3_fill( rt->bounds[0], INFINITY );
177 v3_fill( rt->bounds[1], -INFINITY );
178 }
179
180 void csr_update_subsamples( csr_target *rt )
181 {
182 float range_x = (rt->bounds[1][0]-rt->bounds[0][0]);
183 float range_y = (rt->bounds[1][1]-rt->bounds[0][1]);
184
185 v2f pixel_size = { range_x/(float)rt->x, range_y/(float)rt->y };
186
187 for( int i = 0; i < rt->num_samples; i ++ )
188 {
189 v2_mul( rt->sample_src[i], pixel_size, rt->subsamples[i] );
190 }
191 }
192
193 void csr_rt_free( csr_target *rt )
194 {
195 free( rt->depth );
196 free( rt->colour );
197 }
198
199 void csr_rt_clear( csr_target *rt )
200 {
201 for( u32 i = 0; i < rt->x*rt->y*rt->num_samples; i ++ )
202 {
203 rt->shader->clear( rt->colour + i * rt->shader->stride );
204 rt->depth[i] = 0.f;
205 }
206 }
207
208 void csr_auto_fit( csr_target *rt, float padding )
209 {
210 // Correct aspect ratio to be square
211 float dx, dy, l, cx, cy;
212
213 dx = rt->bounds[1][0] - rt->bounds[0][0];
214 dy = rt->bounds[1][1] - rt->bounds[0][1];
215
216 l = fmaxf( dx, dy ) * .5f;
217
218 cx = (rt->bounds[1][0] + rt->bounds[0][0]) * .5f;
219 cy = (rt->bounds[1][1] + rt->bounds[0][1]) * .5f;
220
221 rt->bounds[0][0] = cx - l - padding;
222 rt->bounds[1][0] = cx + l + padding;
223 rt->bounds[0][1] = cy - l - padding;
224 rt->bounds[1][1] = cy + l + padding;
225
226 rt->scale = l + padding;
227
228 csr_update_subsamples( rt );
229 }
230
231 void csr_write_txt( char const *path, const char *name, csr_target *rt )
232 {
233 FILE *write_ptr;
234
235 write_ptr = fopen( path, "w" );
236
237 fprintf( write_ptr, "\"%s\"\n\{\n", name );
238 fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name );
239 fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] );
240 fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] );
241 fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x );
242 fprintf( write_ptr, "}\n" );
243
244 fclose( write_ptr );
245 }
246
247 void simple_raster( csr_target *rt, vmf_vert tri[3] )
248 {
249 // Very very simplified rasterizing algorithm
250 v2f bmin = { 0.f, 0.f };
251 v2f bmax = { rt->x, rt->y };
252
253 v2_minv( tri[0].co, tri[1].co, bmin );
254 v2_minv( tri[2].co, bmin, bmin );
255
256 v2_maxv( tri[0].co, tri[1].co, bmax );
257 v2_maxv( tri[2].co, bmax, bmax );
258
259 float range_x = (rt->bounds[1][0]-rt->bounds[0][0])/(float)rt->x;
260 float range_y = (rt->bounds[1][1]-rt->bounds[0][1])/(float)rt->y;
261
262 int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x)));
263 int end_x = csr_max( 0, csr_min( rt->x-1, ceilf( (bmax[0]-rt->bounds[0][0])/range_x)));
264 int start_y = csr_min( rt->y-1, csr_max( 0, floorf( (bmin[1]-rt->bounds[0][1])/range_y)));
265 int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y)));
266
267 v2f v0, v1, v2, vp;
268 float d, bca = 0.f, bcb = 0.f, bcc = 0.f;
269
270 v2_sub( tri[1].co, tri[0].co, v0 );
271 v2_sub( tri[2].co, tri[0].co, v1 );
272 v2_sub( tri[1].co, tri[2].co, v2 );
273 d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]);
274
275 // Backface culling
276 if( v2_cross( v0, v1 ) > 0.f )
277 return;
278
279 v2f trace_origin;
280
281 for( u32 py = start_y; py <= end_y; py ++ )
282 {
283 trace_origin[1] = csr_lerpf( rt->bounds[0][1], rt->bounds[1][1], (float)py/(float)rt->y );
284
285 for( u32 px = start_x; px <= end_x; px ++ )
286 {
287 u32 sample_index = (py * rt->y + px) * rt->num_samples;
288
289 void *frag = rt->colour + sample_index*rt->shader->stride;
290 float *depth = &rt->depth[ sample_index ];
291
292 trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x );
293
294 // Determine coverage
295 for( int i = 0; i < rt->num_samples; i ++ )
296 {
297 v3f sample_origin;
298
299 v2_add( rt->subsamples[ i ], trace_origin, sample_origin );
300 v2_sub( sample_origin, tri[0].co, vp );
301
302 if( v2_cross( v0, vp ) > 0.f )
303 continue;
304 if( v2_cross( vp, v1 ) > 0.f )
305 continue;
306
307 v2f vp2;
308 v2_sub( sample_origin, tri[2].co, vp2 );
309
310 if( v2_cross( vp2, v2 ) > 0.f )
311 continue;
312
313 bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d;
314 bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
315 bca = 1.f - bcb - bcc;
316
317 float hit = (tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc) +16385.f;
318
319 if( hit > depth[i] )
320 {
321 depth[i] = hit;
322 rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
323 }
324 }
325 }
326 }
327 }
328
329 void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform )
330 {
331 m3x3f normal;
332 vmf_vert new_tri[3];
333
334 // Derive normal matrix
335 m4x3_to_3x3( transform, normal );
336
337 // NOTE: This isn't strictly necessary since CS:GO only uses uniform scaling.
338 m3x3_inv_transpose( normal, normal );
339
340 for( u32 i = 0; i < triangle_count; i ++ )
341 {
342 vmf_vert *triangle = triangles + i*3;
343
344 m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
345 m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
346 m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
347 m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
348 m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
349 m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
350
351 simple_raster( rt, new_tri );
352 }
353 }
354
355 void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst )
356 {
357 m4x3f transform = M4X3_IDENTITY;
358 vmf_solid solid;
359 vmf_vert tri[3];
360 boxf trf_bounds;
361
362 u32 group_id = 0;
363 int filter_visgroups = 0, filter_classname = 0, compute_bounds_only = 0;
364
365 if( filter )
366 {
367 if( filter->visgroup )
368 {
369 filter_visgroups = 1;
370 group_id = vmf_visgroup_id( root, filter->visgroup );
371 }
372
373 if( filter->classname )
374 {
375 filter_classname = 1;
376 }
377
378 compute_bounds_only = filter->compute_bounds_only;
379 }
380
381 // Multiply previous transform with instance transform to create basis
382 if( prev )
383 {
384 m4x3_mul( prev, inst, transform );
385 }
386
387 // Gather world brushes
388 solidgen_ctx_init( &solid );
389
390 if( !filter_classname )
391 {
392 vdf_node *world = vdf_next( root, "world", NULL );
393
394 vdf_foreach( world, "solid", brush )
395 {
396 if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) )
397 continue;
398
399 // TODO: heap-use-after-free
400 solidgen_push( &solid, brush );
401 }
402 }
403
404 // Actual entity loop
405 m4x3f model;
406
407 vdf_foreach( root, "entity", ent )
408 {
409 if( filter_visgroups && !vmf_visgroup_match( ent, group_id ) )
410 continue;
411
412 if( filter_classname )
413 if( strcmp( kv_get( ent, "classname", "" ), filter->classname ) )
414 continue;
415
416 if( ent->user & VMF_FLAG_IS_PROP )
417 {
418 // Create model transform
419 m4x3_identity( model );
420
421 vmf_entity_transform( ent, model );
422 m4x3_mul( transform, model, model );
423
424 // Draw model
425 mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl;
426
427 if( compute_bounds_only )
428 {
429 map->models[ ent->user1 ].need_load = 1;
430 m4x3_expand_aabb_point( model, rt->bounds, (v3f){0.f,0.f,0.f} );
431 }
432 else
433 {
434 for( int i = 0; i < mdl->num_indices/3; i ++ )
435 {
436 for( int j = 0; j < 3; j ++ )
437 {
438 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ], tri[j].co );
439 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
440 tri[j].xy[0] = 0.f;
441 tri[j].xy[1] = 0.f;
442 }
443
444 csr_draw( rt, tri, 1, model );
445 }
446 }
447 }
448 else if( ent->user & VMF_FLAG_IS_INSTANCE )
449 {
450 m4x3_identity( model );
451 vmf_entity_transform( ent, model );
452
453 csr_vmf_render( rt, map, map->cache[ ent->user1 ].root, filter, transform, model );
454 }
455 else
456 {
457 // Brush entity
458 vdf_foreach( ent, "solid", ent_solid )
459 {
460 solidgen_push( &solid, ent_solid );
461 }
462 }
463 }
464
465 if( compute_bounds_only )
466 {
467 solidgen_bounds( &solid, trf_bounds );
468 m4x3_transform_aabb( transform, trf_bounds );
469 box_concat( rt->bounds, trf_bounds );
470 }
471 else
472 {
473 // Draw brushes
474 for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ )
475 {
476 u32 * base = solid.indices + i*3;
477
478 tri[0] = solid.verts[ base[0] ];
479 tri[1] = solid.verts[ base[1] ];
480 tri[2] = solid.verts[ base[2] ];
481
482 csr_draw( rt, tri, 1, transform );
483 }
484 }
485
486 solidgen_ctx_reset( &solid );
487 solidgen_ctx_free( &solid );
488 }
489
490 // Obsolete
491 void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname )
492 {
493 char output[ 512 ];
494
495 float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
496
497 float contrib = 1.f/(float)rt->num_samples;
498
499 for( int l = 0; l < rt->x; l ++ )
500 {
501 for( int x = 0; x < rt->y; x ++ )
502 {
503 float *dst = &image[ (l*1024+x)*3 ];
504 void *src = rt->colour + ((1023-l)*1024+x) * rt->num_samples * rt->shader->stride;
505
506 v3_muls( (float *)src, contrib, dst );
507
508 for( int j = 1; j < rt->num_samples; j ++ )
509 {
510 v3_muladds( dst, (float *)(src + j*rt->shader->stride), contrib, dst );
511 }
512 }
513 }
514
515 // Save position buffer
516 strcpy( output, basename );
517 strcat( output, "." );
518 strcat( output, subname );
519 strcat( output, "_position.pfm" );
520 csr_32f_write( output, rt->x, rt->y, image );
521
522 free( image );
523 }
524
525 #endif