Skip to content

Commit

Permalink
Remove MslRenderer camera initialization (#1465)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
nicolassavva-autodesk authored Aug 24, 2023
1 parent bcbf8a0 commit 84adc65
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 44 deletions.
15 changes: 12 additions & 3 deletions source/MaterialXRender/ShaderRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion source/MaterialXRender/ShaderRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ using ShaderRendererPtr = std::shared_ptr<class ShaderRenderer>;
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;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXRenderGlsl/GlslRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 0 additions & 8 deletions source/MaterialXRenderMsl/MslRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
};
Expand Down
32 changes: 1 addition & 31 deletions source/MaterialXRenderMsl/MslRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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)
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -217,9 +190,6 @@


[renderCmdEncoder setCullMode:MTLCullModeBack];

updateViewInformation();
updateWorldInformation();

try
{
Expand Down

0 comments on commit 84adc65

Please sign in to comment.