Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/engine/gfx/public/gfx_commandbuffer.hpp

375 lines
11 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#pragma once
#include <vector>
#include <cstring>
#include <array>
#include <string_view>
#include "common.hpp"
class GFXPipeline;
class GFXBuffer;
class GFXFramebuffer;
class GFXRenderPass;
class GFXSampler;
struct GFXRenderPassBeginInfo {
struct ClearColor {
2022-03-06 21:40:00 -05:00
float r = 0.0f, g = 0.0f, b = 0.0f, a = 1.0f;
2020-08-11 12:07:21 -04:00
} clear_color;
2020-08-13 07:48:50 -04:00
prism::Rectangle render_area;
2020-08-11 12:07:21 -04:00
GFXRenderPass* render_pass = nullptr;
GFXFramebuffer* framebuffer = nullptr;
};
struct Viewport {
float x = 0.0f;
float y = 0.0f;
float width = 0.0f;
float height = 0.0f;
float min_depth = 0.0f;
float max_depth = 1.0f;
};
enum class IndexType : int {
UINT16 = 0,
UINT32 = 1
};
enum class GFXCommandType {
Invalid,
SetRenderPass,
SetGraphicsPipeline,
SetComputePipeline,
2020-08-11 12:07:21 -04:00
SetVertexBuffer,
SetIndexBuffer,
SetPushConstant,
BindShaderBuffer,
BindTexture,
BindSampler,
Draw,
DrawIndexed,
MemoryBarrier,
CopyTexture,
SetViewport,
SetScissor,
GenerateMipmaps,
SetDepthBias,
PushGroup,
PopGroup,
InsertLabel,
2021-02-15 17:59:54 -05:00
Dispatch,
EndRenderPass
2020-08-11 12:07:21 -04:00
};
struct GFXDrawCommand {
GFXCommandType type = GFXCommandType::Invalid;
struct CommandData {
GFXRenderPassBeginInfo set_render_pass;
struct SetGraphicsPipelineData {
2020-08-11 12:07:21 -04:00
GFXPipeline* pipeline = nullptr;
} set_graphics_pipeline;
struct SetComputePipelineData {
GFXPipeline* pipeline = nullptr;
} set_compute_pipeline;
2020-08-11 12:07:21 -04:00
struct SetVertexData {
GFXBuffer* buffer = nullptr;
int offset = 0;
int index = 0;
} set_vertex_buffer;
struct SetIndexData {
GFXBuffer* buffer = nullptr;
IndexType index_type = IndexType::UINT32;
} set_index_buffer;
struct SetPushData {
std::vector<unsigned char> bytes;
size_t size = 0;
} set_push_constant;
struct BindShaderData {
GFXBuffer* buffer = nullptr;
int offset = 0;
int index = 0;
int size = 0;
} bind_shader_buffer;
struct BindTextureData {
GFXTexture* texture = nullptr;
int index = 0;
} bind_texture;
struct BindSamplerData {
GFXSampler* sampler = nullptr;
int index = 0;
} bind_sampler;
struct DrawData {
2022-03-06 19:16:54 -05:00
uint32_t vertex_offset = 0;
uint32_t vertex_count = 0;
uint32_t base_instance = 0;
uint32_t instance_count = 0;
2020-08-11 12:07:21 -04:00
} draw;
struct DrawIndexedData {
2022-03-06 19:16:54 -05:00
uint32_t index_count = 0;
uint32_t first_index = 0;
int32_t vertex_offset = 0;
uint32_t base_instance = 0;
2020-08-11 12:07:21 -04:00
} draw_indexed;
struct CopyTextureData {
GFXTexture* src = nullptr, *dst = nullptr;
int width = 0, height = 0;
int to_slice = 0;
int to_layer = 0;
int to_level = 0;
} copy_texture;
struct SetViewportData {
Viewport viewport;
} set_viewport;
struct SetScissorData {
2020-08-13 07:48:50 -04:00
prism::Rectangle rect;
2020-08-11 12:07:21 -04:00
} set_scissor;
struct GenerateMipmapData {
GFXTexture* texture = nullptr;
int mip_count = 0;
} generate_mipmaps;
struct SetDepthBiasData {
float constant = 0.0f;
float clamp = 0.0f;
float slope_factor = 0.0f;
} set_depth_bias;
struct PushGroupData {
std::string_view name;
} push_group;
struct InsertLabelData {
std::string_view name;
} insert_label;
struct DispatchData {
2021-10-14 08:51:58 -04:00
uint32_t group_count_x = 0, group_count_y = 0, group_count_z = 0;
} dispatch;
2020-08-11 12:07:21 -04:00
} data;
};
class GFXCommandBuffer {
public:
void set_render_pass(GFXRenderPassBeginInfo& info) {
GFXDrawCommand command;
command.type = GFXCommandType::SetRenderPass;
command.data.set_render_pass = info;
commands.push_back(command);
}
void set_graphics_pipeline(GFXPipeline* pipeline) {
2020-08-11 12:07:21 -04:00
GFXDrawCommand command;
command.type = GFXCommandType::SetGraphicsPipeline;
command.data.set_graphics_pipeline.pipeline = pipeline;
commands.push_back(command);
}
void set_compute_pipeline(GFXPipeline* pipeline) {
GFXDrawCommand command;
command.type = GFXCommandType::SetComputePipeline;
command.data.set_compute_pipeline.pipeline = pipeline;
2020-08-11 12:07:21 -04:00
commands.push_back(command);
}
void set_vertex_buffer(GFXBuffer* buffer, int offset, int index) {
GFXDrawCommand command;
command.type = GFXCommandType::SetVertexBuffer;
command.data.set_vertex_buffer.buffer = buffer;
command.data.set_vertex_buffer.offset = offset;
command.data.set_vertex_buffer.index = index;
commands.push_back(command);
}
void set_index_buffer(GFXBuffer* buffer, IndexType indexType) {
GFXDrawCommand command;
command.type = GFXCommandType::SetIndexBuffer;
command.data.set_index_buffer.buffer = buffer;
command.data.set_index_buffer.index_type = indexType;
commands.push_back(command);
}
void set_push_constant(const void* data, const size_t size) {
GFXDrawCommand command;
command.type = GFXCommandType::SetPushConstant;
command.data.set_push_constant.size = size;
command.data.set_push_constant.bytes.resize(size);
memcpy(command.data.set_push_constant.bytes.data(), data, size);
commands.push_back(command);
}
void bind_shader_buffer(GFXBuffer* buffer, int offset, int index, int size) {
GFXDrawCommand command;
command.type = GFXCommandType::BindShaderBuffer;
command.data.bind_shader_buffer.buffer = buffer;
command.data.bind_shader_buffer.offset = offset;
command.data.bind_shader_buffer.index = index;
command.data.bind_shader_buffer.size = size;
commands.push_back(command);
}
void bind_texture(GFXTexture* texture, int index) {
GFXDrawCommand command;
command.type = GFXCommandType::BindTexture;
command.data.bind_texture.texture = texture;
command.data.bind_texture.index = index;
commands.push_back(command);
}
void bind_sampler(GFXSampler* sampler, const int index) {
GFXDrawCommand command;
command.type = GFXCommandType::BindSampler;
command.data.bind_sampler.sampler = sampler;
command.data.bind_sampler.index = index;
commands.push_back(command);
}
2022-03-06 19:16:54 -05:00
void draw(const uint32_t offset, const uint32_t count, const uint32_t instance_base, const uint32_t instance_count) {
2020-08-11 12:07:21 -04:00
GFXDrawCommand command;
command.type = GFXCommandType::Draw;
command.data.draw.vertex_offset = offset;
command.data.draw.vertex_count = count;
command.data.draw.base_instance = instance_base;
command.data.draw.instance_count = instance_count;
commands.push_back(command);
}
2022-03-06 19:16:54 -05:00
void draw_indexed(const uint32_t indexCount, const uint32_t firstIndex, const int32_t vertexOffset, const uint32_t base_instance) {
2020-08-11 12:07:21 -04:00
GFXDrawCommand command;
command.type = GFXCommandType::DrawIndexed;
command.data.draw_indexed.vertex_offset = vertexOffset;
command.data.draw_indexed.first_index = firstIndex;
command.data.draw_indexed.index_count = indexCount;
2020-09-23 09:53:45 -04:00
command.data.draw_indexed.base_instance = base_instance;
2020-08-11 12:07:21 -04:00
commands.push_back(command);
}
void memory_barrier() {
GFXDrawCommand command;
command.type = GFXCommandType::MemoryBarrier;
commands.push_back(command);
}
void copy_texture(GFXTexture* src, int width, int height, GFXTexture* dst, int to_slice, int to_layer, int to_level) {
GFXDrawCommand command;
command.type = GFXCommandType::CopyTexture;
command.data.copy_texture.src = src;
command.data.copy_texture.width = width;
command.data.copy_texture.height = height;
command.data.copy_texture.dst = dst;
command.data.copy_texture.to_slice = to_slice;
command.data.copy_texture.to_layer = to_layer;
command.data.copy_texture.to_level = to_level;
commands.push_back(command);
}
void set_viewport(Viewport viewport) {
GFXDrawCommand command;
command.type = GFXCommandType::SetViewport;
command.data.set_viewport.viewport = viewport;
commands.push_back(command);
}
2020-08-13 07:48:50 -04:00
void set_scissor(const prism::Rectangle rect) {
2020-08-11 12:07:21 -04:00
GFXDrawCommand command;
command.type = GFXCommandType::SetScissor;
command.data.set_scissor.rect = rect;
commands.push_back(command);
}
void generate_mipmaps(GFXTexture* texture, int mip_count) {
GFXDrawCommand command;
command.type = GFXCommandType::GenerateMipmaps;
command.data.generate_mipmaps.texture = texture;
command.data.generate_mipmaps.mip_count = mip_count;
commands.push_back(command);
}
void set_depth_bias(const float constant, const float clamp, const float slope) {
GFXDrawCommand command;
command.type = GFXCommandType::SetDepthBias;
command.data.set_depth_bias.constant = constant;
command.data.set_depth_bias.clamp = clamp;
command.data.set_depth_bias.slope_factor = slope;
commands.push_back(command);
}
void push_group(const std::string_view name) {
GFXDrawCommand command;
command.type = GFXCommandType::PushGroup;
command.data.push_group.name = name;
commands.push_back(command);
}
void pop_group() {
GFXDrawCommand command;
command.type = GFXCommandType::PopGroup;
commands.push_back(command);
}
void insert_label(const std::string_view name) {
GFXDrawCommand command;
command.type = GFXCommandType::InsertLabel;
command.data.insert_label.name = name;
commands.push_back(command);
}
void dispatch(const uint32_t group_count_x, const uint32_t group_count_y, const uint32_t group_count_z) {
GFXDrawCommand command;
command.type = GFXCommandType::Dispatch;
command.data.dispatch.group_count_x = group_count_x;
command.data.dispatch.group_count_y = group_count_y;
command.data.dispatch.group_count_z = group_count_z;
commands.push_back(command);
}
2021-02-15 17:59:54 -05:00
void end_render_pass() {
GFXDrawCommand command;
command.type = GFXCommandType::EndRenderPass;
commands.push_back(command);
}
2020-08-11 12:07:21 -04:00
std::vector<GFXDrawCommand> commands;
};