-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTextureArrayExampleComponent.cpp
More file actions
277 lines (232 loc) · 11.7 KB
/
TextureArrayExampleComponent.cpp
File metadata and controls
277 lines (232 loc) · 11.7 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
/*
* 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/TextureArrayExampleComponent.h>
#include <SampleComponentManager.h>
#include <SampleComponentConfig.h>
#include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
#include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
#include <AzCore/Math/Random.h>
#include <Utils/Utils.h>
namespace AtomSampleViewer
{
using namespace AZ;
using namespace RPI;
namespace InternalTA
{
const char* const SampleName = "TextureArray";
const char* const ShaderFilePath = "Shaders/RHI/TextureArray.azshader";
// Texture limit for this sample. This is set in the shader, if more textures need to be presented, this value
// needs to be adjusted on the application side, and in the shader
const uint32_t TextureCount = 8u;
// The current index of the TextureArray.
uint32_t g_textureIndex = 0u;
};
void TextureArrayExampleComponent::Reflect(ReflectContext* context)
{
if (SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context))
{
serializeContext->Class<TextureArrayExampleComponent, AZ::Component>()
->Version(0)
;
}
}
TextureArrayExampleComponent::TextureArrayExampleComponent()
{
m_supportRHISamplePipeline = true;
}
void TextureArrayExampleComponent::Activate()
{
struct RectangleBufferData
{
AZStd::array<VertexPosition, 4> m_positions;
AZStd::array<VertexUV, 4> m_uvs;
AZStd::array<uint16_t, 6> m_indices;
};
RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
m_inputAssemblyBufferPool = RHI::Factory::Get().CreateBufferPool();
// Load the shader
{
m_shader = LoadShader(InternalTA::ShaderFilePath, InternalTA::SampleName);
AZ_Error(InternalTA::SampleName, m_shader, "Failed to load shader.");
}
// Create the shader resource groups
{
m_textureArraySrg = CreateShaderResourceGroup(m_shader, "TextureArraySrg", InternalTA::SampleName);
m_textureIndexSrg = CreateShaderResourceGroup(m_shader, "TextureIndexSrg", InternalTA::SampleName);
}
// Cache the shader input indices
{
FindShaderInputIndex(&m_textureArray, m_textureArraySrg, Name{"m_textureArray"}, InternalTA::SampleName);
FindShaderInputIndex(&m_textureIndex, m_textureIndexSrg, Name{"m_textureArrayIndex"}, InternalTA::SampleName);
// Set the shader constants
{
const char* filePath[InternalTA::TextureCount] = {
"textures/streaming/streaming0.dds.streamingimage",
"textures/streaming/streaming1.dds.streamingimage",
"textures/streaming/streaming2.dds.streamingimage",
"textures/streaming/streaming3.dds.streamingimage",
"textures/streaming/streaming4.dds.streamingimage",
"textures/streaming/streaming5.dds.streamingimage",
"textures/streaming/streaming6.dds.streamingimage",
"textures/streaming/streaming7.dds.streamingimage",
};
for (uint32_t textuerIdx = 0u; textuerIdx < InternalTA::TextureCount; textuerIdx++)
{
AZ::Data::Instance<AZ::RPI::StreamingImage> image = LoadStreamingImage(filePath[textuerIdx], InternalTA::SampleName);
[[maybe_unused]] bool result = m_textureArraySrg->SetImage(m_textureArray, image, textuerIdx);
AZ_Error(InternalTA::SampleName, result, "Failed to set image into shader resource group.");
}
m_textureArraySrg->Compile();
}
}
// Setup pipeline state
{
RHI::InputStreamLayoutBuilder layoutBuilder;
layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
layoutBuilder.AddBuffer()->Channel("UV", RHI::Format::R32G32_FLOAT);
m_rectangleInputStreamLayout = layoutBuilder.End();
AZ::RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
pipelineStateDescriptor.m_inputStreamLayout = m_rectangleInputStreamLayout;
auto shaderVariant = m_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");
m_pipelineState = m_shader->AcquirePipelineState(pipelineStateDescriptor);
AZ_Error(
InternalTA::SampleName, m_pipelineState && m_pipelineState->IsInitialized(),
"Failed for the appropriate acquire default pipeline state for shader '%s'", InternalTA::ShaderFilePath);
}
// Setup input buffer stream
{
RectangleBufferData bufferData;
SetFullScreenRect(bufferData.m_positions.data(), bufferData.m_uvs.data(), bufferData.m_indices.data());
RHI::BufferPoolDescriptor bufferPoolDesc;
bufferPoolDesc.m_bindFlags = RHI::BufferBindFlags::InputAssembly;
bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
m_inputAssemblyBufferPool->Init(*device, bufferPoolDesc);
m_rectangleInputAssemblyBuffer = RHI::Factory::Get().CreateBuffer();
RHI::ResultCode result = RHI::ResultCode::Success;
RHI::BufferInitRequest request;
request.m_buffer = m_rectangleInputAssemblyBuffer.get();
request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
request.m_initialData = &bufferData;
result = m_inputAssemblyBufferPool->InitBuffer(request);
if (result != RHI::ResultCode::Success)
{
AZ_Error(InternalTA::SampleName, false, "Failed to initialize position buffer with error code %d", result);
return;
}
m_rectangleStreamBufferViews[0u] = {
*m_rectangleInputAssemblyBuffer,
offsetof(RectangleBufferData, m_positions),
sizeof(RectangleBufferData::m_positions),
sizeof(VertexPosition)
};
m_rectangleStreamBufferViews[1u] = {
*m_rectangleInputAssemblyBuffer,
offsetof(RectangleBufferData, m_uvs),
sizeof(RectangleBufferData::m_uvs),
sizeof(VertexUV)
};
RHI::ValidateStreamBufferViews(m_rectangleInputStreamLayout, m_rectangleStreamBufferViews);
}
// Creates a scope for rendering the triangle.
{
struct ScopeData
{
};
const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
{
// Binds the swap chain as a color attachment.
{
RHI::ImageScopeAttachmentDescriptor descriptor;
descriptor.m_attachmentId = m_outputAttachmentId;
descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
frameGraph.UseColorAttachment(descriptor);
}
// We will submit a single draw item.
frameGraph.SetEstimatedItemCount(1u);
};
const auto compileFunction = [this]([[maybe_unused]] const AZ::RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
{
[[maybe_unused]] bool success = m_textureIndexSrg->SetConstant(m_textureIndex, InternalTA::g_textureIndex);
AZ_Warning("TriangleExampleComponent", success, "Failed to set SRG Constant m_objectMatrix");
m_textureIndexSrg->Compile();
};
const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
{
RHI::CommandList* commandList = context.GetCommandList();
// Set persistent viewport and scissor state.
// NOTE: set a hard coded offset so that the ImGUI features are still accessible.
RHI::Viewport viewport = m_viewport;
Vector2 viewportOffset = Vector2(m_viewport.m_maxX / 2.0f, 18.0f);
viewport.m_minX = viewportOffset.GetX();
viewport.m_minY = viewportOffset.GetY();
commandList->SetViewports(&viewport, 1u);
commandList->SetScissors(&m_scissor, 1u);
const RHI::IndexBufferView indexBufferView =
{
*m_rectangleInputAssemblyBuffer,
offsetof(RectangleBufferData, m_indices),
sizeof(RectangleBufferData::m_indices),
RHI::IndexFormat::Uint16
};
RHI::DrawIndexed drawIndexed;
drawIndexed.m_indexCount = 6u;
drawIndexed.m_instanceCount = 1u;
const RHI::ShaderResourceGroup* shaderResourceGroups[] = {
m_textureArraySrg->GetRHIShaderResourceGroup(), m_textureIndexSrg->GetRHIShaderResourceGroup() };
RHI::DrawItem drawItem;
drawItem.m_arguments = drawIndexed;
drawItem.m_pipelineState = m_pipelineState.get();
drawItem.m_indexBufferView = &indexBufferView;
drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
drawItem.m_shaderResourceGroups = shaderResourceGroups;
drawItem.m_streamBufferViewCount = static_cast<uint8_t>(m_rectangleStreamBufferViews.size());
drawItem.m_streamBufferViews = m_rectangleStreamBufferViews.data();
// Submit the triangle draw item.
commandList->Submit(drawItem);
};
m_scopeProducers.emplace_back(
aznew RHI::ScopeProducerFunction<
ScopeData,
decltype(prepareFunction),
decltype(compileFunction),
decltype(executeFunction)>(
RHI::ScopeId{"TextureArray"},
ScopeData{ },
prepareFunction,
compileFunction,
executeFunction));
}
AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
TickBus::Handler::BusConnect();
}
void TextureArrayExampleComponent::Deactivate()
{
m_inputAssemblyBufferPool = nullptr;
m_rectangleInputAssemblyBuffer = nullptr;
m_pipelineState = nullptr;
m_shader = nullptr;
m_textureArraySrg = nullptr;
m_textureIndexSrg = nullptr;
}
void TextureArrayExampleComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint scriptTime)
{
static float time = 0.0f;
time += deltaTime;
if (time > 1.0f)
{
time = 0.0f;
InternalTA::g_textureIndex = (InternalTA::g_textureIndex + 1u) % InternalTA::TextureCount;
}
}
};