OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLSPIRVCodeGenerator.h" | 8 #include "SkSLSPIRVCodeGenerator.h" |
9 | 9 |
10 #include "string.h" | 10 #include "string.h" |
(...skipping 2345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2356 this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationB
uffer); | 2356 this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationB
uffer); |
2357 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers)
; | 2357 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers)
; |
2358 SpvId ptrType = this->nextId(); | 2358 SpvId ptrType = this->nextId(); |
2359 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConst
antBuffer); | 2359 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConst
antBuffer); |
2360 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConsta
ntBuffer); | 2360 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConsta
ntBuffer); |
2361 this->writeLayout(intf.fVariable.fModifiers.fLayout, result); | 2361 this->writeLayout(intf.fVariable.fModifiers.fLayout, result); |
2362 fVariableMap[&intf.fVariable] = result; | 2362 fVariableMap[&intf.fVariable] = result; |
2363 return result; | 2363 return result; |
2364 } | 2364 } |
2365 | 2365 |
2366 void SPIRVCodeGenerator::writeGlobalVars(const VarDeclaration& decl, std::ostrea
m& out) { | 2366 void SPIRVCodeGenerator::writeGlobalVars(const VarDeclarations& decl, std::ostre
am& out) { |
2367 for (size_t i = 0; i < decl.fVars.size(); i++) { | 2367 for (size_t i = 0; i < decl.fVars.size(); i++) { |
2368 if (!decl.fVars[i]->fIsReadFrom && !decl.fVars[i]->fIsWrittenTo && | 2368 const VarDeclaration& varDecl = decl.fVars[i]; |
2369 !(decl.fVars[i]->fModifiers.fFlags & (Modifiers::kIn_Flag | | 2369 const Variable* var = varDecl.fVar; |
2370 Modifiers::kOut_Flag | | 2370 if (!var->fIsReadFrom && !var->fIsWrittenTo && |
2371 Modifiers::kUniform_Flag))
) { | 2371 !(var->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 2372 Modifiers::kOut_Flag | |
| 2373 Modifiers::kUniform_Flag))) { |
2372 // variable is dead and not an input / output var (the Vulkan debug
layers complain if | 2374 // variable is dead and not an input / output var (the Vulkan debug
layers complain if |
2373 // we elide an interface var, even if it's dead) | 2375 // we elide an interface var, even if it's dead) |
2374 continue; | 2376 continue; |
2375 } | 2377 } |
2376 SpvStorageClass_ storageClass; | 2378 SpvStorageClass_ storageClass; |
2377 if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kIn_Flag) { | 2379 if (var->fModifiers.fFlags & Modifiers::kIn_Flag) { |
2378 storageClass = SpvStorageClassInput; | 2380 storageClass = SpvStorageClassInput; |
2379 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { | 2381 } else if (var->fModifiers.fFlags & Modifiers::kOut_Flag) { |
2380 storageClass = SpvStorageClassOutput; | 2382 storageClass = SpvStorageClassOutput; |
2381 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kUniform_Flag)
{ | 2383 } else if (var->fModifiers.fFlags & Modifiers::kUniform_Flag) { |
2382 if (decl.fVars[i]->fType.kind() == Type::kSampler_Kind) { | 2384 if (var->fType.kind() == Type::kSampler_Kind) { |
2383 storageClass = SpvStorageClassUniformConstant; | 2385 storageClass = SpvStorageClassUniformConstant; |
2384 } else { | 2386 } else { |
2385 storageClass = SpvStorageClassUniform; | 2387 storageClass = SpvStorageClassUniform; |
2386 } | 2388 } |
2387 } else { | 2389 } else { |
2388 storageClass = SpvStorageClassPrivate; | 2390 storageClass = SpvStorageClassPrivate; |
2389 } | 2391 } |
2390 SpvId id = this->nextId(); | 2392 SpvId id = this->nextId(); |
2391 fVariableMap[decl.fVars[i]] = id; | 2393 fVariableMap[var] = id; |
2392 SpvId type = this->getPointerType(decl.fVars[i]->fType, storageClass); | 2394 SpvId type = this->getPointerType(var->fType, storageClass); |
2393 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantB
uffer); | 2395 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantB
uffer); |
2394 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); | 2396 this->writeInstruction(SpvOpName, id, var->fName.c_str(), fNameBuffer); |
2395 if (decl.fVars[i]->fType.kind() == Type::kMatrix_Kind) { | 2397 if (var->fType.kind() == Type::kMatrix_Kind) { |
2396 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionColMajor, | 2398 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionColMajor, |
2397 fDecorationBuffer); | 2399 fDecorationBuffer); |
2398 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionMatrixStride, | 2400 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionMatrixStride, |
2399 (SpvId) decl.fVars[i]->fType.stride(), fDecor
ationBuffer); | 2401 (SpvId) var->fType.stride(), fDecorationBuffe
r); |
2400 } | 2402 } |
2401 if (decl.fValues[i]) { | 2403 if (varDecl.fValue) { |
2402 ASSERT(!fCurrentBlock); | 2404 ASSERT(!fCurrentBlock); |
2403 fCurrentBlock = -1; | 2405 fCurrentBlock = -1; |
2404 SpvId value = this->writeExpression(*decl.fValues[i], fGlobalInitial
izersBuffer); | 2406 SpvId value = this->writeExpression(*varDecl.fValue, fGlobalInitiali
zersBuffer); |
2405 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuf
fer); | 2407 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuf
fer); |
2406 fCurrentBlock = 0; | 2408 fCurrentBlock = 0; |
2407 } | 2409 } |
2408 this->writeLayout(decl.fVars[i]->fModifiers.fLayout, id); | 2410 this->writeLayout(var->fModifiers.fLayout, id); |
2409 } | 2411 } |
2410 } | 2412 } |
2411 | 2413 |
2412 void SPIRVCodeGenerator::writeVarDeclaration(const VarDeclaration& decl, std::os
tream& out) { | 2414 void SPIRVCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, std::
ostream& out) { |
2413 for (size_t i = 0; i < decl.fVars.size(); i++) { | 2415 for (const auto& varDecl : decl.fVars) { |
| 2416 const Variable* var = varDecl.fVar; |
2414 SpvId id = this->nextId(); | 2417 SpvId id = this->nextId(); |
2415 fVariableMap[decl.fVars[i]] = id; | 2418 fVariableMap[var] = id; |
2416 SpvId type = this->getPointerType(decl.fVars[i]->fType, SpvStorageClassF
unction); | 2419 SpvId type = this->getPointerType(var->fType, SpvStorageClassFunction); |
2417 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction,
fVariableBuffer); | 2420 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction,
fVariableBuffer); |
2418 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); | 2421 this->writeInstruction(SpvOpName, id, var->fName.c_str(), fNameBuffer); |
2419 if (decl.fValues[i]) { | 2422 if (varDecl.fValue) { |
2420 SpvId value = this->writeExpression(*decl.fValues[i], out); | 2423 SpvId value = this->writeExpression(*varDecl.fValue, out); |
2421 this->writeInstruction(SpvOpStore, id, value, out); | 2424 this->writeInstruction(SpvOpStore, id, value, out); |
2422 } | 2425 } |
2423 } | 2426 } |
2424 } | 2427 } |
2425 | 2428 |
2426 void SPIRVCodeGenerator::writeStatement(const Statement& s, std::ostream& out) { | 2429 void SPIRVCodeGenerator::writeStatement(const Statement& s, std::ostream& out) { |
2427 switch (s.fKind) { | 2430 switch (s.fKind) { |
2428 case Statement::kBlock_Kind: | 2431 case Statement::kBlock_Kind: |
2429 this->writeBlock((Block&) s, out); | 2432 this->writeBlock((Block&) s, out); |
2430 break; | 2433 break; |
2431 case Statement::kExpression_Kind: | 2434 case Statement::kExpression_Kind: |
2432 this->writeExpression(*((ExpressionStatement&) s).fExpression, out); | 2435 this->writeExpression(*((ExpressionStatement&) s).fExpression, out); |
2433 break; | 2436 break; |
2434 case Statement::kReturn_Kind: | 2437 case Statement::kReturn_Kind: |
2435 this->writeReturnStatement((ReturnStatement&) s, out); | 2438 this->writeReturnStatement((ReturnStatement&) s, out); |
2436 break; | 2439 break; |
2437 case Statement::kVarDeclaration_Kind: | 2440 case Statement::kVarDeclarations_Kind: |
2438 this->writeVarDeclaration(*((VarDeclarationStatement&) s).fDeclarati
on, out); | 2441 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion, out); |
2439 break; | 2442 break; |
2440 case Statement::kIf_Kind: | 2443 case Statement::kIf_Kind: |
2441 this->writeIfStatement((IfStatement&) s, out); | 2444 this->writeIfStatement((IfStatement&) s, out); |
2442 break; | 2445 break; |
2443 case Statement::kFor_Kind: | 2446 case Statement::kFor_Kind: |
2444 this->writeForStatement((ForStatement&) s, out); | 2447 this->writeForStatement((ForStatement&) s, out); |
2445 break; | 2448 break; |
2446 case Statement::kBreak_Kind: | 2449 case Statement::kBreak_Kind: |
2447 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out); | 2450 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out); |
2448 break; | 2451 break; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2552 InterfaceBlock& intf = (InterfaceBlock&) *program.fElements[i]; | 2555 InterfaceBlock& intf = (InterfaceBlock&) *program.fElements[i]; |
2553 SpvId id = this->writeInterfaceBlock(intf); | 2556 SpvId id = this->writeInterfaceBlock(intf); |
2554 if ((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) || | 2557 if ((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) || |
2555 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) { | 2558 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) { |
2556 interfaceVars.push_back(id); | 2559 interfaceVars.push_back(id); |
2557 } | 2560 } |
2558 } | 2561 } |
2559 } | 2562 } |
2560 for (size_t i = 0; i < program.fElements.size(); i++) { | 2563 for (size_t i = 0; i < program.fElements.size(); i++) { |
2561 if (program.fElements[i]->fKind == ProgramElement::kVar_Kind) { | 2564 if (program.fElements[i]->fKind == ProgramElement::kVar_Kind) { |
2562 this->writeGlobalVars(((VarDeclaration&) *program.fElements[i]), bod
y); | 2565 this->writeGlobalVars(((VarDeclarations&) *program.fElements[i]), bo
dy); |
2563 } | 2566 } |
2564 } | 2567 } |
2565 for (size_t i = 0; i < program.fElements.size(); i++) { | 2568 for (size_t i = 0; i < program.fElements.size(); i++) { |
2566 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { | 2569 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
2567 this->writeFunction(((FunctionDefinition&) *program.fElements[i]), b
ody); | 2570 this->writeFunction(((FunctionDefinition&) *program.fElements[i]), b
ody); |
2568 } | 2571 } |
2569 } | 2572 } |
2570 const FunctionDeclaration* main = nullptr; | 2573 const FunctionDeclaration* main = nullptr; |
2571 for (auto entry : fFunctionMap) { | 2574 for (auto entry : fFunctionMap) { |
2572 if (entry.first->fName == "main") { | 2575 if (entry.first->fName == "main") { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2626 this->writeWord(SpvVersion, out); | 2629 this->writeWord(SpvVersion, out); |
2627 this->writeWord(SKSL_MAGIC, out); | 2630 this->writeWord(SKSL_MAGIC, out); |
2628 std::stringstream buffer; | 2631 std::stringstream buffer; |
2629 this->writeInstructions(program, buffer); | 2632 this->writeInstructions(program, buffer); |
2630 this->writeWord(fIdCount, out); | 2633 this->writeWord(fIdCount, out); |
2631 this->writeWord(0, out); // reserved, always zero | 2634 this->writeWord(0, out); // reserved, always zero |
2632 out << buffer.str(); | 2635 out << buffer.str(); |
2633 } | 2636 } |
2634 | 2637 |
2635 } | 2638 } |
OLD | NEW |