-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathConsoleComputeApp.cpp
More file actions
287 lines (243 loc) · 9.79 KB
/
ConsoleComputeApp.cpp
File metadata and controls
287 lines (243 loc) · 9.79 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
/******************************************************************************
Copyright 2023 Evgeny Gorodetskiy
Licensed under the Apache License, Version 2.0 (the "License"),
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************
FILE: ConsoleComputeApp.cpp
Tutorial demonstrating "game of life" computing on GPU in console application
******************************************************************************/
#include "ConsoleComputeApp.h"
#include "Shaders/GameOfLifeRules.h"
#include <Methane/Data/AppShadersProvider.h>
#include <Methane/Data/Math.hpp>
#include <Methane/Instrumentation.h>
#include <magic_enum/magic_enum.hpp>
#include <random>
namespace gfx = Methane::Graphics;
namespace data = Methane::Data;
namespace Methane::Tutorials
{
static const rhi::Devices& GetComputeDevices()
{
META_FUNCTION_TASK();
static const rhi::Devices& s_compute_devices = []()
{
rhi::System::Get().UpdateGpuDevices(rhi::DeviceCaps{
.features = rhi::DeviceFeatureMask{},
.render_queues_count = 0U,
.transfer_queues_count = 1U,
.compute_queues_count = 1U
});
return rhi::System::Get().GetGpuDevices();
}();
return s_compute_devices;
}
static Methane::Data::Bytes GetRandomFrameData(std::mt19937& random_engine, const gfx::FrameSize& frame_size, double initial_cells_ratio)
{
META_FUNCTION_TASK();
Methane::Data::Bytes frame_data(frame_size.GetPixelsCount(), std::byte());
const auto cells_count = static_cast<uint32_t>(static_cast<double>(frame_size.GetPixelsCount()) * initial_cells_ratio);
std::uniform_int_distribution<uint32_t> dist(0U, frame_size.GetPixelsCount() - 1U);
auto* cell_values = reinterpret_cast<uint8_t*>(frame_data.data()); // NOSONAR
for(uint32_t i = 0; i < cells_count; i++)
{
uint32_t p = 0U;
do
{
p = dist(random_engine);
}
while(cell_values[p]);
cell_values[p] = 1U;
}
return frame_data;
}
ConsoleComputeApp::ConsoleComputeApp()
: m_random_engine([]() { // NOSONAR
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
return std::mt19937(seed);
}())
{
InitUserInterface();
Init();
}
const rhi::Device* ConsoleComputeApp::GetComputeDevice() const
{
META_FUNCTION_TASK();
const int compute_device_index = GetComputeDeviceIndex();
const rhi::Devices& devices = GetComputeDevices();
return compute_device_index < static_cast<int>(devices.size()) ? &devices[compute_device_index] : nullptr;
}
int ConsoleComputeApp::Run()
{
if (!GetComputeDevice())
{
std::cerr << "ERROR: No GPU devices are available for computing!";
return 1;
}
return ConsoleApp::Run();
}
std::string_view ConsoleComputeApp::GetGraphicsApiName() const
{
return magic_enum::enum_name(rhi::System::GetNativeApi());
}
const std::string& ConsoleComputeApp::GetComputeDeviceName() const
{
META_FUNCTION_TASK();
static const std::string s_no_adapter = "N/A";
const rhi::Device* device_ptr = ConsoleComputeApp::GetComputeDevice();
return device_ptr ? device_ptr->GetAdapterName() : s_no_adapter;
}
const std::vector<std::string>& ConsoleComputeApp::GetComputeDeviceNames() const
{
META_FUNCTION_TASK();
static const std::vector<std::string> s_compute_device_names = []()
{
std::vector<std::string> device_names;
for(const rhi::Device& device : GetComputeDevices())
{
device_names.emplace_back(device.GetAdapterName());
}
return device_names;
}();
return s_compute_device_names;
}
uint32_t ConsoleComputeApp::GetFramesCountPerSecond() const
{
return IsScreenRefreshEnabled() ? m_fps_counter.GetFramesPerSecond() : 0U;
}
uint32_t ConsoleComputeApp::GetVisibleCellsCount() const
{
return m_visible_cells_count;
}
void ConsoleComputeApp::Init()
{
META_FUNCTION_TASK();
const rhi::Device* device_ptr = GetComputeDevice();
m_compute_context = device_ptr->CreateComputeContext(m_parallel_executor, {});
m_compute_context.SetName("Game of Life");
m_compute_state = m_compute_context.CreateComputeState({
.program = m_compute_context.CreateProgram({
.shader_set = rhi::Program::ShaderSet {
{ rhi::ShaderType::Compute, { data::ShaderProvider::Get(), { "GameOfLife", "MainCS" } } }
},
.argument_accessors = rhi::ProgramArgumentAccessors
{
META_PROGRAM_ARG_ROOT_VALUE_CONSTANT(rhi::ShaderType::Compute, "g_constants")
},
}),
.thread_group_size = rhi::ThreadGroupSize(16U, 16U, 1U)
});
m_compute_state.GetProgram().SetName("Game of Life Program");
m_compute_state.SetName("Game of Life Compute State");
m_compute_cmd_list = m_compute_context.GetComputeCommandKit().GetQueue().CreateComputeCommandList();
m_compute_cmd_list.SetName("Game of Life Compute");
m_compute_cmd_list_set = rhi::CommandListSet({ m_compute_cmd_list.GetInterface() });
rhi::TextureSettings frame_texture_settings = rhi::TextureSettings::ForImage(
gfx::Dimensions(GetFieldSize()),
std::nullopt, gfx::PixelFormat::R8Uint, false,
rhi::ResourceUsageMask{
rhi::ResourceUsage::ShaderRead,
rhi::ResourceUsage::ShaderWrite,
rhi::ResourceUsage::ReadBack
}
);
m_frame_texture = m_compute_context.CreateTexture(frame_texture_settings);
m_frame_texture.SetName("Game of Life Frame Texture");
const Constants game_constants{ static_cast<uint>(GetGameRuleIndex()) };
m_compute_bindings = m_compute_state.GetProgram().CreateBindings({
{ { rhi::ShaderType::Compute, "g_constants" }, rhi::RootConstant(game_constants) },
{ { rhi::ShaderType::Compute, "g_frame_texture" }, m_frame_texture.GetResourceView() }
});
m_compute_bindings.SetName("Game of Life Compute Bindings");
RandomizeFrameData();
// Complete bindings and texture initialization
m_compute_context.CompleteInitialization();
}
void ConsoleComputeApp::Release()
{
META_FUNCTION_TASK();
m_compute_context.WaitForGpu(rhi::ContextWaitFor::ComputeComplete);
m_compute_cmd_list_set = {};
m_compute_cmd_list = {};
m_compute_bindings = {};
m_frame_texture = {};
m_compute_state = {};
m_compute_context = {};
}
void ConsoleComputeApp::Compute()
{
META_FUNCTION_TASK();
const data::FrameSize& field_size = GetFieldSize();
const rhi::CommandQueue& compute_cmd_queue = m_compute_context.GetComputeCommandKit().GetQueue();
const rhi::ThreadGroupSize& thread_group_size = m_compute_state.GetSettings().thread_group_size;
const rhi::ThreadGroupsCount thread_groups_count(data::DivCeil(field_size.GetWidth(), thread_group_size.GetWidth()),
data::DivCeil(field_size.GetHeight(), thread_group_size.GetHeight()),
1U);
META_DEBUG_GROUP_VAR(s_debum_group, "Compute Frame");
m_compute_cmd_list.ResetWithState(m_compute_state, &s_debum_group);
m_compute_cmd_list.SetProgramBindings(m_compute_bindings);
m_compute_cmd_list.Dispatch(thread_groups_count);
m_compute_cmd_list.Commit();
compute_cmd_queue.Execute(m_compute_cmd_list_set);
m_compute_context.WaitForGpu(rhi::ContextWaitFor::ComputeComplete);
m_frame_data = m_frame_texture.GetData(compute_cmd_queue);
m_fps_counter.OnCpuFrameReadyToPresent();
}
void ConsoleComputeApp::Present(ftxui::Canvas& canvas)
{
META_FUNCTION_TASK();
const data::FrameSize& field_size = GetFieldSize();
const data::FrameRect& frame_rect = GetVisibleFrameRect();
const uint8_t* cells = m_frame_data.GetDataPtr<uint8_t>();
m_visible_cells_count = 0U;
for (uint32_t y = 0; y < frame_rect.size.GetHeight(); y++)
{
const uint32_t cell_y = frame_rect.origin.GetY() + y;
const uint32_t cell_shift = cell_y * field_size.GetWidth();
for (uint32_t x = 0; x < frame_rect.size.GetWidth(); x++)
{
const uint32_t cell_x = frame_rect.origin.GetX() + x;
if (cells[cell_shift + cell_x])
{
canvas.DrawBlockOn(static_cast<int>(x), static_cast<int>(y));
m_visible_cells_count++;
}
}
}
m_fps_counter.OnCpuFramePresented();
}
void ConsoleComputeApp::Restart()
{
META_FUNCTION_TASK();
std::unique_lock lock(GetScreenRefreshMutex());
m_compute_context.WaitForGpu(rhi::ContextWaitFor::ComputeComplete);
RandomizeFrameData();
ResetRules();
}
void ConsoleComputeApp::ResetRules()
{
const Constants game_constants{ static_cast<uint>(GetGameRuleIndex()) };
m_compute_bindings.Get({ rhi::ShaderType::Compute, "g_constants" })
.SetRootConstant(rhi::RootConstant(game_constants));
}
void ConsoleComputeApp::RandomizeFrameData()
{
META_FUNCTION_TASK();
// Randomize initial game state
m_frame_data = rhi::SubResource(GetRandomFrameData(m_random_engine, GetFieldSize(), GetInitialCellsRatio()));
// Set frame texture data
m_frame_texture.SetData(m_compute_context.GetComputeCommandKit().GetQueue(), { m_frame_data });
}
} // namespace Methane::Tutorials
int main(int, const char*[])
{
return Methane::Tutorials::ConsoleComputeApp().Run();
}