From dfe7352f2461290dacff6b75eeeaa3ab57b40f74 Mon Sep 17 00:00:00 2001 From: acheney Date: Wed, 5 Feb 2025 08:58:27 -0500 Subject: [PATCH 01/12] MaterialX Debugging --- pixel.glsl | 29 +++++++++++++++ source/slang/slang-emit.cpp | 4 ++ .../slang-ir-translate-glsl-global-var.cpp | 37 +++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 pixel.glsl diff --git a/pixel.glsl b/pixel.glsl new file mode 100644 index 0000000000..68a1d7d54b --- /dev/null +++ b/pixel.glsl @@ -0,0 +1,29 @@ +import glsl; + +#version 400 + + +in VertexData +{ + vec2 texcoord_0; +} vd; + +/* +in vec2 texcoord_0; +in vec2 texcoord_1; +in vec2 texcoord_2; +*/ + +out vec4 out1; + + + +void main() + +{ + + out1 = vec4(vd.texcoord_0, vd.texcoord_0); + //out1 = vec4(texcoord_0.x, texcoord_1.y, texcoord_2); + +} + diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 58376bbc1c..c56afa0d46 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -635,6 +635,8 @@ Result linkAndOptimizeIR( if (!isKhronosTarget(targetRequest) && requiredLoweringPassSet.glslSSBO) lowerGLSLShaderStorageBufferObjectsToStructuredBuffers(irModule, sink); + printf("Pre GLSL Translate\n"); + irModule->getModuleInst()->dump(); if (requiredLoweringPassSet.glslGlobalVar) translateGLSLGlobalVar(codeGenContext, irModule); @@ -643,6 +645,8 @@ Result linkAndOptimizeIR( fixEntryPointCallsites(irModule); + printf("Post GLSL Translate\n"); + irModule->getModuleInst()->dump(); // Replace any global constants with their values. // replaceGlobalConstants(irModule); diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index 7b2b8d1ee2..e8c62e4d93 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -83,8 +83,39 @@ struct GlobalVarTranslationContext auto key = builder.createStructKey(); inputKeys.add(key); builder.createStructField(inputStructType, key, inputType); - IRTypeLayout::Builder fieldTypeLayout(&builder); - IRVarLayout::Builder varLayoutBuilder(&builder, fieldTypeLayout.build()); + + IRTypeLayout::Builder* fieldTypeLayout; + IRStructTypeLayout::Builder structTypeLayoutBuilder(&builder); + IRTypeLayout::Builder basicTypeLayoutBuilder(&builder); + if (auto structType = as(inputType)) + { + fieldTypeLayout = &structTypeLayoutBuilder; + + // If the input is a struct, we need to add all the fields to the input + // struct type. + int i = 0; + + for (auto field : structType->getFields()) + { + i++; + printf("iteration %d\n", i); + + IRTypeLayout::Builder nestedFieldTypeLayout(&builder); + IRVarLayout::Builder nestedVarLayoutBuilder(&builder, nestedFieldTypeLayout.build()); + nestedFieldTypeLayout.addResourceUsage( + LayoutResourceKind::VaryingInput, + LayoutSize(1)); + auto fieldKey = builder.createStructKey(); + //builder.createStructField(structType, fieldKey, field->getFieldType()); + structTypeLayoutBuilder.addField(fieldKey, nestedVarLayoutBuilder.build()); + } + } + else + { + fieldTypeLayout = &basicTypeLayoutBuilder; + } + + IRVarLayout::Builder varLayoutBuilder(&builder, fieldTypeLayout->build()); varLayoutBuilder.setStage(entryPointDecor->getProfile().getStage()); if (auto semanticDecor = input->findDecoration()) { @@ -94,7 +125,7 @@ struct GlobalVarTranslationContext } else { - fieldTypeLayout.addResourceUsage( + fieldTypeLayout->addResourceUsage( LayoutResourceKind::VaryingInput, LayoutSize(1)); if (auto layoutDecor = findVarLayout(input)) From 9027c736c63834c5c2cb6c259a7ad95496d2842c Mon Sep 17 00:00:00 2001 From: acheney Date: Tue, 11 Feb 2025 13:30:38 -0500 Subject: [PATCH 02/12] Update --- source/slang/slang-ir-glsl-legalize.cpp | 43 +++++++++++++++++-- .../slang-ir-translate-glsl-global-var.cpp | 36 ++++++++++------ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 3ca441f96a..997276ce63 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3275,6 +3275,9 @@ void legalizeEntryPointParameterForGLSL( ptrType->getAddressSpace() == AddressSpace::Input || ptrType->getAddressSpace() == AddressSpace::BuiltinInput); +//DEBUG +// printf("Before global value for varying input:\n"); +pp->getModule()->getModuleInst()->dump(); auto globalValue = createGLSLGlobalVaryings( context, codeGenContext, @@ -3284,6 +3287,12 @@ void legalizeEntryPointParameterForGLSL( LayoutResourceKind::VaryingInput, stage, pp); + +// DEBUG +// Dump IR +printf("Created global value for varying input:\n"); +pp->getModule()->getModuleInst()->dump(); + tryReplaceUsesOfStageInput(context, globalValue, pp); for (auto dec : pp->getDecorations()) { @@ -3298,13 +3307,35 @@ void legalizeEntryPointParameterForGLSL( { for (auto elem : tupleVal->elements) { - if (elem.key == key) + // check if elem is a tuple + if (elem.val.flavor == ScalarizedVal::Flavor::tuple) { - realGlobalVar = elem.val.irValue; - break; + if (auto tupleVal = as(elem.val.impl)) + { + for (auto elem2 : tupleVal->elements) + { + if (elem2.key == key) + { + realGlobalVar = elem2.val.irValue; + break; + } + } + } + } + else + { + if (elem.key == key) + { + realGlobalVar = elem.val.irValue; + break; + } } } } + +//DEBUG +if (!realGlobalVar) + printf("Warning: Could not find real global var for shadowing decoration\n"); SLANG_ASSERT(realGlobalVar); // Remove all stores into the global var introduced during @@ -3331,6 +3362,12 @@ void legalizeEntryPointParameterForGLSL( globalVar->replaceUsesWith(realGlobalVar); globalVar->removeAndDeallocate(); } + +// DEBUG +// Dump IR +printf("Replaced global accessed with shadow:\n"); +pp->getModule()->getModuleInst()->dump(); +printf("printed\n"); } else { diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index e8c62e4d93..ec2f62e029 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -93,21 +93,15 @@ struct GlobalVarTranslationContext // If the input is a struct, we need to add all the fields to the input // struct type. - int i = 0; - for (auto field : structType->getFields()) - { - i++; - printf("iteration %d\n", i); - + { IRTypeLayout::Builder nestedFieldTypeLayout(&builder); IRVarLayout::Builder nestedVarLayoutBuilder(&builder, nestedFieldTypeLayout.build()); nestedFieldTypeLayout.addResourceUsage( LayoutResourceKind::VaryingInput, LayoutSize(1)); - auto fieldKey = builder.createStructKey(); - //builder.createStructField(structType, fieldKey, field->getFieldType()); - structTypeLayoutBuilder.addField(fieldKey, nestedVarLayoutBuilder.build()); + auto fieldKey = builder.createStructKey(); + structTypeLayoutBuilder.addField(fieldKey, nestedVarLayoutBuilder.build()); } } else @@ -174,10 +168,26 @@ struct GlobalVarTranslationContext .emitFieldExtract(inputType, builder.emitLoad(inputParam), inputKeys[i])); // Relate "global variable" to a "global parameter" for use later in compilation // to resolve a "global variable" shadowing a "global parameter" relationship. - builder.addGlobalVariableShadowingGlobalParameterDecoration( - inputParam, - input, - inputKeys[i]); + + // Decorate either the input or nested fields with "shadowing". + if (auto inputStructType = as(inputType)) + { + // TODO: Recurse into nested fields, this works on only one level of nesting. + for (auto field : inputStructType->getFields()) + { + builder.addGlobalVariableShadowingGlobalParameterDecoration( + inputParam, + field, + field->getKey()); + } + } + else + { + builder.addGlobalVariableShadowingGlobalParameterDecoration( + inputParam, + input, + inputKeys[i]); + } } // For each entry point, introduce a new parameter to represent each input parameter, From 1bbe4303be7b075cf23b3dfda4457ae08e643ce7 Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 10:28:32 -0500 Subject: [PATCH 03/12] Works without shadow optimization --- source/slang/slang-ir-glsl-legalize.cpp | 8 +++++--- .../slang/slang-ir-translate-glsl-global-var.cpp | 15 ++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 997276ce63..3bd61da418 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3292,10 +3292,11 @@ pp->getModule()->getModuleInst()->dump(); // Dump IR printf("Created global value for varying input:\n"); pp->getModule()->getModuleInst()->dump(); - tryReplaceUsesOfStageInput(context, globalValue, pp); +#if 0 for (auto dec : pp->getDecorations()) { + if (dec->getOp() != kIROp_GlobalVariableShadowingGlobalParameterDecoration) continue; auto globalVar = dec->getOperand(0); @@ -3362,12 +3363,13 @@ if (!realGlobalVar) globalVar->replaceUsesWith(realGlobalVar); globalVar->removeAndDeallocate(); } - -// DEBUG + + // DEBUG // Dump IR printf("Replaced global accessed with shadow:\n"); pp->getModule()->getModuleInst()->dump(); printf("printed\n"); +#endif } else { diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index ec2f62e029..9d80f3b9f9 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -163,14 +163,15 @@ struct GlobalVarTranslationContext setInsertBeforeOrdinaryInst(&builder, firstBlock->getFirstOrdinaryInst()); auto inputType = cast(input->getDataType())->getValueType(); builder.emitStore( - input, - builder - .emitFieldExtract(inputType, builder.emitLoad(inputParam), inputKeys[i])); + input, + builder + .emitFieldExtract(inputType, builder.emitLoad(inputParam), inputKeys[i])); + // Relate "global variable" to a "global parameter" for use later in compilation // to resolve a "global variable" shadowing a "global parameter" relationship. // Decorate either the input or nested fields with "shadowing". - if (auto inputStructType = as(inputType)) + if (false) //auto inputStructType = as(inputType)) { // TODO: Recurse into nested fields, this works on only one level of nesting. for (auto field : inputStructType->getFields()) @@ -181,13 +182,13 @@ struct GlobalVarTranslationContext field->getKey()); } } - else - { + //else + //{ builder.addGlobalVariableShadowingGlobalParameterDecoration( inputParam, input, inputKeys[i]); - } + //} } // For each entry point, introduce a new parameter to represent each input parameter, From 2c8f4dd92bc50b7b6f915b504b8b2bcbc98a0c97 Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 10:51:17 -0500 Subject: [PATCH 04/12] Clean up --- source/slang/slang-emit.cpp | 4 --- source/slang/slang-ir-glsl-legalize.cpp | 18 ++++++------- .../slang-ir-translate-glsl-global-var.cpp | 6 ++--- tests/glsl/interface-block.glsl | 25 +++++++++++++++++++ 4 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 tests/glsl/interface-block.glsl diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index c562d00a09..e20a4a90fd 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -641,8 +641,6 @@ Result linkAndOptimizeIR( if (!isKhronosTarget(targetRequest) && requiredLoweringPassSet.glslSSBO) lowerGLSLShaderStorageBufferObjectsToStructuredBuffers(irModule, sink); - printf("Pre GLSL Translate\n"); - irModule->getModuleInst()->dump(); if (requiredLoweringPassSet.glslGlobalVar) translateGLSLGlobalVar(codeGenContext, irModule); @@ -651,8 +649,6 @@ Result linkAndOptimizeIR( fixEntryPointCallsites(irModule); - printf("Post GLSL Translate\n"); - irModule->getModuleInst()->dump(); // Replace any global constants with their values. // replaceGlobalConstants(irModule); diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 3bd61da418..af985cfc70 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3290,8 +3290,8 @@ pp->getModule()->getModuleInst()->dump(); // DEBUG // Dump IR -printf("Created global value for varying input:\n"); -pp->getModule()->getModuleInst()->dump(); +//printf("Created global value for varying input:\n"); +// pp->getModule()->getModuleInst()->dump(); tryReplaceUsesOfStageInput(context, globalValue, pp); #if 0 for (auto dec : pp->getDecorations()) @@ -3334,9 +3334,8 @@ pp->getModule()->getModuleInst()->dump(); } } -//DEBUG -if (!realGlobalVar) - printf("Warning: Could not find real global var for shadowing decoration\n"); +//if (!realGlobalVar) +// printf("Warning: Could not find real global var for shadowing decoration\n"); SLANG_ASSERT(realGlobalVar); // Remove all stores into the global var introduced during @@ -3363,12 +3362,11 @@ if (!realGlobalVar) globalVar->replaceUsesWith(realGlobalVar); globalVar->removeAndDeallocate(); } - - // DEBUG + // Dump IR -printf("Replaced global accessed with shadow:\n"); -pp->getModule()->getModuleInst()->dump(); -printf("printed\n"); +//printf("Replaced global accessed with shadow:\n"); +//pp->getModule()->getModuleInst()->dump(); +//printf("printed\n"); #endif } else diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index 9d80f3b9f9..acae9b2898 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -163,9 +163,9 @@ struct GlobalVarTranslationContext setInsertBeforeOrdinaryInst(&builder, firstBlock->getFirstOrdinaryInst()); auto inputType = cast(input->getDataType())->getValueType(); builder.emitStore( - input, - builder - .emitFieldExtract(inputType, builder.emitLoad(inputParam), inputKeys[i])); + input, + builder + .emitFieldExtract(inputType, builder.emitLoad(inputParam), inputKeys[i])); // Relate "global variable" to a "global parameter" for use later in compilation // to resolve a "global variable" shadowing a "global parameter" relationship. diff --git a/tests/glsl/interface-block.glsl b/tests/glsl/interface-block.glsl new file mode 100644 index 0000000000..ce8d5312c3 --- /dev/null +++ b/tests/glsl/interface-block.glsl @@ -0,0 +1,25 @@ +//TEST:SIMPLE(filecheck=CHECK_GLSL): -target glsl -stage fragment -entry fragmentMain -allow-glsl + +//CHECK_GLSL: layout(location = 0) +//CHECK_GLSL: in vec2 vd_texcoord_0_0; + +//CHECK_GLSL: layout(location = 0) +//CHECK_GLSL: in vec2 vd_texcoord_1_0; + +import glsl; + +#version 400 + +in VertexData +{ + vec2 texcoord_0; + vec2 texcoord_1; +} vd; + +out vec4 out1; + +void fragmentMain() +{ + out1 = vec4(vd.texcoord_0, vd.texcoord_1); +} + From 38af779ec8a6e21168ad73c4a5d9e3416dfe4c47 Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 11:06:43 -0500 Subject: [PATCH 05/12] Update --- source/slang/slang-ir-glsl-legalize.cpp | 2 +- .../slang-ir-translate-glsl-global-var.cpp | 24 ++++--------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index af985cfc70..2854c8e757 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3277,7 +3277,7 @@ void legalizeEntryPointParameterForGLSL( //DEBUG // printf("Before global value for varying input:\n"); -pp->getModule()->getModuleInst()->dump(); +//pp->getModule()->getModuleInst()->dump(); auto globalValue = createGLSLGlobalVaryings( context, codeGenContext, diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index acae9b2898..059927d024 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -169,26 +169,10 @@ struct GlobalVarTranslationContext // Relate "global variable" to a "global parameter" for use later in compilation // to resolve a "global variable" shadowing a "global parameter" relationship. - - // Decorate either the input or nested fields with "shadowing". - if (false) //auto inputStructType = as(inputType)) - { - // TODO: Recurse into nested fields, this works on only one level of nesting. - for (auto field : inputStructType->getFields()) - { - builder.addGlobalVariableShadowingGlobalParameterDecoration( - inputParam, - field, - field->getKey()); - } - } - //else - //{ - builder.addGlobalVariableShadowingGlobalParameterDecoration( - inputParam, - input, - inputKeys[i]); - //} + builder.addGlobalVariableShadowingGlobalParameterDecoration( + inputParam, + input, + inputKeys[i]); } // For each entry point, introduce a new parameter to represent each input parameter, From 0759a896544e17675fe3c60f040f7ece0e35414a Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 14:03:00 -0500 Subject: [PATCH 06/12] Update --- .../slang-ir-translate-glsl-global-var.cpp | 48 +++++++++---------- tests/glsl/interface-block.glsl | 16 ++++++- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index 059927d024..d7396f9349 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -84,32 +84,24 @@ struct GlobalVarTranslationContext inputKeys.add(key); builder.createStructField(inputStructType, key, inputType); - IRTypeLayout::Builder* fieldTypeLayout; - IRStructTypeLayout::Builder structTypeLayoutBuilder(&builder); - IRTypeLayout::Builder basicTypeLayoutBuilder(&builder); - if (auto structType = as(inputType)) + IRTypeLayout::Builder fieldTypeLayoutBuilder(&builder); + IRTypeLayout* fieldTypeLayout = nullptr; + bool hasExistingLayout = false; + if (auto existingLayoutDecoration = input->findDecoration()) { - fieldTypeLayout = &structTypeLayoutBuilder; - - // If the input is a struct, we need to add all the fields to the input - // struct type. - for (auto field : structType->getFields()) - { - IRTypeLayout::Builder nestedFieldTypeLayout(&builder); - IRVarLayout::Builder nestedVarLayoutBuilder(&builder, nestedFieldTypeLayout.build()); - nestedFieldTypeLayout.addResourceUsage( - LayoutResourceKind::VaryingInput, - LayoutSize(1)); - auto fieldKey = builder.createStructKey(); - structTypeLayoutBuilder.addField(fieldKey, nestedVarLayoutBuilder.build()); + if (auto existingVarLayout = as(existingLayoutDecoration->getLayout())) + { + fieldTypeLayout = existingVarLayout->getTypeLayout(); + hasExistingLayout = true; } } - else - { - fieldTypeLayout = &basicTypeLayoutBuilder; - } - IRVarLayout::Builder varLayoutBuilder(&builder, fieldTypeLayout->build()); + if (!hasExistingLayout) + { + fieldTypeLayout = fieldTypeLayoutBuilder.build(); + } + + IRVarLayout::Builder varLayoutBuilder(&builder, fieldTypeLayout); varLayoutBuilder.setStage(entryPointDecor->getProfile().getStage()); if (auto semanticDecor = input->findDecoration()) { @@ -119,9 +111,12 @@ struct GlobalVarTranslationContext } else { - fieldTypeLayout->addResourceUsage( - LayoutResourceKind::VaryingInput, - LayoutSize(1)); + if (!hasExistingLayout) + { + fieldTypeLayoutBuilder.addResourceUsage( + LayoutResourceKind::VaryingInput, + LayoutSize(1)); + } if (auto layoutDecor = findVarLayout(input)) { if (auto offsetAttr = @@ -162,11 +157,12 @@ struct GlobalVarTranslationContext auto input = inputVars[i]; setInsertBeforeOrdinaryInst(&builder, firstBlock->getFirstOrdinaryInst()); auto inputType = cast(input->getDataType())->getValueType(); + // TODO: This could be more efficient as a Load(FieldAddress(inputParam, i)) + // operation instead of a FieldExtract(Load(inputParam)). builder.emitStore( input, builder .emitFieldExtract(inputType, builder.emitLoad(inputParam), inputKeys[i])); - // Relate "global variable" to a "global parameter" for use later in compilation // to resolve a "global variable" shadowing a "global parameter" relationship. builder.addGlobalVariableShadowingGlobalParameterDecoration( diff --git a/tests/glsl/interface-block.glsl b/tests/glsl/interface-block.glsl index ce8d5312c3..3d18db6c7f 100644 --- a/tests/glsl/interface-block.glsl +++ b/tests/glsl/interface-block.glsl @@ -1,25 +1,37 @@ //TEST:SIMPLE(filecheck=CHECK_GLSL): -target glsl -stage fragment -entry fragmentMain -allow-glsl //CHECK_GLSL: layout(location = 0) -//CHECK_GLSL: in vec2 vd_texcoord_0_0; +//CHECK_GLSL: out vec4 entryPointParam_fragmentMain_out1_0; //CHECK_GLSL: layout(location = 0) +//CHECK_GLSL: in vec2 vd_texcoord_0_0; + +//CHECK_GLSL: layout(location = 1) //CHECK_GLSL: in vec2 vd_texcoord_1_0; +//CHECK_GLSL: layout(location = 2) +//CHECK_GLSL: in vec4 vd_inner_texcoord_2_0; + import glsl; #version 400 +struct innerData +{ + vec4 texcoord_2; +}; + in VertexData { vec2 texcoord_0; vec2 texcoord_1; + innerData inner; } vd; out vec4 out1; void fragmentMain() { - out1 = vec4(vd.texcoord_0, vd.texcoord_1); + out1 = vec4(vd.texcoord_0, vd.texcoord_1.x, vd.inner.texcoord_2.y); } From 5fb989a405a1697945afbc9eedc754b44ab504b1 Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 14:03:57 -0500 Subject: [PATCH 07/12] Remove pixel.glsl --- pixel.glsl | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 pixel.glsl diff --git a/pixel.glsl b/pixel.glsl deleted file mode 100644 index 68a1d7d54b..0000000000 --- a/pixel.glsl +++ /dev/null @@ -1,29 +0,0 @@ -import glsl; - -#version 400 - - -in VertexData -{ - vec2 texcoord_0; -} vd; - -/* -in vec2 texcoord_0; -in vec2 texcoord_1; -in vec2 texcoord_2; -*/ - -out vec4 out1; - - - -void main() - -{ - - out1 = vec4(vd.texcoord_0, vd.texcoord_0); - //out1 = vec4(texcoord_0.x, texcoord_1.y, texcoord_2); - -} - From 15a6b8afde0fa5173a868d54e46e666bdf87a590 Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 14:11:57 -0500 Subject: [PATCH 08/12] Remove debug prints --- source/slang/slang-ir-glsl-legalize.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 2854c8e757..925877ee6a 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3275,9 +3275,6 @@ void legalizeEntryPointParameterForGLSL( ptrType->getAddressSpace() == AddressSpace::Input || ptrType->getAddressSpace() == AddressSpace::BuiltinInput); -//DEBUG -// printf("Before global value for varying input:\n"); -//pp->getModule()->getModuleInst()->dump(); auto globalValue = createGLSLGlobalVaryings( context, codeGenContext, @@ -3288,10 +3285,6 @@ void legalizeEntryPointParameterForGLSL( stage, pp); -// DEBUG -// Dump IR -//printf("Created global value for varying input:\n"); -// pp->getModule()->getModuleInst()->dump(); tryReplaceUsesOfStageInput(context, globalValue, pp); #if 0 for (auto dec : pp->getDecorations()) @@ -3362,11 +3355,6 @@ void legalizeEntryPointParameterForGLSL( globalVar->replaceUsesWith(realGlobalVar); globalVar->removeAndDeallocate(); } - -// Dump IR -//printf("Replaced global accessed with shadow:\n"); -//pp->getModule()->getModuleInst()->dump(); -//printf("printed\n"); #endif } else From 7598fa3219ca2e40957eb5ef63fde1b836e97d8d Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 16:01:17 -0500 Subject: [PATCH 09/12] Update slang-ir-glsl-legalize.cpp --- source/slang/slang-ir-glsl-legalize.cpp | 36 ++++++------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 925877ee6a..b4541a1eac 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3286,13 +3286,15 @@ void legalizeEntryPointParameterForGLSL( pp); tryReplaceUsesOfStageInput(context, globalValue, pp); -#if 0 for (auto dec : pp->getDecorations()) - { - + { if (dec->getOp() != kIROp_GlobalVariableShadowingGlobalParameterDecoration) continue; auto globalVar = dec->getOperand(0); + // if globalVar has struct type, skip this optimization + auto globalVarType = cast(globalVar->getDataType())->getValueType(); + if (as(globalVarType)) + continue; auto key = dec->getOperand(1); IRInst* realGlobalVar = nullptr; if (globalValue.flavor != ScalarizedVal::Flavor::tuple) @@ -3301,34 +3303,13 @@ void legalizeEntryPointParameterForGLSL( { for (auto elem : tupleVal->elements) { - // check if elem is a tuple - if (elem.val.flavor == ScalarizedVal::Flavor::tuple) + if (elem.key == key) { - if (auto tupleVal = as(elem.val.impl)) - { - for (auto elem2 : tupleVal->elements) - { - if (elem2.key == key) - { - realGlobalVar = elem2.val.irValue; - break; - } - } - } - } - else - { - if (elem.key == key) - { - realGlobalVar = elem.val.irValue; - break; - } + realGlobalVar = elem.val.irValue; + break; } } } - -//if (!realGlobalVar) -// printf("Warning: Could not find real global var for shadowing decoration\n"); SLANG_ASSERT(realGlobalVar); // Remove all stores into the global var introduced during @@ -3355,7 +3336,6 @@ void legalizeEntryPointParameterForGLSL( globalVar->replaceUsesWith(realGlobalVar); globalVar->removeAndDeallocate(); } -#endif } else { From 51aaa5106d54c8544ac552e6977fe264027e36cb Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 16:02:29 -0500 Subject: [PATCH 10/12] Update slang-ir-glsl-legalize.cpp --- source/slang/slang-ir-glsl-legalize.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index b4541a1eac..fce1e04123 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -3284,10 +3284,9 @@ void legalizeEntryPointParameterForGLSL( LayoutResourceKind::VaryingInput, stage, pp); - tryReplaceUsesOfStageInput(context, globalValue, pp); for (auto dec : pp->getDecorations()) - { + { if (dec->getOp() != kIROp_GlobalVariableShadowingGlobalParameterDecoration) continue; auto globalVar = dec->getOperand(0); From 4f233fbd57a5dc861b40880bd5cfa779dbc82d93 Mon Sep 17 00:00:00 2001 From: acheney Date: Thu, 13 Feb 2025 17:08:56 -0500 Subject: [PATCH 11/12] Update slang-ir-glsl-legalize.cpp --- source/slang/slang-ir-glsl-legalize.cpp | 89 +++++++++++++++---------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index fce1e04123..7acffdff14 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -2922,6 +2922,22 @@ void tryReplaceUsesOfStageInput( { fieldVal = element.val; break; + } + if (auto tupleValType = + as(element.val.impl)) + { + for (auto tupleElement : tupleValType->elements) + { + if (tupleElement.key == fieldKey) + { + fieldVal = tupleElement.val; + break; + } + } + } + if (fieldVal.flavor != ScalarizedVal::Flavor::none) + { + break; } } if (fieldVal.flavor != ScalarizedVal::Flavor::none) @@ -3290,49 +3306,54 @@ void legalizeEntryPointParameterForGLSL( if (dec->getOp() != kIROp_GlobalVariableShadowingGlobalParameterDecoration) continue; auto globalVar = dec->getOperand(0); - // if globalVar has struct type, skip this optimization auto globalVarType = cast(globalVar->getDataType())->getValueType(); - if (as(globalVarType)) - continue; - auto key = dec->getOperand(1); - IRInst* realGlobalVar = nullptr; - if (globalValue.flavor != ScalarizedVal::Flavor::tuple) - continue; - if (auto tupleVal = as(globalValue.impl)) + if (as(globalVarType)) { - for (auto elem : tupleVal->elements) - { - if (elem.key == key) - { - realGlobalVar = elem.val.irValue; - break; - } - } + tryReplaceUsesOfStageInput(context, globalValue, globalVar); } - SLANG_ASSERT(realGlobalVar); + else + { - // Remove all stores into the global var introduced during - // the initial glsl global var translation pass since we are - // going to replace the global var with a pointer to the real - // input, and it makes no sense to store values into such real - // input locations. - traverseUses( - globalVar, - [&](IRUse* use) + auto key = dec->getOperand(1); + IRInst* realGlobalVar = nullptr; + if (globalValue.flavor != ScalarizedVal::Flavor::tuple) + continue; + if (auto tupleVal = as(globalValue.impl)) { - auto user = use->getUser(); - if (auto store = as(user)) + for (auto elem : tupleVal->elements) { - if (store->getPtrUse() == use) + if (elem.key == key) { - store->removeAndDeallocate(); + realGlobalVar = elem.val.irValue; + break; } } - }); - // we will be replacing uses of `globalVarToReplace`. We need - // globalVarToReplaceNextUse to catch the next use before it is removed from the - // list of uses. - globalVar->replaceUsesWith(realGlobalVar); + } + SLANG_ASSERT(realGlobalVar); + + // Remove all stores into the global var introduced during + // the initial glsl global var translation pass since we are + // going to replace the global var with a pointer to the real + // input, and it makes no sense to store values into such real + // input locations. + traverseUses( + globalVar, + [&](IRUse* use) + { + auto user = use->getUser(); + if (auto store = as(user)) + { + if (store->getPtrUse() == use) + { + store->removeAndDeallocate(); + } + } + }); + // we will be replacing uses of `globalVarToReplace`. We need + // globalVarToReplaceNextUse to catch the next use before it is removed from the + // list of uses. + globalVar->replaceUsesWith(realGlobalVar); + } globalVar->removeAndDeallocate(); } } From ea00effbac1f4c26174c6324cf487ac083798126 Mon Sep 17 00:00:00 2001 From: slangbot <186143334+slangbot@users.noreply.github.com> Date: Thu, 13 Feb 2025 22:29:21 +0000 Subject: [PATCH 12/12] format code --- source/slang/slang-ir-glsl-legalize.cpp | 2 +- source/slang/slang-ir-translate-glsl-global-var.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 7acffdff14..67300dbe6a 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -2922,7 +2922,7 @@ void tryReplaceUsesOfStageInput( { fieldVal = element.val; break; - } + } if (auto tupleValType = as(element.val.impl)) { diff --git a/source/slang/slang-ir-translate-glsl-global-var.cpp b/source/slang/slang-ir-translate-glsl-global-var.cpp index d7396f9349..80ed3c3e4f 100644 --- a/source/slang/slang-ir-translate-glsl-global-var.cpp +++ b/source/slang/slang-ir-translate-glsl-global-var.cpp @@ -89,7 +89,8 @@ struct GlobalVarTranslationContext bool hasExistingLayout = false; if (auto existingLayoutDecoration = input->findDecoration()) { - if (auto existingVarLayout = as(existingLayoutDecoration->getLayout())) + if (auto existingVarLayout = + as(existingLayoutDecoration->getLayout())) { fieldTypeLayout = existingVarLayout->getTypeLayout(); hasExistingLayout = true; @@ -100,7 +101,7 @@ struct GlobalVarTranslationContext { fieldTypeLayout = fieldTypeLayoutBuilder.build(); } - + IRVarLayout::Builder varLayoutBuilder(&builder, fieldTypeLayout); varLayoutBuilder.setStage(entryPointDecor->getProfile().getStage()); if (auto semanticDecor = input->findDecoration()) @@ -116,7 +117,7 @@ struct GlobalVarTranslationContext fieldTypeLayoutBuilder.addResourceUsage( LayoutResourceKind::VaryingInput, LayoutSize(1)); - } + } if (auto layoutDecor = findVarLayout(input)) { if (auto offsetAttr =