finish up plugin architecture
[csRadar.git] / csrDraw.h
index 29189a1e9cfd16cc21e10dc6d5367cdcc9627845..97fd8a54553966b80b7f94ac210c6e4001ef5857 100644 (file)
--- a/csrDraw.h
+++ b/csrDraw.h
@@ -1,25 +1,76 @@
-typedef struct csr_frag csr_frag;
+// Copyright (C) 2021 Harry Godden (hgn)
+
+// Extremely simple software renderer. Only supports orthographic
+//=======================================================================================================================
+
 typedef struct csr_target csr_target;
 typedef struct csr_filter csr_filter;
+typedef struct csr_shader csr_shader;
+typedef enum EMSAA EMSAA;
 
-struct csr_frag
-{      
-       v3f co;
-       v3f nrm;
-       
-       float depth;
+typedef void (* csr_frag_program)( void *, vmf_vert[3], float, float, float );
+typedef void (* csr_frag_clear)( void * );
+
+// API
+//=======================================================================================================================
+
+// Create a render target. Resolution, msaa, and shader must be known at this point!
+void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa, csr_shader *shader );
+void csr_rt_clear( csr_target *rt );
+void csr_rt_free( csr_target *rt );
+
+// Refit bounds so that it is square, and crops to center with padding
+void csr_auto_fit( csr_target *rt, float padding );
+
+// Run this after bounds have been adjusted on the RT to update the size of the msaa
+// Auto fit will call this.
+void csr_update_subsamples( csr_target *rt );
+
+// Write CS:GO radar txt
+void csr_write_txt( char const *path, const char *name, csr_target *rt  );
+
+// Render calls
+// ------------
+
+// Render a finalzied triangle into target. Coordinates are world space
+void simple_raster( csr_target *rt, vmf_vert tri[3] );
+
+// Draw a batch of triangles with an affine world transformation
+void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform );
+
+// Draw VMF with filtering options. Will automatically branch into instances
+// You should call this with the last two recursive arguments (prev,inst), set to NULL
+//
+// Filter is optional, it can be st to NULL to just render everything.
+void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst );
+
+// Obsolete
+void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname );
+
+// Implementation
+//=======================================================================================================================
+
+struct csr_shader
+{
+       u32 stride;
+       csr_frag_program frag;
+       csr_frag_clear clear;
 };
 
 struct csr_target
 {
-       csr_frag *fragments;
+       void  *colour;
+       float *depth;
        
        u32 x, y; 
        boxf bounds;
        float scale;
        
-       v2f subsamples[ 16 ];
+       v2f subsamples[ 8 ];
        int num_samples;
+       v2f *sample_src;
+       
+       csr_shader *shader;
 };
 
 struct csr_filter
@@ -30,13 +81,97 @@ struct csr_filter
        int compute_bounds_only;
 };
 
-void csr_create_target( csr_target *rt, u32 x, u32 y )
+enum EMSAA
+{
+       k_EMSAA_none,
+       k_EMSAA_2x2,
+       k_EMSAA_RGSS,
+       k_EMSAA_8R
+};
+
+#ifdef CSR_EXECUTABLE
+
+// MSAA patterns
+v2f csr_msaa_1[] =
+{
+       {0.f, 0.f}
+};
+
+// XX
+// XX
+v2f csr_msaa_2x2[] =
+{
+       {  0x0.4p0f,  0x0.4p0f },
+       {  0x0.4p0f, -0x0.4p0f },
+       { -0x0.4p0f, -0x0.4p0f },
+       { -0x0.4p0f,  0x0.4p0f }
+};
+
+//   X 
+// X   
+//    X
+// X   
+v2f csr_msaa_2x2rgss[] =
+{
+       {  0x0.2p0f,  0x0.6p0f },
+       { -0x0.6p0f,  0x0.2p0f },
+       { -0x0.2p0f, -0x0.6p0f },
+       {  0x0.6p0f, -0x0.2p0f }
+};
+
+//     X   
+//   X     
+// X       
+//       X 
+//  X      
+//        X
+//      X  
+//    X    
+v2f csr_msaa_8rook[] = 
+{
+       {  0x0.1p0f,  0x0.7p0f },
+       {  0x0.5p0f,  0x0.1p0f },
+       {  0x0.7p0f, -0x0.3p0f },
+       {  0x0.3p0f, -0x0.5p0f },
+       { -0x0.1p0f, -0x0.7p0f },
+       { -0x0.5p0f, -0x0.1p0f },
+       { -0x0.7p0f,  0x0.3p0f },
+       { -0x0.3p0f,  0x0.5p0f }
+};
+
+
+void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa, csr_shader *shader )
 {
        rt->x = x;
        rt->y = y;
-       rt->num_samples = 4;
        
-       rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag)*rt->num_samples );
+       switch( aa )
+       {
+               default:
+               case k_EMSAA_none:
+                       rt->num_samples = 1;
+                       rt->sample_src = csr_msaa_1;
+                       break;
+                       
+               case k_EMSAA_2x2:
+                       rt->num_samples = 4;
+                       rt->sample_src = csr_msaa_2x2;
+                       break;
+                       
+               case k_EMSAA_RGSS:
+                       rt->num_samples = 4;
+                       rt->sample_src = csr_msaa_2x2rgss;
+                       break;
+                       
+               case k_EMSAA_8R:
+                       rt->num_samples = 8;
+                       rt->sample_src = csr_msaa_8rook;
+                       break;
+       }
+       
+       rt->shader = shader;
+       rt->depth = (float *)csr_malloc( x*y*rt->num_samples * sizeof(float) );
+       rt->colour = csr_malloc( x * y * rt->shader->stride * rt->num_samples );
        
        v3_fill( rt->bounds[0], INFINITY );
        v3_fill( rt->bounds[1], -INFINITY );
