From 84adc65600101118fee687dbd5f5c8e9db9ab95b Mon Sep 17 00:00:00 2001 From: nicolassavva-autodesk <61437351+nicolassavva-autodesk@users.noreply.github.com> Date: Thu, 24 Aug 2023 13:07:31 -0700 Subject: [PATCH] Remove MslRenderer camera initialization (#1465) Allow ShaderRenderer camera to be modified by the client in subclass renderers when using Metal. The PR removes the Metal Renderer specific camera initialization in MslRenderer (and now handled by ShaderRenderer). --- source/MaterialXRender/ShaderRenderer.cpp | 15 ++++++++-- source/MaterialXRender/ShaderRenderer.h | 11 ++++++- source/MaterialXRenderGlsl/GlslRenderer.cpp | 2 +- source/MaterialXRenderMsl/MslRenderer.h | 8 ------ source/MaterialXRenderMsl/MslRenderer.mm | 32 +-------------------- 5 files changed, 24 insertions(+), 44 deletions(-) diff --git a/source/MaterialXRender/ShaderRenderer.cpp b/source/MaterialXRender/ShaderRenderer.cpp index a5e81d1f68..b621e5cd88 100644 --- a/source/MaterialXRender/ShaderRenderer.cpp +++ b/source/MaterialXRender/ShaderRenderer.cpp @@ -25,17 +25,26 @@ const float DEFAULT_FAR_PLANE = 100.0f; // ShaderRenderer methods // -ShaderRenderer::ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) : +ShaderRenderer::ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType, MatrixConvention matrixConvention) : _width(width), _height(height), - _baseType(baseType) + _baseType(baseType), + _matrixConvention(matrixConvention) { // Initialize a default camera. float fH = std::tan(DEFAULT_FIELD_OF_VIEW / 360.0f * PI) * DEFAULT_NEAR_PLANE; float fW = fH * 1.0f; _camera = Camera::create(); _camera->setViewMatrix(Camera::createViewMatrix(DEFAULT_EYE_POSITION, DEFAULT_TARGET_POSITION, DEFAULT_UP_VECTOR)); - _camera->setProjectionMatrix(Camera::createPerspectiveMatrix(-fW, fW, -fH, fH, DEFAULT_NEAR_PLANE, DEFAULT_FAR_PLANE)); + + if (_matrixConvention == ShaderRenderer::MatrixConvention::Metal) + { + _camera->setProjectionMatrix(Camera::createPerspectiveMatrixZP(-fW, fW, -fH, fH, DEFAULT_NEAR_PLANE, DEFAULT_FAR_PLANE)); + } + else // MatrixConvention::OpenGL (default) + { + _camera->setProjectionMatrix(Camera::createPerspectiveMatrix(-fW, fW, -fH, fH, DEFAULT_NEAR_PLANE, DEFAULT_FAR_PLANE)); + } } void ShaderRenderer::createProgram(ShaderPtr) diff --git a/source/MaterialXRender/ShaderRenderer.h b/source/MaterialXRender/ShaderRenderer.h index 2c6f5fbf87..b049f81126 100644 --- a/source/MaterialXRender/ShaderRenderer.h +++ b/source/MaterialXRender/ShaderRenderer.h @@ -30,6 +30,12 @@ using ShaderRendererPtr = std::shared_ptr; class MX_RENDER_API ShaderRenderer { public: + /// Viewing API matrix conventions designation (default to OpenGL). + enum class MatrixConvention + { + OpenGL = 0, + Metal = 1 + }; /// A map with name and source code for each shader stage. using StageMap = StringMap; @@ -123,13 +129,16 @@ class MX_RENDER_API ShaderRenderer /// @} protected: - ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType); + ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType, + MatrixConvention matrixConvention = MatrixConvention::OpenGL); protected: unsigned int _width; unsigned int _height; Image::BaseType _baseType; + MatrixConvention _matrixConvention; + CameraPtr _camera; ImageHandlerPtr _imageHandler; GeometryHandlerPtr _geometryHandler; diff --git a/source/MaterialXRenderGlsl/GlslRenderer.cpp b/source/MaterialXRenderGlsl/GlslRenderer.cpp index e81ded996e..bf2c8cd849 100644 --- a/source/MaterialXRenderGlsl/GlslRenderer.cpp +++ b/source/MaterialXRenderGlsl/GlslRenderer.cpp @@ -25,7 +25,7 @@ GlslRendererPtr GlslRenderer::create(unsigned int width, unsigned int height, Im } GlslRenderer::GlslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) : - ShaderRenderer(width, height, baseType), + ShaderRenderer(width, height, baseType, MatrixConvention::OpenGL), _initialized(false), _screenColor(DEFAULT_SCREEN_COLOR_LIN_REC709) { diff --git a/source/MaterialXRenderMsl/MslRenderer.h b/source/MaterialXRenderMsl/MslRenderer.h index bf968078fe..1c54bdbc6a 100644 --- a/source/MaterialXRenderMsl/MslRenderer.h +++ b/source/MaterialXRenderMsl/MslRenderer.h @@ -126,9 +126,6 @@ class MX_RENDERMSL_API MslRenderer : public ShaderRenderer protected: MslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType); - - virtual void updateViewInformation(); - virtual void updateWorldInformation(); void triggerProgrammaticCapture(); void stopProgrammaticCapture(); @@ -146,11 +143,6 @@ class MX_RENDERMSL_API MslRenderer : public ShaderRenderer bool _initialized; - const Vector3 _eye; - const Vector3 _center; - const Vector3 _up; - float _objectScale; - SimpleWindowPtr _window; Color3 _screenColor; }; diff --git a/source/MaterialXRenderMsl/MslRenderer.mm b/source/MaterialXRenderMsl/MslRenderer.mm index f6de188b2f..072f6f1a3a 100644 --- a/source/MaterialXRenderMsl/MslRenderer.mm +++ b/source/MaterialXRenderMsl/MslRenderer.mm @@ -14,13 +14,6 @@ MATERIALX_NAMESPACE_BEGIN -const float PI = std::acos(-1.0f); - -// View information -const float FOV_PERSP = 45.0f; // degrees -const float NEAR_PLANE_PERSP = 0.05f; -const float FAR_PLANE_PERSP = 100.0f; - // // MslRenderer methods // @@ -36,20 +29,14 @@ } MslRenderer::MslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) : - ShaderRenderer(width, height, baseType), + ShaderRenderer(width, height, baseType, MatrixConvention::Metal), _initialized(false), - _eye(0.0f, 0.0f, 3.0f), - _center(0.0f, 0.0f, 0.0f), - _up(0.0f, 1.0f, 0.0f), - _objectScale(1.0f), _screenColor(DEFAULT_SCREEN_COLOR_LIN_REC709) { _program = MslProgram::create(); _geometryHandler = GeometryHandler::create(); _geometryHandler->addLoader(TinyObjLoader::create()); - - _camera = Camera::create(); } void MslRenderer::initialize(RenderContextHandle) @@ -160,20 +147,6 @@ } -void MslRenderer::updateViewInformation() -{ - float fH = std::tan(FOV_PERSP / 360.0f * PI) * NEAR_PLANE_PERSP; - float fW = fH * 1.0f; - - _camera->setViewMatrix(Camera::createViewMatrix(_eye, _center, _up)); - _camera->setProjectionMatrix(Camera::createPerspectiveMatrixZP(-fW, fW, -fH, fH, NEAR_PLANE_PERSP, FAR_PLANE_PERSP)); -} - -void MslRenderer::updateWorldInformation() -{ - _camera->setWorldMatrix(Matrix44::createScale(Vector3(_objectScale))); -} - void MslRenderer::triggerProgrammaticCapture() { MTLCaptureManager* captureManager = [MTLCaptureManager sharedCaptureManager]; @@ -217,9 +190,6 @@ [renderCmdEncoder setCullMode:MTLCullModeBack]; - - updateViewInformation(); - updateWorldInformation(); try {