From 56fa33af66183d79275cea742b0e6dcf743981ae Mon Sep 17 00:00:00 2001 From: AnyOldName3 Date: Tue, 7 Nov 2017 00:32:04 +0000 Subject: [PATCH] Enable parallel split shadow maps --- apps/openmw/mwrender/renderingmanager.cpp | 6 ++-- apps/openmw/mwrender/shadow.cpp | 40 +++++++++++++---------- apps/openmw/mwrender/shadow.hpp | 7 ++-- files/shaders/objects_fragment.glsl | 7 ++-- files/shaders/objects_vertex.glsl | 8 +++-- files/shaders/terrain_fragment.glsl | 7 ++-- files/shaders/terrain_vertex.glsl | 8 +++-- files/shaders/water_fragment.glsl | 7 ++-- files/shaders/water_vertex.glsl | 9 +++-- 9 files changed, 65 insertions(+), 34 deletions(-) diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index aa5240618..645c10667 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -210,13 +210,13 @@ namespace MWRender settings->setReceivesShadowTraversalMask(~0u); //settings->setShadowMapProjectionHint(osgShadow::ShadowSettings::PERSPECTIVE_SHADOW_MAP); - settings->setBaseShadowTextureUnit(7); + settings->setBaseShadowTextureUnit(8 - MWShadow::numberOfShadowMapsPerLight); //settings->setMinimumShadowMapNearFarRatio(0); - //settings->setNumShadowMapsPerLight(1); + settings->setNumShadowMapsPerLight(MWShadow::numberOfShadowMapsPerLight); //settings->setShadowMapProjectionHint(osgShadow::ShadowSettings::ORTHOGRAPHIC_SHADOW_MAP); //settings->setMultipleShadowMapHint(osgShadow::ShadowSettings::PARALLEL_SPLIT); // ignored //settings->setComputeNearFarModeOverride(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES); - //settings->setDebugDraw(true); + //settings->setDebugDraw(true); // don't turn this on because it makes everything break //settings->setPerspectiveShadowMapCutOffAngle(0); //settings->setShaderHint(osgShadow::ShadowSettings::PROVIDE_VERTEX_AND_FRAGMENT_SHADER); diff --git a/apps/openmw/mwrender/shadow.cpp b/apps/openmw/mwrender/shadow.cpp index 347952b4c..40b2ff311 100644 --- a/apps/openmw/mwrender/shadow.cpp +++ b/apps/openmw/mwrender/shadow.cpp @@ -51,25 +51,31 @@ namespace MWRender "} \n"; - MWShadow::MWShadow() : debugCamera(new osg::Camera), debugProgram(new osg::Program), debugTextureUnit(0) + MWShadow::MWShadow() : debugProgram(new osg::Program), debugTextureUnit(0) { - debugCamera->setViewport(0, 0, 200, 200); - debugCamera->setRenderOrder(osg::Camera::POST_RENDER); - debugCamera->setClearColor(osg::Vec4(1.0, 1.0, 0.0, 1.0)); - osg::ref_ptr vertexShader = new osg::Shader(osg::Shader::VERTEX, debugVertexShaderSource); debugProgram->addShader(vertexShader); osg::ref_ptr fragmentShader = new osg::Shader(osg::Shader::FRAGMENT, debugFragmentShaderSource); debugProgram->addShader(fragmentShader); - debugGeometry = osg::createTexturedQuadGeometry(osg::Vec3(-1, -1, 0), osg::Vec3(2, 0, 0), osg::Vec3(0, 2, 0)); - debugGeometry->setCullingActive(false); - debugCamera->addChild(debugGeometry); - osg::ref_ptr stateSet = debugGeometry->getOrCreateStateSet(); - stateSet->setAttributeAndModes(debugProgram, osg::StateAttribute::ON); - osg::ref_ptr textureUniform = new osg::Uniform("texture", debugTextureUnit); - //textureUniform->setType(osg::Uniform::SAMPLER_2D); - stateSet->addUniform(textureUniform.get()); + for (int i = 0; i < numberOfShadowMapsPerLight; ++i) + { + std::cout << i << std::endl; + + debugCameras.push_back(new osg::Camera); + debugCameras[i]->setViewport(200 * i, 0, 200, 200); + debugCameras[i]->setRenderOrder(osg::Camera::POST_RENDER); + debugCameras[i]->setClearColor(osg::Vec4(1.0, 1.0, 0.0, 1.0)); + + debugGeometry.push_back(osg::createTexturedQuadGeometry(osg::Vec3(-1, -1, 0), osg::Vec3(2, 0, 0), osg::Vec3(0, 2, 0))); + debugGeometry[i]->setCullingActive(false); + debugCameras[i]->addChild(debugGeometry[i]); + osg::ref_ptr stateSet = debugGeometry[i]->getOrCreateStateSet(); + stateSet->setAttributeAndModes(debugProgram, osg::StateAttribute::ON); + osg::ref_ptr textureUniform = new osg::Uniform("texture", debugTextureUnit); + //textureUniform->setType(osg::Uniform::SAMPLER_2D); + stateSet->addUniform(textureUniform.get()); + } } class VDSMCameraCullCallback : public osg::NodeCallback @@ -552,16 +558,16 @@ namespace MWRender previous_sdl.erase(previous_sdl.begin()); } - if (true) + if (debugHud) { osg::ref_ptr texture = sd->_texture; - osg::ref_ptr stateSet = debugGeometry->getOrCreateStateSet(); + osg::ref_ptr stateSet = debugGeometry[sm_i]->getOrCreateStateSet(); stateSet->setTextureAttributeAndModes(debugTextureUnit, texture, osg::StateAttribute::ON); unsigned int traversalMask = cv.getTraversalMask(); - cv.setTraversalMask(debugGeometry->getNodeMask()); + cv.setTraversalMask(debugGeometry[sm_i]->getNodeMask()); cv.pushStateSet(stateSet); - debugCamera->accept(cv); + debugCameras[sm_i]->accept(cv); cv.popStateSet(); cv.setTraversalMask(traversalMask); diff --git a/apps/openmw/mwrender/shadow.hpp b/apps/openmw/mwrender/shadow.hpp index a941f9c2e..e7663c55a 100644 --- a/apps/openmw/mwrender/shadow.hpp +++ b/apps/openmw/mwrender/shadow.hpp @@ -8,17 +8,20 @@ namespace MWRender class MWShadow : public osgShadow::ViewDependentShadowMap { public: + static const int numberOfShadowMapsPerLight = 2; + static const bool debugHud = true; + MWShadow(); virtual void cull(osgUtil::CullVisitor& cv); protected: const int debugTextureUnit; - osg::ref_ptr debugCamera; + std::vector> debugCameras; osg::ref_ptr debugProgram; - osg::ref_ptr debugGeometry; + std::vector> debugGeometry; }; } diff --git a/files/shaders/objects_fragment.glsl b/files/shaders/objects_fragment.glsl index 4dd282dc4..f6355aaa6 100644 --- a/files/shaders/objects_fragment.glsl +++ b/files/shaders/objects_fragment.glsl @@ -56,7 +56,9 @@ varying vec3 passViewPos; varying vec3 passNormal; uniform sampler2DShadow shadowTexture0; -varying vec4 shadowSpaceCoords; +uniform sampler2DShadow shadowTexture1; +varying vec4 shadowSpaceCoords0; +varying vec4 shadowSpaceCoords1; #include "lighting.glsl" #include "parallax.glsl" @@ -116,7 +118,8 @@ void main() gl_FragData[0].xyz = mix(gl_FragData[0].xyz, decalTex.xyz, decalTex.a); #endif - float shadowing = shadow2DProj(shadowTexture0, shadowSpaceCoords).r; + float shadowing = shadow2DProj(shadowTexture0, shadowSpaceCoords0).r; + shadowing *= shadow2DProj(shadowTexture1, shadowSpaceCoords1).r; #if !PER_PIXEL_LIGHTING gl_FragData[0] *= lighting + vec4(shadowDiffuseLighting * shadowing, 0); diff --git a/files/shaders/objects_vertex.glsl b/files/shaders/objects_vertex.glsl index 4046386dd..b90a8722b 100644 --- a/files/shaders/objects_vertex.glsl +++ b/files/shaders/objects_vertex.glsl @@ -47,7 +47,9 @@ varying vec3 passViewPos; varying vec3 passNormal; uniform int shadowTextureUnit0; -varying vec4 shadowSpaceCoords; +uniform int shadowTextureUnit1; +varying vec4 shadowSpaceCoords0; +varying vec4 shadowSpaceCoords1; #include "lighting.glsl" @@ -106,5 +108,7 @@ void main(void) // This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner. mat4 eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit0], gl_EyePlaneT[shadowTextureUnit0], gl_EyePlaneR[shadowTextureUnit0], gl_EyePlaneQ[shadowTextureUnit0]); - shadowSpaceCoords = viewPos * eyePlaneMat; + shadowSpaceCoords0 = viewPos * eyePlaneMat; + eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit1], gl_EyePlaneT[shadowTextureUnit1], gl_EyePlaneR[shadowTextureUnit1], gl_EyePlaneQ[shadowTextureUnit1]); + shadowSpaceCoords1 = viewPos * eyePlaneMat; } diff --git a/files/shaders/terrain_fragment.glsl b/files/shaders/terrain_fragment.glsl index e27dd384e..e79adaafb 100644 --- a/files/shaders/terrain_fragment.glsl +++ b/files/shaders/terrain_fragment.glsl @@ -26,7 +26,9 @@ varying vec3 passViewPos; varying vec3 passNormal; uniform sampler2DShadow shadowTexture0; -varying vec4 shadowSpaceCoords; +uniform sampler2DShadow shadowTexture1; +varying vec4 shadowSpaceCoords0; +varying vec4 shadowSpaceCoords1; #include "lighting.glsl" #include "parallax.glsl" @@ -68,7 +70,8 @@ void main() gl_FragData[0].a *= texture2D(blendMap, blendMapUV).a; #endif - float shadowing = shadow2DProj(shadowTexture0, shadowSpaceCoords).r; + float shadowing = shadow2DProj(shadowTexture0, shadowSpaceCoords0).r; + shadowing *= shadow2DProj(shadowTexture1, shadowSpaceCoords1).r; #if !PER_PIXEL_LIGHTING gl_FragData[0] *= lighting + vec4(shadowDiffuseLighting * shadowing, 0); diff --git a/files/shaders/terrain_vertex.glsl b/files/shaders/terrain_vertex.glsl index 17a9c436e..015039252 100644 --- a/files/shaders/terrain_vertex.glsl +++ b/files/shaders/terrain_vertex.glsl @@ -15,7 +15,9 @@ varying vec3 passViewPos; varying vec3 passNormal; uniform int shadowTextureUnit0; -varying vec4 shadowSpaceCoords; +uniform int shadowTextureUnit1; +varying vec4 shadowSpaceCoords0; +varying vec4 shadowSpaceCoords1; #include "lighting.glsl" @@ -40,5 +42,7 @@ void main(void) // This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner. mat4 eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit0], gl_EyePlaneT[shadowTextureUnit0], gl_EyePlaneR[shadowTextureUnit0], gl_EyePlaneQ[shadowTextureUnit0]); - shadowSpaceCoords = viewPos * eyePlaneMat; + shadowSpaceCoords0 = viewPos * eyePlaneMat; + eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit1], gl_EyePlaneT[shadowTextureUnit1], gl_EyePlaneR[shadowTextureUnit1], gl_EyePlaneQ[shadowTextureUnit1]); + shadowSpaceCoords1 = viewPos * eyePlaneMat; } diff --git a/files/shaders/water_fragment.glsl b/files/shaders/water_fragment.glsl index f04a9d8a8..3529b7347 100644 --- a/files/shaders/water_fragment.glsl +++ b/files/shaders/water_fragment.glsl @@ -143,7 +143,9 @@ uniform vec3 nodePosition; uniform float rainIntensity; uniform sampler2DShadow shadowTexture0; -varying vec4 shadowSpaceCoords; +uniform sampler2DShadow shadowTexture1; +varying vec4 shadowSpaceCoords0; +varying vec4 shadowSpaceCoords1; float frustumDepth; @@ -161,7 +163,8 @@ void main(void) vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0; UV.y *= -1.0; - float shadow = shadow2DProj(shadowTexture0, shadowSpaceCoords).r; + float shadow = shadow2DProj(shadowTexture0, shadowSpaceCoords0).r; + shadow *= shadow2DProj(shadowTexture1, shadowSpaceCoords1).r; vec2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z; screenCoords.y = (1.0-screenCoords.y); diff --git a/files/shaders/water_vertex.glsl b/files/shaders/water_vertex.glsl index 6aaa0537c..0c280fb7e 100644 --- a/files/shaders/water_vertex.glsl +++ b/files/shaders/water_vertex.glsl @@ -5,7 +5,9 @@ varying vec4 position; varying float depthPassthrough; uniform int shadowTextureUnit0; -varying vec4 shadowSpaceCoords; +uniform int shadowTextureUnit1; +varying vec4 shadowSpaceCoords0; +varying vec4 shadowSpaceCoords1; void main(void) { @@ -23,7 +25,10 @@ void main(void) depthPassthrough = gl_Position.z; + vec4 viewPos = gl_ModelViewMatrix * gl_Vertex; // This matrix has the opposite handedness to the others used here, so multiplication must have the vector to the left. Alternatively it could be transposed after construction, but that's extra work for the GPU just to make the code look a tiny bit cleaner. mat4 eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit0], gl_EyePlaneT[shadowTextureUnit0], gl_EyePlaneR[shadowTextureUnit0], gl_EyePlaneQ[shadowTextureUnit0]); - shadowSpaceCoords = (gl_ModelViewMatrix * gl_Vertex) * eyePlaneMat; + shadowSpaceCoords0 = viewPos * eyePlaneMat; + eyePlaneMat = mat4(gl_EyePlaneS[shadowTextureUnit1], gl_EyePlaneT[shadowTextureUnit1], gl_EyePlaneR[shadowTextureUnit1], gl_EyePlaneQ[shadowTextureUnit1]); + shadowSpaceCoords1 = viewPos * eyePlaneMat; }