Skip to content

Commit

Permalink
Make compute work
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 13, 2024
1 parent 5f45f19 commit 2a24600
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <assert.h>

void kope_d3d12_command_list_begin_render_pass(kope_g5_command_list *list, const kope_g5_render_pass_parameters *parameters) {
list->d3d12.compute_pipeline_set = false;

kope_g5_texture *render_target = parameters->color_attachments[0].texture;

if (render_target->d3d12.in_flight_frame_index > 0) {
Expand Down Expand Up @@ -77,7 +79,7 @@ void kope_d3d12_command_list_set_vertex_buffer(kope_g5_command_list *list, uint3
list->d3d12.list->IASetVertexBuffers(slot, 1, &view);
}

void kope_d3d12_command_list_set_pipeline(kope_g5_command_list *list, kope_d3d12_pipeline *pipeline) {
void kope_d3d12_command_list_set_render_pipeline(kope_g5_command_list *list, kope_d3d12_render_pipeline *pipeline) {
list->d3d12.list->SetPipelineState(pipeline->pipe);
list->d3d12.list->SetGraphicsRootSignature(pipeline->root_signature);
}
Expand All @@ -92,14 +94,24 @@ void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, ui
if (set->descriptor_count > 0) {
D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->descriptor_heap->GetGPUDescriptorHandleForHeapStart();
gpu_descriptor.ptr += set->descriptor_allocation.offset * list->d3d12.device->cbv_srv_uav_increment;
list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor);
if (list->d3d12.compute_pipeline_set) {
list->d3d12.list->SetComputeRootDescriptorTable(table_index, gpu_descriptor);
}
else {
list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor);
}
table_index += 1;
}

if (set->sampler_count > 0) {
D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->sampler_heap->GetGPUDescriptorHandleForHeapStart();
gpu_descriptor.ptr += set->sampler_allocation.offset * list->d3d12.device->sampler_increment;
list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor);
if (list->d3d12.compute_pipeline_set) {
list->d3d12.list->SetComputeRootDescriptorTable(table_index, gpu_descriptor);
}
else {
list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor);
}
}
}

Expand Down Expand Up @@ -149,3 +161,13 @@ void kope_g5_command_list_copy_buffer_to_texture(kope_g5_command_list *list, kop

list->d3d12.list->CopyTextureRegion(&dst, 0, 0, 0, &src, NULL);
}

void kope_d3d12_command_list_set_compute_pipeline(kope_g5_command_list *list, kope_d3d12_compute_pipeline *pipeline) {
list->d3d12.compute_pipeline_set = true;
list->d3d12.list->SetPipelineState(pipeline->pipe);
list->d3d12.list->SetComputeRootSignature(pipeline->root_signature);
}

void kope_d3d12_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z) {
list->d3d12.list->Dispatch(workgroup_count_x, workgroup_count_y, workgroup_count_z);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ void kope_d3d12_command_list_set_index_buffer(kope_g5_command_list *list, kope_g
void kope_d3d12_command_list_set_vertex_buffer(kope_g5_command_list *list, uint32_t slot, kope_d3d12_buffer *buffer, uint64_t offset, uint64_t size,
uint64_t stride);

void kope_d3d12_command_list_set_pipeline(kope_g5_command_list *list, kope_d3d12_pipeline *pipeline);
void kope_d3d12_command_list_set_render_pipeline(kope_g5_command_list *list, kope_d3d12_render_pipeline *pipeline);

void kope_d3d12_command_list_draw_indexed(kope_g5_command_list *list, uint32_t index_count, uint32_t instance_count, uint32_t first_index, int32_t base_vertex,
uint32_t first_instance);

void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, uint32_t table_index, kope_d3d12_descriptor_set *set);

void kope_d3d12_command_list_set_compute_pipeline(kope_g5_command_list *list, kope_d3d12_compute_pipeline *pipeline);

void kope_d3d12_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef struct kope_d3d12_command_list {
// set when a framebuffer is attached to a render-pass so we don't render into it during scan-out
uint64_t blocking_frame_index;

bool compute_pipeline_set;

bool presenting;
} kope_d3d12_command_list;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope
D3D12_SHADER_RESOURCE_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.Format = (DXGI_FORMAT)texture->d3d12.format;
desc.Texture2D.MipLevels = 1;
desc.Texture2D.MostDetailedMip = 0;
desc.Texture2D.ResourceMinLODClamp = 0.0f;
Expand All @@ -30,7 +30,7 @@ void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope
void kope_d3d12_descriptor_set_set_texture_view_uav(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index) {
D3D12_UNORDERED_ACCESS_VIEW_DESC desc = {};
desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.Format = (DXGI_FORMAT)texture->d3d12.format;
desc.Texture2D.MipSlice = 0;
desc.Texture2D.PlaneSlice = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_comma
device->d3d12.device->CreateFence(list->d3d12.execution_index - 1, D3D12_FENCE_FLAG_NONE, IID_GRAPHICS_PPV_ARGS(&list->d3d12.fence));
list->d3d12.event = CreateEvent(NULL, FALSE, FALSE, NULL);

list->d3d12.compute_pipeline_set = false;

list->d3d12.blocking_frame_index = 0;

list->d3d12.presenting = false;
Expand Down Expand Up @@ -422,10 +424,15 @@ void kope_d3d12_device_create_texture(kope_g5_device *device, const kope_g5_text
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
desc.Flags = D3D12_RESOURCE_FLAG_NONE;

if ((parameters->usage & KONG_G5_TEXTURE_USAGE_READ_WRITE) != 0) {
desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}

kinc_microsoft_affirm(device->d3d12.device->CreateCommittedResource(
&heap_properties, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, NULL, IID_GRAPHICS_PPV_ARGS(&texture->d3d12.resource)));

texture->d3d12.resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
texture->d3d12.format = format;

texture->d3d12.in_flight_frame_index = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "d3d12mini.h"

#include <kope/graphics5/commandlist.h>
#include <kope/graphics5/texture.h>
#include <kope/util/indexallocator.h>
#include <kope/util/offalloc/offalloc.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static void set_blend_state(D3D12_BLEND_DESC *desc, const kope_d3d12_color_targe
desc->RenderTarget[target].RenderTargetWriteMask = (UINT8)target_state->write_mask;
}

void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_render_pipeline_parameters *parameters) {
void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_render_pipeline *pipe, const kope_d3d12_render_pipeline_parameters *parameters) {
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc = {0};

desc.VS.BytecodeLength = parameters->vertex.shader.size;
Expand Down Expand Up @@ -333,14 +333,14 @@ void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipel
device->device->CreateRootSignature(0, desc.VS.pShaderBytecode, desc.VS.BytecodeLength, IID_GRAPHICS_PPV_ARGS(&pipe->root_signature)));
}

