summaryrefslogtreecommitdiff
path: root/gl_calls.cpp
blob: b08615aa67e59838b532b8b6ade30bdc613d9191 (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "gl_calls.h"

const char *DefaultVertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"out vec2 TexCoord;\n"
"uniform int VertexMode;\n"
"uniform vec3 CompDimensions;\n"
"void main()\n"
"{\n"
" if (VertexMode == 0) {\n"
"   gl_Position = vec4(aPos, 1.0);\n"
"} else {\n"
"   gl_Position = vec4(vec2(aPos.x / CompDimensions.x, aPos.y / CompDimensions.y) * 2 - 1.0f, 0.0f, 1.0);\n"
"}\n"
" TexCoord = aTexCoord;\n"
"}\0";
const char *DefaultFragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D Texture;\n"
"uniform int FragmentMode;\n"
"void main()\n"
"{\n"
"vec4 Col = texture(Texture, TexCoord);\n"
" if (FragmentMode == 0) {\n"
"   FragColor = Col;\n"
"} else {\n"
"   FragColor = vec4(vec3(1.0f), Col.a);\n"
"}\n"
"}\0";
const char *FragmentShaderEffectSource[] = {"",
"#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform float Start;\n"
"uniform float Mid;\n"
"uniform float End;\n"
"uniform vec4 StartCol;\n"
"uniform vec4 MidCol;\n"
"uniform vec4 EndCol;\n"
"uniform sampler2D ourTexture;\n"
"void main()\n"
"{\n"
"vec4 OutCol = texture(ourTexture, TexCoord);\n"
// individual channels
"vec4 ColorI = pow(OutCol, MidCol);\n"
"vec4 ValI = 1.0f / (EndCol - StartCol) * (ColorI - StartCol);\n"
// global channel (doesn't affect alpha)
"vec4 ColorG = pow(ValI, vec4(Mid));\n"
"vec4 ValG = 1.0f / (End - Start) * (ColorG - Start);\n"
"ValG = vec4(ValG.rgb, ValI.a);\n"
"FragColor = clamp(ValG, 0.0f, 1.0f);\n"
"}\0",
"#version 330 core\n"
"uniform float Radius;\n"
"uniform vec2 Direction;\n"
"uniform sampler2D ourTexture;\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"\n"
"vec4 blur(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {\n"
"    vec4 color = vec4(0.0f);\n"
"    float Omega = Radius / 3;\n"
"    float Divisor = 2*Omega*Omega;\n"
"    float A2 = 1.0f / (Omega * sqrt(2*3.141592));\n"
"    for (float Span = -round(Radius); Span < round(Radius); Span++) {\n"
"        float Dividend = -Span * Span;\n"
"        float Multiplier =  A2 * exp(Dividend/Divisor);\n"
"        vec2 Dir = Span*direction;\n"
"        color += texture2D(image, uv + (Dir / resolution)) * Multiplier;\n"
"    }\n"
"    return color;\n"
"}\n"
"void main(void) {\n"
"    gl_FragColor = blur(ourTexture, TexCoord, vec2(1280, 720),  Direction);\n"
"}\0"
};


// #include "effects_gl.cpp"

static void GL_InitDefaultShader() {
    DefaultVertexShader = glCreateShader(GL_VERTEX_SHADER);

    glShaderSource(DefaultVertexShader, 1, &DefaultVertexShaderSource, NULL);
    glCompileShader(DefaultVertexShader);

    int success;
    char infoLog[512];
    glGetShaderiv(DefaultVertexShader, GL_COMPILE_STATUS, &success);

    if(!success)
    {
        glGetShaderInfoLog(DefaultVertexShader, 512, NULL, infoLog);
        printf("Vertex shader fail:\n %s", infoLog);
    }

    uint32 DefaultFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(DefaultFragmentShader, 1, &DefaultFragmentShaderSource, NULL);
    glCompileShader(DefaultFragmentShader);

    glGetShaderiv(DefaultFragmentShader, GL_COMPILE_STATUS, &success);

    if(!success)
    {
        glGetShaderInfoLog(DefaultFragmentShader, 512, NULL, infoLog);
        printf("Fragment shader fail:\n %s", infoLog);
    }

    // Shader programs link both types of shaders together.
    DefaultShaderProgram = glCreateProgram();

    glAttachShader(DefaultShaderProgram, DefaultVertexShader);
    glAttachShader(DefaultShaderProgram, DefaultFragmentShader);
    glLinkProgram(DefaultShaderProgram);

    glGetProgramiv(DefaultShaderProgram, GL_LINK_STATUS, &success);
    if(!success) {
        glGetProgramInfoLog(DefaultShaderProgram, 512, NULL, infoLog);
        printf("Shader linkage fail:\n %s", infoLog);
    }

    // Default vertex shader is still needed to link to other effects.
    glDeleteShader(DefaultFragmentShader);
}

static void GL_InitDefaultVerts() {

    unsigned int GLIndices[] = {
        0, 1, 3,
        1, 2, 3
    };

    // Indices!
    glGenVertexArrays(1, &DefaultVerts.VertexArrayObject);

    glGenBuffers(1, &DefaultVerts.ElementBufferObject);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, DefaultVerts.ElementBufferObject);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLIndices), GLIndices,
                 GL_STATIC_DRAW);
    glGenBuffers(1, &DefaultVerts.VertexBufferObject);

    // Our vertices need to be stored in this buffer.
    glBindBuffer(GL_ARRAY_BUFFER, DefaultVerts.VertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GL_DefaultVertices), GL_DefaultVertices, GL_STATIC_DRAW);

    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    // texture coordinate (note the last parameter's offset)
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
}

#if 0
void
GL_InitTexture(gl_effect_layer *Test)
{
    glGenFramebuffers(1, &Test->FramebufferObject);
    glGenTextures(1, &Test->Texture);
    glGenRenderbuffers(1, &Test->Color_Renderbuffer);
    glGenRenderbuffers(1, &Test->Stencil_Renderbuffer);
    Test->Initialized = true;
}

void
GL_UpdateTexture(gl_effect_layer *Test, void *Data, uint16 Width, uint16 Height, bool32 Multisample)
{
    glViewport(0, 0, Width, Height);

    if (!Test->Initialized) {
        GL_InitTexture(Test);
    }

    GLenum Target = GL_TEXTURE_2D;
    if (Multisample)
        Target = GL_TEXTURE_2D_MULTISAMPLE;

    glBindTexture(Target, Test->Texture);

    if (Multisample) {
        // glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGB, Width, Height, GL_TRUE);
        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);

        glBindRenderbuffer(GL_RENDERBUFFER, Test->Color_Renderbuffer);
        glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, Width, Height);

        glBindRenderbuffer(GL_RENDERBUFFER, (GLuint)Test->Stencil_Renderbuffer );
        glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_STENCIL_INDEX8, Width, Height );
    } else {
        glTexParameteri(Target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(Target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Width, Height, 0, GL_RGBA,
        //              GL_UNSIGNED_BYTE, Data);
        glBindTexture(GL_TEXTURE_2D, 0);

        glBindRenderbuffer(GL_RENDERBUFFER, Test->Color_Renderbuffer);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, Width, Height);

        glBindRenderbuffer(GL_RENDERBUFFER, (GLuint)Test->Stencil_Renderbuffer );
        glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, Width, Height );
    }

    glBindFramebuffer(GL_FRAMEBUFFER, Test->FramebufferObject);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, Test->Color_Renderbuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, Test->Stencil_Renderbuffer);

    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE) {
        printf("incomplete framebuffer");
        Assert(0);
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

static void
GL_BindDefaultVertexArrays()
{
    glBindVertexArray(DefaultVerts.VertexArrayObject);
    // Switch to main buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, DefaultVerts.ElementBufferObject);
    glBindBuffer(GL_ARRAY_BUFFER, DefaultVerts.VertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GL_DefaultVertices), GL_DefaultVertices, GL_STATIC_DRAW);
    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    // texture coordinate (note the last parameter's offset)
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);

}

