Repository navigation
Expand file tree
/
Copy pathGLBlitSink.cpp
More file actions
840 lines (786 loc) · 26.1 KB
/
Copy pathGLBlitSink.cpp
File metadata and controls
840 lines (786 loc) · 26.1 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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// 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 "GLBlitSink.h"
#include "deki-rendering/PixelFormat.h"
#include <deki/LogSystem.h>
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <string>
namespace DekiGL
{
namespace
{
// Quads per batch: four corners each, indexed in 16 bits.
constexpr uint32_t kMaxQuads = 16384;
// Textures not drawn for this many frames go (two seconds at 60 fps), and
// past this many bytes the ones drawn longest ago go first.
constexpr uint32_t kIdleFrames = 120;
constexpr size_t kTextureBudget = 64u * 1024u * 1024u;
// Vertex::key[3].
enum : uint8_t
{
kFlagFlipH = 1,
kFlagFlipV = 2,
kFlagFlipD = 4, // only for a square source, as the CPU transposes only those
kFlagRepeatColumn = 8,
kFlagRepeatRow = 16,
kFlagKey = 32,
kFlagDither = 64,
};
const char* const kQuadVertexShader = R"(
layout(location = 0) in ivec2 aCorner;
layout(location = 1) in ivec4 aOrigin;
layout(location = 2) in ivec4 aStep;
layout(location = 3) in ivec4 aSize;
layout(location = 4) in uvec4 aTint;
layout(location = 5) in uvec4 aKey;
layout(location = 6) in uvec4 aSample;
uniform vec2 uFrame; // the frame's size in pixels
flat out ivec4 vOrigin;
flat out ivec4 vStep;
flat out ivec4 vSize;
flat out uvec4 vTint;
flat out uvec4 vKey;
flat out uvec4 vSample;
void main()
{
// Corners on pixel edges, top-down like the CPU buffer.
vec2 p = vec2(aCorner) / uFrame;
gl_Position = vec4(p.x * 2.0 - 1.0, 1.0 - p.y * 2.0, 0.0, 1.0);
vOrigin = aOrigin;
vStep = aStep;
vSize = aSize;
vTint = aTint;
vKey = aKey;
vSample = aSample;
}
)";
// QuadBlit's pixel pipeline (CompositePixel) for one destination pixel, with
// the CPU's integer stepping and Div255. The blend itself is the GPU's,
// premultiplied: the frame keeps 8 bits a channel where the CPU's RGB565A8
// buffer rounds each write to 565. kBayer is filled in from PixelFormat.h.
const char* const kQuadFragmentHead = R"(
precision highp float;
precision highp int;
uniform highp sampler2D uTexture;
uniform int uHeight; // the frame's, to count rows from the top
flat in ivec4 vOrigin;
flat in ivec4 vStep;
flat in ivec4 vSize;
flat in uvec4 vTint;
flat in uvec4 vKey;
flat in uvec4 vSample;
out vec4 fragColor;
)";
const char* const kQuadFragmentMain = R"(
uint Div255(uint x)
{
return (x + 1u + (x >> 8)) >> 8;
}
// One texel of the source at mipmap `level`, at (x, y) in the sampled space
// (before the flips), clamped into the source so a frame never takes its
// neighbour's texels; premultiplied, a keyed texel transparent.
vec4 TexelAt(int x, int y, int level)
{
uint flags = vKey.w;
ivec2 sampled = max(ivec2(vSize.zw) >> level, ivec2(1));
ivec2 full = max(ivec2(vSize.xy) >> level, ivec2(1));
x = clamp(x, 0, sampled.x - 1);
y = clamp(y, 0, sampled.y - 1);
if ((flags & 2u) != 0u)
{
y = full.y - 1 - y;
}
if ((flags & 1u) != 0u)
{
x = full.x - 1 - x;
}
if ((flags & 4u) != 0u)
{
int t = x;
x = y;
y = t;
}
vec4 c = texelFetch(uTexture, ivec2(x, y), level);
if ((flags & 32u) != 0u && uvec3(c.rgb * 255.0 + 0.5) == vKey.rgb)
{
return vec4(0.0);
}
return vec4(c.rgb * c.a, c.a);
}
// The four texels around `pos` (texel units of level 0, sampled space),
// blended, at mipmap `level`.
vec4 BilinearAt(vec2 pos, int level)
{
vec2 p = pos / float(1 << level) - 0.5;
vec2 f = fract(p);
ivec2 i = ivec2(floor(p));
vec4 a = mix(TexelAt(i.x, i.y, level), TexelAt(i.x + 1, i.y, level), f.x);
vec4 b = mix(TexelAt(i.x, i.y + 1, level), TexelAt(i.x + 1, i.y + 1, level), f.x);
return mix(a, b, f.y);
}
void main()
{
ivec2 p = ivec2(gl_FragCoord.xy);
int px = p.x;
int py = uHeight - 1 - p.y;
// The 16.16 walk, in 32 bits that wrap, as the CPU steps it.
uint dx = uint(px - vOrigin.x);
uint dy = uint(py - vOrigin.y);
int x = int(uint(vOrigin.z) + dx * uint(vStep.x) + dy * uint(vStep.z)) >> 16;
int y = int(uint(vOrigin.w) + dx * uint(vStep.y) + dy * uint(vStep.w)) >> 16;
if (x < 0 || y < 0 || x >= vSize.z || y >= vSize.w)
{
discard;
}
uint flags = vKey.w;
if ((flags & 8u) != 0u)
{
x = 0;
}
if ((flags & 16u) != 0u)
{
y = 0;
}
// ApplyFlips' order: V, then H, then the transpose.
if ((flags & 2u) != 0u)
{
y = vSize.y - 1 - y;
}
if ((flags & 1u) != 0u)
{
x = vSize.x - 1 - x;
}
if ((flags & 4u) != 0u)
{
int t = x;
x = y;
y = t;
}
uvec4 c;
if (vSample.x == 0u)
{
c = uvec4(texelFetch(uTexture, ivec2(x, y), 0) * 255.0 + 0.5);
if (c.a == 0u)
{
discard;
}
// The key is compared against the untinted colour.
if ((flags & 32u) != 0u && c.rgb == vKey.rgb)
{
discard;
}
}
else
{
// Filtered: the same pixels are covered (the walk above), each
// taking the blend at its exact place in the source.
vec2 pos = vec2(float(int(uint(vOrigin.z) + dx * uint(vStep.x) + dy * uint(vStep.z))),
float(int(uint(vOrigin.w) + dx * uint(vStep.y) + dy * uint(vStep.w)))) / 65536.0;
if ((flags & 8u) != 0u)
{
pos.x = 0.5;
}
if ((flags & 16u) != 0u)
{
pos.y = 0.5;
}
vec4 premul;
if (vSample.x == 2u && vSample.y > 0u)
{
// Trilinear: the two levels around how many texels one pixel
// steps across.
float across = max(length(vec2(vStep.xy)), length(vec2(vStep.zw))) / 65536.0;
float lod = clamp(log2(max(across, 1e-6)), 0.0, float(vSample.y));
int low = int(floor(lod));
int high = min(low + 1, int(vSample.y));
premul = mix(BilinearAt(pos, low), BilinearAt(pos, high), lod - float(low));
}
else
{
premul = BilinearAt(pos, 0);
}
if (premul.a <= 0.5 / 255.0)
{
discard;
}
c = uvec4(vec4(premul.rgb / premul.a, premul.a) * 255.0 + 0.5);
}
uvec3 rgb = uvec3(Div255(c.r * vTint.r), Div255(c.g * vTint.g), Div255(c.b * vTint.b));
uint a = Div255(c.a * vTint.a);
if (a == 0u)
{
discard;
}
if ((flags & 64u) != 0u)
{
// 255 always passes, since the matrix tops out at 252.
if (int(a) <= kBayer[((py & 7) << 3) | (px & 7)])
{
discard;
}
a = 255u;
}
float alpha = float(a) / 255.0;
fragColor = vec4(vec3(rgb) / 255.0 * alpha, alpha);
}
)";
/// The engine's 8x8 Bayer matrix as a GLSL array, so the shader's threshold
/// is the CPU's.
std::string BayerDeclaration()
{
std::string s = "const int kBayer[64] = int[64](";
for (int i = 0; i < 64; ++i)
{
s += std::to_string(DekiPixel::kBayer8x8[i]);
s += i < 63 ? ", " : ");\n";
}
return s;
}
/// One row of a source as RGBA8, each pixel read as QuadBlit reads it.
template <DekiPixel::SrcKind SK>
void SourceRowToRGBA(const uint8_t* row, int32_t width, int32_t bytesPerPixel, bool hasAlpha, uint8_t* out)
{
for (int32_t x = 0; x < width; ++x, row += bytesPerPixel, out += 4)
{
DekiPixel::ReadSrcPixel<SK>(row, hasAlpha, out[0], out[1], out[2], out[3]);
}
}
} // namespace
size_t GLBlitSink::KeyHash::operator()(const Key& k) const
{
size_t h = std::hash<const void*>()(k.pixels);
auto mix = [&](size_t v) { h ^= v + 0x9E3779B97F4A7C15ull + (h << 6) + (h >> 2); };
mix(static_cast<size_t>(k.width));
mix(static_cast<size_t>(k.height));
mix(static_cast<size_t>(k.stride));
mix(static_cast<size_t>(k.bytesPerPixel) | (static_cast<size_t>(k.isRGB565) << 8) |
(static_cast<size_t>(k.hasAlpha) << 9));
return h;
}
GLBlitSink::GLBlitSink(GLPresenter& presenter)
: m_Presenter(presenter)
{
}
bool GLBlitSink::Build()
{
const GL& gl = m_Presenter.m_Context->Functions();
const std::string version = m_Presenter.m_Context->IsES() ? "#version 300 es\n" : "#version 330 core\n";
const GLuint vs = m_Presenter.Compile(gl::VERTEX_SHADER, version + kQuadVertexShader, "the 2D vertex shader");
const GLuint fs = vs ? m_Presenter.Compile(gl::FRAGMENT_SHADER,
version + kQuadFragmentHead + BayerDeclaration() + kQuadFragmentMain,
"the 2D fragment shader")
: 0;
if (!vs || !fs)
{
gl.DeleteShader(vs);
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 2D shader failed");
return false;
}
m_FrameUniform = gl.GetUniformLocation(m_Program, "uFrame");
m_HeightUniform = gl.GetUniformLocation(m_Program, "uHeight");
m_TextureUniform = gl.GetUniformLocation(m_Program, "uTexture");
gl.GetIntegerv(gl::MAX_TEXTURE_SIZE, &m_MaxTextureSize);
m_MaxTextureSize = std::min<GLint>(m_MaxTextureSize, 32767); // Vertex::size is 16 bits
// Every batch draws its quads as two triangles each, from one index list.
constexpr uint16_t kCorners[6] = { 0, 1, 2, 2, 1, 3 };
std::vector<uint16_t> indices(static_cast<size_t>(kMaxQuads) * 6);
for (size_t i = 0; i < indices.size(); ++i)
{
indices[i] = static_cast<uint16_t>(i / 6 * 4 + kCorners[i % 6]);
}
gl.GenVertexArrays(1, &m_VertexArray);
gl.GenBuffers(1, &m_VertexBuffer);
gl.GenBuffers(1, &m_IndexBuffer);
gl.BindVertexArray(m_VertexArray);
gl.BindBuffer(gl::ELEMENT_ARRAY_BUFFER, m_IndexBuffer);
gl.BufferData(gl::ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(indices.size() * sizeof(uint16_t)), indices.data(),
gl::STATIC_DRAW);
gl.BindBuffer(gl::ARRAY_BUFFER, m_VertexBuffer);
auto attribute = [&](GLuint index, GLint size, GLenum type, size_t offset)
{
gl.EnableVertexAttribArray(index);
gl.VertexAttribIPointer(index, size, type, sizeof(Vertex), reinterpret_cast<const void*>(offset));
};
attribute(0, 2, gl::SHORT, offsetof(Vertex, x));
attribute(1, 4, gl::INT, offsetof(Vertex, origin));
attribute(2, 4, gl::INT, offsetof(Vertex, step));
attribute(3, 4, gl::SHORT, offsetof(Vertex, size));
attribute(4, 4, gl::UNSIGNED_BYTE, offsetof(Vertex, tint));
attribute(5, 4, gl::UNSIGNED_BYTE, offsetof(Vertex, key));
attribute(6, 4, gl::UNSIGNED_BYTE, offsetof(Vertex, sample));
gl.BindVertexArray(0);
return gl.GetError() == 0;
}
void GLBlitSink::Release()
{
const GL& gl = m_Presenter.m_Context->Functions();
for (auto& entry : m_Textures)
{
gl.DeleteTextures(1, &entry.second.id);
}
m_Textures.clear();
gl.DeleteTextures(1, &m_Scratch.id);
m_Scratch = Texture{};
m_TextureBytes = 0;
gl.DeleteBuffers(1, &m_VertexBuffer);
gl.DeleteBuffers(1, &m_IndexBuffer);
gl.DeleteVertexArrays(1, &m_VertexArray);
gl.DeleteProgram(m_Program);
m_VertexBuffer = m_IndexBuffer = m_VertexArray = m_Program = 0;
}
bool GLBlitSink::BeginFrame(uint8_t* target, int32_t width, int32_t height, Deki::ColorFormat format, uint8_t clearR,
uint8_t clearG, uint8_t clearB)
{
m_Target = nullptr;
m_Written = false;
m_Vertices.clear();
m_Runs.clear();
const char* why = nullptr;
if (!m_Enabled)
{
why = "turned off";
}
else if (!m_Presenter.TakesLayers(width, height, format))
{
why = "the frame buffer is not RGB565A8 at the frame's size";
}
else if (!m_Presenter.m_Context->MakeCurrent())
{
why = "OpenGL is gone";
}
// Which way 2D reaches the screen, said once and again whenever it
// changes, since the two differ so much in cost.
const int mode = why ? 2 : 1;
if (mode != m_LoggedMode)
{
m_LoggedMode = mode;
if (why)
{
DEKI_LOG_INFO("OpenGL: drawing 2D on the CPU (%s)", why);
}
else
{
DEKI_LOG_INFO("OpenGL: drawing 2D straight into the presented frame");
}
}
if (why)
{
return false;
}
if (++m_Frame % 60 == 0)
{
Evict();
}
// The colour the CPU's clear writes: RGB565A8 keeps 565.
DekiPixel::QuantizeRGB565(clearR, clearG, clearB);
m_Presenter.ClearFrame(width, height, clearR, clearG, clearB);
if (!m_Presenter.m_InFrame)
{
return false; // no frame to draw in; said by the presenter
}
std::memset(target, 0, Deki::FrameBufferBytes(format, width, height));
m_Target = target;
m_Width = width;
m_Height = height;
m_Format = format;
return true;
}
bool GLBlitSink::Draw(const QuadBlit::SinkBlit& blit)
{
const QuadBlit::Source& source = *blit.source;
if (!m_Target || !m_Presenter.m_InFrame || source.width > m_MaxTextureSize || source.height > m_MaxTextureSize ||
blit.sampleWidth > 32767 || blit.sampleHeight > 32767 || blit.right > 32767 || blit.bottom > 32767)
{
return false;
}
// What was written into the buffer since the last quad goes under this one.
if (m_Written)
{
m_Written = false;
m_Presenter.PushLayer(m_Target, m_Width, m_Height, m_Format);
std::memset(m_Target, 0, Deki::FrameBufferBytes(m_Format, m_Width, m_Height));
}
// A full batch draws before the texture is found, so a texture queued
// with this quad is queued in the batch it is drawn in.
if (m_Vertices.size() >= static_cast<size_t>(kMaxQuads) * 4)
{
Flush();
}
uint8_t mipLevels = 0;
const GLuint texture = TextureFor(source, mipLevels);
if (!texture)
{
return false;
}
Vertex v;
v.origin[0] = blit.originX;
v.origin[1] = blit.originY;
v.origin[2] = blit.originSx;
v.origin[3] = blit.originSy;
v.step[0] = blit.dSxDx;
v.step[1] = blit.dSyDx;
v.step[2] = blit.dSxDy;
v.step[3] = blit.dSyDy;
v.size[0] = static_cast<int16_t>(source.width);
v.size[1] = static_cast<int16_t>(source.height);
v.size[2] = static_cast<int16_t>(blit.sampleWidth);
v.size[3] = static_cast<int16_t>(blit.sampleHeight);
v.tint[0] = blit.tintR;
v.tint[1] = blit.tintG;
v.tint[2] = blit.tintB;
v.tint[3] = blit.tintA;
v.key[0] = source.keyR;
v.key[1] = source.keyG;
v.key[2] = source.keyB;
uint8_t flags = 0;
flags |= source.flipH ? kFlagFlipH : 0;
flags |= source.flipV ? kFlagFlipV : 0;
flags |= source.flipD && source.width == source.height ? kFlagFlipD : 0;
flags |= blit.repeatColumn ? kFlagRepeatColumn : 0;
flags |= blit.repeatRow ? kFlagRepeatRow : 0;
flags |= source.hasChromaKey ? kFlagKey : 0;
flags |= blit.orderedDither ? kFlagDither : 0;
v.key[3] = flags;
v.sample[0] = static_cast<uint8_t>(source.filter);
v.sample[1] = mipLevels;
v.sample[2] = 0;
v.sample[3] = 0;
const int16_t xs[2] = { static_cast<int16_t>(blit.left), static_cast<int16_t>(blit.right) };
const int16_t ys[2] = { static_cast<int16_t>(blit.top), static_cast<int16_t>(blit.bottom) };
for (int corner = 0; corner < 4; ++corner)
{
v.x = xs[corner & 1];
v.y = ys[corner >> 1];
m_Vertices.push_back(v);
}
const uint32_t quad = static_cast<uint32_t>(m_Vertices.size() / 4 - 1);
if (!m_Runs.empty() && m_Runs.back().texture == texture)
{
++m_Runs.back().count;
}
else
{
m_Runs.push_back({ texture, quad, 1 });
}
++m_Stats.blits;
return true;
}
void GLBlitSink::TargetWritten()
{
if (m_Target)
{
m_Written = true;
}
}
void GLBlitSink::EndFrame()
{
Flush();
// What the buffer still holds, the display composes as it presents.
m_Target = nullptr;
m_Written = false;
}
void GLBlitSink::LayerPushed(const uint8_t* buffer)
{
if (buffer && buffer == m_Target)
{
m_Written = false;
}
}
bool GLBlitSink::Light(const DekiRendering::Lighting2D& lighting)
{
return m_Presenter.DrawLight2D(lighting);
}
void GLBlitSink::Forget(const uint8_t* begin, size_t bytes)
{
// No OpenGL here (a sprite may go while no context is current): the
// texture is marked, uploaded again if drawn, and evicted if not.
const uint8_t* end = begin + bytes;
for (auto& entry : m_Textures)
{
if (entry.first.pixels >= begin && entry.first.pixels < end)
{
entry.second.revision = 0;
entry.second.stable = false;
}
}
}
GLuint GLBlitSink::TextureFor(const QuadBlit::Source& source, uint8_t& mipLevels)
{
mipLevels = 0;
const bool wantsMipmaps = source.filter == Deki::TextureFilter::Trilinear;
const GL& gl = m_Presenter.m_Context->Functions();
// Pixels handed over (ownsPixels) are freed after the blit, and the next
// ones may land anywhere: they go up into one texture kept for them, not
// under their address.
if (source.ownsPixels)
{
if (!m_Scratch.id)
{
gl.GenTextures(1, &m_Scratch.id);
}
if (m_Scratch.batch == m_Batch && !m_Vertices.empty())
{
Flush();
}
Upload(m_Scratch, source);
if (wantsMipmaps)
{
MakeMipmaps(m_Scratch);
}
mipLevels = m_Scratch.mipLevels;
m_Scratch.batch = m_Batch;
return m_Scratch.id;
}
const int32_t stride = source.stride ? source.stride : source.width * source.bytesPerPixel;
Key key;
key.pixels = source.pixels;
key.width = source.width;
key.height = source.height;
key.stride = stride;
key.bytesPerPixel = source.bytesPerPixel;
key.isRGB565 = source.isRGB565;
key.hasAlpha = source.hasAlpha;
auto [it, fresh] = m_Textures.try_emplace(key);
Texture& texture = it->second;
if (fresh)
{
gl.GenTextures(1, &texture.id);
if (!texture.id)
{
m_Textures.erase(it);
return 0;
}
}
// What it holds is these pixels: the revision says so, or the source
// promises they stay as they are and nothing said they went.
const bool current =
!fresh && (source.revision != 0 ? texture.revision == source.revision : source.cacheScaled && texture.stable);
if (!current)
{
// Queued quads read what it holds now.
if (texture.batch == m_Batch && !m_Vertices.empty())
{
Flush();
}
Upload(texture, source);
texture.revision = source.revision;
texture.stable = source.revision == 0 && source.cacheScaled;
}
if (wantsMipmaps && texture.mipLevels == 0)
{
if (texture.batch == m_Batch && !m_Vertices.empty())
{
Flush();
}
MakeMipmaps(texture);
}
mipLevels = texture.mipLevels;
texture.lastUsed = m_Frame;
texture.batch = m_Batch;
return texture.id;
}
void GLBlitSink::MakeMipmaps(Texture& texture)
{
// Halved down to 1x1, the levels a Trilinear quad reads with texelFetch.
// A texture is complete for that only with a mipmapped filter.
int levels = 0;
for (int32_t size = std::max(texture.width, texture.height); size > 1; size >>= 1)
{
++levels;
}
if (levels == 0)
{
return;
}
const GL& gl = m_Presenter.m_Context->Functions();
gl.ActiveTexture(gl::TEXTURE0);
gl.BindTexture(gl::TEXTURE_2D, texture.id);
gl.GenerateMipmap(gl::TEXTURE_2D);
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, static_cast<GLint>(gl::NEAREST_MIPMAP_NEAREST));
texture.mipLevels = static_cast<uint8_t>(std::min(levels, 255));
texture.mipBytes = static_cast<size_t>(texture.width) * texture.height * 4 / 3;
m_TextureBytes += texture.mipBytes;
}
void GLBlitSink::Upload(Texture& texture, const QuadBlit::Source& source)
{
const int32_t w = source.width, h = source.height, bpp = source.bytesPerPixel;
const int32_t stride = source.stride ? source.stride : w * bpp;
m_Staging.resize(static_cast<size_t>(w) * h * 4);
for (int32_t y = 0; y < h; ++y)
{
const uint8_t* row = source.pixels + static_cast<size_t>(y) * stride;
uint8_t* out = &m_Staging[static_cast<size_t>(y) * w * 4];
// QuadBlit's KindOf.
if (source.isRGB565)
{
if (bpp >= 3)
{
SourceRowToRGBA<DekiPixel::SrcKind::RGB565A8>(row, w, bpp, source.hasAlpha, out);
}
else
{
SourceRowToRGBA<DekiPixel::SrcKind::RGB565>(row, w, bpp, source.hasAlpha, out);
}
}
else if (bpp == 4)
{
SourceRowToRGBA<DekiPixel::SrcKind::RGBA8888>(row, w, bpp, source.hasAlpha, out);
}
else if (bpp == 3)
{
SourceRowToRGBA<DekiPixel::SrcKind::RGB888>(row, w, bpp, source.hasAlpha, out);
}
else
{
SourceRowToRGBA<DekiPixel::SrcKind::ALPHA8>(row, w, bpp, source.hasAlpha, out);
}
}
const GL& gl = m_Presenter.m_Context->Functions();
gl.ActiveTexture(gl::TEXTURE0);
gl.BindTexture(gl::TEXTURE_2D, texture.id);
gl.PixelStorei(gl::UNPACK_ALIGNMENT, 4);
if (texture.width != w || texture.height != h)
{
gl.TexImage2D(gl::TEXTURE_2D, 0, static_cast<GLint>(gl::RGBA8), w, h, 0, gl::RGBA, gl::UNSIGNED_BYTE,
m_Staging.data());
if (texture.width == 0)
{
// Read with texelFetch, but a texture is only complete for it
// with a filter that needs no mipmaps.
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_TextureBytes -= static_cast<size_t>(texture.width) * texture.height * 4;
m_TextureBytes += static_cast<size_t>(w) * h * 4;
texture.width = w;
texture.height = h;
}
else
{
gl.TexSubImage2D(gl::TEXTURE_2D, 0, 0, 0, w, h, gl::RGBA, gl::UNSIGNED_BYTE, m_Staging.data());
}
if (texture.mipLevels > 0)
{
// Level 0 changed: back to the one level until mipmaps are made again.
m_TextureBytes -= texture.mipBytes;
texture.mipBytes = 0;
texture.mipLevels = 0;
gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, static_cast<GLint>(gl::NEAREST));
}
++m_Stats.uploads;
m_Stats.textures = static_cast<uint32_t>(m_Textures.size());
m_Stats.textureBytes = m_TextureBytes;
}
void GLBlitSink::DeleteTexture(Texture& texture)
{
const GL& gl = m_Presenter.m_Context->Functions();
gl.DeleteTextures(1, &texture.id);
m_TextureBytes -= static_cast<size_t>(texture.width) * texture.height * 4 + texture.mipBytes;
texture = Texture{};
}
void GLBlitSink::Evict()
{
for (auto it = m_Textures.begin(); it != m_Textures.end();)
{
if (m_Frame - it->second.lastUsed > kIdleFrames)
{
DeleteTexture(it->second);
it = m_Textures.erase(it);
}
else
{
++it;
}
}
// Over the budget: the ones drawn longest ago, never one drawn this frame.
while (m_TextureBytes > kTextureBudget)
{
auto oldest = m_Textures.end();
for (auto it = m_Textures.begin(); it != m_Textures.end(); ++it)
{
if (it->second.lastUsed != m_Frame &&
(oldest == m_Textures.end() || it->second.lastUsed < oldest->second.lastUsed))
{
oldest = it;
}
}
if (oldest == m_Textures.end())
{
break;
}
DeleteTexture(oldest->second);
m_Textures.erase(oldest);
}
m_Stats.textures = static_cast<uint32_t>(m_Textures.size());
m_Stats.textureBytes = m_TextureBytes;
}
void GLBlitSink::Flush()
{
if (m_Vertices.empty())
{
return;
}
if (!m_Presenter.m_InFrame || !m_Presenter.m_Context->MakeCurrent())
{
m_Vertices.clear();
m_Runs.clear();
return;
}
const GL& gl = m_Presenter.m_Context->Functions();
const int width = m_Presenter.m_Width, height = m_Presenter.m_Height;
// Everything set each time: meshes and layers draw in between.
gl.BindFramebuffer(gl::FRAMEBUFFER, m_Presenter.m_Framebuffer);
gl.Viewport(0, 0, width, height);
gl.Disable(gl::DEPTH_TEST);
gl.Disable(gl::CULL_FACE);
gl.Disable(gl::SCISSOR_TEST);
gl.Disable(gl::DITHER);
gl.Enable(gl::BLEND);
gl.BlendFunc(gl::ONE, gl::ONE_MINUS_SRC_ALPHA);
gl.UseProgram(m_Program);
gl.Uniform2f(m_FrameUniform, static_cast<float>(width), static_cast<float>(height));
gl.Uniform1i(m_HeightUniform, height);
gl.Uniform1i(m_TextureUniform, 0);
gl.BindVertexArray(m_VertexArray);
gl.BindBuffer(gl::ARRAY_BUFFER, m_VertexBuffer);
gl.BufferData(gl::ARRAY_BUFFER, static_cast<GLsizeiptr>(m_Vertices.size() * sizeof(Vertex)), m_Vertices.data(),
gl::STREAM_DRAW);
gl.ActiveTexture(gl::TEXTURE0);
for (const Run& run : m_Runs)
{
gl.BindTexture(gl::TEXTURE_2D, run.texture);
gl.DrawElements(gl::TRIANGLES, static_cast<GLsizei>(run.count * 6), gl::UNSIGNED_SHORT,
reinterpret_cast<const void*>(static_cast<uintptr_t>(run.first) * 6 * sizeof(uint16_t)));
}
gl.Disable(gl::BLEND);
m_Stats.draws += static_cast<uint32_t>(m_Runs.size());
const GLenum error = gl.GetError();
if (error != 0 && !m_ErrorReported)
{
m_ErrorReported = true;
DEKI_LOG_WARNING("OpenGL: OpenGL error 0x%04X drawing 2D", error);
}
m_Vertices.clear();
m_Runs.clear();
++m_Batch;
}
} // namespace DekiGL
#endif // DEKI_CAPABILITY_OPENGL