Skip to content

Commit

Permalink
Merge branch 'main' into separate_glsl_msl_libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
jstone-lucasfilm authored Sep 5, 2024
2 parents 8edb03f + 29dc40a commit 14947dc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
3 changes: 2 additions & 1 deletion source/MaterialXRenderMsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ elseif(APPLE)
target_link_libraries(${TARGET_NAME}
PUBLIC
"-framework Foundation"
"-framework Metal")
"-framework Metal"
"-framework MetalPerformanceShaders")
elseif(UNIX)
target_link_libraries(${TARGET_NAME}
PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXRenderMsl/MetalFramebuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
height:height
mipmapped:NO];
[texDescriptor setStorageMode:MTLStorageModePrivate];
[texDescriptor setUsage:MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead];
[texDescriptor setUsage:MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite];

if (extColorTexture == nil)
{
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXRenderMsl/MetalState.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct MX_RENDERMSL_API MetalState
void endEncoder();
void endCommandBuffer();

void waitForComplition();
void waitForCompletion();

MaterialX::MetalFramebufferPtr currentFramebuffer();

Expand Down
6 changes: 3 additions & 3 deletions source/MaterialXRenderMsl/MetalState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

MTLTileRenderPipelineDescriptor* renderPipelineDescriptor = [MTLTileRenderPipelineDescriptor new];
[renderPipelineDescriptor setRasterSampleCount:1];
[[renderPipelineDescriptor colorAttachments][0] setPixelFormat:MTLPixelFormatBGRA8Unorm];
[[renderPipelineDescriptor colorAttachments][0] setPixelFormat:MTLPixelFormatRGBA16Float];
[renderPipelineDescriptor setTileFunction:function];
linearToSRGB_pso = [device newRenderPipelineStateWithTileDescriptor:renderPipelineDescriptor options:0 reflection:nil error:&error];
}
Expand Down Expand Up @@ -171,7 +171,7 @@
MTLRenderPipelineDescriptor* renderPipelineDesc = [MTLRenderPipelineDescriptor new];
[renderPipelineDesc setVertexFunction:vertexfunction];
[renderPipelineDesc setFragmentFunction:Fragmentfunction];
[[renderPipelineDesc colorAttachments][0] setPixelFormat:MTLPixelFormatBGRA8Unorm];
[[renderPipelineDesc colorAttachments][0] setPixelFormat:MTLPixelFormatRGBA16Float];
[renderPipelineDesc setDepthAttachmentPixelFormat:MTLPixelFormatDepth32Float];
linearToSRGB_pso = [device newRenderPipelineStateWithDescriptor:renderPipelineDesc error:&error];
}
Expand Down Expand Up @@ -225,7 +225,7 @@
[cmdBuffer waitUntilCompleted];
}

void MetalState::waitForComplition()
void MetalState::waitForCompletion()
{
std::unique_lock<std::mutex> lock(inFlightMutex);
while (inFlightCommandBuffers != 0)
Expand Down
30 changes: 23 additions & 7 deletions source/MaterialXView/RenderPipelineMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include <nanogui/messagedialog.h>

#include <MetalPerformanceShaders/MetalPerformanceShaders.h>

namespace
{

Expand Down Expand Up @@ -59,10 +61,10 @@
MTL(device),
width * _viewer->m_pixel_ratio,
height * _viewer->m_pixel_ratio,
4, mx::Image::BaseType::UINT8,
4, mx::Image::BaseType::HALF,
MTL(supportsTiledPipeline) ?
(id<MTLTexture>)color_texture : nil,
false, MTLPixelFormatBGRA8Unorm));
false, MTLPixelFormatRGBA16Float));
}

void MetalRenderPipeline::resizeFramebuffer(int width, int height,
Expand Down Expand Up @@ -697,16 +699,30 @@
unsigned int width = MTL(currentFramebuffer())->getWidth();
unsigned int height = MTL(currentFramebuffer())->getHeight();

MTL(waitForComplition());
MTL(waitForCompletion());

id<MTLTexture> srcTexture = MTL(supportsTiledPipeline) ?
(id<MTLTexture>)_viewer->_colorTexture :
MTL(currentFramebuffer())->getColorTexture();

mx::MetalFramebufferPtr framebuffer = mx::MetalFramebuffer::create(
MTL(device),
width, height, 4,
mx::Image::BaseType::UINT8,
MTL(supportsTiledPipeline) ?
(id<MTLTexture>)_viewer->_colorTexture :
MTL(currentFramebuffer())->getColorTexture(),
nil,
false, MTLPixelFormatBGRA8Unorm);
mx::ImagePtr frame = framebuffer->getColorImage(MTL(cmdQueue));
id<MTLTexture> dstTexture = framebuffer->getColorTexture();

id<MTLCommandQueue> cmdQueue = MTL(cmdQueue);

// Copy with format conversion
MPSImageConversion* conversion = [[MPSImageConversion alloc] initWithDevice:MTL(device)];
id<MTLCommandBuffer> cmdBuffer = [cmdQueue commandBuffer];
[conversion encodeToCommandBuffer:cmdBuffer sourceTexture:srcTexture destinationTexture:dstTexture];
[cmdBuffer commit];
[cmdBuffer waitUntilCompleted];

mx::ImagePtr frame = framebuffer->getColorImage(cmdQueue);

// Flips the captured image
std::vector<unsigned char> tmp(frame->getRowStride());
Expand Down
7 changes: 6 additions & 1 deletion source/MaterialXView/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const float DEFAULT_CAMERA_ZOOM = 1.0f;

namespace
{
#ifdef MATERIALXVIEW_METAL_BACKEND
const bool USE_FLOAT_BUFFER = true;
#else
const bool USE_FLOAT_BUFFER = false;
#endif

const int MIN_ENV_SAMPLE_COUNT = 4;
const int MAX_ENV_SAMPLE_COUNT = 1024;
Expand Down Expand Up @@ -152,7 +157,7 @@ Viewer::Viewer(const std::string& materialFilename,
int screenHeight,
const mx::Color3& screenColor) :
ng::Screen(ng::Vector2i(screenWidth, screenHeight), "MaterialXView",
true, false, true, true, false, 4, 0),
true, false, true, true, USE_FLOAT_BUFFER, 4, 0),
_window(nullptr),
_materialFilename(materialFilename),
_meshFilename(meshFilename),
Expand Down

0 comments on commit 14947dc

Please sign in to comment.