-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cpp
345 lines (264 loc) · 11.4 KB
/
Main.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
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
#include "SDL.h"
#include "SDL_gpu.h"
#include "imgui.h"
#include "imgui_impl_sdl.h"
#include "imgui_impl_opengl3.h"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <math.h>
const int SCREEN_FPS = 100;
const int SCREEN_TICK_PER_FRAME = 1000 / SCREEN_FPS;
// Loads a shader and prepends version/compatibility info before compiling it.
// Normally, you can just use GPU_LoadShader() for shader source files or GPU_CompileShader() for strings.
// However, some hardware (certain ATI/AMD cards) does not let you put non-#version preprocessing at the top of the file.
// Therefore, I need to prepend the version info here so I can support both GLSL and GLSLES with one shader file.
Uint32 load_shader(GPU_ShaderEnum shader_type, const char* filename)
{
SDL_RWops* rwops;
Uint32 shader;
char* source;
int header_size, file_size;
const char* header = "";
GPU_Renderer* renderer = GPU_GetCurrentRenderer();
// Open file
rwops = SDL_RWFromFile(filename, "rb");
if(rwops == NULL)
{
GPU_PushErrorCode("load_shader", GPU_ERROR_FILE_NOT_FOUND, "Shader file \"%s\" not found", filename);
return 0;
}
// Get file size
file_size = SDL_RWseek(rwops, 0, SEEK_END);
SDL_RWseek(rwops, 0, SEEK_SET);
// Get size from header
if(renderer->shader_language == GPU_LANGUAGE_GLSL)
{
if(renderer->max_shader_version >= 120)
header = "#version 120\n";
else
header = "#version 110\n"; // Maybe this is good enough?
}
else if(renderer->shader_language == GPU_LANGUAGE_GLSLES)
header = "#version 100\nprecision mediump int;\nprecision mediump float;\n";
header_size = strlen(header);
// Allocate source buffer
source = (char*)malloc(sizeof(char)*(header_size + file_size + 1));
// Prepend header
strcpy(source, header);
// Read in source code
SDL_RWread(rwops, source + strlen(source), 1, file_size);
source[header_size + file_size] = '\0';
// Compile the shader
shader = GPU_CompileShader(shader_type, source);
// Clean up
free(source);
SDL_RWclose(rwops);
return shader;
}
GPU_ShaderBlock load_shader_program(Uint32* p, const char* vertex_shader_file, const char* fragment_shader_file)
{
Uint32 v, f;
v = load_shader(GPU_VERTEX_SHADER, vertex_shader_file);
if(!v)
GPU_LogError("Failed to load vertex shader (%s): %s\n", vertex_shader_file, GPU_GetShaderMessage());
f = load_shader(GPU_FRAGMENT_SHADER, fragment_shader_file);
if(!f)
GPU_LogError("Failed to load fragment shader (%s): %s\n", fragment_shader_file, GPU_GetShaderMessage());
*p = GPU_LinkShaders(v, f);
if(!*p)
{
GPU_ShaderBlock b = {-1, -1, -1, -1};
GPU_LogError("Failed to link shader program (%s + %s): %s\n", vertex_shader_file, fragment_shader_file, GPU_GetShaderMessage());
return b;
}
{
GPU_ShaderBlock block = GPU_LoadShaderBlock(*p, "gpu_Vertex", "gpu_TexCoord", "gpu_Color", "gpu_ModelViewProjectionMatrix");
GPU_ActivateShaderProgram(*p, &block);
return block;
}
}
void free_shader(Uint32 p)
{
GPU_FreeShaderProgram(p);
}
// Not going to change dynamically in this demo, so can be done once.
void prepare_mask_shader(Uint32 shader, GPU_Image* image, GPU_Image* mask_image)
{
if(image != NULL)
{
GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_x"), image->w);
GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_y"), image->h);
}
if(mask_image != NULL)
{
GPU_SetUniformf(GPU_GetUniformLocation(shader, "mask_resolution_x"), mask_image->w);
GPU_SetUniformf(GPU_GetUniformLocation(shader, "mask_resolution_y"), mask_image->h);
}
GPU_SetUniformf(GPU_GetUniformLocation(shader, "offset_x"), 0.0f);
GPU_SetUniformf(GPU_GetUniformLocation(shader, "offset_y"), 0.0f);
GPU_SetShaderImage(mask_image, GPU_GetUniformLocation(shader, "mask_tex"), 1);
}
void prepare_marching_ants_shader(Uint32 shader, GPU_Target* screen, GPU_Image* image)
{
if(image != NULL)
{
GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_x"), image->w);
GPU_SetUniformf(GPU_GetUniformLocation(shader, "resolution_y"), image->h);
}
GPU_SetUniformf(GPU_GetUniformLocation(shader, "screen_w"), screen->w);
GPU_SetUniformf(GPU_GetUniformLocation(shader, "screen_h"), screen->h);
GPU_SetUniformf(GPU_GetUniformLocation(shader, "zoom"), 1.0f);
}
// Will change every frame
void update_color_shader(float r, float g, float b, float a, int color_loc)
{
float fcolor[4] = {r, g, b, a};
GPU_SetUniformfv(color_loc, 4, 1, fcolor);
}
void update_vertex_color_shader(float t, float vertex_colors[24])
{
for(int i = 0; i < 6; ++i)
{
vertex_colors[i] = 0.5f + 0.5f*(1 + sin(t + (i)))/2;
vertex_colors[i+1] = 0.5f + 0.5f*(1 + sin(t + (i+1)))/2;
vertex_colors[i+2] = 0.5f + 0.5f*(1 + sin(t + (i+2)))/2;
vertex_colors[i+3] = 1.0f;
}
}
void update_marching_ants_shader(float t, int time_loc)
{
GPU_SetUniformf(time_loc, t);
}
void main_loop(GPU_Target* screen)
{
GPU_Image* image = GPU_LoadImage("data/test3.png");
Uint32 color_shader;
GPU_ShaderBlock color_block = load_shader_program(&color_shader, "data/shaders/common.vert", "data/shaders/color.frag");
int color_loc = GPU_GetUniformLocation(color_shader, "myColor");
// Disabled for now because it seems to be a bit buggy
// Having problems with this... It seems to take 1 color for each vertex, even shared ones (indices, so not 16 floats but 24).
// It also seems not to be working properly anyhow. Some vertices are still dark.
/*Uint32 vertex_color_shader;
GPU_ShaderBlock vertex_color_block = load_shader_program(&vertex_color_shader, "data/shaders/common.vert", "data/shaders/common.frag");
vertex_color_block.color_loc = -1; // Disable so it doesn't conflict with custom attribute values
int vertex_color_loc = GPU_GetAttributeLocation(vertex_color_shader, "gpu_Color");
GPU_AttributeFormat vertex_color_attribute_format = GPU_MakeAttributeFormat(4, GPU_TYPE_FLOAT, 0, 4*sizeof(float), 0);
float vertex_colors[24];
GPU_Attribute vertex_color_attribute = GPU_MakeAttribute(vertex_color_loc, vertex_colors, vertex_color_attribute_format);*/
Uint32 mask_shader;
GPU_ShaderBlock mask_block = load_shader_program(&mask_shader, "data/shaders/common.vert", "data/shaders/alpha_mask.frag");
GPU_Image* mask_image = GPU_LoadImage("data/test2.png");
prepare_mask_shader(mask_shader, image, mask_image);
Uint32 marching_ants_shader;
GPU_ShaderBlock marching_ants_block = load_shader_program(&marching_ants_shader, "data/shaders/common.vert", "data/shaders/marching_ants.frag");
prepare_marching_ants_shader(marching_ants_shader, screen, image);
int marching_ants_time_loc = GPU_GetUniformLocation(marching_ants_shader, "time");
// TODO: Add a real vertex shader
Uint8 done;
SDL_Event event;
///---Start ImGUI---///
SDL_GLContext& gl_context = screen->context->context;
SDL_Window* window = SDL_GetWindowFromID(screen->context->windowID);
// Setup Dear ImGui binding
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
const char* glsl_version = "#version 120";
ImGui_ImplOpenGL3_Init(glsl_version);
// Setup style
ImGui::StyleColorsClassic();
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
bool show_demo_window = true;
int startTicks = SDL_GetTicks();
//Start counting frames per second
done = 0;
while(!done)
{
//FPS cap start tick
int fpsCapStartTicks = SDL_GetTicks();
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
done = 1;
else if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
}
ImGui_ImplSDL2_ProcessEvent(&event);
}
GPU_ClearRGBA(screen, clear_color.x * 255, clear_color.y * 255, clear_color.z * 255, clear_color.w * 255);
/** Start drawing staff with SDL_gpu **/
// Color mod
float t = fpsCapStartTicks / 1000.f;
GPU_ActivateShaderProgram(color_shader, &color_block);
update_color_shader((1+sin(t))/2, (1+sin(t+1))/2, (1+sin(t+2))/2, 1.0f, color_loc);
GPU_Blit(image, NULL, screen, screen->w/4, screen->h/4);
// Per-vertex colors
// Disabled for now because it seems to be a bit buggy
/*GPU_ActivateShaderProgram(vertex_color_shader, &vertex_color_block);
update_vertex_color_shader(t, vertex_colors);
GPU_SetAttributeSource(6, vertex_color_attribute);
GPU_Blit(image, NULL, screen, 3*screen->w/4, screen->h/4);
GPU_SetAttributeSource(0, vertex_color_attribute);*/
// Alpha mask
GPU_ActivateShaderProgram(mask_shader, &mask_block);
GPU_Blit(image, NULL, screen, screen->w/4, 3*screen->h/4);
// Marching ants
GPU_ActivateShaderProgram(0, NULL);
GPU_Blit(image, NULL, screen, 3*screen->w/4, 3*screen->h/4);
GPU_ActivateShaderProgram(marching_ants_shader, &marching_ants_block);
update_marching_ants_shader(t, marching_ants_time_loc);
GPU_Blit(image, NULL, screen, 3*screen->w/4, 3*screen->h/4);
GPU_ActivateShaderProgram(0, NULL);
GPU_FlushBlitBuffer(); // IMPORTANT: run GPU_FlushBlitBuffer before ImGui_ImplOpenGL3_NewFrame();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(window);
ImGui::NewFrame();
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
ImGui::Begin("Debug Info");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
GPU_Renderer* renderer = GPU_GetCurrentRenderer();
GPU_RendererID id = renderer->id;
ImGui::Text("Using renderer: %s (%d.%d)\n", id.name, id.major_version, id.minor_version);
ImGui::Text(" Shader versions supported: %d to %d\n\n", renderer->min_shader_version, renderer->max_shader_version);
ImGui::End();
// Rendering
ImGui::Render();
SDL_GL_MakeCurrent(window, gl_context);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
GPU_Flip(screen);
// If frame finished early
int frameTicks = SDL_GetTicks() - fpsCapStartTicks;
if( frameTicks < SCREEN_TICK_PER_FRAME )
{
//Wait remaining time
SDL_Delay( SCREEN_TICK_PER_FRAME - frameTicks );
}
}
GPU_FreeImage(mask_image);
GPU_FreeImage(image);
free_shader(color_shader);
free_shader(mask_shader);
free_shader(marching_ants_shader);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
}
int main(int, char**)
{
GPU_Target* screen;
GPU_SetPreInitFlags(GPU_INIT_DISABLE_VSYNC | GPU_INIT_REQUEST_COMPATIBILITY_PROFILE);
screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);
if(screen == NULL)
return -1;
main_loop(screen);
GPU_Quit();
return 0;
}