-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain_rasterize_triangles.cpp
260 lines (188 loc) · 7.21 KB
/
main_rasterize_triangles.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
#include <iostream>
#include <filesystem>
#include <locale.h>
#include <string>
#include <queue>
#include <vector>
#include <mutex>
#include <thread>
#include <format>
#include "CudaModularProgram.h"
#include "GLRenderer.h"
#include "cudaGL.h"
// #include "builtin_types.h"
#include "unsuck.hpp"
#include "ObjLoader.h"
#include "HostDeviceInterface.h"
using namespace std;
CUdeviceptr cptr_buffer;
CUdeviceptr cptr_positions, cptr_uvs, cptr_colors;
CUdeviceptr cptr_texture;
CUgraphicsResource cugl_colorbuffer;
CudaModularProgram* cuda_program = nullptr;
CUevent cevent_start, cevent_end;
shared_ptr<ObjData> model;
// vector<uint32_t> colors;
int colorMode = COLORMODE_TEXTURE;
int sampleMode = SAMPLEMODE_LINEAR;
void initCuda(){
cuInit(0);
CUdevice cuDevice;
CUcontext context;
cuDeviceGet(&cuDevice, 0);
cuCtxCreate(&context, 0, cuDevice);
}
void renderCUDA(shared_ptr<GLRenderer> renderer){
cuGraphicsGLRegisterImage(
&cugl_colorbuffer,
renderer->view.framebuffer->colorAttachments[0]->handle,
GL_TEXTURE_2D,
CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD);
CUresult resultcode = CUDA_SUCCESS;
CUdevice device;
int numSMs;
cuCtxGetDevice(&device);
cuDeviceGetAttribute(&numSMs, CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT, device);
int workgroupSize = 128;
int numGroups;
resultcode = cuOccupancyMaxActiveBlocksPerMultiprocessor(&numGroups, cuda_program->kernels["kernel"], workgroupSize, 0);
numGroups *= numSMs;
//numGroups = 100;
// make sure at least 10 workgroups are spawned)
numGroups = std::clamp(numGroups, 10, 100'000);
std::vector<CUgraphicsResource> dynamic_resources = {cugl_colorbuffer};
cuGraphicsMapResources(dynamic_resources.size(), dynamic_resources.data(), ((CUstream)CU_STREAM_DEFAULT));
CUDA_RESOURCE_DESC res_desc = {};
res_desc.resType = CUresourcetype::CU_RESOURCE_TYPE_ARRAY;
cuGraphicsSubResourceGetMappedArray(&res_desc.res.array.hArray, cugl_colorbuffer, 0, 0);
CUsurfObject output_surf;
cuSurfObjectCreate(&output_surf, &res_desc);
cuEventRecord(cevent_start, 0);
float time = now();
Uniforms uniforms;
uniforms.width = renderer->width;
uniforms.height = renderer->height;
uniforms.time = now();
uniforms.colorMode = colorMode;
uniforms.sampleMode = sampleMode;
glm::mat4 rotX = glm::rotate(glm::mat4(), 3.1415f * 0.5f, glm::vec3(1.0, 0.0, 0.0));
glm::mat4 world = rotX;
glm::mat4 view = renderer->camera->view;
glm::mat4 proj = renderer->camera->proj;
glm::mat4 worldViewProj = proj * view * world;
world = glm::transpose(world);
view = glm::transpose(view);
proj = glm::transpose(proj);
worldViewProj = glm::transpose(worldViewProj);
memcpy(&uniforms.world, &world, sizeof(world));
memcpy(&uniforms.view, &view, sizeof(view));
memcpy(&uniforms.proj, &proj, sizeof(proj));
memcpy(&uniforms.transform, &worldViewProj, sizeof(worldViewProj));
float values[16];
memcpy(&values, &worldViewProj, sizeof(worldViewProj));
void* args[] = {
&uniforms, &cptr_buffer, &output_surf,
&model->numTriangles, &cptr_positions, &cptr_uvs, &cptr_colors,
&cptr_texture
};
auto res_launch = cuLaunchCooperativeKernel(cuda_program->kernels["kernel"],
numGroups, 1, 1,
workgroupSize, 1, 1,
0, 0, args);
if(res_launch != CUDA_SUCCESS){
const char* str;
cuGetErrorString(res_launch, &str);
printf("error: %s \n", str);
}
cuEventRecord(cevent_end, 0);
// cuEventSynchronize(cevent_end);
// {
// float total_ms;
// cuEventElapsedTime(&total_ms, cevent_start, cevent_end);
// cout << "CUDA durations: " << endl;
// cout << std::format("total: {:6.1f} ms", total_ms) << endl;
// }
cuCtxSynchronize();
cuSurfObjectDestroy(output_surf);
cuGraphicsUnmapResources(dynamic_resources.size(), dynamic_resources.data(), ((CUstream)CU_STREAM_DEFAULT));
cuGraphicsUnregisterResource(cugl_colorbuffer);
}
void initCudaProgram(
shared_ptr<GLRenderer> renderer,
shared_ptr<ObjData> model,
vector<uint32_t>& texture
){
cuMemAlloc(&cptr_buffer, 100'000'000);
int numVertices = model->numTriangles * 3;
cuMemAlloc(&cptr_positions, numVertices * 12);
cuMemAlloc(&cptr_uvs , numVertices * 8);
cuMemcpyHtoD(cptr_positions, model->xyz.data(), numVertices * 12);
cuMemcpyHtoD(cptr_uvs , model->uv.data() , numVertices * 8);
cuMemAlloc(&cptr_texture , 4 * 1024 * 1024);
cuMemcpyHtoD(cptr_texture, texture.data(), 4 * 1024 * 1024);
cuda_program = new CudaModularProgram({
.modules = {
"./modules/rasterizeTriangles/rasterize.cu",
"./modules/rasterizeTriangles/utils.cu",
},
.kernels = {"kernel"}
});
cuEventCreate(&cevent_start, 0);
cuEventCreate(&cevent_end, 0);
cuGraphicsGLRegisterImage(&cugl_colorbuffer, renderer->view.framebuffer->colorAttachments[0]->handle, GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD);
}
int main(){
cout << std::setprecision(2) << std::fixed;
setlocale( LC_ALL, "en_AT.UTF-8" );
auto renderer = make_shared<GLRenderer>();
renderer->controls->yaw = -2.6;
renderer->controls->pitch = -0.4;
renderer->controls->radius = 6.0;
renderer->controls->target = {0.0f, 0.0f, 0.0f};
initCuda();
model = ObjLoader::load("./resources/spot/spot_triangulated.obj");
auto ppmdata = readBinaryFile("./resources/spot/spot_texture.ppm", 40, 1000000000000);
vector<uint32_t> colors(1024 * 1024, 0);
for(int i = 0; i < 1024 * 1024; i++){
uint32_t r = ppmdata->get<uint8_t>(3 * i + 0);
uint32_t g = ppmdata->get<uint8_t>(3 * i + 1);
uint32_t b = ppmdata->get<uint8_t>(3 * i + 2);
uint32_t color = r | (g << 8) | (b << 16);
colors[i] = color;
}
initCudaProgram(renderer, model, colors);
auto update = [&](){
};
auto render = [&](){
renderer->view.framebuffer->setSize(renderer->width, renderer->height);
glBindFramebuffer(GL_FRAMEBUFFER, renderer->view.framebuffer->handle);
renderCUDA(renderer);
{ // INFO WINDOW
ImGui::SetNextWindowPos(ImVec2(10, 280));
ImGui::SetNextWindowSize(ImVec2(490, 180));
ImGui::Begin("Infos");
ImGui::BulletText("Cuda software rasterizer rendering 25 instances of the spot model \n(5856 triangles, each).");
ImGui::BulletText("Each cuda block renders one triangle, \nwith each thread processing a different fragment.");
ImGui::BulletText("Cuda Kernel: rasterizeTriangles/rasterize.cu");
ImGui::BulletText("Spot model courtesy of Keenan Crane.");
ImGui::End();
}
{ // SETTINGS WINDOW
ImGui::SetNextWindowPos(ImVec2(10, 280 + 180 + 10));
ImGui::SetNextWindowSize(ImVec2(490, 230));
ImGui::Begin("Settings");
ImGui::Text("Color:");
ImGui::RadioButton("Texture", &colorMode, COLORMODE_TEXTURE);
ImGui::RadioButton("UVs", &colorMode, COLORMODE_UV);
ImGui::RadioButton("Triangle Index", &colorMode, COLORMODE_TRIANGLE_ID);
ImGui::RadioButton("Time", &colorMode, COLORMODE_TIME);
ImGui::RadioButton("Time (normalized)", &colorMode, COLORMODE_TIME_NORMALIZED);
ImGui::Text("Sampling:");
ImGui::RadioButton("Nearest", &sampleMode, SAMPLEMODE_NEAREST);
ImGui::RadioButton("Linear", &sampleMode, SAMPLEMODE_LINEAR);
ImGui::End();
}
};
renderer->loop(update, render);
return 0;
}