@@ -49,50 +184,46 @@ void csr_update_subsamples( csr_target *rt )
 
        v2f pixel_size = { range_x/(float)rt->x, range_y/(float)rt->y };
 
-       rt->subsamples[0][0] = pixel_size[0] * -0.25f;
-       rt->subsamples[0][1] = 0.f;
-       rt->subsamples[1][0] = pixel_size[0] * 0.75f;
-       rt->subsamples[1][1] = pixel_size[1] * 0.25f;
-       rt->subsamples[2][0] = 0.f;
-       rt->subsamples[2][1] = pixel_size[1] * 0.5f;
-       rt->subsamples[3][0] = pixel_size[0] * 0.5f;
-       rt->subsamples[3][1] = pixel_size[1] * 0.75f;
+       for( int i = 0; i < rt->num_samples; i ++ )
+       {
+               v2_mul( rt->sample_src[i], pixel_size, rt->subsamples[i] );
+       }
 }
 
 void csr_rt_free( csr_target *rt )
 {
-       free( rt->fragments );
+       free( rt->depth );
+       free( rt->colour );
 }
 
 void csr_rt_clear( csr_target *rt )
 {
        for( u32 i = 0; i < rt->x*rt->y*rt->num_samples; i ++ )
        {
-               v3_zero( rt->fragments[ i ].co );
-               v3_zero( rt->fragments[ i ].nrm );
-               rt->fragments[i].depth = 0.f;
+               rt->shader->clear( rt->colour + i * rt->shader->stride );       
+               rt->depth[i] = 0.f;
        }
 }
 
 void csr_auto_fit( csr_target *rt, float padding )
 {
        // Correct aspect ratio to be square
-       float dx, dy, d, l, cx, cy;
+       float dx, dy, l, cx, cy;
+
        dx = rt->bounds[1][0] - rt->bounds[0][0];
        dy = rt->bounds[1][1] - rt->bounds[0][1];
        
-       l = fmaxf( dx, dy );
-       d = l * ( l / dx ) * .5f;
+       l = fmaxf( dx, dy ) * .5f;
                
        cx = (rt->bounds[1][0] + rt->bounds[0][0]) * .5f;
        cy = (rt->bounds[1][1] + rt->bounds[0][1]) * .5f;
 
-       rt->bounds[0][0] = cx - d - padding;
-       rt->bounds[1][0] = cx + d + padding;
-       rt->bounds[0][1] = cy - d - padding;
-       rt->bounds[1][1] = cy + d + padding;
+       rt->bounds[0][0] = cx - l - padding;
+       rt->bounds[1][0] = cx + l + padding;
+       rt->bounds[0][1] = cy - l - padding;
+       rt->bounds[1][1] = cy + l + padding;
        
-       rt->scale = d + padding;
+       rt->scale = l + padding;
        
        csr_update_subsamples( rt );
 }
