Skip to content

Commit

Permalink
Allow access to native result image without readback
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Nov 12, 2024
1 parent a6c89ad commit 0c60e6a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 43 deletions.
42 changes: 42 additions & 0 deletions RendererBase.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#pragma once

#include <cstring>
#include <memory>

#include "Config.h"
#include "SceneBase.h"
#include "Types.h"
#include "VulkanFunctions.h"

struct ID3D12Resource;
struct ID3D12DescriptorHeap;

/**
@file RendererBase.h
*/
Expand Down Expand Up @@ -57,6 +61,16 @@ struct settings_t {
VulkanFunctions vk_functions = {};
};

enum class eGPUResState {
RenderTarget = 4,
UnorderedAccess = 5,
DepthRead = 6,
DepthWrite = 7,
ShaderResource = 9,
CopyDst = 11,
CopySrc = 12
};

/** Render region context,
holds information for specific rectangle on image
*/
Expand All @@ -78,6 +92,28 @@ class RegionContext {

class ILog;

struct GpuImage {
union {
VkImage vk_image;
ID3D12Resource *dx_image;
};
union {
VkImageView vk_image_view;
struct {
ID3D12DescriptorHeap *heap;
uint32_t offset;
} dx_image_view;
};
eGPUResState state;

GpuImage() { memset(this, 0, sizeof(GpuImage)); }
GpuImage(VkImage _vk_image, VkImageView _vk_image_view, 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)
: dx_image(_dx_image), dx_image_view{dx_view_heap, dx_view_offset}, state(_state) {}
};

/** Base class for all renderer backends
*/
class RendererBase {
Expand Down Expand Up @@ -114,6 +150,12 @@ class RendererBase {
/// Returns pointer to SH data
virtual const shl1_data_t *get_sh_data_ref() const = 0;

/// Returns native GPU image that holds rendered pixels
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) {}

/** @brief Resize framebuffer
@param w new image width
@param h new image height
Expand Down
13 changes: 13 additions & 0 deletions internal/RendererDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ static_assert(Types::FILTER_GAUSSIAN == int(Ray::ePixelFilter::Gaussian), "!");
static_assert(Types::FILTER_BLACKMAN_HARRIS == int(Ray::ePixelFilter::BlackmanHarris), "!");
static_assert(Types::FILTER_TABLE_SIZE == Ray::FILTER_TABLE_SIZE, "!");

static_assert(int(Ray::eGPUResState::RenderTarget) == int(Ray::Dx::eResState::RenderTarget), "!");
static_assert(int(Ray::eGPUResState::UnorderedAccess) == int(Ray::Dx::eResState::UnorderedAccess), "!");
static_assert(int(Ray::eGPUResState::DepthRead) == int(Ray::Dx::eResState::DepthRead), "!");
static_assert(int(Ray::eGPUResState::DepthWrite) == int(Ray::Dx::eResState::DepthWrite), "!");
static_assert(int(Ray::eGPUResState::ShaderResource) == int(Ray::Dx::eResState::ShaderResource), "!");
static_assert(int(Ray::eGPUResState::CopyDst) == int(Ray::Dx::eResState::CopyDst), "!");
static_assert(int(Ray::eGPUResState::CopySrc) == int(Ray::Dx::eResState::CopySrc), "!");

namespace Ray {
extern const int LUT_DIMS;
extern const uint32_t *transform_luts[];
Expand Down Expand Up @@ -1574,6 +1582,11 @@ Ray::color_data_rgba_t Ray::Dx::Renderer::get_aux_pixels_ref(const eAUXBuffer bu
round_up(w_, TextureDataPitchAlignment / sizeof(color_rgba_t))};
}

Ray::GpuImage Ray::Dx::Renderer::get_native_raw_pixels() const {
return GpuImage{raw_filtered_buf_.handle().img, raw_filtered_buf_.handle().views_ref.heap,
raw_filtered_buf_.handle().views_ref.offset, eGPUResState(raw_filtered_buf_.resource_state)};
}

bool Ray::Dx::Renderer::InitUNetFilterPipelines() {
ILog *log = ctx_->log();

Expand Down
8 changes: 7 additions & 1 deletion internal/RendererGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ class Renderer : public RendererBase {

const shl1_data_t *get_sh_data_ref() const override { return &sh_data_host_[0]; }

GpuImage get_native_raw_pixels() const override;

void set_native_raw_pixels_state(const eGPUResState state) override {
raw_filtered_buf_.resource_state = eResState(state);
}

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

Expand Down Expand Up @@ -509,7 +515,7 @@ inline void Ray::NS::Renderer::Clear(const color_rgba_t &c) {

inline void Ray::NS::Renderer::UpdateFilterTable(CommandBuffer cmd_buf, const ePixelFilter filter, float filter_width) {
float (*filter_func)(float v, float width);
switch (filter) {
switch (filter) {
case ePixelFilter::Box:
filter_func = filter_box;
filter_width = 1.0f;
Expand Down
13 changes: 13 additions & 0 deletions internal/RendererVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ static_assert(Types::FILTER_GAUSSIAN == int(Ray::ePixelFilter::Gaussian), "!");
static_assert(Types::FILTER_BLACKMAN_HARRIS == int(Ray::ePixelFilter::BlackmanHarris), "!");
static_assert(Types::FILTER_TABLE_SIZE == Ray::FILTER_TABLE_SIZE, "!");

static_assert(int(Ray::eGPUResState::RenderTarget) == int(Ray::Vk::eResState::RenderTarget), "!");
static_assert(int(Ray::eGPUResState::UnorderedAccess) == int(Ray::Vk::eResState::UnorderedAccess), "!");
static_assert(int(Ray::eGPUResState::DepthRead) == int(Ray::Vk::eResState::DepthRead), "!");
static_assert(int(Ray::eGPUResState::DepthWrite) == int(Ray::Vk::eResState::DepthWrite), "!");
static_assert(int(Ray::eGPUResState::ShaderResource) == int(Ray::Vk::eResState::ShaderResource), "!");
static_assert(int(Ray::eGPUResState::CopyDst) == int(Ray::Vk::eResState::CopyDst), "!");
static_assert(int(Ray::eGPUResState::CopySrc) == int(Ray::Vk::eResState::CopySrc), "!");

namespace Ray {
extern const int LUT_DIMS;
extern const uint32_t *transform_luts[];
Expand Down Expand Up @@ -1700,6 +1708,11 @@ Ray::color_data_rgba_t Ray::Vk::Renderer::get_aux_pixels_ref(const eAUXBuffer bu
return {((buf == eAUXBuffer::BaseColor) ? base_color_pixels_ : depth_normals_pixels_), w_};
}

Ray::GpuImage Ray::Vk::Renderer::get_native_raw_pixels() const {
return GpuImage{raw_filtered_buf_.handle().img, raw_filtered_buf_.handle().views[0],
eGPUResState(raw_filtered_buf_.resource_state)};
}

bool Ray::Vk::Renderer::InitUNetFilterPipelines() {
ILog *log = ctx_->log();

Expand Down
84 changes: 42 additions & 42 deletions internal/Vk/ContextVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,48 +217,6 @@ bool Ray::Vk::Context::Init(ILog *log, const VulkanDevice &vk_device, const Vulk
supported_stages_mask_ &= ~VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR;
}

if (fp16_supported_) {
VkPhysicalDeviceShaderFloat16Int8Features fp16_features = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES};

VkPhysicalDeviceFeatures2 prop2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
prop2.pNext = &fp16_features;

api_.vkGetPhysicalDeviceFeatures2KHR(physical_device_, &prop2);

fp16_supported_ &= (fp16_features.shaderFloat16 != 0);
}

if (coop_matrix_supported_) {
VkPhysicalDeviceCooperativeMatrixFeaturesKHR coop_matrix_features = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR};

VkPhysicalDeviceFeatures2 prop2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
prop2.pNext = &coop_matrix_features;

api_.vkGetPhysicalDeviceFeatures2KHR(physical_device_, &prop2);

uint32_t props_count = 0;
api_.vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(physical_device_, &props_count, nullptr);

SmallVector<VkCooperativeMatrixPropertiesKHR, 16> coop_matrix_props(
props_count, {VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR});

api_.vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(physical_device_, &props_count,
coop_matrix_props.data());

bool found = false;
for (const VkCooperativeMatrixPropertiesKHR &p : coop_matrix_props) {
if (p.AType == VK_COMPONENT_TYPE_FLOAT16_KHR && p.BType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
p.CType == VK_COMPONENT_TYPE_FLOAT16_KHR && p.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
p.MSize == 16 && p.NSize == 8 && p.KSize == 8 && p.scope == VK_SCOPE_SUBGROUP_KHR) {
found = true;
break;
}
}
coop_matrix_supported_ &= found;
}

if (!external_ && !InitVkDevice(api_, device_, physical_device_, graphics_family_index_, raytracing_supported_,
ray_query_supported_, fp16_supported_, int64_supported_, int64_atomics_supported_,
coop_matrix_supported_, g_enabled_layers, g_enabled_layers_count, log)) {
Expand Down Expand Up @@ -646,6 +604,48 @@ void Ray::Vk::Context::CheckVkPhysicalDeviceFeatures(

shader_int64_supported = (device_features2.features.shaderInt64 == VK_TRUE);
shader_buf_int64_atomics_supported &= (atomic_int64_features.shaderBufferInt64Atomics == VK_TRUE);

if (shader_fp16_supported) {
VkPhysicalDeviceShaderFloat16Int8Features fp16_features = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES};

VkPhysicalDeviceFeatures2 prop2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
prop2.pNext = &fp16_features;

api.vkGetPhysicalDeviceFeatures2KHR(physical_device, &prop2);

shader_fp16_supported &= (fp16_features.shaderFloat16 != 0);
}

if (coop_matrix_supported) {
VkPhysicalDeviceCooperativeMatrixFeaturesKHR coop_matrix_features = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COOPERATIVE_MATRIX_FEATURES_KHR};

VkPhysicalDeviceFeatures2 prop2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
prop2.pNext = &coop_matrix_features;

api.vkGetPhysicalDeviceFeatures2KHR(physical_device, &prop2);

uint32_t props_count = 0;
api.vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(physical_device, &props_count, nullptr);

SmallVector<VkCooperativeMatrixPropertiesKHR, 16> coop_matrix_props(
props_count, {VK_STRUCTURE_TYPE_COOPERATIVE_MATRIX_PROPERTIES_KHR});

api.vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR(physical_device, &props_count,
coop_matrix_props.data());

bool found = false;
for (const VkCooperativeMatrixPropertiesKHR &p : coop_matrix_props) {
if (p.AType == VK_COMPONENT_TYPE_FLOAT16_KHR && p.BType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
p.CType == VK_COMPONENT_TYPE_FLOAT16_KHR && p.ResultType == VK_COMPONENT_TYPE_FLOAT16_KHR &&
p.MSize == 16 && p.NSize == 8 && p.KSize == 8 && p.scope == VK_SCOPE_SUBGROUP_KHR) {
found = true;
break;
}
}
coop_matrix_supported &= found;
}
}

out_raytracing_supported = (acc_struct_supported && raytracing_supported);
Expand Down

0 comments on commit 0c60e6a

Please sign in to comment.