fixed instance loading forget to append basepath.. other path fixes (windows)
[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 void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc );
48 void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset );
49
50
51 // Implementation
52 //=======================================================================================================================
53
54 struct csr_shader
55 {
56 u32 stride;
57 csr_frag_program frag;
58 csr_frag_clear clear;
59 };
60
61 struct csr_target
62 {
63 void *colour;
64 float *depth;
65
66 u32 x, y;
67 boxf bounds;
68 float scale;
69
70 v2f subsamples[ 8 ];
71 int num_samples;
72 v2f *sample_src;
73
74 csr_shader *shader;
75 };
76
77 struct csr_filter
78 {
79 const char *visgroup; // Limit to this visgroup only
80 const char *classname; // Limit to this exact classname. will not draw world
81
82 int compute_bounds_only;
83 };
84
85 enum EMSAA
86 {
87 k_EMSAA_none,
88 k_EMSAA_2x2,
89 k_EMSAA_RGSS,
90 k_EMSAA_8R
91 };
92
93 #ifdef CSR_EXECUTABLE
94
95 // MSAA patterns
96 v2f csr_msaa_1[] =
97 {
98 {0.f, 0.f}
99 };
100
101 // XX
102 // XX
103 v2f csr_msaa_2x2[] =
104 {
105 { 0x0.4p0f, 0x0.4p0f },
106 { 0x0.4p0f, -0x0.4p0f },
107 { -0x0.4p0f, -0x0.4p0f },
108 { -0x0.4p0f, 0x0.4p0f }
109 };
110
111 // X
112 // X
113 // X
114 // X
115 v2f csr_msaa_2x2rgss[] =
116 {
117 { 0x0.2p0f, 0x0.6p0f },
118 { -0x0.6p0f, 0x0.2p0f },
119 { -0x0.2p0f, -0x0.6p0f },
120 { 0x0.6p0f, -0x0.2p0f }
121 };
122
123 // X
124 // X
125 // X
126 // X
127 // X
128 // X
129 // X
130 // X
131 v2f csr_msaa_8rook[] =
132 {
133 { 0x0.1p0f, 0x0.7p0f },
134 { 0x0.5p0f, 0x0.1p0f },
135 { 0x0.7p0f, -0x0.3p0f },
136 { 0x0.3p0f, -0x0.5p0f },
137 { -0x0.1p0f, -0x0.7p0f },
138 { -0x0.5p0f, -0x0.1p0f },
139 { -0x0.7p0f, 0x0.3p0f },
140 { -0x0.3p0f, 0x0.5p0f }
141 };
142
143
144 void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa, csr_shader *shader )
145 {
146 rt->x = x;
147 rt->y = y;
148
149 switch( aa )
150 {
151 default:
152 case k_EMSAA_none:
153 rt->num_samples = 1;
154 rt->sample_src = csr_msaa_1;
155 break;
156
157 case k_EMSAA_2x2:
158 rt->num_samples = 4;
159 rt->sample_src = csr_msaa_2x2;
160 break;
161
162 case k_EMSAA_RGSS:
163 rt->num_samples = 4;
164 rt->sample_src = csr_msaa_2x2rgss;
165 break;
166
167 case k_EMSAA_8R:
168 rt->num_samples = 8;
169 rt->sample_src = csr_msaa_8rook;
170 break;
171 }
172
173 rt->shader = shader;
174 rt->depth = (float *)csr_malloc( x*y*rt->num_samples * sizeof(float) );
175 rt->colour = csr_malloc( x * y * rt->shader->stride * rt->num_samples );
176
177 v3_fill( rt->bounds[0], INFINITY );
178 v3_fill( rt->bounds[1], -INFINITY );
179 }
180
181 void csr_update_subsamples( csr_target *rt )
182 {
183 float range_x = (rt->bounds[1][0]-rt->bounds[0][0]);
184 float range_y = (rt->bounds[1][1]-rt->bounds[0][1]);
185
186 v2f pixel_size = { range_x/(float)rt->x, range_y/(float)rt->y };
187
188 for( int i = 0; i < rt->num_samples; i ++ )
189 {
190 v2_mul( rt->sample_src[i], pixel_size, rt->subsamples[i] );
191 }
192 }
193
194 void csr_rt_free( csr_target *rt )
195 {
196 free( rt->depth );
197 free( rt->colour );
198 }
199
200 void csr_rt_clear( csr_target *rt )
201 {
202 for( u32 i = 0; i < rt->x*rt->y*rt->num_samples; i ++ )
203 {
204 rt->shader->clear( rt->colour + i * rt->shader->stride );
205 rt->depth[i] = 0.f;
206 }
207 }
208
209 void csr_auto_fit( csr_target *rt, float padding )
210 {
211 // Correct aspect ratio to be square
212 float dx, dy, l, cx, cy;
213
214 dx = rt->bounds[1][0] - rt->bounds[0][0];
215 dy = rt->bounds[1][1] - rt->bounds[0][1];
216
217 l = fmaxf( dx, dy ) * .5f;
218
219 cx = (rt->bounds[1][0] + rt->bounds[0][0]) * .5f;
220 cy = (rt->bounds[1][1] + rt->bounds[0][1]) * .5f;
221
222 rt->bounds[0][0] = cx - l - padding;
223 rt->bounds[1][0] = cx + l + padding;
224 rt->bounds[0][1] = cy - l - padding;
225 rt->bounds[1][1] = cy + l + padding;
226
227 rt->scale = l + padding;
228
229 csr_update_subsamples( rt );
230 }
231
232 void simple_raster( csr_target *rt, vmf_vert tri[3] )
233 {
234 // Very very simplified rasterizing algorithm
235 v2f bmin = { 0.f, 0.f };
236 v2f bmax = { rt->x, rt->y };
237
238 v2_minv( tri[0].co, tri[1].co, bmin );
239 v2_minv( tri[2].co, bmin, bmin );
240
241 v2_maxv( tri[0].co, tri[1].co, bmax );
242 v2_maxv( tri[2].co, bmax, bmax );
243
244 float range_x = (rt->bounds[1][0]-rt->bounds[0][0])/(float)rt->x;
245 float range_y = (rt->bounds[1][1]-rt->bounds[0][1])/(float)rt->y;
246
247 int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x)));
248 int end_x = csr_max( 0, csr_min( rt->x-1, ceilf( (bmax[0]-rt->bounds[0][0])/range_x)));
249 int start_y = csr_min( rt->y-1, csr_max( 0, floorf( (bmin[1]-rt->bounds[0][1])/range_y)));
250 int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y)));
251
252 v2f v0, v1, v2, vp;
253 float d, bca = 0.f, bcb = 0.f, bcc = 0.f;
254
255 v2_sub( tri[1].co, tri[0].co, v0 );
256 v2_sub( tri[2].co, tri[0].co, v1 );
257 v2_sub( tri[1].co, tri[2].co, v2 );
258 d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]);
259
260 // Backface culling
261 if( v2_cross( v0, v1 ) > 0.f )
262 return;
263
264 v2f trace_origin;
265
266 for( u32 py = start_y; py <= end_y; py ++ )
267 {
268 trace_origin[1] = csr_lerpf( rt->bounds[0][1], rt->bounds[1][1], (float)py/(float)rt->y );
269
270 for( u32 px = start_x; px <= end_x; px ++ )
271 {
272 u32 sample_index = ((rt->y-py-1)*rt->x+px) * rt->num_samples;
273
274 void *frag = rt->colour + sample_index*rt->shader->stride;
275 float *depth = &rt->depth[ sample_index ];
276
277 trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x );
278
279 // Determine coverage
280 for( int i = 0; i < rt->num_samples; i ++ )
281 {
282 v3f sample_origin;
283
284 v2_add( rt->subsamples[ i ], trace_origin, sample_origin );
285 v2_sub( sample_origin, tri[0].co, vp );
286
287 if( v2_cross( v0, vp ) > 0.f )
288 continue;
289 if( v2_cross( vp, v1 ) > 0.f )
290 continue;
291
292 v2f vp2;
293 v2_sub( sample_origin, tri[2].co, vp2 );
294
295 if( v2_cross( vp2, v2 ) > 0.f )
296 continue;
297
298 bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d;
299 bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
300 bca = 1.f - bcb - bcc;
301
302 float hit = tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc;
303 float hit_depth = hit + 16385.f;
304
305 if( hit_depth > depth[i] && hit >= rt->bounds[0][2] && hit <= rt->bounds[1][2] )
306 {
307 depth[i] = hit_depth;
308 rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
309 }
310 }
311 }
312 }
313 }
314
315 void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform )
316 {
317 m3x3f normal;
318 vmf_vert new_tri[3];
319
320 // Derive normal matrix
321 m4x3_to_3x3( transform, normal );
322 m3x3_inv_transpose( normal, normal );
323
324 for( u32 i = 0; i < triangle_count; i ++ )
325 {
326 vmf_vert *triangle = triangles + i*3;
327
328 m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
329 m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
330 m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
331
332 m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
333 m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
334 m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
335
336 v3_normalize( new_tri[0].nrm );
337 v3_normalize( new_tri[1].nrm );
338 v3_normalize( new_tri[2].nrm );
339
340 m4x3_mulv( transform, triangles[0].origin, new_tri[0].origin );
341
342 simple_raster( rt, new_tri );
343 }
344 }
345
346 void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst )
347 {
348 m4x3f transform = M4X3_IDENTITY;
349 vmf_solid solid;
350 vmf_vert tri[3];
351 boxf trf_bounds;
352
353 u32 group_id = 0;
354 int filter_visgroups = 0, filter_classname = 0, compute_bounds_only = 0;
355
356 if( filter )
357 {
358 if( filter->visgroup )
359 {
360 filter_visgroups = 1;
361 group_id = vmf_visgroup_id( root, filter->visgroup );
362 }
363
364 if( filter->classname )
365 {
366 filter_classname = 1;
367 }
368
369 compute_bounds_only = filter->compute_bounds_only;
370 }
371
372 // Multiply previous transform with instance transform to create basis
373 if( prev )
374 {
375 m4x3_mul( prev, inst, transform );
376 }
377
378 // Gather world brushes
379 solidgen_ctx_init( &solid );
380
381 if( !filter_classname )
382 {
383 vdf_node *world = vdf_next( root, "world", NULL );
384
385 vdf_foreach( world, "solid", brush )
386 {
387 if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) )
388 continue;
389
390 // TODO: heap-use-after-free
391 solidgen_push( &solid, brush );
392 }
393 }
394
395 // Actual entity loop
396 m4x3f model;
397
398 vdf_foreach( root, "entity", ent )
399 {
400 if( filter_visgroups && !vmf_visgroup_match( ent, group_id ) )
401 continue;
402
403 if( filter_classname )
404 if( strcmp( kv_get( ent, "classname", "" ), filter->classname ) )
405 continue;
406
407 if( ent->user & VMF_FLAG_IS_PROP )
408 {
409 // Create model transform
410 m4x3_identity( model );
411
412 vmf_entity_transform( ent, model );
413 m4x3_mul( transform, model, model );
414
415 // Draw model
416 mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl;
417
418 if( compute_bounds_only )
419 {
420 map->models[ ent->user1 ].need_load = 1;
421 m4x3_expand_aabb_point( model, rt->bounds, (v3f){0.f,0.f,0.f} );
422 }
423 else
424 {
425 for( int i = 0; i < mdl->num_indices/3; i ++ )
426 {
427 for( int j = 0; j < 3; j ++ )
428 {
429 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ], tri[j].co );
430 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
431 v3_zero( tri[j].origin );
432 }
433
434 csr_draw( rt, tri, 1, model );
435 }
436 }
437 }
438 else if( ent->user & VMF_FLAG_IS_INSTANCE )
439 {
440 m4x3_identity( model );
441 vmf_entity_transform( ent, model );
442
443 csr_vmf_render( rt, map, map->cache[ ent->user1 ].root, filter, transform, model );
444 }
445 else
446 {
447 // Brush entity
448 vdf_foreach( ent, "solid", ent_solid )
449 {
450 solidgen_push( &solid, ent_solid );
451 }
452 }
453 }
454
455 if( compute_bounds_only )
456 {
457 solidgen_bounds( &solid, trf_bounds );
458 m4x3_transform_aabb( transform, trf_bounds );
459 box_concat( rt->bounds, trf_bounds );
460 }
461 else
462 {
463 // Draw brushes
464 for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ )
465 {
466 u32 * base = solid.indices + i*3;
467
468 tri[0] = solid.verts[ base[0] ];
469 tri[1] = solid.verts[ base[1] ];
470 tri[2] = solid.verts[ base[2] ];
471
472 csr_draw( rt, tri, 1, transform );
473 }
474 }
475
476 solidgen_ctx_reset( &solid );
477 solidgen_ctx_free( &solid );
478 }
479
480 void csr_write_filerr( const char *path )
481 {
482 log_error( "File write error (No such file or directory): '%s'\n", path );
483 }
484
485 void csr_write_txt( char const *path, const char *name, csr_target *rt )
486 {
487 FILE *write_ptr;
488
489 write_ptr = fopen( path, "w" );
490
491 if( write_ptr )
492 {
493 fprintf( write_ptr, "\"%s\"\n\{\n", name );
494 fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name );
495 fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] );
496 fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] );
497 fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x );
498 fprintf( write_ptr, "}\n" );
499
500 fclose( write_ptr );
501 }
502 else
503 {
504 csr_write_filerr( path );
505 }
506 }
507
508 // ALWAYS RGB32
509 void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset )
510 {
511 float *image = (float *)csr_malloc( rt->x*rt->y*3*sizeof(float) );
512
513 float contrib = 1.f/(float)rt->num_samples;
514
515 for( int i = 0; i < rt->x*rt->y; i ++ )
516 {
517 void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
518 float *dst = image + i*3;
519
520 v3_zero( dst );
521 for( int k = 0; k < rt->num_samples; k ++ )
522 {
523 v3_muladds( dst, (float *)(src + k*rt->shader->stride), contrib, dst );
524 }
525 }
526
527 if( !csr_32f_write( path, rt->x, rt->y, image ) )
528 csr_write_filerr( path );
529
530 free( image );
531 }
532
533 // Save floating point buffer to tga. Must be in range (0-1)
534 // Offset and stride are in bytes
535 void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc )
536 {
537 u8 *image = (u8 *)csr_malloc( rt->x*rt->y * 4 );
538
539 float contrib = 255.f/(float)rt->num_samples;
540
541 for( int i = 0; i < rt->x*rt->y; i ++ )
542 {
543 void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
544 u8 *dst = image + i*4;
545
546 v4f accum = { 0.f, 0.f, 0.f, 0.f };
547
548 for( int k = 0; k < rt->num_samples; k ++ )
549 {
550 float *src_sample = (float *)(src + k*rt->shader->stride);
551
552 for( int j = 0; j < nc; j ++ )
553 {
554 accum[ j ] += src_sample[ j ] * contrib;
555 }
556 }
557
558 // TODO: Clamp this value
559 dst[0] = accum[0];
560 dst[1] = accum[1];
561 dst[2] = accum[2];
562 dst[3] = accum[3];
563 }
564
565 if( !csr_tga_write( path, rt->x, rt->y, nc, image ) )
566 csr_write_filerr( path );
567
568 free( image );
569 }
570
571 #endif