readd mesh shader

This commit is contained in:
jacob 2025-05-27 04:25:59 -05:00
parent ee96df51e3
commit dc46fb270d
6 changed files with 130 additions and 99 deletions

View File

@ -2,24 +2,18 @@
struct vs_input {
DECL(float4, pos);
DECL(float2, uv);
DECL(float4, tint_srgb);
DECL(float4, color_srgb);
};
struct ps_input {
DESV(float4, screen_pos, SV_POSITION);
DECL(float2, uv);
DECL(float4, tint_lin);
DECL(float4, color_lin);
};
/* ========================== *
* Globals
* ========================== */
SamplerState G_sampler : register(s0);
Texture2D G_texture : register(t0);
cbuffer constants : register(b0)
{
float4x4 G_projection;
@ -33,8 +27,7 @@ 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.uv = input.uv;
output.tint_lin = linear_from_srgb(input.tint_srgb);
output.color_lin = linear_from_srgb(input.color_srgb);
return output;
}
@ -45,6 +38,5 @@ ps_input vs_main(vs_input input)
float4 ps_main(ps_input input) : SV_TARGET
{
float4 color = G_texture.Sample(G_sampler, input.uv) * input.tint_lin;
return color;
return input.color_lin;
}

View File

@ -47,10 +47,10 @@ void draw_texture(struct gpu_cmd_store store, struct draw_texture_params params)
void draw_poly_ex(struct gpu_cmd_store store, struct v2_array vertices, struct gpu_indices indices, u32 color)
{
struct gpu_cmd_params cmd = ZI;
cmd.kind = GPU_CMD_KIND_DRAW_TRIANGLES;
cmd.triangles.vertices = vertices;
cmd.triangles.indices = indices;
cmd.triangles.color = color;
cmd.kind = GPU_CMD_KIND_DRAW_MESH;
cmd.mesh.vertices = vertices;
cmd.mesh.indices = indices;
cmd.mesh.color = color;
gpu_push_cmd(store, cmd);
}

View File

