-
Notifications
You must be signed in to change notification settings - Fork 6
/
Shaders.h
executable file
·559 lines (446 loc) · 17 KB
/
Shaders.h
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// Shaders.h - some basic GLSL shaders
//
// Copyright (c) 2013-2016 Arthur Danskin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef SHADERS_H
#define SHADERS_H
#include "Graphics.h"
struct ShaderPosBase : public ShaderProgramBase {
virtual void UseProgram(const ShaderState& ss, const float2* ptr) const = 0;
virtual ~ShaderPosBase() {}
// a b
// d c
void DrawQuad(const ShaderState& ss, float2 a, float2 b, float2 c, float2 d) const;
void DrawSquare(const ShaderState &ss, float r) const
{
DrawQuad(ss, float2(-r, -r), float2(-r, r), float2(r, r), float2(r, -r));
}
void DrawRect(const ShaderState &ss, float2 r) const
{
DrawQuad(ss, float2(-r.x, -r.y), float2(-r.x, r.y), r, float2(r.x, -r.y));
}
void DrawRect(const ShaderState &ss, float2 pos, float2 r) const
{
DrawQuad(ss, pos + float2(-r.x, -r.y), pos + float2(-r.x, r.y),
pos + r, pos + float2(r.x, -r.y));
}
void DrawColorRect(ShaderState& ss, uint color, float2 r) const
{
if (color&ALPHA_OPAQUE) {
ss.color32(color);
DrawRect(ss, r);
}
}
void DrawRectCorners(const ShaderState &ss, float2 a, float2 b) const
{
float2 bl(min(a.x, b.x), min(a.y, b.y));
float2 ur(max(a.x, b.x), max(a.y, b.y));
DrawQuad(ss, float2(bl.x, ur.y), ur, float2(ur.x, bl.y), bl);
}
void DrawLineRect(const ShaderState &ss, float2 pos, float2 rad) const
{
DrawLineRectCorners(ss, pos - rad, pos + rad);
}
// a b
// c d
void DrawLineQuad(const ShaderState& ss, float2 a, float2 b, float2 c, float2 d,
bool outline=true, bool cross=false) const
{
const float2 v[] = { a, b, c, d };
static const ushort i[] = {0,1, 1,3, 3,2, 2,0, 0,3, 1,2};
UseProgram(ss, v);
ss.DrawElements(GL_LINES, (outline ? 8 : 0) + (cross ? 4 : 0), outline ? i : (i + 8));
UnuseProgram();
}
void DrawLineSquare(const ShaderState& ss, float r) const
{
DrawLineQuad(ss, float2(-r, -r), float2(-r, r), float2(r, -r), float2(r, r));
}
void DrawLineRect(const ShaderState& ss, float2 r) const
{
DrawLineQuad(ss, float2(-r.x, -r.y), float2(-r.x, r.y), float2(r.x, -r.y), r);
}
void DrawLineRectCorners(const ShaderState& ss, float2 a, float2 b) const
{
float2 bl(min(a.x, b.x), min(a.y, b.y));
float2 ur(max(a.x, b.x), max(a.y, b.y));
DrawLineQuad(ss, float2(bl.x, ur.y), ur, bl, float2(ur.x, bl.y));
}
void DrawLine(const ShaderState& ss, float2 a, float2 b) const
{
float2 v[] = { a, b };
UseProgram(ss, v);
ss.DrawArrays(GL_LINES, 2);
UnuseProgram();
}
void DrawLineTri(const ShaderState& ss, float2 a, float2 b, float2 c) const
{
float2 v[] = { a, b, c };
static const ushort i[] = {0, 1, 1, 2, 2, 0};
UseProgram(ss, v);
ss.DrawElements(GL_LINES, 6, i);
UnuseProgram();
}
void DrawTri(const ShaderState& ss, float2 a, float2 b, float2 c) const
{
float2 v[] = { a, b, c };
UseProgram(ss, v);
ss.DrawArrays(GL_TRIANGLES, 3);
UnuseProgram();
}
void DrawGrid(const ShaderState &ss, double width, double3 first, double3 last) const;
void DrawLineCircle(const ShaderState& ss, float radius, uint numVerts=32) const;
void DrawCircle(const ShaderState& ss, float radius, uint numVerts=32) const;
};
struct ShaderUColor final : public ShaderPosBase, public ShaderBase<ShaderUColor> {
uint m_colorSlot;
void LoadTheProgram()
{
LoadProgram("ShaderUColor",
"varying vec4 DestinationColor;\n"
,
"uniform vec4 SourceColor;\n"
"void main(void) {\n"
" DestinationColor = SourceColor;\n"
" gl_Position = Transform * Position;\n"
"}\n"
,
"void main(void) {\n"
" gl_FragColor = DestinationColor;\n"
"}\n"
);
m_colorSlot = getUniformLocation("SourceColor");
}
void UseProgram(const ShaderState& ss, const float2* ptr) const
{
UseProgramBase(ss, ptr, (float2*)NULL);
uniformColor(m_colorSlot, ss.uColor);
glReportError();
}
void UseProgram(const ShaderState& ss, const VertexPos* ptr, const VertexPos* base) const
{
UseProgramBase(ss, &ptr->pos, base);
uniformColor(m_colorSlot, ss.uColor);
glReportError();
}
};
struct ShaderIridescent final : public ShaderProgramBase, public ShaderBase<ShaderIridescent> {
int SourceColor0;
int SourceColor1;
int TimeA;
void LoadTheProgram()
{
LoadProgram("ShaderIridescent");
GET_ATTR_LOC(SourceColor0);
GET_ATTR_LOC(SourceColor1);
GET_ATTR_LOC(TimeA);
}
void UseProgram(const ShaderState& input, const VertexPos2ColorTime* ptr, const VertexPos2ColorTime* base) const
{
UseProgramBase(input, &ptr->pos, base);
vertexAttribPointer(SourceColor0, &ptr->color, base);
vertexAttribPointer(SourceColor1, &ptr->color1, base);
vertexAttribPointer(TimeA, &ptr->time, base);
}
};
struct ShaderResource : public ShaderProgramBase, public ShaderBase<ShaderResource> {
int SourceColor0, SourceColor1, Radius;
void LoadTheProgram()
{
LoadProgram("ShaderResource");
GET_ATTR_LOC(SourceColor0);
GET_ATTR_LOC(SourceColor1);
GET_ATTR_LOC(Radius);
}
void UseProgram(const ShaderState& input, const VertexPos2ColorTime* ptr, const VertexPos2ColorTime* base) const
{
UseProgramBase(input, &ptr->pos, base);
vertexAttribPointer(SourceColor0, &ptr->color, base);
vertexAttribPointer(SourceColor1, &ptr->color1, base);
vertexAttribPointer(Radius, &ptr->time, base);
}
};
struct ShaderWormhole : public ShaderProgramBase, public ShaderBase<ShaderWormhole> {
int TexCoord;
void LoadTheProgram()
{
LoadProgram("ShaderWormhole");
GET_ATTR_LOC(TexCoord);
}
void UseProgram(const ShaderState& input, const VertexPos2ColorTex* ptr, const VertexPos2ColorTex* base) const
{
UseProgramBase(input, &ptr->pos, base);
vertexAttribPointer(TexCoord, &ptr->tex, base);
}
};
struct ShaderTextureBase : public ShaderProgramBase {
void bindTextureDrawElements(const ShaderState &ss, const GLTexture &texture,
VertexPosTex *vtx, int icount, const uint *idxs) const;
virtual void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture& ot) const = 0;
virtual void UseProgram(const ShaderState &ss, const VertexPosTex *ptr) const { }
// a b
// d c
void DrawQuad(const ShaderState& ss, const GLTexture& texture,
float2 a, float2 b, float2 c, float2 d) const;
void DrawQuad(const ShaderState& ss, float2 a, float2 b, float2 c, float2 d) const;
void DrawRectCorners(const ShaderState &ss, const GLTexture& texture, float2 a, float2 b) const
{
float2 bl(min(a.x, b.x), min(a.y, b.y));
float2 ur(max(a.x, b.x), max(a.y, b.y));
DrawQuad(ss, texture, float2(bl.x, ur.y), ur, float2(ur.x, bl.y), bl);
}
void DrawRect(const ShaderState &ss, const GLTexture& texture, float2 pos, float2 rad) const
{
DrawRectCorners(ss, texture, pos - rad, pos + rad);
}
void DrawRect(const ShaderState &ss, float2 pos, float2 rad) const
{
DrawQuad(ss, pos + f2(-rad.x, rad.y), pos + rad, pos + f2(rad.x, -rad.y), pos - rad);
}
void DrawRectScale(const ShaderState &ss, const GLTexture& texture,
float2 scale, float2 pos, float2 rad) const;
void DrawRectWidth(const ShaderState &ss, const GLTexture& texture, float2 pos, float width) const
{
float2 rad(width, width * texture.size().y / texture.size().x);
DrawRect(ss, texture, pos, rad);
}
void DrawButton(const ShaderState &ss, const GLTexture& texture, float2 pos, float2 r) const;
};
struct ShaderTexture1 : public ShaderTextureBase {
uint m_uTexture;
uint m_uColorSlot;
uint m_aTexCoords;
void onLoad()
{
m_uTexture = getUniformLocation("texture1");
m_uColorSlot = getUniformLocation("SourceColor");
m_aTexCoords = getAttribLocation("SourceTexCoord");
}
void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture &ot) const override
{
UseProgram(ss, ptr, (const VertexPosTex*) NULL);
}
template <typename Vtx>
void UseProgram(const ShaderState &ss, const Vtx *ptr, const Vtx *base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(m_aTexCoords, &ptr->tex, base);
glUniform1i(m_uTexture, 0);
uniformColor(m_uColorSlot, ss.uColor);
}
};
struct ShaderTexture final : public ShaderTexture1, public ShaderBase<ShaderTexture>
{
void LoadTheProgram() override
{
LoadProgram("ShaderTexture");
onLoad();
}
};
struct ShaderText final : public ShaderTexture1, public ShaderBase<ShaderText>
{
void LoadTheProgram() override
{
LoadProgram("ShaderText",
"varying vec2 DestTexCoord;\n"
"varying vec4 DestColor;\n"
,
"attribute vec2 SourceTexCoord;\n"
"uniform vec4 SourceColor;\n"
"void main(void) {\n"
" DestTexCoord = SourceTexCoord;\n"
" DestColor = SourceColor;\n"
" gl_Position = Transform * Position;\n"
"}"
,
"uniform sampler2D texture1;\n"
"void main(void) {\n"
" vec2 texCoord = DestTexCoord;\n"
" gl_FragColor = DestColor * texture2D(texture1, texCoord);\n"
"}");
onLoad();
}
};
struct ShaderTextureWarp final : public ShaderTextureBase, public ShaderBase<ShaderTextureWarp> {
GLuint texture1, warpTex, SourceColor, SourceTexCoord, camWorldPos, camWorldSize;
mutable float2 camPos;
mutable float2 camSize;
void LoadTheProgram() override
{
LoadProgram("ShaderTextureWarp");
GET_UNIF_LOC(texture1);
GET_UNIF_LOC(warpTex);
GET_UNIF_LOC(SourceColor);
GET_UNIF_LOC(camWorldPos);
GET_UNIF_LOC(camWorldSize);
GET_ATTR_LOC(SourceTexCoord);
}
void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture &ot) const override
{
UseProgram(ss, ptr, (const VertexPosTex*) NULL);
}
template <typename Vtx>
void UseProgram(const ShaderState &ss, const Vtx *ptr, const Vtx *base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(SourceTexCoord, &ptr->tex, base);
glUniform1i(texture1, 0);
glUniform1i(warpTex, 1);
glUniform2fv(camWorldPos, 1, &camPos[0]);
glUniform2fv(camWorldSize, 1, &camSize[0]);
uniformColor(SourceColor, ss.uColor);
}
};
struct ShaderTextureHSV final : public ShaderTextureBase, public ShaderBase<ShaderTextureHSV> {
uint m_uTexture;
uint m_uColorSlot;
uint m_aTexCoords;
void LoadTheProgram() override
{
LoadProgram("ShaderTextureHSV");
m_uTexture = getUniformLocation("texture1");
m_uColorSlot = getUniformLocation("SourceColor");
m_aTexCoords = getAttribLocation("SourceTexCoord");
}
void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture &ot) const override
{
UseProgram(ss, ptr, (const VertexPosTex*) NULL);
}
template <typename Vtx>
void UseProgram(const ShaderState &ss, const Vtx *ptr, const Vtx *base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(m_aTexCoords, &ptr->tex, base);
glUniform1i(m_uTexture, 0);
uniformColor(m_uColorSlot, ss.uColor);
}
};
struct ShaderTonemapDither final : public ShaderTextureBase, public ShaderBase<ShaderTonemapDither> {
uint texture1;
uint dithertex;
uint SourceTexCoord;
void LoadTheProgram();
void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture &ot) const;
};
struct ShaderTonemap final : public ShaderTextureBase, public ShaderBase<ShaderTonemap> {
uint texture1;
uint SourceTexCoord;
void LoadTheProgram();
void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture &ot) const;
};
// seperable gaussian blur
struct ShaderBlur final : public ShaderTextureBase {
private:
uint usourceTex;
uint asourceTexCoord;
uint uoffsets;
mutable int dimension = 0;
mutable vector<float2> offsets;
int samples = 0;
int scale = 0;
float getBlurOffset(int sample) const;
void LoadTheProgram() { LoadShader(samples, scale); }
public:
void LoadShader(int smpls, int scl);
void UseProgram(const ShaderState &ss, const VertexPosTex *ptr, const GLTexture &ot) const;
void setDimension(uint dim) const
{
dimension = dim;
}
// radius is points of blur radius (not pixels)
static const ShaderBlur &instance(int radius);
};
struct ShaderColor final : public ShaderProgramBase, public ShaderBase<ShaderColor> {
int m_colorSlot;
void LoadTheProgram()
{
LoadProgram("ShaderColor",
//"#extension GL_EXT_gpu_shader4 : require\n"
//"flat varying vec4 DestinationColor;\n"
"varying vec4 DestinationColor;\n"
,
"attribute vec4 SourceColor;\n"
"void main(void) {\n"
" DestinationColor = SourceColor;\n"
" gl_Position = Transform * Position;\n"
"}\n"
,
"void main(void) {\n"
" gl_FragColor = DestinationColor;\n"
"}\n"
);
m_colorSlot = getAttribLocation("SourceColor");
}
template <typename Vtx>
void UseProgram(const ShaderState&ss, const Vtx* ptr, const Vtx* base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(m_colorSlot, &ptr->color, base);
}
};
template <typename T, typename L>
void MeshPair<T, L>::Handle::Draw(const ShaderState& ss)
{
mp.Draw(ss, ShaderColor::instance(), ShaderColor::instance());
}
struct ShaderColorDither final : public ShaderProgramBase, public ShaderBase<ShaderColorDither> {
int SourceColor, dithertex;
void LoadTheProgram()
{
LoadProgram("ShaderColorDither");
GET_ATTR_LOC(SourceColor);
GET_UNIF_LOC(dithertex);
}
template <typename Vtx>
void UseProgram(const ShaderState&ss, const Vtx* ptr, const Vtx* base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(SourceColor, &ptr->color, base);
getDitherTex().BindTexture(0);
glUniform1i(dithertex, 0);
}
};
struct ShaderHsv final : public ShaderProgramBase, public ShaderBase<ShaderHsv> {
int m_colorSlot;
void LoadTheProgram()
{
LoadProgram("ShaderHsv");
m_colorSlot = getAttribLocation("ColorHSVA");
}
template <typename Vtx>
void UseProgram(const ShaderState&ss, const Vtx* ptr, const Vtx* base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(m_colorSlot, &ptr->color, base);
}
};
struct ShaderColorLuma final : public ShaderProgramBase, public ShaderBase<ShaderColorLuma> {
int SourceColor, Luma;
void LoadTheProgram();
template <typename Vtx>
void UseProgram(const ShaderState&ss, const Vtx* ptr, const Vtx* base) const
{
UseProgramBase(ss, &ptr->pos, base);
vertexAttribPointer(SourceColor, &ptr->color, base);
vertexAttribPointer(Luma, &ptr->luma, base);
}
};
#endif // SHADERS_H