initial upload

This commit is contained in:
Ryan Fleury
2024-01-10 19:53:18 -08:00
commit a42ec6aeff
308 changed files with 162362 additions and 0 deletions
@@ -0,0 +1,66 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
//- GENERATED CODE
String8 r_d3d11_g_vshad_kind_source_table[] =
{
r_d3d11_g_rect_shader_src,
r_d3d11_g_blur_shader_src,
r_d3d11_g_mesh_shader_src,
r_d3d11_g_geo3dcomposite_shader_src,
r_d3d11_g_finalize_shader_src,
};
String8 r_d3d11_g_vshad_kind_source_name_table[] =
{
str8_lit_comp("r_d3d11_g_rect_shader_src"),
str8_lit_comp("r_d3d11_g_blur_shader_src"),
str8_lit_comp("r_d3d11_g_mesh_shader_src"),
str8_lit_comp("r_d3d11_g_geo3dcomposite_shader_src"),
str8_lit_comp("r_d3d11_g_finalize_shader_src"),
};
D3D11_INPUT_ELEMENT_DESC * r_d3d11_g_vshad_kind_elements_ptr_table[] =
{
r_d3d11_g_rect_ilay_elements,
0,
r_d3d11_g_mesh_ilay_elements,
0,
0,
};
U64 r_d3d11_g_vshad_kind_elements_count_table[] =
{
ArrayCount(r_d3d11_g_rect_ilay_elements) ,
0,
ArrayCount(r_d3d11_g_mesh_ilay_elements) ,
0,
0,
};
String8 r_d3d11_g_pshad_kind_source_table[] =
{
r_d3d11_g_rect_shader_src,
r_d3d11_g_blur_shader_src,
r_d3d11_g_mesh_shader_src,
r_d3d11_g_geo3dcomposite_shader_src,
r_d3d11_g_finalize_shader_src,
};
String8 r_d3d11_g_pshad_kind_source_name_table[] =
{
str8_lit_comp("r_d3d11_g_rect_shader_src"),
str8_lit_comp("r_d3d11_g_blur_shader_src"),
str8_lit_comp("r_d3d11_g_mesh_shader_src"),
str8_lit_comp("r_d3d11_g_geo3dcomposite_shader_src"),
str8_lit_comp("r_d3d11_g_finalize_shader_src"),
};
U64 r_d3d11_g_uniform_type_kind_size_table[] =
{
sizeof(R_D3D11_Uniforms_Rect),
sizeof(R_D3D11_Uniforms_Blur),
sizeof(R_D3D11_Uniforms_Mesh),
};
@@ -0,0 +1,464 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
//- GENERATED CODE
#ifndef RENDER_D3D11_META_H
#define RENDER_D3D11_META_H
typedef enum R_D3D11_VShadKind
{
R_D3D11_VShadKind_Rect,
R_D3D11_VShadKind_Blur,
R_D3D11_VShadKind_Mesh,
R_D3D11_VShadKind_Geo3DComposite,
R_D3D11_VShadKind_Finalize,
R_D3D11_VShadKind_COUNT
} R_D3D11_VShadKind;
typedef enum R_D3D11_PShadKind
{
R_D3D11_PShadKind_Rect,
R_D3D11_PShadKind_Blur,
R_D3D11_PShadKind_Mesh,
R_D3D11_PShadKind_Geo3DComposite,
R_D3D11_PShadKind_Finalize,
R_D3D11_PShadKind_COUNT
} R_D3D11_PShadKind;
typedef enum R_D3D11_UniformTypeKind
{
R_D3D11_UniformTypeKind_Rect,
R_D3D11_UniformTypeKind_Blur,
R_D3D11_UniformTypeKind_Mesh,
R_D3D11_UniformTypeKind_COUNT
} R_D3D11_UniformTypeKind;
read_only global String8 r_d3d11_g_rect_shader_src =
str8_lit_comp(
""
"\n"
"cbuffer Globals : register(b0)\n"
"{\n"
" float2 viewport_size_px;\n"
" float opacity;\n"
" row_major float4x4 texture_sample_channel_map;\n"
" float2 texture_t2d_size_px;\n"
" row_major float3x3 xform;\n"
" float2 xform_scale;\n"
"}\n"
"\n"
"struct CPU2Vertex\n"
"{\n"
" float4 dst_rect_px : POS;\n"
" float4 src_rect_px : TEX;\n"
" float4 color00 : COL0;\n"
" float4 color01 : COL1;\n"
" float4 color10 : COL2;\n"
" float4 color11 : COL3;\n"
" float4 corner_radii_px : CRAD;\n"
" float4 style_params : STY; // x: border_thickness_px, y: softness_px, z: omit_texture, w: unused\n"
" uint vertex_id : SV_VertexID;\n"
"};\n"
"\n"
"struct Vertex2Pixel\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 rect_half_size_px : PSIZE;\n"
" float2 texcoord_pct : TEX;\n"
" float2 cornercoord_pct : COLC;\n"
" float4 color00 : COL0;\n"
" float4 color01 : COL1;\n"
" float4 color10 : COL2;\n"
" float4 color11 : COL3;\n"
" float corner_radius_px : CRAD;\n"
" float border_thickness_px : BTHC;\n"
" float softness_px : SFT;\n"
" float omit_texture : OTX;\n"
"};\n"
"\n"
"Texture2D main_t2d : register(t0);\n"
"SamplerState main_sampler : register(s0);\n"
"\n"
"float rect_sdf(float2 sample_pos, float2 rect_half_size, float r)\n"
"{\n"
" return length(max(abs(sample_pos) - rect_half_size + r, 0.0)) - r;\n"
"}\n"
"\n"
"//- rjf: vertex shader\n"
"\n"
"Vertex2Pixel\n"
"vs_main(CPU2Vertex cpu2vertex)\n"
"{\n"
" //- rjf: unpack & xform rectangle src/dst vertices\n"
" float2 dst_p0_px = cpu2vertex.dst_rect_px.xy;\n"
" float2 dst_p1_px = cpu2vertex.dst_rect_px.zw;\n"
" float2 src_p0_px = cpu2vertex.src_rect_px.xy;\n"
" float2 src_p1_px = cpu2vertex.src_rect_px.zw;\n"
" float2 dst_size_px = abs(dst_p1_px - dst_p0_px);\n"
" \n"
" //- rjf: unpack style params\n"
" float border_thickness_px = cpu2vertex.style_params.x;\n"
" float softness_px = cpu2vertex.style_params.y;\n"
" float omit_texture = cpu2vertex.style_params.z;\n"
" \n"
" //- rjf: prep per-vertex arrays to sample from (p: position, t: texcoord, c: colorcoord, r: cornerradius)\n"
" float2 dst_p_verts_px[] =\n"
" {\n"
" mul(xform, float3(dst_p0_px.x, dst_p1_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),\n"
" mul(xform, float3(dst_p0_px.x, dst_p0_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),\n"
" mul(xform, float3(dst_p1_px.x, dst_p1_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),\n"
" mul(xform, float3(dst_p1_px.x, dst_p0_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),\n"
" };\n"
" float2 src_p_verts_pct[] =\n"
" {\n"
" float2(src_p0_px.x/texture_t2d_size_px.x, src_p1_px.y/texture_t2d_size_px.y),\n"
" float2(src_p0_px.x/texture_t2d_size_px.x, src_p0_px.y/texture_t2d_size_px.y),\n"
" float2(src_p1_px.x/texture_t2d_size_px.x, src_p1_px.y/texture_t2d_size_px.y),\n"
" float2(src_p1_px.x/texture_t2d_size_px.x, src_p0_px.y/texture_t2d_size_px.y),\n"
" };\n"
" float2 dst_c_verts_pct[] =\n"
" {\n"
" float2(0, 1),\n"
" float2(0, 0),\n"
" float2(1, 1),\n"
" float2(1, 0),\n"
" };\n"
" float dst_r_verts_px[] =\n"
" {\n"
" cpu2vertex.corner_radii_px.y,\n"
" cpu2vertex.corner_radii_px.x,\n"
" cpu2vertex.corner_radii_px.w,\n"
" cpu2vertex.corner_radii_px.z,\n"
" };\n"
" \n"
" // rjf: fill vertex -> pixel data\n"
" Vertex2Pixel vertex2pixel;\n"
" {\n"
" vertex2pixel.position.x = 2 * dst_p_verts_px[cpu2vertex.vertex_id].x / viewport_size_px.x - 1.f;\n"
" vertex2pixel.position.y = 2 * dst_p_verts_px[cpu2vertex.vertex_id].y / viewport_size_px.y - 1.f;\n"
" vertex2pixel.position.z = 0.f;\n"
" vertex2pixel.position.w = 1.f;\n"
" vertex2pixel.rect_half_size_px = dst_size_px/2 * xform_scale;\n"
" vertex2pixel.texcoord_pct = src_p_verts_pct[cpu2vertex.vertex_id];\n"
" vertex2pixel.cornercoord_pct = dst_c_verts_pct[cpu2vertex.vertex_id];\n"
" vertex2pixel.color00 = cpu2vertex.color00;\n"
" vertex2pixel.color01 = cpu2vertex.color01;\n"
" vertex2pixel.color10 = cpu2vertex.color10;\n"
" vertex2pixel.color11 = cpu2vertex.color11;\n"
" vertex2pixel.corner_radius_px = dst_r_verts_px[cpu2vertex.vertex_id];\n"
" vertex2pixel.border_thickness_px = border_thickness_px;\n"
" vertex2pixel.softness_px = softness_px;\n"
" vertex2pixel.omit_texture = omit_texture;\n"
" }\n"
" return vertex2pixel;\n"
"}\n"
"\n"
"//- rjf: pixel shader\n"
"\n"
"float4\n"
"ps_main(Vertex2Pixel vertex2pixel) : SV_TARGET\n"
"{\n"
" // rjf: blend corner colors to produce final tint\n"
" float4 top_color = (1-vertex2pixel.cornercoord_pct.x)*vertex2pixel.color00 + (vertex2pixel.cornercoord_pct.x)*vertex2pixel.color10;\n"
" float4 bot_color = (1-vertex2pixel.cornercoord_pct.x)*vertex2pixel.color01 + (vertex2pixel.cornercoord_pct.x)*vertex2pixel.color11;\n"
" float4 tint = (1-vertex2pixel.cornercoord_pct.y)*top_color + (vertex2pixel.cornercoord_pct.y)*bot_color;\n"
" \n"
" // rjf: sample texture\n"
" float4 albedo_sample = float4(1, 1, 1, 1);\n"
" if(vertex2pixel.omit_texture < 1)\n"
" {\n"
" albedo_sample = mul(main_t2d.Sample(main_sampler, vertex2pixel.texcoord_pct), texture_sample_channel_map);\n"
" }\n"
" \n"
" // rjf: determine SDF sample position\n"
" float2 sdf_sample_pos = float2((2*vertex2pixel.cornercoord_pct.x-1)*vertex2pixel.rect_half_size_px.x,\n"
" (2*vertex2pixel.cornercoord_pct.y-1)*vertex2pixel.rect_half_size_px.y);\n"
" \n"
" // rjf: sample for corners\n"
" float corner_sdf_s = rect_sdf(sdf_sample_pos,\n"
" vertex2pixel.rect_half_size_px - float2(vertex2pixel.softness_px*2.f, vertex2pixel.softness_px*2.f),\n"
" vertex2pixel.corner_radius_px);\n"
" float corner_sdf_t = 1-smoothstep(0, 2*vertex2pixel.softness_px, corner_sdf_s);\n"
" \n"
" // rjf: sample for borders\n"
" float border_sdf_s = rect_sdf(sdf_sample_pos,\n"
" vertex2pixel.rect_half_size_px - float2(vertex2pixel.softness_px*2.f, vertex2pixel.softness_px*2.f) - vertex2pixel.border_thickness_px,\n"
" max(vertex2pixel.corner_radius_px-vertex2pixel.border_thickness_px, 0));\n"
" float border_sdf_t = smoothstep(0, 2*vertex2pixel.softness_px, border_sdf_s);\n"
" if(vertex2pixel.border_thickness_px == 0)\n"
" {\n"
" border_sdf_t = 1;\n"
" }\n"
" \n"
" // rjf: form+return final color\n"
" float4 final_color = albedo_sample;\n"
" final_color *= tint;\n"
" final_color *= opacity;\n"
" final_color.a *= corner_sdf_t;\n"
" final_color.a *= border_sdf_t;\n"
" return final_color;\n"
"}\n"
""
);
read_only global String8 r_d3d11_g_blur_shader_src =
str8_lit_comp(
""
"\n"
"cbuffer Globals : register(b0)\n"
"{\n"
" float4 rect;\n"
" float2 viewport_size;\n"
" float blur_size;\n"
" float is_vertical;\n"
" float4 corner_radii_px;\n"
" float4 kernel[32];\n"
"}\n"
"\n"
"struct CPU2Vertex\n"
"{\n"
" uint vertex_id : SV_VertexID;\n"
"};\n"
"\n"
"struct Vertex2Pixel\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 texcoord : TEX;\n"
" float2 cornercoord : CRN;\n"
" float corner_radius : RAD;\n"
"};\n"
"\n"
"Texture2D stage_t2d : register(t0);\n"
"SamplerState stage_sampler : register(s0);\n"
"\n"
"float rect_sdf(float2 sample_pos, float2 rect_half_size, float r)\n"
"{\n"
" return length(max(abs(sample_pos) - rect_half_size + r, 0.0)) - r;\n"
"}\n"
"\n"
"//- rjf: vertex shader\n"
"\n"
"Vertex2Pixel\n"
"vs_main(CPU2Vertex c2v)\n"
"{\n"
" float4 vertex_positions__scrn[] =\n"
" {\n"
" float4(rect.x, rect.w, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),\n"
" float4(rect.x, rect.y, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),\n"
" float4(rect.z, rect.w, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),\n"
" float4(rect.z, rect.y, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),\n"
" };\n"
" float corner_radii__px[] =\n"
" {\n"
" corner_radii_px.y,\n"
" corner_radii_px.x,\n"
" corner_radii_px.w,\n"
" corner_radii_px.z,\n"
" };\n"
" float2 cornercoords__pct[] =\n"
" {\n"
" float2(0, 1),\n"
" float2(0, 0),\n"
" float2(1, 1),\n"
" float2(1, 0),\n"
" };\n"
" float4 vertex_position__scrn = vertex_positions__scrn[c2v.vertex_id];\n"
" float4 vertex_position__clip = float4(2*vertex_position__scrn.x/viewport_size.x - 1,\n"
" 2*vertex_position__scrn.y/viewport_size.y - 1,\n"
" 0, 1);\n"
" Vertex2Pixel v2p;\n"
" {\n"
" v2p.position = vertex_position__clip;\n"
" v2p.texcoord = float2(vertex_position__scrn.x/viewport_size.x, 1 - vertex_position__scrn.y/viewport_size.y);\n"
" v2p.cornercoord = cornercoords__pct[c2v.vertex_id];\n"
" v2p.corner_radius = corner_radii__px[c2v.vertex_id];\n"
" }\n"
" return v2p;\n"
"}\n"
"\n"
"//- rjf: pixel shader\n"
"\n"
"float4\n"
"ps_main(Vertex2Pixel v2p) : SV_TARGET\n"
"{\n"
" // rjf: blend weighted texture samples into color\n"
" float4 color = stage_t2d.Sample(stage_sampler, v2p.texcoord) * kernel[0].x;\n"
" color.a = kernel[0].x;\n"
" for(float i = 1; i < blur_size; i += 1)\n"
" {\n"
" float weight = ((float[4])kernel[uint(i)/4])[uint(i)%4];\n"
" float4 min_sample = stage_t2d.Sample(stage_sampler, v2p.texcoord - float2(!is_vertical*i/viewport_size.x, is_vertical*i/viewport_size.y));\n"
" float4 max_sample = stage_t2d.Sample(stage_sampler, v2p.texcoord + float2(!is_vertical*i/viewport_size.x, is_vertical*i/viewport_size.y));\n"
" min_sample.a = 1;\n"
" max_sample.a = 1;\n"
" color += min_sample*weight;\n"
" color += max_sample*weight;\n"
" }\n"
" \n"
" // rjf: determine SDF sample position\n"
" float2 rect_half_size = float2((rect.z-rect.x)/2, (rect.w-rect.y)/2);\n"
" float2 sdf_sample_pos = float2((2*v2p.cornercoord.x-1)*rect_half_size.x,\n"
" (2*v2p.cornercoord.y-1)*rect_half_size.y);\n"
" \n"
" // rjf: sample for corners\n"
" float corner_sdf_s = rect_sdf(sdf_sample_pos, rect_half_size - float2(2.f, 2.f), v2p.corner_radius);\n"
" float corner_sdf_t = 1-smoothstep(0, 2, corner_sdf_s);\n"
" \n"
" // rjf: weight output color by sdf\n"
" color.a *= corner_sdf_t;\n"
" \n"
" return color;\n"
"}\n"
""
);
read_only global String8 r_d3d11_g_mesh_shader_src =
str8_lit_comp(
""
"\n"
"cbuffer Uniforms : register(b0)\n"
"{\n"
" row_major float4x4 xform;\n"
"}\n"
"\n"
"struct CPU2Vertex\n"
"{\n"
" float3 position : POS;\n"
" float3 normal : NOR;\n"
" float2 texcoord : TEX;\n"
" float3 color : COL;\n"
"};\n"
"\n"
"struct Vertex2Pixel\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 texcoord : TEX;\n"
" float4 color : COL;\n"
"};\n"
"\n"
"Vertex2Pixel vs_main(CPU2Vertex c2v)\n"
"{\n"
" Vertex2Pixel v2p;\n"
" v2p.position = mul(float4(c2v.position, 1.f), xform);\n"
" v2p.texcoord = c2v.texcoord;\n"
" v2p.color = float4(c2v.color, 1.f);\n"
" return v2p;\n"
"}\n"
"\n"
"float4 ps_main(Vertex2Pixel v2p) : SV_TARGET\n"
"{\n"
" return v2p.color;\n"
"}\n"
""
);
read_only global String8 r_d3d11_g_geo3dcomposite_shader_src =
str8_lit_comp(
""
"\n"
"struct CPU2Vertex\n"
"{\n"
" uint vertex_id : SV_VertexID;\n"
"};\n"
"\n"
"struct Vertex2Pixel\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 texcoord : TEX;\n"
"};\n"
"\n"
"Texture2D stage_t2d : register(t0);\n"
"SamplerState stage_sampler : register(s0);\n"
"\n"
"//- rjf: vertex shader\n"
"\n"
"Vertex2Pixel\n"
"vs_main(CPU2Vertex c2v)\n"
"{\n"
" float4 vertex_positions__modl[] =\n"
" {\n"
" float4(0, 0, 0, 1),\n"
" float4(0, 1, 0, 1),\n"
" float4(1, 0, 0, 1),\n"
" float4(1, 1, 0, 1),\n"
" };\n"
" float4 vertex_position__modl = vertex_positions__modl[c2v.vertex_id];\n"
" float4 vertex_position__clip = float4(2*vertex_position__modl.x - 1, 2*vertex_position__modl.y - 1, 0, 1);\n"
" float2 texcoord = float2(vertex_position__modl.x, vertex_position__modl.y);\n"
" texcoord.y = 1-texcoord.y;\n"
" Vertex2Pixel v2p;\n"
" {\n"
" v2p.position = vertex_position__clip;\n"
" v2p.texcoord = texcoord;\n"
" }\n"
" return v2p;\n"
"}\n"
"\n"
"//- rjf: pixel shader\n"
"\n"
"float4\n"
"ps_main(Vertex2Pixel v2p) : SV_TARGET\n"
"{\n"
" float4 final_color = stage_t2d.Sample(stage_sampler, v2p.texcoord);\n"
" return final_color;\n"
"}\n"
""
);
read_only global String8 r_d3d11_g_finalize_shader_src =
str8_lit_comp(
""
"\n"
"struct CPU2Vertex\n"
"{\n"
" uint vertex_id : SV_VertexID;\n"
"};\n"
"\n"
"struct Vertex2Pixel\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 texcoord : TEX;\n"
"};\n"
"\n"
"Texture2D stage_t2d : register(t0);\n"
"SamplerState stage_sampler : register(s0);\n"
"\n"
"//- rjf: vertex shader\n"
"\n"
"Vertex2Pixel\n"
"vs_main(CPU2Vertex c2v)\n"
"{\n"
" float4 vertex_positions__modl[] =\n"
" {\n"
" float4(0, 0, 0, 1),\n"
" float4(0, 1, 0, 1),\n"
" float4(1, 0, 0, 1),\n"
" float4(1, 1, 0, 1),\n"
" };\n"
" float4 vertex_position__modl = vertex_positions__modl[c2v.vertex_id];\n"
" float4 vertex_position__clip = float4(2*vertex_position__modl.x - 1, 2*vertex_position__modl.y - 1, 0, 1);\n"
" float2 texcoord = float2(vertex_position__modl.x, vertex_position__modl.y);\n"
" texcoord.y = 1-texcoord.y;\n"
" Vertex2Pixel v2p;\n"
" {\n"
" v2p.position = vertex_position__clip;\n"
" v2p.texcoord = texcoord;\n"
" }\n"
" return v2p;\n"
"}\n"
"\n"
"//- rjf: pixel shader\n"
"\n"
"float4\n"
"ps_main(Vertex2Pixel v2p) : SV_TARGET\n"
"{\n"
" float4 final_color = stage_t2d.Sample(stage_sampler, v2p.texcoord);\n"
" final_color.a = 1;\n"
" return final_color;\n"
"}\n"
""
);
#endif // RENDER_D3D11_META_H
File diff suppressed because it is too large Load Diff
+169
View File
@@ -0,0 +1,169 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef RENDER_D3D11_H
#define RENDER_D3D11_H
#include <windows.h>
#include <d3d11_1.h>
#include <d3dcompiler.h>
#pragma comment(lib, "user32")
#pragma comment(lib, "d3d11")
#pragma comment(lib, "d3dcompiler")
////////////////////////////////
//~ rjf: Generated Code
#include "generated/render_d3d11.meta.h"
////////////////////////////////
//~ rjf: C-side Shader Types
struct R_D3D11_Uniforms_Rect
{
Vec2F32 viewport_size;
F32 opacity;
F32 _padding0_;
Vec4F32 texture_sample_channel_map[4];
Vec2F32 texture_t2d_size;
Vec2F32 translate;
Vec4F32 xform[3];
Vec2F32 xform_scale;
};
struct R_D3D11_Uniforms_Blur
{
Rng2F32 rect;
Vec2F32 viewport_size;
F32 blur_size;
F32 is_vertical;
Vec4F32 corner_radii;
Vec4F32 kernel[32];
};
struct R_D3D11_Uniforms_Mesh
{
Mat4x4F32 xform;
};
////////////////////////////////
//~ rjf: Main State Types
struct R_D3D11_Tex2D
{
R_D3D11_Tex2D *next;
U64 generation;
ID3D11Texture2D *texture;
ID3D11ShaderResourceView *view;
R_Tex2DKind kind;
Vec2S32 size;
R_Tex2DFormat format;
};
struct R_D3D11_Buffer
{
R_D3D11_Buffer *next;
U64 generation;
ID3D11Buffer *buffer;
R_BufferKind kind;
U64 size;
};
struct R_D3D11_Window
{
R_D3D11_Window *next;
U64 generation;
// rjf: swapchain/framebuffer
IDXGISwapChain1 *swapchain;
ID3D11Texture2D *framebuffer;
ID3D11RenderTargetView *framebuffer_rtv;
// rjf: staging buffer
ID3D11Texture2D *stage_color;
ID3D11RenderTargetView *stage_color_rtv;
ID3D11ShaderResourceView *stage_color_srv;
ID3D11Texture2D *stage_scratch_color;
ID3D11RenderTargetView *stage_scratch_color_rtv;
ID3D11ShaderResourceView *stage_scratch_color_srv;
// rjf: geo3d buffer
ID3D11Texture2D *geo3d_color;
ID3D11RenderTargetView *geo3d_color_rtv;
ID3D11ShaderResourceView *geo3d_color_srv;
ID3D11Texture2D *geo3d_depth;
ID3D11DepthStencilView *geo3d_depth_dsv;
ID3D11ShaderResourceView *geo3d_depth_srv;
// rjf: last state
Vec2S32 last_resolution;
};
struct R_D3D11_FlushBuffer
{
R_D3D11_FlushBuffer *next;
ID3D11Buffer *buffer;
};
struct R_D3D11_State
{
// rjf: state
Arena *arena;
R_D3D11_Window *first_free_window;
R_D3D11_Tex2D *first_free_tex2d;
R_D3D11_Buffer *first_free_buffer;
R_D3D11_Tex2D *first_to_free_tex2d;
R_D3D11_Buffer *first_to_free_buffer;
OS_Handle device_rw_mutex;
// rjf: base d3d11 objects
ID3D11Device *base_device;
ID3D11DeviceContext *base_device_ctx;
ID3D11Device1 *device;
ID3D11DeviceContext1 *device_ctx;
IDXGIDevice1 *dxgi_device;
IDXGIAdapter *dxgi_adapter;
IDXGIFactory2 *dxgi_factory;
ID3D11RasterizerState1 *main_rasterizer;
ID3D11BlendState *main_blend_state;
ID3D11SamplerState *samplers[R_Tex2DSampleKind_COUNT];
ID3D11DepthStencilState *noop_depth_stencil;
ID3D11DepthStencilState *plain_depth_stencil;
ID3D11Buffer *instance_scratch_buffer_64kb;
// rjf: backups
R_Handle backup_texture;
// rjf: vertex shaders
ID3D11VertexShader *vshads[R_D3D11_VShadKind_COUNT];
ID3D11InputLayout *ilays[R_D3D11_VShadKind_COUNT];
ID3D11PixelShader *pshads[R_D3D11_PShadKind_COUNT];
ID3D11Buffer *uniform_type_kind_buffers[R_D3D11_UniformTypeKind_COUNT];
// rjf: buffers to flush at subsequent frame
Arena *buffer_flush_arena;
R_D3D11_FlushBuffer *first_buffer_to_flush;
R_D3D11_FlushBuffer *last_buffer_to_flush;
};
////////////////////////////////
//~ rjf: Globals
global R_D3D11_State *r_d3d11_state = 0;
global R_D3D11_Window r_d3d11_window_nil = {&r_d3d11_window_nil};
global R_D3D11_Tex2D r_d3d11_tex2d_nil = {&r_d3d11_tex2d_nil};
global R_D3D11_Buffer r_d3d11_buffer_nil = {&r_d3d11_buffer_nil};
////////////////////////////////
//~ rjf: Helpers
internal R_D3D11_Window *r_d3d11_window_from_handle(R_Handle handle);
internal R_Handle r_d3d11_handle_from_window(R_D3D11_Window *window);
internal R_D3D11_Tex2D *r_d3d11_tex2d_from_handle(R_Handle handle);
internal R_Handle r_d3d11_handle_from_tex2d(R_D3D11_Tex2D *texture);
internal R_D3D11_Buffer *r_d3d11_buffer_from_handle(R_Handle handle);
internal R_Handle r_d3d11_handle_from_buffer(R_D3D11_Buffer *buffer);
internal ID3D11Buffer *r_d3d11_instance_buffer_from_size(U64 size);
#endif // RENDER_D3D11_H
+525
View File
@@ -0,0 +1,525 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ rjf: Pipeline Tables
@table(name, source, ilay_table)
R_D3D11_VShadTable:
{
{Rect r_d3d11_g_rect_shader_src r_d3d11_g_rect_ilay_elements }
{Blur r_d3d11_g_blur_shader_src 0 }
{Mesh r_d3d11_g_mesh_shader_src r_d3d11_g_mesh_ilay_elements }
{Geo3DComposite r_d3d11_g_geo3dcomposite_shader_src 0 }
{Finalize r_d3d11_g_finalize_shader_src 0 }
}
@table(name, source)
R_D3D11_PShadTable:
{
{Rect r_d3d11_g_rect_shader_src }
{Blur r_d3d11_g_blur_shader_src }
{Mesh r_d3d11_g_mesh_shader_src }
{Geo3DComposite r_d3d11_g_geo3dcomposite_shader_src }
{Finalize r_d3d11_g_finalize_shader_src }
}
@table(name)
R_D3D11_UniformTypeTable:
{
{Rect}
{Blur}
{Mesh}
}
////////////////////////////////
//~ rjf: UI Rectangle Shaders
@embed_string r_d3d11_g_rect_shader_src:
"""
cbuffer Globals : register(b0)
{
float2 viewport_size_px;
float opacity;
row_major float4x4 texture_sample_channel_map;
float2 texture_t2d_size_px;
row_major float3x3 xform;
float2 xform_scale;
}
struct CPU2Vertex
{
float4 dst_rect_px : POS;
float4 src_rect_px : TEX;
float4 color00 : COL0;
float4 color01 : COL1;
float4 color10 : COL2;
float4 color11 : COL3;
float4 corner_radii_px : CRAD;
float4 style_params : STY; // x: border_thickness_px, y: softness_px, z: omit_texture, w: unused
uint vertex_id : SV_VertexID;
};
struct Vertex2Pixel
{
float4 position : SV_POSITION;
float2 rect_half_size_px : PSIZE;
float2 texcoord_pct : TEX;
float2 cornercoord_pct : COLC;
float4 color00 : COL0;
float4 color01 : COL1;
float4 color10 : COL2;
float4 color11 : COL3;
float corner_radius_px : CRAD;
float border_thickness_px : BTHC;
float softness_px : SFT;
float omit_texture : OTX;
};
Texture2D main_t2d : register(t0);
SamplerState main_sampler : register(s0);
float rect_sdf(float2 sample_pos, float2 rect_half_size, float r)
{
return length(max(abs(sample_pos) - rect_half_size + r, 0.0)) - r;
}
//- rjf: vertex shader
Vertex2Pixel
vs_main(CPU2Vertex cpu2vertex)
{
//- rjf: unpack & xform rectangle src/dst vertices
float2 dst_p0_px = cpu2vertex.dst_rect_px.xy;
float2 dst_p1_px = cpu2vertex.dst_rect_px.zw;
float2 src_p0_px = cpu2vertex.src_rect_px.xy;
float2 src_p1_px = cpu2vertex.src_rect_px.zw;
float2 dst_size_px = abs(dst_p1_px - dst_p0_px);
//- rjf: unpack style params
float border_thickness_px = cpu2vertex.style_params.x;
float softness_px = cpu2vertex.style_params.y;
float omit_texture = cpu2vertex.style_params.z;
//- rjf: prep per-vertex arrays to sample from (p: position, t: texcoord, c: colorcoord, r: cornerradius)
float2 dst_p_verts_px[] =
{
mul(xform, float3(dst_p0_px.x, dst_p1_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
mul(xform, float3(dst_p0_px.x, dst_p0_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
mul(xform, float3(dst_p1_px.x, dst_p1_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
mul(xform, float3(dst_p1_px.x, dst_p0_px.y, 1)).xy * float2(1, -1) + float2(0, viewport_size_px.y),
};
float2 src_p_verts_pct[] =
{
float2(src_p0_px.x/texture_t2d_size_px.x, src_p1_px.y/texture_t2d_size_px.y),
float2(src_p0_px.x/texture_t2d_size_px.x, src_p0_px.y/texture_t2d_size_px.y),
float2(src_p1_px.x/texture_t2d_size_px.x, src_p1_px.y/texture_t2d_size_px.y),
float2(src_p1_px.x/texture_t2d_size_px.x, src_p0_px.y/texture_t2d_size_px.y),
};
float2 dst_c_verts_pct[] =
{
float2(0, 1),
float2(0, 0),
float2(1, 1),
float2(1, 0),
};
float dst_r_verts_px[] =
{
cpu2vertex.corner_radii_px.y,
cpu2vertex.corner_radii_px.x,
cpu2vertex.corner_radii_px.w,
cpu2vertex.corner_radii_px.z,
};
// rjf: fill vertex -> pixel data
Vertex2Pixel vertex2pixel;
{
vertex2pixel.position.x = 2 * dst_p_verts_px[cpu2vertex.vertex_id].x / viewport_size_px.x - 1.f;
vertex2pixel.position.y = 2 * dst_p_verts_px[cpu2vertex.vertex_id].y / viewport_size_px.y - 1.f;
vertex2pixel.position.z = 0.f;
vertex2pixel.position.w = 1.f;
vertex2pixel.rect_half_size_px = dst_size_px/2 * xform_scale;
vertex2pixel.texcoord_pct = src_p_verts_pct[cpu2vertex.vertex_id];
vertex2pixel.cornercoord_pct = dst_c_verts_pct[cpu2vertex.vertex_id];
vertex2pixel.color00 = cpu2vertex.color00;
vertex2pixel.color01 = cpu2vertex.color01;
vertex2pixel.color10 = cpu2vertex.color10;
vertex2pixel.color11 = cpu2vertex.color11;
vertex2pixel.corner_radius_px = dst_r_verts_px[cpu2vertex.vertex_id];
vertex2pixel.border_thickness_px = border_thickness_px;
vertex2pixel.softness_px = softness_px;
vertex2pixel.omit_texture = omit_texture;
}
return vertex2pixel;
}
//- rjf: pixel shader
float4
ps_main(Vertex2Pixel vertex2pixel) : SV_TARGET
{
// rjf: blend corner colors to produce final tint
float4 top_color = (1-vertex2pixel.cornercoord_pct.x)*vertex2pixel.color00 + (vertex2pixel.cornercoord_pct.x)*vertex2pixel.color10;
float4 bot_color = (1-vertex2pixel.cornercoord_pct.x)*vertex2pixel.color01 + (vertex2pixel.cornercoord_pct.x)*vertex2pixel.color11;
float4 tint = (1-vertex2pixel.cornercoord_pct.y)*top_color + (vertex2pixel.cornercoord_pct.y)*bot_color;
// rjf: sample texture
float4 albedo_sample = float4(1, 1, 1, 1);
if(vertex2pixel.omit_texture < 1)
{
albedo_sample = mul(main_t2d.Sample(main_sampler, vertex2pixel.texcoord_pct), texture_sample_channel_map);
}
// rjf: determine SDF sample position
float2 sdf_sample_pos = float2((2*vertex2pixel.cornercoord_pct.x-1)*vertex2pixel.rect_half_size_px.x,
(2*vertex2pixel.cornercoord_pct.y-1)*vertex2pixel.rect_half_size_px.y);
// rjf: sample for corners
float corner_sdf_s = rect_sdf(sdf_sample_pos,
vertex2pixel.rect_half_size_px - float2(vertex2pixel.softness_px*2.f, vertex2pixel.softness_px*2.f),
vertex2pixel.corner_radius_px);
float corner_sdf_t = 1-smoothstep(0, 2*vertex2pixel.softness_px, corner_sdf_s);
// rjf: sample for borders
float border_sdf_s = rect_sdf(sdf_sample_pos,
vertex2pixel.rect_half_size_px - float2(vertex2pixel.softness_px*2.f, vertex2pixel.softness_px*2.f) - vertex2pixel.border_thickness_px,
max(vertex2pixel.corner_radius_px-vertex2pixel.border_thickness_px, 0));
float border_sdf_t = smoothstep(0, 2*vertex2pixel.softness_px, border_sdf_s);
if(vertex2pixel.border_thickness_px == 0)
{
border_sdf_t = 1;
}
// rjf: form+return final color
float4 final_color = albedo_sample;
final_color *= tint;
final_color *= opacity;
final_color.a *= corner_sdf_t;
final_color.a *= border_sdf_t;
return final_color;
}
"""
////////////////////////////////
//~ rjf: Blur Shaders
@embed_string r_d3d11_g_blur_shader_src:
"""
cbuffer Globals : register(b0)
{
float4 rect;
float2 viewport_size;
float blur_size;
float is_vertical;
float4 corner_radii_px;
float4 kernel[32];
}
struct CPU2Vertex
{
uint vertex_id : SV_VertexID;
};
struct Vertex2Pixel
{
float4 position : SV_POSITION;
float2 texcoord : TEX;
float2 cornercoord : CRN;
float corner_radius : RAD;
};
Texture2D stage_t2d : register(t0);
SamplerState stage_sampler : register(s0);
float rect_sdf(float2 sample_pos, float2 rect_half_size, float r)
{
return length(max(abs(sample_pos) - rect_half_size + r, 0.0)) - r;
}
//- rjf: vertex shader
Vertex2Pixel
vs_main(CPU2Vertex c2v)
{
float4 vertex_positions__scrn[] =
{
float4(rect.x, rect.w, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),
float4(rect.x, rect.y, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),
float4(rect.z, rect.w, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),
float4(rect.z, rect.y, 0, 1) * float4(1, -1, 1, 1) + float4(0, viewport_size.y, 0, 0),
};
float corner_radii__px[] =
{
corner_radii_px.y,
corner_radii_px.x,
corner_radii_px.w,
corner_radii_px.z,
};
float2 cornercoords__pct[] =
{
float2(0, 1),
float2(0, 0),
float2(1, 1),
float2(1, 0),
};
float4 vertex_position__scrn = vertex_positions__scrn[c2v.vertex_id];
float4 vertex_position__clip = float4(2*vertex_position__scrn.x/viewport_size.x - 1,
2*vertex_position__scrn.y/viewport_size.y - 1,
0, 1);
Vertex2Pixel v2p;
{
v2p.position = vertex_position__clip;
v2p.texcoord = float2(vertex_position__scrn.x/viewport_size.x, 1 - vertex_position__scrn.y/viewport_size.y);
v2p.cornercoord = cornercoords__pct[c2v.vertex_id];
v2p.corner_radius = corner_radii__px[c2v.vertex_id];
}
return v2p;
}
//- rjf: pixel shader
float4
ps_main(Vertex2Pixel v2p) : SV_TARGET
{
// rjf: blend weighted texture samples into color
float4 color = stage_t2d.Sample(stage_sampler, v2p.texcoord) * kernel[0].x;
color.a = kernel[0].x;
for(float i = 1; i < blur_size; i += 1)
{
float weight = ((float[4])kernel[uint(i)/4])[uint(i)%4];
float4 min_sample = stage_t2d.Sample(stage_sampler, v2p.texcoord - float2(!is_vertical*i/viewport_size.x, is_vertical*i/viewport_size.y));
float4 max_sample = stage_t2d.Sample(stage_sampler, v2p.texcoord + float2(!is_vertical*i/viewport_size.x, is_vertical*i/viewport_size.y));
min_sample.a = 1;
max_sample.a = 1;
color += min_sample*weight;
color += max_sample*weight;
}
// rjf: determine SDF sample position
float2 rect_half_size = float2((rect.z-rect.x)/2, (rect.w-rect.y)/2);
float2 sdf_sample_pos = float2((2*v2p.cornercoord.x-1)*rect_half_size.x,
(2*v2p.cornercoord.y-1)*rect_half_size.y);
// rjf: sample for corners
float corner_sdf_s = rect_sdf(sdf_sample_pos, rect_half_size - float2(2.f, 2.f), v2p.corner_radius);
float corner_sdf_t = 1-smoothstep(0, 2, corner_sdf_s);
// rjf: weight output color by sdf
color.a *= corner_sdf_t;
return color;
}
"""
////////////////////////////////
//~ rjf: Mesh Shaders
@embed_string r_d3d11_g_mesh_shader_src:
"""
cbuffer Uniforms : register(b0)
{
row_major float4x4 xform;
}
struct CPU2Vertex
{
float3 position : POS;
float3 normal : NOR;
float2 texcoord : TEX;
float3 color : COL;
};
struct Vertex2Pixel
{
float4 position : SV_POSITION;
float2 texcoord : TEX;
float4 color : COL;
};
Vertex2Pixel vs_main(CPU2Vertex c2v)
{
Vertex2Pixel v2p;
v2p.position = mul(float4(c2v.position, 1.f), xform);
v2p.texcoord = c2v.texcoord;
v2p.color = float4(c2v.color, 1.f);
return v2p;
}
float4 ps_main(Vertex2Pixel v2p) : SV_TARGET
{
return v2p.color;
}
""";
////////////////////////////////
//~ rjf: Geo3D Composition Shaders
@embed_string r_d3d11_g_geo3dcomposite_shader_src:
"""
struct CPU2Vertex
{
uint vertex_id : SV_VertexID;
};
struct Vertex2Pixel
{
float4 position : SV_POSITION;
float2 texcoord : TEX;
};
Texture2D stage_t2d : register(t0);
SamplerState stage_sampler : register(s0);
//- rjf: vertex shader
Vertex2Pixel
vs_main(CPU2Vertex c2v)
{
float4 vertex_positions__modl[] =
{
float4(0, 0, 0, 1),
float4(0, 1, 0, 1),
float4(1, 0, 0, 1),
float4(1, 1, 0, 1),
};
float4 vertex_position__modl = vertex_positions__modl[c2v.vertex_id];
float4 vertex_position__clip = float4(2*vertex_position__modl.x - 1, 2*vertex_position__modl.y - 1, 0, 1);
float2 texcoord = float2(vertex_position__modl.x, vertex_position__modl.y);
texcoord.y = 1-texcoord.y;
Vertex2Pixel v2p;
{
v2p.position = vertex_position__clip;
v2p.texcoord = texcoord;
}
return v2p;
}
//- rjf: pixel shader
float4
ps_main(Vertex2Pixel v2p) : SV_TARGET
{
float4 final_color = stage_t2d.Sample(stage_sampler, v2p.texcoord);
return final_color;
}
"""
////////////////////////////////
//~ rjf: Finalize Shaders
@embed_string r_d3d11_g_finalize_shader_src:
"""
struct CPU2Vertex
{
uint vertex_id : SV_VertexID;
};
struct Vertex2Pixel
{
float4 position : SV_POSITION;
float2 texcoord : TEX;
};
Texture2D stage_t2d : register(t0);
SamplerState stage_sampler : register(s0);
//- rjf: vertex shader
Vertex2Pixel
vs_main(CPU2Vertex c2v)
{
float4 vertex_positions__modl[] =
{
float4(0, 0, 0, 1),
float4(0, 1, 0, 1),
float4(1, 0, 0, 1),
float4(1, 1, 0, 1),
};
float4 vertex_position__modl = vertex_positions__modl[c2v.vertex_id];
float4 vertex_position__clip = float4(2*vertex_position__modl.x - 1, 2*vertex_position__modl.y - 1, 0, 1);
float2 texcoord = float2(vertex_position__modl.x, vertex_position__modl.y);
texcoord.y = 1-texcoord.y;
Vertex2Pixel v2p;
{
v2p.position = vertex_position__clip;
v2p.texcoord = texcoord;
}
return v2p;
}
//- rjf: pixel shader
float4
ps_main(Vertex2Pixel v2p) : SV_TARGET
{
float4 final_color = stage_t2d.Sample(stage_sampler, v2p.texcoord);
final_color.a = 1;
return final_color;
}
"""
////////////////////////////////
//~ rjf: Table Generators
@table_gen_enum
R_D3D11_VShadKind:
{
@expand(R_D3D11_VShadTable a) `R_D3D11_VShadKind_$(a.name),`;
`R_D3D11_VShadKind_COUNT`;
}
@table_gen_enum
R_D3D11_PShadKind:
{
@expand(R_D3D11_PShadTable a) `R_D3D11_PShadKind_$(a.name),`;
`R_D3D11_PShadKind_COUNT`;
}
@table_gen_enum
R_D3D11_UniformTypeKind:
{
@expand(R_D3D11_UniformTypeTable a) `R_D3D11_UniformTypeKind_$(a.name),`;
`R_D3D11_UniformTypeKind_COUNT`;
}
@c_file @table_gen_data(type:String8, fallback:`{0}`)
r_d3d11_g_vshad_kind_source_table:
{
@expand(R_D3D11_VShadTable a) `$(a.source),`;
}
@c_file @table_gen_data(type:String8, fallback:`{0}`)
r_d3d11_g_vshad_kind_source_name_table:
{
@expand(R_D3D11_VShadTable a) `str8_lit_comp("$(a.source)"),`;
}
@c_file @table_gen_data(type:`D3D11_INPUT_ELEMENT_DESC *`, fallback:`0`)
r_d3d11_g_vshad_kind_elements_ptr_table:
{
@expand(R_D3D11_VShadTable a) `$(a.ilay_table),`;
}
@c_file @table_gen_data(type:U64, fallback:`0`)
r_d3d11_g_vshad_kind_elements_count_table:
{
@expand(R_D3D11_VShadTable a) `$(a.ilay_table != 0 -> "ArrayCount("..a.ilay_table..")") $(a.ilay_table == 0 -> "0"),`;
}
@c_file @table_gen_data(type:String8, fallback:`{0}`)
r_d3d11_g_pshad_kind_source_table:
{
@expand(R_D3D11_PShadTable a) `$(a.source),`;
}
@c_file @table_gen_data(type:String8, fallback:`{0}`)
r_d3d11_g_pshad_kind_source_name_table:
{
@expand(R_D3D11_PShadTable a) `str8_lit_comp("$(a.source)"),`;
}
@c_file @table_gen_data(type:U64, fallback:`0`)
r_d3d11_g_uniform_type_kind_size_table:
{
@expand(R_D3D11_UniformTypeTable a) `sizeof(R_D3D11_Uniforms_$(a.name)),`;
}
+16
View File
@@ -0,0 +1,16 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#define SUPPLEMENT_UNIT 1
#define OS_FEATURE_GRAPHICAL 1
#include "base/base_inc.h"
#include "os/os_inc.h"
#include "render/render_core.h"
#include "render_d3d11.h"
#include "render_d3d11.cpp"
#include "base/base_inc.c"
#include "os/os_inc.c"
#include "render/render_core.c"
+12
View File
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
//- GENERATED CODE
U64 r_pass_kind_params_size_table[] =
{
sizeof(R_PassParams_UI),
sizeof(R_PassParams_Blur),
sizeof(R_PassParams_Geo3D),
};
+96
View File
@@ -0,0 +1,96 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
//- GENERATED CODE
#ifndef RENDER_META_H
#define RENDER_META_H
typedef enum R_Tex2DFormat
{
R_Tex2DFormat_R8,
R_Tex2DFormat_RGBA8,
R_Tex2DFormat_BGRA8,
R_Tex2DFormat_COUNT
} R_Tex2DFormat;
typedef enum R_Tex2DKind
{
R_Tex2DKind_Static,
R_Tex2DKind_Dynamic,
R_Tex2DKind_COUNT
} R_Tex2DKind;
typedef enum R_Tex2DSampleKind
{
R_Tex2DSampleKind_Nearest,
R_Tex2DSampleKind_Linear,
R_Tex2DSampleKind_COUNT
} R_Tex2DSampleKind;
typedef enum R_GeoTopologyKind
{
R_GeoTopologyKind_Lines,
R_GeoTopologyKind_LineStrip,
R_GeoTopologyKind_Triangles,
R_GeoTopologyKind_TriangleStrip,
R_GeoTopologyKind_COUNT
} R_GeoTopologyKind;
typedef enum R_BufferKind
{
R_BufferKind_Static,
R_BufferKind_Dynamic,
R_BufferKind_COUNT
} R_BufferKind;
typedef enum R_PassKind
{
R_PassKind_UI,
R_PassKind_Blur,
R_PassKind_Geo3D,
R_PassKind_COUNT
} R_PassKind;
String8 r_tex2d_format_display_string_table[] =
{
str8_lit_comp("R8"),
str8_lit_comp("RGBA8"),
str8_lit_comp("BGRA8"),
};
U8 r_tex2d_format_bytes_per_pixel_table[] =
{
1,
4,
4,
};
String8 r_tex2d_kind_display_string_table[] =
{
str8_lit_comp("Static"),
str8_lit_comp("Dynamic"),
};
String8 r_tex2d_sample_kind_display_string_table[] =
{
str8_lit_comp("Nearest"),
str8_lit_comp("Linear"),
};
String8 r_pass_kind_display_string_table[] =
{
str8_lit_comp("UI"),
str8_lit_comp("Blur"),
str8_lit_comp("Geo3D"),
};
U8 r_pass_kind_batch_table[] =
{
1,
0,
1,
};
#endif // RENDER_META_H
+77
View File
@@ -0,0 +1,77 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ rjf: Generated Code
#include "generated/render.meta.c"
////////////////////////////////
//~ rjf: Basic Type Functions
internal R_Handle
r_handle_zero(void)
{
R_Handle handle = {0};
return handle;
}
internal B32
r_handle_match(R_Handle a, R_Handle b)
{
return a.u64[0] == b.u64[0] && a.u64[1] == b.u64[1];
}
////////////////////////////////
//~ rjf: Batch Type Functions
internal R_BatchList
r_batch_list_make(U64 instance_size)
{
R_BatchList list = {0};
list.bytes_per_inst = instance_size;
return list;
}
internal void *
r_batch_list_push_inst(Arena *arena, R_BatchList *list, U64 batch_inst_cap)
{
void *inst = 0;
{
R_BatchNode *n = list->last;
if(n == 0 || n->v.byte_count+list->bytes_per_inst > n->v.byte_cap)
{
n = push_array(arena, R_BatchNode, 1);
n->v.byte_cap = batch_inst_cap*list->bytes_per_inst;
n->v.v = push_array_no_zero(arena, U8, n->v.byte_cap);
SLLQueuePush(list->first, list->last, n);
list->batch_count += 1;
}
inst = n->v.v + n->v.byte_count;
n->v.byte_count += list->bytes_per_inst;
list->byte_count += list->bytes_per_inst;
}
return inst;
}
////////////////////////////////
//~ rjf: Pass Type Functions
internal R_Pass *
r_pass_from_kind(Arena *arena, R_PassList *list, R_PassKind kind)
{
R_PassNode *n = list->last;
if(!r_pass_kind_batch_table[kind])
{
n = 0;
}
if(n == 0 || n->v.kind != kind)
{
n = push_array(arena, R_PassNode, 1);
SLLQueuePush(list->first, list->last, n);
list->count += 1;
n->v.kind = kind;
n->v.params = push_array(arena, U8, r_pass_kind_params_size_table[kind]);
}
return &n->v;
}
+299
View File
@@ -0,0 +1,299 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef RENDER_CORE_H
#define RENDER_CORE_H
#define r_hook C_LINKAGE
////////////////////////////////
//~ rjf: Generated Code
#include "generated/render.meta.h"
////////////////////////////////
//~ rjf: Enums
typedef U32 R_GeoVertexFlags;
enum
{
R_GeoVertexFlag_TexCoord = (1<<0),
R_GeoVertexFlag_Normals = (1<<1),
R_GeoVertexFlag_RGB = (1<<2),
R_GeoVertexFlag_RGBA = (1<<3),
};
////////////////////////////////
//~ rjf: Handle Type
typedef union R_Handle R_Handle;
union R_Handle
{
U64 u64[2];
U32 u32[4];
U16 u16[8];
};
////////////////////////////////
//~ rjf: Instance Types
typedef struct R_Rect2DInst R_Rect2DInst;
struct R_Rect2DInst
{
Rng2F32 dst;
Rng2F32 src;
Vec4F32 colors[Corner_COUNT];
F32 corner_radii[Corner_COUNT];
F32 border_thickness;
F32 edge_softness;
F32 white_texture_override;
F32 _unused_[1];
};
typedef struct R_Mesh3DInst R_Mesh3DInst;
struct R_Mesh3DInst
{
Mat4x4F32 xform;
};
////////////////////////////////
//~ rjf: Batch Types
typedef struct R_Batch R_Batch;
struct R_Batch
{
U8 *v;
U64 byte_count;
U64 byte_cap;
};
typedef struct R_BatchNode R_BatchNode;
struct R_BatchNode
{
R_BatchNode *next;
R_Batch v;
};
typedef struct R_BatchList R_BatchList;
struct R_BatchList
{
R_BatchNode *first;
R_BatchNode *last;
U64 batch_count;
U64 byte_count;
U64 bytes_per_inst;
};
typedef struct R_BatchGroup2DParams R_BatchGroup2DParams;
struct R_BatchGroup2DParams
{
R_Handle tex;
R_Tex2DSampleKind tex_sample_kind;
Mat3x3F32 xform;
Rng2F32 clip;
F32 transparency;
};
typedef struct R_BatchGroup2DNode R_BatchGroup2DNode;
struct R_BatchGroup2DNode
{
R_BatchGroup2DNode *next;
R_BatchList batches;
R_BatchGroup2DParams params;
};
typedef struct R_BatchGroup2DList R_BatchGroup2DList;
struct R_BatchGroup2DList
{
R_BatchGroup2DNode *first;
R_BatchGroup2DNode *last;
U64 count;
};
typedef struct R_BatchGroup3DParams R_BatchGroup3DParams;
struct R_BatchGroup3DParams
{
R_Handle mesh_vertices;
R_Handle mesh_indices;
R_GeoTopologyKind mesh_geo_topology;
R_GeoVertexFlags mesh_geo_vertex_flags;
R_Handle albedo_tex;
R_Tex2DSampleKind albedo_tex_sample_kind;
Mat4x4F32 xform;
};
typedef struct R_BatchGroup3DMapNode R_BatchGroup3DMapNode;
struct R_BatchGroup3DMapNode
{
R_BatchGroup3DMapNode *next;
U64 hash;
R_BatchList batches;
R_BatchGroup3DParams params;
};
typedef struct R_BatchGroup3DMap R_BatchGroup3DMap;
struct R_BatchGroup3DMap
{
R_BatchGroup3DMapNode **slots;
U64 slots_count;
};
////////////////////////////////
//~ rjf: Pass Types
typedef struct R_PassParams_UI R_PassParams_UI;
struct R_PassParams_UI
{
R_BatchGroup2DList rects;
};
typedef struct R_PassParams_Blur R_PassParams_Blur;
struct R_PassParams_Blur
{
Rng2F32 rect;
F32 blur_size;
F32 corner_radii[Corner_COUNT];
};
typedef struct R_PassParams_Geo3D R_PassParams_Geo3D;
struct R_PassParams_Geo3D
{
Rng2F32 viewport;
Rng2F32 clip;
Mat4x4F32 view;
Mat4x4F32 projection;
R_BatchGroup3DMap mesh_batches;
};
typedef struct R_Pass R_Pass;
struct R_Pass
{
R_PassKind kind;
union
{
void *params;
R_PassParams_UI *params_ui;
R_PassParams_Blur *params_blur;
R_PassParams_Geo3D *params_geo3d;
};
};
typedef struct R_PassNode R_PassNode;
struct R_PassNode
{
R_PassNode *next;
R_Pass v;
};
typedef struct R_PassList R_PassList;
struct R_PassList
{
R_PassNode *first;
R_PassNode *last;
U64 count;
};
////////////////////////////////
//~ rjf: 2D Rendering Types
typedef enum R2_CmdKind
{
R2_CmdKind_Null,
R2_CmdKind_Rects,
R2_CmdKind_COUNT
}
R2_CmdKind;
typedef struct R2_CmdInst_Rect R2_CmdInst_Rect;
struct R2_CmdInst_Rect
{
Rng2F32 dst;
Rng2F32 src;
Vec4F32 colors[Corner_COUNT];
F32 corner_radii[Corner_COUNT];
F32 border_thickness;
F32 edge_softness;
F32 white_texture_override;
F32 _unused_[1];
};
typedef struct R2_Cmd R2_Cmd;
struct R2_Cmd
{
R2_CmdKind kind;
R_Handle texture;
Rng2F32 clip;
Vec2F32 translate;
F32 transparency;
U64 count;
union
{
void *data;
R2_CmdInst_Rect *data__rect;
};
};
typedef struct R2_CmdNode R2_CmdNode;
struct R2_CmdNode
{
R2_CmdNode *next;
R2_Cmd cmd;
};
typedef struct R2_CmdList R2_CmdList;
struct R2_CmdList
{
R2_CmdNode *first;
R2_CmdNode *last;
U64 count;
};
////////////////////////////////
//~ rjf: Handle Type Functions
internal R_Handle r_handle_zero(void);
internal B32 r_handle_match(R_Handle a, R_Handle b);
////////////////////////////////
//~ rjf: Batch Type Functions
internal R_BatchList r_batch_list_make(U64 instance_size);
internal void *r_batch_list_push_inst(Arena *arena, R_BatchList *list, U64 batch_inst_cap);
////////////////////////////////
//~ rjf: Pass Type Functions
internal R_Pass *r_pass_from_kind(Arena *arena, R_PassList *list, R_PassKind kind);
////////////////////////////////
//~ rjf: Backend Hooks
//- rjf: top-level layer initialization
r_hook void r_init(void);
//- rjf: window setup/teardown
r_hook R_Handle r_window_equip(OS_Handle window);
r_hook void r_window_unequip(OS_Handle window, R_Handle window_equip);
//- rjf: textures
r_hook R_Handle r_tex2d_alloc(R_Tex2DKind kind, Vec2S32 size, R_Tex2DFormat format, void *data);
r_hook void r_tex2d_release(R_Handle texture);
r_hook R_Tex2DKind r_kind_from_tex2d(R_Handle texture);
r_hook Vec2S32 r_size_from_tex2d(R_Handle texture);
r_hook R_Tex2DFormat r_format_from_tex2d(R_Handle texture);
r_hook void r_fill_tex2d_region(R_Handle texture, Rng2S32 subrect, void *data);
//- rjf: buffers
r_hook R_Handle r_buffer_alloc(R_BufferKind kind, U64 size, void *data);
r_hook void r_buffer_release(R_Handle buffer);
//- rjf: frame markers
r_hook void r_begin_frame(void);
r_hook void r_end_frame(void);
r_hook void r_window_begin_frame(OS_Handle window, R_Handle window_equip);
r_hook void r_window_end_frame(OS_Handle window, R_Handle window_equip);
//- rjf: render pass submission
r_hook void r_window_submit(OS_Handle window, R_Handle window_equip, R_PassList *passes);
#endif // RENDER_CORE_H
+125
View File
@@ -0,0 +1,125 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ rjf: Tables
@table(name, display_string, bytes_per_pixel)
R_Tex2DFormatTable:
{
{R8 "R8" 1}
{RGBA8 "RGBA8" 4}
{BGRA8 "BGRA8" 4}
}
@table(name, display_string)
R_Tex2DKindTable:
{
{Static "Static" }
{Dynamic "Dynamic"}
}
@table(name, display_string)
R_Tex2DSampleKindTable:
{
{Nearest "Nearest" }
{Linear "Linear" }
}
@table(name, display_string)
R_GeoTopologyKindTable:
{
{Lines "Lines" }
{LineStrip "Line Strip" }
{Triangles "Triangles" }
{TriangleStrip "Triangle Strip" }
}
@table(name, display_string)
R_BufferKindTable:
{
{Static "Static" }
{Dynamic "Dynamic"}
}
@table(name, batch, display_string)
R_PassKindTable:
{
{UI 1 "UI" }
{Blur 0 "Blur" }
{Geo3D 1 "Geo3D" }
}
////////////////////////////////
//~ rjf: Generators
@table_gen_enum R_Tex2DFormat:
{
@expand(R_Tex2DFormatTable a) `R_Tex2DFormat_$(a.name),`;
`R_Tex2DFormat_COUNT`;
}
@table_gen_enum R_Tex2DKind:
{
@expand(R_Tex2DKindTable a) `R_Tex2DKind_$(a.name),`;
`R_Tex2DKind_COUNT`;
}
@table_gen_enum R_Tex2DSampleKind:
{
@expand(R_Tex2DSampleKindTable a) `R_Tex2DSampleKind_$(a.name),`;
`R_Tex2DSampleKind_COUNT`;
}
@table_gen_enum R_GeoTopologyKind:
{
@expand(R_GeoTopologyKindTable a) `R_GeoTopologyKind_$(a.name),`;
`R_GeoTopologyKind_COUNT`;
}
@table_gen_enum R_BufferKind:
{
@expand(R_BufferKindTable a) `R_BufferKind_$(a.name),`;
`R_BufferKind_COUNT`;
}
@table_gen_enum R_PassKind:
{
@expand(R_PassKindTable a) `R_PassKind_$(a.name),`;
`R_PassKind_COUNT`;
}
@table_gen_data(type:String8) r_tex2d_format_display_string_table:
{
@expand(R_Tex2DFormatTable a) `str8_lit_comp("$(a.display_string)"),`;
}
@table_gen_data(type:U8) r_tex2d_format_bytes_per_pixel_table:
{
@expand(R_Tex2DFormatTable a) `$(a.bytes_per_pixel),`;
}
@table_gen_data(type:String8) r_tex2d_kind_display_string_table:
{
@expand(R_Tex2DKindTable a) `str8_lit_comp("$(a.display_string)"),`;
}
@table_gen_data(type:String8) r_tex2d_sample_kind_display_string_table:
{
@expand(R_Tex2DSampleKindTable a) `str8_lit_comp("$(a.display_string)"),`;
}
@table_gen_data(type:String8) r_pass_kind_display_string_table:
{
@expand(R_PassKindTable a) `str8_lit_comp("$(a.display_string)"),`;
}
@table_gen_data(type:U8) r_pass_kind_batch_table:
{
@expand(R_PassKindTable a) `$(a.batch),`;
}
@table_gen_data(type:U64) @c_file r_pass_kind_params_size_table:
{
@expand(R_PassKindTable a) `sizeof(R_PassParams_$(a.name)),`;
}
+12
View File
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#include "render_core.c"
#if LANG_CPP
# if R_BACKEND == R_BACKEND_D3D11
# include "d3d11/render_d3d11.cpp"
# else
# error Renderer backend not specified.
# endif
#endif
+35
View File
@@ -0,0 +1,35 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef RENDER_INC_H
#define RENDER_INC_H
////////////////////////////////
//~ rjf: Backend Constants
#define R_BACKEND_D3D11 1
////////////////////////////////
//~ rjf: Decide On Backend
#if !defined(R_BACKEND) && OS_WINDOWS
# define R_BACKEND R_BACKEND_D3D11
#endif
////////////////////////////////
//~ rjf: Main Includes
#include "render_core.h"
////////////////////////////////
//~ rjf: Backend Includes
#if LANG_CPP
# if R_BACKEND == R_BACKEND_D3D11
# include "d3d11/render_d3d11.h"
# else
# error Renderer backend not specified.
# endif
#endif
#endif // RENDER_INC_H