Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SkSL::WGSLCodeGenerator::SwizzleLValue Class Reference
Inheritance diagram for SkSL::WGSLCodeGenerator::SwizzleLValue:
SkSL::WGSLCodeGenerator::LValue

Public Member Functions

 SwizzleLValue (const Context &ctx, std::string name, const Type &t, const ComponentArray &c)
 
std::string load () override
 
std::string store (const std::string &value) override
 
- Public Member Functions inherited from SkSL::WGSLCodeGenerator::LValue
virtual ~LValue ()=default
 

Detailed Description

Definition at line 1158 of file SkSLWGSLCodeGenerator.cpp.

Constructor & Destructor Documentation

◆ SwizzleLValue()

SkSL::WGSLCodeGenerator::SwizzleLValue::SwizzleLValue ( const Context ctx,
std::string  name,
const Type t,
const ComponentArray c 
)
inline

Definition at line 1161 of file SkSLWGSLCodeGenerator.cpp.

1162 : fContext(ctx)
1163 , fName(std::move(name))
1164 , fType(t)
1165 , fComponents(c) {
1166 // If the component array doesn't cover the entire value, we need to create masks for
1167 // writing back into the lvalue. For example, if the type is vec4 and the component array
1168 // holds `zx`, a GLSL assignment would look like:
1169 // name.zx = new_value;
1170 //
1171 // The equivalent WGSL assignment statement would look like:
1172 // name = vec4<f32>(new_value, name.xw).yzxw;
1173 //
1174 // This replaces name.zy with new_value.xy, and leaves name.xw at their original values.
1175 // By convention, we always put the new value first and the original values second; it might
1176 // be possible to find better arrangements which simplify the assignment overall, but we
1177 // don't attempt this.
1178 int fullSlotCount = fType.slotCount();
1179 SkASSERT(fullSlotCount <= 4);
1180
1181 // First, see which components are used.
1182 // The assignment swizzle must not reuse components.
1183 bool used[4] = {};
1184 for (int8_t component : fComponents) {
1185 SkASSERT(!used[component]);
1186 used[component] = true;
1187 }
1188
1189 // Any untouched components will need to be fetched from the original value.
1190 for (int index = 0; index < fullSlotCount; ++index) {
1191 if (!used[index]) {
1192 fUntouchedComponents.push_back(index);
1193 }
1194 }
1195
1196 // The reintegration swizzle needs to move the components back into their proper slots.
1197 // First, place the new-value components into the proper slots.
1198 fReintegrationSwizzle.resize(fullSlotCount);
1199 for (int index = 0; index < fComponents.size(); ++index) {
1200 fReintegrationSwizzle[fComponents[index]] = index;
1201 }
1202 // Then, refill the untouched slots with the original values.
1203 int originalValueComponentIndex = fComponents.size();
1204 for (int index = 0; index < fullSlotCount; ++index) {
1205 if (!used[index]) {
1206 fReintegrationSwizzle[index] = originalValueComponentIndex++;
1207 }
1208 }
1209 }
#define SkASSERT(cond)
Definition SkAssert.h:116
virtual size_t slotCount() const
Definition SkSLType.h:457
void resize(size_t count)
Definition SkTArray.h:418
int size() const
Definition SkTArray.h:416
const char * name
Definition fuchsia.cc:50

Member Function Documentation

◆ load()

std::string SkSL::WGSLCodeGenerator::SwizzleLValue::load ( )
inlineoverridevirtual

Implements SkSL::WGSLCodeGenerator::LValue.

Definition at line 1211 of file SkSLWGSLCodeGenerator.cpp.

1211 {
1212 return fName + "." + Swizzle::MaskString(fComponents);
1213 }
static std::string MaskString(const ComponentArray &inComponents)

◆ store()

std::string SkSL::WGSLCodeGenerator::SwizzleLValue::store ( const std::string &  value)
inlineoverridevirtual

Implements SkSL::WGSLCodeGenerator::LValue.

Definition at line 1215 of file SkSLWGSLCodeGenerator.cpp.

1215 {
1216 // `variable = `
1217 std::string result = fName;
1218 result += " = ";
1219
1220 if (fUntouchedComponents.empty()) {
1221 // `(new_value).wzyx;`
1222 result += '(';
1223 result += value;
1224 result += ").";
1225 result += Swizzle::MaskString(fReintegrationSwizzle);
1226 } else {
1227 // `vec4<f32>((new_value), `
1228 result += to_wgsl_type(fContext, fType);
1229 result += "((";
1230 result += value;
1231 result += "), ";
1232
1233 // `variable.yz).xzwy;`
1234 result += fName;
1235 result += '.';
1236 result += Swizzle::MaskString(fUntouchedComponents);
1237 result += ").";
1238 result += Swizzle::MaskString(fReintegrationSwizzle);
1239 }
1240 return result + ';';
1241 }
bool empty() const
Definition SkTArray.h:194
uint8_t value
GAsyncResult * result

The documentation for this class was generated from the following file: