unify gpu api types into 'gpu_handle'

This commit is contained in:
jacob 2025-06-03 16:54:57 -05:00
parent 4bc7d2a589
commit da2de90de1
17 changed files with 635 additions and 168 deletions

View File

@ -864,6 +864,7 @@ void OnBuild(StringList cli_args)
ignore = !(StringEqual(name, Lit("sys_win32.c")) || ignore = !(StringEqual(name, Lit("sys_win32.c")) ||
StringEqual(name, Lit("sock_win32.c")) || StringEqual(name, Lit("sock_win32.c")) ||
StringEqual(name, Lit("gpu_dx11.c")) || StringEqual(name, Lit("gpu_dx11.c")) ||
StringEqual(name, Lit("gpu_dx12.c")) ||
StringEqual(name, Lit("playback_wasapi.c")) || StringEqual(name, Lit("playback_wasapi.c")) ||
StringEqual(name, Lit("mp3_mmf.c")) || StringEqual(name, Lit("mp3_mmf.c")) ||
StringEqual(name, Lit("ttf_dwrite.cpp"))); StringEqual(name, Lit("ttf_dwrite.cpp")));

View File

@ -0,0 +1,22 @@
#define DECL(t, n) t n : n
#define DESV(t, n, s) t n : s
#define PI 3.14159265359
#define GOLDEN 1.61803398875
/* Linear color from normalized sRGB */
float4 linear_from_srgb(float4 srgb)
{
return float4(pow(srgb.rgb, 2.2), srgb.a);
}
/* Linear color from R8G8B8A8 sRGB */
float4 linear_from_srgb32(uint srgb32)
{
float4 res;
res.r = ((srgb32 >> 0) & 0xFF) / 255.0;
res.g = ((srgb32 >> 8) & 0xFF) / 255.0;
res.b = ((srgb32 >> 16) & 0xFF) / 255.0;
res.a = ((srgb32 >> 24) & 0xFF) / 255.0;
return linear_from_srgb(res);
}

106
res/shaders_dx11/grid.hlsl Normal file
View File

@ -0,0 +1,106 @@
#include "shaders_dx11/common.hlsl"
struct vs_instance {
float2x3 xf;
float line_thickness;
float line_spacing;
float2 offset;
uint bg0_srgb;
uint bg1_srgb;
uint line_srgb;
uint x_srgb;
uint y_srgb;
};
struct ps_input {
DESV(float4, screen_pos, SV_POSITION);
DECL(float, line_thickness);
DECL(float, line_spacing);
DECL(float2, offset);
DECL(float4, bg0_lin);
DECL(float4, bg1_lin);
DECL(float4, line_lin);
DECL(float4, x_lin);
DECL(float4, y_lin);
};
/* ========================== *
* Globals
* ========================== */
StructuredBuffer<vs_instance> G_instance_buffer : register(t0);
cbuffer constants : register(b0)
{
float4x4 G_projection;
uint G_instance_offset;
};
/* ========================== *
* Vertex shader
* ========================== */
static const float2 G_quad_verts[4] = {
float2(-0.5f, -0.5f),
float2( 0.5f, -0.5f),
float2( 0.5f, 0.5f),
float2(-0.5f, 0.5f)
};
ps_input vs_main(uint instance_id : SV_InstanceID, uint vertex_id : SV_VertexID)
{
vs_instance instance = G_instance_buffer[G_instance_offset + instance_id];
float2 vert = G_quad_verts[vertex_id];
float2 world_pos = mul(instance.xf, float3(vert, 1)).xy;
ps_input output;
output.screen_pos = mul(G_projection, float4(world_pos, 0, 1));
output.line_thickness = instance.line_thickness;
output.line_spacing = instance.line_spacing;
output.offset = instance.offset;
output.bg0_lin = linear_from_srgb32(instance.bg0_srgb);
output.bg1_lin = linear_from_srgb32(instance.bg1_srgb);
output.line_lin = linear_from_srgb32(instance.line_srgb);
output.x_lin = linear_from_srgb32(instance.x_srgb);
output.y_lin = linear_from_srgb32(instance.y_srgb);
return output;
}
/* ========================== *
* Pixel shader
* ========================== */
float4 ps_main(ps_input input) : SV_TARGET
{
float2 grid_pos = input.screen_pos.xy + input.offset;
float half_thickness = input.line_thickness / 2;
float spacing = input.line_spacing;
float4 color = input.bg0_lin;
float2 v = abs(round(grid_pos / spacing) * spacing - grid_pos);
float dist = min(v.x, v.y);
if (grid_pos.y <= half_thickness && grid_pos.y >= -half_thickness) {
color = input.x_lin;
} else if (grid_pos.x <= half_thickness && grid_pos.x >= -half_thickness) {
color = input.y_lin;
} else if (dist < half_thickness) {
color = input.line_lin;
} else {
bool checker = false;
uint cell_x = (uint)(abs(grid_pos.x) / spacing) + (grid_pos.x < 0);
uint cell_y = (uint)(abs(grid_pos.y) / spacing) + (grid_pos.y < 0);
if (cell_x % 2 == 0) {
checker = cell_y % 2 == 0;
} else {
checker = cell_y % 2 == 1;
}
if (checker) {
color = input.bg1_lin;
}
}
return color;
}

View File

@ -0,0 +1,42 @@
#include "shaders_dx11/common.hlsl"
struct vs_input {
DECL(float4, pos);
DECL(float4, color_srgb);
};
struct ps_input {
DESV(float4, screen_pos, SV_POSITION);
DECL(float4, color_lin);
};
/* ========================== *
* Globals
* ========================== */
cbuffer constants : register(b0)
{
float4x4 G_projection;
};
/* ========================== *
* Vertex shader
* ========================== */
ps_input vs_main(vs_input input)
{
ps_input output;
output.screen_pos = mul(G_projection, float4(input.pos.xy, 0.f, 1.f));
output.color_lin = linear_from_srgb(input.color_srgb);
return output;
}
/* ========================== *
* Pixel shader
* ========================== */
float4 ps_main(ps_input input) : SV_TARGET
{
return input.color_lin;
}

View File

@ -0,0 +1,51 @@
#include "shaders_dx11/common.hlsl"
struct vs_instance {
float2x3 xf;
};
struct ps_input {
DESV(float4, screen_pos, SV_POSITION);
};
/* ========================== *
* Globals
* ========================== */
StructuredBuffer<vs_instance> G_instance_buffer : register(t0);
cbuffer constants : register(b0)
{
float4x4 G_projection;
uint G_instance_offset;
};
/* ========================== *
* Vertex shader
* ========================== */
static const float2 G_quad_verts[4] = {
float2(-0.5f, -0.5f),
float2( 0.5f, -0.5f),
float2( 0.5f, 0.5f),
float2(-0.5f, 0.5f)
};
ps_input vs_main(uint instance_id : SV_InstanceID, uint vertex_id : SV_VertexID)
{
vs_instance instance = G_instance_buffer[G_instance_offset + instance_id];
float2 vert = G_quad_verts[vertex_id];
float2 world_pos = mul(instance.xf, float3(vert, 1)).xy;
ps_input output;
output.screen_pos = mul(G_projection, float4(world_pos, 0, 1));
return output;
}
/* ========================== *
* Pixel shader
* ========================== */
float4 ps_main(ps_input input) : SV_TARGET
{
return float4(0, 0, 0, 0);
}

View File

@ -0,0 +1,74 @@
#include "shaders_dx11/common.hlsl"
struct vs_instance {
float2x3 xf;
float2 uv0;
float2 uv1;
uint tint_srgb;
float emittance;
};
struct ps_input {
DESV(float4, screen_pos, SV_POSITION);
DECL(float2, uv);
DECL(float4, tint_lin);
};
/* ========================== *
* Globals
* ========================== */
StructuredBuffer<vs_instance> G_instance_buffer : register(t0);
Texture2D G_texture : register(t1);
SamplerState G_sampler : register(s0);
cbuffer constants : register(b0)
{
float4x4 G_projection;
uint G_instance_offset;
};
/* ========================== *
* Vertex shader
* ========================== */
static const float2 G_quad_verts[4] = {
float2(-0.5f, -0.5f),
float2( 0.5f, -0.5f),
float2( 0.5f, 0.5f),
float2(-0.5f, 0.5f)
};
static const int2 G_uv_factors[4] = {
int2(0, 0),
int2(1, 0),
int2(1, 1),
int2(0, 1)
};
ps_input vs_main(uint instance_id : SV_InstanceID, uint vertex_id : SV_VertexID)
{
vs_instance instance = G_instance_buffer[G_instance_offset + instance_id];
float2 vert = G_quad_verts[vertex_id];
float2 uv_factor = G_uv_factors[vertex_id];
float2 world_pos = mul(instance.xf, float3(vert, 1)).xy;
ps_input output;
output.screen_pos = mul(G_projection, float4(world_pos, 0, 1));
output.uv = instance.uv0 + uv_factor * (instance.uv1 - instance.uv0);
output.tint_lin = linear_from_srgb32(instance.tint_srgb);
return output;
}
/* ========================== *
* Pixel shader
* ========================== */
float4 ps_main(ps_input input) : SV_TARGET
{
float4 color = G_texture.Sample(G_sampler, input.uv) * input.tint_lin;
return color;
}

View File

@ -76,6 +76,18 @@
/* If enabled, things like network writes & memory allocations will be tracked in a global statistics struct */ /* If enabled, things like network writes & memory allocations will be tracked in a global statistics struct */
#define GSTAT_ENABLED 1 #define GSTAT_ENABLED 1
#define DX12_TEST 0
/* ========================== * /* ========================== *
* Settings * Settings
* ========================== */ * ========================== */

View File

