Skip to content

Commit

Permalink
[WGSL] Initial support for override default values
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=262622
rdar://116466820

Reviewed by Mike Wyrzykowski.

Pass the initializer expression to the API, which can later be evaluated with
the evaluation function introduced in 268833@main.

* Source/WebGPU/WGSL/GlobalVariableRewriter.cpp:
(WGSL::RewriteGlobalVariables::usesOverride):
* Source/WebGPU/WGSL/WGSL.h:
* Source/WebGPU/WebGPU/Pipeline.mm:
(WebGPU::createConstantValues):

Canonical link: https://commits.webkit.org/268905@main
  • Loading branch information
tadeuzagallo committed Oct 5, 2023
1 parent e8585e2 commit aba1459
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Source/WebGPU/WGSL/GlobalVariableRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ void RewriteGlobalVariables::usesOverride(AST::Variable& variable)
}
}

m_entryPointInformation->specializationConstants.add(originalName, Reflection::SpecializationConstant { variable.name(), constantType });
m_entryPointInformation->specializationConstants.add(originalName, Reflection::SpecializationConstant { variable.name(), constantType, variable.maybeInitializer() });
}

void RewriteGlobalVariables::insertStructs(const UsedResources& usedResources)
Expand Down
1 change: 1 addition & 0 deletions Source/WebGPU/WGSL/WGSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ enum class SpecializationConstantType : uint8_t {
struct SpecializationConstant {
String mangledName;
SpecializationConstantType type;
AST::Expression* defaultValue;
};

struct EntryPointInformation {
Expand Down
41 changes: 37 additions & 4 deletions Source/WebGPU/WebGPU/Pipeline.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@
return { };

auto constantValues = [MTLFunctionConstantValues new];
for (auto& kvp : entryPointInformation.specializationConstants) {
auto& specializationConstant = kvp.value;
if (!specializationConstant.defaultValue)
continue;

auto constantValue = WGSL::evaluate(*kvp.value.defaultValue, wgslConstantValues);
auto addResult = wgslConstantValues.add(specializationConstant.mangledName, constantValue);
ASSERT_UNUSED(addResult, addResult.isNewEntry);

switch (specializationConstant.type) {
case WGSL::Reflection::SpecializationConstantType::Boolean: {
bool value = constantValue.toBool();
[constantValues setConstantValue:&value type:MTLDataTypeBool withName:specializationConstant.mangledName];
break;
}
case WGSL::Reflection::SpecializationConstantType::Float: {
float value = constantValue.toDouble();
[constantValues setConstantValue:&value type:MTLDataTypeFloat withName:specializationConstant.mangledName];
break;
}
case WGSL::Reflection::SpecializationConstantType::Int: {
int value = constantValue.toInt();
[constantValues setConstantValue:&value type:MTLDataTypeInt withName:specializationConstant.mangledName];
break;
}
case WGSL::Reflection::SpecializationConstantType::Unsigned: {
unsigned value = constantValue.toInt();
[constantValues setConstantValue:&value type:MTLDataTypeUInt withName:specializationConstant.mangledName];
break;
}
}
}

for (uint32_t i = 0; i < constantCount; ++i) {
const auto& entry = constants[i];
auto indexIterator = entryPointInformation.specializationConstants.find(fromAPI(entry.key));
Expand All @@ -77,25 +110,25 @@
switch (specializationConstant.type) {
case WGSL::Reflection::SpecializationConstantType::Boolean: {
bool value = entry.value;
wgslConstantValues.add(fromAPI(entry.key), value);
wgslConstantValues.set(fromAPI(entry.key), value);
[constantValues setConstantValue:&value type:MTLDataTypeBool withName:specializationConstant.mangledName];
break;
}
case WGSL::Reflection::SpecializationConstantType::Float: {
float value = entry.value;
wgslConstantValues.add(fromAPI(entry.key), value);
wgslConstantValues.set(fromAPI(entry.key), value);
[constantValues setConstantValue:&value type:MTLDataTypeFloat withName:specializationConstant.mangledName];
break;
}
case WGSL::Reflection::SpecializationConstantType::Int: {
int value = entry.value;
wgslConstantValues.add(fromAPI(entry.key), value);
wgslConstantValues.set(fromAPI(entry.key), value);
[constantValues setConstantValue:&value type:MTLDataTypeInt withName:specializationConstant.mangledName];
break;
}
case WGSL::Reflection::SpecializationConstantType::Unsigned: {
unsigned value = entry.value;
wgslConstantValues.add(fromAPI(entry.key), value);
wgslConstantValues.set(fromAPI(entry.key), value);
[constantValues setConstantValue:&value type:MTLDataTypeUInt withName:specializationConstant.mangledName];
break;
}
Expand Down

0 comments on commit aba1459

Please sign in to comment.