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
|
#include "UnityPrefix.h"
#include "RenderTextureGLES30.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/Shaders/GraphicsCaps.h"
#include "Runtime/Graphics/Image.h"
#include "Runtime/Utilities/ArrayUtility.h"
#include "Runtime/Utilities/BitUtility.h"
#include "Runtime/Graphics/RenderTexture.h"
#include "Runtime/Graphics/ScreenManager.h"
#include "IncludesGLES30.h"
#include "AssertGLES30.h"
#include "DebugGLES30.h"
#include "TextureIdMapGLES30.h"
#include "UtilsGLES30.h"
#if UNITY_ANDROID
#include "PlatformDependent/AndroidPlayer/EntryPoint.h"
#endif
#if 1
#define DBG_LOG_RT_GLES30(...) {}
#else
#define DBG_LOG_RT_GLES30(...) {printf_console(__VA_ARGS__);printf_console("\n");}
#endif
#if GFX_SUPPORTS_OPENGLES30
namespace
{
static const UInt32 kCubeFacesES3[] =
{
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
};
inline bool IsDepthStencilFormat (UInt32 format)
{
switch (format)
{
case GL_DEPTH24_STENCIL8:
case GL_DEPTH32F_STENCIL8:
return true;
default:
return false;
}
}
const char* GetFBOStatusName (UInt32 status)
{
switch (status)
{
case GL_FRAMEBUFFER_COMPLETE: return "COMPLETE";
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: return "INCOMPLETE_ATTACHMENT";
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: return "INCOMPLETE_MISSING_ATTACHMENT";
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: return "INCOMPLETE_DIMENSIONS";
case GL_FRAMEBUFFER_UNSUPPORTED: return "UNSUPPORTED";
default: return "unknown error";
}
}
} // anonymous
// RenderSurfaceGLES30
RenderSurfaceGLES30::RenderSurfaceGLES30 (Type type, UInt32 format, int w, int h, int numSamples)
: m_type (type)
, m_format (format)
, m_flags (0)
{
RenderSurfaceBase_Init(*this);
width = w;
height = h;
samples = numSamples;
}
// RenderTexture2DGLES30
RenderTexture2DGLES30::RenderTexture2DGLES30 (TextureID texID, UInt32 format, int w, int h)
: RenderSurfaceGLES30 (kTypeTexture2D, format, w, h, 1)
{
textureID = texID;
TransferFormatGLES30 transferFmt = GetTransferFormatGLES30(format);
GLuint glTexID = TextureIdMapGLES30_QueryOrCreate(textureID);
Assert(glTexID != 0);
GetRealGfxDevice().SetTexture(kShaderFragment, 0, 0, textureID, kTexDim2D, std::numeric_limits<float>::infinity());
GLES_CHK(glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, transferFmt.format, transferFmt.dataType, 0));
}
RenderTexture2DGLES30::~RenderTexture2DGLES30 (void)
{
// \todo [2013-04-29 pyry] Set texture storage to null?
}
void RenderTexture2DGLES30::AttachColor (int ndx, CubemapFace face)
{
Assert(face == kCubeFaceUnknown);
GLES_CHK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+ndx, GL_TEXTURE_2D, GetGLTextureID(), 0));
}
void RenderTexture2DGLES30::AttachDepthStencil (CubemapFace face)
{
// \note Using GL_DEPTH_STENCIL_ATTACHMENT would eliminate one more call, but unfortunately
// that is broken at least on ARM GLES3 EMU.
Assert(face == kCubeFaceUnknown);
GLES_CHK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, GetGLTextureID(), 0));
if (IsDepthStencilFormat(m_format))
GLES_CHK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, GetGLTextureID(), 0));
}
// RenderTextureCubeGLES30
RenderTextureCubeGLES30::RenderTextureCubeGLES30 (TextureID texID, UInt32 format, int w, int h)
: RenderSurfaceGLES30 (kTypeTextureCube, format, w, h, 1)
{
textureID = texID;
TransferFormatGLES30 transferFmt = GetTransferFormatGLES30(format);
GLuint glTexID = TextureIdMapGLES30_QueryOrCreate(textureID);
Assert(glTexID != 0);
GetRealGfxDevice().SetTexture(kShaderFragment, 0, 0, textureID, kTexDimCUBE, std::numeric_limits<float>::infinity());
for (int ndx = 0; ndx < 6; ndx++)
GLES_CHK(glTexImage2D(kCubeFacesES3[ndx], 0, format, width, height, 0, transferFmt.format, transferFmt.dataType, 0));
}
RenderTextureCubeGLES30::~RenderTextureCubeGLES30 (void)
{
// \todo [2013-04-29 pyry] Set texture storage to null?
}
void RenderTextureCubeGLES30::AttachColor (int ndx, CubemapFace face)
{
const int faceIndex = clamp<int>(face,0,5); // can be passed -1 when restoring from previous RT
GLES_CHK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+ndx, kCubeFacesES3[faceIndex], GetGLTextureID(), 0));
}
void RenderTextureCubeGLES30::AttachDepthStencil (CubemapFace face)
{
const int faceIndex = clamp<int>(face,0,5); // can be passed -1 when restoring from previous RT
GLES_CHK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, kCubeFacesES3[faceIndex], GetGLTextureID(), 0));
if (IsDepthStencilFormat(m_format))
GLES_CHK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, kCubeFacesES3[faceIndex], GetGLTextureID(), 0));
}
// RenderBufferGLES30
RenderBufferGLES30::RenderBufferGLES30 (UInt32 format, int w, int h, int numSamples)
: RenderSurfaceGLES30 (kTypeRenderBuffer, format, w, h, numSamples)
, m_bufferID (0)
{
glGenRenderbuffers(1, (GLuint*)&m_bufferID);
glBindRenderbuffer(GL_RENDERBUFFER, m_bufferID);
if (numSamples > 1)
GLES_CHK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, numSamples, format, width, height));
else
GLES_CHK(glRenderbufferStorage(GL_RENDERBUFFER, format, width, height));
}
RenderBufferGLES30::~RenderBufferGLES30 (void)
{
if (m_bufferID)
glDeleteRenderbuffers(1, (GLuint*)&m_bufferID);
}
void RenderBufferGLES30::AttachColor (int ndx, CubemapFace face)
{
Assert(face == kCubeFaceUnknown);
GLES_CHK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+ndx, GL_RENDERBUFFER, m_bufferID));
}
void RenderBufferGLES30::AttachDepthStencil (CubemapFace face)
{
Assert(face == kCubeFaceUnknown);
GLES_CHK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_bufferID));
if (IsDepthStencilFormat(m_format))
GLES_CHK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_bufferID));
}
void RenderBufferGLES30::Disown (void)
{
m_bufferID = 0;
}
// RenderBufferCubeGLES30
RenderBufferCubeGLES30::RenderBufferCubeGLES30 (UInt32 format, int w, int h, int numSamples)
: RenderSurfaceGLES30 (kTypeRenderBufferCube, format, w, h, numSamples)
{
memset(&m_buffers[0], 0, sizeof(m_buffers));
for (int i = 0; i < 6; i++)
m_buffers[i] = new RenderBufferGLES30(format, width, height, numSamples);
}
RenderBufferCubeGLES30::~RenderBufferCubeGLES30 (void)
{
for (int i = 0; i < 6; i++)
delete m_buffers[i];
}
void RenderBufferCubeGLES30::AttachColor (int ndx, CubemapFace face)
{
const int faceIndex = clamp<int>(face,0,5); // can be passed -1 when restoring from previous RT
m_buffers[faceIndex]->AttachColor(ndx, kCubeFaceUnknown);
}
void RenderBufferCubeGLES30::AttachDepthStencil (CubemapFace face)
{
const int faceIndex = clamp<int>(face,0,5); // can be passed -1 when restoring from previous RT
m_buffers[faceIndex]->AttachDepthStencil(kCubeFaceUnknown);
}
// FramebufferAttachmentsGLES30
FramebufferAttachmentsGLES30::FramebufferAttachmentsGLES30 (int numColorAttachments, RenderSurfaceGLES30* colorAttachments, RenderSurfaceGLES30* depthStencilAttachment, CubemapFace face)
: numColorAttachments (numColorAttachments)
, depthStencil (depthStencilAttachment)
, cubemapFace (face)
{
for (int ndx = 0; ndx < numColorAttachments; ndx++)
color[ndx] = &colorAttachments[ndx];
for (int ndx = numColorAttachments; ndx < kMaxColorAttachments; ndx++)
color[ndx] = 0;
}
FramebufferAttachmentsGLES30::FramebufferAttachmentsGLES30 (void)
: numColorAttachments (0)
, depthStencil (0)
, cubemapFace (kCubeFaceUnknown)
{
memset(&color[0], 0, sizeof(color));
}
// FramebufferObjectGLES30
FramebufferObjectGLES30::FramebufferObjectGLES30 (const FramebufferAttachmentsGLES30& attachments)
: m_fboID (0)
, m_attachments (attachments)
{
GLuint oldFbo = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&oldFbo);
glGenFramebuffers(1, (GLuint*)&m_fboID);
GLES_CHK(glBindFramebuffer(GL_FRAMEBUFFER, m_fboID));
for (int colorNdx = 0; colorNdx < attachments.numColorAttachments; colorNdx++)
{
if (m_attachments.color[colorNdx] && m_attachments.color[colorNdx]->GetType() != RenderSurfaceGLES30::kTypeDummy)
m_attachments.color[colorNdx]->AttachColor(colorNdx, m_attachments.cubemapFace);
}
if (m_attachments.depthStencil)
m_attachments.depthStencil->AttachDepthStencil(m_attachments.cubemapFace);
UInt32 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
const char* statusName = GetFBOStatusName(status);
ErrorStringMsg("Framebuffer is not complete: %s", statusName);
Assert(status == GL_FRAMEBUFFER_COMPLETE);
}
// \todo [2013-04-30 pyry] We should really fail object construction if fbo is not complete.
GLES_CHK(glBindFramebuffer(GL_FRAMEBUFFER, oldFbo));
}
FramebufferObjectGLES30::~FramebufferObjectGLES30 (void)
{
if (m_fboID)
glDeleteFramebuffers(1, (GLuint*)&m_fboID);
}
void FramebufferObjectGLES30::Disown (void)
{
m_fboID = 0;
}
RenderSurfaceGLES30* FramebufferObjectGLES30::GetColorAttachment (int ndx)
{
Assert(0 <= ndx && ndx <= FramebufferAttachmentsGLES30::kMaxColorAttachments);
return m_attachments.color[ndx];
}
const RenderSurfaceGLES30* FramebufferObjectGLES30::GetColorAttachment (int ndx) const
{
Assert(0 <= ndx && ndx <= FramebufferAttachmentsGLES30::kMaxColorAttachments);
return m_attachments.color[ndx];
}
RenderSurfaceGLES30* FramebufferObjectGLES30::GetDepthStencilAttachment (void)
{
return m_attachments.depthStencil;
}
const RenderSurfaceGLES30* FramebufferObjectGLES30::GetDepthStencilAttachment (void) const
{
return m_attachments.depthStencil;
}
// FramebufferObjectManagerGLES30
bool CompareFramebufferAttachmentsGLES30::operator() (const FramebufferAttachmentsGLES30* a, const FramebufferAttachmentsGLES30* b) const
{
if (a->numColorAttachments < b->numColorAttachments) return true;
else if (a->numColorAttachments > b->numColorAttachments) return false;
if (a->depthStencil < b->depthStencil) return true;
else if (a->depthStencil > b->depthStencil) return false;
if (a->cubemapFace < b->cubemapFace) return true;
else if (a->cubemapFace > b->cubemapFace) return false;
for (int ndx = 0; ndx < a->numColorAttachments; ndx++)
{
if (a->color[ndx] < b->color[ndx]) return true;
else if (a->color[ndx] > b->color[ndx]) return false;
}
return false; // Equal.
}
bool CompareRenderBufferParamsGLES30::operator() (const RenderBufferParamsGLES30& a, const RenderBufferParamsGLES30& b) const
{
if (a.format < b.format) return true;
else if (a.format > b.format) return false;
if (a.width < b.width) return true;
else if (a.width > b.width) return false;
if (a.height < b.height) return true;
else if (a.height > b.height) return false;
return false; // Equal.
}
FramebufferObjectManagerGLES30::FramebufferObjectManagerGLES30 (void)
{
}
FramebufferObjectManagerGLES30::~FramebufferObjectManagerGLES30 (void)
{
Clear();
}
void FramebufferObjectManagerGLES30::InvalidateObjects (void)
{
// Call disown on all FBOs first.
for (FramebufferObjectMapGLES30::iterator iter = m_fboMap.begin(); iter != m_fboMap.end(); ++iter)
iter->second->Disown();
for (RenderBufferMapGLES30::iterator iter = m_rbufMap.begin(); iter != m_rbufMap.end(); ++iter)
iter->second->Disown();
// Clear objects.
Clear();
}
void FramebufferObjectManagerGLES30::Clear (void)
{
// \todo [pyry] This actually invalidates keys as well. Is that okay in clear()?
for (FramebufferObjectMapGLES30::iterator iter = m_fboMap.begin(); iter != m_fboMap.end(); ++iter)
delete iter->second;
m_fboMap.clear();
for (RenderBufferMapGLES30::iterator iter = m_rbufMap.begin(); iter != m_rbufMap.end(); ++iter)
delete iter->second;
m_rbufMap.clear();
}
void FramebufferObjectManagerGLES30::InvalidateSurface (const RenderSurfaceGLES30* surface)
{
std::vector<FramebufferObjectGLES30*> deleteFbos;
for (FramebufferObjectMapGLES30::iterator iter = m_fboMap.begin(); iter != m_fboMap.end(); ++iter)
{
if (IsInFramebufferAttachmentsGLES30(*iter->first, surface))
deleteFbos.push_back(iter->second);
}
for (std::vector<FramebufferObjectGLES30*>::iterator iter = deleteFbos.begin(); iter != deleteFbos.end(); ++iter)
{
m_fboMap.erase((*iter)->GetAttachments());
delete *iter;
}
}
FramebufferObjectGLES30* FramebufferObjectManagerGLES30::GetFramebufferObject (const FramebufferAttachmentsGLES30& attachments)
{
// Try to fetch from cache.
{
FramebufferObjectMapGLES30::const_iterator fboPos = m_fboMap.find(&attachments);
if (fboPos != m_fboMap.end())
return fboPos->second;
}
DBG_LOG_RT_GLES30("FramebufferObjectManagerGLES30::GetFramebufferObject(): cache miss, creating FBO");
// Not found - create a new one and insert.
{
FramebufferObjectGLES30* fbo = new FramebufferObjectGLES30(attachments);
m_fboMap.insert(std::make_pair(fbo->GetAttachments(), fbo));
return fbo;
}
}
RenderBufferGLES30* FramebufferObjectManagerGLES30::GetRenderBuffer (UInt32 format, int width, int height)
{
RenderBufferParamsGLES30 params(format, width, height);
{
RenderBufferMapGLES30::const_iterator pos = m_rbufMap.find(params);
if (pos != m_rbufMap.end())
return pos->second;
}
DBG_LOG_RT_GLES30("FramebufferObjectManagerGLES30::GetRenderBuffer(): cache miss, creating RBO");
{
RenderBufferGLES30* buf = new RenderBufferGLES30(format, width, height, 1);
m_rbufMap.insert(std::make_pair(params, buf));
return buf;
}
}
// Utilities
bool IsInFramebufferAttachmentsGLES30 (const FramebufferAttachmentsGLES30& attachments, const RenderSurfaceGLES30* renderSurface)
{
for (int ndx = 0; ndx < attachments.numColorAttachments; ndx++)
{
if (attachments.color[ndx] == renderSurface)
return true;
}
if (attachments.depthStencil == renderSurface)
return true;
return false;
}
void BindFramebufferObjectGLES30 (FramebufferObjectGLES30* fbo)
{
Assert(fbo != 0);
GLenum drawBuffers [FramebufferAttachmentsGLES30::kMaxColorAttachments];
GLenum discardBuffers [FramebufferAttachmentsGLES30::kMaxColorAttachments+1];
int drawBufferNdx = 0;
int discardBufferNdx = 0;
for (int ndx = 0; ndx < fbo->GetNumColorAttachments(); ndx++)
{
RenderSurfaceGLES30* attachment = fbo->GetColorAttachment(ndx);
if (attachment)
drawBuffers[drawBufferNdx++] = GL_COLOR_ATTACHMENT0+ndx;
if (attachment && (attachment->GetFlags() & RenderSurfaceGLES30::kDiscardOnBind))
{
if(fbo->GetFboID() == 0)
discardBuffers[discardBufferNdx++] = GL_COLOR;
else
discardBuffers[discardBufferNdx++] = GL_COLOR_ATTACHMENT0+ndx;
attachment->SetFlags(attachment->GetFlags() & ~RenderSurfaceGLES30::kDiscardOnBind);
}
}
if (fbo->GetDepthStencilAttachment())
{
RenderSurfaceGLES30* depthStencilAttachment = fbo->GetDepthStencilAttachment();
if (depthStencilAttachment->GetFlags() & RenderSurfaceGLES30::kDiscardOnBind && depthStencilAttachment->GetType() != RenderSurfaceGLES30::kTypeDummy)
{
if(fbo->GetFboID() == 0)
{
discardBuffers[discardBufferNdx++] = GL_DEPTH;
discardBuffers[discardBufferNdx++] = GL_STENCIL;
}
else
{
discardBuffers[discardBufferNdx++] = GL_DEPTH_ATTACHMENT;
discardBuffers[discardBufferNdx++] = GL_STENCIL_ATTACHMENT;
}
depthStencilAttachment->SetFlags(depthStencilAttachment->GetFlags() & ~RenderSurfaceGLES30::kDiscardOnBind);
}
}
GLES_CHK(glBindFramebuffer(GL_FRAMEBUFFER, fbo->GetFboID()));
GLES_CHK(glDrawBuffers(drawBufferNdx, &drawBuffers[0]));
if (discardBufferNdx > 0)
GLES_CHK(glInvalidateFramebuffer(GL_FRAMEBUFFER, discardBufferNdx, &discardBuffers[0]));
}
void BindDefaultFramebufferGLES30 (void)
{
GLuint defaultDrawBuffer = GL_BACK;
GLES_CHK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
GLES_CHK(glDrawBuffers(1, &defaultDrawBuffer));
}
FramebufferObjectGLES30* GetResolveFramebufferObjectGLES30 (FramebufferObjectManagerGLES30* fboManager, UInt32 colorFormat, UInt32 depthStencilFormat, int width, int height)
{
RenderBufferGLES30* colorBuf = colorFormat != 0 ? fboManager->GetRenderBuffer(colorFormat, width, height) : 0;
RenderBufferGLES30* depthBuf = depthStencilFormat != 0 ? fboManager->GetRenderBuffer(depthStencilFormat, width, height) : 0;
FramebufferAttachmentsGLES30 attachments;
attachments.color[0] = colorBuf;
attachments.depthStencil = depthBuf;
attachments.numColorAttachments = colorBuf ? 1 : 0;
return fboManager->GetFramebufferObject(attachments);
}
// back buffer
RenderSurfaceBase* CreateBackBufferColorSurfaceGLES3()
{
DummyRenderSurfaceGLES30* rs = new DummyRenderSurfaceGLES30(0,0);
RenderSurfaceBase_InitColor(*rs);
rs->backBuffer = true;
return rs;
}
RenderSurfaceBase* CreateBackBufferDepthSurfaceGLES3()
{
DummyRenderSurfaceGLES30* rs = new DummyRenderSurfaceGLES30(0,0);
RenderSurfaceBase_InitDepth(*rs);
rs->backBuffer = true;
return rs;
}
#endif // GFX_SUPPORTS_OPENGLES30
|