fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / csrDraw.h
index 00346e444491d36e736ec116242787d3613b25c9..a3f601e722575d887b965ccd1865f4f64f86ac8d 100644 (file)
--- a/csrDraw.h
+++ b/csrDraw.h
@@ -1,9 +1,96 @@
-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;
 
-typedef void (* csr_frag_shader)( v4f, vmf_vert[3], float, float, float );
+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 );
+
+void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc );
+void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset );
+
+
+// Implementation
+//=======================================================================================================================
+
+struct csr_shader
+{
+       u32 stride;
+       csr_frag_program frag;
+       csr_frag_clear clear;
+};
+
+struct csr_target
+{
+       void  *colour;
+       float *depth;
+       
+       u32 x, y; 
+       boxf bounds;
+       float scale;
+       
+       v2f subsamples[ 8 ];
+       int num_samples;
+       v2f *sample_src;
+       
+       csr_shader *shader;
+};
+
+struct csr_filter
+{
+       const char *visgroup;           // Limit to this visgroup only
+       const char *classname;          // Limit to this exact classname. will not draw world
+       
+       int compute_bounds_only;
+};
+
+enum EMSAA
+{
+       k_EMSAA_none,
+       k_EMSAA_2x2,
+       k_EMSAA_RGSS,
+       k_EMSAA_8R
+};
+
+#ifdef CSR_EXECUTABLE
 
 // MSAA patterns
 v2f csr_msaa_1[] =
@@ -53,49 +140,8 @@ v2f csr_msaa_8rook[] =
        { -0x0.3p0f,  0x0.5p0f }
 };
 
-struct csr_frag
-{      
-       v4f colour;
-       float depth;
-};
-
-struct csr_target
-{
-       csr_frag *fragments;
-       
-       u32 x, y; 
-       boxf bounds;
-       float scale;
-       
-       v2f subsamples[ 8 ];
-       int num_samples;
-       v2f *sample_src;
-       
-       csr_frag_shader shader;
-};
-
-void csr_use_program( csr_target *rt, csr_frag_shader shader )
-{
-       rt->shader = shader;
-}
-
-struct csr_filter
-{
-       const char *visgroup;           // Limit to this visgroup only
-       const char *classname;          // Limit to this exact classname. will not draw world
-       
-       int compute_bounds_only;
-};
-
-enum EMSAA
-{
-       k_EMSAA_none,
-       k_EMSAA_2x2,
-       k_EMSAA_RGSS,
-       k_EMSAA_8R
-};
 
-void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa )
+void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa, csr_shader *shader )
 {
        rt->x = x;
        rt->y = y;
@@ -124,7 +170,9 @@ void csr_create_target( csr_target *rt, u32 x, u32 y, EMSAA aa )
                        break;
        }
        
-       rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag)*rt->num_samples );
+       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 );
@@ -145,15 +193,16 @@ void csr_update_subsamples( csr_target *rt )
 
 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 ++ )
        {
-               v4_zero( rt->fragments[ i ].colour );
-               rt->fragments[i].depth = 0.f;
+               rt->shader->clear( rt->colour + i * rt->shader->stride );       
+               rt->depth[i] = 0.f;
        }
 }
 
@@ -161,6 +210,7 @@ void csr_auto_fit( csr_target *rt, float padding )
 {
        // Correct aspect ratio to be square
        float dx, dy, l, cx, cy;
+
        dx = rt->bounds[1][0] - rt->bounds[0][0];
        dy = rt->bounds[1][1] - rt->bounds[0][1];
        
@@ -179,29 +229,6 @@ void csr_auto_fit( csr_target *rt, float padding )
        csr_update_subsamples( rt );
 }
 
-void csr_write_txt( char const *path, const char *name, csr_target *rt  )
-{
-       FILE *write_ptr;
-       
-       write_ptr = fopen( path, "w" );
-       
-       fprintf( write_ptr, "\"%s\"\n\{\n", name );
-       fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name );
-       fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] );
-       fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] );
-       fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x );
-       fprintf( write_ptr, "}\n" );
-       
-       fclose( write_ptr );
-}
-
-void frag_gbuffer( v4f frag_colour, vmf_vert tri[3], float bca, float bcb, float bcc )
-{
-       v3_muls( tri[0].co, bca, frag_colour );
-       v3_muladds( frag_colour, tri[1].co, bcb, frag_colour );
-       v3_muladds( frag_colour, tri[2].co, bcc, frag_colour );
-}
-
 void simple_raster( csr_target *rt, vmf_vert tri[3] )
 {
        // Very very simplified rasterizing algorithm
@@ -242,7 +269,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 = ((rt->y-py-1)*rt->x+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 );
                        
@@ -269,12 +299,13 @@ void simple_raster( csr_target *rt, vmf_vert tri[3] )
                                bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
                                bca = 1.f - bcb - bcc;
                                
-                               float hit = (tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc) +16385.f;
+                               float hit = tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc;
+                               float hit_depth = hit + 16385.f;
                                
-                               if( hit > frag[i].depth )
+                               if( hit_depth > depth[i] && hit >= rt->bounds[0][2] && hit <= rt->bounds[1][2] )
                                {
-                                       frag[i].depth = hit;
-                                       rt->shader( frag[i].colour, tri, bca, bcb, bcc );
+                                       depth[i] = hit_depth;
+                                       rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
                                }
                        }
                }
