-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTexture3dExampleComponent.cpp
More file actions
290 lines (240 loc) · 12.2 KB
/
Texture3dExampleComponent.cpp
File metadata and controls
290 lines (240 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <RHI/Texture3dExampleComponent.h>
#include <Utils/Utils.h>
#include <SampleComponentManager.h>
#include <Atom/RHI/Factory.h>
#include <Atom/RHI/CommandList.h>
#include <Atom/RHI/FrameScheduler.h>
#include <Atom/RHI/Image.h>
#include <Atom/RHI/ImagePool.h>
#include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
#include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
#include <Atom/RPI.Public/Image/ImageSystemInterface.h>
#include <Atom/RPI.Public/Image/StreamingImagePool.h>
#include <Atom/RPI.Public/Shader/Shader.h>
#include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
#include <AzCore/Serialization/SerializeContext.h>
namespace AtomSampleViewer
{
void Texture3dExampleComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<Texture3dExampleComponent, AZ::Component>()
->Version(0)
;
}
}
Texture3dExampleComponent::Texture3dExampleComponent()
{
m_supportRHISamplePipeline = true;
}
void Texture3dExampleComponent::Activate()
{
using namespace AZ;
const RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
// Get the window size
AzFramework::NativeWindowHandle windowHandle = nullptr;
AzFramework::WindowSystemRequestBus::BroadcastResult(
windowHandle,
&AzFramework::WindowSystemRequestBus::Events::GetDefaultWindowHandle);
AzFramework::WindowRequestBus::EventResult(
m_windowSize,
windowHandle,
&AzFramework::WindowRequestBus::Events::GetClientAreaSize);
// Create image pool
{
AZ::RHI::Factory& factory = RHI::Factory::Get();
m_imagePool = factory.CreateImagePool();
m_imagePool->SetName(Name("Texture3DPool"));
RHI::ImagePoolDescriptor imagePoolDesc = {};
imagePoolDesc.m_bindFlags = RHI::ImageBindFlags::ShaderRead;
const uint64_t imagePoolBudget = 1 << 24; // 16 Megabyte
imagePoolDesc.m_budgetInBytes = imagePoolBudget;
const RHI::ResultCode resultCode = m_imagePool->Init(*device, imagePoolDesc);
if (resultCode != RHI::ResultCode::Success)
{
AZ_Error("Texture3dExampleComponent", false, "Failed to initialize image pool.");
return;
}
}
// Create image
{
// Create the 3d image data
AZStd::vector<uint8_t> imageData;
RHI::Format format = {};
BasicRHIComponent::CreateImage3dData(imageData, m_imageLayout, format, {
"textures/streaming/streaming13.dds.streamingimage",
"textures/streaming/streaming14.dds.streamingimage",
"textures/streaming/streaming15.dds.streamingimage",
"textures/streaming/streaming16.dds.streamingimage",
"textures/streaming/streaming17.dds.streamingimage",
"textures/streaming/streaming19.dds.streamingimage" });
// Create the image resource
m_image = RHI::Factory::Get().CreateImage();
m_image->SetName(Name("Texture3D"));
RHI::ImageInitRequest imageRequest;
imageRequest.m_image = m_image.get();
imageRequest.m_descriptor = RHI::ImageDescriptor::Create3D(RHI::ImageBindFlags::ShaderRead, m_imageLayout.m_size.m_width, m_imageLayout.m_size.m_height, m_imageLayout.m_size.m_depth, format);
RHI::ResultCode resultCode = m_imagePool->InitImage(imageRequest);
if (resultCode != RHI::ResultCode::Success)
{
AZ_Error("Texture3dExampleComponent", false, "Failed to initialize image.");
return;
}
// Create image view resource
RHI::ImageViewDescriptor imageViewDescriptor = {};
imageViewDescriptor = RHI::ImageViewDescriptor::Create(format, 0, 0);
imageViewDescriptor.m_overrideBindFlags = RHI::ImageBindFlags::ShaderRead;
m_imageView = m_image->GetImageView(imageViewDescriptor);
m_imageView->SetName(Name("Texture3DView"));
if(!m_imageView.get())
{
AZ_Error("Texture3dExampleComponent", false, "Failed to initialize image view.");
return;
}
// Update/stage the image with data
RHI::ImageSubresourceLayout imageSubresourceLayout;
RHI::ImageSubresourceRange range(0, 0, 0, 0);
m_image->GetSubresourceLayouts(range, &imageSubresourceLayout, nullptr);
RHI::ImageUpdateRequest updateRequest;
updateRequest.m_image = m_image.get();
updateRequest.m_sourceSubresourceLayout = imageSubresourceLayout;
updateRequest.m_sourceData = imageData.data();
updateRequest.m_imageSubresourcePixelOffset = RHI::Origin(0, 0, 0);
resultCode = m_imagePool->UpdateImageContents(updateRequest);
if (resultCode != RHI::ResultCode::Success)
{
AZ_Error("Texture3dExampleComponent", false, "Failed to update/stage the image.");
return;
}
}
// Create the pipeline object
{
const char* texture3dShaderFilePath = "Shaders/RHI/texture3d.azshader";
const char* sampleName = "Texture3DExample";
auto shader = LoadShader(texture3dShaderFilePath, sampleName);
if (shader == nullptr)
{
return;
}
// Setup the pipeline state descriptor
AZ::RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
const RPI::ShaderVariant& shaderVariant = shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
attachmentsBuilder.AddSubpass()
->RenderTargetAttachment(m_outputFormat);
[[maybe_unused]] AZ::RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
AZ_Assert(result == AZ::RHI::ResultCode::Success, "Failed to create render attachment layout");
pipelineStateDescriptor.m_inputStreamLayout.SetTopology(AZ::RHI::PrimitiveTopology::TriangleStrip);
pipelineStateDescriptor.m_inputStreamLayout.Finalize();
m_pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
if (!m_pipelineState || !m_pipelineState->IsInitialized())
{
AZ_Error("Render", false, "Failed to acquire default pipeline state for shader %s", texture3dShaderFilePath);
}
// Create the SRG
m_shaderResourceGroup = CreateShaderResourceGroup(shader, "ImageSrg", sampleName);
// Set the shader input indices
FindShaderInputIndex(&m_texture3dInputIndex, m_shaderResourceGroup, Name("m_texture3D"), sampleName);
FindShaderInputIndex(&m_sliceIndexInputIndex, m_shaderResourceGroup, Name("m_sliceIndex"), sampleName);
FindShaderInputIndex(&m_positionInputIndex, m_shaderResourceGroup, Name("m_position"), sampleName);
FindShaderInputIndex(&m_sizeInputIndex, m_shaderResourceGroup, Name("m_size"), sampleName);
}
// Create the scope producer
{
struct ScopeData
{
};
const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
{
{
RHI::ImageScopeAttachmentDescriptor desc;
desc.m_attachmentId = m_outputAttachmentId;
frameGraph.UseColorAttachment(desc);
}
frameGraph.SetEstimatedItemCount(1);
};
const auto compileFunction = [this]([[maybe_unused]] const AZ::RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
{
// Compile the srg
AZStd::array<float, 2> position = { {0.00f, 0.1f} };
const float ratioYtoX = m_windowSize.m_height / static_cast<float>(m_windowSize.m_width);
AZStd::array<float, 2> size = { {0.7f * ratioYtoX, 0.7f} };
m_shaderResourceGroup->SetImageView(m_texture3dInputIndex, m_imageView.get());
m_shaderResourceGroup->SetConstant<uint32_t>(m_sliceIndexInputIndex, static_cast<uint32_t>(m_sliceIndex));
m_shaderResourceGroup->SetConstant(m_positionInputIndex, position);
m_shaderResourceGroup->SetConstant(m_sizeInputIndex, size);
m_shaderResourceGroup->Compile();
};
const auto executeFunction = [=]([[maybe_unused]] const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
{
RHI::CommandList* commandList = context.GetCommandList();
commandList->SetViewports(&m_viewport, 1);
commandList->SetScissors(&m_scissor, 1);
RHI::DrawLinear drawIndexed;
drawIndexed.m_vertexCount = 4;
drawIndexed.m_instanceCount = 1;
const RHI::ShaderResourceGroup* shaderResourceGroups[] = { m_shaderResourceGroup->GetRHIShaderResourceGroup() };
// Create the draw item
RHI::DrawItem drawItem;
drawItem.m_arguments = drawIndexed;
drawItem.m_pipelineState = m_pipelineState.get();
drawItem.m_indexBufferView = nullptr;
drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
drawItem.m_shaderResourceGroups = shaderResourceGroups;
drawItem.m_streamBufferViewCount = 0;
drawItem.m_streamBufferViews = nullptr;
// Add the draw item to the commandlist
commandList->Submit(drawItem);
};
// Create and add the scope producer
m_scopeProducers.emplace_back(aznew RHI::ScopeProducerFunction<
ScopeData,
decltype(prepareFunction),
decltype(compileFunction),
decltype(executeFunction)>(
RHI::ScopeId{ "Texture3d" },
ScopeData{},
prepareFunction,
compileFunction,
executeFunction));
}
AZ::TickBus::Handler::BusConnect();
AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
m_imguiSidebar.Activate();
}
void Texture3dExampleComponent::Deactivate()
{
m_imguiSidebar.Deactivate();
AZ::TickBus::Handler::BusDisconnect();
AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
m_scopeProducers.clear();
m_windowContext = nullptr;
m_imageView = nullptr;
m_image = nullptr;
m_imagePool = nullptr;
m_shaderResourceGroup = nullptr;
m_pipelineState = nullptr;
}
void Texture3dExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
{
if (m_imguiSidebar.Begin())
{
ImGui::Text("3D image slice index");
for (int32_t i = 0; i < static_cast<int32_t>(m_imageLayout.m_size.m_depth); i++)
{
const AZStd::string label = AZStd::string::format("Image Slice %d", i);
ScriptableImGui::RadioButton(label.c_str(), &m_sliceIndex, i);
}
m_imguiSidebar.End();
}
}
}