History

Merged with master

Olivier Prat committed 8 years ago

Commit f5a25572 parents 92e1fe8 0ef27941213 files · +53263 −55587Browse files

Showing the changes to libraries/render-utils/src/DeferredLightingEffect.cpp (1 of 1213 files). Show all

Unified · Split

libraries/render-utils/src/DeferredLightingEffect.cpp+82−58
@@ -11,9 +11,12 @@ · 10 unmodified lines
1111
1212 #include "DeferredLightingEffect.h"
1313
14+#include <QtCore/QFileSelector>
15+
1416 #include <GLMHelpers.h>
1517 #include <PathUtils.h>
1618 #include <ViewFrustum.h>
19+#include <shared/FileUtils.h>
1720
1821 #include <gpu/Batch.h>
1922 #include <gpu/Context.h>
@@ -42,6 +45,7 @@ using namespace render; · 22 unmodified lines
4245
4346 struct LightLocations {
4447 int radius{ -1 };
48+ int keyLightBufferUnit{ -1 };
4549 int lightBufferUnit{ -1 };
4650 int ambientBufferUnit { -1 };
4751 int lightIndexBufferUnit { -1 };
@@ -58,7 +62,7 @@ enum DeferredShader_MapSlot { · 10 unmodified lines
5862 DEFERRED_BUFFER_DEPTH_UNIT = 3,
5963 DEFERRED_BUFFER_OBSCURANCE_UNIT = 4,
6064 SHADOW_MAP_UNIT = 5,
61− SKYBOX_MAP_UNIT = 6,
65+ SKYBOX_MAP_UNIT = SHADOW_MAP_UNIT + SHADOW_CASCADE_MAX_COUNT,
6266 DEFERRED_BUFFER_LINEAR_DEPTH_UNIT,
6367 DEFERRED_BUFFER_CURVATURE_UNIT,
6468 DEFERRED_BUFFER_DIFFUSED_CURVATURE_UNIT,
@@ -71,15 +75,15 @@ enum DeferredShader_BufferSlot { · 6 unmodified lines
7175 SCATTERING_PARAMETERS_BUFFER_SLOT,
7276 LIGHTING_MODEL_BUFFER_SLOT = render::ShapePipeline::Slot::LIGHTING_MODEL,
7377 LIGHT_GPU_SLOT = render::ShapePipeline::Slot::LIGHT,
74− LIGHT_AMBIENT_SLOT,
78+ LIGHT_AMBIENT_SLOT = render::ShapePipeline::Slot::LIGHT_AMBIENT_BUFFER,
79+ HAZE_MODEL_BUFFER_SLOT = render::ShapePipeline::Slot::HAZE_MODEL,
7580 LIGHT_INDEX_GPU_SLOT,
7681 LIGHT_CLUSTER_GRID_FRUSTUM_GRID_SLOT,
7782 LIGHT_CLUSTER_GRID_CLUSTER_GRID_SLOT,
7883 LIGHT_CLUSTER_GRID_CLUSTER_CONTENT_SLOT,
79− HAZE_MODEL_BUFFER_SLOT
8084 };
8185
82−static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocationsPtr& locations);
86+static void loadLightProgram(const gpu::ShaderPointer& vertShader, const gpu::ShaderPointer& fragShader, bool lightVolume, gpu::PipelinePointer& program, LightLocationsPtr& locations);
8387
8488 void DeferredLightingEffect::init() {
8589 _directionalAmbientSphereLightLocations = std::make_shared<LightLocations>();
@@ -91,25 +95,25 @@ void DeferredLightingEffect::init() { · 5 unmodified lines
9195 _localLightLocations = std::make_shared<LightLocations>();
9296 _localLightOutlineLocations = std::make_shared<LightLocations>();
9397
94− loadLightProgram(deferred_light_vert, directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
95− loadLightProgram(deferred_light_vert, directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
98+ loadLightProgram(deferred_light_vert::getShader(), directional_ambient_light_frag::getShader(), false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations);
99+ loadLightProgram(deferred_light_vert::getShader(), directional_skybox_light_frag::getShader(), false, _directionalSkyboxLight, _directionalSkyboxLightLocations);
96100
97− loadLightProgram(deferred_light_vert, directional_ambient_light_shadow_frag, false, _directionalAmbientSphereLightShadow, _directionalAmbientSphereLightShadowLocations);
98− loadLightProgram(deferred_light_vert, directional_skybox_light_shadow_frag, false, _directionalSkyboxLightShadow, _directionalSkyboxLightShadowLocations);
101+ loadLightProgram(deferred_light_vert::getShader(), directional_ambient_light_shadow_frag::getShader(), false, _directionalAmbientSphereLightShadow, _directionalAmbientSphereLightShadowLocations);
102+ loadLightProgram(deferred_light_vert::getShader(), directional_skybox_light_shadow_frag::getShader(), false, _directionalSkyboxLightShadow, _directionalSkyboxLightShadowLocations);
99103
100− loadLightProgram(deferred_light_vert, local_lights_shading_frag, true, _localLight, _localLightLocations);
101− loadLightProgram(deferred_light_vert, local_lights_drawOutline_frag, true, _localLightOutline, _localLightOutlineLocations);
104+ loadLightProgram(deferred_light_vert::getShader(), local_lights_shading_frag::getShader(), true, _localLight, _localLightLocations);
105+ loadLightProgram(deferred_light_vert::getShader(), local_lights_drawOutline_frag::getShader(), true, _localLightOutline, _localLightOutlineLocations);
102106 }
103107
104108 void DeferredLightingEffect::setupKeyLightBatch(const RenderArgs* args, gpu::Batch& batch, int lightBufferUnit, int ambientBufferUnit, int skyboxCubemapUnit) {
105109 PerformanceTimer perfTimer("DLE->setupBatch()");
106− model::LightPointer keySunLight;
110+ graphics::LightPointer keySunLight;
107111 auto lightStage = args->_scene->getStage<LightStage>();
108112 if (lightStage) {
109113 keySunLight = lightStage->getCurrentKeyLight();
110114 }
111115
112− model::LightPointer keyAmbiLight;
116+ graphics::LightPointer keyAmbiLight;
113117 if (lightStage) {
114118 keyAmbiLight = lightStage->getCurrentAmbientLight();
115119 }
@@ -144,19 +148,38 @@ void DeferredLightingEffect::unsetKeyLightBatch(gpu::Batch& batch, int lightBuff · 28 unmodified lines
144148 }
145149 }
146150
147−static gpu::ShaderPointer makeLightProgram(const char* vertSource, const char* fragSource, LightLocationsPtr& locations) {
148− auto VS = gpu::Shader::createVertex(std::string(vertSource));
149− auto PS = gpu::Shader::createPixel(std::string(fragSource));
150−
151− gpu::ShaderPointer program = gpu::Shader::createProgram(VS, PS);
151+void DeferredLightingEffect::setupLocalLightsBatch(gpu::Batch& batch,
152+ int clusterGridBufferUnit, int clusterContentBufferUnit, int frustumGridBufferUnit,
153+ const LightClustersPointer& lightClusters) {
154+ // Bind the global list of lights and the visible lights this frame
155+ batch.setUniformBuffer(_localLightLocations->lightBufferUnit, lightClusters->_lightStage->getLightArrayBuffer());
156+
157+ batch.setUniformBuffer(frustumGridBufferUnit, lightClusters->_frustumGridBuffer);
158+ batch.setUniformBuffer(clusterGridBufferUnit, lightClusters->_clusterGridBuffer);
159+ batch.setUniformBuffer(clusterContentBufferUnit, lightClusters->_clusterContentBuffer);
160+}
152161
162+void DeferredLightingEffect::unsetLocalLightsBatch(gpu::Batch& batch, int clusterGridBufferUnit, int clusterContentBufferUnit, int frustumGridBufferUnit) {
163+ if (clusterGridBufferUnit >= 0) {
164+ batch.setUniformBuffer(clusterGridBufferUnit, nullptr);
165+ }
166+ if (clusterContentBufferUnit >= 0) {
167+ batch.setUniformBuffer(clusterContentBufferUnit, nullptr);
168+ }
169+ if (frustumGridBufferUnit >= 0) {
170+ batch.setUniformBuffer(frustumGridBufferUnit, nullptr);
171+ }
172+}
173+
174+static gpu::ShaderPointer makeLightProgram(const gpu::ShaderPointer& vertShader, const gpu::ShaderPointer& fragShader, LightLocationsPtr& locations) {
175+ gpu::ShaderPointer program = gpu::Shader::createProgram(vertShader, fragShader);
153176 gpu::Shader::BindingSet slotBindings;
154177 slotBindings.insert(gpu::Shader::Binding(std::string("colorMap"), DEFERRED_BUFFER_COLOR_UNIT));
155178 slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), DEFERRED_BUFFER_NORMAL_UNIT));
156179 slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), DEFERRED_BUFFER_EMISSIVE_UNIT));
157180 slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), DEFERRED_BUFFER_DEPTH_UNIT));
158181 slotBindings.insert(gpu::Shader::Binding(std::string("obscuranceMap"), DEFERRED_BUFFER_OBSCURANCE_UNIT));
159− slotBindings.insert(gpu::Shader::Binding(std::string("shadowMap"), SHADOW_MAP_UNIT));
182+ slotBindings.insert(gpu::Shader::Binding(std::string("shadowMaps"), SHADOW_MAP_UNIT));
160183 slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), SKYBOX_MAP_UNIT));
161184
162185 slotBindings.insert(gpu::Shader::Binding(std::string("linearZeyeMap"), DEFERRED_BUFFER_LINEAR_DEPTH_UNIT));
@@ -186,6 +209,7 @@ static gpu::ShaderPointer makeLightProgram(const char* vertSource, const char* f · 23 unmodified lines
186209
187210 locations->texcoordFrameTransform = program->getUniforms().findLocation("texcoordFrameTransform");
188211
212+ locations->keyLightBufferUnit = program->getUniformBuffers().findLocation("keyLightBuffer");
189213 locations->lightBufferUnit = program->getUniformBuffers().findLocation("lightBuffer");
190214 locations->ambientBufferUnit = program->getUniformBuffers().findLocation("lightAmbientBuffer");
191215 locations->lightIndexBufferUnit = program->getUniformBuffers().findLocation("lightIndexBuffer");
@@ -196,9 +220,9 @@ static gpu::ShaderPointer makeLightProgram(const char* vertSource, const char* f · 4 unmodified lines
196220 return program;
197221 }
198222
199−static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocationsPtr& locations) {
223+static void loadLightProgram(const gpu::ShaderPointer& vertShader, const gpu::ShaderPointer& fragShader, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocationsPtr& locations) {
200224
201− gpu::ShaderPointer program = makeLightProgram(vertSource, fragSource, locations);
225+ gpu::ShaderPointer program = makeLightProgram(vertShader, fragShader, locations);
202226
203227 auto state = std::make_shared<gpu::State>();
204228 state->setColorWriteMask(true, true, true, false);
@@ -229,9 +253,9 @@ static void loadLightProgram(const char* vertSource, const char* fragSource, boo · 24 unmodified lines
229253
230254 #include <shared/Shapes.h>
231255
232−model::MeshPointer DeferredLightingEffect::getPointLightMesh() {
256+graphics::MeshPointer DeferredLightingEffect::getPointLightMesh() {
233257 if (!_pointLightMesh) {
234− _pointLightMesh = std::make_shared<model::Mesh>();
258+ _pointLightMesh = std::make_shared<graphics::Mesh>();
235259
236260 // let's use a icosahedron
237261 auto solid = geometry::icosahedron();
@@ -256,19 +280,19 @@ model::MeshPointer DeferredLightingEffect::getPointLightMesh() { · 18 unmodified lines
256280 delete[] indexData;
257281
258282
259− std::vector<model::Mesh::Part> parts;
260− parts.push_back(model::Mesh::Part(0, nbIndices, 0, model::Mesh::TRIANGLES));
261− parts.push_back(model::Mesh::Part(0, nbIndices, 0, model::Mesh::LINE_STRIP)); // outline version
283+ std::vector<graphics::Mesh::Part> parts;
284+ parts.push_back(graphics::Mesh::Part(0, nbIndices, 0, graphics::Mesh::TRIANGLES));
285+ parts.push_back(graphics::Mesh::Part(0, nbIndices, 0, graphics::Mesh::LINE_STRIP)); // outline version
262286
263287
264− _pointLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(model::Mesh::Part), (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL));
288+ _pointLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(graphics::Mesh::Part), (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL));
265289 }
266290 return _pointLightMesh;
267291 }
268292
269−model::MeshPointer DeferredLightingEffect::getSpotLightMesh() {
293+graphics::MeshPointer DeferredLightingEffect::getSpotLightMesh() {
270294 if (!_spotLightMesh) {
271− _spotLightMesh = std::make_shared<model::Mesh>();
295+ _spotLightMesh = std::make_shared<graphics::Mesh>();
272296
273297 int slices = 16;
274298 int rings = 3;
@@ -353,12 +377,12 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { · 78 unmodified lines
353377 delete[] indexData;
354378
355379
356− std::vector<model::Mesh::Part> parts;
357− parts.push_back(model::Mesh::Part(0, indices, 0, model::Mesh::TRIANGLES));
358− parts.push_back(model::Mesh::Part(0, indices, 0, model::Mesh::LINE_STRIP)); // outline version
380+ std::vector<graphics::Mesh::Part> parts;
381+ parts.push_back(graphics::Mesh::Part(0, indices, 0, graphics::Mesh::TRIANGLES));
382+ parts.push_back(graphics::Mesh::Part(0, indices, 0, graphics::Mesh::LINE_STRIP)); // outline version
359383
360384
361− _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(model::Mesh::Part), (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL));
385+ _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(parts.size() * sizeof(graphics::Mesh::Part), (gpu::Byte*) parts.data()), gpu::Element::PART_DRAWCALL));
362386 }
363387 return _spotLightMesh;
364388 }
@@ -439,7 +463,7 @@ void RenderDeferredSetup::run(const render::RenderContextPointer& renderContext, · 74 unmodified lines
439463 const DeferredFrameTransformPointer& frameTransform,
440464 const DeferredFramebufferPointer& deferredFramebuffer,
441465 const LightingModelPointer& lightingModel,
442− const model::HazePointer& haze,
466+ const graphics::HazePointer& haze,
443467 const SurfaceGeometryFramebufferPointer& surfaceGeometryFramebuffer,
444468 const AmbientOcclusionFramebufferPointer& ambientOcclusionFramebuffer,
445469 const SubsurfaceScatteringResourcePointer& subsurfaceScatteringResource) {
@@ -501,9 +525,11 @@ void RenderDeferredSetup::run(const render::RenderContextPointer& renderContext, · 55 unmodified lines
501525 auto lightAndShadow = lightStage->getCurrentKeyLightAndShadow();
502526 const auto& globalShadow = lightAndShadow.second;
503527
504− // Bind the shadow buffer
528+ // Bind the shadow buffers
505529 if (globalShadow) {
506− batch.setResourceTexture(SHADOW_MAP_UNIT, globalShadow->map);
530+ for (unsigned int i = 0; i < globalShadow->getCascadeCount(); i++) {
531+ batch.setResourceTexture(SHADOW_MAP_UNIT+i, globalShadow->getCascade(i).map);
532+ }
507533 }
508534
509535 auto& program = deferredLightingEffect->_directionalSkyboxLight;
@@ -511,7 +537,7 @@ void RenderDeferredSetup::run(const render::RenderContextPointer& renderContext, · 1 unmodified line
511537
512538 auto keyLight = lightAndShadow.first;
513539
514− model::LightPointer keyAmbientLight;
540+ graphics::LightPointer keyAmbientLight;
515541 if (lightStage && lightStage->_currentFrame._ambientLights.size()) {
516542 keyAmbientLight = lightStage->getLight(lightStage->_currentFrame._ambientLights.front());
517543 }
@@ -556,19 +582,20 @@ void RenderDeferredSetup::run(const render::RenderContextPointer& renderContext, · 38 unmodified lines
556582 batch._glUniform4fv(locations->texcoordFrameTransform, 1, reinterpret_cast< const float* >(&textureFrameTransform));
557583
558584 // Setup the global lighting
559− deferredLightingEffect->setupKeyLightBatch(args, batch, locations->lightBufferUnit, locations->ambientBufferUnit, SKYBOX_MAP_UNIT);
585+ deferredLightingEffect->setupKeyLightBatch(args, batch, locations->keyLightBufferUnit, locations->ambientBufferUnit, SKYBOX_MAP_UNIT);
560586
561587 // Haze
562588 if (haze) {
563− batch.setUniformBuffer(HAZE_MODEL_BUFFER_SLOT, haze->getHazeParametersBuffer());
589+ batch.setUniformBuffer(HAZE_MODEL_BUFFER_SLOT, haze->getHazeParametersBuffer());
564590 }
565−
591+
566592 batch.draw(gpu::TRIANGLE_STRIP, 4);
567593
568− deferredLightingEffect->unsetKeyLightBatch(batch, locations->lightBufferUnit, locations->ambientBufferUnit, SKYBOX_MAP_UNIT);
569−
594+ deferredLightingEffect->unsetKeyLightBatch(batch, locations->keyLightBufferUnit, locations->ambientBufferUnit, SKYBOX_MAP_UNIT);
570595
571− batch.setResourceTexture(SHADOW_MAP_UNIT, nullptr);
596+ for (auto i = 0; i < SHADOW_CASCADE_MAX_COUNT; i++) {
597+ batch.setResourceTexture(SHADOW_MAP_UNIT+i, nullptr);
598+ }
572599 }
573600 }
574601
@@ -619,12 +646,8 @@ void RenderDeferredLocals::run(const render::RenderContextPointer& renderContext · 44 unmodified lines
619646
620647 auto& lightIndices = lightClusters->_visibleLightIndices;
621648 if (!lightIndices.empty() && lightIndices[0] > 0) {
622− // Bind the global list of lights and the visible lights this frame
623− batch.setUniformBuffer(deferredLightingEffect->_localLightLocations->lightBufferUnit, lightClusters->_lightStage->getLightArrayBuffer());
624−
625− batch.setUniformBuffer(LIGHT_CLUSTER_GRID_FRUSTUM_GRID_SLOT, lightClusters->_frustumGridBuffer);
626− batch.setUniformBuffer(LIGHT_CLUSTER_GRID_CLUSTER_GRID_SLOT, lightClusters->_clusterGridBuffer);
627− batch.setUniformBuffer(LIGHT_CLUSTER_GRID_CLUSTER_CONTENT_SLOT, lightClusters->_clusterContentBuffer);
649+ deferredLightingEffect->setupLocalLightsBatch(batch, LIGHT_CLUSTER_GRID_CLUSTER_GRID_SLOT, LIGHT_CLUSTER_GRID_CLUSTER_CONTENT_SLOT, LIGHT_CLUSTER_GRID_FRUSTUM_GRID_SLOT,
650+ lightClusters);
628651
629652 // Local light pipeline
630653 batch.setPipeline(deferredLightingEffect->_localLight);
@@ -726,11 +749,11 @@ void DefaultLightingSetup::run(const RenderContextPointer& renderContext) { · 95 unmodified lines
726749 auto textureCache = DependencyManager::get<TextureCache>();
727750 {
728751 PROFILE_RANGE(render, "Process Default Skybox");
729− auto textureCache = DependencyManager::get<TextureCache>();
730−
731− auto skyboxUrl = PathUtils::resourcesPath().toStdString() + "images/Default-Sky-9-cubemap.ktx";
752+ QFileSelector fileSelector;
753+ fileSelector.setExtraSelectors(FileUtils::getFileSelectors());
754+ auto skyboxUrl = fileSelector.select(PathUtils::resourcesPath() + "images/Default-Sky-9-cubemap.ktx");
732755
733− _defaultSkyboxTexture = gpu::Texture::unserialize(skyboxUrl);
756+ _defaultSkyboxTexture = gpu::Texture::unserialize(skyboxUrl.toStdString());
734757 _defaultSkyboxAmbientTexture = _defaultSkyboxTexture;
735758
736759 _defaultSkybox->setCubemap(_defaultSkyboxTexture);
@@ -741,12 +764,12 @@ void DefaultLightingSetup::run(const RenderContextPointer& renderContext) { · 4 unmodified lines
741764 if (lightStage) {
742765
743766 // Allocate a default global light directional and ambient
744− auto lp = std::make_shared<model::Light>();
745− lp->setType(model::Light::SUN);
767+ auto lp = std::make_shared<graphics::Light>();
768+ lp->setType(graphics::Light::SUN);
746769 lp->setDirection(glm::vec3(-1.0f));
747770 lp->setColor(glm::vec3(1.0f));
748771 lp->setIntensity(1.0f);
749− lp->setType(model::Light::SUN);
772+ lp->setType(graphics::Light::SUN);
750773 lp->setAmbientSpherePreset(gpu::SphericalHarmonics::Preset::OLD_TOWN_SQUARE);
751774
752775 lp->setAmbientIntensity(0.5f);
@@ -760,14 +783,15 @@ void DefaultLightingSetup::run(const RenderContextPointer& renderContext) { · 7 unmodified lines
760783 _defaultLight = lp;
761784
762785 // Add the global light to the light stage (for later shadow rendering)
763− _defaultLightID = lightStage->addLight(lp);
786+ // Set this light to be the default
787+ _defaultLightID = lightStage->addLight(lp, true);
764788 lightStage->addShadow(_defaultLightID);
765789 }
766790
767791 auto backgroundStage = renderContext->_scene->getStage<BackgroundStage>();
768792 if (backgroundStage) {
769793
770− auto background = std::make_shared<model::SunSkyStage>();
794+ auto background = std::make_shared<graphics::SunSkyStage>();
771795 background->setSkybox(_defaultSkybox);
772796
773797 // capture deault background
@@ -782,7 +806,7 @@ void DefaultLightingSetup::run(const RenderContextPointer& renderContext) { · 8 unmodified lines
782806 auto hazeStage = renderContext->_scene->getStage<HazeStage>();
783807 if (hazeStage) {
784808
785− auto haze = std::make_shared<model::Haze>();
809+ auto haze = std::make_shared<graphics::Haze>();
786810
787811 _defaultHaze = haze;
788812 _defaultHazeID = hazeStage->addHaze(_defaultHaze);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t