@@ -288,8 +319,6 @@ 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 ++ )
@@ -299,15 +328,22 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr
                m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
                m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
                m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
+
                m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
                m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
                m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
                
+               v3_normalize( new_tri[0].nrm );
+               v3_normalize( new_tri[1].nrm );
+               v3_normalize( new_tri[2].nrm );
+               
+               m4x3_mulv( transform, triangles[0].origin, new_tri[0].origin );
+               
                simple_raster( rt, new_tri );
        }
 }
 
-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;
@@ -392,8 +428,7 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
                                        {
                                                v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ],   tri[j].co );
                                                v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
-                                               tri[j].xy[0] = 0.f;
-                                               tri[j].xy[1] = 0.f;
+                                               v3_zero( tri[j].origin );
                                        }
                                        
                                        csr_draw( rt, tri, 1, model );
@@ -405,7 +440,7 @@ 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
                {
@@ -442,37 +477,95 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
        solidgen_ctx_free( &solid );
 }
 
-void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname )
+void csr_write_filerr( const char *path )
+{
+       log_error( "File write error (No such file or directory): '%s'\n", path );
+}
+
+void csr_write_txt( char const *path, const char *name, csr_target *rt  )
 {
-       char output[ 512 ];
+       FILE *write_ptr;
        
-       float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
+       write_ptr = fopen( path, "w" );
+       
+       if( write_ptr )
+       {
+               fprintf( write_ptr, "\"%s\"\n\{\n", name );
+               fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name );
+               fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] );
+               fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] );
+               fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x );
+               fprintf( write_ptr, "}\n" );
+               
+               fclose( write_ptr );
+       }
+       else
+       {
+               csr_write_filerr( path );
+       }
+}
+
+// ALWAYS RGB32
+void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset )
+{
+       float *image = (float *)csr_malloc( rt->x*rt->y*3*sizeof(float) );
        
        float contrib = 1.f/(float)rt->num_samples;
        
-       for( int l = 0; l < rt->x; l ++ )
+       for( int i = 0; i < rt->x*rt->y; i ++ )
        {
-               for( int x = 0; x < rt->y; x ++ )
+               void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
+               float *dst = image + i*3;
+               
+               v3_zero( dst );
+               for( int k = 0; k < rt->num_samples; k ++ )
                {
-                       float *dst = &image[ (l*1024+x)*3 ];
-                       csr_frag *src = &rt->fragments[ ((1023-l)*1024+x)*rt->num_samples ];
-                       
-                       v3_zero( dst );
-                       v3_muls( src[0].colour, contrib, dst );
-                       
-                       for( int j = 1; j < rt->num_samples; j ++ )
+                       v3_muladds( dst, (float *)(src + k*rt->shader->stride), contrib, dst );
+               }
+       }
+       
+       if( !csr_32f_write( path, rt->x, rt->y, image ) )
+               csr_write_filerr( path );
+               
+       free( image );
+}
+
+// Save floating point buffer to tga. Must be in range (0-1)
+// Offset and stride are in bytes
+void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc )
+{
+       u8 *image = (u8 *)csr_malloc( rt->x*rt->y * 4 );
+       
+       float contrib = 255.f/(float)rt->num_samples;
+       
+       for( int i = 0; i < rt->x*rt->y; i ++ )
+       {
+               void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
+               u8 *dst = image + i*4;
+               
+               v4f accum = { 0.f, 0.f, 0.f, 0.f };
+               
+               for( int k = 0; k < rt->num_samples; k ++ )
+               {
+                       float *src_sample = (float *)(src + k*rt->shader->stride);
+               
+                       for( int j = 0; j < nc; j ++ )
                        {
-                               v3_muladds( dst, src[j].colour, contrib, dst );
+                               accum[ j ] += src_sample[ j ] * contrib;
                        }
                }
+               
+               // TODO: Clamp this value
+               dst[0] = accum[0];
+               dst[1] = accum[1];
+               dst[2] = accum[2];
+               dst[3] = accum[3];
        }
        
-       // Save position buffer
-       strcpy( output, basename );
-       strcat( output, "." );
-       strcat( output, subname );
-       strcat( output, "_position.pfm" );
-       csr_32f_write( output, rt->x, rt->y, image );   
-       
+       if( !csr_tga_write( path, rt->x, rt->y, nc, image ) )
+               csr_write_filerr( path );
+               
        free( image );
 }
+
+#endif