123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- /*
- * Copyright 2016 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
- #include "src/gpu/vk/GrVkUniformHandler.h"
- #include "src/gpu/GrTexturePriv.h"
- #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
- #include "src/gpu/vk/GrVkGpu.h"
- #include "src/gpu/vk/GrVkPipelineStateBuilder.h"
- #include "src/gpu/vk/GrVkTexture.h"
- // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
- // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
- // are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
- // This alignment mask will give correct alignments for using the std430 block layout. If you want
- // the std140 alignment, you can use this, but then make sure if you have an array type it is
- // aligned to 16 bytes (i.e. has mask of 0xF).
- // These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment".
- // https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout
- static uint32_t grsltype_to_alignment_mask(GrSLType type) {
- switch(type) {
- case kByte_GrSLType: // fall through
- case kUByte_GrSLType:
- return 0x0;
- case kByte2_GrSLType: // fall through
- case kUByte2_GrSLType:
- return 0x1;
- case kByte3_GrSLType: // fall through
- case kByte4_GrSLType:
- case kUByte3_GrSLType:
- case kUByte4_GrSLType:
- return 0x3;
- case kShort_GrSLType: // fall through
- case kUShort_GrSLType:
- return 0x1;
- case kShort2_GrSLType: // fall through
- case kUShort2_GrSLType:
- return 0x3;
- case kShort3_GrSLType: // fall through
- case kShort4_GrSLType:
- case kUShort3_GrSLType:
- case kUShort4_GrSLType:
- return 0x7;
- case kInt_GrSLType:
- case kUint_GrSLType:
- return 0x3;
- case kHalf_GrSLType: // fall through
- case kFloat_GrSLType:
- return 0x3;
- case kHalf2_GrSLType: // fall through
- case kFloat2_GrSLType:
- return 0x7;
- case kHalf3_GrSLType: // fall through
- case kFloat3_GrSLType:
- return 0xF;
- case kHalf4_GrSLType: // fall through
- case kFloat4_GrSLType:
- return 0xF;
- case kUint2_GrSLType:
- return 0x7;
- case kInt2_GrSLType:
- return 0x7;
- case kInt3_GrSLType:
- return 0xF;
- case kInt4_GrSLType:
- return 0xF;
- case kHalf2x2_GrSLType: // fall through
- case kFloat2x2_GrSLType:
- return 0x7;
- case kHalf3x3_GrSLType: // fall through
- case kFloat3x3_GrSLType:
- return 0xF;
- case kHalf4x4_GrSLType: // fall through
- case kFloat4x4_GrSLType:
- return 0xF;
- // This query is only valid for certain types.
- case kVoid_GrSLType:
- case kBool_GrSLType:
- case kTexture2DSampler_GrSLType:
- case kTextureExternalSampler_GrSLType:
- case kTexture2DRectSampler_GrSLType:
- break;
- }
- SK_ABORT("Unexpected type");
- return 0;
- }
- /** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
- static inline uint32_t grsltype_to_vk_size(GrSLType type) {
- switch(type) {
- case kByte_GrSLType:
- return sizeof(int8_t);
- case kByte2_GrSLType:
- return 2 * sizeof(int8_t);
- case kByte3_GrSLType:
- return 3 * sizeof(int8_t);
- case kByte4_GrSLType:
- return 4 * sizeof(int8_t);
- case kUByte_GrSLType:
- return sizeof(uint8_t);
- case kUByte2_GrSLType:
- return 2 * sizeof(uint8_t);
- case kUByte3_GrSLType:
- return 3 * sizeof(uint8_t);
- case kUByte4_GrSLType:
- return 4 * sizeof(uint8_t);
- case kShort_GrSLType:
- return sizeof(int16_t);
- case kShort2_GrSLType:
- return 2 * sizeof(int16_t);
- case kShort3_GrSLType:
- return 3 * sizeof(int16_t);
- case kShort4_GrSLType:
- return 4 * sizeof(int16_t);
- case kUShort_GrSLType:
- return sizeof(uint16_t);
- case kUShort2_GrSLType:
- return 2 * sizeof(uint16_t);
- case kUShort3_GrSLType:
- return 3 * sizeof(uint16_t);
- case kUShort4_GrSLType:
- return 4 * sizeof(uint16_t);
- case kInt_GrSLType:
- return sizeof(int32_t);
- case kUint_GrSLType:
- return sizeof(int32_t);
- case kHalf_GrSLType: // fall through
- case kFloat_GrSLType:
- return sizeof(float);
- case kHalf2_GrSLType: // fall through
- case kFloat2_GrSLType:
- return 2 * sizeof(float);
- case kHalf3_GrSLType: // fall through
- case kFloat3_GrSLType:
- return 3 * sizeof(float);
- case kHalf4_GrSLType: // fall through
- case kFloat4_GrSLType:
- return 4 * sizeof(float);
- case kUint2_GrSLType:
- return 2 * sizeof(uint32_t);
- case kInt2_GrSLType:
- return 2 * sizeof(int32_t);
- case kInt3_GrSLType:
- return 3 * sizeof(int32_t);
- case kInt4_GrSLType:
- return 4 * sizeof(int32_t);
- case kHalf2x2_GrSLType: // fall through
- case kFloat2x2_GrSLType:
- //TODO: this will be 4 * szof(float) on std430.
- return 8 * sizeof(float);
- case kHalf3x3_GrSLType: // fall through
- case kFloat3x3_GrSLType:
- return 12 * sizeof(float);
- case kHalf4x4_GrSLType: // fall through
- case kFloat4x4_GrSLType:
- return 16 * sizeof(float);
- // This query is only valid for certain types.
- case kVoid_GrSLType:
- case kBool_GrSLType:
- case kTexture2DSampler_GrSLType:
- case kTextureExternalSampler_GrSLType:
- case kTexture2DRectSampler_GrSLType:
- break;
- }
- SK_ABORT("Unexpected type");
- return 0;
- }
- // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
- // taking into consideration all alignment requirements. The uniformOffset is set to the offset for
- // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
- static void get_ubo_aligned_offset(uint32_t* uniformOffset,
- uint32_t* currentOffset,
- GrSLType type,
- int arrayCount) {
- uint32_t alignmentMask = grsltype_to_alignment_mask(type);
- // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
- if (arrayCount || type == kFloat2x2_GrSLType) {
- alignmentMask = 0xF;
- }
- uint32_t offsetDiff = *currentOffset & alignmentMask;
- if (offsetDiff != 0) {
- offsetDiff = alignmentMask - offsetDiff + 1;
- }
- *uniformOffset = *currentOffset + offsetDiff;
- SkASSERT(sizeof(float) == 4);
- if (arrayCount) {
- uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
- SkASSERT(0 == (elementSize & 0xF));
- *currentOffset = *uniformOffset + elementSize * arrayCount;
- } else {
- *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
- }
- }
- GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
- uint32_t visibility,
- GrSLType type,
- const char* name,
- bool mangleName,
- int arrayCount,
- const char** outName) {
- SkASSERT(name && strlen(name));
- // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
- // etc.) or only fragment.
- SkASSERT(kVertex_GrShaderFlag == visibility ||
- kGeometry_GrShaderFlag == visibility ||
- (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
- kFragment_GrShaderFlag == visibility);
- GrSLTypeIsFloatType(type);
- UniformInfo& uni = fUniforms.push_back();
- uni.fVariable.setType(type);
- // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
- // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
- // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
- // the names will mismatch. I think the correct solution is to have all GPs which need the
- // uniform view matrix, they should upload the view matrix in their setData along with regular
- // uniforms.
- char prefix = 'u';
- if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
- prefix = '\0';
- }
- fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
- uni.fVariable.setArrayCount(arrayCount);
- uni.fVisibility = visibility;
- // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
- // we set the modifier to none for all uniforms declared inside the block.
- uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
- uint32_t* currentOffset;
- uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
- if (geomStages & visibility) {
- currentOffset = &fCurrentGeometryUBOOffset;
- } else {
- SkASSERT(kFragment_GrShaderFlag == visibility);
- currentOffset = &fCurrentFragmentUBOOffset;
- }
- get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
- SkString layoutQualifier;
- layoutQualifier.appendf("offset=%d", uni.fUBOffset);
- uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
- if (outName) {
- *outName = uni.fVariable.c_str();
- }
- return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
- }
- GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(const GrTexture* texture,
- const GrSamplerState& state,
- const GrSwizzle& swizzle,
- const char* name,
- const GrShaderCaps* shaderCaps) {
- SkASSERT(name && strlen(name));
- SkString mangleName;
- char prefix = 'u';
- fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
- GrTextureType type = texture->texturePriv().textureType();
- UniformInfo& info = fSamplers.push_back();
- info.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
- info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
- info.fVariable.setName(mangleName);
- SkString layoutQualifier;
- layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
- info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
- info.fVisibility = kFragment_GrShaderFlag;
- info.fUBOffset = 0;
- // Check if we are dealing with an external texture and store the needed information if so
- const GrVkTexture* vkTexture = static_cast<const GrVkTexture*>(texture);
- if (vkTexture->ycbcrConversionInfo().isValid()) {
- SkASSERT(type == GrTextureType::kExternal);
- GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
- info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
- state, vkTexture->ycbcrConversionInfo());
- SkASSERT(info.fImmutableSampler);
- }
- SkASSERT(shaderCaps->textureSwizzleAppliedInShader());
- fSamplerSwizzles.push_back(swizzle);
- SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
- return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
- }
- void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
- SkASSERT(kVertex_GrShaderFlag == visibility ||
- kGeometry_GrShaderFlag == visibility ||
- kFragment_GrShaderFlag == visibility);
- for (int i = 0; i < fSamplers.count(); ++i) {
- const UniformInfo& sampler = fSamplers[i];
- SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
- if (visibility == sampler.fVisibility) {
- sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
- out->append(";\n");
- }
- }
- #ifdef SK_DEBUG
- bool firstGeomOffsetCheck = false;
- bool firstFragOffsetCheck = false;
- for (int i = 0; i < fUniforms.count(); ++i) {
- const UniformInfo& localUniform = fUniforms[i];
- if (kVertex_GrShaderFlag == localUniform.fVisibility ||
- kGeometry_GrShaderFlag == localUniform.fVisibility ||
- (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
- if (!firstGeomOffsetCheck) {
- // Check to make sure we are starting our offset at 0 so the offset qualifier we
- // set on each variable in the uniform block is valid.
- SkASSERT(0 == localUniform.fUBOffset);
- firstGeomOffsetCheck = true;
- }
- } else {
- SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
- if (!firstFragOffsetCheck) {
- // Check to make sure we are starting our offset at 0 so the offset qualifier we
- // set on each variable in the uniform block is valid.
- SkASSERT(0 == localUniform.fUBOffset);
- firstFragOffsetCheck = true;
- }
- }
- }
- #endif
- SkString uniformsString;
- for (int i = 0; i < fUniforms.count(); ++i) {
- const UniformInfo& localUniform = fUniforms[i];
- if (visibility & localUniform.fVisibility) {
- if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
- localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
- uniformsString.append(";\n");
- }
- }
- }
- if (!uniformsString.isEmpty()) {
- uint32_t uniformBinding;
- const char* stage;
- if (kVertex_GrShaderFlag == visibility) {
- uniformBinding = kGeometryBinding;
- stage = "vertex";
- } else if (kGeometry_GrShaderFlag == visibility) {
- uniformBinding = kGeometryBinding;
- stage = "geometry";
- } else {
- SkASSERT(kFragment_GrShaderFlag == visibility);
- uniformBinding = kFragBinding;
- stage = "fragment";
- }
- out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
- kUniformBufferDescSet, uniformBinding, stage);
- out->appendf("%s\n};\n", uniformsString.c_str());
- }
- }
|