|
29 | 29 | return false; |
30 | 30 | } |
31 | 31 | } |
32 | | - _imageBindingInfo[image->getResourceId()] = std::make_pair(image, samplingProperties); |
| 32 | + _imageBindingInfo[image->getResourceId()] = samplingProperties; |
33 | 33 | return true; |
34 | 34 | } |
35 | 35 |
|
|
72 | 72 | _boundTextureLocations[textureUnit] = image->getResourceId(); |
73 | 73 |
|
74 | 74 | [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]; |
76 | 76 |
|
77 | 77 | return true; |
78 | 78 | } |
|
90 | 90 |
|
91 | 91 | id<MTLTexture> MetalTextureHandler::getMTLTextureForImage(unsigned int index) const |
92 | 92 | { |
93 | | - auto imageInfo = _imageBindingInfo.find(index); |
94 | | - if (imageInfo != _imageBindingInfo.end()) |
| 93 | + auto metalTexture = _metalTextureMap.find(index); |
| 94 | + if (metalTexture != _metalTextureMap.end()) |
95 | 95 | { |
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; |
102 | 97 | } |
103 | 98 |
|
104 | 99 | return nil; |
|
109 | 104 | auto imageInfo = _imageBindingInfo.find(index); |
110 | 105 | if (imageInfo != _imageBindingInfo.end()) |
111 | 106 | { |
112 | | - return getSamplerState(imageInfo->second.second); |
| 107 | + return getSamplerState(imageInfo->second); |
113 | 108 | } |
114 | 109 | return nil; |
115 | 110 | } |
|
202 | 197 | std::vector<unsigned char> rearrangedDataC; |
203 | 198 | void* imageData = image->getResourceBuffer(); |
204 | 199 |
|
205 | | - if ((pixelFormat == MTLPixelFormatRGBA32Float || pixelFormat == MTLPixelFormatRGBA8Unorm) && channelCount == 3) |
| 200 | + id<MTLBuffer> buffer = nil; |
| 201 | + if (imageData) |
206 | 202 | { |
207 | | - bool isFloat = pixelFormat == MTLPixelFormatRGBA32Float; |
| 203 | + if ((pixelFormat == MTLPixelFormatRGBA32Float || pixelFormat == MTLPixelFormatRGBA8Unorm) && channelCount == 3) |
| 204 | + { |
| 205 | + bool isFloat = pixelFormat == MTLPixelFormatRGBA32Float; |
208 | 206 |
|
209 | | - sourceBytesPerRow = sourceBytesPerRow / 3 * 4; |
210 | | - sourceBytesPerImage = sourceBytesPerImage / 3 * 4; |
| 207 | + sourceBytesPerRow = sourceBytesPerRow / 3 * 4; |
| 208 | + sourceBytesPerImage = sourceBytesPerImage / 3 * 4; |
211 | 209 |
|
212 | | - size_t srcIdx = 0; |
| 210 | + size_t srcIdx = 0; |
213 | 211 |
|
214 | | - if (isFloat) |
215 | | - { |
216 | | - rearrangedDataF.resize(sourceBytesPerImage / sizeof(float)); |
217 | | - for (size_t dstIdx = 0; dstIdx < rearrangedDataF.size(); ++dstIdx) |
| 212 | + if (isFloat) |
218 | 213 | { |
219 | | - if ((dstIdx & 0x3) == 3) |
| 214 | + rearrangedDataF.resize(sourceBytesPerImage / sizeof(float)); |
| 215 | + for (size_t dstIdx = 0; dstIdx < rearrangedDataF.size(); ++dstIdx) |
220 | 216 | { |
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++]; |
223 | 224 | } |
224 | 225 |
|
225 | | - rearrangedDataF[dstIdx] = ((float*) imageData)[srcIdx++]; |
| 226 | + imageData = rearrangedDataF.data(); |
226 | 227 | } |
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 |
234 | 229 | { |
235 | | - if ((dstIdx & 0x3) == 3) |
| 230 | + rearrangedDataC.resize(sourceBytesPerImage); |
| 231 | + for (size_t dstIdx = 0; dstIdx < rearrangedDataC.size(); ++dstIdx) |
236 | 232 | { |
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++]; |
239 | 240 | } |
240 | 241 |
|
241 | | - rearrangedDataC[dstIdx] = ((unsigned char*) imageData)[srcIdx++]; |
| 242 | + imageData = rearrangedDataC.data(); |
242 | 243 | } |
243 | 244 |
|
244 | | - imageData = rearrangedDataC.data(); |
| 245 | + channelCount = 4; |
245 | 246 | } |
246 | 247 |
|
247 | | - channelCount = 4; |
248 | | - } |
249 | | - |
250 | | - id<MTLBuffer> buffer = nil; |
251 | | - if (imageData) |
252 | | - { |
253 | 248 | buffer = [_device newBufferWithBytes:imageData |
254 | 249 | length:sourceBytesPerImage |
255 | 250 | options:MTLStorageModeShared]; |
|
281 | 276 | void MetalTextureHandler::releaseRenderResources(ImagePtr image) |
282 | 277 | { |
283 | 278 | if (!image) |
| 279 | + { |
| 280 | + for (auto iter : _imageCache) |
| 281 | + { |
| 282 | + if (iter.second) |
| 283 | + { |
| 284 | + releaseRenderResources(iter.second); |
| 285 | + } |
| 286 | + } |
284 | 287 | return; |
| 288 | + } |
285 | 289 |
|
286 | 290 | if (image->getResourceId() == MslProgram::UNDEFINED_METAL_RESOURCE_ID) |
287 | 291 | { |
|
0 commit comments