Skip to content

Commit

Permalink
Implement a draw command
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 2, 2024
1 parent 9eaa786 commit 6085134
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <kope/graphics5/commandlist.h>
#include <kope/graphics5/device.h>

#include "pipeline_structs.h"

#include <assert.h>

void kope_d3d12_command_list_begin_render_pass(kope_g5_command_list *list, const kope_g5_render_pass_parameters *parameters) {
Expand Down Expand Up @@ -54,4 +56,26 @@ void kope_d3d12_command_list_set_index_buffer(kope_g5_command_list *list, kope_g
view.Format = index_format == KOPE_G5_INDEX_FORMAT_UINT16 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;

list->d3d12.list->IASetIndexBuffer(&view);
}
}

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) {
D3D12_VERTEX_BUFFER_VIEW view = {0};

view.BufferLocation = buffer->resource->GetGPUVirtualAddress() + offset;
view.SizeInBytes = (UINT)size;
view.StrideInBytes = (UINT)stride;

list->d3d12.list->IASetVertexBuffers(slot, 1, &view);
}

void kope_d3d12_command_list_set_pipeline(kope_g5_command_list *list, kope_d3d12_pipeline *pipeline) {
list->d3d12.list->SetPipelineState(pipeline->pipe);
list->d3d12.list->SetGraphicsRootSignature(pipeline->root_signature);
}

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) {
list->d3d12.list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
list->d3d12.list->DrawIndexedInstanced(index_count, instance_count, first_index, base_vertex, first_instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <kope/graphics5/commandlist.h>

#include "pipeline_structs.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -16,6 +18,14 @@ void kope_d3d12_command_list_present(kope_g5_command_list *list);
void kope_d3d12_command_list_set_index_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer, kope_g5_index_format index_format, uint64_t offset,
uint64_t size);

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_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);

#ifdef __cplusplus
}
#endif
Expand Down
19 changes: 9 additions & 10 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,30 +332,29 @@ void kope_d3d12_pipeline_init(kope_d3d12_device *device, kope_d3d12_pipeline *pi
desc.RasterizerState.ForcedSampleCount = 0;
desc.RasterizerState.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;

desc.BlendState.IndependentBlendEnable = FALSE;

bool independent_blend = false;
for (int i = 1; i < KOPE_D3D12_MAX_COLOR_TARGETS; ++i) {
if (parameters->fragment.targets[0].write_mask != parameters->fragment.targets[i].write_mask) {
for (int i = 1; i < parameters->fragment.targets_count; ++i) {
if (memcmp(&parameters->fragment.targets[0], &parameters->fragment.targets[i], sizeof(kope_d3d12_color_target_state)) != 0) {
independent_blend = true;
break;
}
}

desc.BlendState.IndependentBlendEnable = independent_blend ? TRUE : FALSE;

set_blend_state(&desc.BlendState, &parameters->fragment.targets[0], 0);
if (independent_blend) {
desc.BlendState.IndependentBlendEnable = true;
for (int i = 1; i < KOPE_D3D12_MAX_COLOR_TARGETS; ++i) {
for (int i = 1; i < parameters->fragment.targets_count; ++i) {
set_blend_state(&desc.BlendState, &parameters->fragment.targets[i], i);
}
}

desc.pRootSignature = NULL;

HRESULT result = device->device->CreateGraphicsPipelineState(&desc, IID_GRAPHICS_PPV_ARGS(&pipe->pipe));
if (result != S_OK) {
kinc_log(KINC_LOG_LEVEL_WARNING, "Could not create pipeline.");
}
kinc_microsoft_affirm(device->device->CreateGraphicsPipelineState(&desc, IID_GRAPHICS_PPV_ARGS(&pipe->pipe)));

kinc_microsoft_affirm(
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@ typedef struct kope_d3d12_pipeline_parameters {
} kope_d3d12_pipeline_parameters;

struct ID3D12PipelineState;
struct ID3D12RootSignature;

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

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions Sources/kope/graphics5/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef enum kope_g5_api { KOPE_G5_API_DIRECT3D12, KOPE_G5_API_VULKAN } kope_g5_
#define KOPE_G5_CALL3(name, arg0, arg1, arg2) kope_d3d12_##name(arg0, arg1, arg2)
#define KOPE_G5_CALL4(name, arg0, arg1, arg2, arg3) kope_d3d12_##name(arg0, arg1, arg2, arg3)
#define KOPE_G5_CALL5(name, arg0, arg1, arg2, arg3, arg4) kope_d3d12_##name(arg0, arg1, arg2, arg3, arg4)
#define KOPE_G5_CALL6(name, arg0, arg1, arg2, arg3, arg4, arg5) kope_d3d12_##name(arg0, arg1, arg2, arg3, arg4, arg5)

#endif

Expand All @@ -89,6 +90,7 @@ typedef enum kope_g5_api { KOPE_G5_API_DIRECT3D12, KOPE_G5_API_VULKAN } kope_g5_
#define KOPE_G5_CALL3(name, arg0, arg1, arg2) kope_vulkan_##name(arg0, arg1, arg2)
#define KOPE_G5_CALL4(name, arg0, arg1, arg2, arg3) kope_vulkan_##name(arg0, arg1, arg2, arg3)
#define KOPE_G5_CALL5(name, arg0, arg1, arg2, arg3, arg4) kope_vulkan_##name(arg0, arg1, arg2, arg3, arg4)
#define KOPE_G5_CALL6(name, arg0, arg1, arg2, arg3, arg4, arg5) kope_vulkan_##name(arg0, arg1, arg2, arg3, arg4, arg5)

#endif

Expand Down
5 changes: 5 additions & 0 deletions Sources/kope/graphics5/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ void kope_g5_command_list_set_index_buffer(kope_g5_command_list *list, kope_g5_b
uint64_t size) {
KOPE_G5_CALL5(command_list_set_index_buffer, list, buffer, index_format, offset, size);
}

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);
}
4 changes: 0 additions & 4 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ KOPE_FUNC void kope_g5_command_list_clear_buffer(kope_g5_command_list *list, kop
// 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_set_pipeline(kope_g5_command_list *list, void *pipeline);

typedef enum kope_g5_index_format { KOPE_G5_INDEX_FORMAT_UINT16, KOPE_G5_INDEX_FORMAT_UINT32 } kope_g5_index_format;

KOPE_FUNC void kope_g5_command_list_set_index_buffer(kope_g5_command_list *list, kope_g5_buffer *buffer, kope_g5_index_format index_format, uint64_t offset,
uint64_t size);

KOPE_FUNC void kope_g5_command_list_set_vertex_buffer(kope_g5_command_list *list, uint32_t slot, kope_g5_buffer *buffer, uint64_t offset, uint64_t size);

KOPE_FUNC void kope_g5_command_list_draw(kope_g5_command_list *list, uint32_t vertex_count, uint32_t instance_count, uint32_t first_vertex,
uint32_t first_instance);

Expand Down

0 comments on commit 6085134

Please sign in to comment.