Enable parallel split shadow maps

pull/541/head
AnyOldName3 7 years ago
parent 3f63ebce77
commit 56fa33af66

@ -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);

@ -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<osg::Shader> vertexShader = new osg::Shader(osg::Shader::VERTEX, debugVertexShaderSource);
debugProgram->addShader(vertexShader);
osg::ref_ptr<osg::Shader> 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<osg::StateSet> stateSet = debugGeometry->getOrCreateStateSet();
stateSet->setAttributeAndModes(debugProgram, osg::StateAttribute::ON);
osg::ref_ptr<osg::Uniform> 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<osg::StateSet> stateSet = debugGeometry[i]->getOrCreateStateSet();
stateSet->setAttributeAndModes(debugProgram, osg::StateAttribute::ON);
osg::ref_ptr<osg::Uniform> 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<osg::Texture2D> texture = sd->_texture;
osg::ref_ptr<osg::StateSet> stateSet = debugGeometry->getOrCreateStateSet();
osg::ref_ptr<osg::StateSet> 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);

@ -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<osg::Camera> debugCamera;
std::vector<osg::ref_ptr<osg::Camera>> debugCameras;
osg::ref_ptr<osg::Program> debugProgram;
osg::ref_ptr<osg::Node> debugGeometry;
std::vector<osg::ref_ptr<osg::Node>> debugGeometry;
};
}

@ -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);

@ -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;
}

@ -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);

@ -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;
}

@ -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);

@ -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;
}

Loading…
Cancel
Save