Skip to content

Commit 3b9994b

Browse files
authored
Improve handling of types of semantics in glsl (shader-slang#964)
* Attempt to improve the glsl handling of hlsl semantics by taking into account the underlying glsl type. * Improve comments around 'NV_VIEWPORT_MASK' on glsl.
1 parent 1816935 commit 3b9994b

File tree

1 file changed

+92
-5
lines changed

1 file changed

+92
-5
lines changed

source/slang/ir-glsl-legalize.cpp

+92-5
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,18 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
229229

230230
auto semanticName = semanticNameSpelling.toLower();
231231

232+
// HLSL semantic types can be found here
233+
// https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-semantics
234+
235+
auto builder = context->getBuilder();
232236
IRType* requiredType = nullptr;
233237

234238
if(semanticName == "sv_position")
235239
{
240+
// float4 in hlsl & glsl
241+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_FragCoord.xhtml
242+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_Position.xhtml
243+
236244
// This semantic can either work like `gl_FragCoord`
237245
// when it is used as a fragment shader input, or
238246
// like `gl_Position` when used in other stages.
@@ -271,10 +279,19 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
271279
else if(semanticName == "sv_clipdistance")
272280
{
273281
// TODO: type conversion is required here.
282+
283+
// float in hlsl & glsl.
284+
// "Clip distance data. SV_ClipDistance values are each assumed to be a float32 signed distance to a plane."
285+
// In glsl clipping value meaning is probably different
286+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_ClipDistance.xhtml
287+
274288
name = "gl_ClipDistance";
275289
}
276290
else if(semanticName == "sv_culldistance")
277291
{
292+
// float in hlsl & glsl.
293+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_CullDistance.xhtml
294+
278295
context->requireGLSLExtension("ARB_cull_distance");
279296

280297
// TODO: type conversion is required here.
@@ -285,80 +302,121 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
285302
// TODO: deal with `gl_SampleMaskIn` when used as an input.
286303

287304
// TODO: type conversion is required here.
305+
306+
// uint in hlsl, int in glsl
307+
// https://www.opengl.org/sdk/docs/manglsl/docbook4/xhtml/gl_SampleMask.xml
308+
309+
requiredType = builder->getBasicType(BaseType::Int);
310+
288311
name = "gl_SampleMask";
289312
}
290313
else if(semanticName == "sv_depth")
291314
{
315+
// Float in hlsl & glsl
316+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_FragDepth.xhtml
292317
name = "gl_FragDepth";
293318
}
294319
else if(semanticName == "sv_depthgreaterequal")
295320
{
296321
// TODO: layout(depth_greater) out float gl_FragDepth;
322+
323+
// Type is 'unknown' in hlsl
297324
name = "gl_FragDepth";
298325
}
299326
else if(semanticName == "sv_depthlessequal")
300327
{
301328
// TODO: layout(depth_greater) out float gl_FragDepth;
329+
330+
// 'unknown' in hlsl, float in glsl
302331
name = "gl_FragDepth";
303332
}
304333
else if(semanticName == "sv_dispatchthreadid")
305334
{
335+
// uint3 in hlsl, uvec3 in glsl
336+
// https://www.opengl.org/sdk/docs/manglsl/docbook4/xhtml/gl_GlobalInvocationID.xml
306337
name = "gl_GlobalInvocationID";
307338

308-
auto builder = context->getBuilder();
309339
requiredType = builder->getVectorType(builder->getBasicType(BaseType::UInt), builder->getIntValue(builder->getIntType(), 3));
310340
}
311341
else if(semanticName == "sv_domainlocation")
312342
{
343+
// TODO: Not 100% confident that say float2 will convert into float3 glsl?
344+
// float2|3 in hlsl, vec3 in glsl
345+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_TessCoord.xhtml
346+
347+
requiredType = builder->getVectorType(builder->getBasicType(BaseType::Float), builder->getIntValue(builder->getIntType(), 3));
348+
313349
name = "gl_TessCoord";
314350
}
315351
else if(semanticName == "sv_groupid")
316352
{
353+
// uint3 in hlsl, uvec3 in glsl
354+
// https://www.opengl.org/sdk/docs/manglsl/docbook4/xhtml/gl_WorkGroupID.xml
317355
name = "gl_WorkGroupID";
318356

319-
auto builder = context->getBuilder();
320357
requiredType = builder->getVectorType(builder->getBasicType(BaseType::UInt), builder->getIntValue(builder->getIntType(), 3));
321358
}
322359
else if(semanticName == "sv_groupindex")
323360
{
361+
// uint in hlsl & in glsl
324362
name = "gl_LocalInvocationIndex";
325363
}
326364
else if(semanticName == "sv_groupthreadid")
327365
{
366+
// uint3 in hlsl, uvec3 in glsl
328367
name = "gl_LocalInvocationID";
329368

330-
auto builder = context->getBuilder();
331369
requiredType = builder->getVectorType(builder->getBasicType(BaseType::UInt), builder->getIntValue(builder->getIntType(), 3));
332370
}
333371
else if(semanticName == "sv_gsinstanceid")
334372
{
373+
// uint in hlsl, int in glsl
374+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_InvocationID.xhtml
375+
376+
requiredType = builder->getBasicType(BaseType::Int);
335377
name = "gl_InvocationID";
336378
}
337379
else if(semanticName == "sv_instanceid")
338380
{
381+
// https://docs.microsoft.com/en-us/windows/desktop/direct3d11/d3d10-graphics-programming-guide-input-assembler-stage-using#instanceid
382+
// uint in hlsl, int in glsl
383+
384+
requiredType = builder->getBasicType(BaseType::Int);
339385
name = "gl_InstanceIndex";
340386
}
341387
else if(semanticName == "sv_isfrontface")
342388
{
389+
// bool in hlsl & glsl
390+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_FrontFacing.xhtml
343391
name = "gl_FrontFacing";
344392
}
345393
else if(semanticName == "sv_outputcontrolpointid")
346394
{
395+
// uint in hlsl, int in glsl
396+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_InvocationID.xhtml
397+
347398
name = "gl_InvocationID";
399+
400+
requiredType = builder->getBasicType(BaseType::Int);
348401
}
349402
else if (semanticName == "sv_pointsize")
350403
{
404+
// float in hlsl & glsl
351405
name = "gl_PointSize";
352406
}
353407
else if(semanticName == "sv_primitiveid")
354408
{
409+
// uint in hlsl, int in glsl
410+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_PrimitiveID.xhtml
355411
name = "gl_PrimitiveID";
356412

357-
auto builder = context->getBuilder();
358413
requiredType = builder->getBasicType(BaseType::Int);
359414
}
360415
else if (semanticName == "sv_rendertargetarrayindex")
361416
{
417+
// uint on hlsl, int on glsl
418+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_Layer.xhtml
419+
362420
switch (context->getStage())
363421
{
364422
case Stage::Geometry:
@@ -376,27 +434,46 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
376434
}
377435

378436
name = "gl_Layer";
379-
requiredType = context->getBuilder()->getBasicType(BaseType::Int);
437+
requiredType = builder->getBasicType(BaseType::Int);
380438
}
381439
else if (semanticName == "sv_sampleindex")
382440
{
441+
// uint in hlsl, int in glsl
442+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_SampleID.xhtml
443+
444+
requiredType = builder->getBasicType(BaseType::Int);
383445
name = "gl_SampleID";
384446
}
385447
else if (semanticName == "sv_stencilref")
386448
{
449+
// uint in hlsl, int in glsl
450+
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_stencil_export.txt
451+
452+
requiredType = builder->getBasicType(BaseType::Int);
453+
387454
context->requireGLSLExtension("ARB_shader_stencil_export");
388455
name = "gl_FragStencilRef";
389456
}
390457
else if (semanticName == "sv_tessfactor")
391458
{
459+
// TODO(JS): Need to ensure the adjustType can handle such a scenario.
460+
// float[2|3|4] in hlsl, float[4] on glsl (ie both are arrays but might be different size)
461+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_TessLevelOuter.xhtml
462+
392463
name = "gl_TessLevelOuter";
393464
}
394465
else if (semanticName == "sv_vertexid")
395466
{
467+
// uint in hlsl, int in glsl (https://www.khronos.org/opengl/wiki/Built-in_Variable_(GLSL))
468+
requiredType = builder->getBasicType(BaseType::Int);
396469
name = "gl_VertexIndex";
397470
}
398471
else if (semanticName == "sv_viewportarrayindex")
399472
{
473+
// uint on hlsl, int on glsl
474+
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_ViewportIndex.xhtml
475+
476+
requiredType = builder->getBasicType(BaseType::Int);
400477
name = "gl_ViewportIndex";
401478
}
402479
else if (semanticName == "nv_x_right")
@@ -425,6 +502,16 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo(
425502
}
426503
else if (semanticName == "nv_viewport_mask")
427504
{
505+
// TODO: This doesn't seem to work correctly on it's own between hlsl/glsl
506+
507+
// Indeed on slang issue 109 claims this remains a problem
508+
// https://github.com/shader-slang/slang/issues/109
509+
510+
// On hlsl it's UINT related. "higher 16 bits for the right view, lower 16 bits for the left view."
511+
// There is use in hlsl shader code as uint4 - not clear if that varies
512+
// https://github.com/KhronosGroup/GLSL/blob/master/extensions/nvx/GL_NVX_multiview_per_view_attributes.txt
513+
// On glsl its highp int gl_ViewportMaskPerViewNV[];
514+
428515
context->requireGLSLVersion(ProfileVersion::GLSL_450);
429516
context->requireGLSLExtension("GL_NVX_multiview_per_view_attributes");
430517

0 commit comments

Comments
 (0)