History

pack all the lights in a single buffer and shade the opaque with a single full screen path

sam committed 10 years ago

Commit b81dfef1 parent 4bc8a835 files · +123 −46Browse files

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

Unified · Split

libraries/render-utils/src/DeferredLightingEffect.cpp+55−7
@@ -97,7 +97,7 @@ void DeferredLightingEffect::init() { · 96 unmodified lines
9797 loadLightProgram(deferred_light_vert, directional_skybox_light_shadow_frag, false, _directionalSkyboxLightShadow, _directionalSkyboxLightShadowLocations);
9898
9999 loadLightProgram(deferred_light_limited_vert, point_light_frag, true, _pointLight, _pointLightLocations);
100− loadLightProgram(deferred_light_spot_vert, spot_light_frag, true, _spotLight, _spotLightLocations);
100+ loadLightProgram(deferred_light_vert, spot_light_frag, false, _spotLight, _spotLightLocations);
101101
102102 // Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light
103103 _allocatedLights.push_back(std::make_shared<model::Light>());
@@ -571,6 +571,11 @@ void RenderDeferredSetup::run(const render::SceneContextPointer& sceneContext, c · 467 unmodified lines
571571
572572 }
573573
574+RenderDeferredLocals::RenderDeferredLocals() :
575+_spotLightsBuffer(std::make_shared<gpu::Buffer>()) {
576+
577+}
578+
574579 void RenderDeferredLocals::run(const render::SceneContextPointer& sceneContext, const render::RenderContextPointer& renderContext,
575580 const DeferredFrameTransformPointer& frameTransform,
576581 const DeferredFramebufferPointer& deferredFramebuffer,
@@ -585,6 +590,8 @@ void RenderDeferredLocals::run(const render::SceneContextPointer& sceneContext, · 8 unmodified lines
585590 }
586591 auto args = renderContext->args;
587592 gpu::doInBatch(args->_context, [&](gpu::Batch& batch) {
593+ // Framebuffer copy operations cannot function as multipass stereo operations.
594+ batch.enableStereo(false);
588595
589596 // THe main viewport is assumed to be the mono viewport (or the 2 stereo faces side by side within that viewport)
590597 auto viewport = args->_viewport;
@@ -609,9 +616,46 @@ void RenderDeferredLocals::run(const render::SceneContextPointer& sceneContext, · 18 unmodified lines
609616
610617 auto textureFrameTransform = gpu::Framebuffer::evalSubregionTexcoordTransformCoefficients(deferredFramebuffer->getFrameSize(), viewport);
611618
612− batch.setProjectionTransform(projMat);
613− batch.setViewTransform(viewTransform, true);
619+ // batch.setProjectionTransform(projMat);
620+ // batch.setViewTransform(viewTransform, true);
621+
622+ // gather lights
623+ auto& srcPointLights = deferredLightingEffect->_pointLights;
624+ auto& srcSpotLights = deferredLightingEffect->_spotLights;
625+ std::vector<int> lightIndices(srcPointLights.size() + srcSpotLights.size() + 1);
626+ lightIndices[0] = 0;
627+
628+ if (points && !srcPointLights.empty()) {
629+ memcpy(lightIndices.data() + (lightIndices[0] + 1), srcPointLights.data(), srcPointLights.size() * sizeof(int));
630+ lightIndices[0] += srcPointLights.size();
631+ }
632+ if (spots && !srcSpotLights.empty()) {
633+ memcpy(lightIndices.data() + (lightIndices[0] + 1), srcSpotLights.data(), srcSpotLights.size() * sizeof(int));
634+ lightIndices[0] += srcSpotLights.size();
635+ }
636+
637+ if (lightIndices[0] > 0) {
638+ _spotLightsBuffer._buffer->setData(lightIndices.size() * sizeof(int), (const gpu::Byte*) lightIndices.data());
639+ _spotLightsBuffer._size = lightIndices.size() * sizeof(int);
640+
641+ // Spot light pipeline
642+ batch.setPipeline(deferredLightingEffect->_spotLight);
643+ batch._glUniform4fv(deferredLightingEffect->_spotLightLocations->texcoordFrameTransform, 1, reinterpret_cast< const float* >(&textureFrameTransform));
644+
645+ // Spot mesh
646+ auto mesh = deferredLightingEffect->getSpotLightMesh();
647+ batch.setIndexBuffer(mesh->getIndexBuffer());
648+ batch.setInputBuffer(0, mesh->getVertexBuffer());
649+ batch.setInputFormat(mesh->getVertexFormat());
650+ auto& conePart = mesh->getPartBuffer().get<model::Mesh::Part>(0);
651+
652+
653+ batch.setUniformBuffer(deferredLightingEffect->_spotLightLocations->lightBufferUnit, deferredLightingEffect->getLightStage()._lightArrayBuffer);
654+ batch.setUniformBuffer(deferredLightingEffect->_spotLightLocations->lightIndexBufferUnit, _spotLightsBuffer);
655+ // batch.drawIndexedInstanced(deferredLightingEffect->_spotLights.size(), model::Mesh::topologyToPrimitive(conePart._topology), conePart._numIndices, conePart._startIndex);
614656
657+ batch.draw(gpu::TRIANGLE_STRIP, 4);
658+ /*
615659 // Splat Point lights
616660 if (points && !deferredLightingEffect->_pointLights.empty()) {
617661 // POint light pipeline
@@ -645,9 +689,11 @@ void RenderDeferredLocals::run(const render::SceneContextPointer& sceneContext, · 27 unmodified lines
645689
646690 // Splat spot lights
647691 if (spots && !deferredLightingEffect->_spotLights.empty()) {
648−
649−
650− _spotLightsBuffer._buffer->setData(deferredLightingEffect->_spotLights.size() * sizeof(int), (const gpu::Byte*) deferredLightingEffect->_spotLights.data());
692+ std::vector<int> again(deferredLightingEffect->_spotLights.size() + 1);
693+ again[0] = deferredLightingEffect->_spotLights.size();
694+ memcpy(again.data() + 1, deferredLightingEffect->_spotLights.data(), deferredLightingEffect->_spotLights.size() * sizeof(int));
695+ _spotLightsBuffer._buffer->setData(again.size() * sizeof(int), (const gpu::Byte*) again.data());
696+ _spotLightsBuffer._size = again.size() * sizeof(int);
651697
652698 // Spot light pipeline
653699 batch.setPipeline(deferredLightingEffect->_spotLight);
@@ -663,7 +709,9 @@ void RenderDeferredLocals::run(const render::SceneContextPointer& sceneContext, · 9 unmodified lines
663709
664710 batch.setUniformBuffer(deferredLightingEffect->_spotLightLocations->lightBufferUnit, deferredLightingEffect->getLightStage()._lightArrayBuffer);
665711 batch.setUniformBuffer(deferredLightingEffect->_spotLightLocations->lightIndexBufferUnit, _spotLightsBuffer);
666− batch.drawIndexedInstanced(deferredLightingEffect->_spotLights.size(), model::Mesh::topologyToPrimitive(conePart._topology), conePart._numIndices, conePart._startIndex);
712+ // batch.drawIndexedInstanced(deferredLightingEffect->_spotLights.size(), model::Mesh::topologyToPrimitive(conePart._topology), conePart._numIndices, conePart._startIndex);
713+
714+ batch.draw(gpu::TRIANGLE_STRIP, 4);
667715 /*
668716 for (auto lightID : deferredLightingEffect->_spotLights) {
669717 auto light = deferredLightingEffect->getLightStage().getLight(lightID);