@ -7,7 +7,7 @@
#include "collider.h" #include "collider.h"
GLOBAL struct { GLOBAL struct {
struct gpu_texture solid_white; struct gpu_handle solid_white_texture;
} G = ZI, DEBUG_ALIAS(G, G_draw); } G = ZI, DEBUG_ALIAS(G, G_draw);
/* ========================== * /* ========================== *
@ -20,7 +20,7 @@ struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr,
(UNUSED)gpu_sr; (UNUSED)gpu_sr;
(UNUSED)font_sr; (UNUSED)font_sr;
u32 pixel_white = 0xFFFFFFFF; u32 pixel_white = 0xFFFFFFFF;
G.solid_white = gpu_texture_alloc(GPU_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, V2I32(1, 1), &pixel_white); G.solid_white_texture = gpu_texture_alloc(GPU_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, V2I32(1, 1), &pixel_white);
return (struct draw_startup_receipt) { 0 }; return (struct draw_startup_receipt) { 0 };
} }
@ -28,7 +28,7 @@ struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr,
* View * View
* ========================== */ * ========================== */
void draw_set_view(struct gpu_cmd_store store, struct xform xf) void draw_set_view(struct gpu_handle store, struct xform xf)
{ {
struct gpu_cmd_params cmd = ZI; struct gpu_cmd_params cmd = ZI;
cmd.kind = GPU_CMD_KIND_DRAW_VIEW; cmd.kind = GPU_CMD_KIND_DRAW_VIEW;
@ -40,7 +40,7 @@ void draw_set_view(struct gpu_cmd_store store, struct xform xf)
* Texture * Texture
* ========================== */ * ========================== */
void draw_texture(struct gpu_cmd_store store, struct draw_texture_params params) void draw_texture(struct gpu_handle store, struct draw_texture_params params)
{ {
struct gpu_cmd_params cmd = ZI; struct gpu_cmd_params cmd = ZI;
cmd.kind = GPU_CMD_KIND_DRAW_TEXTURE; cmd.kind = GPU_CMD_KIND_DRAW_TEXTURE;
@ -57,7 +57,7 @@ void draw_texture(struct gpu_cmd_store store, struct draw_texture_params params)
* Fill shapes * Fill shapes
* ========================== */ * ========================== */
void draw_poly_ex(struct gpu_cmd_store store, struct v2_array vertices, struct gpu_indices indices, u32 color) void draw_poly_ex(struct gpu_handle store, struct v2_array vertices, struct gpu_indices indices, u32 color)
{ {
struct gpu_cmd_params cmd = ZI; struct gpu_cmd_params cmd = ZI;
cmd.kind = GPU_CMD_KIND_DRAW_MESH; cmd.kind = GPU_CMD_KIND_DRAW_MESH;
@ -68,7 +68,7 @@ void draw_poly_ex(struct gpu_cmd_store store, struct v2_array vertices, struct g
} }
/* Draws a filled polygon using triangles in a fan pattern */ /* Draws a filled polygon using triangles in a fan pattern */
void draw_poly(struct gpu_cmd_store store, struct v2_array vertices, u32 color) void draw_poly(struct gpu_handle store, struct v2_array vertices, u32 color)
{ {
if (vertices.count >= 3) { if (vertices.count >= 3) {
struct temp_arena scratch = scratch_begin_no_conflict(); struct temp_arena scratch = scratch_begin_no_conflict();
@ -94,7 +94,7 @@ void draw_poly(struct gpu_cmd_store store, struct v2_array vertices, u32 color)
} }
} }
void draw_circle(struct gpu_cmd_store store, struct v2 pos, f32 radius, u32 color, u32 detail) void draw_circle(struct gpu_handle store, struct v2 pos, f32 radius, u32 color, u32 detail)
{ {
struct temp_arena scratch = scratch_begin_no_conflict(); struct temp_arena scratch = scratch_begin_no_conflict();
@ -116,7 +116,7 @@ void draw_circle(struct gpu_cmd_store store, struct v2 pos, f32 radius, u32 colo
scratch_end(scratch); scratch_end(scratch);
} }
void draw_quad(struct gpu_cmd_store store, struct quad quad, u32 color) void draw_quad(struct gpu_handle store, struct quad quad, u32 color)
{ {
LOCAL_PERSIST u16 indices_array[6] = { LOCAL_PERSIST u16 indices_array[6] = {
0, 1, 2, 0, 1, 2,
@ -131,11 +131,11 @@ void draw_quad(struct gpu_cmd_store store, struct quad quad, u32 color)
* Line shapes * Line shapes
* ========================== */ * ========================== */
void draw_gradient_line(struct gpu_cmd_store store, struct v2 start, struct v2 end, f32 thickness, u32 start_color, u32 end_color) void draw_gradient_line(struct gpu_handle store, struct v2 start, struct v2 end, f32 thickness, u32 start_color, u32 end_color)
{ {
#if 0 #if 0
struct quad quad = quad_from_line(start, end, thickness); struct quad quad = quad_from_line(start, end, thickness);
draw_texture(store, DRAW_TEXTURE_PARAMS(.texture = G.solid_white, .tint0 = start_color, .tint1 = end_color, .quad = quad)); draw_texture(store, DRAW_TEXTURE_PARAMS(.texture = G.solid_white_texture, .tint0 = start_color, .tint1 = end_color, .quad = quad));
#else #else
/* Placeholder */ /* Placeholder */
(UNUSED)end_color; (UNUSED)end_color;
@ -144,19 +144,19 @@ void draw_gradient_line(struct gpu_cmd_store store, struct v2 start, struct v2 e
#endif #endif
} }
void draw_line(struct gpu_cmd_store store, struct v2 start, struct v2 end, f32 thickness, u32 color) void draw_line(struct gpu_handle store, struct v2 start, struct v2 end, f32 thickness, u32 color)
{ {
struct quad quad = quad_from_line(start, end, thickness); struct quad quad = quad_from_line(start, end, thickness);
draw_quad(store, quad, color); draw_quad(store, quad, color);
} }
void draw_ray(struct gpu_cmd_store store, struct v2 pos, struct v2 rel, f32 thickness, u32 color) void draw_ray(struct gpu_handle store, struct v2 pos, struct v2 rel, f32 thickness, u32 color)
{ {
struct quad quad = quad_from_ray(pos, rel, thickness); struct quad quad = quad_from_ray(pos, rel, thickness);
draw_quad(store, quad, color); draw_quad(store, quad, color);
} }
void draw_poly_line(struct gpu_cmd_store store, struct v2_array points, b32 loop, f32 thickness, u32 color) void draw_poly_line(struct gpu_handle store, struct v2_array points, b32 loop, f32 thickness, u32 color)
{ {
if (points.count >= 2) { if (points.count >= 2) {
for (u64 i = 1; i < points.count; ++i) { for (u64 i = 1; i < points.count; ++i) {
@ -174,7 +174,7 @@ void draw_poly_line(struct gpu_cmd_store store, struct v2_array points, b32 loop
} }
} }
void draw_circle_line(struct gpu_cmd_store store, struct v2 pos, f32 radius, f32 thickness, u32 color, u32 detail) void draw_circle_line(struct gpu_handle store, struct v2 pos, f32 radius, f32 thickness, u32 color, u32 detail)
{ {
struct temp_arena scratch = scratch_begin_no_conflict(); struct temp_arena scratch = scratch_begin_no_conflict();
@ -196,14 +196,14 @@ void draw_circle_line(struct gpu_cmd_store store, struct v2 pos, f32 radius, f32
scratch_end(scratch); scratch_end(scratch);
} }
void draw_quad_line(struct gpu_cmd_store store, struct quad quad, f32 thickness, u32 color) void draw_quad_line(struct gpu_handle store, struct quad quad, f32 thickness, u32 color)
{ {
struct v2 points[] = { quad.p0, quad.p1, quad.p2, quad.p3 }; struct v2 points[] = { quad.p0, quad.p1, quad.p2, quad.p3 };
struct v2_array a = { .points = points, .count = ARRAY_COUNT(points) }; struct v2_array a = { .points = points, .count = ARRAY_COUNT(points) };
draw_poly_line(store, a, true, thickness, color); draw_poly_line(store, a, true, thickness, color);
} }
void draw_arrow_line(struct gpu_cmd_store store, struct v2 start, struct v2 end, f32 thickness, f32 arrowhead_height, u32 color) void draw_arrow_line(struct gpu_handle store, struct v2 start, struct v2 end, f32 thickness, f32 arrowhead_height, u32 color)
{ {
const f32 head_width_ratio = 0.5f; /* Width of arrowhead relative to its length */ const f32 head_width_ratio = 0.5f; /* Width of arrowhead relative to its length */
@ -233,13 +233,13 @@ void draw_arrow_line(struct gpu_cmd_store store, struct v2 start, struct v2 end,
draw_quad(store, line_quad, color); draw_quad(store, line_quad, color);
} }
void draw_arrow_ray(struct gpu_cmd_store store, struct v2 pos, struct v2 rel, f32 thickness, f32 arrowhead_height, u32 color) void draw_arrow_ray(struct gpu_handle store, struct v2 pos, struct v2 rel, f32 thickness, f32 arrowhead_height, u32 color)
{ {
struct v2 end = v2_add(pos, rel); struct v2 end = v2_add(pos, rel);
draw_arrow_line(store, pos, end, thickness, arrowhead_height, color); draw_arrow_line(store, pos, end, thickness, arrowhead_height, color);
} }
void draw_collider_line(struct gpu_cmd_store store, struct xform draw_xf, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail) void draw_collider_line(struct gpu_handle store, struct xform draw_xf, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail)
{ {
struct temp_arena scratch = scratch_begin_no_conflict(); struct temp_arena scratch = scratch_begin_no_conflict();
@ -262,7 +262,7 @@ void draw_collider_line(struct gpu_cmd_store store, struct xform draw_xf, struct
* Grid * Grid
* ========================== */ * ========================== */
void draw_grid(struct gpu_cmd_store store, struct xform xf, u32 bg0_color, u32 bg1_color, u32 line_color, u32 x_color, u32 y_color, f32 thickness, f32 spacing, struct v2 offset) void draw_grid(struct gpu_handle store, struct xform xf, u32 bg0_color, u32 bg1_color, u32 line_color, u32 x_color, u32 y_color, f32 thickness, f32 spacing, struct v2 offset)
{ {
struct gpu_cmd_params cmd = ZI; struct gpu_cmd_params cmd = ZI;
cmd.kind = GPU_CMD_KIND_DRAW_GRID; cmd.kind = GPU_CMD_KIND_DRAW_GRID;
@ -283,7 +283,7 @@ void draw_grid(struct gpu_cmd_store store, struct xform xf, u32 bg0_color, u32 b
* ========================== */ * ========================== */
/* Returns the rect of the text area */ /* Returns the rect of the text area */
struct rect draw_text(struct gpu_cmd_store store, struct draw_text_params params) struct rect draw_text(struct gpu_handle store, struct draw_text_params params)
{ {
struct temp_arena scratch = scratch_begin_no_conflict(); struct temp_arena scratch = scratch_begin_no_conflict();

View File

@ -14,7 +14,7 @@ struct draw_startup_receipt draw_startup(struct gpu_startup_receipt *gpu_sr,
* View * View
* ========================== */ * ========================== */
void draw_set_view(struct gpu_cmd_store store, struct xform xf); void draw_set_view(struct gpu_handle store, struct xform xf);
/* ========================== * /* ========================== *
* Texture * Texture
@ -28,54 +28,54 @@ void draw_set_view(struct gpu_cmd_store store, struct xform xf);
struct draw_texture_params { struct draw_texture_params {
struct xform xf; struct xform xf;
struct gpu_texture texture; /* Overrides sprite if set */ struct gpu_handle texture; /* Overrides sprite if set */
struct sprite_tag sprite; struct sprite_tag sprite;
struct clip_rect clip; struct clip_rect clip;
u32 tint; u32 tint;
f32 emittance; f32 emittance;
}; };
void draw_texture(struct gpu_cmd_store store, struct draw_texture_params params); void draw_texture(struct gpu_handle store, struct draw_texture_params params);
/* ========================== * /* ========================== *
* Fill shapes * Fill shapes
* ========================== */ * ========================== */
void draw_poly_ex(struct gpu_cmd_store store, struct v2_array vertices, struct gpu_indices indices, u32 color); void draw_poly_ex(struct gpu_handle store, struct v2_array vertices, struct gpu_indices indices, u32 color);
void draw_poly(struct gpu_cmd_store store, struct v2_array points, u32 color); void draw_poly(struct gpu_handle store, struct v2_array points, u32 color);
void draw_circle(struct gpu_cmd_store store, struct v2 pos, f32 radius, u32 color, u32 detail); void draw_circle(struct gpu_handle store, struct v2 pos, f32 radius, u32 color, u32 detail);
void draw_quad(struct gpu_cmd_store store, struct quad quad, u32 color); void draw_quad(struct gpu_handle store, struct quad quad, u32 color);
/* ========================== * /* ========================== *
* Line shapes * Line shapes
* ========================== */ * ========================== */
void draw_gradient_line(struct gpu_cmd_store store, struct v2 start, struct v2 end, f32 thickness, u32 start_color, u32 end_color); void draw_gradient_line(struct gpu_handle store, struct v2 start, struct v2 end, f32 thickness, u32 start_color, u32 end_color);
void draw_line(struct gpu_cmd_store store, struct v2 start, struct v2 end, f32 thickness, u32 color); void draw_line(struct gpu_handle store, struct v2 start, struct v2 end, f32 thickness, u32 color);
void draw_ray(struct gpu_cmd_store store, struct v2 pos, struct v2 rel, f32 thickness, u32 color); void draw_ray(struct gpu_handle store, struct v2 pos, struct v2 rel, f32 thickness, u32 color);
void draw_poly_line(struct gpu_cmd_store store, struct v2_array points, b32 loop, f32 thickness, u32 color); void draw_poly_line(struct gpu_handle store, struct v2_array points, b32 loop, f32 thickness, u32 color);
void draw_circle_line(struct gpu_cmd_store store, struct v2 pos, f32 radius, f32 thickness, u32 color, u32 detail); void draw_circle_line(struct gpu_handle store, struct v2 pos, f32 radius, f32 thickness, u32 color, u32 detail);
void draw_quad_line(struct gpu_cmd_store store, struct quad quad, f32 thickness, u32 color); void draw_quad_line(struct gpu_handle store, struct quad quad, f32 thickness, u32 color);
void draw_arrow_line(struct gpu_cmd_store store, struct v2 start, struct v2 end, f32 thickness, f32 arrowhead_height, u32 color); void draw_arrow_line(struct gpu_handle store, struct v2 start, struct v2 end, f32 thickness, f32 arrowhead_height, u32 color);
void draw_arrow_ray(struct gpu_cmd_store store, struct v2 pos, struct v2 rel, f32 thickness, f32 arrowhead_height, u32 color); void draw_arrow_ray(struct gpu_handle store, struct v2 pos, struct v2 rel, f32 thickness, f32 arrowhead_height, u32 color);
void draw_collider_line(struct gpu_cmd_store store, struct xform draw_xf, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail); void draw_collider_line(struct gpu_handle store, struct xform draw_xf, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail);
/* ========================== * /* ========================== *
* Grid * Grid
* ========================== */ * ========================== */
void draw_grid(struct gpu_cmd_store store, struct xform xf, u32 bg0_color, u32 bg1_color, u32 line_color, u32 x_color, u32 y_color, f32 thickness, f32 spacing, struct v2 offset); void draw_grid(struct gpu_handle store, struct xform xf, u32 bg0_color, u32 bg1_color, u32 line_color, u32 x_color, u32 y_color, f32 thickness, f32 spacing, struct v2 offset);
/* ========================== * /* ========================== *
* Text * Text
@ -123,6 +123,6 @@ struct draw_text_params {
struct string str; struct string str;
}; };
struct rect draw_text(struct gpu_cmd_store store, struct draw_text_params params); struct rect draw_text(struct gpu_handle store, struct draw_text_params params);
#endif #endif

View File

@ -119,7 +119,7 @@ INTERNAL WORK_TASK_FUNC_DEF(font_load_asset_task, vparams)
resource_close(&res); resource_close(&res);
/* Send texture to GPU */ /* Send texture to GPU */
struct gpu_texture texture = gpu_texture_alloc(GPU_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, V2I32(result.image_data.width, result.image_data.height), result.image_data.pixels); struct gpu_handle texture = gpu_texture_alloc(GPU_TEXTURE_FORMAT_R8G8B8A8_UNORM, 0, V2I32(result.image_data.width, result.image_data.height), result.image_data.pixels);
/* Allocate store memory */ /* Allocate store memory */
struct font *font = NULL; struct font *font = NULL;

View File

@ -21,7 +21,7 @@ struct font_glyph {
}; };
struct font { struct font {
struct gpu_texture texture; struct gpu_handle texture;
u32 image_width; u32 image_width;
u32 image_height; u32 image_height;
f32 point_size; f32 point_size;

View File

@ -3,9 +3,6 @@
struct sys_window; struct sys_window;
#define GPU_TEXTURE_MAX_WIDTH 16384
#define GPU_TEXTURE_MAX_HEIGHT 16384
/* ========================== * /* ========================== *
* Startup * Startup
* ========================== */ * ========================== */
@ -13,6 +10,16 @@ struct sys_window;
struct gpu_startup_receipt { i32 _; }; struct gpu_startup_receipt { i32 _; };
struct gpu_startup_receipt gpu_startup(struct sys_window *window); struct gpu_startup_receipt gpu_startup(struct sys_window *window);
/* ========================== *
* Handle
* ========================== */
struct gpu_handle {
u64 v;
};
void gpu_release(struct gpu_handle handle);
/* ========================== * /* ========================== *
* Texture * Texture
* ========================== */ * ========================== */
@ -30,17 +37,11 @@ enum gpu_texture_flag {
GPU_TEXTURE_FLAG_TARGETABLE = (1 << 0) GPU_TEXTURE_FLAG_TARGETABLE = (1 << 0)
}; };
struct gpu_texture { struct gpu_handle gpu_texture_alloc(enum gpu_texture_format format, u32 flags, struct v2i32 size, void *initial_data);
u64 handle;
};
struct gpu_texture gpu_texture_alloc(enum gpu_texture_format format, u32 flags, struct v2i32 size, void *initial_data); void gpu_texture_clear(struct gpu_handle target_texture, u32 clear_color);
void gpu_texture_release(struct gpu_texture t); struct v2i32 gpu_texture_get_size(struct gpu_handle texture);
void gpu_texture_clear(struct gpu_texture target_texture, u32 clear_color);
struct v2i32 gpu_texture_get_size(struct gpu_texture texture);
/* ========================== * /* ========================== *
* Cmd buffer * Cmd buffer
@ -76,7 +77,7 @@ struct gpu_cmd_params {
struct { struct {
struct xform xf; struct xform xf;
struct sprite_tag sprite; struct sprite_tag sprite;
struct gpu_texture texture; struct gpu_handle texture;
struct clip_rect clip; struct clip_rect clip;
u32 tint; u32 tint;
f32 emittance; f32 emittance;
@ -98,17 +99,11 @@ struct gpu_cmd_params {
}; };
}; };
struct gpu_cmd_store { struct gpu_handle gpu_cmd_store_alloc(void);
u64 handle;
};
struct gpu_cmd_store gpu_cmd_store_alloc(void); void gpu_push_cmd(struct gpu_handle gpu_cmd_store, struct gpu_cmd_params params);
void gpu_cmd_store_release(struct gpu_cmd_store gpu_cmd_store); void gpu_submit_cmds(struct gpu_handle gpu_cmd_store);
void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params params);
void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store);
/* ========================== * /* ========================== *
* Dispatch * Dispatch
@ -116,31 +111,25 @@ void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store);
struct gpu_dispatch_cmds_array { struct gpu_dispatch_cmds_array {
u64 count; u64 count;
struct gpu_cmd_store **cmds; struct gpu_handle **stores;
}; };
struct gpu_dispatch_params { struct gpu_dispatch_params {
struct gpu_dispatch_cmds_array cmds_array; struct gpu_dispatch_cmds_array cmds_array;
struct gpu_texture draw_target; struct gpu_handle draw_target;
struct rect draw_target_viewport; struct rect draw_target_viewport;
}; };
struct gpu_dispatch_state { struct gpu_handle gpu_dispatch_state_alloc(void);
u64 handle;
};
struct gpu_dispatch_state gpu_dispatch_state_alloc(void); void gpu_dispatch(struct gpu_handle gpu_dispatch_state, struct gpu_dispatch_params params);
void gpu_dispatch_state_release(struct gpu_dispatch_state gpu_dispatch_state);
void gpu_dispatch(struct gpu_dispatch_state gpu_dispatch_state, struct gpu_dispatch_params params);
/* ========================== * /* ========================== *
* Backbuffer * Backbuffer
* ========================== */ * ========================== */
/* Returns a texture representing the internal backbuffer. Lifetime is managed by gpu layer. */ /* Returns a texture representing the internal backbuffer. Lifetime is managed by gpu layer. */
struct gpu_texture gpu_recreate_backbuffer(struct v2i32 size); struct gpu_handle gpu_recreate_backbuffer(struct v2i32 size);
/* Presents the backbuffer to the screen */ /* Presents the backbuffer to the screen */
void gpu_swap_backbuffer(i32 vsync); void gpu_swap_backbuffer(i32 vsync);

View File

@ -1,3 +1,5 @@
#if !DX12_TEST
#include "gpu.h" #include "gpu.h"
#include "resource.h" #include "resource.h"
#include "sys.h" #include "sys.h"
@ -55,6 +57,19 @@ enum dx11_shader_kind {
NUM_DX11_SHADER_KINDS NUM_DX11_SHADER_KINDS
}; };
enum dx11_handle_kind {
DX11_HANDLE_KIND_NONE,
DX11_HANDLE_KIND_TEXTURE,
DX11_HANDLE_KIND_CMD_STORE,
DX11_HANDLE_KIND_DISPATCH_STATE,
NUM_DX11_HANDLE_KINDS
};
struct dx11_handle_header {
enum dx11_handle_kind kind;
};
struct dx11_shader { struct dx11_shader {
enum dx11_shader_kind kind; enum dx11_shader_kind kind;
b32 valid; /* Is this shader allocated */ b32 valid; /* Is this shader allocated */
@ -64,6 +79,8 @@ struct dx11_shader {
}; };
struct dx11_dispatch_state { struct dx11_dispatch_state {
struct dx11_handle_header header;
struct dx11_texture *albedo; struct dx11_texture *albedo;
struct dx11_buffer *constant_buffer; struct dx11_buffer *constant_buffer;
@ -114,7 +131,7 @@ struct dx11_cmd {
u32 index_count; u32 index_count;
} mesh; } mesh;
struct { struct {
struct gpu_texture texture; /* Overrides sprite if set */ struct gpu_handle texture; /* Overrides sprite if set */
struct sprite_tag sprite; struct sprite_tag sprite;
u32 instance_offset; u32 instance_offset;
u32 instance_count; u32 instance_count;
@ -133,6 +150,8 @@ struct dx11_cmd {
}; };
struct dx11_cmd_store { struct dx11_cmd_store {
struct dx11_handle_header header;
/* Commands w/ data still in cpu memory */ /* Commands w/ data still in cpu memory */
struct arena cpu_cmds_arena; struct arena cpu_cmds_arena;
struct dx11_cmd *cpu_first_cmd; struct dx11_cmd *cpu_first_cmd;
@ -163,6 +182,8 @@ struct dx11_cmd_store {
}; };
struct dx11_texture { struct dx11_texture {
struct dx11_handle_header header;
ID3D11Texture2D *texture; ID3D11Texture2D *texture;
ID3D11ShaderResourceView *srv; ID3D11ShaderResourceView *srv;
ID3D11RenderTargetView *rtv; ID3D11RenderTargetView *rtv;
@ -240,18 +261,6 @@ GLOBAL struct {
} G = ZI, DEBUG_ALIAS(G, G_gpu_dx11); } G = ZI, DEBUG_ALIAS(G, G_gpu_dx11);
/* ========================== *
* Util
* ========================== */
/* Calculate the view projection matrix */
INLINE struct mat4x4 calculate_vp(struct xform view, f32 viewport_width, f32 viewport_height)
{
struct mat4x4 projection = mat4x4_from_ortho(0.0, viewport_width, viewport_height, 0.0, -1.0, 1.0);
struct mat4x4 view4x4 = mat4x4_from_xform(view);
return mat4x4_mul(projection, view4x4);
}
/* ========================== * /* ========================== *
* Startup * Startup
* ========================== */ * ========================== */
@ -520,6 +529,49 @@ struct gpu_startup_receipt gpu_startup(struct sys_window *window)
return (struct gpu_startup_receipt) { 0 }; return (struct gpu_startup_receipt) { 0 };
} }
/* ========================== *
* Util
* ========================== */
/* Calculate the view projection matrix */
INLINE struct mat4x4 calculate_vp(struct xform view, f32 viewport_width, f32 viewport_height)
{
struct mat4x4 projection = mat4x4_from_ortho(0.0, viewport_width, viewport_height, 0.0, -1.0, 1.0);
struct mat4x4 view4x4 = mat4x4_from_xform(view);
return mat4x4_mul(projection, view4x4);
}
/* ========================== *
* Handle
* ========================== */
INTERNAL void dx11_texture_release(struct dx11_texture *t);
void gpu_release(struct gpu_handle handle)
{
struct dx11_handle_header *header = (struct dx11_handle_header *)handle.v;
switch (header->kind) {
default: break;
case DX11_HANDLE_KIND_TEXTURE:
{
dx11_texture_release((struct dx11_texture *)header);
} break;
case DX11_HANDLE_KIND_CMD_STORE:
{
/* TODO */
ASSERT(false);
} break;
case DX11_HANDLE_KIND_DISPATCH_STATE:
{
/* TODO */
ASSERT(false);
} break;
}
}
/* ========================== * /* ========================== *
* Mesh shader structs * Mesh shader structs
* ========================== */ * ========================== */
@ -595,7 +647,7 @@ INTERNAL void init_shader_table(void)
/* Mesh shader layout */ /* Mesh shader layout */
G.shader_info[DX11_SHADER_KIND_MESH] = (struct dx11_shader_desc) { G.shader_info[DX11_SHADER_KIND_MESH] = (struct dx11_shader_desc) {
.kind = DX11_SHADER_KIND_MESH, .kind = DX11_SHADER_KIND_MESH,
.name_cstr = "shaders/mesh.hlsl", .name_cstr = "shaders_dx11/mesh.hlsl",
.input_layout_desc = { .input_layout_desc = {
{ "pos", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "pos", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "color_srgb", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 } { "color_srgb", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
@ -605,19 +657,19 @@ INTERNAL void init_shader_table(void)
/* Texture shader layout */ /* Texture shader layout */
G.shader_info[DX11_SHADER_KIND_TEXTURE] = (struct dx11_shader_desc) { G.shader_info[DX11_SHADER_KIND_TEXTURE] = (struct dx11_shader_desc) {
.kind = DX11_SHADER_KIND_TEXTURE, .kind = DX11_SHADER_KIND_TEXTURE,
.name_cstr = "shaders/texture.hlsl" .name_cstr = "shaders_dx11/texture.hlsl"
}; };
/* Grid shader layout */ /* Grid shader layout */
G.shader_info[DX11_SHADER_KIND_GRID] = (struct dx11_shader_desc) { G.shader_info[DX11_SHADER_KIND_GRID] = (struct dx11_shader_desc) {
.kind = DX11_SHADER_KIND_GRID, .kind = DX11_SHADER_KIND_GRID,
.name_cstr = "shaders/grid.hlsl" .name_cstr = "shaders_dx11/grid.hlsl"
}; };
/* Test shader layout */ /* Test shader layout */
G.shader_info[DX11_SHADER_KIND_TEST] = (struct dx11_shader_desc) { G.shader_info[DX11_SHADER_KIND_TEST] = (struct dx11_shader_desc) {
.kind = DX11_SHADER_KIND_TEST, .kind = DX11_SHADER_KIND_TEST,
.name_cstr = "shaders/test.hlsl" .name_cstr = "shaders_dx11/test.hlsl"
}; };
#if RESOURCE_RELOADING #if RESOURCE_RELOADING
@ -988,6 +1040,7 @@ INTERNAL struct dx11_texture *dx11_texture_alloc(enum DXGI_FORMAT format, u32 fl
sys_mutex_unlock(&lock); sys_mutex_unlock(&lock);
} }
MEMZERO_STRUCT(t); MEMZERO_STRUCT(t);
t->header.kind = DX11_HANDLE_KIND_TEXTURE;
D3D11_TEXTURE2D_DESC desc = ZI; D3D11_TEXTURE2D_DESC desc = ZI;
desc.Width = max_i32(size.x, 1); desc.Width = max_i32(size.x, 1);
@ -1051,10 +1104,10 @@ INTERNAL void dx11_texture_release(struct dx11_texture *t)
} }
} }
struct gpu_texture gpu_texture_alloc(enum gpu_texture_format format, u32 flags, struct v2i32 size, void *initial_data) struct gpu_handle gpu_texture_alloc(enum gpu_texture_format format, u32 flags, struct v2i32 size, void *initial_data)
{ {
__prof; __prof;
struct gpu_texture res = ZI; struct gpu_handle res = ZI;
/* Convert format to dx11 format */ /* Convert format to dx11 format */
enum DXGI_FORMAT dx11_format = dx11_format_from_gpu_format(format); enum DXGI_FORMAT dx11_format = dx11_format_from_gpu_format(format);
@ -1071,20 +1124,14 @@ struct gpu_texture gpu_texture_alloc(enum gpu_texture_format format, u32 flags,
} }
struct dx11_texture *t = dx11_texture_alloc(dx11_format, dx11_flags, size, initial_data); struct dx11_texture *t = dx11_texture_alloc(dx11_format, dx11_flags, size, initial_data);
res.handle = (u64)t; res.v = (u64)t;
return res; return res;
} }
void gpu_texture_release(struct gpu_texture t) void gpu_texture_clear(struct gpu_handle target_texture, u32 clear_color)
{ {
__prof; __prof;
dx11_texture_release((struct dx11_texture *)t.handle); struct dx11_texture *t = (struct dx11_texture *)target_texture.v;
}
void gpu_texture_clear(struct gpu_texture target_texture, u32 clear_color)
{
__prof;
struct dx11_texture *t = (struct dx11_texture *)target_texture.handle;
if (t->rtv) { if (t->rtv) {
f32 r = (f32)((clear_color >> 0) & 0xFF) / 255.0f; f32 r = (f32)((clear_color >> 0) & 0xFF) / 255.0f;
f32 g = (f32)((clear_color >> 8) & 0xFF) / 255.0f; f32 g = (f32)((clear_color >> 8) & 0xFF) / 255.0f;
@ -1095,9 +1142,9 @@ void gpu_texture_clear(struct gpu_texture target_texture, u32 clear_color)
} }
} }
struct v2i32 gpu_texture_get_size(struct gpu_texture texture) struct v2i32 gpu_texture_get_size(struct gpu_handle texture)
{ {
return ((struct dx11_texture *)texture.handle)->size; return ((struct dx11_texture *)texture.v)->size;
} }
/* ========================== * /* ========================== *
@ -1228,7 +1275,7 @@ INTERNAL void dx11_buffer_submit(struct dx11_buffer *buffer)
* Cmd store * Cmd store
* ========================== */ * ========================== */
struct gpu_cmd_store gpu_cmd_store_alloc(void) struct gpu_handle gpu_cmd_store_alloc(void)
{ {
__prof; __prof;
struct dx11_cmd_store *store = NULL; struct dx11_cmd_store *store = NULL;
@ -1259,6 +1306,7 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void)
arena_reset(&store->cpu_cmds_arena); arena_reset(&store->cpu_cmds_arena);
arena_reset(&store->gpu_cmds_arena); arena_reset(&store->gpu_cmds_arena);
} }
store->header.kind = DX11_HANDLE_KIND_CMD_STORE;
/* Desc template */ /* Desc template */
const D3D11_BUFFER_DESC structured_buffer_desc = { const D3D11_BUFFER_DESC structured_buffer_desc = {
@ -1309,23 +1357,15 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void)
} }
} }
struct gpu_cmd_store res = ZI; struct gpu_handle res = ZI;
res.handle = (u64)store; res.v = (u64)store;
return res; return res;
} }
void gpu_cmd_store_release(struct gpu_cmd_store gpu_cmd_store) void gpu_push_cmd(struct gpu_handle gpu_cmd_store, struct gpu_cmd_params params)
{ {
__prof; __prof;
/* TODO */ struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.v;
ASSERT(false);
(UNUSED)gpu_cmd_store;
}
void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params params)
{
__prof;
struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.handle;
switch (params.kind) { switch (params.kind) {
default: default:
@ -1396,7 +1436,7 @@ void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params para
if (cmd && if (cmd &&
((cmd->kind != params.kind) || ((cmd->kind != params.kind) ||
(cmd->texture.sprite.hash != params.texture.sprite.hash) || (cmd->texture.sprite.hash != params.texture.sprite.hash) ||
(cmd->texture.texture.handle != params.texture.texture.handle))) { (cmd->texture.texture.v != params.texture.texture.v))) {
/* Cannot batch */ /* Cannot batch */
cmd = NULL; cmd = NULL;
} }
@ -1483,10 +1523,10 @@ void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params para
} }
} }
void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store) void gpu_submit_cmds(struct gpu_handle gpu_cmd_store)
{ {
__prof; __prof;
struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.handle; struct dx11_cmd_store *store = (struct dx11_cmd_store *)gpu_cmd_store.v;
/* Swap cmd lists */ /* Swap cmd lists */
struct arena swp_arena = store->gpu_cmds_arena; struct arena swp_arena = store->gpu_cmds_arena;
@ -1518,11 +1558,10 @@ void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store)
* Dispatch state * Dispatch state
* ========================== */ * ========================== */
struct gpu_dispatch_state gpu_dispatch_state_alloc(void) struct gpu_handle gpu_dispatch_state_alloc(void)
{ {
__prof; __prof;
struct dx11_dispatch_state *state = NULL; struct dx11_dispatch_state *state = NULL;
{
{ {
struct sys_lock lock = sys_mutex_lock_e(&G.dispatch_states_mutex); struct sys_lock lock = sys_mutex_lock_e(&G.dispatch_states_mutex);
if (G.first_free_dispatch_state) { if (G.first_free_dispatch_state) {
@ -1534,22 +1573,13 @@ struct gpu_dispatch_state gpu_dispatch_state_alloc(void)
sys_mutex_unlock(&lock); sys_mutex_unlock(&lock);
} }
MEMZERO_STRUCT(state); MEMZERO_STRUCT(state);
} state->header.kind = DX11_HANDLE_KIND_DISPATCH_STATE;
struct gpu_handle res = ZI;
struct gpu_dispatch_state res = ZI; res.v = (u64)state;
res.handle = (u64)state;
return res; return res;
} }
void gpu_dispatch_state_release(struct gpu_dispatch_state gpu_dispatch_state)
{
/* TODO */
__prof;
ASSERT(false);
(UNUSED)gpu_dispatch_state;
}
/* ========================== * /* ========================== *
* Dispatch * Dispatch
* ========================== */ * ========================== */
@ -1621,12 +1651,12 @@ INTERNAL void dx11_unbind(u32 flags)
} }
/* TODO: Lock resources during dispatch */ /* TODO: Lock resources during dispatch */
void gpu_dispatch(struct gpu_dispatch_state gpu_dispatch_state, struct gpu_dispatch_params params) void gpu_dispatch(struct gpu_handle gpu_dispatch_state, struct gpu_dispatch_params params)
{ {
__prof; __prof;
__profscope_dx11(G.profiling_ctx, Dispatch, RGB_F(0.5, 0.2, 0.2)); __profscope_dx11(G.profiling_ctx, Dispatch, RGB_F(0.5, 0.2, 0.2));
struct sprite_scope *sprite_scope = sprite_scope_begin(); struct sprite_scope *sprite_scope = sprite_scope_begin();
struct dx11_dispatch_state *state = (struct dx11_dispatch_state *)gpu_dispatch_state.handle; struct dx11_dispatch_state *state = (struct dx11_dispatch_state *)gpu_dispatch_state.v;
struct rect viewport = params.draw_target_viewport; struct rect viewport = params.draw_target_viewport;
@ -1640,7 +1670,7 @@ void gpu_dispatch(struct gpu_dispatch_state gpu_dispatch_state, struct gpu_dispa
d3d11_viewport.TopLeftY = viewport.y; d3d11_viewport.TopLeftY = viewport.y;
ID3D11DeviceContext_RSSetViewports(G.devcon, 1, &d3d11_viewport); ID3D11DeviceContext_RSSetViewports(G.devcon, 1, &d3d11_viewport);
struct dx11_texture *final_tex = (struct dx11_texture *)params.draw_target.handle; struct dx11_texture *final_tex = (struct dx11_texture *)params.draw_target.v;
struct v2i32 final_tex_size = final_tex->size; struct v2i32 final_tex_size = final_tex->size;
/* Allocate constant buffer */ /* Allocate constant buffer */
@ -1671,7 +1701,7 @@ void gpu_dispatch(struct gpu_dispatch_state gpu_dispatch_state, struct gpu_dispa
__profscope(Regular pass); __profscope(Regular pass);
__profscope_dx11(G.profiling_ctx, Regular pass, RGB_F(0.2, 0.5, 0.5)); __profscope_dx11(G.profiling_ctx, Regular pass, RGB_F(0.2, 0.5, 0.5));
for (u64 cmd_stores_array_index = 0; cmd_stores_array_index < params.cmds_array.count; ++cmd_stores_array_index) { for (u64 cmd_stores_array_index = 0; cmd_stores_array_index < params.cmds_array.count; ++cmd_stores_array_index) {
struct dx11_cmd_store *store = (struct dx11_cmd_store *)params.cmds_array.cmds[cmd_stores_array_index]->handle; struct dx11_cmd_store *store = (struct dx11_cmd_store *)params.cmds_array.stores[cmd_stores_array_index]->v;
for (struct dx11_cmd *cmd = store->gpu_first_cmd; cmd; cmd = cmd->next) { for (struct dx11_cmd *cmd = store->gpu_first_cmd; cmd; cmd = cmd->next) {
enum gpu_cmd_kind cmd_kind = cmd->kind; enum gpu_cmd_kind cmd_kind = cmd->kind;
@ -1741,14 +1771,14 @@ void gpu_dispatch(struct gpu_dispatch_state gpu_dispatch_state, struct gpu_dispa
struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE]; struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE];
if (shader->valid) { if (shader->valid) {
struct dx11_texture *texture = NULL; struct dx11_texture *texture = NULL;
if (cmd->texture.texture.handle) { if (cmd->texture.texture.v) {
/* Load texture if handle is set */ /* Load texture if handle is set */
texture = (struct dx11_texture *)cmd->texture.texture.handle; texture = (struct dx11_texture *)cmd->texture.texture.v;
} else if (cmd->texture.sprite.hash) { } else if (cmd->texture.sprite.hash) {
/* Otherwise load sprite */ /* Otherwise load sprite */
struct sprite_texture *sprite_texture = sprite_texture_from_tag_async(sprite_scope, cmd->texture.sprite); struct sprite_texture *sprite_texture = sprite_texture_from_tag_async(sprite_scope, cmd->texture.sprite);
if (sprite_texture->loaded) { if (sprite_texture->loaded) {
texture = (struct dx11_texture *)sprite_texture->texture.handle; texture = (struct dx11_texture *)sprite_texture->texture.v;
} }
} }
@ -1938,9 +1968,9 @@ INTERNAL struct DXGI_QUERY_VIDEO_MEMORY_INFO get_memory_info(void)
} }
#endif #endif
struct gpu_texture gpu_recreate_backbuffer(struct v2i32 size) struct gpu_handle gpu_recreate_backbuffer(struct v2i32 size)
{ {
struct gpu_texture res = ZI; struct gpu_handle res = ZI;
/* Delay backbuffer recreation so that we don't recreate it too quickly. /* Delay backbuffer recreation so that we don't recreate it too quickly.
* It seems that doing this without a delay too often will cause windows * It seems that doing this without a delay too often will cause windows
@ -1973,7 +2003,7 @@ struct gpu_texture gpu_recreate_backbuffer(struct v2i32 size)
rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
ID3D11Device_CreateRenderTargetView(G.dev, (ID3D11Resource *)G.backbuffer_texture.texture, &rtv_desc, &G.backbuffer_texture.rtv); ID3D11Device_CreateRenderTargetView(G.dev, (ID3D11Resource *)G.backbuffer_texture.texture, &rtv_desc, &G.backbuffer_texture.rtv);
res.handle = (u64)&G.backbuffer_texture; res.v = (u64)&G.backbuffer_texture;
return res; return res;
} }
@ -2139,3 +2169,6 @@ INTERNAL void gpu_capture_image_for_profiler(void)
} }
#endif #endif
#endif

137
src/gpu_dx12.c Normal file
View File

@ -0,0 +1,137 @@
#if DX12_TEST
#include "gpu.h"
#include "sys.h"
/* ========================== *
* Global state
* ========================== */
GLOBAL struct {
i32 _;
} G = ZI, DEBUG_ALIAS(G, G_gpu_dx12);
/* ========================== *
* Startup
* ========================== */
struct gpu_startup_receipt gpu_startup(struct sys_window *window)
{
(UNUSED)window;
struct gpu_startup_receipt res = ZI;
return res;
}
/* ========================== *
* Handle
* ========================== */
void gpu_release(struct gpu_handle handle)
{
(UNUSED)handle;
}
/* ========================== *
* Texture
* ========================== */
struct gpu_handle gpu_texture_alloc(enum gpu_texture_format format, u32 flags, struct v2i32 size, void *initial_data)
{
(UNUSED)format;
(UNUSED)flags;
(UNUSED)size;
(UNUSED)initial_data;
struct gpu_handle res = ZI;
return res;
}
void gpu_texture_clear(struct gpu_handle target_texture, u32 clear_color)
{
(UNUSED)target_texture;
(UNUSED)clear_color;
}
struct v2i32 gpu_texture_get_size(struct gpu_handle texture)
{
(UNUSED)texture;
struct v2i32 res = ZI;
return res;
}
/* ========================== *
* Cmd buffer
* ========================== */
struct gpu_handle gpu_cmd_store_alloc(void)
{
struct gpu_handle res = ZI;
return res;
}
void gpu_push_cmd(struct gpu_handle gpu_cmd_store, struct gpu_cmd_params params)
{
(UNUSED)gpu_cmd_store;
(UNUSED)params;
}
void gpu_submit_cmds(struct gpu_handle gpu_cmd_store)
{
(UNUSED)gpu_cmd_store;
}
/* ========================== *
* Dispatch
* ========================== */
struct gpu_handle gpu_dispatch_state_alloc(void)
{
struct gpu_handle res = ZI;
return res;
}
void gpu_dispatch(struct gpu_handle gpu_dispatch_state, struct gpu_dispatch_params params)
{
(UNUSED)gpu_dispatch_state;
(UNUSED)params;
}
/* ========================== *
* Backbuffer
* ========================== */
struct gpu_handle gpu_recreate_backbuffer(struct v2i32 size)
{
(UNUSED)size;
struct gpu_handle res = ZI;
return res;
}
void gpu_swap_backbuffer(i32 vsync)
{
(UNUSED)vsync;
}
#endif

View File

@ -1344,7 +1344,7 @@ INTERNAL SYS_THREAD_ENTRY_POINT_FUNC_DEF(sprite_evictor_thread_entry_point, arg)
for (struct evict_node *en = first_evicted; en; en = en->next_evicted) { for (struct evict_node *en = first_evicted; en; en = en->next_evicted) {
struct cache_entry *n = en->cache_entry; struct cache_entry *n = en->cache_entry;
if (n->kind == CACHE_ENTRY_KIND_TEXTURE && n->texture->valid) { if (n->kind == CACHE_ENTRY_KIND_TEXTURE && n->texture->valid) {
gpu_texture_release(n->texture->texture); gpu_release(n->texture->texture);
} }
arena_release(&n->arena); arena_release(&n->arena);
} }

View File

@ -48,7 +48,7 @@ void sprite_scope_end(struct sprite_scope *scope);
struct sprite_texture { struct sprite_texture {
b32 loaded; b32 loaded;
b32 valid; b32 valid;
struct gpu_texture texture; struct gpu_handle texture;
u32 width; u32 width;
u32 height; u32 height;
}; };

View File

@ -71,15 +71,15 @@ GLOBAL struct {
struct second_stat net_bytes_sent; struct second_stat net_bytes_sent;
/* Gpu handles */ /* Gpu handles */
struct gpu_texture user_texture; struct gpu_handle user_texture;
struct gpu_texture backbuffer_texture; struct gpu_handle backbuffer_texture;
struct gpu_cmd_store world_gpu_cmd_store; struct gpu_handle world_gpu_cmd_store;
struct gpu_cmd_store user_gpu_cmd_store; struct gpu_handle user_gpu_cmd_store;
struct gpu_cmd_store backbuffer_gpu_cmd_store; struct gpu_handle backbuffer_gpu_cmd_store;
struct gpu_dispatch_state user_dispatch_state; struct gpu_handle user_dispatch_state;
struct gpu_dispatch_state backbuffer_dispatch_state; struct gpu_handle backbuffer_dispatch_state;
struct xform world_to_user_xf; struct xform world_to_user_xf;
@ -2085,15 +2085,15 @@ INTERNAL void user_update(void)
{ {
/* User texture */ /* User texture */
if (!G.user_texture.handle || !v2i32_eq(gpu_texture_get_size(G.user_texture), user_resolution)) { if (!G.user_texture.v || !v2i32_eq(gpu_texture_get_size(G.user_texture), user_resolution)) {
if (G.user_texture.handle) { if (G.user_texture.v) {
gpu_texture_release(G.user_texture); gpu_release(G.user_texture);
} }
G.user_texture = gpu_texture_alloc(GPU_TEXTURE_FORMAT_R8G8B8A8_UNORM, GPU_TEXTURE_FLAG_TARGETABLE, user_resolution, NULL); G.user_texture = gpu_texture_alloc(GPU_TEXTURE_FORMAT_R8G8B8A8_UNORM, GPU_TEXTURE_FLAG_TARGETABLE, user_resolution, NULL);
} }
/* Backbuffer texture */ /* Backbuffer texture */
if (!G.backbuffer_texture.handle || !v2i32_eq(gpu_texture_get_size(G.backbuffer_texture), backbuffer_resolution)) { if (!G.backbuffer_texture.v || !v2i32_eq(gpu_texture_get_size(G.backbuffer_texture), backbuffer_resolution)) {
G.backbuffer_texture = gpu_recreate_backbuffer(backbuffer_resolution); G.backbuffer_texture = gpu_recreate_backbuffer(backbuffer_resolution);
} }
} }
@ -2116,9 +2116,9 @@ INTERNAL void user_update(void)
/* Render to user texture */ /* Render to user texture */
{ {
struct gpu_cmd_store *dispatch_cmds[] = { &G.world_gpu_cmd_store, &G.user_gpu_cmd_store }; struct gpu_handle *dispatch_cmds[] = { &G.world_gpu_cmd_store, &G.user_gpu_cmd_store };
struct gpu_dispatch_params params = ZI; struct gpu_dispatch_params params = ZI;
params.cmds_array.cmds = dispatch_cmds; params.cmds_array.stores = dispatch_cmds;
params.cmds_array.count = ARRAY_COUNT(dispatch_cmds); params.cmds_array.count = ARRAY_COUNT(dispatch_cmds);
params.draw_target = G.user_texture; params.draw_target = G.user_texture;
params.draw_target_viewport = user_viewport; params.draw_target_viewport = user_viewport;
@ -2127,9 +2127,9 @@ INTERNAL void user_update(void)
/* Render to backbuffer texture */ /* Render to backbuffer texture */
{ {
struct gpu_cmd_store *dispatch_cmds[] = { &G.backbuffer_gpu_cmd_store }; struct gpu_handle *dispatch_cmds[] = { &G.backbuffer_gpu_cmd_store };
struct gpu_dispatch_params params = ZI; struct gpu_dispatch_params params = ZI;
params.cmds_array.cmds = dispatch_cmds; params.cmds_array.stores = dispatch_cmds;
params.cmds_array.count = ARRAY_COUNT(dispatch_cmds); params.cmds_array.count = ARRAY_COUNT(dispatch_cmds);
params.draw_target = G.backbuffer_texture; params.draw_target = G.backbuffer_texture;
params.draw_target_viewport = backbuffer_viewport; params.draw_target_viewport = backbuffer_viewport;