Skip to content

Commit

Permalink
Merge branch '49_multiview' into 'master'
Browse files Browse the repository at this point in the history
.49 stereo fixes

See merge request OpenMW/openmw!4527
  • Loading branch information
Capostrophic committed Jan 31, 2025
2 parents 8128703 + 4428c1d commit c04cd2d
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 67 deletions.
1 change: 0 additions & 1 deletion apps/openmw/mwrender/characterpreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ namespace MWRender
// turn off sky blending
stateset->addUniform(new osg::Uniform("far", 10000000.0f));
stateset->addUniform(new osg::Uniform("skyBlendingStart", 8000000.0f));
stateset->addUniform(new osg::Uniform("sky", 0));
stateset->addUniform(new osg::Uniform("screenRes", osg::Vec2f{ 1, 1 }));

stateset->addUniform(new osg::Uniform("emissiveMult", 1.f));
Expand Down
1 change: 0 additions & 1 deletion apps/openmw/mwrender/localmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ namespace MWRender
// turn of sky blending
stateset->addUniform(new osg::Uniform("far", 10000000.0f));
stateset->addUniform(new osg::Uniform("skyBlendingStart", 8000000.0f));
stateset->addUniform(new osg::Uniform("sky", 0));
stateset->addUniform(new osg::Uniform("screenRes", osg::Vec2f{ 1, 1 }));

