-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_HelloTriangle_Compute.cpp
292 lines (226 loc) · 8.19 KB
/
Main_HelloTriangle_Compute.cpp
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
// #if API == GL
// #include "Gfx/OpenGL/Api.h"
// #elif API == VK
// #elif API == DX
// #include "Gfx/D3D12/Api.h"
// #endif
#include <iostream>
#include "Gfx/Api.h"
#include "App/App.h"
#define MULTISTREAM 0
void WindowErrorCallback(const std::string &errorMessage)
{
std::cout << "Window Error : " << errorMessage << std::endl;
}
void ErrorCallback(const std::string &message)
{
std::cout << " Error : " << message << std::endl;
}
void InfoCallback(const std::string &message)
{
std::cout << " Info : " << message << std::endl;
}
void OnResizeWindow(app::window &Window, app::v2i NewSize);
struct application
{
std::shared_ptr<app::window> Window;
std::shared_ptr<gfx::context> GfxContext;
gfx::renderPassHandle SwapchainPass;
gfx::pipelineHandle PipelineHandleCompute;
gfx::pipelineHandle PipelineHandleSwapchain;
gfx::vertexBufferHandle VertexBufferHandle;
std::shared_ptr<gfx::swapchain> Swapchain;
std::shared_ptr<gfx::uniformGroup> Uniforms;
struct uniformData
{
gfx::v4f Color0;
gfx::v4f Color1;
};
uniformData UniformData1;
uniformData UniformData2;
gfx::imageHandle TextureHandle1;
gfx::bufferHandle UniformBufferHandle1;
gfx::bufferHandle UniformBufferHandle2;
gfx::bufferHandle StorageBufferHandle;
uint32_t Width, Height;
uint32_t InstanceCount = 1024;
void Init()
{
UniformData1 =
{
gfx::v4f(1,0,0,1),
gfx::v4f(0,0,0,1)
};
UniformData2 =
{
gfx::v4f(0,1,0,1),
gfx::v4f(0,0,0,1)
};
gfx::memory::Get()->Init();
// Create the appropriate graphics API object based on runtime configuration
Width = 1280;
Height = 720;
// gfx::memory::Get()->Init();
app::windowCreateOptions WindowCreateOptions;
WindowCreateOptions.Position = app::v2f(300, 100);
WindowCreateOptions.Size = app::v2f(Width, Height);
WindowCreateOptions.ErrorCallback = WindowErrorCallback;
#if GFX_API == GFX_VK
WindowCreateOptions.VersionMajor = 1;
WindowCreateOptions.VersionMinor = 0;
#elif GFX_API == GFX_GL
WindowCreateOptions.VersionMajor = 4;
WindowCreateOptions.VersionMinor = 5;
#endif
Window = std::make_shared<app::window>(WindowCreateOptions);
Window->OnResize = OnResizeWindow;
// Initialize the graphics API
gfx::context::initializeInfo ContextInitialize;
ContextInitialize.Extensions = Window->GetRequiredExtensions();
ContextInitialize.ErrorCallback = ErrorCallback;
ContextInitialize.InfoCallback = InfoCallback;
ContextInitialize.Debug = true;
GfxContext = gfx::context::Initialize(ContextInitialize, *Window);
Swapchain = GfxContext->CreateSwapchain(Width, Height);
gfx::imageData ImageData = gfx::ImageFromFile("resources/Textures/Debug.jpg");
gfx::imageCreateInfo ImageCreateInfo =
{
{0.0f,0.0f,0.0f,0.0f},
gfx::samplerFilter::Linear,
gfx::samplerFilter::Linear,
gfx::samplerWrapMode::ClampToBorder,
gfx::samplerWrapMode::ClampToBorder,
gfx::samplerWrapMode::ClampToBorder,
true
};
TextureHandle1 = GfxContext->CreateImage(ImageData, ImageCreateInfo);
gfx::image *Texture1 = GfxContext->GetImage(TextureHandle1);
float vertices[] =
{
0.0f, 0.25f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.25f, -0.25f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.25f, -0.25f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f
};
gfx::vertexStreamData VertexStream1 = {};
VertexStream1
.SetSize(sizeof(vertices))
.SetStride(7 * sizeof(float))
.SetData(&vertices)
.SetStreamIndex(0)
.AddAttribute({sizeof(float), 3, gfx::vertexAttributeType::Float, false, gfx::attributeSemantic::POSITION, 0, 0})
.AddAttribute({sizeof(float), 4, gfx::vertexAttributeType::Float, false, gfx::attributeSemantic::COLOR, 0, 1});
gfx::vertexBufferCreateInfo VertexBufferCreateInfo = {};
VertexBufferCreateInfo.Init()
.AddVertexStream(VertexStream1);
VertexBufferHandle = GfxContext->CreateVertexBuffer(VertexBufferCreateInfo);
SwapchainPass = GfxContext->GetDefaultRenderPass();
PipelineHandleCompute = GfxContext->CreatePipelineFromFile("resources/Shaders/Compute/Compute.json");
PipelineHandleSwapchain = GfxContext->CreatePipelineFromFile("resources/Shaders/Compute/Triangle_Compute.json");
UniformBufferHandle1 = GfxContext->CreateBuffer(sizeof(uniformData), gfx::bufferUsage::UniformBuffer, gfx::memoryUsage::CpuToGpu);
gfx::buffer *UniformBuffer1 = GfxContext->GetBuffer(UniformBufferHandle1);
UniformBuffer1->CopyData((uint8_t*)&UniformData1, sizeof(uniformData), 0);
UniformBufferHandle2 = GfxContext->CreateBuffer(sizeof(uniformData), gfx::bufferUsage::UniformBuffer, gfx::memoryUsage::CpuToGpu);
gfx::buffer *UniformBuffer2 = GfxContext->GetBuffer(UniformBufferHandle2);
UniformBuffer2->CopyData((uint8_t*)&UniformData2, sizeof(uniformData), 0);
std::vector<glm::vec4> InstancePositionsVec(InstanceCount);
uint32_t i=0;
for(uint32_t y=0; y<32; y++)
{
for(uint32_t x=0; x<32; x++)
{
InstancePositionsVec[i] = glm::vec4(
(((float)x / 32.0f) - 0.5f) * 2.0f,
(((float)y / 32.0f) - 0.5f) * 2.0f,
0,0);
i++;
}
}
StorageBufferHandle = GfxContext->CreateBuffer(InstanceCount * sizeof(glm::vec4), gfx::bufferUsage::StorageBuffer, gfx::memoryUsage::GpuOnly, sizeof(glm::vec4));
gfx::buffer *StorageBuffer = GfxContext->GetBuffer(StorageBufferHandle);
GfxContext->CopyDataToBuffer(StorageBufferHandle, InstancePositionsVec.data(), InstanceCount * sizeof(glm::vec4), 0);
//That's the content of a descriptor set
Uniforms = std::make_shared<gfx::uniformGroup>();
Uniforms->Reset()
.AddUniformBuffer(0, UniformBufferHandle1)
.AddUniformBuffer(3, UniformBufferHandle2)
.AddStorageBuffer(2, StorageBufferHandle)
.AddTexture(4, TextureHandle1);
//Tell the context that we'll be using this uniforms with this pipeline at binding 0
//It's possible to bind a uniform group to multiple pipelines.
GfxContext->BindUniformsToPipeline(Uniforms, PipelineHandleSwapchain, 0);
GfxContext->BindUniformsToPipeline(Uniforms, PipelineHandleCompute, 0);
//Update the bindings
Uniforms->Update();
}
void Cleanup()
{
GfxContext->WaitIdle();
DestroyProgramSpecific();
GfxContext->DestroySwapchain();
GfxContext->Cleanup();
gfx::memory *Memory = gfx::memory::Get();
Memory->Destroy();
delete Memory;
system("pause");
}
void DestroyProgramSpecific()
{
GfxContext->DestroyBuffer(UniformBufferHandle1);
GfxContext->DestroyBuffer(StorageBufferHandle);
GfxContext->DestroyPipeline(PipelineHandleSwapchain);
GfxContext->DestroyPipeline(PipelineHandleCompute);
GfxContext->DestroyVertexBuffer(VertexBufferHandle);
GfxContext->DestroyImage(TextureHandle1);
}
void Run()
{
float t = 0;
while(!Window->ShouldClose())
{
t += 0.1f;
UniformData1.Color0.r = (cos(t) + 1.0f) * 0.5f;
Uniforms->UpdateBuffer(0, &UniformData1, sizeof(uniformData), 0);
UniformData2.Color0.g = (cos(t + 3.14) + 1.0f) * 0.5f;
Uniforms->UpdateBuffer(1, &UniformData2, sizeof(uniformData), 0);
Window->PollEvents();
GfxContext->StartFrame();
// Set up the render state
std::shared_ptr<gfx::commandBuffer> CommandBuffer = GfxContext->GetCurrentFrameCommandBuffer();
// Begin recording commands into the command buffer
CommandBuffer->Begin();
CommandBuffer->BindComputePipeline(PipelineHandleCompute);
CommandBuffer->BindUniformGroup(Uniforms, 0);
CommandBuffer->Dispatch(1, 1, 1);
CommandBuffer->BeginPass(GfxContext->GetSwapchainFramebuffer(), {0.5f, 0.0f, 0.8f, 1.0f}, {1.0f, 0});
CommandBuffer->SetViewport(0.0f, 0.0f, (float)Width, (float)Height);
CommandBuffer->SetScissor(0, 0, Width, Height);
CommandBuffer->BindGraphicsPipeline(PipelineHandleSwapchain);
CommandBuffer->BindUniformGroup(Uniforms, 0);
CommandBuffer->BindVertexBuffer(VertexBufferHandle);
CommandBuffer->DrawArrays(0, 3, InstanceCount);
CommandBuffer->EndPass();
GfxContext->EndFrame();
// Present the rendered frame
GfxContext->Present();
}
}
void OnResize(uint32_t NewWidth, uint32_t NewHeight)
{
Width = NewWidth;
Height = NewHeight;
GfxContext->OnResize(Width, Height);
std::cout << "ON RESIZE " << NewWidth << std::endl;
}
};
application App;
void OnResizeWindow(app::window &Window, app::v2i NewSize)
{
App.OnResize(NewSize.x, NewSize.y);
}
int main()
{
App.Init();
App.Run();
App.Cleanup();
return 0;
}