Skip to content

Commit 8bf9b69

Browse files
committed
Unify texture parameters struct
1 parent 8fce174 commit 8bf9b69

File tree

11 files changed

+85
-85
lines changed

11 files changed

+85
-85
lines changed

Bitmask.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#pragma once
22

3+
#include <cassert>
4+
#include <limits>
35
#include <type_traits>
46

57
namespace Ray {
68
template <class enum_type, typename = typename std::enable_if<std::is_enum<enum_type>::value>::type> class Bitmask {
79
using underlying_type = typename std::underlying_type<enum_type>::type;
810

9-
static underlying_type to_mask(const enum_type e) { return 1 << static_cast<underlying_type>(e); }
11+
static underlying_type to_mask(const enum_type e) {
12+
assert(1ull << static_cast<underlying_type>(e) <= std::numeric_limits<underlying_type>::max());
13+
return 1 << static_cast<underlying_type>(e);
14+
}
1015

1116
explicit Bitmask(const underlying_type mask) : mask_(mask) {}
1217

internal/Dx/TextureDX.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ bool EndsWith(const std::string &str1, const char *str2);
109109

110110
Ray::eTexUsage Ray::Dx::TexUsageFromState(const eResState state) { return g_tex_usage_per_state[int(state)]; }
111111

112-
Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams &p, MemAllocators *mem_allocs,
113-
ILog *log)
112+
Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const TexParams &p, MemAllocators *mem_allocs, ILog *log)
114113
: ctx_(ctx), name_(name) {
115114
Init(p, mem_allocs, log);
116115
}
117116

118-
Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const void *data, const uint32_t size,
119-
const Tex2DParams &p, Buffer &stage_buf, ID3D12GraphicsCommandList *cmd_buf,
120-
MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log)
117+
Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const void *data, const uint32_t size, const TexParams &p,
118+
Buffer &stage_buf, ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs,
119+
eTexLoadStatus *load_status, ILog *log)
121120
: ctx_(ctx), name_(name) {
122121
Init(data, size, p, stage_buf, cmd_buf, mem_allocs, load_status, log);
123122
}
@@ -142,11 +141,11 @@ Ray::Dx::Texture2D &Ray::Dx::Texture2D::operator=(Texture2D &&rhs) noexcept {
142141
return (*this);
143142
}
144143

