Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MslRenderer camera initialization #1465

Conversation

nicolassavva-autodesk
Copy link
Contributor

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).

This addresses the same issue in a similar way to what was previously done for the GlslRenderer:

"Improvements to ShaderRenderer"(60b1053)

@nicolassavva-autodesk
Copy link
Contributor Author

@ashwinbhat @Morteeza @jstone-lucasfilm: Please have a look when you get a chance

@jstone-lucasfilm
Copy link
Member

This change looks good to me, thanks @nicolassavva-autodesk, and I'd be interested in thoughts from @Morteeza and @ashwinbhat.

@Morteeza
Copy link
Contributor

Morteeza commented Aug 21, 2023

Without setting view and world information, tests generate wrong results. I believe we need to find another way to set these:

MSL output after this change:
Copper_msl

GLSL output:
Copper_glsl

@nicolassavva-autodesk
Copy link
Contributor Author

I believe the difference comes from the following line:
https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/source/MaterialXRender/ShaderRenderer.cpp#L38
we can have the Metal path call createPerspectiveMatrixZP() instead of createPerspectiveMatrix()

@Morteeza
Copy link
Contributor

true. That probably will fix the issue. Btw, I noticed metal rendering tests were broken before. I opened a PR to fix it in case you want to try it locally.

@Morteeza
Copy link
Contributor

Also I believe this line needs to be removed too

@@ -35,7 +35,12 @@ ShaderRenderer::ShaderRenderer(unsigned int width, unsigned int height, Image::B
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 defined (__APPLE__)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to make this Metal specific so that it can still work with OpenGL based MacOS
@jstone-lucasfilm: what would you recommend for the cleanest way for supporting both here?
Perhaps we can move the MaterialXView specific customization to a reusable place:
https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/source/MaterialXView/CMakeLists.txt#L8-L27

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the default camera in ShaderRenderer and have it setup by the caller?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to that option, @ashwinbhat, though I'm imagining that there may still be value in providing a default camera for simple use cases. As long as we continue to provide the option of overriding the default camera, it seems reasonable to make this default as suitable as possible for each platform.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolassavva-autodesk What if we were to pass a perspective matrix convention boolean into the constructor for ShaderRenderer, allowing the caller to state which convention should be used for the default perspective matrix? This would allow the platform-independent ShaderRenderer class to remain agnostic of this platform detail, without requiring us to remove the default camera from ShaderRender entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced an enum to select between OpenGL and Metal (in lieu of a boolean)
This should suffice for now and we should eventually consider propagating the changes into a future refactor of the Camera class and the MaterialXViewer

Copy link
Contributor

@ashwinbhat ashwinbhat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend removing the default camera initialization ShaderRenderer so that the client can create and supply a camera that is suitable based on the underlying API implementation.
We already have setCamera API to allow this workflow.
@jstone-lucasfilm @Morteeza any thoughts on this suggestion?

@@ -35,7 +35,12 @@ ShaderRenderer::ShaderRenderer(unsigned int width, unsigned int height, Image::B
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 defined (__APPLE__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the default camera in ShaderRenderer and have it setup by the caller?

@nicolassavva-autodesk nicolassavva-autodesk force-pushed the savvan/MSLshader_render_fix branch 2 times, most recently from f01b0bb to c00d0b4 Compare August 23, 2023 13:51
@nicolassavva-autodesk
Copy link
Contributor Author

Here is a sample of the render tests using the latest updates:

MSL
Copper_msl

GLSL
Copper_glsl

@Morteeza
Copy link
Contributor

Thanks @nicolassavva-autodesk for changes and the rendered test.

I would recommend removing the default camera initialization ShaderRenderer so that the client can create and supply a camera that is suitable based on the underlying API implementation. We already have setCamera API to allow this workflow. @jstone-lucasfilm @Morteeza any thoughts on this suggestion?

Change look good as is. But as Ashwin suggested maybe it will be more explicit to remove camera GL/Metal camera initialization, and move it to test code implementations; something like this:
patch3.diff.zip

@nicolassavva-autodesk
Copy link
Contributor Author

Yes, agreed though perhaps that can wait for a future code refactor

@jstone-lucasfilm jstone-lucasfilm changed the title Remove MSLRenderer Camera initialization Remove MslRenderer camera initialization Aug 24, 2023
Copy link
Member

@jstone-lucasfilm jstone-lucasfilm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good approach to me, thanks @nicolassavva-autodesk.

With respect to naming conventions, what would you think about the following enumerated type for the new input argument?

enum MatrixConvention
{
    MatrixOpenGL = 0,
    MatrixMetal = 1
};

This would help to clarify which convention we're specifying with this argument, and the new enumeration could potentially be moved into a lower-level library (e.g. a math library) if needed further in the future.

@nicolassavva-autodesk
Copy link
Contributor Author

Yes, renaming the enums to something more explicit sounds good to me

@nicolassavva-autodesk
Copy link
Contributor Author

nicolassavva-autodesk commented Aug 24, 2023

I take it we are still keeping it as an enum class and perhaps do not need to repeat "Matrix" in the name so we get:

enum class MatrixConvention
{
    OpenGL = 0,
    Metal  = 1
};

MatrixConvention::Metal

@jstone-lucasfilm
Copy link
Member

That's completely fine, and although we don't always use enum classes in MaterialX, I think it's a good direction for the future.

@nicolassavva-autodesk
Copy link
Contributor Author

I just renamed the enum as discussed above and rebased the PR branch

Copy link
Member

@jstone-lucasfilm jstone-lucasfilm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thanks @nicolassavva-autodesk!

@jstone-lucasfilm jstone-lucasfilm merged commit 84adc65 into AcademySoftwareFoundation:main Aug 24, 2023
13 checks passed
Michaelredaa pushed a commit to Michaelredaa/MaterialX that referenced this pull request Oct 21, 2023
)

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).
@nicolassavva-autodesk nicolassavva-autodesk deleted the savvan/MSLshader_render_fix branch October 23, 2023 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants