Skip to content

Commit

Permalink
Use typesafe bitmasks in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Jan 3, 2025
1 parent 22c2125 commit 7c09cb9
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 246 deletions.
19 changes: 14 additions & 5 deletions Bitmask.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ template <class enum_type, typename = typename std::enable_if<std::is_enum<enum_
Bitmask &operator=(const Bitmask &rhs) = default;
Bitmask &operator=(Bitmask &&rhs) = default;

Bitmask operator|(const enum_type rhs) { return Bitmask(mask_ | to_mask(rhs)); }
Bitmask operator|(const Bitmask rhs) { return Bitmask(mask_ | rhs.mask_); }
Bitmask operator|(const Bitmask rhs) const { return Bitmask(mask_ | rhs.mask_); }
Bitmask operator|=(const Bitmask rhs) { return (*this) = Bitmask(mask_ | rhs.mask_); }

Bitmask operator|=(const enum_type rhs) { return (*this) = Bitmask(mask_ | to_mask(rhs)); }
Bitmask operator&(const Bitmask rhs) const { return Bitmask(mask_ & rhs.mask_); }
Bitmask operator&=(const Bitmask rhs) { return (*this) = Bitmask(mask_ & rhs.mask_); }

bool operator&(const enum_type rhs) const { return (mask_ & to_mask(rhs)) != 0; }
bool operator&(const Bitmask rhs) const { return (mask_ & rhs.mask_) != 0; }
Bitmask operator~() const { return Bitmask(~mask_); }

bool operator==(const enum_type rhs) const { return mask_ == to_mask(rhs); }
bool operator==(const Bitmask rhs) const { return mask_ == rhs.mask_; }

bool operator!=(const enum_type rhs) const { return mask_ != to_mask(rhs); }
bool operator!=(const Bitmask rhs) const { return mask_ != rhs.mask_; }

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

private:
underlying_type mask_;
Expand Down
6 changes: 3 additions & 3 deletions internal/Dx/TextureAtlasDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ void Ray::Dx::TextureAtlas::WritePageData(const int page, const int posx, const
}

