Skip to content

Commit db8ff3a

Browse files
committed
Use variant in resource transition
1 parent d85b9cf commit db8ff3a

4 files changed

Lines changed: 46 additions & 44 deletions

File tree

internal/Dx/ResourceDX.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const
5252
SmallVector<D3D12_RESOURCE_BARRIER, 64> barriers;
5353

5454
for (const TransitionInfo &transition : transitions) {
55-
if (transition.p_tex && *transition.p_tex) {
55+
if (std::holds_alternative<const Texture *>(transition.p_res) && *std::get<const Texture *>(transition.p_res)) {
5656
eResState old_state = transition.old_state;
5757
if (old_state == eResState::Undefined) {
5858
// take state from resource itself
59-
old_state = transition.p_tex->resource_state;
59+
old_state = std::get<const Texture *>(transition.p_res)->resource_state;
6060
if (old_state == transition.new_state && old_state != eResState::UnorderedAccess) {
6161
// transition is not needed
6262
continue;
@@ -66,23 +66,24 @@ void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const
6666
auto &new_barrier = barriers.emplace_back();
6767
if (old_state != transition.new_state) {
6868
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
69-
new_barrier.Transition.pResource = transition.p_tex->handle().img;
69+
new_barrier.Transition.pResource = std::get<const Texture *>(transition.p_res)->handle().img;
7070
new_barrier.Transition.StateBefore = DXResourceState(old_state);
7171
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
7272
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
7373
} else {
7474
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
75-
new_barrier.UAV.pResource = transition.p_tex->handle().img;
75+
new_barrier.UAV.pResource = std::get<const Texture *>(transition.p_res)->handle().img;
7676
}
7777

7878
if (transition.update_internal_state) {
79-
transition.p_tex->resource_state = transition.new_state;
79+
std::get<const Texture *>(transition.p_res)->resource_state = transition.new_state;
8080
}
81-
} else if (transition.p_buf && *transition.p_buf) {
81+
} else if (std::holds_alternative<const Buffer *>(transition.p_res) &&
82+
*std::get<const Buffer *>(transition.p_res)) {
8283
eResState old_state = transition.old_state;
8384
if (old_state == eResState::Undefined) {
8485
// take state from resource itself
85-
old_state = transition.p_buf->resource_state;
86+
old_state = std::get<const Buffer *>(transition.p_res)->resource_state;
8687
if (old_state == transition.new_state && old_state != eResState::UnorderedAccess) {
8788
// transition is not needed
8889
continue;
@@ -92,23 +93,24 @@ void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const
9293
auto &new_barrier = barriers.emplace_back();
9394
if (old_state != transition.new_state) {
9495
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
95-
new_barrier.Transition.pResource = transition.p_buf->dx_resource();
96+
new_barrier.Transition.pResource = std::get<const Buffer *>(transition.p_res)->dx_resource();
9697
new_barrier.Transition.StateBefore = DXResourceState(old_state);
9798
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
9899
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
99100
} else {
100101
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
101-
new_barrier.UAV.pResource = transition.p_buf->dx_resource();
102+
new_barrier.UAV.pResource = std::get<const Buffer *>(transition.p_res)->dx_resource();
102103
}
103104

104105
if (transition.update_internal_state) {
105-
transition.p_buf->resource_state = transition.new_state;
106+
std::get<const Buffer *>(transition.p_res)->resource_state = transition.new_state;
106107
}
107-
} else if (transition.p_tex_arr && transition.p_tex_arr->page_count()) {
108+
} else if (std::holds_alternative<const TextureAtlas *>(transition.p_res) &&
109+
std::get<const TextureAtlas *>(transition.p_res)->page_count()) {
108110
eResState old_state = transition.old_state;
109111
if (old_state == eResState::Undefined) {
110112
// take state from resource itself
111-
old_state = transition.p_tex_arr->resource_state;
113+
old_state = std::get<const TextureAtlas *>(transition.p_res)->resource_state;
112114
if (old_state != eResState::Undefined && old_state == transition.new_state &&
113115
old_state != eResState::UnorderedAccess) {
114116
// transition is not needed
@@ -119,17 +121,17 @@ void Ray::Dx::TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, const
119121
auto &new_barrier = barriers.emplace_back();
120122
if (old_state != transition.new_state) {
121123
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
122-
new_barrier.Transition.pResource = transition.p_tex_arr->dx_resource();
124+
new_barrier.Transition.pResource = std::get<const TextureAtlas *>(transition.p_res)->dx_resource();
123125
new_barrier.Transition.StateBefore = DXResourceState(old_state);
124126
new_barrier.Transition.StateAfter = DXResourceState(transition.new_state);
125127
new_barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
126128
} else {
127129
new_barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
128-
new_barrier.UAV.pResource = transition.p_tex_arr->dx_resource();
130+
new_barrier.UAV.pResource = std::get<const TextureAtlas *>(transition.p_res)->dx_resource();
129131
}
130132

131133
if (transition.update_internal_state) {
132-
transition.p_tex_arr->resource_state = transition.new_state;
134+
std::get<const TextureAtlas *>(transition.p_res)->resource_state = transition.new_state;
133135
}
134136
}
135137
}

internal/Dx/ResourceDX.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <variant>
45

56
#include "../../Bitmask.h"
67
#include "../../Span.h"
@@ -66,9 +67,7 @@ class Texture;
6667
class TextureAtlas;
6768

6869
struct TransitionInfo {
69-
const Texture *p_tex = nullptr;
70-
const TextureAtlas *p_tex_arr = nullptr;
71-
const Buffer *p_buf = nullptr;
70+
std::variant<const Texture *, const TextureAtlas *, const Buffer *> p_res;
7271

7372
eResState old_state = eResState::Undefined;
7473
eResState new_state = eResState::Undefined;
@@ -77,11 +76,11 @@ struct TransitionInfo {
7776

7877
TransitionInfo() = default;
7978
TransitionInfo(const Texture *_p_tex, eResState _new_state)
80-
: p_tex(_p_tex), new_state(_new_state), update_internal_state(true) {}
79+
: p_res(_p_tex), new_state(_new_state), update_internal_state(true) {}
8180
TransitionInfo(const TextureAtlas *_p_tex_arr, eResState _new_state)
82-
: p_tex_arr(_p_tex_arr), new_state(_new_state), update_internal_state(true) {}
81+
: p_res(_p_tex_arr), new_state(_new_state), update_internal_state(true) {}
8382
TransitionInfo(const Buffer *_p_buf, eResState _new_state)
84-
: p_buf(_p_buf), new_state(_new_state), update_internal_state(true) {}
83+
: p_res(_p_buf), new_state(_new_state), update_internal_state(true) {}
8584
};
8685

8786
void TransitionResourceStates(ID3D12GraphicsCommandList *cmd_buf, Bitmask<eStage> src_stages_mask,

internal/Vk/ResourceVK.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
155155
const Context *ctx = nullptr;
156156

157157
for (const TransitionInfo &transition : transitions) {
158-
if (transition.p_tex && *transition.p_tex) {
159-
ctx = transition.p_tex->ctx();
158+
if (std::holds_alternative<const Texture *>(transition.p_res) && *std::get<const Texture *>(transition.p_res)) {
159+
ctx = std::get<const Texture *>(transition.p_res)->ctx();
160160

161161
eResState old_state = transition.old_state;
162162
if (old_state == eResState::Undefined) {
163163
// take state from resource itself
164-
old_state = transition.p_tex->resource_state;
164+
old_state = std::get<const Texture *>(transition.p_res)->resource_state;
165165
if (old_state != eResState::Undefined && old_state == transition.new_state &&
166166
old_state != eResState::UnorderedAccess) {
167167
// transition is not needed
@@ -177,10 +177,10 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
177177
new_barrier.newLayout = VKImageLayoutForState(transition.new_state);
178178
new_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
179179
new_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
180-
new_barrier.image = transition.p_tex->handle().img;
181-
if (IsDepthStencilFormat(transition.p_tex->params.format)) {
180+
new_barrier.image = std::get<const Texture *>(transition.p_res)->handle().img;
181+
if (IsDepthStencilFormat(std::get<const Texture *>(transition.p_res)->params.format)) {
182182
new_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
183-
} else if (IsDepthFormat(transition.p_tex->params.format)) {
183+
} else if (IsDepthFormat(std::get<const Texture *>(transition.p_res)->params.format)) {
184184
new_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
185185
} else {
186186
new_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
@@ -195,15 +195,16 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
195195
dst_stages |= VKPipelineStagesForState(transition.new_state);
196196

197197
if (transition.update_internal_state) {
198-
transition.p_tex->resource_state = transition.new_state;
198+
std::get<const Texture *>(transition.p_res)->resource_state = transition.new_state;
199199
}
200-
} else if (transition.p_buf && *transition.p_buf) {
201-
ctx = transition.p_buf->ctx();
200+
} else if (std::holds_alternative<const Buffer *>(transition.p_res) &&
201+
*std::get<const Buffer *>(transition.p_res)) {
202+
ctx = std::get<const Buffer *>(transition.p_res)->ctx();
202203

203204
eResState old_state = transition.old_state;
204205
if (old_state == eResState::Undefined) {
205206
// take state from resource itself
206-
old_state = transition.p_buf->resource_state;
207+
old_state = std::get<const Buffer *>(transition.p_res)->resource_state;
207208
if (old_state == transition.new_state && old_state != eResState::UnorderedAccess) {
208209
// transition is not needed
209210
continue;
@@ -216,7 +217,7 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
216217
new_barrier.dstAccessMask = VKAccessFlagsForState(transition.new_state);
217218
new_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
218219
new_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
219-
new_barrier.buffer = transition.p_buf->vk_handle();
220+
new_barrier.buffer = std::get<const Buffer *>(transition.p_res)->vk_handle();
220221
// transition whole buffer for now
221222
new_barrier.offset = 0;
222223
new_barrier.size = VK_WHOLE_SIZE;
@@ -225,15 +226,16 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
225226
dst_stages |= VKPipelineStagesForState(transition.new_state);
226227

227228
if (transition.update_internal_state) {
228-
transition.p_buf->resource_state = transition.new_state;
229+
std::get<const Buffer *>(transition.p_res)->resource_state = transition.new_state;
229230
}
230-
} else if (transition.p_tex_arr && transition.p_tex_arr->page_count()) {
231-
ctx = transition.p_tex_arr->ctx();
231+
} else if (std::holds_alternative<const TextureAtlas *>(transition.p_res) &&
232+
std::get<const TextureAtlas *>(transition.p_res)->page_count()) {
233+
ctx = std::get<const TextureAtlas *>(transition.p_res)->ctx();
232234

233235
eResState old_state = transition.old_state;
234236
if (old_state == eResState::Undefined) {
235237
// take state from resource itself
236-
old_state = transition.p_tex_arr->resource_state;
238+
old_state = std::get<const TextureAtlas *>(transition.p_res)->resource_state;
237239
if (old_state != eResState::Undefined && old_state == transition.new_state &&
238240
old_state != eResState::UnorderedAccess) {
239241
// transition is not needed
@@ -249,7 +251,7 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
249251
new_barrier.newLayout = VKImageLayoutForState(transition.new_state);
250252
new_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
251253
new_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
252-
new_barrier.image = transition.p_tex_arr->vk_image();
254+
new_barrier.image = std::get<const TextureAtlas *>(transition.p_res)->vk_image();
253255
new_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
254256

255257
// transition whole image for now
@@ -262,7 +264,7 @@ void Ray::Vk::TransitionResourceStates(VkCommandBuffer cmd_buf, const Bitmask<eS
262264
dst_stages |= VKPipelineStagesForState(transition.new_state);
263265

264266
if (transition.update_internal_state) {
265-
transition.p_tex_arr->resource_state = transition.new_state;
267+
std::get<const TextureAtlas *>(transition.p_res)->resource_state = transition.new_state;
266268
}
267269
}
268270
}

internal/Vk/ResourceVK.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <variant>
45

56
#include "../../Bitmask.h"
67
#include "../../Span.h"
@@ -62,9 +63,7 @@ class Texture;
6263
class TextureAtlas;
6364

6465
struct TransitionInfo {
65-
const Texture *p_tex = nullptr;
66-
const TextureAtlas *p_tex_arr = nullptr;
67-
const Buffer *p_buf = nullptr;
66+
std::variant<const Texture *, const TextureAtlas *, const Buffer *> p_res;
6867

6968
eResState old_state = eResState::Undefined;
7069
eResState new_state = eResState::Undefined;
@@ -73,11 +72,11 @@ struct TransitionInfo {
7372

7473
TransitionInfo() = default;
7574
TransitionInfo(const Texture *_p_tex, eResState _new_state)
76-
: p_tex(_p_tex), new_state(_new_state), update_internal_state(true) {}
75+
: p_res(_p_tex), new_state(_new_state), update_internal_state(true) {}
7776
TransitionInfo(const TextureAtlas *_p_tex_arr, eResState _new_state)
78-
: p_tex_arr(_p_tex_arr), new_state(_new_state), update_internal_state(true) {}
77+
: p_res(_p_tex_arr), new_state(_new_state), update_internal_state(true) {}
7978
TransitionInfo(const Buffer *_p_buf, eResState _new_state)
80-
: p_buf(_p_buf), new_state(_new_state), update_internal_state(true) {}
79+
: p_res(_p_buf), new_state(_new_state), update_internal_state(true) {}
8180
};
8281

8382
void TransitionResourceStates(VkCommandBuffer cmd_buf, Bitmask<eStage> src_stages_mask, Bitmask<eStage> dst_stages_mask,

0 commit comments

Comments
 (0)