osg::ref_ptr<osg::LightModel> lightmodel = new osg::LightModel;
Expand Down
21 changes: 17 additions & 4 deletions components/fx/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace fx
#define omw_Position @position
#define omw_Texture1D @texture1D
#define omw_Texture2D @texture2D
#define omw_Texture2DArray @texture2DArray
#define omw_Texture3D @texture3D
#define omw_Vertex @vertex
#define omw_FragColor @fragColor
Expand Down Expand Up @@ -154,7 +155,7 @@ mat4 omw_InvProjectionMatrix()
float omw_GetDepth(vec2 uv)
{
#if OMW_MULTIVIEW
float depth = omw_Texture2D(omw_SamplerDepth, vec3(uv, gl_ViewID_OVR)).r;
float depth = omw_Texture2DArray(omw_SamplerDepth, vec3(uv, gl_ViewID_OVR)).r;
#else
float depth = omw_Texture2D(omw_SamplerDepth, uv).r;
#endif
Expand All @@ -165,10 +166,19 @@ mat4 omw_InvProjectionMatrix()
#endif
}
vec4 omw_GetDistortion(vec2 uv)
{
#if OMW_MULTIVIEW
return omw_Texture2DArray(omw_SamplerDistortion, vec3(uv, gl_ViewID_OVR));
#else
return omw_Texture2D(omw_SamplerDistortion, uv);
#endif
}
vec4 omw_GetLastShader(vec2 uv)
{
#if OMW_MULTIVIEW
return omw_Texture2D(omw_SamplerLastShader, vec3(uv, gl_ViewID_OVR));
return omw_Texture2DArray(omw_SamplerLastShader, vec3(uv, gl_ViewID_OVR));
#else
return omw_Texture2D(omw_SamplerLastShader, uv);
#endif
Expand All @@ -177,7 +187,7 @@ mat4 omw_InvProjectionMatrix()
vec4 omw_GetLastPass(vec2 uv)
{
#if OMW_MULTIVIEW
return omw_Texture2D(omw_SamplerLastPass, vec3(uv, gl_ViewID_OVR));
return omw_Texture2DArray(omw_SamplerLastPass, vec3(uv, gl_ViewID_OVR));
#else
return omw_Texture2D(omw_SamplerLastPass, uv);
#endif
Expand All @@ -186,7 +196,7 @@ mat4 omw_InvProjectionMatrix()
vec3 omw_GetNormals(vec2 uv)
{
#if OMW_MULTIVIEW
return omw_Texture2D(omw_SamplerNormals, vec3(uv, gl_ViewID_OVR)).rgb * 2.0 - 1.0;
return omw_Texture2DArray(omw_SamplerNormals, vec3(uv, gl_ViewID_OVR)).rgb * 2.0 - 1.0;
#else
return omw_Texture2D(omw_SamplerNormals, uv).rgb * 2.0 - 1.0;
#endif
Expand Down Expand Up @@ -275,6 +285,9 @@ float omw_EstimateFogCoverageFromUV(vec2 uv)
{ "@hdr", technique.getHDR() ? "1" : "0" }, { "@in", mLegacyGLSL ? "varying" : "in" },
{ "@out", mLegacyGLSL ? "varying" : "out" }, { "@position", "gl_Position" },
{ "@texture1D", mLegacyGLSL ? "texture1D" : "texture" },
// Note, @texture2DArray must be defined before @texture2D since @texture2D is a perfect prefix of
// texture2DArray
{ "@texture2DArray", mLegacyGLSL ? "texture2DArray" : "texture" },
{ "@texture2D", mLegacyGLSL ? "texture2D" : "texture" },
{ "@texture3D", mLegacyGLSL ? "texture3D" : "texture" },
{ "@vertex", mLegacyGLSL ? "gl_Vertex" : "_omw_Vertex" },
Expand Down
2 changes: 1 addition & 1 deletion components/fx/technique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace fx
mDescription = {};
mVersion = {};
mGLSLExtensions.clear();
mGLSLVersion = mUBO ? 330 : 120;
mGLSLVersion = (mUBO || Stereo::getMultiview()) ? 330 : 120;
mGLSLProfile.clear();
mDynamic = false;
}
Expand Down
125 changes: 77 additions & 48 deletions components/sceneutil/mwshadowtechnique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,12 @@ void SceneUtil::MWShadowTechnique::copyShadowMap(osgUtil::CullVisitor& cv, ViewD
lhs_sd->_camera = rhs_sd->_camera;
lhs_sd->_textureUnit = rhs_sd->_textureUnit;
lhs_sd->_texture = rhs_sd->_texture;
lhs_sd->_sm_i = rhs_sd->_sm_i;
sdl.push_back(lhs_sd);
}

copyShadowStateSettings(cv, lhs);

if (lhs->_numValidShadows > 0)
{
prepareStateSetForRenderingShadow(*lhs, cv.getTraversalNumber());
Expand All @@ -1000,6 +1003,14 @@ void SceneUtil::MWShadowTechnique::setCustomFrustumCallback(CustomFrustumCallbac
_customFrustumCallback = cfc;
}

void SceneUtil::MWShadowTechnique::copyShadowStateSettings(osgUtil::CullVisitor& cv, ViewDependentData* vdd)
{
for (const auto& sd : vdd->getShadowDataList())
{
assignValidRegionSettings(cv, sd->_camera, sd->_sm_i, vdd->_uniforms[cv.getTraversalNumber()%2]);
assignShadowStateSettings(cv, sd->_camera, sd->_sm_i, vdd->_uniforms[cv.getTraversalNumber()%2]);
}
}

void MWShadowTechnique::update(osg::NodeVisitor& nv)
{
Expand Down Expand Up @@ -1053,6 +1064,8 @@ void MWShadowTechnique::cull(osgUtil::CullVisitor& cv)
_shadowedScene->osg::Group::traverse(cv);
return;
}

Uniforms& vddUniforms = vdd->_uniforms[cv.getTraversalNumber() % 2];

ShadowSettings* settings = getShadowedScene()->getShadowSettings();

Expand Down Expand Up @@ -1500,29 +1513,7 @@ void MWShadowTechnique::cull(osgUtil::CullVisitor& cv)

if (!orthographicViewFrustum && settings->getShadowMapProjectionHint()==ShadowSettings::PERSPECTIVE_SHADOW_MAP)
{
{
osg::Matrix validRegionMatrix = cv.getCurrentCamera()->getInverseViewMatrix() * camera->getViewMatrix() * camera->getProjectionMatrix();

std::string validRegionUniformName = "validRegionMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> validRegionUniform;

for (const auto & uniform : _uniforms[cv.getTraversalNumber() % 2])
{
if (uniform->getName() == validRegionUniformName)
{
validRegionUniform = uniform;
break;
}
}

if (!validRegionUniform)
{
validRegionUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, validRegionUniformName);
_uniforms[cv.getTraversalNumber() % 2].push_back(validRegionUniform);
}

validRegionUniform->set(validRegionMatrix);
}
assignValidRegionSettings(cv, camera, sm_i, vddUniforms);

if (settings->getMultipleShadowMapHint() == ShadowSettings::CASCADED)
adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get(), cascaseNear, cascadeFar);
Expand All @@ -1537,38 +1528,15 @@ void MWShadowTechnique::cull(osgUtil::CullVisitor& cv)
// 4.4 compute main scene graph TexGen + uniform settings + setup state
//
{
osg::Matrix shadowSpaceMatrix = cv.getCurrentCamera()->getInverseViewMatrix() *
camera->getViewMatrix() *
camera->getProjectionMatrix() *
osg::Matrix::translate(1.0,1.0,1.0) *
osg::Matrix::scale(0.5,0.5,0.5);

std::string shadowSpaceUniformName = "shadowSpaceMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> shadowSpaceUniform;

for (const auto & uniform : _uniforms[cv.getTraversalNumber() % 2])
{
if (uniform->getName() == shadowSpaceUniformName)
{
shadowSpaceUniform = uniform;
break;
}
}

if (!shadowSpaceUniform)
{
shadowSpaceUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, shadowSpaceUniformName);
_uniforms[cv.getTraversalNumber() % 2].push_back(shadowSpaceUniform);
}

shadowSpaceUniform->set(shadowSpaceMatrix);
assignShadowStateSettings(cv, camera, sm_i, vddUniforms);
}

// mark the light as one that has active shadows and requires shaders
pl.textureUnits.push_back(textureUnit);

// pass on shadow data to ShadowDataList
sd->_textureUnit = textureUnit;
sd->_sm_i = sm_i;

sdl.push_back(sd);

Expand Down Expand Up @@ -3080,6 +3048,61 @@ bool MWShadowTechnique::adjustPerspectiveShadowMapCameraSettings(osgUtil::Render
return true;
}

void MWShadowTechnique::assignShadowStateSettings(osgUtil::CullVisitor& cv, osg::Camera* camera, unsigned int sm_i, Uniforms& uniforms)
{
osg::Matrix inverseViewMatrix = osg::Matrix::inverse(*cv.getModelViewMatrix());
osg::Matrix shadowSpaceMatrix = inverseViewMatrix *
camera->getViewMatrix() *
camera->getProjectionMatrix() *
osg::Matrix::translate(1.0,1.0,1.0) *
osg::Matrix::scale(0.5,0.5,0.5);

std::string shadowSpaceUniformName = "shadowSpaceMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> shadowSpaceUniform;

for (const auto & uniform : uniforms)
{
if (uniform->getName() == shadowSpaceUniformName)
{
shadowSpaceUniform = uniform;
break;
}
}

if (!shadowSpaceUniform)
{
shadowSpaceUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, shadowSpaceUniformName);
uniforms.push_back(shadowSpaceUniform);
}

shadowSpaceUniform->set(shadowSpaceMatrix);
}

