tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gl/GrGLShaderBuilder.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | // number of each input/output type in a single allocation block |
| 13 | static const int sVarsPerBlock = 8; |
| 14 | |
| 15 | // except FS outputs where we expect 2 at most. |
| 16 | static const int sMaxFSOutputs = 2; |
| 17 | |
| 18 | } |
| 19 | |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame^] | 20 | // Architectural assumption: always 2-d input coords. |
| 21 | // Likely to become non-constant and non-static, perhaps even |
| 22 | // varying by stage, if we use 1D textures for gradients! |
| 23 | //const int GrGLShaderBuilder::fCoordDims = 2; |
| 24 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 25 | GrGLShaderBuilder::GrGLShaderBuilder() |
| 26 | : fVSUnis(sVarsPerBlock) |
| 27 | , fVSAttrs(sVarsPerBlock) |
| 28 | , fVSOutputs(sVarsPerBlock) |
| 29 | , fGSInputs(sVarsPerBlock) |
| 30 | , fGSOutputs(sVarsPerBlock) |
| 31 | , fFSInputs(sVarsPerBlock) |
| 32 | , fFSUnis(sVarsPerBlock) |
| 33 | , fFSOutputs(sMaxFSOutputs) |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame^] | 34 | , fVaryingDims(0) |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 35 | , fUsesGS(false) { |
| 36 | |
| 37 | } |
| 38 | |
| 39 | void GrGLShaderBuilder::appendVarying(GrSLType type, |
| 40 | const char* name, |
| 41 | const char** vsOutName, |
| 42 | const char** fsInName) { |
| 43 | fVSOutputs.push_back(); |
| 44 | fVSOutputs.back().setType(type); |
| 45 | fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
| 46 | fVSOutputs.back().accessName()->printf("v%s", name); |
| 47 | if (vsOutName) { |
| 48 | *vsOutName = fVSOutputs.back().getName().c_str(); |
| 49 | } |
| 50 | // input to FS comes either from VS or GS |
| 51 | const GrStringBuilder* fsName; |
| 52 | if (fUsesGS) { |
| 53 | // if we have a GS take each varying in as an array |
| 54 | // and output as non-array. |
| 55 | fGSInputs.push_back(); |
| 56 | fGSInputs.back().setType(type); |
| 57 | fGSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 58 | fGSInputs.back().setUnsizedArray(); |
| 59 | *fGSInputs.back().accessName() = fVSOutputs.back().getName(); |
| 60 | fGSOutputs.push_back(); |
| 61 | fGSOutputs.back().setType(type); |
| 62 | fGSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier); |
| 63 | fGSOutputs.back().accessName()->printf("g%s", name); |
| 64 | fsName = fGSOutputs.back().accessName(); |
| 65 | } else { |
| 66 | fsName = fVSOutputs.back().accessName(); |
| 67 | } |
| 68 | fFSInputs.push_back(); |
| 69 | fFSInputs.back().setType(type); |
| 70 | fFSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier); |
| 71 | fFSInputs.back().setName(*fsName); |
| 72 | if (fsInName) { |
| 73 | *fsInName = fsName->c_str(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void GrGLShaderBuilder::appendVarying(GrSLType type, |
| 79 | const char* name, |
| 80 | int stageNum, |
| 81 | const char** vsOutName, |
| 82 | const char** fsInName) { |
| 83 | GrStringBuilder nameWithStage(name); |
| 84 | nameWithStage.appendS32(stageNum); |
| 85 | this->appendVarying(type, nameWithStage.c_str(), vsOutName, fsInName); |
| 86 | } |