145-
void Ray::Dx::Texture2D::Init(const Tex2DParams &p, MemAllocators *mem_allocs, ILog *log) {
144+
void Ray::Dx::Texture2D::Init(const TexParams &p, MemAllocators *mem_allocs, ILog *log) {
146145
InitFromRAWData(nullptr, 0, nullptr, mem_allocs, p, log);
147146
}
148147

149-
void Ray::Dx::Texture2D::Init(const void *data, const uint32_t size, const Tex2DParams &p, Buffer &sbuf,
148+
void Ray::Dx::Texture2D::Init(const void *data, const uint32_t size, const TexParams &p, Buffer &sbuf,
150149
ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs,
151150
eTexLoadStatus *load_status, ILog *log) {
152151
uint8_t *stage_data = sbuf.Map();
@@ -417,7 +416,7 @@ bool Ray::Dx::Texture2D::Realloc(const int w, const int h, int mip_count, const
417416
}
418417

419418
void Ray::Dx::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, ID3D12GraphicsCommandList *cmd_buf,
420-
MemAllocators *mem_allocs, const Tex2DParams &p, ILog *log) {
419+
MemAllocators *mem_allocs, const TexParams &p, ILog *log) {
421420
Free();
422421

423422
handle_.generation = TextureHandleCounter++;
@@ -874,7 +873,7 @@ void Ray::Dx::_ClearColorImage(Texture2D &tex, const void *rgba, ID3D12GraphicsC
874873

875874
////////////////////////////////////////////////////////////////////////////////////////
876875

877-
Ray::Dx::Texture3D::Texture3D(const char *name, Context *ctx, const Tex3DParams &params, MemAllocators *mem_allocs,
876+
Ray::Dx::Texture3D::Texture3D(const char *name, Context *ctx, const TexParams &params, MemAllocators *mem_allocs,
878877
ILog *log)
879878
: name_(name), ctx_(ctx) {
880879
Init(params, mem_allocs, log);
@@ -900,7 +899,7 @@ Ray::Dx::Texture3D &Ray::Dx::Texture3D::operator=(Texture3D &&rhs) noexcept {
900899
return (*this);
901900
}
902901

903-
void Ray::Dx::Texture3D::Init(const Tex3DParams &p, MemAllocators *mem_allocs, ILog *log) {
902+
void Ray::Dx::Texture3D::Init(const TexParams &p, MemAllocators *mem_allocs, ILog *log) {
904903
Free();
905904

906905
handle_.generation = TextureHandleCounter++;

internal/Dx/TextureDX.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ class Texture2D {
7171
void Free();
7272

7373
void InitFromRAWData(Buffer *sbuf, int data_off, ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs,
74-
const Tex2DParams &p, ILog *log);
74+
const TexParams &p, ILog *log);
7575

7676
public:
77-
Tex2DParams params;
77+
TexParams params;
7878

7979
uint32_t first_user = 0xffffffff;
8080
mutable eResState resource_state = eResState::Undefined;
8181

8282
Texture2D() = default;
83-
Texture2D(const char *name, Context *ctx, const Tex2DParams &params, MemAllocators *mem_allocs, ILog *log);
83+
Texture2D(const char *name, Context *ctx, const TexParams &params, MemAllocators *mem_allocs, ILog *log);
8484
Texture2D(const char *name, Context *ctx,
8585
ID3D12Resource *img, // const VkImageView view, const VkSampler sampler,
86-
const Tex2DParams &_params, ILog *log)
86+
const TexParams &_params, ILog *log)
8787
: handle_{img, /*view, VK_NULL_HANDLE, sampler,*/ 0}, name_(name), params(_params) {}
88-
Texture2D(const char *name, Context *ctx, const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf,
88+
Texture2D(const char *name, Context *ctx, const void *data, uint32_t size, const TexParams &p, Buffer &stage_buf,
8989
ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log);
9090
Texture2D(const Texture2D &rhs) = delete;
9191
Texture2D(Texture2D &&rhs) noexcept { (*this) = std::move(rhs); }
@@ -96,13 +96,13 @@ class Texture2D {
9696

9797
operator bool() const { return (handle_.img != nullptr); }
9898

99-
void Init(const Tex2DParams &params, MemAllocators *mem_allocs, ILog *log);
100-
void Init(ID3D12Resource *img, /*const VkImageView view, const VkSampler sampler,*/ const Tex2DParams &_params,
99+
void Init(const TexParams &params, MemAllocators *mem_allocs, ILog *log);
100+
void Init(ID3D12Resource *img, /*const VkImageView view, const VkSampler sampler,*/ const TexParams &_params,
101101
ILog *log) {
102102
handle_ = {img, /*view, VK_NULL_HANDLE, sampler,*/ 0};
103103
params = _params;
104104
}
105-
void Init(const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf,
105+
void Init(const void *data, uint32_t size, const TexParams &p, Buffer &stage_buf,
106106
ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log);
107107

108108
bool Realloc(int w, int h, int mip_count, int samples, eTexFormat format, bool is_srgb,
@@ -158,11 +158,11 @@ class Texture3D {
158158
void Free();
159159

160160
public:
161-
Tex3DParams params;
161+
TexParams params;
162162
mutable eResState resource_state = eResState::Undefined;
163163

164164
Texture3D() = default;
165-
Texture3D(const char *name, Context *ctx, const Tex3DParams &params, MemAllocators *mem_allocs, ILog *log);
165+
Texture3D(const char *name, Context *ctx, const TexParams &params, MemAllocators *mem_allocs, ILog *log);
166166
Texture3D(const Texture3D &rhs) = delete;
167167
Texture3D(Texture3D &&rhs) noexcept { (*this) = std::move(rhs); }
168168
~Texture3D();
@@ -176,7 +176,7 @@ class Texture3D {
176176
ID3D12Resource *dx_resource() const { return handle_.img; }
177177
PoolRef sampler_ref() const { return handle_.sampler_ref; }
178178

179-
void Init(const Tex3DParams &params, MemAllocators *mem_allocs, ILog *log);
179+
void Init(const TexParams &params, MemAllocators *mem_allocs, ILog *log);
180180

181181
void SetSubImage(int offsetx, int offsety, int offsetz, int sizex, int sizey, int sizez, eTexFormat format,
182182
const Buffer &sbuf, ID3D12GraphicsCommandList *cmd_buf, int data_off, int data_len);

internal/RendererDX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ Ray::Dx::Renderer::Renderer(const settings_t &s, ILog *log,
237237
}
238238

239239
{ // create tonemap LUT texture
240-
Tex3DParams params = {};
240+
TexParams params = {};
241241
params.w = params.h = params.d = LUT_DIMS;
242242
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
243243
params.format = eTexFormat::RGB10_A2;

internal/RendererGPU.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ inline void Ray::NS::Renderer::Resize(const int w, const int h) {
389389

390390
const int num_pixels = w * h;
391391

392-
Tex2DParams params;
392+
TexParams params;
393393
params.w = w;
394394
params.h = h;
395395
params.format = eTexFormat::RGBA32F;
@@ -408,7 +408,7 @@ inline void Ray::NS::Renderer::Resize(const int w, const int h) {
408408
raw_filtered_buf_ =
409409
Texture2D{"Raw Filtered Final Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()};
410410
{ // Texture that holds required sample count per pixel
411-
Tex2DParams uparams = params;
411+
TexParams uparams = params;
412412
uparams.format = eTexFormat::R16UI;
413413
required_samples_buf_ =
414414
Texture2D{"Required samples Image", ctx_.get(), uparams, ctx_->default_mem_allocs(), ctx_->log()};

internal/RendererVK.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Ray::Vk::Renderer::Renderer(const settings_t &s, ILog *log,
283283
}
284284

285285
{ // create tonemap LUT texture
286-
Tex3DParams params = {};
286+
TexParams params = {};
287287
params.w = params.h = params.d = LUT_DIMS;
288288
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
289289
params.format = eTexFormat::RGB10_A2;

internal/SceneGPU.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ inline Ray::TextureHandle Ray::NS::Scene::AddBindlessTexture_nolock(const tex_de
698698

699699
temp_stage_buf.Unmap();
700700

701-
Tex2DParams p = {};
701+
TexParams p = {};
702702
p.w = _t.w;
703703
p.h = _t.h;
704704
if (_t.is_srgb && !is_YCoCg && !RequiresManualSRGBConversion(fmt)) {
@@ -1514,7 +1514,7 @@ inline void Ray::NS::Scene::Finalize(const std::function<void(int, int, Parallel
15141514
PrepareEnvMapQTree_nolock();
15151515
} else {
15161516
// Dummy
1517-
Tex2DParams p;
1517+
TexParams p;
15181518
p.w = p.h = 1;
15191519
p.format = eTexFormat::RGBA32F;
15201520
p.mip_count = 1;
@@ -1539,7 +1539,7 @@ inline void Ray::NS::Scene::Finalize(const std::function<void(int, int, Parallel
15391539
}
15401540
} else {
15411541
// Dummy
1542-
Tex2DParams p;
1542+
TexParams p;
15431543
p.w = p.h = 1;
15441544
p.format = eTexFormat::RGBA32F;
15451545
p.mip_count = 1;
@@ -1640,7 +1640,7 @@ int SaveEXR(const float *data, int width, int height, int components, const int
16401640
inline std::vector<Ray::color_rgba8_t> Ray::NS::Scene::CalcSkyEnvTexture(const atmosphere_params_t &params,
16411641
const int res[2], const light_t lights[],
16421642
Span<const uint32_t> dir_lights) {
1643-
Tex2DParams p;
1643+
TexParams p;
16441644
p.w = res[0];
16451645
p.h = res[1];
16461646
p.format = eTexFormat::RGBA32F;
@@ -1782,7 +1782,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::function<void(int, int, Paral
17821782
// }
17831783

17841784
if (!sky_moon_tex_) {
1785-
Tex2DParams params;
1785+
TexParams params;
17861786
params.w = MOON_TEX_W;
17871787
params.h = MOON_TEX_H;
17881788
params.format = eTexFormat::RGBA8;
@@ -1812,7 +1812,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::function<void(int, int, Paral
18121812
}
18131813

18141814
if (!sky_weather_tex_) {
1815-
Tex2DParams params;
1815+
TexParams params;
18161816
params.w = params.h = WEATHER_TEX_RES;
18171817
params.format = eTexFormat::RGBA8;
18181818
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
@@ -1840,7 +1840,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::function<void(int, int, Paral
18401840
}
18411841

18421842
if (!sky_cirrus_tex_) {
1843-
Tex2DParams params;
1843+
TexParams params;
18441844
params.w = params.h = CIRRUS_TEX_RES;
18451845
params.format = eTexFormat::RG8;
18461846
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
@@ -1863,7 +1863,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::function<void(int, int, Paral
18631863
}
18641864

18651865
if (!sky_curl_tex_) {
1866-
Tex2DParams params;
1866+
TexParams params;
18671867
params.w = params.h = CURL_TEX_RES;
18681868
params.format = eTexFormat::RGBA8;
18691869
params.flags = eTexFlags::SRGB;
@@ -1892,7 +1892,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::function<void(int, int, Paral
18921892
}
18931893

18941894
if (!sky_noise3d_tex_.handle()) {
1895-
Tex3DParams params;
1895+
TexParams params;
18961896
params.w = params.h = params.d = NOISE_3D_RES;
18971897
params.format = eTexFormat::R8;
18981898
params.usage = Bitmask<eTexUsage>(eTexUsage::Sampled) | eTexUsage::Transfer;
@@ -2177,7 +2177,7 @@ inline void Ray::NS::Scene::PrepareEnvMapQTree_nolock() {
21772177
}
21782178
temp_stage_buf.Unmap();
21792179

2180-
Tex2DParams p;
2180+
TexParams p;
21812181
p.w = p.h = (env_map_qtree_.res / 2);
21822182
p.format = eTexFormat::RGBA32F;
21832183
p.mip_count = env_.qtree_levels;
@@ -2515,7 +2515,7 @@ inline void Ray::NS::Scene::SetEnvironment(const environment_desc_t &env) {
25152515
SceneCommon::SetEnvironment(env);
25162516

25172517
if (!sky_transmittance_lut_tex_) {
2518-
Tex2DParams params;
2518+
TexParams params;
25192519
params.w = SKY_TRANSMITTANCE_LUT_W;
25202520
params.h = SKY_TRANSMITTANCE_LUT_H;
25212521
params.format = eTexFormat::RGBA32F;
@@ -2526,7 +2526,7 @@ inline void Ray::NS::Scene::SetEnvironment(const environment_desc_t &env) {
25262526
sky_transmittance_lut_tex_ = Texture2D{"Sky Transmittance LUT", ctx_, params, ctx_->default_mem_allocs(), log_};
25272527
}
25282528
if (!sky_multiscatter_lut_tex_) {
2529-
Tex2DParams params;
2529+
TexParams params;
25302530
params.w = params.h = SKY_MULTISCATTER_LUT_RES;
25312531
params.format = eTexFormat::RGBA32F;
25322532
params.sampling.wrap = eTexWrap::ClampToEdge;

internal/TextureParams.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ int Ray::GetBlockCount(const int w, const int h, const eTexFormat format) {
4444
((h + g_tex_format_info[i].block_y - 1) / g_tex_format_info[i].block_y);
4545
}
4646

47-
uint32_t Ray::EstimateMemory(const Tex2DParams &params) {
47+
uint32_t Ray::EstimateMemory(const TexParams &params) {
4848
uint32_t total_len = 0;
4949
for (int i = 0; i < params.mip_count; i++) {
5050
const int w = std::max(params.w >> i, 1);
5151
const int h = std::max(params.h >> i, 1);
52+
const int d = std::max(params.d >> i, 1);
5253

5354
if (IsCompressedFormat(params.format)) {
5455
const int block_len = GetBlockLenBytes(params.format);
5556
const int block_cnt = GetBlockCount(w, h, params.format);
5657

58+
assert(params.d == 0);
5759
total_len += uint32_t(block_len) * block_cnt;
5860
} else {
5961
assert(g_tex_format_info[int(params.format)].pp_data_len != 0);
60-
total_len += w * h * g_tex_format_info[int(params.format)].pp_data_len;
62+
total_len += w * h * d * g_tex_format_info[int(params.format)].pp_data_len;
6163
}
6264
}
6365
return total_len;

internal/TextureParams.h

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,29 @@ struct Texture1DParams {
5858
};
5959
static_assert(sizeof(Texture1DParams) == 6, "!");
6060

61-
struct Tex2DParams {
61+
struct TexParams {
6262
uint16_t w = 0, h = 0;
63+
uint8_t d = 0;
64+
uint8_t mip_count : 5;
65+
uint8_t samples : 3;
6366
Bitmask<eTexFlags> flags;
64-
uint8_t mip_count = 1;
6567
Bitmask<eTexUsage> usage;
66-
uint8_t samples = 1;
6768
eTexFormat format = eTexFormat::Undefined;
6869
SamplingParams sampling;
70+
71+
TexParams() {
72+
mip_count = 1;
73+
samples = 1;
74+
}
6975
};
70-
static_assert(sizeof(Tex2DParams) == 16, "!");
76+
static_assert(sizeof(TexParams) == 16, "!");
7177

72-
inline bool operator==(const Tex2DParams &lhs, const Tex2DParams &rhs) {
73-
return lhs.w == rhs.w && lhs.h == rhs.h && lhs.flags == rhs.flags && lhs.mip_count == rhs.mip_count &&
74-
lhs.usage == rhs.usage && lhs.samples == rhs.samples && lhs.format == rhs.format &&
78+
inline bool operator==(const TexParams &lhs, const TexParams &rhs) {
79+
return lhs.w == rhs.w && lhs.h == rhs.h && lhs.d == rhs.d && lhs.mip_count == rhs.mip_count &&
80+
lhs.samples == rhs.samples && lhs.flags == rhs.flags && lhs.usage == rhs.usage && lhs.format == rhs.format &&
7581
lhs.sampling == rhs.sampling;
7682
}
77-
inline bool operator!=(const Tex2DParams &lhs, const Tex2DParams &rhs) { return !operator==(lhs, rhs); }
78-
79-
struct Tex3DParams {
80-
uint16_t w = 0, h = 0;
81-
uint8_t d = 0;
82-
Bitmask<eTexFlags> flags;
83-
Bitmask<eTexUsage> usage;
84-
eTexFormat format = eTexFormat::Undefined;
85-
SamplingParams sampling;
86-
};
87-
static_assert(sizeof(Tex3DParams) == 14, "!");
83+
inline bool operator!=(const TexParams &lhs, const TexParams &rhs) { return !operator==(lhs, rhs); }
8884

8985
enum class eTexLoadStatus { Found, Reinitialized, CreatedDefault, CreatedFromData };
9086

@@ -97,7 +93,7 @@ int GetBlockCount(int w, int h, eTexFormat format);
9793
inline int GetMipDataLenBytes(const int w, const int h, const eTexFormat format) {
9894
return GetBlockCount(w, h, format) * GetBlockLenBytes(format);
9995
}
100-
uint32_t EstimateMemory(const Tex2DParams &params);
96+
uint32_t EstimateMemory(const TexParams &params);
10197

10298
eTexFormat FormatFromGLInternalFormat(uint32_t gl_internal_format, bool *is_srgb);
10399
int BlockLenFromGLInternalFormat(uint32_t gl_internal_format);

0 commit comments

Comments
 (0)