void kope_d3d12_render_pipeline_destroy(kope_d3d12_pipeline *pipe) {
void kope_d3d12_render_pipeline_destroy(kope_d3d12_render_pipeline *pipe) {
if (pipe->pipe != NULL) {
pipe->pipe->Release();
pipe->pipe = NULL;
}
}

void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_compute_pipeline_parameters *parameters) {
void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_compute_pipeline *pipe, const kope_d3d12_compute_pipeline_parameters *parameters) {
D3D12_COMPUTE_PIPELINE_STATE_DESC desc = {0};

desc.CS.pShaderBytecode = parameters->shader.data;
Expand All @@ -353,7 +353,7 @@ void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipe
device->device->CreateRootSignature(0, desc.CS.pShaderBytecode, desc.CS.BytecodeLength, IID_GRAPHICS_PPV_ARGS(&pipe->root_signature)));
}

void kope_d3d12_compute_pipeline_destroy(kope_d3d12_pipeline *pipe) {
void kope_d3d12_compute_pipeline_destroy(kope_d3d12_compute_pipeline *pipe) {
if (pipe->pipe != NULL) {
pipe->pipe->Release();
pipe->pipe = NULL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
extern "C" {
#endif

void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_render_pipeline_parameters *parameters);
void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_render_pipeline *pipe, const kope_d3d12_render_pipeline_parameters *parameters);

void kope_d3d12_render_pipeline_destroy(kope_d3d12_pipeline *pipe);
void kope_d3d12_render_pipeline_destroy(kope_d3d12_render_pipeline *pipe);

void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_compute_pipeline_parameters *parameters);
void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_compute_pipeline *pipe, const kope_d3d12_compute_pipeline_parameters *parameters);

void kope_d3d12_compute_pipeline_destroy(kope_d3d12_pipeline *pipe);
void kope_d3d12_compute_pipeline_destroy(kope_d3d12_compute_pipeline *pipe);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,15 @@ typedef struct kope_d3d12_compute_pipeline_parameters {
struct ID3D12PipelineState;
struct ID3D12RootSignature;

typedef struct kope_d3d12_pipeline {
typedef struct kope_d3d12_render_pipeline {
struct ID3D12PipelineState *pipe;
struct ID3D12RootSignature *root_signature;
} kope_d3d12_pipeline;
} kope_d3d12_render_pipeline;

typedef struct kope_d3d12_compute_pipeline {
struct ID3D12PipelineState *pipe;
struct ID3D12RootSignature *root_signature;
} kope_d3d12_compute_pipeline;

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef struct kope_d3d12_texture {
uint32_t dsv_index;

uint32_t resource_state;
uint32_t format;

// used for framebuffer-synchronization
uint64_t in_flight_frame_index;
Expand Down
6 changes: 5 additions & 1 deletion Sources/kope/graphics5/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ void kope_g5_command_list_set_index_buffer(kope_g5_command_list *list, kope_g5_b
void kope_g5_command_list_draw_indexed(kope_g5_command_list *list, uint32_t index_count, uint32_t instance_count, uint32_t first_index, int32_t base_vertex,
uint32_t first_instance) {
KOPE_G5_CALL6(command_list_draw_indexed, list, index_count, instance_count, first_index, base_vertex, first_instance);
}
}

void kope_g5_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z) {
KOPE_G5_CALL4(command_list_compute, list, workgroup_count_x, workgroup_count_y, workgroup_count_z);
}
2 changes: 0 additions & 2 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ KOPE_FUNC void kope_g5_command_list_end_occlusion_query(kope_g5_command_list *li

KOPE_FUNC void kope_g5_command_list_end_render_pass(kope_g5_command_list *list);

KOPE_FUNC void kope_g5_command_list_set_compute_shader(kope_g5_command_list *list, void *shader);

KOPE_FUNC void kope_g5_command_list_compute(kope_g5_command_list *list, uint32_t workgroup_count_x, uint32_t workgroup_count_y, uint32_t workgroup_count_z);

KOPE_FUNC void kope_g5_command_list_compute_indirect(kope_g5_command_list *list, kope_g5_buffer *indirect_buffer, uint64_t indirect_offset);
Expand Down

0 comments on commit 2a24600

Please sign in to comment.