-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
189 lines (159 loc) · 5.28 KB
/
Main.cpp
File metadata and controls
189 lines (159 loc) · 5.28 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
// CUDA
#include "cuda_runtime.h"
#include "helper_string.h"
#include "helper_image.h"
// Native
#include <cstdio>
#include <chrono>
#include <random>
#include <cmath>
// Work around false error squiggly lines inside VS; see: https://stackoverflow.com/a/27992604
#ifdef __INTELLISENSE__
#define KERNEL_2ARGS(gridSize, blockSize)
#define KERNEL_3ARGS(gridSize, blockSize, sh_mem)
#define KERNEL_4ARGS(gridSize, blockSize, sh_mem, stream)
#else
#define KERNEL_2ARGS(gridSize, blockSize) <<< gridSize, blockSize >>>
#define KERNEL_3ARGS(gridSize, blockSize, sh_mem) <<< gridSize, blockSize, sh_mem >>>
#define KERNEL_4ARGS(gridSize, blockSize, sh_mem, stream) <<< gridSize, blockSize, sh_mem, stream >>>
#endif
// Parameters
#define NPOINTS 10
// Forward declares
void jumpFloodWithCuda(unsigned char* hostResultCanvas, float2* hostSourceCanvas, int diagramXDim, int diagramYDim);
/**
* Generate a random uniform distribution of 2D floats.
*/
float2* randomUniformClamped2D(int nPoints)
{
std::mt19937_64 goodRng;
// Time dependent seed, the "modern" way
uint64_t genSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::seed_seq sequence{ uint32_t(genSeed & 0xFFFFFFFF), uint32_t(genSeed >> 32) };
goodRng.seed(sequence);
// Initialize uniform distribution, clamped to [0,1)
std::uniform_real_distribution<float> unif(0, 1);
// Generate point "cloud"
float2* points = (float2*)malloc(nPoints * sizeof(float2));
for (int iSim = 0; iSim < nPoints; ++iSim)
{
points[iSim].x = unif(goodRng);
points[iSim].y = unif(goodRng);
}
return points;
}
/**
* Main program: generate a Voronoi fill using the Jump-Flood algorithm, computed on the GPU.
*/
int main()
{
const int diagramXDim = 256;
const int diagramYDim = 256;
const int channels = 3;
/**
* 1. Generate some seed pixel locations.
*/
float2* voronoiSeedsUV = randomUniformClamped2D(NPOINTS);
// Texture coordinates (sub-pixel measurement, similar to gl_TexCoord)
float2 voronoiSeeds[NPOINTS];
for (int iSeed = 0; iSeed < NPOINTS; ++iSeed)
{
voronoiSeeds[iSeed] = float2{
.49f + floor(voronoiSeedsUV[iSeed].x * float(diagramXDim)),
.49f + floor(voronoiSeedsUV[iSeed].y * float(diagramYDim))
};
}
/**
* 2. Set the R,G values of the seed pixels to their own tex coordinates.
*/
float2 voronoiCanvas[diagramXDim * diagramYDim];
float2* voronoiCanvasFill = voronoiCanvas;
for (int iRow = 0; iRow < diagramYDim; iRow++)
{
for (int iCol = 0; iCol < diagramXDim; iCol++)
{
for (int iSeed = 0; iSeed < NPOINTS; ++iSeed)
{
if (iCol == int(voronoiSeeds[iSeed].x) && iRow == int(voronoiSeeds[iSeed].y))
{
printf("New seed at %f %f\n", voronoiSeeds[iSeed].x, voronoiSeeds[iSeed].y);
voronoiCanvasFill->x = voronoiSeeds[iSeed].x;
voronoiCanvasFill->y = voronoiSeeds[iSeed].y;
goto canvasIterationDone;
}
}
voronoiCanvasFill->x = 0.f;
voronoiCanvasFill->y = 0.f;
canvasIterationDone:
voronoiCanvasFill++;
}
}
/**
* Allocate GPU resources and launch the main computation kernel.
*/
unsigned char* voronoiOutputImage = (unsigned char*)malloc(diagramXDim * diagramYDim * channels * sizeof(unsigned char));
jumpFloodWithCuda(voronoiOutputImage, voronoiCanvas, diagramXDim, diagramYDim);
char* diagramFileName = "./data/Diagram.ppm";
__savePPM(diagramFileName, voronoiOutputImage, diagramXDim, diagramYDim, 3);
printf("Wrote '%s'\n", diagramFileName);
/**
* Cleanup.
*/
free(voronoiOutputImage);
free(voronoiSeedsUV);
return 0;
#if 0
/**
* Abandoned idea:
* 1. Read and rasterize a truetype font using the freetype lib
* 2. Calculate the signed-distance-function (usually stored in the alpha channel for post processing) of a large resolution raster
*/
const char* fontFaceFile = "TruenoLt.otf";
// Scan some default dirs to find the resource (inside ../../data, in this case)
const char* fontFacePath = sdkFindFilePath(fontFaceFile, 0);
if (fontFacePath == NULL)
{
fprintf(stderr, "Font face not found: %s\n", fontFaceFile);
return 1;
}
FT_Library ftLibraryHandle;
if (FT_Init_FreeType(&ftLibraryHandle))
{
fprintf(stderr, "Error initializing FreeType library handle\n");
return 1;
}
FT_Face fontFaceHandle;
if (FT_New_Face(ftLibraryHandle, fontFacePath, 0, &fontFaceHandle))
{
fprintf(stderr, "Error loading font face: %s\n", fontFacePath);
return 1;
}
FT_Set_Pixel_Sizes(fontFaceHandle, 0, 512); // Set one of the dims as 0 and it will replicate the other
if (FT_Load_Char(fontFaceHandle, 'g', FT_LOAD_RENDER)) // Load default AND render
{
fprintf(stderr, "Error loading character 'g'\n");
return 1;
}
char outputFilename[1024];
strcpy(outputFilename, fontFacePath);
strcpy(outputFilename + strlen(fontFacePath) - 4, "_out.pgm");
sdkSavePGM(
outputFilename,
fontFaceHandle->glyph->bitmap.buffer,
fontFaceHandle->glyph->bitmap.width,
fontFaceHandle->glyph->bitmap.rows
);
printf("Wrote '%s'\n", outputFilename);
char* terminator = "";
for (int iRow = 0; iRow < fontFaceHandle->glyph->bitmap.rows; iRow++)
{
printf("%s", terminator);
for (int iCol = 0; iCol < fontFaceHandle->glyph->bitmap.width; iCol++)
{
size_t charIndex = iRow * fontFaceHandle->glyph->bitmap.width + iCol;
printf("%u ", fontFaceHandle->glyph->bitmap.buffer[charIndex]);
}
terminator = "\n";
}
#endif
}