Repository navigation
Expand file tree
/
Copy pathGLPresenter.cpp
More file actions
706 lines (658 loc) · 24.4 KB
/
Copy pathGLPresenter.cpp
File metadata and controls
706 lines (658 loc) · 24.4 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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Only where the platform has OpenGL: the build defines DEKI_CAPABILITY_OPENGL
// for one whose platform provides "opengl" (the desktop, a Linux handheld) and
// never for a microcontroller, where this file compiles to nothing.
#ifdef DEKI_CAPABILITY_OPENGL
#include "GLPresenter.h"
#include "GLBlitSink.h"
#include "ScreenEffect.h"
#include "deki-rendering/Lighting2D.h"
#include "deki-rendering/PixelFormat.h"
#include "deki-rendering/QuadBlit.h"
#include "deki-rendering/ShaderAsset.h"
#include <deki/LogSystem.h>
#include <algorithm>
#include <cstdio>
#include <chrono>
#include <string>
namespace DekiGL
{
namespace
{
#include "GLLight2DShader.inl"
GLPresenter* s_Active = nullptr;
// One triangle over the whole target, from the vertex id alone: no buffers.
const char* const kVertexShader = R"(
void main()
{
vec2 p = vec2(float((gl_VertexID & 1) << 2) - 1.0, float((gl_VertexID & 2) << 1) - 1.0);
gl_Position = vec4(p, 0.0, 1.0);
}
)";
// A layer's texel for each frame pixel, exactly: the layer's rows run top
// down like the CPU buffer, the frame's bottom up like GL.
const char* const kFragmentShader = R"(
precision highp float;
precision highp int;
uniform highp sampler2D uLayer;
uniform int uHeight;
out vec4 fragColor;
void main()
{
ivec2 p = ivec2(gl_FragCoord.xy);
fragColor = texelFetch(uLayer, ivec2(p.x, uHeight - 1 - p.y), 0);
}
)";
/// One row of `format` as RGBA8, premultiplied. RGB565A8 from the 2D
/// blitter already is (a cleared, transparent buffer blends to colour times
/// coverage); the other formats have no alpha and are opaque. Returns
/// whether the row covers anything.
template <Deki::ColorFormat F>
bool RowToRGBA(const uint8_t* row, int width, uint8_t* out)
{
bool any = false;
for (int x = 0; x < width; ++x, out += 4)
{
DekiPixel::ReadDstPixel<F>(row, static_cast<size_t>(x), out[0], out[1], out[2], out[3]);
any = any || out[3] != 0;
}
return any;
}
} // namespace
GLPresenter* GLPresenter::Active()
{
return s_Active;
}
GLPresenter::GLPresenter(std::shared_ptr<GLContext> context, const Window& window)
: m_Context(std::move(context)), m_Window(window)
{
}
std::unique_ptr<GLPresenter> GLPresenter::Create(std::shared_ptr<GLContext> context, const Window& window)
{
if (!context || !context->MakeCurrent())
{
return nullptr;
}
std::unique_ptr<GLPresenter> presenter(new GLPresenter(std::move(context), window));
if (!presenter->Build())
{
return nullptr;
}
SetWindowContext(presenter->m_Context);
s_Active = presenter.get();
// The 2D renderer's blits come here from now on; without the 2D shader
// they stay on the CPU and arrive as layers, as before.
presenter->m_Blits = std::make_unique<GLBlitSink>(*presenter);
if (!presenter->m_Blits->Build())
{
DEKI_LOG_WARNING("OpenGL: drawing 2D on the CPU: its shader does not build");
presenter->m_Blits->Release();
presenter->m_Blits.reset();
}
QuadBlit::SetBlitSink(presenter->m_Blits.get());
const GL& gl = presenter->m_Context->Functions();
const char* renderer = reinterpret_cast<const char*>(gl.GetString(gl::RENDERER));
const char* version = reinterpret_cast<const char*>(gl.GetString(gl::VERSION));
DEKI_LOG_INFO("OpenGL: presenting through %s, %s", renderer ? renderer : "?", version ? version : "?");
return presenter;
}
GLPresenter::~GLPresenter()
{
if (s_Active == this)
{
s_Active = nullptr;
SetWindowContext(nullptr);
}
if (m_Blits && QuadBlit::GetBlitSink() == m_Blits.get())
{
QuadBlit::SetBlitSink(nullptr);
}
if (m_Context->MakeCurrent())
{
const GL& gl = m_Context->Functions();
if (m_Blits)
{
m_Blits->Release();
}
ReleaseFrame();
gl.DeleteTextures(1, &m_LayerTexture);
gl.DeleteVertexArrays(1, &m_VertexArray);
gl.DeleteProgram(m_Program);
for (auto& entry : m_Effects)
{
gl.DeleteProgram(entry.second.id);
}
}
}
bool GLPresenter::Build()
{
const GL& gl = m_Context->Functions();
const char* version = m_Context->IsES() ? "#version 300 es\n" : "#version 330 core\n";
auto compile = [&](GLenum type, const char* body) -> GLuint
{
const GLuint shader = gl.CreateShader(type);
const GLchar* sources[] = { version, body };
gl.ShaderSource(shader, 2, sources, nullptr);
gl.CompileShader(shader);
GLint ok = 0;
gl.GetShaderiv(shader, gl::COMPILE_STATUS, &ok);
if (!ok)
{
GLint length = 0;
gl.GetShaderiv(shader, gl::INFO_LOG_LENGTH, &length);
std::string log(static_cast<size_t>(std::max(length, 1)), '\0');
gl.GetShaderInfoLog(shader, length, nullptr, log.data());
DEKI_LOG_ERROR("OpenGL: compiling the layer shader failed: %s", log.c_str());
gl.DeleteShader(shader);
return 0;
}
return shader;
};
const GLuint vs = compile(gl::VERTEX_SHADER, kVertexShader);
const GLuint fs = compile(gl::FRAGMENT_SHADER, kFragmentShader);
if (!vs || !fs)
{
return false;
}
m_Program = gl.CreateProgram();
gl.AttachShader(m_Program, vs);
gl.AttachShader(m_Program, fs);
gl.LinkProgram(m_Program);
gl.DeleteShader(vs);
gl.DeleteShader(fs);
GLint linked = 0;
gl.GetProgramiv(m_Program, gl::LINK_STATUS, &linked);
if (!linked)
{
DEKI_LOG_ERROR("OpenGL: linking the layer shader failed");
return false;
}
m_LayerUniform = gl.GetUniformLocation(m_Program, "uLayer");
m_HeightUniform = gl.GetUniformLocation(m_Program, "uHeight");
// Core profiles draw nothing without a vertex array bound, even one that
// holds no attributes.
gl.GenVertexArrays(1, &m_VertexArray);
gl.GenTextures(1, &m_LayerTexture);
return gl.GetError() == 0;
}
void GLPresenter::ReleaseFrame()
{
const GL& gl = m_Context->Functions();
if (m_Framebuffer)
{
gl.DeleteFramebuffers(1, &m_Framebuffer);
}
gl.DeleteTextures(1, &m_ColorTexture);
gl.DeleteRenderbuffers(1, &m_DepthBuffer);
m_Framebuffer = m_ColorTexture = m_DepthBuffer = 0;
m_Width = m_Height = 0;
}
bool GLPresenter::EnsureFrame(int width, int height)
{
if (m_Framebuffer && width == m_Width && height == m_Height)
{
return true;
}
const GL& gl = m_Context->Functions();
ReleaseFrame();
gl.GenFramebuffers(1, &m_Framebuffer);
gl.GenTextures(1, &m_ColorTexture);
gl.GenRenderbuffers(1, &m_DepthBuffer);
gl.BindTexture(gl::TEXTURE_2D, m_ColorTexture);
gl.TexImage2D(gl::TEXTURE_2D, 0, static_cast<GLint>(gl::RGBA8), width, height, 0, gl::RGBA, gl::UNSIGNED_BYTE,
nullptr);
// Nearest: an effect samples the frame's pixels as they are, sharp.
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, static_cast<GLint>(gl::NEAREST));
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, static_cast<GLint>(gl::NEAREST));
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, static_cast<GLint>(gl::CLAMP_TO_EDGE));
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, static_cast<GLint>(gl::CLAMP_TO_EDGE));
gl.BindRenderbuffer(gl::RENDERBUFFER, m_DepthBuffer);
gl.RenderbufferStorage(gl::RENDERBUFFER, gl::DEPTH_COMPONENT24, width, height);
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.FramebufferTexture2D(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, gl::TEXTURE_2D, m_ColorTexture, 0);
gl.FramebufferRenderbuffer(gl::FRAMEBUFFER, gl::DEPTH_ATTACHMENT, gl::RENDERBUFFER, m_DepthBuffer);
if (gl.CheckFramebufferStatus(gl::FRAMEBUFFER) != gl::FRAMEBUFFER_COMPLETE)
{
DEKI_LOG_ERROR("OpenGL: no %dx%d frame to build on", width, height);
ReleaseFrame();
return false;
}
gl.BindTexture(gl::TEXTURE_2D, m_LayerTexture);
gl.TexImage2D(gl::TEXTURE_2D, 0, static_cast<GLint>(gl::RGBA8), width, height, 0, gl::RGBA, gl::UNSIGNED_BYTE,
nullptr);
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, static_cast<GLint>(gl::NEAREST));
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, static_cast<GLint>(gl::NEAREST));
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, static_cast<GLint>(gl::CLAMP_TO_EDGE));
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, static_cast<GLint>(gl::CLAMP_TO_EDGE));
m_Width = width;
m_Height = height;
return true;
}
void GLPresenter::StartFrame(int width, int height)
{
if (m_InFrame || !EnsureFrame(width, height))
{
return;
}
const GL& gl = m_Context->Functions();
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.Viewport(0, 0, width, height);
gl.Disable(gl::SCISSOR_TEST);
gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.SetClearDepth(1.0f);
gl.DepthMask(gl::kTrue);
gl.Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
m_InFrame = true;
}
void GLPresenter::ClearFrame(int width, int height, uint8_t r, uint8_t g, uint8_t b)
{
StartFrame(width, height);
if (!m_InFrame)
{
return;
}
const GL& gl = m_Context->Functions();
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.Viewport(0, 0, m_Width, m_Height);
gl.Disable(gl::SCISSOR_TEST);
gl.ClearColor(static_cast<float>(r) / 255.0f, static_cast<float>(g) / 255.0f, static_cast<float>(b) / 255.0f, 1.0f);
gl.Clear(gl::COLOR_BUFFER_BIT);
}
void GLPresenter::Flush2D()
{
if (m_Blits)
{
m_Blits->Flush();
}
}
void GLPresenter::SetDraws2D(bool draws)
{
if (m_Blits)
{
m_Blits->SetEnabled(draws);
}
}
bool GLPresenter::Draws2D() const
{
return m_Blits && m_Blits->Enabled();
}
GLPresenter::DrawStats GLPresenter::Stats2D() const
{
return m_Blits ? m_Blits->Stats() : DrawStats{};
}
bool GLPresenter::TakesLayers(int width, int height, Deki::ColorFormat format) const
{
return format == Deki::ColorFormat::RGB565A8 && (!m_InFrame || (width == m_Width && height == m_Height));
}
void GLPresenter::BindFrame(int width, int height)
{
if (!m_Context->MakeCurrent())
{
return;
}
Flush2D();
StartFrame(width, height);
const GL& gl = m_Context->Functions();
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.Viewport(0, 0, m_Width, m_Height);
}
void GLPresenter::PushLayer(const uint8_t* buffer, int width, int height, Deki::ColorFormat format)
{
if (!buffer || width <= 0 || height <= 0 || !m_Context->MakeCurrent())
{
return;
}
StartFrame(width, height);
if (!m_InFrame || width != m_Width || height != m_Height)
{
return;
}
// The 2D queued so far is below this layer. If the layer is the 2D's own
// buffer, what was written into it is in the frame after this.
Flush2D();
if (m_Blits)
{
m_Blits->LayerPushed(buffer);
}
// To RGBA8, noting the rows that hold anything: a layer of interface over
// 3D is mostly transparent, and only its covered rows go up and draw.
m_Staging.resize(static_cast<size_t>(width) * height * 4);
const size_t pitch = Deki::FrameBufferBytes(format, width, 1);
int first = height, last = -1;
for (int y = 0; y < height; ++y)
{
const uint8_t* row = buffer + static_cast<size_t>(y) * pitch;
uint8_t* out = &m_Staging[static_cast<size_t>(y) * width * 4];
bool any = false;
switch (format)
{
case Deki::ColorFormat::RGB565: any = RowToRGBA<Deki::ColorFormat::RGB565>(row, width, out); break;
case Deki::ColorFormat::RGB888: any = RowToRGBA<Deki::ColorFormat::RGB888>(row, width, out); break;
case Deki::ColorFormat::ARGB8888: any = RowToRGBA<Deki::ColorFormat::ARGB8888>(row, width, out); break;
case Deki::ColorFormat::RGB565A8: any = RowToRGBA<Deki::ColorFormat::RGB565A8>(row, width, out); break;
}
if (any)
{
first = std::min(first, y);
last = y;
}
}
if (last < 0)
{
return; // all transparent
}
const GL& gl = m_Context->Functions();
const int rows = last - first + 1;
gl.ActiveTexture(gl::TEXTURE0);
gl.BindTexture(gl::TEXTURE_2D, m_LayerTexture);
gl.PixelStorei(gl::UNPACK_ALIGNMENT, 4);
gl.TexSubImage2D(gl::TEXTURE_2D, 0, 0, first, width, rows, gl::RGBA, gl::UNSIGNED_BYTE,
&m_Staging[static_cast<size_t>(first) * width * 4]);
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.Viewport(0, 0, width, height);
gl.Disable(gl::DEPTH_TEST);
gl.Disable(gl::CULL_FACE);
gl.Enable(gl::BLEND);
gl.BlendFunc(gl::ONE, gl::ONE_MINUS_SRC_ALPHA);
// Only the covered rows; GL counts them from the bottom.
gl.Enable(gl::SCISSOR_TEST);
gl.Scissor(0, height - 1 - last, width, rows);
gl.UseProgram(m_Program);
gl.BindVertexArray(m_VertexArray);
gl.Uniform1i(m_LayerUniform, 0);
gl.Uniform1i(m_HeightUniform, height);
gl.DrawArrays(gl::TRIANGLES, 0, 3);
gl.Disable(gl::SCISSOR_TEST);
gl.Disable(gl::BLEND);
const GLenum error = gl.GetError();
if (error != 0 && !m_ErrorReported)
{
m_ErrorReported = true;
DEKI_LOG_WARNING("OpenGL: OpenGL error 0x%04X composing a layer", error);
}
}
void GLPresenter::Present(const uint8_t* buffer, int width, int height, Deki::ColorFormat format)
{
Effect effect;
if (const ScreenEffect* scene = ScreenEffect::Current())
{
effect.shader = scene->shader.Get();
effect.params = scene->params;
effect.color = scene->color.ToABGR8888(); // 0xAABBGGRR, as GLPresenter::Effect wants
}
Present(buffer, width, height, format, effect);
}
void GLPresenter::Present(const uint8_t* buffer, int width, int height, Deki::ColorFormat format,
const Effect& effect)
{
if (!m_Context->MakeCurrent())
{
return;
}
PushLayer(buffer, width, height, format);
if (!m_InFrame)
{
return;
}
const GL& gl = m_Context->Functions();
int windowW = 0, windowH = 0;
m_Window.drawableSize(m_Window.user, &windowW, &windowH);
// Whole pixels where the window allows, centred; a window smaller than
// the screen gets the largest fit that keeps the shape.
int scale = std::min(windowW / m_Width, windowH / m_Height);
int dstW = m_Width * scale, dstH = m_Height * scale;
if (scale < 1)
{
const float fit = std::min(static_cast<float>(windowW) / m_Width, static_cast<float>(windowH) / m_Height);
dstW = static_cast<int>(m_Width * fit);
dstH = static_cast<int>(m_Height * fit);
}
const int dstX = (windowW - dstW) / 2;
const int dstY = (windowH - dstH) / 2;
gl.BindFramebuffer(gl::DRAW_FRAMEBUFFER, m_Window.target);
gl.Viewport(0, 0, windowW, windowH);
gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.Clear(gl::COLOR_BUFFER_BIT);
if (!effect.shader || !DrawEffect(effect, dstX, dstY, dstW, dstH))
{
gl.BindFramebuffer(gl::READ_FRAMEBUFFER, m_Framebuffer);
gl.BlitFramebuffer(0, 0, m_Width, m_Height, dstX, dstY, dstX + dstW, dstY + dstH, gl::COLOR_BUFFER_BIT,
gl::NEAREST);
}
// The frame's contents are spent. Saying so lets a tiled GPU (Mali)
// skip writing them back to memory.
if (gl.InvalidateFramebuffer)
{
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
const GLenum spent[2] = { gl::COLOR_ATTACHMENT0, gl::DEPTH_ATTACHMENT };
gl.InvalidateFramebuffer(gl::FRAMEBUFFER, 2, spent);
}
gl.BindFramebuffer(gl::FRAMEBUFFER, 0);
m_Window.swap(m_Window.user);
m_InFrame = false;
}
GLuint GLPresenter::Compile(GLenum type, const std::string& source, const char* what)
{
const GL& gl = m_Context->Functions();
const GLuint shader = gl.CreateShader(type);
const GLchar* sources[] = { source.c_str() };
gl.ShaderSource(shader, 1, sources, nullptr);
gl.CompileShader(shader);
GLint ok = 0;
gl.GetShaderiv(shader, gl::COMPILE_STATUS, &ok);
if (!ok)
{
GLint length = 0;
gl.GetShaderiv(shader, gl::INFO_LOG_LENGTH, &length);
std::string log(static_cast<size_t>(std::max(length, 1)), '\0');
gl.GetShaderInfoLog(shader, length, nullptr, log.data());
DEKI_LOG_ERROR("OpenGL: %s does not build: %s", what, log.c_str());
gl.DeleteShader(shader);
return 0;
}
return shader;
}
bool GLPresenter::BuildLight2D()
{
m_Light2D.tried = true;
const GL& gl = m_Context->Functions();
const bool es = m_Context->IsES();
const GLuint vs = Compile(gl::VERTEX_SHADER, es ? kLight2DVertexES : kLight2DVertexGL, "the 2D light's shader");
const GLuint fs =
vs ? Compile(gl::FRAGMENT_SHADER, es ? kLight2DFragmentES : kLight2DFragmentGL, "the 2D light's shader") : 0;
if (!vs || !fs)
{
gl.DeleteShader(vs);
return false;
}
m_Light2D.id = gl.CreateProgram();
gl.AttachShader(m_Light2D.id, vs);
gl.AttachShader(m_Light2D.id, fs);
gl.LinkProgram(m_Light2D.id);
gl.DeleteShader(vs);
gl.DeleteShader(fs);
GLint linked = 0;
gl.GetProgramiv(m_Light2D.id, gl::LINK_STATUS, &linked);
if (!linked)
{
DEKI_LOG_ERROR("OpenGL: the 2D light's shader does not link");
gl.DeleteProgram(m_Light2D.id);
m_Light2D.id = 0;
return false;
}
// In the struct `u` the compiler keeps uniforms in, an array as a struct
// of its own whose `data` holds the elements (ShaderCompiler.h).
char name[32];
for (int i = 0; i < kMaxLights2D; i++)
{
std::snprintf(name, sizeof(name), "u.uLightA.data[%d]", i);
m_Light2D.a[i] = gl.GetUniformLocation(m_Light2D.id, name);
std::snprintf(name, sizeof(name), "u.uLightB.data[%d]", i);
m_Light2D.b[i] = gl.GetUniformLocation(m_Light2D.id, name);
std::snprintf(name, sizeof(name), "u.uLightC.data[%d]", i);
m_Light2D.c[i] = gl.GetUniformLocation(m_Light2D.id, name);
}
m_Light2D.count = gl.GetUniformLocation(m_Light2D.id, "u.uCount");
m_Light2D.ambient = gl.GetUniformLocation(m_Light2D.id, "u.uAmbient");
m_Light2D.height = gl.GetUniformLocation(m_Light2D.id, "u.uHeight");
return true;
}
bool GLPresenter::DrawLight2D(const DekiRendering::Lighting2D& lighting)
{
if (!m_InFrame || !m_Context->MakeCurrent())
{
return false;
}
if (!m_Light2D.tried)
{
BuildLight2D();
}
if (!m_Light2D.id)
{
return false;
}
// What is queued lies under the light.
Flush2D();
const GL& gl = m_Context->Functions();
const auto& lights = lighting.Lights();
const int count = static_cast<int>(std::min<size_t>(lights.size(), kMaxLights2D));
if (lights.size() > static_cast<size_t>(kMaxLights2D) && !m_TooManyLightsReported)
{
m_TooManyLightsReported = true;
DEKI_LOG_WARNING("Lights: the frame has %d point and spot lights; the GPU draws the first %d",
static_cast<int>(lights.size()), kMaxLights2D);
}
gl.UseProgram(m_Light2D.id);
for (int i = 0; i < count; i++)
{
const DekiRendering::Light2D& l = lights[static_cast<size_t>(i)];
gl.Uniform4f(m_Light2D.a[i], l.x, l.y, l.radius, static_cast<float>(l.bands));
gl.Uniform4f(m_Light2D.b[i], l.r, l.g, l.b, l.spot ? 1.0f : 0.0f);
gl.Uniform4f(m_Light2D.c[i], l.dirX, l.dirY, l.cosOuter, l.cosInner);
}
float ar, ag, ab;
lighting.Ambient(ar, ag, ab);
gl.Uniform1i(m_Light2D.count, count);
gl.Uniform3f(m_Light2D.ambient, ar, ag, ab);
gl.Uniform1f(m_Light2D.height, static_cast<float>(m_Height));
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.Viewport(0, 0, m_Width, m_Height);
gl.Disable(gl::DEPTH_TEST);
gl.Disable(gl::CULL_FACE);
gl.Enable(gl::BLEND);
// Frame x 2 x (light / 2): the frame times the light, up to twice it.
gl.BlendFunc(gl::DST_COLOR, gl::SRC_COLOR);
gl.BindVertexArray(m_VertexArray);
gl.DrawArrays(gl::TRIANGLES, 0, 3);
gl.Disable(gl::BLEND);
const GLenum error = gl.GetError();
if (error != 0 && !m_ErrorReported)
{
m_ErrorReported = true;
DEKI_LOG_WARNING("OpenGL: OpenGL error 0x%04X lighting the frame", error);
}
return true;
}
GLPresenter::EffectProgram GLPresenter::LinkEffect(const DekiRendering::ShaderAsset& shader)
{
const GL& gl = m_Context->Functions();
// The asset's "effect" program, compiled from HLSL by the editor
// (editor/EffectShaderUse.cpp): both stages, for this context's GL.
const DekiRendering::ShaderProgramSource* source = shader.Program("effect", m_Context->IsES());
EffectProgram program;
const GLuint vs = Compile(gl::VERTEX_SHADER, source->vertex, "a screen effect's shader");
const GLuint fs = vs ? Compile(gl::FRAGMENT_SHADER, source->fragment, "a screen effect's shader") : 0;
if (!vs || !fs)
{
gl.DeleteShader(vs);
DEKI_LOG_WARNING("OpenGL: showing the frame without its screen effect");
return program;
}
program.id = gl.CreateProgram();
gl.AttachShader(program.id, vs);
gl.AttachShader(program.id, fs);
gl.LinkProgram(program.id);
gl.DeleteShader(vs);
gl.DeleteShader(fs);
GLint linked = 0;
gl.GetProgramiv(program.id, gl::LINK_STATUS, &linked);
if (!linked)
{
DEKI_LOG_ERROR("OpenGL: a screen effect's shader does not link; showing the frame without it");
gl.DeleteProgram(program.id);
program.id = 0;
return program;
}
// The compiler keeps the uniforms in one struct, `u`, and names a texture
// as the shader does (deki-editor's ShaderCompiler.h).
program.frame = gl.GetUniformLocation(program.id, "uFrame");
program.dst = gl.GetUniformLocation(program.id, "u.uDst");
program.resolution = gl.GetUniformLocation(program.id, "u.uResolution");
program.time = gl.GetUniformLocation(program.id, "u.uTime");
program.params = gl.GetUniformLocation(program.id, "u.uParams");
program.color = gl.GetUniformLocation(program.id, "u.uColor");
return program;
}
bool GLPresenter::DrawEffect(const Effect& effect, int x, int y, int width, int height)
{
const DekiRendering::ShaderAsset* shader = effect.shader;
if (!shader || !shader->Program("effect", m_Context->IsES()))
{
return false;
}
auto [it, fresh] = m_Effects.try_emplace(shader->Revision());
if (fresh)
{
it->second = LinkEffect(*shader);
}
const EffectProgram& program = it->second;
if (!program.id)
{
return false;
}
// The effect's clock, from its first frame.
static const auto s_Start = std::chrono::steady_clock::now();
const float time = std::chrono::duration<float>(std::chrono::steady_clock::now() - s_Start).count();
const uint32_t c = effect.color; // 0xAABBGGRR
const GL& gl = m_Context->Functions();
gl.BindFramebuffer(gl::DRAW_FRAMEBUFFER, m_Window.target);
gl.Viewport(x, y, width, height);
gl.Disable(gl::BLEND);
gl.Disable(gl::DEPTH_TEST);
gl.Disable(gl::CULL_FACE);
gl.UseProgram(program.id);
gl.BindVertexArray(m_VertexArray);
gl.ActiveTexture(gl::TEXTURE0);
gl.BindTexture(gl::TEXTURE_2D, m_ColorTexture);
gl.Uniform1i(program.frame, 0);
gl.Uniform4f(program.dst, static_cast<float>(x), static_cast<float>(y), static_cast<float>(width),
static_cast<float>(height));
gl.Uniform2f(program.resolution, static_cast<float>(m_Width), static_cast<float>(m_Height));
gl.Uniform1f(program.time, time);
gl.Uniform3f(program.params, effect.params.x, effect.params.y, effect.params.z);
gl.Uniform4f(program.color, static_cast<float>(c & 0xFF) / 255.0f, static_cast<float>((c >> 8) & 0xFF) / 255.0f,
static_cast<float>((c >> 16) & 0xFF) / 255.0f, static_cast<float>((c >> 24) & 0xFF) / 255.0f);
gl.DrawArrays(gl::TRIANGLES, 0, 3);
return true;
}
bool GLPresenter::ReadFrame(std::vector<uint8_t>& rgba)
{
if (!m_InFrame || !m_Context->MakeCurrent())
{
return false;
}
Flush2D();
const GL& gl = m_Context->Functions();
const size_t row = static_cast<size_t>(m_Width) * 4;
std::vector<uint8_t> bottomUp(row * m_Height);
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Framebuffer);
gl.PixelStorei(gl::PACK_ALIGNMENT, 4);
gl.ReadPixels(0, 0, m_Width, m_Height, gl::RGBA, gl::UNSIGNED_BYTE, bottomUp.data());
rgba.resize(bottomUp.size());
for (int y = 0; y < m_Height; ++y)
{
std::copy_n(&bottomUp[(m_Height - 1 - y) * row], row, &rgba[y * row]);
}
return gl.GetError() == 0;
}
} // namespace DekiGL
#endif // DEKI_CAPABILITY_OPENGL