@@ -153,7 +284,10 @@ void simple_raster( csr_target *rt, vmf_vert tri[3] )
                
                for( u32 px = start_x; px <= end_x; px ++ )
                {
-                       csr_frag *frag = &rt->fragments[ (py * rt->y + px) * rt->num_samples ];
+                       u32 sample_index = (py * rt->y + px) * rt->num_samples;
+               
+                       void *frag = rt->colour + sample_index*rt->shader->stride;
+                       float *depth = &rt->depth[ sample_index ];
 
                        trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x );
                        
@@ -182,14 +316,10 @@ void simple_raster( csr_target *rt, vmf_vert tri[3] )
                                
                                float hit = (tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc) +16385.f;
                                
-                               if( hit > frag[i].depth )
+                               if( hit > depth[i] )
                                {
-                                       frag[i].depth = hit;
-                                       v3_muls( tri[0].co, bca, frag[i].co );
-                                       v3_muladds( frag[i].co, tri[1].co, bcb, frag[i].co );
-                                       v3_muladds( frag[i].co, tri[2].co, bcc, frag[i].co );
-                                       
-                                       // TODO: Same for normal map
+                                       depth[i] = hit;
+                                       rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
                                }
                        }
                }
@@ -203,6 +333,8 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr
 
        // Derive normal matrix
        m4x3_to_3x3( transform, normal );
+       
+       // NOTE: This isn't strictly necessary since CS:GO only uses uniform scaling.
        m3x3_inv_transpose( normal, normal );
 
        for( u32 i = 0; i < triangle_count; i ++ )
@@ -220,12 +352,11 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr
        }
 }
 
-void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst )
+void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst )
 {
        m4x3f transform = M4X3_IDENTITY;
        vmf_solid solid;
        vmf_vert tri[3];
-       vdf_node *ent_solid;
        boxf trf_bounds;
 
        u32 group_id = 0;
@@ -265,6 +396,7 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
                        if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) )
                                continue;
                        
+                       // TODO: heap-use-after-free
                        solidgen_push( &solid, brush );
                }
        }
@@ -294,11 +426,8 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
                        
                        if( compute_bounds_only )
                        {
-                               box_copy( mdl->bounds, trf_bounds );
-                               m4x3_transform_aabb( model, trf_bounds );
-                               
-                               // Join
-                               box_concat( rt->bounds, trf_bounds );
+                               map->models[ ent->user1 ].need_load = 1;
+                               m4x3_expand_aabb_point( model, rt->bounds, (v3f){0.f,0.f,0.f} );
                        }
                        else
                        {
@@ -321,12 +450,12 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
                        m4x3_identity( model );
                        vmf_entity_transform( ent, model );
                        
-                       draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, filter, transform, model );
+                       csr_vmf_render( rt, map, map->cache[ ent->user1 ].root, filter, transform, model );
                }
                else
                {
                        // Brush entity
-                       if( (ent_solid = vdf_next( ent, "solid", NULL )) )
+                       vdf_foreach( ent, "solid", ent_solid )
                        {
                                solidgen_push( &solid, ent_solid );
                        }
@@ -358,24 +487,28 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
        solidgen_ctx_free( &solid );
 }
 
+// Obsolete
 void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname )
 {
        char output[ 512 ];
        
        float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
        
+       float contrib = 1.f/(float)rt->num_samples;
+       
        for( int l = 0; l < rt->x; l ++ )
        {
                for( int x = 0; x < rt->y; x ++ )
                {
                        float *dst = &image[ (l*1024+x)*3 ];
-                       csr_frag *src = &rt->fragments[ ((1023-l)*1024+x)*rt->num_samples ];
+                       void *src = rt->colour + ((1023-l)*1024+x) * rt->num_samples * rt->shader->stride;
+                       
+                       v3_muls( (float *)src, contrib, dst );
                        
-                       v3_zero( dst );
-                       v3_muls( src[0].co, 1.f/(float)rt->num_samples, dst );
-                       v3_muladds( dst, src[1].co, 1.f/(float)rt->num_samples, dst );
-                       v3_muladds( dst, src[2].co, 1.f/(float)rt->num_samples, dst );
-                       v3_muladds( dst, src[3].co, 1.f/(float)rt->num_samples, dst );
+                       for( int j = 1; j < rt->num_samples; j ++ )
+                       {
+                               v3_muladds( dst, (float *)(src + j*rt->shader->stride), contrib, dst );
+                       }
                }
        }
        
@@ -388,3 +521,5 @@ void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subn
        
        free( image );
 }
+
+#endif