This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNVFBOBox.cpp
811 lines (732 loc) · 24.1 KB
/
NVFBOBox.cpp
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
/*
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
//
// Implementation of different antialiasing methods through FBOs
// - typical MSAA
// - CSAA
// - Hardware AA mixed with FBO for supersampling pass
// - simple downsampling
// - downsampling with 1 or 2 kernel filters
//
// NVFBOBox is the class that will handle everything related to supersampling through
// an offscreen surface defined thanks to FBO
//
//--------------------------------------------------------------------------------------
#include "nvpwindow.hpp"
#if defined(_MSC_VER)
# include <windows.h>
# include <direct.h>
#endif
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>
#include <vector>
#ifdef USE_UNMANAGED
# pragma managed(push,off)
#endif
#include "NVFBOBox.h"
#include <nvgl/extensions_gl.hpp>
//#include "Logging.h"
//#include "glError.h"
#include <map>
#include "helper_fbo.h"
/////////////////////////////////////////////
//
// Methods
//
/////////////////////////////////////////////
NVFBOBox::NVFBOBox() :
bOneFBOPerTile(false),
scaleFactor(1.0),
depthSamples(0), coverageSamples(0),
bValid(false),
vpx(0), vpy(0), vpw(0), vph(0),
bCSAA(false),
bufw(0), bufh(0),
width(0), height(0),
curtilex(0), curtiley(0),
//color_texture(0),
depth_texture(0),
//fb(0), fbms(0),
depth_texture_ms(0),
//color_texture_ms(0),
pngData(NULL),
pngDataTile(NULL),
pngDataSz(0)
{
}
NVFBOBox::~NVFBOBox()
{
pngDataSz = 0;
if(pngData)
delete []pngData;
pngData = NULL;
if(pngDataTile)
delete []pngDataTile;
pngDataTile = NULL;
}
void NVFBOBox::Finish()
{
pngDataSz = 0;
if(pngData)
delete []pngData;
pngData = NULL;
if(pngDataTile)
delete []pngDataTile;
pngDataTile = NULL;
for(unsigned int i=0; i<tileData.size(); i++)
{
if(tileData[i].color_texture_ms)
glDeleteTextures(1, &tileData[i].color_texture_ms);
if(tileData[i].color_texture)
glDeleteTextures(1, &tileData[i].color_texture);
if(tileData[i].fb)
glDeleteFramebuffers(1, &tileData[i].fb);
if(tileData[i].fbms)
glDeleteFramebuffers(1, &tileData[i].fbms);
}
tileData.clear();
if(depth_texture_ms)
glDeleteTextures(1, &depth_texture_ms);
if(depth_texture)
glDeleteTextures(1, &depth_texture);
depth_texture_ms=0;
depth_texture=0;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBox::initRT()
{
bool multisample = depthSamples > 1;
bool csaa = (coverageSamples > depthSamples) && (has_GL_NV_texture_multisample);
bool ret = true;
if(bOneFBOPerTile)
tileData.resize(tilesw*tilesh);
else
tileData.resize(1);
//loop in tiles
for(unsigned int i=0; i<tileData.size(); i++)
{
//
// init the texture that will also be the buffer to render to
//
tileData[i].color_texture = texture::createRGBA8(bufw, bufh, 1);
tileData[i].fb = fbo::create();
fbo::attachTexture2D(tileData[i].fb, tileData[i].color_texture, 0, 1);
//
// Handle multisample FBO's first
//
if (multisample)
{
//now handle the FBO in MS resolution
tileData[i].fbms = fbo::create();
// initialize color texture
tileData[i].color_texture_ms = texture::createRGBA8(bufw, bufh, depthSamples, coverageSamples);
fbo::attachTexture2D(tileData[i].fbms, tileData[i].color_texture_ms, 0, depthSamples);
// bind the multisampled depth buffer
if(depth_texture_ms == 0)
depth_texture_ms = texture::createDST(bufw, bufh, depthSamples, bCSAA ? coverageSamples:0);
fbo::attachDSTTexture2D(tileData[i].fbms, depth_texture_ms, depthSamples);
fbo::CheckStatus();
} // if (multisample)
else // Depth buffer created without the need to resolve MSAA
{
// Create it one for many FBOs
if(depth_texture == 0)
{
depth_texture = texture::createDST(bufw, bufh, 1, 0);
}
fbo::attachDSTTexture2D(tileData[i].fb, depth_texture, 1);
}
} // for i
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return ret;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBox::resize(int w, int h, float ssfact, int depthSamples_, int coverageSamples_)
{
if(depthSamples_ >= 0)
depthSamples = depthSamples_;
if(coverageSamples_ >= 0)
coverageSamples = coverageSamples_;
if(ssfact >= 1.0)
scaleFactor = ssfact;
if(w > 0)
width = w;
if(h > 0)
height = h;
bufw = (int)(scaleFactor*(float)width);
bufh = (int)(scaleFactor*(float)height);
bool multisample = depthSamples > 1;
bool csaa = (coverageSamples > depthSamples) && (has_GL_NV_texture_multisample);
bool ret = true;
if(depth_texture)
{
texture::deleteTexture(depth_texture);
depth_texture = 0;
}
if(depth_texture_ms)
{
texture::deleteTexture(depth_texture_ms);
depth_texture_ms = 0;
}
//loop in tiles
for(unsigned int i=0; i<tileData.size(); i++)
{
if(tileData[i].color_texture_ms)
texture::deleteTexture(tileData[i].color_texture_ms);
texture::deleteTexture(tileData[i].color_texture);
if(tileData[i].fbms)
{
fbo::detachColorTexture(tileData[i].fbms, 0, depthSamples);
fbo::detachDSTTexture(tileData[i].fbms, depthSamples);
}
if(tileData[i].fb)
{
fbo::detachColorTexture(tileData[i].fb, 0, depthSamples);
fbo::detachDSTTexture(tileData[i].fb, depthSamples);
}
tileData[i].color_texture = texture::createRGBA8(bufw, bufh, 1);
fbo::attachTexture2D(tileData[i].fb, tileData[i].color_texture, 0, 1);
if (multisample)
{
// initialize color texture
tileData[i].color_texture_ms = texture::createRGBA8(bufw, bufh, depthSamples, coverageSamples);
fbo::attachTexture2D(tileData[i].fbms, tileData[i].color_texture_ms, 0, depthSamples);
if(depth_texture_ms == 0)
depth_texture_ms = texture::createDST(bufw, bufh, depthSamples, bCSAA ? coverageSamples:0);
fbo::attachDSTTexture2D(tileData[i].fbms, depth_texture_ms, depthSamples);
fbo::CheckStatus();
} // if (multisample)
else // Depth buffer created without the need to resolve MSAA
{
if(depth_texture == 0)
depth_texture = texture::createDST(bufw, bufh, 1, 0);
fbo::attachDSTTexture2D(tileData[i].fb, depth_texture, 1);
fbo::CheckStatus();
}
} // for i
return ret; // TODO: return false if failed...
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBox::Initialize(int w, int h, float ssfact, int depthSamples_, int coverageSamples_, int tilesW, int tilesH, bool bOneFBOPerTile_)
{
Finish();
if(depthSamples_ >= 0)
depthSamples = depthSamples_;
if(coverageSamples_ >= 0)
coverageSamples = coverageSamples_;
if(tilesW > 0)
tilesw = tilesW;
if(tilesH > 0)
tilesh = tilesH;
if(ssfact >= 1.0)
{
scaleFactor = ssfact;
}
bValid = false;
if(w > 0)
width = w;
if(h > 0)
height = h;
bufw = (int)(scaleFactor*(float)width);
bufh = (int)(scaleFactor*(float)height);
bOneFBOPerTile = bOneFBOPerTile_;
//
// FBO
//
initRT();
//
// GLSL things
//
const char passThrough[] =
"void main(void)\n"
"{\n"
" gl_Position = gl_Vertex;\n"
" gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;\n"
"}\n";
downsampling[0].cleanup();
//downsampling[0].addVertexShader("../EffectBox/GLSLShaders/passThrough.glsl");
downsampling[0].addVertexShaderFromString(passThrough);
//downsampling[0].addFragmentShader("../EffectBox/GLSLShaders/downSample1.glsl");
downsampling[0].addFragmentShaderFromString(
"uniform sampler2D texImage;\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(texImage, gl_TexCoord[0].xy);\n"
"}\n"
);
downsampling[1].cleanup();
//downsampling[1].addVertexShader("../EffectBox/GLSLShaders/passThrough.glsl");
downsampling[1].addVertexShaderFromString(passThrough);
//downsampling[1].addFragmentShader("../EffectBox/GLSLShaders/downSample2.glsl");
downsampling[1].addFragmentShaderFromString(
"uniform sampler2D texImage;\n"
"uniform vec2 texelSize;\n"
"void main()\n"
"{\n"
" vec4 tap0 = texture2D(texImage, gl_TexCoord[0].xy);\n"
" vec4 tap1 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( 0.4, 0.9 ));\n"
" vec4 tap2 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( -0.4, -0.9 ));\n"
" vec4 tap3 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( -0.9, 0.4 ));\n"
" vec4 tap4 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( 0.9, -0.4 ));\n"
" gl_FragColor = 0.2 * ( tap0 + tap1 + tap2 + tap3 + tap4 );\n"
"}\n"
);
downsampling[2].cleanup();
//downsampling[2].addVertexShader("../EffectBox/GLSLShaders/passThrough.glsl");
downsampling[2].addVertexShaderFromString(passThrough);
//downsampling[2].addFragmentShader("../EffectBox/GLSLShaders/downSample3.glsl");
downsampling[2].addFragmentShaderFromString(
"uniform sampler2D texImage;\n"
"uniform vec2 texelSize;\n"
"void main()\n"
"{\n"
" vec4 color, color2;\n"
" vec4 tap0 = texture2D(texImage, gl_TexCoord[0].xy);\n"
" vec4 tap1 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( 0.4, 0.9 ));\n"
" vec4 tap2 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( -0.4, -0.9 ));\n"
" vec4 tap3 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( -0.9, 0.4 ));\n"
" vec4 tap4 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( 0.9, -0.4 ));\n"
" color = 0.2 * ( tap0 + tap1 + tap2 + tap3 + tap4 );\n"
" vec4 tap11 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( 0.9, 1.9 ));\n"
" vec4 tap21 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( -0.9, -1.9 ));\n"
" vec4 tap31 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( -1.9, 0.9 ));\n"
" vec4 tap41 = texture2D(texImage, gl_TexCoord[0].xy + texelSize * vec2( 1.9, -0.9 ));\n"
" color2 = 0.2 * ( tap0 + tap11 + tap21 + tap31 + tap41 );\n"
" float mask = clamp(color2.w, 0.0, 1.0);\n"
" gl_FragColor.rgb = color.rgb * mask + color2.rgb * (1.0-mask); \n"
" gl_FragColor.w = mask; \n"
"}\n"
);
bValid = true;
return true;
}
void NVFBOBox::MakeResourcesResident()
{
GLuint64 handle;
for(unsigned int i=0; i<tileData.size(); i++)
{
if(tileData[i].color_texture_ms)
{
handle = glGetTextureHandleARB(tileData[i].color_texture_ms);
glMakeTextureHandleResidentARB(handle);
}
if(tileData[i].color_texture)
{
handle = glGetTextureHandleARB(tileData[i].color_texture);
glMakeTextureHandleResidentARB(handle);
}
}
if(depth_texture_ms)
{
handle = glGetTextureHandleARB(depth_texture_ms);
glMakeTextureHandleResidentARB(handle);
}
if(depth_texture)
{
handle = glGetTextureHandleARB(depth_texture);
glMakeTextureHandleResidentARB(handle);
}
}
#if 0
#define FULLSCRQUAD(x,y)\
float ww = 2.0f/(float)tilesw;\
float hh = 2.0f/(float)tilesh;\
float xx = (x*ww)-1;\
float yy = (y*hh)-1;\
glBegin(GL_QUADS);\
glTexCoord2f(0,0);\
glVertex4f(xx, yy, 0.0,1);\
glTexCoord2f(1,0);\
glVertex4f(xx+ww, yy,0.0,1);\
glTexCoord2f(1,1);\
glVertex4f(xx+ww, yy+hh,0.0,1);\
glTexCoord2f(0,1);\
glVertex4f(xx, yy+hh,0.0,1);\
glEnd();
#else
#define FULLSCRQUAD(x,y)
#endif
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
bool NVFBOBox::ResolveAA(DownSamplingTechnique technique=DS1, int tilex=0, int tiley=0)
{
if(!bValid)
return false;
if(tilex < 0)
tilex = curtilex;
else
curtilex = tilex;
if(tiley < 0)
tiley = curtiley;
else
curtiley = tiley;
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
//
// if this FBO is multisampled, resolve it, so it can be displayed
// the blit will allow the multisampled buffer to be stretched to a normal buffer at res bufw/bufh
//
int i = bOneFBOPerTile ? tilesw * tiley + tilex : 0;
bool toBackBuffer = false;
if( depthSamples > 1 )
{
glBindFramebuffer( GL_READ_FRAMEBUFFER, tileData[i].fbms);
// scalefactor == 1.0 means that we simply need to blit things with no filtering to the backbuffer
// We also want to not have any tiling...
if((scaleFactor ==1.0) && (tilesw == 1) && (tilesh == 1))
toBackBuffer = true;
glBindFramebuffer( GL_DRAW_FRAMEBUFFER,
toBackBuffer ? 0 : tileData[i].fb);
glBlitFramebuffer( 0, 0, bufw, bufh, 0, 0, bufw, bufh, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
return toBackBuffer;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void NVFBOBox::Draw(DownSamplingTechnique technique, int tilex, int tiley, int windowW, int windowH, float *offset)
{
if(tilex < 0)
tilex = curtilex;
else
curtilex = tilex;
if(tiley < 0)
tiley = curtiley;
else
curtiley = tiley;
bool toBackBuffer = ResolveAA(technique, tilex, tiley);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// we will go through a fullscreen quad with shader if tiling or scalefactor > 1
if((scaleFactor > 1.0) || (tilesw > 1) || (tilesh > 1))
{
assert(toBackBuffer == false);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if(technique < 0) //Fallback
technique = DS1;
downsampling[technique].bindShader();
downsampling[technique].bindTexture(GL_TEXTURE_2D, "texImage", tileData[bOneFBOPerTile ? tilesw * tiley + tilex : 0].color_texture, 0);
float v2[2] = { 1.0f/(float)bufw, 1.0f/(float)bufh };
downsampling[technique].setUniformVector("texelSize", v2, 2);
glDepthMask(false);
//PRINT_GL_ERROR;
//
// During this full screen pass, we will down-sample the buffer and
// eventually filter it
//
//FULLSCRQUAD(tilex, tiley);
float pw = 2.0f/(float)windowW;
float ph = 2.0f/(float)windowH;
float ww = (width * pw);
float hh = (height * ph);
float xx = ((((float)tilex - 0.5f*(float)tilesw)*(float)width) * pw);
float yy = ((((float)tiley - 0.5f*(float)tilesh)*(float)height) * ph);
if(offset)
{
xx += offset[0];
yy += offset[1];
}
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex4f(xx, yy, 0.0,1);
glTexCoord2f(1,0);
glVertex4f(xx+ww, yy,0.0,1);
glTexCoord2f(1,1);
glVertex4f(xx+ww, yy+hh,0.0,1);
glTexCoord2f(0,1);
glVertex4f(xx, yy+hh,0.0,1);
glEnd();
//PRINT_GL_ERROR;
downsampling[technique].unbindShader();
glDepthMask(true);
glEnable(GL_DEPTH_TEST);
}
else if(!toBackBuffer)// simple case : copy the data to the backbuffer
{
//
// WARNING: CREATES a MEMORY LEAK!!!
//
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
glBindFramebuffer( GL_READ_FRAMEBUFFER, tileData[0].fb);
// scalefactor == 1.0 means that we simply need to blit things with no filtering to the backbuffer
// We also want to not have any tiling...
glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer( 0, 0, bufw, bufh, 0, 0, bufw, bufh, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void NVFBOBox::ActivateBuffer(int tilex, int tiley, GLenum target)
{
if(!bValid)
return;
if(tilex < 0)
tilex = curtilex;
else
curtilex = tilex;
if(tiley < 0)
tiley = curtiley;
else
curtiley = tiley;
int i = bOneFBOPerTile ? tilesw * tiley + tilex : 0;
glBindFramebuffer(target, depthSamples > 1 ? tileData[i].fbms : tileData[i].fb);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
if(GL_FRAMEBUFFER == target)
{
glViewport(0, 0, bufw, bufh);
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void NVFBOBox::Activate(int tilex, int tiley, float m_frustum[][4])
{
if(!bValid)
return;
if(tilex < 0)
tilex = curtilex;
else
curtilex = tilex;
if(tiley < 0)
tiley = curtiley;
else
curtiley = tiley;
//
// Bind the framebuffer to render on : can be either Multisampled one or normal one
//
int i = bOneFBOPerTile ? tilesw * tiley + tilex : 0;
glBindFramebuffer(GL_FRAMEBUFFER, depthSamples > 1 ? tileData[i].fbms : tileData[i].fb);
glPushAttrib(GL_VIEWPORT_BIT);
glEnable(GL_MULTISAMPLE);
glViewport(0, 0, bufw, bufh);
//
// Change the projection matrix
//
// Do this only if needed : when in tiling mode
if((tilesw > 1)||(tilesh > 1))
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
float proj[16];
float matTiling[16] = {
(float)tilesw,0.0f ,0.0f,0.0f,
0.0f ,(float)tilesh ,0.0f,0.0f,
0.0f ,0.0f ,1.0f,0.0f,
(float)tilesw -1.0f - 2.0f*(float)tilex,
(float)tilesh -1.0f - 2.0f*(float)tiley,
0,1
};
glGetFloatv(GL_PROJECTION_MATRIX, proj);
glLoadMatrixf(matTiling);
glMultMatrixf(proj);
glMatrixMode(GL_MODELVIEW);
// Clipping plane computation according to the tile we are in
float mview[16];
float clip[16];
tilex = tilesw-1-tilex;
tiley = tilesh-1-tiley;
glGetFloatv(GL_MODELVIEW_MATRIX, mview);
glPushMatrix();
glLoadMatrixf(proj);
glMultMatrixf(mview);
glGetFloatv(GL_MODELVIEW_MATRIX, clip);
glPopMatrix();
float clipedges[6][2] = {
{-1.f, 1.f-(2.f*(float)tilex/(float)tilesw)},
{1.f, (2.f*((float)tilex+1.f)/(float)tilesw) - 1.f},
{-1.f, 1.f-(2.f*(float)tiley/(float)tilesh)},
{1.f, (2.f*((float)tiley+1.f)/(float)tilesh) - 1.f},
{-1.f, 1.0f}, {1.f, 1.0f} };
for(int i = 0; i < 6; ++i)
{
int axis = i / 2;
for(int j = 0; j < 4; ++j)
m_frustum[i][j] =
clipedges[i][0]*clip[j*4+axis] // Directional vector of plane eq.
+ clipedges[i][1]*clip[j*4+3]; // offset of plane eq.
//Normalize the plane
float invLength = 1.f / sqrtf((m_frustum[i][0]*m_frustum[i][0])+(m_frustum[i][1]*m_frustum[i][1])+(m_frustum[i][2]*m_frustum[i][2]));
for(int j = 0; j < 4; ++j)
m_frustum[i][j] *= invLength;
}
}
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void NVFBOBox::Deactivate()
{
if(!bValid)
return;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glPopAttrib();
// Do this only if needed : when in tiling mode
if((tilesw > 1)||(tilesh > 1))
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
}
/*-------------------------------------------------------------------------
# of tiles in W and H - doesn't return the width and height...
-------------------------------------------------------------------------*/
int NVFBOBox::getTilesW()
{
return tilesw;
}
int NVFBOBox::getTilesH()
{
return tilesh;
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
#ifdef USEPNG
#include "png.h"
void pngWriteFn( png_structp png_ptr, png_bytep data, png_size_t length) {
FILE* fp = (FILE*)png_get_io_ptr(png_ptr);
if (!data)
png_error( png_ptr, "Attempt to write to null file pointer");
fwrite( data, length, 1, fp);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void pngFlushFn( png_structp png_ptr) {
FILE* fp = (FILE*)png_get_io_ptr(png_ptr);
fflush(fp);
}
/*-------------------------------------------------------------------------
-------------------------------------------------------------------------*/
void errorFn (png_structp p, png_const_charp pchr)
{
LOGE("Error : %s\n", pchr);
}
#endif
/*-------------------------------------------------------------------------
- Note: DownSamplingTechnique technique currently not used...
In fact, the kernel filtering for downsampling is not used at all when in tiling mode.
The downsampling is only used when doing a Fullscreen quad rendering in a render target...
This is only done in Draw(), For now. Not sure this is needed for our tiling stuff.
However, MSAA is working as expected.
-------------------------------------------------------------------------*/
void NVFBOBox::PngWriteData(DownSamplingTechnique technique, int tilex, int tiley)
{
#ifdef USEPNG
int bit_depth = 8;
int color_type = PNG_COLOR_TYPE_RGB;
int row_bytes = bufw * 3 * (bit_depth >> 3);
if(tilex < 0)
tilex = curtilex;
else
curtilex = tilex;
if(tiley < 0)
tiley = curtiley;
else
curtiley = tiley;
if((pngDataSz < (row_bytes * bufh * tilesw * tilesh))||(!pngData))
{
if(pngData) delete []pngData;
pngData = new GLubyte[row_bytes * bufh * tilesw * tilesh];
//zeromemory(pngData, row_bytes * bufh * tilesw * tilesh);
pngDataSz = row_bytes * bufh * tilesw * tilesh;
}
if(!pngDataTile)
{
pngDataTile = new GLubyte[row_bytes * bufh];
//zeromemory(pngData, row_bytes * bufh * tilesh);
}
// Shall we do ResolveAA(DownSamplingTechnique technique=DS1, int tilex=0, int tiley=0) ?
// Right now : previous Draw() call made it...
glBindFramebuffer(GL_FRAMEBUFFER, tileData[bOneFBOPerTile ? tilex + tiley*tilesw : 0].fb);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(0,0,bufw,bufh,GL_RGB,GL_UNSIGNED_BYTE, pngDataTile);
// Maybe we should put back the state that currently was before I changed it...
glBindFramebuffer(GL_FRAMEBUFFER, 0);
for(int y=0; y<bufh; y++)
{
memcpy(pngData + tilex*row_bytes + (bufh*tiley+y)*row_bytes*tilesw,
pngDataTile + y*row_bytes, row_bytes);
}
#endif
}
/*-------------------------------------------------------------------------
// writePng
//
// Image saver function for png files. The code is heavily
// based on the example png save code distributed with libPNG
-------------------------------------------------------------------------*/
#if 1
static int counter = 0;
bool NVFBOBox::PngWriteFile( const char *file)
{
#ifdef USEPNG
int bit_depth = 8;
int color_type = PNG_COLOR_TYPE_RGB;
int row_bytes = tilesw * bufw * 3 * (bit_depth >> 3);
if((pngDataSz < (row_bytes * bufh * tilesh))||(!pngData))
return false;
char tmpname[100];
sprintf(tmpname, "%s_%d.png", file, counter++);
FILE *fp = fopen( tmpname, "wb");
if ( !fp)
return false;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, &errorFn, NULL);
if (!png_ptr) {
fclose(fp);
return false; /* out of memory */
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return false; /* out of memory */
}
// setjmp() is used for error handling with libPNG, if something goes wrong it is coming back here
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return false;
}
// Need to override the standard I/O methods since libPNG may be linked against a different run-time
png_set_write_fn( png_ptr, fp, pngWriteFn, pngFlushFn);
png_set_IHDR(png_ptr, info_ptr, tilesw*bufw, tilesh*bufh, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png_ptr, info_ptr);
for ( int ii = 0; ii < tilesh*bufh; ii++)
{
png_write_row(png_ptr, pngData + (tilesh*bufh - 1 - ii)*row_bytes);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
#else
return false;
#endif
}
unsigned int NVFBOBox::GetFBO(int i)
{
return depthSamples > 1 ? tileData[i].fbms : tileData[i].fb;
}
#endif