@@ -180,17 +180,67 @@ void TextureTypeInfo::writeFunc(
180
180
readNoneMode);
181
181
}
182
182
183
+ enum class DimType
184
+ {
185
+ Float,
186
+ Int,
187
+ UInt,
188
+
189
+ Count,
190
+ };
191
+
192
+ // The WGSL texture attribute types for 'expr' are unsigned int, and anything else requires a
193
+ // conversion.
194
+ template <typename S>
195
+ static String wgslTextureAttributeConversion (DimType type, S expr)
196
+ {
197
+
198
+ switch (type)
199
+ {
200
+
201
+ case DimType::UInt:
202
+ return expr;
203
+
204
+
205
+ case DimType::Float:
206
+ {
207
+ // Conversion to float is exact for values <= 2^24.
208
+ String castExpr (" f32(" );
209
+ castExpr.append (expr);
210
+ castExpr.append (" )" );
211
+ return castExpr;
212
+ }
213
+ break ;
214
+
215
+ case DimType::Int:
216
+ {
217
+ // We can assume two's complement and just do a bitcast, since texture dimensions can't
218
+ // be anywhere near big enough to yield a negative result.
219
+ String castExpr (" bitcast<i32>(" );
220
+ castExpr.append (expr);
221
+ castExpr.append (" )" );
222
+ return castExpr;
223
+ }
224
+ break ;
225
+
226
+ default :
227
+ SLANG_UNREACHABLE (" Unexpected DimType enum value" );
228
+ break ;
229
+ };
230
+ }
231
+
183
232
void TextureTypeInfo::writeGetDimensionFunctions ()
184
233
{
185
234
static const char * kComponentNames []{" x" , " y" , " z" , " w" };
186
235
187
236
SlangResourceShape baseShape = base.baseShape ;
188
237
189
238
// `GetDimensions`
190
- const char * dimParamTypes[] = {" out float " , " out int " , " out uint " };
191
- const char * dimParamTypesInner[] = {" float" , " int" , " uint" };
192
- for (int tid = 0 ; tid < 3 ; tid++)
239
+ const char * dimParamTypes[int (DimType::Count) ] = {" out float " , " out int " , " out uint " };
240
+ const char * dimParamTypesInner[int (DimType::Count) ] = {" float" , " int" , " uint" };
241
+ for (int tid = 0 ; tid < int (DimType::Count) ; tid++)
193
242
{
243
+ DimType dimType = DimType (tid);
194
244
auto t = dimParamTypes[tid];
195
245
auto rawT = dimParamTypesInner[tid];
196
246
@@ -227,8 +277,11 @@ void TextureTypeInfo::writeGetDimensionFunctions()
227
277
params << t << " width" ;
228
278
metal << " (*($" << String (paramCount) << " ) = $0.get_width("
229
279
<< String (metalMipLevel) << " ))," ;
230
- wgsl << " ($" << String (paramCount) << " ) = textureDimensions($0"
231
- << (includeMipInfo ? " , $1" : " " ) << " );" ;
280
+ wgsl << " ($" << String (paramCount) << " ) = "
281
+ << wgslTextureAttributeConversion (
282
+ dimType,
283
+ String (" textureDimensions($0" ) + (includeMipInfo ? " , $1" : " " ) + " )" )
284
+ << " ;" ;
232
285
233
286
sizeDimCount = 1 ;
234
287
break ;
@@ -240,13 +293,15 @@ void TextureTypeInfo::writeGetDimensionFunctions()
240
293
metal << " (*($" << String (paramCount) << " ) = $0.get_width("
241
294
<< String (metalMipLevel) << " ))," ;
242
295
wgsl << " var dim = textureDimensions($0" << (includeMipInfo ? " , $1" : " " ) << " );" ;
243
- wgsl << " ($" << String (paramCount) << " ) = dim.x;" ;
296
+ wgsl << " ($" << String (paramCount)
297
+ << " ) = " << wgslTextureAttributeConversion (dimType, " dim.x" ) << " ;" ;
244
298
245
299
++paramCount;
246
300
params << t << " height" ;
247
301
metal << " (*($" << String (paramCount) << " ) = $0.get_height("
248
302
<< String (metalMipLevel) << " ))," ;
249
- wgsl << " ($" << String (paramCount) << " ) = dim.y;" ;
303
+ wgsl << " ($" << String (paramCount)
304
+ << " ) = " << wgslTextureAttributeConversion (dimType, " dim.y" ) << " ;" ;
250
305
251
306
sizeDimCount = 2 ;
252
307
break ;
@@ -257,19 +312,22 @@ void TextureTypeInfo::writeGetDimensionFunctions()
257
312
metal << " (*($" << String (paramCount) << " ) = $0.get_width("
258
313
<< String (metalMipLevel) << " ))," ;
259
314
wgsl << " var dim = textureDimensions($0" << (includeMipInfo ? " , $1" : " " ) << " );" ;
260
- wgsl << " ($" << String (paramCount) << " ) = dim.x;" ;
315
+ wgsl << " ($" << String (paramCount)
316
+ << " ) = " << wgslTextureAttributeConversion (dimType, " dim.x" ) << " ;" ;
261
317
262
318
++paramCount;
263
319
params << t << " height," ;
264
320
metal << " (*($" << String (paramCount) << " ) = $0.get_height("
265
321
<< String (metalMipLevel) << " ))," ;
266
- wgsl << " ($" << String (paramCount) << " ) = dim.y;" ;
322
+ wgsl << " ($" << String (paramCount)
323
+ << " ) = " << wgslTextureAttributeConversion (dimType, " dim.y" ) << " ;" ;
267
324
268
325
++paramCount;
269
326
params << t << " depth" ;
270
327
metal << " (*($" << String (paramCount) << " ) = $0.get_depth("
271
328
<< String (metalMipLevel) << " ))," ;
272
- wgsl << " ($" << String (paramCount) << " ) = dim.z;" ;
329
+ wgsl << " ($" << String (paramCount)
330
+ << " ) = " << wgslTextureAttributeConversion (dimType, " dim.z" ) << " ;" ;
273
331
274
332
sizeDimCount = 3 ;
275
333
break ;
@@ -285,23 +343,29 @@ void TextureTypeInfo::writeGetDimensionFunctions()
285
343
++paramCount;
286
344
params << " , " << t << " elements" ;
287
345
metal << " (*($" << String (paramCount) << " ) = $0.get_array_size())," ;
288
- wgsl << " ($" << String (paramCount) << " ) = textureNumLayers($0);" ;
346
+ wgsl << " ($" << String (paramCount)
347
+ << " ) = " << wgslTextureAttributeConversion (dimType, " textureNumLayers($0)" )
348
+ << " ;" ;
289
349
}
290
350
291
351
if (isMultisample)
292
352
{
293
353
++paramCount;
294
354
params << " , " << t << " sampleCount" ;
295
355
metal << " (*($" << String (paramCount) << " ) = $0.get_num_samples())," ;
296
- wgsl << " ($" << String (paramCount) << " ) = textureNumSamples($0);" ;
356
+ wgsl << " ($" << String (paramCount)
357
+ << " ) = " << wgslTextureAttributeConversion (dimType, " textureNumSamples($0)" )
358
+ << " ;" ;
297
359
}
298
360
299
361
if (includeMipInfo)
300
362
{
301
363
++paramCount;
302
364
params << " , " << t << " numberOfLevels" ;
303
365
metal << " (*($" << String (paramCount) << " ) = $0.get_num_mip_levels())," ;
304
- wgsl << " ($" << String (paramCount) << " ) = textureNumLevels($0);" ;
366
+ wgsl << " ($" << String (paramCount)
367
+ << " ) = " << wgslTextureAttributeConversion (dimType, " textureNumLevels($0)" )
368
+ << " ;" ;
305
369
}
306
370
307
371
metal.reduceLength (metal.getLength () - 1 ); // drop the last comma
0 commit comments