summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Lemire <paul.lemire@kdab.com>2020-11-26 11:48:42 +0100
committerPaul Lemire <paul.lemire@kdab.com>2020-11-26 16:42:10 +0100
commit9c682cf8ffe302719df1202d89e560e4fe67739d (patch)
tree744a1d8aeea9e14cf442e359a1110d6860b8ecfe
parent1e2902ba3e1285fb2db5a90712135b130fdbbb84 (diff)
Use RHI's clipSpaceCorrMatrix for backend where Y is not up
It's better to rely on RHI instead of a hard coded matrix. Also modify the UBO to provide the clipSpaceCorrectionMatrix and the uncorrected projectionMatrix. All the other matrices we compute and provide use the corrected projectionMatrix but these two new matrices give users the libertity of bypassing the correction if they need to. Pick-to: 6.0 6.0.0 Change-Id: I52e409cc9b38ea0e939d19d92e2ddd907aa7d1cb Reviewed-by: Mike Krus <mike.krus@kdab.com>
-rw-r--r--src/extras/shaders/rhi/default_pos_norm.vert2
-rw-r--r--src/extras/shaders/rhi/default_pos_norm_tex_tan.vert2
-rw-r--r--src/extras/shaders/rhi/default_pos_tex.vert2
-rw-r--r--src/extras/shaders/rhi/defaultuniforms.inc2
-rw-r--r--src/extras/shaders/rhi/gooch.frag2
-rw-r--r--src/extras/shaders/rhi/pervertexcolor.frag2
-rw-r--r--src/extras/shaders/rhi/skybox.frag2
-rw-r--r--src/extras/shaders/rhi/skybox.vert2
-rw-r--r--src/extras/shaders/rhi/unlittexture.vert2
-rw-r--r--src/plugins/renderers/rhi/renderer/renderview.cpp16
-rw-r--r--src/plugins/renderers/rhi/renderer/renderview_p.h8
-rw-r--r--tests/manual/rhi/simple-rhi-cpp/main.cpp4
-rw-r--r--tests/manual/rhi/ubo/main.qml4
13 files changed, 37 insertions, 13 deletions
diff --git a/src/extras/shaders/rhi/default_pos_norm.vert b/src/extras/shaders/rhi/default_pos_norm.vert
index 52f14fbdc..0d352123e 100644
--- a/src/extras/shaders/rhi/default_pos_norm.vert
+++ b/src/extras/shaders/rhi/default_pos_norm.vert
@@ -59,6 +59,8 @@ layout(location = 1) out vec3 worldNormal;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/default_pos_norm_tex_tan.vert b/src/extras/shaders/rhi/default_pos_norm_tex_tan.vert
index 5679d8681..80ca6e55c 100644
--- a/src/extras/shaders/rhi/default_pos_norm_tex_tan.vert
+++ b/src/extras/shaders/rhi/default_pos_norm_tex_tan.vert
@@ -63,6 +63,8 @@ layout(location = 3) out vec2 texCoord;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/default_pos_tex.vert b/src/extras/shaders/rhi/default_pos_tex.vert
index 44176ef35..d9d9cd512 100644
--- a/src/extras/shaders/rhi/default_pos_tex.vert
+++ b/src/extras/shaders/rhi/default_pos_tex.vert
@@ -59,6 +59,8 @@ layout(location = 3) out vec2 texCoord;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/defaultuniforms.inc b/src/extras/shaders/rhi/defaultuniforms.inc
index df01c3eeb..f0ee8cfd9 100644
--- a/src/extras/shaders/rhi/defaultuniforms.inc
+++ b/src/extras/shaders/rhi/defaultuniforms.inc
@@ -1,6 +1,8 @@
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/gooch.frag b/src/extras/shaders/rhi/gooch.frag
index 8a0a32f18..cfa40a17a 100644
--- a/src/extras/shaders/rhi/gooch.frag
+++ b/src/extras/shaders/rhi/gooch.frag
@@ -8,6 +8,8 @@ layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/pervertexcolor.frag b/src/extras/shaders/rhi/pervertexcolor.frag
index da3ea149e..156c595fd 100644
--- a/src/extras/shaders/rhi/pervertexcolor.frag
+++ b/src/extras/shaders/rhi/pervertexcolor.frag
@@ -9,6 +9,8 @@ layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/skybox.frag b/src/extras/shaders/rhi/skybox.frag
index 649894b15..eedf80231 100644
--- a/src/extras/shaders/rhi/skybox.frag
+++ b/src/extras/shaders/rhi/skybox.frag
@@ -9,6 +9,8 @@ layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/skybox.vert b/src/extras/shaders/rhi/skybox.vert
index 8f3de6f62..61383f730 100644
--- a/src/extras/shaders/rhi/skybox.vert
+++ b/src/extras/shaders/rhi/skybox.vert
@@ -6,6 +6,8 @@ layout(location = 0) out vec3 texCoord0;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/extras/shaders/rhi/unlittexture.vert b/src/extras/shaders/rhi/unlittexture.vert
index 08f06e817..e343f45b7 100644
--- a/src/extras/shaders/rhi/unlittexture.vert
+++ b/src/extras/shaders/rhi/unlittexture.vert
@@ -9,6 +9,8 @@ layout(location = 1) out vec2 texCoord;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/src/plugins/renderers/rhi/renderer/renderview.cpp b/src/plugins/renderers/rhi/renderer/renderview.cpp
index 073c7fb04..bed1ef641 100644
--- a/src/plugins/renderers/rhi/renderer/renderview.cpp
+++ b/src/plugins/renderers/rhi/renderer/renderview.cpp
@@ -142,18 +142,11 @@ static QRectF resolveViewport(const QRectF &fractionalViewport, const QSize &sur
fractionalViewport.height() * surfaceSize.height());
}
-static Matrix4x4 getProjectionMatrix(const CameraLens *lens, bool yIsUp)
+static Matrix4x4 getProjectionMatrix(const CameraLens *lens)
{
Matrix4x4 m;
if (lens)
m = lens->projection();
- if (!yIsUp) {
- const Matrix4x4 rev { 1, 0, 0, 0,
- 0, -1, 0, 0,
- 0, 0, 0.5, 0.5,
- 0, 0, 0, 1 };
- return rev * m;
- }
return m;
}
@@ -1083,7 +1076,10 @@ void RenderView::updateRenderCommand(const EntityRenderCommandDataSubView &subVi
{
// Update RenderViewUBO (Qt3D standard uniforms)
const bool yIsUp = m_renderer->submissionContext()->rhi()->isYUpInNDC();
- const Matrix4x4 projectionMatrix = getProjectionMatrix(m_renderCameraLens, yIsUp);
+
+ const Matrix4x4 clipCorrectionMatrix = Matrix4x4(m_renderer->submissionContext()->rhi()->clipSpaceCorrMatrix());
+ const Matrix4x4 unCorrectedProjectionMatrix = getProjectionMatrix(m_renderCameraLens);
+ const Matrix4x4 projectionMatrix = clipCorrectionMatrix * unCorrectedProjectionMatrix;
const Matrix4x4 inverseViewMatrix = m_viewMatrix.inverted();
const Matrix4x4 inversedProjectionMatrix = projectionMatrix.inverted();
const Matrix4x4 viewProjectionMatrix = (projectionMatrix * m_viewMatrix);
@@ -1091,6 +1087,8 @@ void RenderView::updateRenderCommand(const EntityRenderCommandDataSubView &subVi
{
memcpy(&m_renderViewUBO.viewMatrix, &m_viewMatrix, sizeof(Matrix4x4));
memcpy(&m_renderViewUBO.projectionMatrix, &projectionMatrix, sizeof(Matrix4x4));
+ memcpy(&m_renderViewUBO.clipCorrectionMatrix, &clipCorrectionMatrix, sizeof(Matrix4x4));
+ memcpy(&m_renderViewUBO.uncorrectedProjectionMatrix, &unCorrectedProjectionMatrix, sizeof(Matrix4x4));
memcpy(&m_renderViewUBO.viewProjectionMatrix, &viewProjectionMatrix, sizeof(Matrix4x4));
memcpy(&m_renderViewUBO.inverseViewMatrix, &inverseViewMatrix, sizeof(Matrix4x4));
memcpy(&m_renderViewUBO.inverseProjectionMatrix, &inversedProjectionMatrix,
diff --git a/src/plugins/renderers/rhi/renderer/renderview_p.h b/src/plugins/renderers/rhi/renderer/renderview_p.h
index df5fe0438..dad66d2d5 100644
--- a/src/plugins/renderers/rhi/renderer/renderview_p.h
+++ b/src/plugins/renderers/rhi/renderer/renderview_p.h
@@ -125,13 +125,12 @@ struct Q_AUTOTEST_EXPORT BlitFramebufferInfo
QBlitFramebuffer::InterpolationMethod interpolationMethod;
};
-// This class is kind of analogous to RenderBin but I want to avoid trampling
-// on that until we get this working
-
struct RenderViewUBO
{
float viewMatrix[16];
float projectionMatrix[16];
+ float uncorrectedProjectionMatrix[16];
+ float clipCorrectionMatrix[16];
float viewProjectionMatrix[16];
float inverseViewMatrix[16];
float inverseProjectionMatrix[16];
@@ -147,7 +146,7 @@ struct RenderViewUBO
float yUpInNDC;
float yUpInFBO;
};
-static_assert(sizeof(RenderViewUBO) == sizeof(float) * (8 * 16 + 1 * 4 + 1 * 3 + 6 * 1),
+static_assert(sizeof(RenderViewUBO) == sizeof(float) * (10 * 16 + 1 * 4 + 1 * 3 + 6 * 1),
"UBO doesn't match std140");
class Q_AUTOTEST_EXPORT RenderView
@@ -348,6 +347,7 @@ private:
Qt3DCore::QNodeIdVector m_layerFilterIds;
Matrix4x4 m_viewMatrix;
Matrix4x4 m_viewProjectionMatrix;
+ Matrix4x4 m_clipCorrectionMatrix;
Vector3D m_eyePos;
Vector3D m_eyeViewDir;
diff --git a/tests/manual/rhi/simple-rhi-cpp/main.cpp b/tests/manual/rhi/simple-rhi-cpp/main.cpp
index 123647ac1..f839012f2 100644
--- a/tests/manual/rhi/simple-rhi-cpp/main.cpp
+++ b/tests/manual/rhi/simple-rhi-cpp/main.cpp
@@ -109,6 +109,8 @@ layout(location = 0) out vec3 color;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
@@ -150,6 +152,8 @@ layout(location = 0) in vec3 color;
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
diff --git a/tests/manual/rhi/ubo/main.qml b/tests/manual/rhi/ubo/main.qml
index b658a55b7..33f2adb99 100644
--- a/tests/manual/rhi/ubo/main.qml
+++ b/tests/manual/rhi/ubo/main.qml
@@ -109,6 +109,8 @@ Entity {
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;
@@ -147,6 +149,8 @@ Entity {
layout(std140, binding = 0) uniform qt3d_render_view_uniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
+ mat4 uncorrectedProjectionMatrix;
+ mat4 clipCorrectionMatrix;
mat4 viewProjectionMatrix;
mat4 inverseViewMatrix;
mat4 inverseProjectionMatrix;