Skip to content

Commit 9b7c1ea

Browse files
Merge branch 'main' into guard-mslprogram-updateAttributeList
2 parents 3a98031 + 6ce8fd0 commit 9b7c1ea

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

source/MaterialXRenderMsl/MetalTextureHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class MX_RENDERMSL_API MetalTextureHandler : public ImageHandler
8989
std::vector<unsigned int> _boundTextureLocations;
9090

9191
std::unordered_map<unsigned int, id<MTLTexture>> _metalTextureMap;
92-
std::unordered_map<unsigned int, std::pair<ImagePtr, ImageSamplingProperties>> _imageBindingInfo;
92+
std::unordered_map<unsigned int, ImageSamplingProperties> _imageBindingInfo;
9393
std::unordered_map<ImageSamplingProperties, id<MTLSamplerState>, ImageSamplingKeyHasher> _imageSamplerStateMap;
9494

9595
id<MTLDevice> _device = nil;

source/MaterialXRenderMsl/MetalTextureHandler.mm

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
return false;
3030
}
3131
}
32-
_imageBindingInfo[image->getResourceId()] = std::make_pair(image, samplingProperties);
32+
_imageBindingInfo[image->getResourceId()] = samplingProperties;
3333
return true;
3434
}
3535

@@ -72,7 +72,7 @@
7272
_boundTextureLocations[textureUnit] = image->getResourceId();
7373

7474
[renderCmdEncoder setFragmentTexture:_metalTextureMap[image->getResourceId()] atIndex:textureUnit];
75-
[renderCmdEncoder setFragmentSamplerState:getSamplerState(_imageBindingInfo[image->getResourceId()].second) atIndex:textureUnit];
75+
[renderCmdEncoder setFragmentSamplerState:getSamplerState(_imageBindingInfo[image->getResourceId()]) atIndex:textureUnit];
7676

7777
return true;
7878
}
@@ -90,15 +90,10 @@
9090

9191
id<MTLTexture> MetalTextureHandler::getMTLTextureForImage(unsigned int index) const
9292
{
93-
auto imageInfo = _imageBindingInfo.find(index);
94-
if (imageInfo != _imageBindingInfo.end())
93+
auto metalTexture = _metalTextureMap.find(index);
94+
if (metalTexture != _metalTextureMap.end())
9595
{
96-
if (!imageInfo->second.first)
97-
return nil;
98-
99-
auto metalTexture = _metalTextureMap.find(imageInfo->second.first->getResourceId());
100-
if (metalTexture != _metalTextureMap.end())
101-
return metalTexture->second;
96+
return metalTexture->second;
10297
}
10398

10499
return nil;
@@ -109,7 +104,7 @@
109104
auto imageInfo = _imageBindingInfo.find(index);
110105
if (imageInfo != _imageBindingInfo.end())
111106
{
112-
return getSamplerState(imageInfo->second.second);
107+
return getSamplerState(imageInfo->second);
113108
}
114109
return nil;
115110
}
@@ -202,54 +197,54 @@
202197
std::vector<unsigned char> rearrangedDataC;
203198
void* imageData = image->getResourceBuffer();
204199

205-
if ((pixelFormat == MTLPixelFormatRGBA32Float || pixelFormat == MTLPixelFormatRGBA8Unorm) && channelCount == 3)
200+
id<MTLBuffer> buffer = nil;
201+
if (imageData)
206202
{
207-
bool isFloat = pixelFormat == MTLPixelFormatRGBA32Float;
203+
if ((pixelFormat == MTLPixelFormatRGBA32Float || pixelFormat == MTLPixelFormatRGBA8Unorm) && channelCount == 3)
204+
{
205+
bool isFloat = pixelFormat == MTLPixelFormatRGBA32Float;
208206

209-
sourceBytesPerRow = sourceBytesPerRow / 3 * 4;
210-
sourceBytesPerImage = sourceBytesPerImage / 3 * 4;
207+
sourceBytesPerRow = sourceBytesPerRow / 3 * 4;
208+
sourceBytesPerImage = sourceBytesPerImage / 3 * 4;
211209

212-
size_t srcIdx = 0;
210+
size_t srcIdx = 0;
213211

214-
if (isFloat)
215-
{
216-
rearrangedDataF.resize(sourceBytesPerImage / sizeof(float));
217-
for (size_t dstIdx = 0; dstIdx < rearrangedDataF.size(); ++dstIdx)
212+
if (isFloat)
218213
{
219-
if ((dstIdx & 0x3) == 3)
214+
rearrangedDataF.resize(sourceBytesPerImage / sizeof(float));
215+
for (size_t dstIdx = 0; dstIdx < rearrangedDataF.size(); ++dstIdx)
220216
{
221-
rearrangedDataF[dstIdx] = 1.0f;
222-
continue;
217+
if ((dstIdx & 0x3) == 3)
218+
{
219+
rearrangedDataF[dstIdx] = 1.0f;
220+
continue;
221+
}
222+
223+
rearrangedDataF[dstIdx] = ((float*) imageData)[srcIdx++];
223224
}
224225

225-
rearrangedDataF[dstIdx] = ((float*) imageData)[srcIdx++];
226+
imageData = rearrangedDataF.data();
226227
}
227-
228-
imageData = rearrangedDataF.data();
229-
}
230-
else
231-
{
232-
rearrangedDataC.resize(sourceBytesPerImage);
233-
for (size_t dstIdx = 0; dstIdx < rearrangedDataC.size(); ++dstIdx)
228+
else
234229
{
235-
if ((dstIdx & 0x3) == 3)
230+
rearrangedDataC.resize(sourceBytesPerImage);
231+
for (size_t dstIdx = 0; dstIdx < rearrangedDataC.size(); ++dstIdx)
236232
{
237-
rearrangedDataC[dstIdx] = 255;
238-
continue;
233+
if ((dstIdx & 0x3) == 3)
234+
{
235+
rearrangedDataC[dstIdx] = 255;
236+
continue;
237+
}
238+
239+
rearrangedDataC[dstIdx] = ((unsigned char*) imageData)[srcIdx++];
239240
}
240241

241-
rearrangedDataC[dstIdx] = ((unsigned char*) imageData)[srcIdx++];
242+
imageData = rearrangedDataC.data();
242243
}
243244

244-
imageData = rearrangedDataC.data();
245+
channelCount = 4;
245246
}
246247

247-
channelCount = 4;
248-
}
249-
250-
id<MTLBuffer> buffer = nil;
251-
if (imageData)
252-
{
253248
buffer = [_device newBufferWithBytes:imageData
254249
length:sourceBytesPerImage
255250
options:MTLStorageModeShared];
@@ -281,7 +276,16 @@
281276
void MetalTextureHandler::releaseRenderResources(ImagePtr image)
282277
{
283278
if (!image)
279+
{
280+
for (auto iter : _imageCache)
281+
{
282+
if (iter.second)
283+
{
284+
releaseRenderResources(iter.second);
285+
}
286+
}
284287
return;
288+
}
285289

286290
if (image->getResourceId() == MslProgram::UNDEFINED_METAL_RESOURCE_ID)
287291
{

0 commit comments

Comments
 (0)