@ -63,7 +63,7 @@ struct gpu_indices {
enum gpu_cmd_kind {
GPU_CMD_KIND_NONE,
GPU_CMD_KIND_DRAW_TRIANGLES,
GPU_CMD_KIND_DRAW_MESH,
GPU_CMD_KIND_DRAW_TEXTURE,
GPU_CMD_KIND_DRAW_GRID,
@ -77,7 +77,7 @@ struct gpu_cmd_params {
struct v2_array vertices;
struct gpu_indices indices;
u32 color;
} triangles;
} mesh;
struct {
struct xform xf;
struct sprite_tag sprite;

View File

@ -30,7 +30,7 @@
/* FIXME: Enable this and resolve unreleased references */
#if RTC
# define DX11_DEBUG 1
# define DX11_DEBUG 0
# define DX11_SHADER_DEBUG 1
#else
# define DX11_DEBUG 0
@ -44,7 +44,7 @@
enum dx11_shader_kind {
DX11_SHADER_KIND_NONE,
DX11_SHADER_KIND_TRIANGLE,
DX11_SHADER_KIND_MESH,
DX11_SHADER_KIND_TEXTURE,
DX11_SHADER_KIND_GRID,
@ -80,7 +80,7 @@ struct dx11_cmd_buffers {
struct {
struct dx11_buffer *vertex_buffer;
struct dx11_buffer *index_buffer;
} triangles;
} mesh;
struct {
struct dx11_buffer *instance_buffer;
} texture;
@ -98,7 +98,7 @@ struct dx11_cmd {
u32 vertex_count;
u32 index_offset;
u32 index_count;
} triangles;
} mesh;
struct {
struct gpu_texture texture; /* Overrides sprite if set */
struct sprite_tag sprite;
@ -128,7 +128,7 @@ struct dx11_cmd_store {
struct {
struct dx11_buffer *vertex_buffer;
struct dx11_buffer *index_buffer;
} triangles;
} mesh;
struct {
struct dx11_buffer *instance_buffer;
} texture;
@ -495,16 +495,15 @@ struct gpu_startup_receipt gpu_startup(struct sys_window *window)
* Shader table
* ========================== */
/* Triangle structs */
/* Mesh structs */
PACK(struct dx11_triangle_uniform {
PACK(struct dx11_mesh_uniform {
struct mat4x4 vp;
});
PACK(struct dx11_triangle_vertex {
PACK(struct dx11_mesh_vertex {
struct v2 pos;
struct v2 uv;
u32 tint_srgb;
u32 color_srgb;
});
/* Texture structs */
@ -544,14 +543,13 @@ INTERNAL void init_shader_table(void)
{
MEMZERO_ARRAY(G.shader_info);
/* Triangle shader layout */
G.shader_info[DX11_SHADER_KIND_TRIANGLE] = (struct dx11_shader_desc) {
.kind = DX11_SHADER_KIND_TRIANGLE,
.name_cstr = "shaders/triangle.hlsl",
/* Mesh shader layout */
G.shader_info[DX11_SHADER_KIND_MESH] = (struct dx11_shader_desc) {
.kind = DX11_SHADER_KIND_MESH,
.name_cstr = "shaders/mesh.hlsl",
.input_layout_desc = {
{ "pos", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "uv", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "tint_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 }
}
};
@ -1070,12 +1068,21 @@ struct gpu_cmd_store gpu_cmd_store_alloc(void)
store->buffers.constant_buffer = dx11_buffer_alloc(constant_buffer_desc, NULL);
}
/* Triangle buffers */
/* Mesh buffers */
{
#if 0
store->buffers.triangles.vertex_buffer = dx11_buffer_alloc(D3D11_BIND_VERTEX_BUFFER);
store->buffers.triangles.index_buffer = dx11_buffer_alloc(D3D11_BIND_INDEX_BUFFER);
#endif
D3D11_BUFFER_DESC vdesc = ZI;
vdesc.Usage = D3D11_USAGE_DYNAMIC;
vdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vdesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
/* Quad index buffer */
D3D11_BUFFER_DESC idesc = ZI;
idesc.Usage = D3D11_USAGE_DYNAMIC;
idesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
idesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
store->buffers.mesh.vertex_buffer = dx11_buffer_alloc(vdesc, NULL);
store->buffers.mesh.index_buffer = dx11_buffer_alloc(idesc, NULL);
}
/* Texture buffers */
@ -1115,9 +1122,47 @@ void gpu_push_cmd(struct gpu_cmd_store gpu_cmd_store, struct gpu_cmd_params para
ASSERT(false);
} break;
case GPU_CMD_KIND_DRAW_TRIANGLES:
case GPU_CMD_KIND_DRAW_MESH:
{
/* TODO */
struct dx11_cmd *cmd = store->cpu_last_cmd;
if (cmd && cmd->kind != params.kind) {
/* Cannot batch */
cmd = NULL;
}
/* Start new cmd */
if (!cmd) {
/* TODO: Better count method */
cmd = arena_push(&store->cpu_cmds_arena, struct dx11_cmd);
cmd->kind = params.kind;
cmd->mesh.vertex_offset = (store->buffers.mesh.vertex_buffer->cpu_buffer_arena.pos / sizeof(struct dx11_mesh_vertex));
cmd->mesh.index_offset = (store->buffers.mesh.index_buffer->cpu_buffer_arena.pos / sizeof(u16));
if (store->cpu_last_cmd) {
store->cpu_last_cmd->next = cmd;
} else {
store->cpu_first_cmd = cmd;
}
store->cpu_last_cmd = cmd;
}
/* Push vertices */
u64 vertex_count = params.mesh.vertices.count;
u64 start_index = cmd->mesh.vertex_count;
cmd->mesh.vertex_count += vertex_count;
struct dx11_mesh_vertex *verts = dx11_buffer_push(store->buffers.mesh.vertex_buffer, sizeof(struct dx11_mesh_vertex) * vertex_count);
for (u64 i = 0; i < vertex_count; ++i) {
struct dx11_mesh_vertex *vert = &verts[i];
vert->pos = params.mesh.vertices.points[i];
vert->color_srgb = params.mesh.color;
}
/* Push indices */
u64 index_count = params.mesh.indices.count;
u16 *indices = dx11_buffer_push(store->buffers.mesh.index_buffer, sizeof(u16) * index_count);
cmd->mesh.index_count += index_count;
for (u64 i = 0; i < index_count; ++i) {
indices[i] = start_index + params.mesh.indices.indices[i];
}
} break;
case GPU_CMD_KIND_DRAW_TEXTURE:
@ -1211,9 +1256,9 @@ void gpu_submit_cmds(struct gpu_cmd_store gpu_cmd_store)
store->cpu_last_cmd = NULL;
arena_reset(&store->cpu_cmds_arena);
/* Submit triangle buffers */
//dx11_buffer_submit(store->buffers.triangles.vertex_buffer);
//dx11_buffer_submit(store->buffers.triangles.index_buffer);
/* Submit mesh buffers */
dx11_buffer_submit(store->buffers.mesh.vertex_buffer);
dx11_buffer_submit(store->buffers.mesh.index_buffer);
/* Submit texture buffers */
dx11_buffer_submit(store->buffers.texture.instance_buffer);
@ -1261,60 +1306,56 @@ void gpu_run_cmds(struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target,
ASSERT(false);
} break;
case GPU_CMD_KIND_DRAW_TRIANGLES:
case GPU_CMD_KIND_DRAW_MESH:
{
/* TODO */
#if 0
__profscope_dx11(G.profiling_ctx, Triangles, RGB_F(0.2, 0.2, 0.5));
__profscope(Draw mesh);
__profscope_dx11(G.profiling_ctx, Draw mesh, RGB_F(0.5, 0.2, 0.2));
struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_MESH];
if (shader->valid) {
struct dx11_buffer *constant_buffer = store->buffers.constant_buffer;
struct dx11_buffer *vertex_buffer = store->buffers.mesh.vertex_buffer;
struct dx11_buffer *index_buffer = store->buffers.mesh.index_buffer;
/* FIXME: Texture refcount needs to be increased here to prevent release mid-render */
ID3D11Texture2D *texture = NULL;
if (cmd->texture.handle) {
/* Load texture if handle is set */
texture = ((struct dx11_texture *)cmd->texture.handle)->texture;
} else {
/* Otherwise load sprite */
struct sprite_texture *sprite_texture = sprite_texture_from_tag_async(sprite_scope, cmd->sprite);
if (sprite_texture->loaded) {
texture = ((struct dx11_texture *)sprite_texture->texture.handle)->texture;
u32 vertex_offset = cmd->mesh.vertex_offset;
u32 index_offset = cmd->mesh.index_offset;
u32 index_count = cmd->mesh.index_count;
/* Bind shader */
ID3D11DeviceContext_VSSetShader(G.devcon, shader->vs, 0, 0);
ID3D11DeviceContext_PSSetShader(G.devcon, shader->ps, 0, 0);
/* Bind input layout */
ID3D11DeviceContext_IASetInputLayout(G.devcon, shader->input_layout);
/* Fill & bind constant buffer */
{
struct dx11_mesh_uniform *uniform = dx11_buffer_push(constant_buffer, sizeof(struct dx11_mesh_uniform));
uniform->vp = vp_matrix;
dx11_buffer_submit(constant_buffer);
}
}
ID3D11DeviceContext_VSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer);
ID3D11DeviceContext_PSSetConstantBuffers(G.devcon, 0, 1, &constant_buffer->gpu_buffer);
if (texture) {
/* Create SRV */
D3D11_TEXTURE2D_DESC texture_desc = ZI;
ID3D11Texture2D_GetDesc(texture, &texture_desc);
D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc = { .Format = texture_desc.Format, .ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D, .Texture2D.MipLevels = texture_desc.MipLevels, .Texture2D.MostDetailedMip = 0 };
ID3D11ShaderResourceView *texture_srv = NULL;
ID3D11Device_CreateShaderResourceView(G.dev, (ID3D11Resource *)texture, &srv_desc, &texture_srv);
/* Bind texture */
ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, &texture_srv);
/* Activate buffer */
/* Bind vertex buffer */
u32 zero = 0;
UINT vertex_stride = shader->vertex_size;
ID3D11DeviceContext_IASetVertexBuffers(G.devcon, 0, 1, &buffer->gpu_vertex_buffer, &vertex_stride, &zero);
ID3D11DeviceContext_IASetIndexBuffer(G.devcon, buffer->gpu_index_buffer, DXGI_FORMAT_R32_UINT, zero);
u32 stride = sizeof(struct dx11_mesh_vertex);
ID3D11DeviceContext_IASetVertexBuffers(G.devcon, 0, 1, &vertex_buffer->gpu_buffer, &stride, &zero);
ID3D11DeviceContext_IASetIndexBuffer(G.devcon, index_buffer->gpu_buffer, DXGI_FORMAT_R16_UINT, zero);
/* Draw */
u32 vertex_offset = cmd->vertex_offset;
u32 index_offset = cmd->index_offset;
u32 index_count = cmd->index_count;
ID3D11DeviceContext_IASetPrimitiveTopology(G.devcon, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
ID3D11DeviceContext_DrawIndexed(G.devcon, index_count, index_offset, vertex_offset);
/* Release SRV */
ID3D11ShaderResourceView_Release(texture_srv);
/* Unbind */
ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 1, null_srv);
#endif
/* Unbind SRVs */
ID3D11DeviceContext_VSSetShaderResources(G.devcon, 0, 8, null_srvs);
ID3D11DeviceContext_PSSetShaderResources(G.devcon, 0, 8, null_srvs);
}
} break;
case GPU_CMD_KIND_DRAW_TEXTURE:
{
__profscope_dx11(G.profiling_ctx, Texture, RGB_F(0.2, 0.2, 0.5));
__profscope(Draw texture);
__profscope_dx11(G.profiling_ctx, Draw texture, RGB_F(0.2, 0.5, 0.2));
struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_TEXTURE];
if (shader->valid) {
struct dx11_texture *texture = NULL;
@ -1327,9 +1368,6 @@ void gpu_run_cmds(struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target,
if (sprite_texture->loaded) {
texture = (struct dx11_texture *)sprite_texture->texture.handle;
}
if (cmd->texture.sprite.hash == 13036381201575792585ULL) {
DEBUGBREAKABLE;
}
}
if (texture && texture->srv && shader->valid) {
@ -1378,7 +1416,8 @@ void gpu_run_cmds(struct gpu_cmd_store gpu_cmd_store, struct gpu_texture target,
case GPU_CMD_KIND_DRAW_GRID:
{
__profscope_dx11(G.profiling_ctx, Grid, RGB_F(0.2, 0.5, 0.2));
__profscope(Draw grid);
__profscope_dx11(G.profiling_ctx, Draw grid, RGB_F(0.2, 0.2, 0.5));
struct dx11_shader *shader = &G.shaders[DX11_SHADER_KIND_GRID];
if (shader->valid) {
struct dx11_buffer *constant_buffer = store->buffers.constant_buffer;

View File

@ -209,7 +209,7 @@ void host_release(struct host *host)
{
atomic_i32_eval_exchange(&host->receiver_thread_shutdown_flag, 1);
sock_wake(host->sock);
while (!sys_thread_try_release(&host->receiver_thread, 0.001)) {
while (!sys_thread_try_release(&host->receiver_thread, 0.001f)) {
sock_wake(host->sock);
}
sys_mutex_release(&host->rcv_buffer_write_mutex);

View File

@ -109,7 +109,7 @@ INTERNAL struct sim_ent *test_spawn_chucker(struct sim_ent *parent)
struct collider_shape collider = ZI;
collider.count = 2;
collider.points[1] = V2(0, -0.5);
collider.radius = 0.1;
collider.radius = 0.1f;
zone->local_collider = collider;
chucker->chucker_zone = zone->id;
@ -517,7 +517,7 @@ INTERNAL void test_generate_walls(struct sim_snapshot *world)
if (wall_end == SIM_TILES_PER_CHUNK_SQRT) {
u64 end_hash = rand_u64_from_seed(*(u64 *)&end);
end_hash = rand_u64_from_seeds(end_hash, wall_dir);
dict_set(scratch.arena, &horizontal_ends_dict, end_hash, node);
dict_set(scratch.arena, &horizontal_ends_dict, end_hash, (u64)node);
}
wall_start = -1;
wall_end = -1;
@ -607,7 +607,7 @@ INTERNAL void test_generate_walls(struct sim_snapshot *world)
if (wall_end == SIM_TILES_PER_CHUNK_SQRT) {
u64 end_hash = rand_u64_from_seed(*(u64 *)&end);
end_hash = rand_u64_from_seeds(end_hash, wall_dir);
dict_set(scratch.arena, &vertical_ends_dict, end_hash, node);
dict_set(scratch.arena, &vertical_ends_dict, end_hash, (u64)node);
}
wall_start = -1;
wall_end = -1;
@ -643,7 +643,7 @@ INTERNAL void test_generate_walls(struct sim_snapshot *world)
wall_ent->local_collider.count = 2;
wall_ent->local_collider.points[1] = v2_sub(end, start);
LOCAL_PERSIST struct v2 dirs[4] = { V2(0, -1), V2(1, 0), V2(0, 1), V2(-1, 0) };
struct v2 dirs[4] = { V2(0, -1), V2(1, 0), V2(0, 1), V2(-1, 0) };
ASSERT(node->wall_dir >= 0 && (u32)node->wall_dir < ARRAY_COUNT(dirs));
wall_ent->collision_dir = dirs[node->wall_dir];
@ -729,7 +729,7 @@ INTERNAL PHYS_COLLISION_CALLBACK_FUNC_DEF(on_collision, data, step_ctx)
linear_velocity = v2_add(linear_velocity, v2_mul(v2_perp(normal), rand_f64_from_state(&step_ctx->rand, -perp_range, perp_range)));
f32 angular_velocity_range = 5;
f32 angular_velocity = rand_f64_from_seed(&step_ctx->rand, -angular_velocity_range, angular_velocity_range);
f32 angular_velocity = rand_f64_from_state(&step_ctx->rand, -angular_velocity_range, angular_velocity_range);
sim_ent_enable_prop(decal, SEPROP_KINEMATIC);
sim_ent_set_linear_velocity(decal, linear_velocity);
@ -1383,7 +1383,7 @@ void sim_step(struct sim_step_ctx *ctx)
/* Point collider */
bullet->local_collider.points[0] = V2(0, 0);
bullet->local_collider.count = 1;
bullet->local_collider.radius = 0.05;
bullet->local_collider.radius = 0.05f;
}
@ -1422,9 +1422,9 @@ void sim_step(struct sim_step_ctx *ctx)
def.e1 = target->id;
def.xf = xf0_to_xf1;
def.linear_spring_hz = 10;
def.linear_spring_damp = 0.3;
def.linear_spring_damp = 0.3f;
def.angular_spring_hz = 10;
def.angular_spring_damp = 0.3;
def.angular_spring_damp = 0.3f;
joint_ent->weld_joint_data = phys_weld_joint_from_def(def);
ent->chucker_joint = joint_ent->id;
}
@ -1647,9 +1647,9 @@ void sim_step(struct sim_step_ctx *ctx)
def.point_end = cursor;
def.max_force = F32_INFINITY;
def.linear_spring_hz = 5;
def.linear_spring_damp = 0.7;
def.linear_spring_damp = 0.7f;
def.angular_spring_hz = 1;
def.angular_spring_damp = 0.1;
def.angular_spring_damp = 0.1f;
joint_ent->mouse_joint_data = phys_mouse_joint_from_def(def);
} else if (sim_ent_is_valid_and_active(joint_ent)) {
joint_ent->mouse_joint_data.target = target_ent->id;