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

Add RGBA format in GpuShaderCreator for better Metal support (Issue #1956) #1984

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

wRosie
Copy link

@wRosie wRosie commented Jun 6, 2024

Hi OCIO team,

As per the discussion in #1956, I added RGBA texture format in GpuShaderCreator and made GpuShaderCreator store LUTs in RGBA when the shader language is in metal.

Here is the interface and behavior change involved in this PR:

  1. AddTEXTURE_RGBA_CHANNEL value to enum TextureType

  2. Add a TextureType parameter to add3DTexture(). The signature is consistent with addTexture().

  3. Add a TextureType& field to get3DTexture().

The addTexture()/getTexture() functions already take TextureType as a parameter, so I will not change their function signature. However, I updated their behavior -- I changed GPUShaderCreator to use RGBA formats when GPULanguage is Metal. That involves updating functions such as GetLut1DGPUShaderProgram() and GetLut3DGPUShaderProgram(). Also, getTexture() and get3DTexture() will return textures in RGBA type in Metal mode.

Additionally, I updated the tests and function calls to work with the new interface.

I am aware that the behavior is not tested on a per-platform basis, and I would love suggestions on that. If possible, I'd like to add tests to ensure that RGBA is used only in metal mode.

Thanks!

Copy link

linux-foundation-easycla bot commented Jun 6, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@doug-walker
Copy link
Collaborator

Thank you for the PR @wRosie ! Have you started the process of getting the corporate CLA agreement signed? Please don't hesitate to reach out on Slack if you have any questions about that.

@wRosie
Copy link
Author

wRosie commented Jun 6, 2024

Thank you for the PR @wRosie ! Have you started the process of getting the corporate CLA agreement signed? Please don't hesitate to reach out on Slack if you have any questions about that.

Yes I am working on that.

@wRosie wRosie force-pushed the feature/add_rgba_for_metal branch from 1b2664e to c1a48db Compare June 6, 2024 19:13
Copy link
Collaborator

@remia remia left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @wRosie and sorry for the delay in reviewing.

I triggered the CI workflows, please ignore the macOS stalled jobs, we use a macOS version no longer available in Github and will need to update the workflows shortly. Unfortunately, I don't think we have the Metal tests running in Github currently, you can try running them on your side and using ociodisplay in metal mode as well (use -metal flag).

I believe you will need to update the msl.mm file and remove the RGB_to_RGBA conversions after this change to have it work?

I think we are missing a Python binding updates, in PyGPUShaderCreator.cpp, you need to add the new textureType in enumTextureType.

I don't think we have GPU unit tests dedicated to specific APIs for now, might be a good addition, making sure Metal always returns Red or RGBA LUTs only.

src/OpenColorIO/GpuShaderUtils.cpp Outdated Show resolved Hide resolved
src/OpenColorIO/ops/lut1d/Lut1DOpGPU.cpp Show resolved Hide resolved
src/OpenColorIO/ops/lut1d/Lut1DOpGPU.cpp Outdated Show resolved Hide resolved
@doug-walker doug-walker requested a review from cozdas July 22, 2024 01:24
Copy link
Author

@wRosie wRosie left a comment

Choose a reason for hiding this comment

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

Thank you for the review, and I apologize for the delayed response.
Please let me know if you have additional comments. Thanks!

@doug-walker I am aware of the CLA approval issue. Thank you for the patience here, I am actively tracking it down -- it is just a lengthy process :(

src/OpenColorIO/GpuShaderUtils.cpp Outdated Show resolved Hide resolved
src/OpenColorIO/ops/lut1d/Lut1DOpGPU.cpp Outdated Show resolved Hide resolved
src/OpenColorIO/ops/lut1d/Lut1DOpGPU.cpp Show resolved Hide resolved
@wRosie wRosie force-pushed the feature/add_rgba_for_metal branch 2 times, most recently from d35ff53 to 7b611ef Compare August 19, 2024 18:30
@wRosie wRosie force-pushed the feature/add_rgba_for_metal branch from 646cf51 to 1f0fdb2 Compare August 19, 2024 18:52
Interpolation interpolation = INTERP_LINEAR;
m_shaderDesc->get3DTexture(idx, textureName, samplerName, edgelen, interpolation);
m_shaderDesc->get3DTexture(idx, textureName, samplerName, edgelen, channel, interpolation);

Copy link
Collaborator

@doug-walker doug-walker Aug 24, 2024

Choose a reason for hiding this comment

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

Following up on Remi's comment above, GitHub won't let me leave a comment at that line, but on line 233, AllocateTexture3D seems to assume it is getting an RGB array. It calls RGB_to_RGBA on it. Isn't the idea that this should not be necessary anymore?

Are the Metal GPU unit tests passing on your local build?

When I run "ctest -v" on your branch, I get a seg-fault in the Metal tests in the RGB_to_RGBA function.

@doug-walker
Copy link
Collaborator

@wRosie , just wanted to check in with you about this PR. Given that this would be an API-breaking change, it would need to be included in OCIO 2.4.0 which will be released on Sept. 30. Do you think you will be able to make the requested changes in time? Otherwise it will likely have to wait for OCIO 2.5.0 next fall.

@wRosie wRosie force-pushed the feature/add_rgba_for_metal branch from ad10217 to 69c7780 Compare September 18, 2024 04:39
Copy link
Collaborator

@remia remia left a comment

Choose a reason for hiding this comment

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

Thanks for the follow up @wRosie!

There are still a couple of things to address (see the code comments as well):

  • Can you please add the new Texture type to the Python bindings?
  • The unit tests for Metal currently Segfault, having a deeper look, this is due to the internal copy of the LUT array within the GPUShader not handling RGBA yet.

I have another issue on Metal test locally but it is only happening for the first test being run and is due to an OpenGL setup error where a non existing texture is attached to a framebuffer, simply initialising m_imageTexID to 0 and checking before calling glFramebufferTexture2D seems to fix it. But we can address this in a later PR and review if the Metal / OpenGL interactions are correctly setup.

std::vector<float> float4AdaptedLutValues;
RGB_to_RGBA(lutValues, 3*edgelen*edgelen*edgelen, float4AdaptedLutValues);
memcpy(float4AdaptedLutValues.data(), lutValues, 4 * edgelen*edgelen*edgelen * sizeof(float));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This results in a Segfault because the float4AdaptedLutValues vector is empty. My suggestion would be to remove this copy as it is no longer required and we can use the lutValues pointer directly below. Similar update can also applies to AllocateTexture2D where you can remove the two memcpy.

// (Using CacheID here to potentially allow reuse of existing textures.)
shaderCreator->add3DTexture(name.c_str(),

if(shaderCreator->getLanguage() == GPU_LANGUAGE_MSL_2_0){
Copy link
Collaborator

Choose a reason for hiding this comment

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

[Minor] Brace opening should be on the next line.

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.

3 participants