Home | History | Annotate | Download | only in vk
      1 /*
      2 * Copyright 2016 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 "GrVkUniformHandler.h"
      9 #include "glsl/GrGLSLProgramBuilder.h"
     10 
     11 // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
     12 // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
     13 // are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
     14 // This alignment mask will give correct alignments for using the std430 block layout. If you want
     15 // the std140 alignment, you can use this, but then make sure if you have an array type it is
     16 // aligned to 16 bytes (i.e. has mask of 0xF).
     17 uint32_t grsltype_to_alignment_mask(GrSLType type) {
     18     switch(type) {
     19         case kInt_GrSLType:
     20             return 0x3;
     21         case kUint_GrSLType:
     22             return 0x3;
     23         case kFloat_GrSLType:
     24             return 0x3;
     25         case kVec2f_GrSLType:
     26             return 0x7;
     27         case kVec3f_GrSLType:
     28             return 0xF;
     29         case kVec4f_GrSLType:
     30             return 0xF;
     31         case kVec2i_GrSLType:
     32             return 0x7;
     33         case kVec3i_GrSLType:
     34             return 0xF;
     35         case kVec4i_GrSLType:
     36             return 0xF;
     37         case kMat22f_GrSLType:
     38             return 0x7;
     39         case kMat33f_GrSLType:
     40             return 0xF;
     41         case kMat44f_GrSLType:
     42             return 0xF;
     43 
     44         // This query is only valid for certain types.
     45         case kVoid_GrSLType:
     46         case kBool_GrSLType:
     47         case kTexture2DSampler_GrSLType:
     48         case kITexture2DSampler_GrSLType:
     49         case kTextureExternalSampler_GrSLType:
     50         case kTexture2DRectSampler_GrSLType:
     51         case kBufferSampler_GrSLType:
     52         case kTexture2D_GrSLType:
     53         case kSampler_GrSLType:
     54         case kImageStorage2D_GrSLType:
     55         case kIImageStorage2D_GrSLType:
     56             break;
     57     }
     58     SkFAIL("Unexpected type");
     59     return 0;
     60 }
     61 
     62 /** Returns the size in bytes taken up in vulkanbuffers for floating point GrSLTypes.
     63     For non floating point type returns 0. Currently this reflects the std140 alignment
     64     so a mat22 takes up 8 floats. */
     65 static inline uint32_t grsltype_to_vk_size(GrSLType type) {
     66     switch(type) {
     67         case kInt_GrSLType:
     68             return sizeof(int32_t);
     69         case kUint_GrSLType:
     70             return sizeof(int32_t);
     71         case kFloat_GrSLType:
     72             return sizeof(float);
     73         case kVec2f_GrSLType:
     74             return 2 * sizeof(float);
     75         case kVec3f_GrSLType:
     76             return 3 * sizeof(float);
     77         case kVec4f_GrSLType:
     78             return 4 * sizeof(float);
     79         case kVec2i_GrSLType:
     80             return 2 * sizeof(int32_t);
     81         case kVec3i_GrSLType:
     82             return 3 * sizeof(int32_t);
     83         case kVec4i_GrSLType:
     84             return 4 * sizeof(int32_t);
     85         case kMat22f_GrSLType:
     86             //TODO: this will be 4 * szof(float) on std430.
     87             return 8 * sizeof(float);
     88         case kMat33f_GrSLType:
     89             return 12 * sizeof(float);
     90         case kMat44f_GrSLType:
     91             return 16 * sizeof(float);
     92 
     93         // This query is only valid for certain types.
     94         case kVoid_GrSLType:
     95         case kBool_GrSLType:
     96         case kTexture2DSampler_GrSLType:
     97         case kITexture2DSampler_GrSLType:
     98         case kTextureExternalSampler_GrSLType:
     99         case kTexture2DRectSampler_GrSLType:
    100         case kBufferSampler_GrSLType:
    101         case kTexture2D_GrSLType:
    102         case kSampler_GrSLType:
    103         case kImageStorage2D_GrSLType:
    104         case kIImageStorage2D_GrSLType:
    105             break;
    106     }
    107     SkFAIL("Unexpected type");
    108     return 0;
    109 }
    110 
    111 
    112 // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
    113 // taking into consideration all alignment requirements. The uniformOffset is set to the offset for
    114 // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
    115 void get_ubo_aligned_offset(uint32_t* uniformOffset,
    116                             uint32_t* currentOffset,
    117                             GrSLType type,
    118                             int arrayCount) {
    119     uint32_t alignmentMask = grsltype_to_alignment_mask(type);
    120     // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
    121     if (arrayCount || type == kMat22f_GrSLType) {
    122         alignmentMask = 0xF;
    123     }
    124     uint32_t offsetDiff = *currentOffset & alignmentMask;
    125     if (offsetDiff != 0) {
    126         offsetDiff = alignmentMask - offsetDiff + 1;
    127     }
    128     *uniformOffset = *currentOffset + offsetDiff;
    129     SkASSERT(sizeof(float) == 4);
    130     if (arrayCount) {
    131         uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
    132         SkASSERT(0 == (elementSize & 0xF));
    133         *currentOffset = *uniformOffset + elementSize * arrayCount;
    134     } else {
    135         *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
    136     }
    137 }
    138 
    139 GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
    140                                                                             uint32_t visibility,
    141                                                                             GrSLType type,
    142                                                                             GrSLPrecision precision,
    143                                                                             const char* name,
    144                                                                             bool mangleName,
    145                                                                             int arrayCount,
    146                                                                             const char** outName) {
    147     SkASSERT(name && strlen(name));
    148     SkDEBUGCODE(static const uint32_t kVisibilityMask = kVertex_GrShaderFlag|kFragment_GrShaderFlag);
    149     SkASSERT(0 == (~kVisibilityMask & visibility));
    150     SkASSERT(0 != visibility);
    151     SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
    152     GrSLTypeIsFloatType(type);
    153 
    154     UniformInfo& uni = fUniforms.push_back();
    155     uni.fVariable.setType(type);
    156     // TODO this is a bit hacky, lets think of a better way.  Basically we need to be able to use
    157     // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
    158     // exactly what name it wants to use for the uniform view matrix.  If we prefix anythings, then
    159     // the names will mismatch.  I think the correct solution is to have all GPs which need the
    160     // uniform view matrix, they should upload the view matrix in their setData along with regular
    161     // uniforms.
    162     char prefix = 'u';
    163     if ('u' == name[0]) {
    164         prefix = '\0';
    165     }
    166     fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
    167     uni.fVariable.setArrayCount(arrayCount);
    168     // For now asserting the the visibility is either only vertex or only fragment
    169     SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
    170     uni.fVisibility = visibility;
    171     uni.fVariable.setPrecision(precision);
    172     // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
    173     // we set the modifier to none for all uniforms declared inside the block.
    174     uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
    175 
    176     uint32_t* currentOffset = kVertex_GrShaderFlag == visibility ? &fCurrentVertexUBOOffset
    177                                                                  : &fCurrentFragmentUBOOffset;
    178     get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
    179 
    180     SkString layoutQualifier;
    181     layoutQualifier.appendf("offset=%d", uni.fUBOffset);
    182     uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
    183 
    184     if (outName) {
    185         *outName = uni.fVariable.c_str();
    186     }
    187 
    188     return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
    189 }
    190 
    191 GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(uint32_t visibility,
    192                                                                    GrSwizzle swizzle,
    193                                                                    GrSLType type,
    194                                                                    GrSLPrecision precision,
    195                                                                    const char* name) {
    196     SkASSERT(name && strlen(name));
    197     SkDEBUGCODE(static const uint32_t kVisMask = kVertex_GrShaderFlag | kFragment_GrShaderFlag);
    198     SkASSERT(0 == (~kVisMask & visibility));
    199     SkASSERT(0 != visibility);
    200     SkString mangleName;
    201     char prefix = 'u';
    202     fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
    203 
    204     UniformInfo& info = fSamplers.push_back();
    205     SkASSERT(GrSLTypeIsCombinedSamplerType(type));
    206     info.fVariable.setType(type);
    207     info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
    208     info.fVariable.setPrecision(precision);
    209     info.fVariable.setName(mangleName);
    210     SkString layoutQualifier;
    211     layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
    212     info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
    213     info.fVisibility = visibility;
    214     info.fUBOffset = 0;
    215     fSamplerSwizzles.push_back(swizzle);
    216     SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
    217     return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
    218 }
    219 
    220 void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
    221     SkASSERT(kVertex_GrShaderFlag == visibility || kFragment_GrShaderFlag == visibility);
    222 
    223     for (int i = 0; i < fSamplers.count(); ++i) {
    224         const UniformInfo& sampler = fSamplers[i];
    225         SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
    226         if (visibility == sampler.fVisibility) {
    227             sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
    228             out->append(";\n");
    229         }
    230     }
    231 
    232     SkDEBUGCODE(bool firstOffsetCheck = false);
    233     SkString uniformsString;
    234     for (int i = 0; i < fUniforms.count(); ++i) {
    235         const UniformInfo& localUniform = fUniforms[i];
    236         if (visibility == localUniform.fVisibility) {
    237             if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
    238 #ifdef SK_DEBUG
    239                 if (!firstOffsetCheck) {
    240                     // Check to make sure we are starting our offset at 0 so the offset qualifier we
    241                     // set on each variable in the uniform block is valid.
    242                     SkASSERT(0 == localUniform.fUBOffset);
    243                     firstOffsetCheck = true;
    244                 }
    245 #endif
    246                 localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
    247                 uniformsString.append(";\n");
    248             }
    249         }
    250     }
    251     if (!uniformsString.isEmpty()) {
    252         uint32_t uniformBinding = (visibility == kVertex_GrShaderFlag) ? kVertexBinding
    253                                                                        : kFragBinding;
    254         const char* stage = (visibility == kVertex_GrShaderFlag) ? "vertex" : "fragment";
    255         out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
    256                      kUniformBufferDescSet, uniformBinding, stage);
    257         out->appendf("%s\n};\n", uniformsString.c_str());
    258     }
    259 }
    260