Skip to content

Commit

Permalink
Support compute pipelines and UAV textures
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 12, 2024
1 parent 21713ef commit 5f45f19
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope
device->d3d12.device->CreateShaderResourceView(texture->d3d12.resource, &desc, descriptor_handle);
}

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.Texture2D.MipSlice = 0;
desc.Texture2D.PlaneSlice = 0;

D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.descriptor_heap->GetCPUDescriptorHandleForHeapStart();
descriptor_handle.ptr += (set->descriptor_allocation.offset + index) * device->d3d12.cbv_srv_uav_increment;
device->d3d12.device->CreateUnorderedAccessView(texture->d3d12.resource, NULL, &desc, descriptor_handle);
}

void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_sampler *sampler, uint32_t index) {
D3D12_CPU_DESCRIPTOR_HANDLE src_handle = device->d3d12.all_samplers->GetCPUDescriptorHandleForHeapStart();
src_handle.ptr += sampler->d3d12.sampler_index * device->d3d12.sampler_increment;
Expand Down Expand Up @@ -68,3 +80,19 @@ void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, k
texture->d3d12.resource_state = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
}
}

void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, kope_g5_texture *texture) {
if (texture->d3d12.resource_state != D3D12_RESOURCE_STATE_UNORDERED_ACCESS) {
D3D12_RESOURCE_BARRIER barrier;
barrier.Transition.pResource = texture->d3d12.resource;
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)texture->d3d12.resource_state;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;

list->d3d12.list->ResourceBarrier(1, &barrier);

texture->d3d12.resource_state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ extern "C" {

void kope_d3d12_descriptor_set_set_buffer_view_cbv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_buffer *buffer, uint32_t index);
void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index);
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);
void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_sampler *sampler, uint32_t index);

void kope_d3d12_descriptor_set_prepare_cbv_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer);
void kope_d3d12_descriptor_set_prepare_srv_texture(kope_g5_command_list *list, kope_g5_texture *texture);
void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, kope_g5_texture *texture);

#ifdef __cplusplus
}
Expand Down
24 changes: 22 additions & 2 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp
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_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pipe, const kope_d3d12_pipeline_parameters *parameters) {
void kope_d3d12_render_pipeline_init(kope_d3d12_device *device, kope_d3d12_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,7 +333,27 @@ void kope_d3d12_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pi
device->device->CreateRootSignature(0, desc.VS.pShaderBytecode, desc.VS.BytecodeLength, IID_GRAPHICS_PPV_ARGS(&pipe->root_signature)));
}

void kope_d3d12_pipeline_destroy(kope_d3d12_pipeline *pipe) {
void kope_d3d12_render_pipeline_destroy(kope_d3d12_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) {
D3D12_COMPUTE_PIPELINE_STATE_DESC desc = {0};

desc.CS.pShaderBytecode = parameters->shader.data;
desc.CS.BytecodeLength = parameters->shader.size;
desc.pRootSignature = NULL;

kinc_microsoft_affirm(device->device->CreateComputePipelineState(&desc, IID_GRAPHICS_PPV_ARGS(&pipe->pipe)));

kinc_microsoft_affirm(
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) {
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,9 +8,13 @@
extern "C" {
#endif

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

void kope_d3d12_pipeline_destroy(kope_d3d12_pipeline *pipe);
void kope_d3d12_render_pipeline_destroy(kope_d3d12_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_destroy(kope_d3d12_pipeline *pipe);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@ typedef struct kope_d3d12_fragment_state {
size_t targets_count;
} kope_d3d12_fragment_state;

typedef struct kope_d3d12_pipeline_parameters {
typedef struct kope_d3d12_render_pipeline_parameters {
kope_d3d12_vertex_state vertex;
kope_d3d12_primitive_state primitive;
kope_d3d12_depth_stencil_state depth_stencil;
kope_d3d12_multisample_state multisample;
kope_d3d12_fragment_state fragment;
} kope_d3d12_pipeline_parameters;
} kope_d3d12_render_pipeline_parameters;

typedef struct kope_d3d12_compute_pipeline_parameters {
kope_d3d12_shader shader;
} kope_d3d12_compute_pipeline_parameters;

struct ID3D12PipelineState;
struct ID3D12RootSignature;
Expand Down
6 changes: 3 additions & 3 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ KOPE_FUNC void kope_g5_command_list_copy_texture_to_texture(kope_g5_command_list

KOPE_FUNC void kope_g5_command_list_clear_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer, size_t offset, uint64_t size);

// KOPE_FUNC void kope_g5_command_list_resolve_query_set(kope_g5_command_list *list, GPUQuerySet querySet, uint32_t firstQuery, uint32_t queryCount,
// kope_g5_buffer *destination, uint64_t destinationOffset);
// KOPE_FUNC void kope_g5_command_list_resolve_query_set(kope_g5_command_list *list, GPUQuerySet query_set, uint32_t first_query, uint32_t query_count,
// kope_g5_buffer *destination, uint64_t destination_offset);

typedef enum kope_g5_index_format { KOPE_G5_INDEX_FORMAT_UINT16, KOPE_G5_INDEX_FORMAT_UINT32 } kope_g5_index_format;

Expand All @@ -117,7 +117,7 @@ KOPE_FUNC void kope_g5_command_list_set_stencil_reference(kope_g5_command_list *

KOPE_FUNC void kope_g5_command_list_begin_occlusion_query(kope_g5_command_list *list, uint32_t query_index);

KOPE_FUNC void kope_g5_command_list_endOcclusionQuery(kope_g5_command_list *list);
KOPE_FUNC void kope_g5_command_list_end_occlusion_query(kope_g5_command_list *list);

KOPE_FUNC void kope_g5_command_list_end_render_pass(kope_g5_command_list *list);

Expand Down

0 comments on commit 5f45f19

Please sign in to comment.