Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | #ifndef GrVkUniformHandler_DEFINED |
| 9 | #define GrVkUniformHandler_DEFINED |
| 10 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrAllocator.h" |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 12 | #include "GrSamplerState.h" |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 13 | #include "GrShaderVar.h" |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 14 | #include "GrVkSampler.h" |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 15 | #include "glsl/GrGLSLUniformHandler.h" |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 16 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 17 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | class GrVkUniformHandler : public GrGLSLUniformHandler { |
| 19 | public: |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 20 | static const int kUniformsPerBlock = 8; |
| 21 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | enum { |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 23 | /** |
| 24 | * Binding a descriptor set invalidates all higher index descriptor sets. We must bind |
| 25 | * in the order of this enumeration. Samplers are after Uniforms because GrOps can specify |
| 26 | * GP textures as dynamic state, meaning they get rebound for each GrMesh in a draw while |
| 27 | * uniforms are bound once before all the draws. |
| 28 | */ |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 29 | kUniformBufferDescSet = 0, |
| 30 | kSamplerDescSet = 1, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 31 | }; |
| 32 | enum { |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 33 | kGeometryBinding = 0, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 34 | kFragBinding = 1, |
| 35 | }; |
| 36 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 37 | struct UniformInfo { |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 38 | GrShaderVar fVariable; |
| 39 | uint32_t fVisibility; |
| 40 | // fUBOffset is only valid if the GrSLType of the fVariable is not a sampler |
| 41 | uint32_t fUBOffset; |
| 42 | // The SamplerState, maxMipLevel, and ycbcrInfo are only valid if the GrSLType is a sampler |
| 43 | // and that sampler is used for sampling an external image with a ycbcr conversion. |
| 44 | const GrVkSampler* fImmutableSampler = nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 45 | }; |
| 46 | typedef GrTAllocator<UniformInfo> UniformInfoArray; |
| 47 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 48 | const GrShaderVar& getUniformVariable(UniformHandle u) const override { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 49 | return fUniforms[u.toIndex()].fVariable; |
| 50 | } |
| 51 | |
| 52 | const char* getUniformCStr(UniformHandle u) const override { |
| 53 | return this->getUniformVariable(u).c_str(); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | explicit GrVkUniformHandler(GrGLSLProgramBuilder* program) |
| 58 | : INHERITED(program) |
| 59 | , fUniforms(kUniformsPerBlock) |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 60 | , fSamplers(kUniformsPerBlock) |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 61 | , fCurrentGeometryUBOOffset(0) |
Greg Daniel | 31ec144 | 2017-05-08 10:30:59 -0400 | [diff] [blame] | 62 | , fCurrentFragmentUBOOffset(0) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | UniformHandle internalAddUniformArray(uint32_t visibility, |
| 66 | GrSLType type, |
| 67 | GrSLPrecision precision, |
| 68 | const char* name, |
| 69 | bool mangleName, |
| 70 | int arrayCount, |
| 71 | const char** outName) override; |
| 72 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 73 | SamplerHandle addSampler(const GrTexture* texture, |
| 74 | const GrSamplerState&, |
| 75 | const char* name, |
| 76 | const GrShaderCaps*) override; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 77 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 78 | int numSamplers() const { return fSamplers.count(); } |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 79 | const GrShaderVar& samplerVariable(SamplerHandle handle) const override { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 80 | return fSamplers[handle.toIndex()].fVariable; |
| 81 | } |
| 82 | GrSwizzle samplerSwizzle(SamplerHandle handle) const override { |
| 83 | return fSamplerSwizzles[handle.toIndex()]; |
| 84 | } |
| 85 | uint32_t samplerVisibility(SamplerHandle handle) const { |
| 86 | return fSamplers[handle.toIndex()].fVisibility; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 89 | const GrVkSampler* immutableSampler(UniformHandle u) const { |
| 90 | return fSamplers[u.toIndex()].fImmutableSampler; |
| 91 | } |
| 92 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | void appendUniformDecls(GrShaderFlags, SkString*) const override; |
| 94 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 95 | bool hasGeometryUniforms() const { return fCurrentGeometryUBOOffset > 0; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | bool hasFragmentUniforms() const { return fCurrentFragmentUBOOffset > 0; } |
| 97 | |
| 98 | |
| 99 | const UniformInfo& getUniformInfo(UniformHandle u) const { |
| 100 | return fUniforms[u.toIndex()]; |
| 101 | } |
| 102 | |
| 103 | |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 104 | UniformInfoArray fUniforms; |
| 105 | UniformInfoArray fSamplers; |
| 106 | SkTArray<GrSwizzle> fSamplerSwizzles; |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 107 | |
Greg Daniel | 18f9602 | 2017-05-04 15:09:03 -0400 | [diff] [blame] | 108 | uint32_t fCurrentGeometryUBOOffset; |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 109 | uint32_t fCurrentFragmentUBOOffset; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 110 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 111 | friend class GrVkPipelineStateBuilder; |
egdaniel | 707bbd6 | 2016-07-26 07:19:47 -0700 | [diff] [blame] | 112 | friend class GrVkDescriptorSetManager; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 113 | |
| 114 | typedef GrGLSLUniformHandler INHERITED; |
| 115 | }; |
| 116 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 117 | #endif |