-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathBloomExampleComponent.cpp
More file actions
459 lines (374 loc) · 16.6 KB
/
BloomExampleComponent.cpp
File metadata and controls
459 lines (374 loc) · 16.6 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* 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 <BloomExampleComponent.h>
#include <AzCore/Component/Entity.h>
#include <AzCore/IO/Path/Path.h>
#include <AzFramework/Components/TransformComponent.h>
#include <AzFramework/Entity/EntityContextBus.h>
#include <SampleComponentManager.h>
#include <SampleComponentConfig.h>
#include <EntityUtilityFunctions.h>
#include <Atom/Feature/Utils/FrameCaptureBus.h>
#include <Atom/RHI/DrawPacketBuilder.h>
#include <Atom/RPI.Public/RPISystemInterface.h>
#include <Atom/RPI.Public/Shader/Shader.h>
#include <Atom/RPI.Reflect/Asset/AssetUtils.h>
#include <RHI/BasicRHIComponent.h>
namespace AtomSampleViewer
{
using namespace AZ;
void BloomExampleComponent::Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
{
serializeContext->Class<BloomExampleComponent, AZ::Component>()
->Version(0)
;
}
}
BloomExampleComponent::BloomExampleComponent()
: m_imageBrowser("@user@/BloomExampleComponent/image_browser.xml")
{
}
void BloomExampleComponent::Activate()
{
m_dynamicDraw = RPI::GetDynamicDraw();
m_postProcessFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::PostProcessFeatureProcessorInterface>();
CreateBloomEntity();
m_imguiSidebar.Activate();
AZ::TickBus::Handler::BusConnect();
PrepareRenderData();
m_imageBrowser.SetFilter([this](const AZ::Data::AssetInfo& assetInfo)
{
if (!AzFramework::StringFunc::Path::IsExtension(assetInfo.m_relativePath.c_str(), "streamingimage"))
{
return false;
}
AZStd::string assetPath(assetInfo.m_relativePath);
if (!AzFramework::StringFunc::Path::Normalize(assetPath))
{
return false;
}
if (!Utils::IsFileUnderFolder(assetPath, InputImageFolder))
{
return false;
}
return true;
});
m_imageBrowser.Activate();
// Load a default image
QueueAssetPathForLoad("textures/tonemapping/hdr_test_pattern.exr.streamingimage");
Data::Asset<RPI::AnyAsset> displayMapperAsset = RPI::AssetUtils::LoadAssetByProductPath<RPI::AnyAsset>("passes/DisplayMapperConfiguration.azasset", RPI::AssetUtils::TraceLevel::Error);
const Render::DisplayMapperConfigurationDescriptor* displayMapperConfigurationDescriptor = RPI::GetDataFromAnyAsset<Render::DisplayMapperConfigurationDescriptor>(displayMapperAsset);
if (displayMapperConfigurationDescriptor == nullptr)
{
AZ_Error("DisplayMapperPass", false, "Failed to load display mapper configuration file.");
return;
}
m_displayMapperConfiguration = *displayMapperConfigurationDescriptor;
}
void BloomExampleComponent::Deactivate()
{
AZ::TickBus::Handler::BusDisconnect();
AZ::EntityBus::MultiHandler::BusDisconnect();
if (m_bloomEntity)
{
DestroyEntity(m_bloomEntity, GetEntityContextId());
m_postProcessFeatureProcessor = nullptr;
}
m_imguiSidebar.Deactivate();
m_imageBrowser.Deactivate();
}
void BloomExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
{
AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
if (m_bloomEntity && m_bloomEntity->GetId() == entityId)
{
m_postProcessFeatureProcessor->RemoveSettingsInterface(m_bloomEntity->GetId());
m_bloomEntity = nullptr;
}
else
{
AZ_Assert(false, "unexpected entity destruction is signaled.");
}
}
void BloomExampleComponent::OnTick([[maybe_unused]] float deltaTime, AZ::ScriptTimePoint)
{
if (m_drawImage.m_image && !m_drawImage.m_wasStreamed)
{
m_drawImage.m_wasStreamed = m_drawImage.m_image->GetResidentMipLevel() == 0;
m_drawImage.m_srg->Compile();
}
DrawSidebar();
DrawImage(&m_drawImage);
}
void BloomExampleComponent::DrawSidebar()
{
using namespace AZ::Render;
const char* items[] = { "White", "Red", "Green", "Blue" };
static int item_current0 = 0;
static int item_current1 = 0;
static int item_current2 = 0;
static int item_current3 = 0;
static int item_current4 = 0;
auto ColorPicker = [](int index)->Vector3 {
switch (index)
{
case 0: return Vector3(1.0, 1.0, 1.0); break;
case 1: return Vector3(1.0, 0.5, 0.5); break;
case 2: return Vector3(0.5, 1.0, 0.5); break;
case 3: return Vector3(0.5, 0.5, 1.0); break;
default: return Vector3(1.0, 1.0, 1.0);
}
};
if (!m_imguiSidebar.Begin())
{
return;
}
ImGui::Spacing();
bool enabled = m_bloomSettings->GetEnabled();
if (ImGui::Checkbox("Enable", &enabled) || m_isInitParameters)
{
m_bloomSettings->SetEnabled(enabled);
m_bloomSettings->OnConfigChanged();
}
ImGui::Spacing();
float threshold = m_bloomSettings->GetThreshold();
if (ImGui::SliderFloat("Threshold", &threshold, 0.0f, 20.0f, "%0.1f") || !m_isInitParameters)
{
m_bloomSettings->SetThreshold(threshold);
m_bloomSettings->OnConfigChanged();
}
float knee = m_bloomSettings->GetKnee();
if (ImGui::SliderFloat("Knee", &knee, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetKnee(knee);
m_bloomSettings->OnConfigChanged();
}
ImGui::Spacing();
float sizeScale = m_bloomSettings->GetKernelSizeScale();
if (ImGui::SliderFloat("KernelSizeScale", &sizeScale, 0.0f, 2.0f) || m_isInitParameters)
{
m_bloomSettings->SetKernelSizeScale(sizeScale);
m_bloomSettings->OnConfigChanged();
}
if (ImGui::CollapsingHeader("KernelSize", ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::Indent();
float kernelSize0 = m_bloomSettings->GetKernelSizeStage0();
if (ImGui::SliderFloat("Size0", &kernelSize0, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetKernelSizeStage0(kernelSize0);
m_bloomSettings->OnConfigChanged();
}
float kernelSize1 = m_bloomSettings->GetKernelSizeStage1();
if (ImGui::SliderFloat("Size1", &kernelSize1, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetKernelSizeStage1(kernelSize1);
m_bloomSettings->OnConfigChanged();
}
float kernelSize2 = m_bloomSettings->GetKernelSizeStage2();
if (ImGui::SliderFloat("Size2", &kernelSize2, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetKernelSizeStage2(kernelSize2);
m_bloomSettings->OnConfigChanged();
}
float kernelSize3 = m_bloomSettings->GetKernelSizeStage3();
if (ImGui::SliderFloat("Size3", &kernelSize3, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetKernelSizeStage3(kernelSize3);
m_bloomSettings->OnConfigChanged();
}
float kernelSize4 = m_bloomSettings->GetKernelSizeStage4();
if (ImGui::SliderFloat("Size4", &kernelSize4, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetKernelSizeStage4(kernelSize4);
m_bloomSettings->OnConfigChanged();
}
ImGui::Unindent();
}
ImGui::Spacing();
float intensity = m_bloomSettings->GetIntensity();
if (ImGui::SliderFloat("Intensity", &intensity, 0.0f, 1.0f) || m_isInitParameters)
{
m_bloomSettings->SetIntensity(intensity);
m_bloomSettings->OnConfigChanged();
}
if (ImGui::CollapsingHeader("Tint", ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::Indent();
if (ImGui::ListBox("Tint0", &item_current0, items, IM_ARRAYSIZE(items), 4) || m_isInitParameters)
{
m_bloomSettings->SetTintStage0(ColorPicker(item_current0));
m_bloomSettings->OnConfigChanged();
}
if (ImGui::ListBox("Tint1", &item_current1, items, IM_ARRAYSIZE(items), 4) || m_isInitParameters)
{
m_bloomSettings->SetTintStage1(ColorPicker(item_current1));
m_bloomSettings->OnConfigChanged();
}
if (ImGui::ListBox("Tint2", &item_current2, items, IM_ARRAYSIZE(items), 4) || m_isInitParameters)
{
m_bloomSettings->SetTintStage2(ColorPicker(item_current2));
m_bloomSettings->OnConfigChanged();
}
if (ImGui::ListBox("Tint3", &item_current3, items, IM_ARRAYSIZE(items), 4) || m_isInitParameters)
{
m_bloomSettings->SetTintStage3(ColorPicker(item_current3));
m_bloomSettings->OnConfigChanged();
}
if (ImGui::ListBox("Tint4", &item_current4, items, IM_ARRAYSIZE(items), 4) || m_isInitParameters)
{
m_bloomSettings->SetTintStage4(ColorPicker(item_current4));
m_bloomSettings->OnConfigChanged();
}
ImGui::Unindent();
}
ImGui::Spacing();
bool bicubicEnabled = m_bloomSettings->GetBicubicEnabled();
if (ImGui::Checkbox("Bicubic upsampling", &bicubicEnabled) || m_isInitParameters)
{
m_bloomSettings->SetBicubicEnabled(bicubicEnabled);
m_bloomSettings->OnConfigChanged();
}
m_imguiSidebar.End();
}
void BloomExampleComponent::PrepareRenderData()
{
const auto CreatePipeline = [](const char* shaderFilepath,
const char* srgName,
Data::Asset<AZ::RPI::ShaderAsset>& shaderAsset,
RHI::Ptr<AZ::RHI::ShaderResourceGroupLayout>& srgLayout,
RHI::ConstPtr<RHI::PipelineState>& pipelineState,
RHI::DrawListTag& drawListTag,
RPI::Scene* scene)
{
// Since the shader is using SV_VertexID and SV_InstanceID as VS input, we won't need to have vertex buffer.
// Also, the index buffer is not needed with DrawLinear.
RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
shaderAsset = RPI::AssetUtils::LoadAssetByProductPath<RPI::ShaderAsset>(shaderFilepath, RPI::AssetUtils::TraceLevel::Error);
Data::Instance<RPI::Shader> shader = RPI::Shader::FindOrCreate(shaderAsset);
if (!shader)
{
AZ_Error("Render", false, "Failed to find or create shader instance from shader asset with path %s", shaderFilepath);
return;
}
const RPI::ShaderVariant& shaderVariant = shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
drawListTag = shader->GetDrawListTag();
scene->ConfigurePipelineState(shader->GetDrawListTag(), pipelineStateDescriptor);
pipelineStateDescriptor.m_inputStreamLayout.SetTopology(AZ::RHI::PrimitiveTopology::TriangleStrip);
pipelineStateDescriptor.m_inputStreamLayout.Finalize();
pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
if (!pipelineState || !pipelineState->IsInitialized())
{
AZ_Error("Render", false, "Failed to acquire default pipeline state for shader %s", shaderFilepath);
}
// Load shader resource group layout
srgLayout = shaderAsset->FindShaderResourceGroupLayout(AZ::Name(srgName));
};
// Create the example's main pipeline object
{
CreatePipeline("Shaders/tonemappingexample/renderimage.azshader", "RenderImageSrg", m_shaderAsset, m_srgLayout, m_pipelineState, m_drawListTag, m_scene);
// Set the input indices
m_imageInputIndex.Reset();
m_positionInputIndex.Reset();
m_sizeInputIndex.Reset();
m_colorSpaceIndex.Reset();
}
m_drawImage.m_srg = RPI::ShaderResourceGroup::Create(m_shaderAsset, m_srgLayout->GetName());
m_drawImage.m_wasStreamed = false;
// Set the image to occupy the full screen.
// The window's left bottom is (-1, -1). The window size is (2, 2)
AZStd::array<float, 2> position, size;
position[0] = -1.f;
position[1] = -1.f;
size[0] = 2.0f;
size[1] = 2.0f;
m_drawImage.m_srg->SetConstant(m_positionInputIndex, position);
m_drawImage.m_srg->SetConstant(m_sizeInputIndex, size);
m_drawImage.m_srg->SetConstant<int>(m_colorSpaceIndex, static_cast<int>(m_inputColorSpace));
}
void BloomExampleComponent::DrawImage(const ImageToDraw* imageInfo)
{
// Build draw packet
RHI::DrawPacketBuilder drawPacketBuilder;
drawPacketBuilder.Begin(nullptr);
RHI::DrawLinear drawLinear;
drawLinear.m_vertexCount = 4;
drawPacketBuilder.SetDrawArguments(drawLinear);
RHI::DrawPacketBuilder::DrawRequest drawRequest;
drawRequest.m_listTag = m_drawListTag;
drawRequest.m_pipelineState = m_pipelineState.get();
drawRequest.m_sortKey = 0;
drawRequest.m_uniqueShaderResourceGroup = imageInfo->m_srg->GetRHIShaderResourceGroup();
drawPacketBuilder.AddDrawItem(drawRequest);
// Submit draw packet
AZStd::unique_ptr<const RHI::DrawPacket> drawPacket(drawPacketBuilder.End());
m_dynamicDraw->AddDrawPacket(m_scene, AZStd::move(drawPacket));
}
RPI::ColorSpaceId BloomExampleComponent::GetColorSpaceIdForIndex(uint8_t colorSpaceIndex) const
{
const AZStd::vector<AZ::RPI::ColorSpaceId> colorSpaces =
{
RPI::ColorSpaceId::SRGB,
RPI::ColorSpaceId::LinearSRGB,
RPI::ColorSpaceId::ACEScg,
RPI::ColorSpaceId::ACES2065
};
if (colorSpaceIndex >= colorSpaces.size())
{
AZ_Assert(false, "Invalid colorSpaceIndex");
return RPI::ColorSpaceId::SRGB;
}
return colorSpaces[colorSpaceIndex];
}
void BloomExampleComponent::CreateBloomEntity()
{
m_bloomEntity = CreateEntity("Bloom", GetEntityContextId());
// Bloom
auto* bloomSettings = m_postProcessFeatureProcessor->GetOrCreateSettingsInterface(m_bloomEntity->GetId());
m_bloomSettings = bloomSettings->GetOrCreateBloomSettingsInterface();
m_bloomSettings->SetEnabled(false);
m_bloomEntity->Activate();
AZ::EntityBus::MultiHandler::BusConnect(m_bloomEntity->GetId());
}
void BloomExampleComponent::QueueAssetPathForLoad(const AZStd::string& filePath)
{
AZ::Data::AssetId imageAssetId;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
imageAssetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath, filePath.c_str(),
azrtti_typeid <AZ::RPI::StreamingImageAsset>(), false);
AZ_Assert(imageAssetId.IsValid(), "Unable to load file %s", filePath.c_str());
QueueAssetIdForLoad(imageAssetId);
}
void BloomExampleComponent::QueueAssetIdForLoad(const AZ::Data::AssetId& imageAssetId)
{
if (imageAssetId.IsValid())
{
Data::Asset<AZ::RPI::StreamingImageAsset> imageAsset;
if (!imageAsset.Create(imageAssetId))
{
auto assetId = imageAssetId.ToString<AZStd::string>();
AZ_Assert(false, "Unable to create image asset for asset ID %s", assetId.c_str());
return;
}
auto image = AZ::RPI::StreamingImage::FindOrCreate(imageAsset);
if (image == nullptr)
{
auto imageAssetName = imageAssetId.ToString<AZStd::string>();
AZ_Assert(false, "Failed to find or create an image instance from image asset %s", imageAssetName.c_str());
return;
}
m_drawImage.m_assetId = imageAssetId;
m_drawImage.m_image = image;
m_drawImage.m_wasStreamed = false;
m_drawImage.m_srg->SetImage(m_imageInputIndex, m_drawImage.m_image);
}
}
} // namespace AtomSampleViewer