Skip to content

Commit

Permalink
Allow to pass custom command buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Nov 14, 2024
1 parent 5755d62 commit 0e703aa
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 172 deletions.
25 changes: 21 additions & 4 deletions RendererBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

struct ID3D12Resource;
struct ID3D12DescriptorHeap;
struct ID3D12GraphicsCommandList;

/**
@file RendererBase.h
Expand Down Expand Up @@ -107,13 +108,26 @@ struct GpuImage {
eGPUResState state;

GpuImage() { memset(this, 0, sizeof(GpuImage)); }
GpuImage(VkImage _vk_image, VkImageView _vk_image_view, eGPUResState _state)
GpuImage(const VkImage _vk_image, const VkImageView _vk_image_view, const eGPUResState _state)
: vk_image(_vk_image), vk_image_view(_vk_image_view), state(_state) {}
GpuImage(ID3D12Resource *_dx_image, ID3D12DescriptorHeap *dx_view_heap, uint32_t dx_view_offset,
eGPUResState _state)
GpuImage(ID3D12Resource *const _dx_image, ID3D12DescriptorHeap *const dx_view_heap, const uint32_t dx_view_offset,
const eGPUResState _state)
: dx_image(_dx_image), dx_image_view{dx_view_heap, dx_view_offset}, state(_state) {}
};

struct GpuCommandBuffer {
union {
VkCommandBuffer vk_cmd_buf;
ID3D12GraphicsCommandList *dx_cmd_buf;
};
int index;

GpuCommandBuffer() { memset(this, 0, sizeof(GpuCommandBuffer)); }
GpuCommandBuffer(const VkCommandBuffer _vk_cmd_buf, const int _index) : vk_cmd_buf(_vk_cmd_buf), index(_index) {}
GpuCommandBuffer(ID3D12GraphicsCommandList *const _dx_cmd_buf, const int _index)
: dx_cmd_buf(_dx_cmd_buf), index(_index) {}
};

/** Base class for all renderer backends
*/
class RendererBase {
Expand Down Expand Up @@ -154,7 +168,10 @@ class RendererBase {
virtual GpuImage get_native_raw_pixels() const { return {}; }

/// Allows to set native GPU image state
virtual void set_native_raw_pixels_state(const eGPUResState state) {}
virtual void set_native_raw_pixels_state(const eGPUResState) {}

/// Allows to set native GPU command buffer to use
virtual void set_command_buffer(const GpuCommandBuffer) {}

/** @brief Resize framebuffer
@param w new image width
Expand Down
2 changes: 2 additions & 0 deletions internal/Dx/ContextDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class Context {
bool render_finished_semaphore_is_set[MaxFramesInFlight] = {};
uint64_t fence_values[MaxFramesInFlight] = {};

bool frame_cpu_synced[MaxFramesInFlight] = {};

Buffer uniform_data_bufs[MaxFramesInFlight];
uint32_t uniform_data_buf_offs[MaxFramesInFlight];

Expand Down
29 changes: 29 additions & 0 deletions internal/RendererDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,35 @@ Ray::eRendererType Ray::Dx::Renderer::type() const { return eRendererType::Direc

const char *Ray::Dx::Renderer::device_name() const { return ctx_->device_name().c_str(); }

inline void Ray::Dx::Renderer::Clear(const color_rgba_t &c) {
CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool());

const TransitionInfo img_transitions[] = {{&full_buf_, ResStateForClear},
{&half_buf_, ResStateForClear},
{&final_buf_, ResStateForClear},
{&raw_filtered_buf_, ResStateForClear},
{&base_color_buf_, ResStateForClear},
{&depth_normals_buf_, ResStateForClear},
{&required_samples_buf_, ResStateForClear}};
TransitionResourceStates(cmd_buf, AllStages, AllStages, img_transitions);

ClearColorImage(full_buf_, c.v, cmd_buf);
ClearColorImage(half_buf_, c.v, cmd_buf);
ClearColorImage(final_buf_, c.v, cmd_buf);
ClearColorImage(raw_filtered_buf_, c.v, cmd_buf);

static const float rgba_zero[] = {0.0f, 0.0f, 0.0f, 0.0f};
ClearColorImage(base_color_buf_, rgba_zero, cmd_buf);
ClearColorImage(depth_normals_buf_, rgba_zero, cmd_buf);

{ // Clear integer texture
static const uint32_t rgba[4] = {0xffff, 0xffff, 0xffff, 0xffff};
ClearColorImage(required_samples_buf_, rgba, cmd_buf);
}

EndSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->graphics_queue(), cmd_buf, ctx_->temp_command_pool());
}

void Ray::Dx::Renderer::RenderScene(const SceneBase &scene, RegionContext &region) {
const auto &s = dynamic_cast<const Dx::Scene &>(scene);

Expand Down
35 changes: 6 additions & 29 deletions internal/RendererGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct scene_data_t {
class Renderer : public RendererBase {
protected:
std::unique_ptr<Context> ctx_;
GpuCommandBuffer external_cmd_buf_;

Shader sh_prim_rays_gen_simple_, sh_prim_rays_gen_adaptive_;
Shader sh_intersect_scene_, sh_intersect_scene_indirect_, sh_intersect_area_lights_;
Expand Down Expand Up @@ -340,6 +341,11 @@ class Renderer : public RendererBase {
raw_filtered_buf_.resource_state = eResState(state);
}

void set_command_buffer(const GpuCommandBuffer cmd_buf) {
external_cmd_buf_ = cmd_buf;
ctx_->frame_cpu_synced[cmd_buf.index] = false;
}

void Resize(int w, int h) override;
void Clear(const color_rgba_t &c) override;

Expand Down Expand Up @@ -484,35 +490,6 @@ inline void Ray::NS::Renderer::Resize(const int w, const int h) {
Clear(color_rgba_t{});
}

inline void Ray::NS::Renderer::Clear(const color_rgba_t &c) {
CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool());

const TransitionInfo img_transitions[] = {{&full_buf_, ResStateForClear},
{&half_buf_, ResStateForClear},
{&final_buf_, ResStateForClear},
{&raw_filtered_buf_, ResStateForClear},
{&base_color_buf_, ResStateForClear},
{&depth_normals_buf_, ResStateForClear},
{&required_samples_buf_, ResStateForClear}};
TransitionResourceStates(cmd_buf, AllStages, AllStages, img_transitions);

ClearColorImage(full_buf_, c.v, cmd_buf);
ClearColorImage(half_buf_, c.v, cmd_buf);
ClearColorImage(final_buf_, c.v, cmd_buf);
ClearColorImage(raw_filtered_buf_, c.v, cmd_buf);

static const float rgba_zero[] = {0.0f, 0.0f, 0.0f, 0.0f};
ClearColorImage(base_color_buf_, rgba_zero, cmd_buf);
ClearColorImage(depth_normals_buf_, rgba_zero, cmd_buf);

{ // Clear integer texture
static const uint32_t rgba[4] = {0xffff, 0xffff, 0xffff, 0xffff};
ClearColorImage(required_samples_buf_, rgba, cmd_buf);
}

EndSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->graphics_queue(), cmd_buf, ctx_->temp_command_pool());
}

inline void Ray::NS::Renderer::UpdateFilterTable(CommandBuffer cmd_buf, const ePixelFilter filter, float filter_width) {
float (*filter_func)(float v, float width);
switch (filter) {
Expand Down
Loading

0 comments on commit 0e703aa

Please sign in to comment.