Menu â–¾ â–´

[r5769]: / G3D10 / GLG3D.lib / source / DefaultRenderer.cpp  Maximize  Restore  History

Download this file

237 lines (178 with data), 11.0 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
/**
\file GLG3D/DefaultRenderer.cpp
\maintainer Morgan McGuire, http://graphics.cs.williams.edu
\created 2014-12-21
\edited 2015-01-20
Copyright 2000-2015, Morgan McGuire.
All rights reserved.
*/
#include "GLG3D/DefaultRenderer.h"
#include "GLG3D/RenderDevice.h"
#include "GLG3D/Framebuffer.h"
#include "GLG3D/LightingEnvironment.h"
#include "GLG3D/Camera.h"
#include "GLG3D/Surface.h"
#include "GLG3D/AmbientOcclusion.h"
#include "GLG3D/SkyboxSurface.h"
#include "GLG3D/GApp.h"
namespace G3D {
void DefaultRenderer::render
(RenderDevice* rd,
const shared_ptr<Framebuffer>& framebuffer,
const shared_ptr<Framebuffer>& depthPeelFramebuffer,
LightingEnvironment& lightingEnvironment,
const shared_ptr<GBuffer>& gbuffer,
const Array<shared_ptr<Surface>>& allSurfaces) {
alwaysAssertM(! lightingEnvironment.ambientOcclusionSettings.enabled || notNull(lightingEnvironment.ambientOcclusion),
"Ambient occlusion is enabled but no ambient occlusion object is bound to the lighting environment");
const shared_ptr<Camera>& camera = gbuffer->camera();
// Share the depth buffer with the forward-rendering pipeline
framebuffer->set(Framebuffer::DEPTH, gbuffer->texture(GBuffer::Field::DEPTH_AND_STENCIL));
depthPeelFramebuffer->resize(framebuffer->width(), framebuffer->height());
// Cull and sort
Array<shared_ptr<Surface> > sortedVisibleSurfaces, forwardOpaqueSurfaces, forwardBlendedSurfaces;
cullAndSort(rd, gbuffer, allSurfaces, sortedVisibleSurfaces, forwardOpaqueSurfaces, forwardBlendedSurfaces);
// Bind the main framebuffer
rd->pushState(framebuffer); {
rd->clear();
rd->setProjectionAndCameraMatrix(camera->projection(), camera->frame());
const bool needDepthPeel = lightingEnvironment.ambientOcclusionSettings.useDepthPeelBuffer;
computeGBuffer(rd, sortedVisibleSurfaces, gbuffer, needDepthPeel ? depthPeelFramebuffer : shared_ptr<Framebuffer>(), lightingEnvironment.ambientOcclusionSettings.depthPeelSeparationHint);
// Shadowing + AO
computeShadowing(rd, allSurfaces, gbuffer, depthPeelFramebuffer, lightingEnvironment);
// Maybe launch deferred pass
if (deferredShading()) {
renderDeferredShading(rd, gbuffer, lightingEnvironment);
}
// Main forward pass
renderOpaqueSamples(rd, deferredShading() ? forwardOpaqueSurfaces : sortedVisibleSurfaces, gbuffer, lightingEnvironment);
// Prepare screen-space lighting for the *next* frame
lightingEnvironment.copyScreenSpaceBuffers(framebuffer, gbuffer->colorGuardBandThickness());
renderOpaqueScreenSpaceRefractingSamples(rd, deferredShading() ? forwardOpaqueSurfaces : sortedVisibleSurfaces, gbuffer, lightingEnvironment);
// Samples that require blending
if (m_orderIndependentTransparency) {
renderOrderIndependentBlendedSamples(rd, forwardBlendedSurfaces, gbuffer, lightingEnvironment);
} else {
renderSortedBlendedSamples(rd, forwardBlendedSurfaces, gbuffer, lightingEnvironment);
}
} rd->popState();
}
void DefaultRenderer::renderDeferredShading(RenderDevice* rd, const shared_ptr<GBuffer>& gbuffer, const LightingEnvironment& environment) {
// Make a pass over the screen, performing shading
rd->push2D(); {
rd->setGuardBandClip2D(gbuffer->colorGuardBandThickness());
// Don't shade the skybox on this pass because it will be forward rendered
rd->setDepthTest(RenderDevice::DEPTH_GREATER);
Args args;
environment.setShaderArgs(args);
gbuffer->setShaderArgsRead(args, "gbuffer_");
args.setRect(rd->viewport());
LAUNCH_SHADER("DefaultRenderer_deferredShade.pix", args);
} rd->pop2D();
}
void DefaultRenderer::renderOpaqueSamples
(RenderDevice* rd,
Array<shared_ptr<Surface> >& surfaceArray,
const shared_ptr<GBuffer>& gbuffer,
const LightingEnvironment& environment) {
//screenPrintf("renderOpaqueSamples: %d", surfaceArray.length());
BEGIN_PROFILER_EVENT("DefaultRenderer::renderOpaqueSamples");
forwardShade(rd, surfaceArray, gbuffer, environment, RenderPassType::OPAQUE_SAMPLES, Surface::defaultWritePixelDeclaration(), ARBITRARY);
END_PROFILER_EVENT();
}
void DefaultRenderer::renderOpaqueScreenSpaceRefractingSamples
(RenderDevice* rd,
Array<shared_ptr<Surface> >& surfaceArray,
const shared_ptr<GBuffer>& gbuffer,
const LightingEnvironment& environment) {
BEGIN_PROFILER_EVENT("DefaultRenderer::renderOpaqueScreenSpaceRefractingSamples");
//screenPrintf("renderOpaqueScreenSpaceRefractingSamples: %d", surfaceArray.length());
forwardShade(rd, surfaceArray, gbuffer, environment, RenderPassType::OPAQUE_SAMPLES_WITH_SCREEN_SPACE_REFRACTION, Surface::defaultWritePixelDeclaration(), ARBITRARY);
END_PROFILER_EVENT();
}
void DefaultRenderer::renderSortedBlendedSamples
(RenderDevice* rd,
Array<shared_ptr<Surface> >& surfaceArray,
const shared_ptr<GBuffer>& gbuffer,
const LightingEnvironment& environment) {
BEGIN_PROFILER_EVENT("DefaultRenderer::renderSortedBlendedSamples");
//screenPrintf("renderBlendedSamples: %d", surfaceArray.length());
forwardShade(rd, surfaceArray, gbuffer, environment, RenderPassType::MULTIPASS_BLENDED_SAMPLES, Surface::defaultWritePixelDeclaration(), BACK_TO_FRONT);
END_PROFILER_EVENT();
}
void DefaultRenderer::renderOrderIndependentBlendedSamples
(RenderDevice* rd,
Array<shared_ptr<Surface> >& surfaceArray,
const shared_ptr<GBuffer>& gbuffer,
const LightingEnvironment& environment) {
BEGIN_PROFILER_EVENT("DefaultRenderer::renderOrderIndependentBlendedSamples");
if (surfaceArray.size() > 0) {
//screenPrintf("renderOrderIndependentBlendedSamples: %d", surfaceArray.length());
// Do we need to allocate the OIT buffers?
if (isNull(m_oitFramebuffer)) {
m_oitFramebuffer = Framebuffer::create("G3D::DefaultRenderer::m_oitFramebuffer");
m_oitFramebuffer->set(Framebuffer::COLOR0, Texture::createEmpty("G3D::DefaultRenderer accum", rd->width(), rd->height(), ImageFormat::RGBA16F()));
// This could use R8 as well to reduce bandwidth
const shared_ptr<Texture>& texture = Texture::createEmpty("G3D::DefaultRenderer revealage", rd->width(), rd->height(), ImageFormat::R16F());
texture->visualization.channels = Texture::Visualization::RasL;
m_oitFramebuffer->set(Framebuffer::COLOR1, texture);
m_oitFramebuffer->setClearValue(Framebuffer::COLOR0, Color4::zero());
m_oitFramebuffer->setClearValue(Framebuffer::COLOR1, Color4::one());
}
// Do we need to resize the OIT buffers?
if ((m_oitFramebuffer->width() != rd->width()) ||
(m_oitFramebuffer->height() != rd->height())) {
m_oitFramebuffer->texture(Framebuffer::COLOR0)->resize(rd->width(), rd->height());
m_oitFramebuffer->texture(Framebuffer::COLOR1)->resize(rd->width(), rd->height());
}
m_oitFramebuffer->set(Framebuffer::DEPTH, rd->drawFramebuffer()->texture(Framebuffer::DEPTH));
////////////////////////////////////////////////////////////////////////////////////
//
// 3D accumulation pass over transparent surfaces
//
// The following must not contain newlines because it is a macro
static const String oitWriteDeclaration = STR(
layout(location = 0) out float4 _accum;
layout(location = 1) out float _revealage;
void writePixel(vec4 premultipliedReflect, vec3 transmit, float csZ) {
/* Modulate the net coverage for composition by the transmission. This does not affect the color channels of the
transparent surface because the caller's BSDF model should have already taken into account if transmission modulates
reflection. This model doesn't handled colored transmission, so it averages the color channels. See
McGuire and Enderton, Colored Stochastic Shadow Maps, ACM I3D, February 2011
http://graphics.cs.williams.edu/papers/CSSM/
for a full explanation and derivation.*/
premultipliedReflect.a *= 1.0 - clamp((transmit.r + transmit.g + transmit.b) * (1.0 / 3.0), 0, 1);
// Intermediate terms to be cubed
float a = min(1.0, premultipliedReflect.a) * 8.0 + 0.01;
float b = -gl_FragCoord.z * 0.95 + 1.0;
/* If a lot of the scene is close to the far plane, then gl_FragCoord.z does not
provide enough discrimination. Add this term to compensate:
b /= sqrt(abs(csZ)); */
float w = clamp(a * a * a * 1e3 * b * b * b, 1e-2, 3e2);
_accum = premultipliedReflect * w;
_revealage = premultipliedReflect.a;
});
rd->pushState(m_oitFramebuffer); {
rd->clearFramebuffer(true, false);
// Set blending modes
rd->setBlendFunc(RenderDevice::BLEND_ONE, RenderDevice::BLEND_ONE, RenderDevice::BLENDEQ_ADD, RenderDevice::BLENDEQ_SAME_AS_RGB, Framebuffer::COLOR0);
rd->setBlendFunc(RenderDevice::BLEND_ZERO, RenderDevice::BLEND_ONE_MINUS_SRC_COLOR, RenderDevice::BLENDEQ_ADD, RenderDevice::BLENDEQ_SAME_AS_RGB, Framebuffer::COLOR1);
forwardShade(rd, surfaceArray, gbuffer, environment, RenderPassType::SINGLE_PASS_UNORDERED_BLENDED_SAMPLES, oitWriteDeclaration, ARBITRARY);
} rd->popState();
////////////////////////////////////////////////////////////////////////////////////
//
// 2D compositing pass
//
rd->push2D(); {
rd->setDepthTest(RenderDevice::DEPTH_ALWAYS_PASS);
rd->setBlendFunc(RenderDevice::BLEND_SRC_ALPHA, RenderDevice::BLEND_ONE_MINUS_SRC_ALPHA);
Args args;
args.setUniform("accumTexture", m_oitFramebuffer->texture(0), Sampler::buffer());
args.setUniform("revealageTexture", m_oitFramebuffer->texture(1), Sampler::buffer());
args.setRect(rd->viewport());
LAUNCH_SHADER("DefaultRenderer_compositeWeightedBlendedOIT.pix", args);
} rd->pop2D();
}
END_PROFILER_EVENT();
}
} //namespace

Oh no! Some styles failed to load. 😵

Please try reloading this page

Get latest updates about Open Source Projects, Conferences and News.

Sign Up No, Thank you