Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Bitmask.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Ray {
template <class enum_type, typename = typename std::enable_if<std::is_enum<enum_type>::value>::type> class Bitmask {
using underlying_type = typename std::underlying_type<enum_type>::type;

static underlying_type to_mask(const enum_type e) {
static constexpr underlying_type to_mask(const enum_type e) {
assert(1ull << static_cast<underlying_type>(e) <= std::numeric_limits<underlying_type>::max());
return 1 << static_cast<underlying_type>(e);
}

public:
Bitmask() : mask_(0) {}
Bitmask(const enum_type e) : mask_(to_mask(e)) {}
explicit Bitmask(const underlying_type mask) : mask_(mask) {}
constexpr Bitmask() : mask_(0) {}
constexpr Bitmask(const enum_type e) : mask_(to_mask(e)) {}
explicit constexpr Bitmask(const underlying_type mask) : mask_(mask) {}

Bitmask(const Bitmask &rhs) = default;
Bitmask(Bitmask &&rhs) = default;
Expand All @@ -39,7 +39,7 @@ template <class enum_type, typename = typename std::enable_if<std::is_enum<enum_
bool operator!=(const Bitmask rhs) const { return mask_ != rhs.mask_; }

operator bool() const { return mask_ != 0; }
explicit operator underlying_type() const { return mask_; }
explicit constexpr operator underlying_type() const { return mask_; }

private:
underlying_type mask_;
Expand Down
18 changes: 8 additions & 10 deletions internal/Dx/DrawCallDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ void Ray::Dx::PrepareDescriptors(Context *ctx, ID3D12GraphicsCommandList *cmd_bu

DescrSizes descr_sizes;
for (const auto &b : bindings) {
if (b.trg == eBindTarget::Tex2DSampled || b.trg == eBindTarget::Tex3DSampled ||
b.trg == eBindTarget::Tex2DArraySampled) {
if (b.trg == eBindTarget::TexSampled || b.trg == eBindTarget::TexArraySampled) {
descr_sizes.cbv_srv_uav_count += b.handle.count;
descr_sizes.sampler_count += b.handle.count;
} else if (b.trg == eBindTarget::Tex2D || b.trg == eBindTarget::Tex2DArray || b.trg == eBindTarget::Tex3D ||
b.trg == eBindTarget::UBuf || b.trg == eBindTarget::TBuf || b.trg == eBindTarget::SBufRO ||
b.trg == eBindTarget::SBufRW || b.trg == eBindTarget::Image || b.trg == eBindTarget::AccStruct) {
} else if (b.trg == eBindTarget::Tex || b.trg == eBindTarget::TexArray || b.trg == eBindTarget::UBuf ||
b.trg == eBindTarget::TBuf || b.trg == eBindTarget::SBufRO || b.trg == eBindTarget::SBufRW ||
b.trg == eBindTarget::Image || b.trg == eBindTarget::AccStruct) {
descr_sizes.cbv_srv_uav_count += b.handle.count;
} else if (b.trg == eBindTarget::Sampler) {
descr_sizes.sampler_count += b.handle.count;
Expand Down Expand Up @@ -94,8 +93,7 @@ void Ray::Dx::PrepareDescriptors(Context *ctx, ID3D12GraphicsCommandList *cmd_bu
continue;
}

if (b.trg == eBindTarget::Tex2D || b.trg == eBindTarget::Tex2DSampled || b.trg == eBindTarget::Tex3D ||
b.trg == eBindTarget::Tex3DSampled) {
if (b.trg == eBindTarget::Tex || b.trg == eBindTarget::TexSampled) {
D3D12_CPU_DESCRIPTOR_HANDLE src_handle =
b.handle.tex->handle().views_ref.heap->GetCPUDescriptorHandleForHeapStart();
src_handle.ptr += CBV_SRV_UAV_INCR * b.handle.tex->handle().views_ref.offset;
Expand All @@ -105,7 +103,7 @@ void Ray::Dx::PrepareDescriptors(Context *ctx, ID3D12GraphicsCommandList *cmd_bu

device->CopyDescriptorsSimple(1, dest_handle, src_handle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

if (b.trg == eBindTarget::Tex2DSampled || b.trg == eBindTarget::Tex3DSampled) {
if (b.trg == eBindTarget::TexSampled) {
const short descr_index = prog->descr_index(1, b.loc);

D3D12_CPU_DESCRIPTOR_HANDLE src_handle =
Expand All @@ -117,7 +115,7 @@ void Ray::Dx::PrepareDescriptors(Context *ctx, ID3D12GraphicsCommandList *cmd_bu

device->CopyDescriptorsSimple(1, dest_handle, src_handle, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
}
} else if (b.trg == eBindTarget::Tex2DArray || b.trg == eBindTarget::Tex2DArraySampled) {
} else if (b.trg == eBindTarget::TexArray || b.trg == eBindTarget::TexArraySampled) {
for (int i = 0; i < b.handle.count; ++i) {
D3D12_CPU_DESCRIPTOR_HANDLE src_handle =
b.handle.tex_arr[i].srv_ref().heap->GetCPUDescriptorHandleForHeapStart();
Expand All @@ -128,7 +126,7 @@ void Ray::Dx::PrepareDescriptors(Context *ctx, ID3D12GraphicsCommandList *cmd_bu

device->CopyDescriptorsSimple(1, dest_handle, src_handle, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

if (b.trg == eBindTarget::Tex2DArraySampled) {
if (b.trg == eBindTarget::TexArraySampled) {
const short descr_index = prog->descr_index(1, b.loc);

D3D12_CPU_DESCRIPTOR_HANDLE src_handle =
Expand Down
13 changes: 5 additions & 8 deletions internal/Dx/DrawCallDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ struct DescrTable;
class Sampler;

enum class eBindTarget : uint16_t {
Tex2D,
Tex2DSampled,
Tex2DMs,
Tex2DArray,
Tex2DArraySampled,
TexCubeArray,
Tex3D,
Tex3DSampled,
Tex,
TexSampled,
// TODO: remove these two!
TexArray,
TexArraySampled,
TBuf,
UBuf,
SBufRO,
Expand Down
99 changes: 49 additions & 50 deletions internal/Dx/ResourceDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ D3D12_RESOURCE_STATES Ray::Dx::DXResourceState(const eResState state) { return g
// uint32_t Ray::Vk::VKPipelineStagesForState(const eResState state) { return
// g_pipeline_stages_per_state_vk[int(state)]; }

void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const eStageBits src_stages_mask,
const eStageBits dst_stages_mask, Span<const TransitionInfo> transitions) {
void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const Bitmask<eStage> src_stages_mask,
const Bitmask<eStage> dst_stages_mask, Span<const TransitionInfo> transitions) {
SmallVector<D3D12_RESOURCE_BARRIER, 64> barriers;

for (const TransitionInfo &transition : transitions) {
Expand Down Expand Up @@ -81,61 +81,60 @@ void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const
if (transition.update_internal_state) {
transition.p_tex->resource_state = transition.new_state;
}
} else
if (transition.p_buf && *transition.p_buf) {
eResState old_state = transition.old_state;
if (old_state == eResState::Undefined) {
// take state from resource itself
old_state = transition.p_buf->resource_state;
if (old_state == transition.new_state && old_state != eResState::UnorderedAccess) {
// transition is not needed
continue;
}
} else if (transition.p_buf && *transition.p_buf) {
eResState old_state = transition.old_state;
if (old_state == eResState::Undefined) {
// take state from resource itself
old_state = transition.p_buf->resource_state;
if (old_state == transition.new_state && old_state != eResState::UnorderedAccess) {
// transition is not needed
continue;
}
}

auto &new_barrier = barriers.emplace_back();
if (old_state != transition.new_state) {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
new_barrier.Transition.pResource = transition.p_buf->dx_resource();
new_barrier.Transition.StateBefore = DXResourceState(old_state);
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
} else {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
new_barrier.UAV.pResource = transition.p_buf->dx_resource();
}
auto &new_barrier = barriers.emplace_back();
if (old_state != transition.new_state) {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
new_barrier.Transition.pResource = transition.p_buf->dx_resource();
new_barrier.Transition.StateBefore = DXResourceState(old_state);
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
} else {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
new_barrier.UAV.pResource = transition.p_buf->dx_resource();
}

if (transition.update_internal_state) {
transition.p_buf->resource_state = transition.new_state;
}
} else if (transition.p_tex_arr && transition.p_tex_arr->page_count()) {
eResState old_state = transition.old_state;
if (old_state == eResState::Undefined) {
// take state from resource itself
old_state = transition.p_tex_arr->resource_state;
if (old_state != eResState::Undefined && old_state == transition.new_state &&
old_state != eResState::UnorderedAccess) {
// transition is not needed
continue;
}
if (transition.update_internal_state) {
transition.p_buf->resource_state = transition.new_state;
}
} else if (transition.p_tex_arr && transition.p_tex_arr->page_count()) {
eResState old_state = transition.old_state;
if (old_state == eResState::Undefined) {
// take state from resource itself
old_state = transition.p_tex_arr->resource_state;
if (old_state != eResState::Undefined && old_state == transition.new_state &&
old_state != eResState::UnorderedAccess) {
// transition is not needed
continue;
}
}

auto &new_barrier = barriers.emplace_back();
if (old_state != transition.new_state) {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
new_barrier.Transition.pResource = transition.p_tex_arr->dx_resource();
new_barrier.Transition.StateBefore = DXResourceState(old_state);
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
} else {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
new_barrier.UAV.pResource = transition.p_tex_arr->dx_resource();
}
auto &new_barrier = barriers.emplace_back();
if (old_state != transition.new_state) {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
new_barrier.Transition.pResource = transition.p_tex_arr->dx_resource();
new_barrier.Transition.StateBefore = DXResourceState(old_state);
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
} else {
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
new_barrier.UAV.pResource = transition.p_tex_arr->dx_resource();
}

if (transition.update_internal_state) {
transition.p_tex_arr->resource_state = transition.new_state;
}
if (transition.update_internal_state) {
transition.p_tex_arr->resource_state = transition.new_state;
}
}
}

if (!barriers.empty()) {
Expand Down
70 changes: 28 additions & 42 deletions internal/Dx/ResourceDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,35 @@

#define COUNT_OF(x) ((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))

#include "../../Bitmask.h"
#include "../../Span.h"

enum D3D12_RESOURCE_STATES;
struct ID3D12GraphicsCommandList;

namespace Ray {
namespace Dx {
enum class eStageBits : uint16_t {
None = 0,
VertexInput = (1u << 0u),
VertexShader = (1u << 1u),
TessCtrlShader = (1u << 2u),
TessEvalShader = (1u << 3u),
GeometryShader = (1u << 4u),
FragmentShader = (1u << 5u),
ComputeShader = (1u << 6u),
RayTracingShader = (1u << 7u),
ColorAttachment = (1u << 8u),
DepthAttachment = (1u << 9u),
DrawIndirect = (1u << 10u),
Transfer = (1u << 11u),
AccStructureBuild = (1u << 12u)
enum class eStage : uint16_t {
VertexInput,
VertexShader,
TessCtrlShader,
TessEvalShader,
GeometryShader,
FragmentShader,
ComputeShader,
RayTracingShader,
ColorAttachment,
DepthAttachment,
DrawIndirect,
Transfer,
AccStructureBuild
};
inline eStageBits operator|(const eStageBits lhs, const eStageBits rhs) {
return eStageBits(uint16_t(lhs) | uint16_t(rhs));
}
inline eStageBits operator&(const eStageBits lhs, const eStageBits rhs) {
return eStageBits(uint16_t(lhs) & uint16_t(rhs));
}
inline eStageBits operator|=(eStageBits &lhs, const eStageBits rhs) {
lhs = eStageBits(uint16_t(lhs) | uint16_t(rhs));
return lhs;
}
inline eStageBits operator&=(eStageBits &lhs, const eStageBits rhs) {
lhs = eStageBits(uint16_t(lhs) & uint16_t(rhs));
return lhs;
}

const eStageBits AllStages = eStageBits::VertexInput | eStageBits::VertexShader | eStageBits::TessCtrlShader |
eStageBits::TessEvalShader | eStageBits::GeometryShader | eStageBits::FragmentShader |
eStageBits::ComputeShader | eStageBits::RayTracingShader | eStageBits::ColorAttachment |
eStageBits::DepthAttachment | eStageBits::DrawIndirect | eStageBits::Transfer |
eStageBits::AccStructureBuild;

const Bitmask<eStage> AllStages = Bitmask<eStage>{eStage::VertexInput} | eStage::VertexShader | eStage::TessCtrlShader |
eStage::TessEvalShader | eStage::GeometryShader | eStage::FragmentShader |
eStage::ComputeShader | eStage::RayTracingShader | eStage::ColorAttachment |
eStage::DepthAttachment | eStage::DrawIndirect | eStage::Transfer |
eStage::AccStructureBuild;

enum class eResState : uint8_t {
Undefined,
Expand All @@ -70,13 +56,13 @@ enum class eResState : uint8_t {

const eResState ResStateForClear = eResState::UnorderedAccess;

//VkImageLayout VKImageLayoutForState(eResState state);
//uint32_t VKAccessFlagsForState(eResState state);
//uint32_t VKPipelineStagesForState(eResState state);
// VkImageLayout VKImageLayoutForState(eResState state);
// uint32_t VKAccessFlagsForState(eResState state);
// uint32_t VKPipelineStagesForState(eResState state);

D3D12_RESOURCE_STATES DXResourceState(eResState state);

eStageBits StageBitsForState(eResState state);
Bitmask<eStage> StagesForState(eResState state);

class Buffer;
class Texture;
Expand All @@ -101,7 +87,7 @@ struct TransitionInfo {
: p_buf(_p_buf), new_state(_new_state), update_internal_state(true) {}
};

void TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, eStageBits src_stages_mask, eStageBits dst_stages_mask,
Span<const TransitionInfo> transitions);
} // namespace Vk
void TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, Bitmask<eStage> src_stages_mask,
Bitmask<eStage> dst_stages_mask, Span<const TransitionInfo> transitions);
} // namespace Dx
} // namespace Ray
Loading