void Ray::Dx::TextureAtlas::CopyRegionTo(const int page, const int x, const int y, const int w, const int h,
const Buffer &dst_buf, ID3D12GraphicsCommandList *cmd_buf, const int data_off) const {
const Buffer &dst_buf, ID3D12GraphicsCommandList *cmd_buf,
const int data_off) const {
SmallVector<D3D12_RESOURCE_BARRIER, 2> barriers;

if (resource_state != eResState::CopySrc) {
Expand Down Expand Up @@ -543,8 +544,7 @@ void Ray::Dx::TextureAtlas::CopyRegionTo(const int page, const int x, const int
dst_loc.PlacedFootprint.Footprint.Format = g_dx_formats[int(real_format_)];
if (IsCompressedFormat(real_format_)) {
dst_loc.PlacedFootprint.Footprint.RowPitch =
round_up(GetBlockCount(w, 1, eTexBlock::_4x4) * GetBlockLenBytes(real_format_, eTexBlock::_4x4),
TextureDataPitchAlignment);
round_up(GetBlockCount(w, 1, real_format_) * GetBlockLenBytes(real_format_), TextureDataPitchAlignment);
} else {
dst_loc.PlacedFootprint.Footprint.RowPitch =
round_up(w * GetPerPixelDataLen(real_format_), TextureDataPitchAlignment);
Expand Down
49 changes: 25 additions & 24 deletions internal/Dx/TextureDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern const D3D12_COMPARISON_FUNC g_dx_compare_func[];

extern const float AnisotropyLevel;

#define DECORATE(X, Y, Z, W, XX) XX,
#define DECORATE(X, Y, Z, W, XX, YY, ZZ) ZZ,
extern const DXGI_FORMAT g_dx_formats[] = {
#include "../TextureFormat.inl"
};
Expand All @@ -57,13 +57,13 @@ DXGI_FORMAT ToSRGBFormat(const DXGI_FORMAT format) {
return DXGI_FORMAT_UNKNOWN;
}

D3D12_RESOURCE_FLAGS to_dx_image_flags(const eTexUsage usage, const eTexFormat format) {
D3D12_RESOURCE_FLAGS to_dx_image_flags(const Bitmask<eTexUsage> usage, const eTexFormat format) {
D3D12_RESOURCE_FLAGS ret = D3D12_RESOURCE_FLAG_NONE;
if (uint8_t(usage & eTexUsage::Storage)) {
if (usage & eTexUsage::Storage) {
assert(!IsCompressedFormat(format));
ret |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
if (uint8_t(usage & eTexUsage::RenderTarget)) {
if (usage & eTexUsage::RenderTarget) {
assert(!IsCompressedFormat(format));
if (IsDepthFormat(format)) {
ret |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
Expand Down Expand Up @@ -162,7 +162,7 @@ void Ray::Dx::Texture2D::Init(const void *data, const uint32_t size, const Tex2D
_p.w = _p.h = 1;
_p.mip_count = 1;
_p.format = eTexFormat::RGBA8;
_p.usage = eTexUsage::Sampled | eTexUsage::Transfer;
_p.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;

InitFromRAWData(&sbuf, 0, cmd_buf, mem_allocs, _p, log);
// mark it as not ready
Expand All @@ -182,7 +182,7 @@ void Ray::Dx::Texture2D::Init(const void *data, const uint32_t size, const Tex2D
}

void Ray::Dx::Texture2D::Free() {
if (params.format != eTexFormat::Undefined && !bool(params.flags & eTexFlagBits::NoOwnership)) {
if (params.format != eTexFormat::Undefined && !bool(params.flags & eTexFlags::NoOwnership)) {
ctx_->staging_descr_alloc()->Free(eDescrType::CBV_SRV_UAV, handle_.views_ref);
ctx_->resources_to_destroy[ctx_->backend_frame].push_back(handle_.img);
ctx_->staging_descr_alloc()->Free(eDescrType::Sampler, handle_.sampler_ref);
Expand All @@ -194,8 +194,8 @@ void Ray::Dx::Texture2D::Free() {
}

bool Ray::Dx::Texture2D::Realloc(const int w, const int h, int mip_count, const int samples, const eTexFormat format,
const eTexBlock block, const bool is_srgb, ID3D12GraphicsCommandList *cmd_buf,
MemAllocators *mem_allocs, ILog *log) {
const bool is_srgb, ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs,
ILog *log) {
ID3D12Resource *new_image = nullptr;
// VkImageView new_image_view = VK_NULL_HANDLE;
MemAllocation new_alloc = {};
Expand Down Expand Up @@ -454,7 +454,7 @@ void Ray::Dx::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, ID3D12Graph
image_desc.DepthOrArraySize = 1;
image_desc.MipLevels = params.mip_count;
image_desc.Format = g_dx_formats[int(p.format)];
if (bool(p.flags & eTexFlagBits::SRGB)) {
if (p.flags & eTexFlags::SRGB) {
image_desc.Format = ToSRGBFormat(image_desc.Format);
}
image_desc.SampleDesc.Count = p.samples;
Expand Down Expand Up @@ -488,14 +488,14 @@ void Ray::Dx::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, ID3D12Graph
ctx_->device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
const UINT SAMPLER_INCR = ctx_->device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);

const bool requires_uav = int(p.usage & eTexUsageBits::Storage) != 0;
const bool requires_uav = bool(p.usage & eTexUsage::Storage);

handle_.views_ref = ctx_->staging_descr_alloc()->Alloc(eDescrType::CBV_SRV_UAV, requires_uav ? 2 : 1);

{ // create default SRV
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};
srv_desc.Format = g_dx_formats[int(p.format)];
if (bool(p.flags & eTexFlagBits::SRGB)) {
if (p.flags & eTexFlags::SRGB) {
srv_desc.Format = ToSRGBFormat(srv_desc.Format);
}
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
Expand All @@ -505,7 +505,7 @@ void Ray::Dx::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, ID3D12Graph
srv_desc.Texture2D.ResourceMinLODClamp = 0.0f;

srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
if (GetChannelCount(p.format) == 1 && int(p.usage & eTexUsageBits::Storage) == 0) {
if (GetChannelCount(p.format) == 1 && !bool(p.usage & eTexUsage::Storage)) {
srv_desc.Shader4ComponentMapping = D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(0, 0, 0, 0);
}

Expand Down Expand Up @@ -694,13 +694,12 @@ void Ray::Dx::Texture2D::SetSubImage(const int level, const int offsetx, const i
src_loc.PlacedFootprint.Footprint.Height = sizey;
src_loc.PlacedFootprint.Footprint.Depth = 1;
src_loc.PlacedFootprint.Footprint.Format = g_dx_formats[int(params.format)];
if (bool(params.flags & eTexFlagBits::SRGB)) {
if (params.flags & eTexFlags::SRGB) {
src_loc.PlacedFootprint.Footprint.Format = ToSRGBFormat(src_loc.PlacedFootprint.Footprint.Format);
}
if (IsCompressedFormat(params.format)) {
src_loc.PlacedFootprint.Footprint.RowPitch =
round_up(GetBlockCount(sizex, 1, params.block) * GetBlockLenBytes(params.format, params.block),
TextureDataPitchAlignment);
src_loc.PlacedFootprint.Footprint.RowPitch = round_up(
GetBlockCount(sizex, 1, params.format) * GetBlockLenBytes(params.format), TextureDataPitchAlignment);
} else {
src_loc.PlacedFootprint.Footprint.RowPitch =
round_up(sizex * GetPerPixelDataLen(params.format), TextureDataPitchAlignment);
Expand Down Expand Up @@ -822,10 +821,9 @@ void Ray::Dx::CopyImageToBuffer(const Texture2D &src_tex, const int level, const
dst_loc.PlacedFootprint.Footprint.Depth = 1;
dst_loc.PlacedFootprint.Footprint.Format = g_dx_formats[int(src_tex.params.format)];
if (IsCompressedFormat(src_tex.params.format)) {
dst_loc.PlacedFootprint.Footprint.RowPitch =
round_up(GetBlockCount(src_tex.params.w, 1, src_tex.params.block) *
GetBlockLenBytes(src_tex.params.format, src_tex.params.block),
TextureDataPitchAlignment);
dst_loc.PlacedFootprint.Footprint.RowPitch = round_up(
GetBlockCount(src_tex.params.w, 1, src_tex.params.format) * GetBlockLenBytes(src_tex.params.format),
TextureDataPitchAlignment);
} else {
dst_loc.PlacedFootprint.Footprint.RowPitch =
round_up(src_tex.params.w * GetPerPixelDataLen(src_tex.params.format), TextureDataPitchAlignment);
Expand Down Expand Up @@ -945,7 +943,7 @@ void Ray::Dx::Texture3D::Init(const Tex3DParams &p, MemAllocators *mem_allocs, I
image_desc.DepthOrArraySize = p.d;
image_desc.MipLevels = 1;
image_desc.Format = g_dx_formats[int(p.format)];
if (bool(p.flags & eTexFlagBits::SRGB)) {
if (p.flags & eTexFlags::SRGB) {
image_desc.Format = ToSRGBFormat(image_desc.Format);
}
image_desc.SampleDesc.Count = 1;
Expand Down Expand Up @@ -1024,7 +1022,7 @@ void Ray::Dx::Texture3D::Init(const Tex3DParams &p, MemAllocators *mem_allocs, I
}

void Ray::Dx::Texture3D::Free() {
if (params.format != eTexFormat::Undefined && !bool(params.flags & eTexFlagBits::NoOwnership)) {
if (params.format != eTexFormat::Undefined && !bool(params.flags & eTexFlags::NoOwnership)) {
ctx_->staging_descr_alloc()->Free(eDescrType::CBV_SRV_UAV, handle_.views_ref);
ctx_->resources_to_destroy[ctx_->backend_frame].push_back(handle_.img);
ctx_->staging_descr_alloc()->Free(eDescrType::Sampler, handle_.sampler_ref);
Expand Down Expand Up @@ -1101,11 +1099,14 @@ bool Ray::Dx::RequiresManualSRGBConversion(const eTexFormat format) {
return dxgi_format == ToSRGBFormat(dxgi_format);
}

bool Ray::Dx::CanBeBlockCompressed(int w, int h, const int mip_count, const eTexBlock block) {
bool Ray::Dx::CanBeBlockCompressed(int w, int h, const int mip_count) {
// NOTE: Assume only BC-formats for now
static const int block_res[2] = {4, 4};

bool ret = true;
for (int i = 0; i < mip_count && ret; ++i) {
// make sure resolution is multiple of block size
ret &= (w % g_block_res[int(block)][0]) == 0 && (h % g_block_res[int(block)][1]) == 0;
ret &= (w % block_res[0]) == 0 && (h % block_res[1]) == 0;
w /= 2;
h /= 2;
}
Expand Down
4 changes: 2 additions & 2 deletions internal/Dx/TextureDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Texture2D {
void Init(const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf,
ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log);

bool Realloc(int w, int h, int mip_count, int samples, eTexFormat format, eTexBlock block, bool is_srgb,
bool Realloc(int w, int h, int mip_count, int samples, eTexFormat format, bool is_srgb,
ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, ILog *log);

Context *ctx() { return ctx_; }
Expand Down Expand Up @@ -191,7 +191,7 @@ DXGI_FORMAT DXFormatFromTexFormat(eTexFormat format);
DXGI_FORMAT ToSRGBFormat(DXGI_FORMAT format);

bool RequiresManualSRGBConversion(eTexFormat format);
bool CanBeBlockCompressed(int w, int h, int mip_count, eTexBlock block);
bool CanBeBlockCompressed(int w, int h, int mip_count);

} // namespace Dx
} // namespace Ray
Expand Down
2 changes: 0 additions & 2 deletions internal/Fixed.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <limits>
#undef min
#undef max

namespace Ray {
template <typename T, int FpBits> class Fixed {
Expand Down
2 changes: 1 addition & 1 deletion internal/RendererDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Ray::Dx::Renderer::Renderer(const settings_t &s, ILog *log,
{ // create tonemap LUT texture
Tex3DParams params = {};
params.w = params.h = params.d = LUT_DIMS;
params.usage = eTexUsage::Sampled | eTexUsage::Transfer;
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
params.format = eTexFormat::RGB10_A2;
params.sampling.filter = eTexFilter::Bilinear;
params.sampling.wrap = eTexWrap::ClampToEdge;
Expand Down
2 changes: 1 addition & 1 deletion internal/RendererGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ inline void Ray::NS::Renderer::Resize(const int w, const int h) {
params.w = w;
params.h = h;
params.format = eTexFormat::RGBA32F;
params.usage = eTexUsageBits::Sampled | eTexUsageBits::Storage | eTexUsageBits::Transfer;
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Storage | eTexUsage::Transfer;
params.sampling.wrap = eTexWrap::ClampToEdge;

temp_buf0_ = Texture2D{"Temp Image 0", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()};
Expand Down
2 changes: 1 addition & 1 deletion internal/RendererVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Ray::Vk::Renderer::Renderer(const settings_t &s, ILog *log,
{ // create tonemap LUT texture
Tex3DParams params = {};
params.w = params.h = params.d = LUT_DIMS;
params.usage = eTexUsage::Sampled | eTexUsage::Transfer;
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
params.format = eTexFormat::RGB10_A2;
params.sampling.filter = eTexFilter::Bilinear;
params.sampling.wrap = eTexWrap::ClampToEdge;
Expand Down
Loading

0 comments on commit 7c09cb9

Please sign in to comment.