static void
GL_RasterizeShape(project_layer *Layer, mask *Mask)
{
    gl_effect_layer Test = Layer->BitmapInfo.TestM;
    gl_effect_layer Test2 = Layer->BitmapInfo.Test;
    gl_vertex_shader VertData;

    uint32 Width = Layer->Source->Info.Width;
    uint32 Height = Layer->Source->Info.Height;

    uint8 *Data = (uint8 *)Layer->BitmapInfo.BitmapBuffer;
    glBindFramebuffer(GL_FRAMEBUFFER, Test.FramebufferObject);

	glEnable(GL_STENCIL_TEST);
    // glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    glStencilFunc(GL_ALWAYS, 0, 0xFF);   // always write
    glStencilMask(0xff);                 // allow writing; ANDs any writes to the stencil buffer with this

	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

    glUseProgram(DefaultShaderProgram);

    int VertexMode = glGetUniformLocation(DefaultShaderProgram, "VertexMode");
    int FragmentMode = glGetUniformLocation(DefaultShaderProgram, "FragmentMode");
    glUniform1i(VertexMode, 1);
    glUniform1i(FragmentMode, 0);

    // secondary VBO
    glGenVertexArrays(1, &VertData.VertexArrayObject);
    glGenBuffers(1, &VertData.VertexBufferObject);
    glBindVertexArray(VertData.VertexArrayObject);
    glBindBuffer(GL_ARRAY_BUFFER, VertData.VertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, Mask->NumberOfVerts*3*sizeof(real32), Mask->TriangulatedPointCache, GL_STREAM_DRAW);

    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    int Scale = glGetUniformLocation(DefaultShaderProgram, "CompDimensions");
    glUniform3f(Scale, Width, Height, 0);


	glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
	glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR_WRAP);

	glDisable(GL_CULL_FACE);

	glDrawArrays(GL_TRIANGLE_FAN, 0, Mask->NumberOfVerts);

	// glEnable(GL_CULL_FACE);

	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

    glBindVertexArray(0);

    // glBindRenderbuffer(GL_RENDERBUFFER, Test.Color_Renderbuffer);

    VertexMode = glGetUniformLocation(DefaultShaderProgram, "VertexMode");
    FragmentMode = glGetUniformLocation(DefaultShaderProgram, "FragmentMode");
    glUniform1i(VertexMode, 0);
    glUniform1i(FragmentMode, 1);

    GL_BindDefaultVertexArrays();

    glStencilFunc(GL_NOTEQUAL, 0, 0xFF);
	glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
    glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_INT, 0);

	glDisable(GL_STENCIL_TEST);
    glStencilMask(0xFF);
    glStencilFunc(GL_ALWAYS, 0, 0xFF);

    glBindFramebuffer(GL_READ_FRAMEBUFFER, Test.FramebufferObject);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, Test2.FramebufferObject);
    glBlitFramebuffer(0, 0, Width, Height, 0, 0, Width, Height,
                      GL_COLOR_BUFFER_BIT, GL_NEAREST);
    glBindFramebuffer(GL_FRAMEBUFFER, Test2.FramebufferObject);

    glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, &Data[0]);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

static uint16
Effect_GL_InitShader(int EffectIndex)
{
    uint16 *ShaderProgram = &GL_EffectShaderProgram[EffectIndex];

    // this is safe to do, riiight
    if (*ShaderProgram) {
        glUseProgram(*ShaderProgram);
        return *ShaderProgram;
    }

    glShaderSource(DefaultVertexShader, 1, &DefaultVertexShaderSource, NULL);
    glCompileShader(DefaultVertexShader);

    int success;
    char infoLog[512];
    glGetShaderiv(DefaultVertexShader, GL_COMPILE_STATUS, &success);

    if(!success)
    {
        glGetShaderInfoLog(DefaultVertexShader, 512, NULL, infoLog);
        printf("Vertex shader fail:\n %s", infoLog);
    }

    uint32 FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(FragmentShader, 1, &FragmentShaderEffectSource[EffectIndex], NULL);
    glCompileShader(FragmentShader);

    glGetShaderiv(FragmentShader, GL_COMPILE_STATUS, &success);

    if(!success)
    {
        glGetShaderInfoLog(FragmentShader, 512, NULL, infoLog);
        printf("Fragment shader fail:\n %s", infoLog);
    }

    *ShaderProgram = glCreateProgram();

    glAttachShader(*ShaderProgram, DefaultVertexShader);
    glAttachShader(*ShaderProgram, FragmentShader);
    glLinkProgram(*ShaderProgram);

    glGetProgramiv(*ShaderProgram, GL_LINK_STATUS, &success);
    if(!success) {
        glGetProgramInfoLog(*ShaderProgram, 512, NULL, infoLog);
        printf("Shader linkage fail:\n %s", infoLog);
    }

    glDeleteShader(FragmentShader);

    glUseProgram(*ShaderProgram);

    return *ShaderProgram;
}
#endif