void SceneUtil::MWShadowTechnique::assignValidRegionSettings(osgUtil::CullVisitor & cv, osg::Camera* camera, unsigned int sm_i, Uniforms & uniforms)
{
osg::Matrix validRegionMatrix = osg::Matrix::inverse(*cv.getModelViewMatrix()) * camera->getViewMatrix() * camera->getProjectionMatrix();

std::string validRegionUniformName = "validRegionMatrix" + std::to_string(sm_i);
osg::ref_ptr<osg::Uniform> validRegionUniform;

for (const auto & uniform : uniforms)
{
if (uniform->getName() == validRegionUniformName)
{
validRegionUniform = uniform;
break;
}
}

if (!validRegionUniform)
{
validRegionUniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, validRegionUniformName);
uniforms.push_back(validRegionUniform);
}

validRegionUniform->set(validRegionMatrix);
}

void MWShadowTechnique::cullShadowReceivingScene(osgUtil::CullVisitor* cv) const
{
OSG_INFO<<"cullShadowReceivingScene()"<<std::endl;
Expand Down Expand Up @@ -3128,6 +3151,12 @@ osg::StateSet* MWShadowTechnique::prepareStateSetForRenderingShadow(ViewDependen
stateset->addUniform(uniform);
}

for(const auto& uniform : vdd._uniforms[traversalNumber % 2])
{
OSG_INFO<<"addUniform("<<uniform->getName()<<")"<<std::endl;
stateset->addUniform(uniform);
}

if (_program.valid())
{
stateset->setAttribute(_program.get());
Expand Down
10 changes: 9 additions & 1 deletion components/sceneutil/mwshadowtechnique.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ namespace SceneUtil {
* the resulting shadow map may be invalid. */
virtual void operator()(osgUtil::CullVisitor& cv, osg::BoundingBoxd& customClipSpace, osgUtil::CullVisitor*& sharedFrustumHint) = 0;
};
typedef std::vector< osg::ref_ptr<osg::Uniform> > Uniforms;

// forward declare
class ViewDependentData;
Expand Down Expand Up @@ -192,6 +193,7 @@ namespace SceneUtil {
ViewDependentData* _viewDependentData;

unsigned int _textureUnit;
unsigned int _sm_i;
osg::ref_ptr<osg::Texture2D> _texture;
osg::ref_ptr<osg::Camera> _camera;
};
Expand Down Expand Up @@ -228,6 +230,7 @@ namespace SceneUtil {

LightDataList _lightDataList;
ShadowDataList _shadowDataList;
std::array<Uniforms, 2> _uniforms;

unsigned int _numValidShadows;
};
Expand All @@ -240,6 +243,8 @@ namespace SceneUtil {

void setCustomFrustumCallback(CustomFrustumCallback* cfc);

void copyShadowStateSettings(osgUtil::CullVisitor& cv, ViewDependentData* vdd);

virtual void createShaders();

virtual bool selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const;
Expand All @@ -251,6 +256,10 @@ namespace SceneUtil {
virtual bool cropShadowCameraToMainFrustum(Frustum& frustum, osg::Camera* camera, double viewNear, double viewFar, std::vector<osg::Plane>& planeList);

virtual bool adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera, double viewNear, double viewFar);

virtual void assignShadowStateSettings(osgUtil::CullVisitor& cv, osg::Camera* camera, unsigned int sm_i, Uniforms& uniforms);

virtual void assignValidRegionSettings(osgUtil::CullVisitor& cv, osg::Camera* camera, unsigned int sm_i, Uniforms& uniforms);

virtual void cullShadowReceivingScene(osgUtil::CullVisitor* cv) const;

Expand Down Expand Up @@ -280,7 +289,6 @@ namespace SceneUtil {
osg::ref_ptr<osg::Texture2D> _fallbackBaseTexture;
osg::ref_ptr<osg::Texture2D> _fallbackShadowMapTexture;

typedef std::vector< osg::ref_ptr<osg::Uniform> > Uniforms;
std::array<Uniforms, 2> _uniforms;
osg::ref_ptr<osg::Program> _program;

Expand Down
4 changes: 2 additions & 2 deletions files/data/shaders/internal_distortion.omwfx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ fragment main {
{
const float multiplier = 0.14;

vec2 offset = omw_Texture2D(omw_SamplerDistortion, omw_TexCoord).rg;
vec2 offset = omw_GetDistortion(omw_TexCoord).rg;
offset *= multiplier;
offset = clamp(offset, vec2(-1.0), vec2(1.0));

float occlusionFactor = omw_Texture2D(omw_SamplerDistortion, omw_TexCoord+offset).b;
float occlusionFactor = omw_GetDistortion(omw_TexCoord+offset).b;

omw_FragColor = mix(omw_GetLastShader(omw_TexCoord + offset), omw_GetLastShader(omw_TexCoord), occlusionFactor);
}
Expand Down
5 changes: 2 additions & 3 deletions files/shaders/compatibility/bs/default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ uniform sampler2D normalMap;
varying vec2 normalMapUV;
#endif

uniform sampler2D opaqueDepthTex;

varying float euclideanDepth;
varying float linearDepth;

Expand All @@ -42,6 +40,7 @@ uniform float specStrength;
uniform bool useTreeAnim;
uniform float distortionStrength;

#include "lib/core/fragment.h.glsl"
#include "lib/light/lighting.glsl"
#include "lib/material/alpha.glsl"
#include "lib/util/distortion.glsl"
Expand All @@ -59,7 +58,7 @@ void main()
#if defined(DISTORTION) && DISTORTION
vec2 screenCoords = gl_FragCoord.xy / (screenRes * @distorionRTRatio);
gl_FragData[0].a *= getDiffuseColor().a;
gl_FragData[0] = applyDistortion(gl_FragData[0], distortionStrength, gl_FragCoord.z, texture2D(opaqueDepthTex, screenCoords).x);
gl_FragData[0] = applyDistortion(gl_FragData[0], distortionStrength, gl_FragCoord.z, sampleOpaqueDepthTex(screenCoords).x);

return;
#endif
Expand Down
4 changes: 2 additions & 2 deletions files/shaders/compatibility/bs/nolighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ uniform float far;
uniform float near;
uniform float alphaRef;

#include "lib/core/fragment.h.glsl"
#include "lib/material/alpha.glsl"

#include "compatibility/vertexcolors.glsl"
Expand All @@ -35,7 +36,6 @@ uniform float alphaRef;
#if @softParticles
#include "lib/particle/soft.glsl"

uniform sampler2D opaqueDepthTex;
uniform float particleSize;
uniform bool particleFade;
uniform float softFalloffDepth;
Expand Down Expand Up @@ -70,7 +70,7 @@ void main()
viewNormal,
near,
far,
texture2D(opaqueDepthTex, screenCoords).x,
sampleOpaqueDepthTex(screenCoords).x,
particleSize,
particleFade,
softFalloffDepth
Expand Down
Loading

0 comments on commit c04cd2d

Please sign in to comment.