-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 );
+
+// 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
+{
+ 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[] =
{ -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;
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 );
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;
}
}
{
// 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];
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
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 );
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;
- rt->shader( frag[i].colour, tri, bca, bcb, bcc );
+ depth[i] = hit;
+ rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
}
}
}
}
}
-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;
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
{
solidgen_ctx_free( &solid );
}
+// Obsolete
void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname )
{
char output[ 512 ];
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_zero( dst );
- v3_muls( src[0].colour, contrib, dst );
+ v3_muls( (float *)src, contrib, dst );
for( int j = 1; j < rt->num_samples; j ++ )
{
- v3_muladds( dst, src[j].colour, contrib, dst );
+ v3_muladds( dst, (float *)(src + j*rt->shader->stride), contrib, dst );
}
}
}
free( image );
}
+
+#endif