fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / csrDraw.h
index 14c91c23fd1f1504902f5eeb1f7429239a7e84d2..a3f601e722575d887b965ccd1865f4f64f86ac8d 100644 (file)
--- a/csrDraw.h
+++ b/csrDraw.h
@@ -1,25 +1,77 @@
-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 );
+
+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
 {
-       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 +82,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,35 +185,32 @@ 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];
        
@@ -96,22 +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 simple_raster( csr_target *rt, vmf_vert tri[3] )
 {
        // Very very simplified rasterizing algorithm
@@ -152,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 );
                        
@@ -179,16 +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;
-                                       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_depth;
+                                       rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
                                }
                        }
                }
@@ -211,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;
@@ -263,6 +387,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 );
                }
        }
@@ -292,11 +417,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
                        {
@@ -306,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 );
@@ -319,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
                {
@@ -356,33 +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  )
+{
+       FILE *write_ptr;
+       
+       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 )
 {
-       char output[ 512 ];
+       float *image = (float *)csr_malloc( rt->x*rt->y*3*sizeof(float) );
        
-       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 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].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 );
+                       v3_muladds( dst, (float *)(src + k*rt->shader->stride), contrib, dst );
                }
        }
        
-       // 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_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 ++ )
+                       {
+                               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];
+       }
        
+       if( !csr_tga_write( path, rt->x, rt->y, nc, image ) )
+               csr_write_filerr( path );
+               
        free( image );
 }
+
+#endif