GrVkUniformHandler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "src/gpu/vk/GrVkUniformHandler.h"
  8. #include "src/gpu/GrTexturePriv.h"
  9. #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
  10. #include "src/gpu/vk/GrVkGpu.h"
  11. #include "src/gpu/vk/GrVkPipelineStateBuilder.h"
  12. #include "src/gpu/vk/GrVkTexture.h"
  13. // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
  14. // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
  15. // are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
  16. // This alignment mask will give correct alignments for using the std430 block layout. If you want
  17. // the std140 alignment, you can use this, but then make sure if you have an array type it is
  18. // aligned to 16 bytes (i.e. has mask of 0xF).
  19. // These are designated in the Vulkan spec, section 14.5.4 "Offset and Stride Assignment".
  20. // https://www.khronos.org/registry/vulkan/specs/1.0-wsi_extensions/html/vkspec.html#interfaces-resources-layout
  21. static uint32_t grsltype_to_alignment_mask(GrSLType type) {
  22. switch(type) {
  23. case kByte_GrSLType: // fall through
  24. case kUByte_GrSLType:
  25. return 0x0;
  26. case kByte2_GrSLType: // fall through
  27. case kUByte2_GrSLType:
  28. return 0x1;
  29. case kByte3_GrSLType: // fall through
  30. case kByte4_GrSLType:
  31. case kUByte3_GrSLType:
  32. case kUByte4_GrSLType:
  33. return 0x3;
  34. case kShort_GrSLType: // fall through
  35. case kUShort_GrSLType:
  36. return 0x1;
  37. case kShort2_GrSLType: // fall through
  38. case kUShort2_GrSLType:
  39. return 0x3;
  40. case kShort3_GrSLType: // fall through
  41. case kShort4_GrSLType:
  42. case kUShort3_GrSLType:
  43. case kUShort4_GrSLType:
  44. return 0x7;
  45. case kInt_GrSLType:
  46. case kUint_GrSLType:
  47. return 0x3;
  48. case kHalf_GrSLType: // fall through
  49. case kFloat_GrSLType:
  50. return 0x3;
  51. case kHalf2_GrSLType: // fall through
  52. case kFloat2_GrSLType:
  53. return 0x7;
  54. case kHalf3_GrSLType: // fall through
  55. case kFloat3_GrSLType:
  56. return 0xF;
  57. case kHalf4_GrSLType: // fall through
  58. case kFloat4_GrSLType:
  59. return 0xF;
  60. case kUint2_GrSLType:
  61. return 0x7;
  62. case kInt2_GrSLType:
  63. return 0x7;
  64. case kInt3_GrSLType:
  65. return 0xF;
  66. case kInt4_GrSLType:
  67. return 0xF;
  68. case kHalf2x2_GrSLType: // fall through
  69. case kFloat2x2_GrSLType:
  70. return 0x7;
  71. case kHalf3x3_GrSLType: // fall through
  72. case kFloat3x3_GrSLType:
  73. return 0xF;
  74. case kHalf4x4_GrSLType: // fall through
  75. case kFloat4x4_GrSLType:
  76. return 0xF;
  77. // This query is only valid for certain types.
  78. case kVoid_GrSLType:
  79. case kBool_GrSLType:
  80. case kTexture2DSampler_GrSLType:
  81. case kTextureExternalSampler_GrSLType:
  82. case kTexture2DRectSampler_GrSLType:
  83. break;
  84. }
  85. SK_ABORT("Unexpected type");
  86. return 0;
  87. }
  88. /** Returns the size in bytes taken up in vulkanbuffers for GrSLTypes. */
  89. static inline uint32_t grsltype_to_vk_size(GrSLType type) {
  90. switch(type) {
  91. case kByte_GrSLType:
  92. return sizeof(int8_t);
  93. case kByte2_GrSLType:
  94. return 2 * sizeof(int8_t);
  95. case kByte3_GrSLType:
  96. return 3 * sizeof(int8_t);
  97. case kByte4_GrSLType:
  98. return 4 * sizeof(int8_t);
  99. case kUByte_GrSLType:
  100. return sizeof(uint8_t);
  101. case kUByte2_GrSLType:
  102. return 2 * sizeof(uint8_t);
  103. case kUByte3_GrSLType:
  104. return 3 * sizeof(uint8_t);
  105. case kUByte4_GrSLType:
  106. return 4 * sizeof(uint8_t);
  107. case kShort_GrSLType:
  108. return sizeof(int16_t);
  109. case kShort2_GrSLType:
  110. return 2 * sizeof(int16_t);
  111. case kShort3_GrSLType:
  112. return 3 * sizeof(int16_t);
  113. case kShort4_GrSLType:
  114. return 4 * sizeof(int16_t);
  115. case kUShort_GrSLType:
  116. return sizeof(uint16_t);
  117. case kUShort2_GrSLType:
  118. return 2 * sizeof(uint16_t);
  119. case kUShort3_GrSLType:
  120. return 3 * sizeof(uint16_t);
  121. case kUShort4_GrSLType:
  122. return 4 * sizeof(uint16_t);
  123. case kInt_GrSLType:
  124. return sizeof(int32_t);
  125. case kUint_GrSLType:
  126. return sizeof(int32_t);
  127. case kHalf_GrSLType: // fall through
  128. case kFloat_GrSLType:
  129. return sizeof(float);
  130. case kHalf2_GrSLType: // fall through
  131. case kFloat2_GrSLType:
  132. return 2 * sizeof(float);
  133. case kHalf3_GrSLType: // fall through
  134. case kFloat3_GrSLType:
  135. return 3 * sizeof(float);
  136. case kHalf4_GrSLType: // fall through
  137. case kFloat4_GrSLType:
  138. return 4 * sizeof(float);
  139. case kUint2_GrSLType:
  140. return 2 * sizeof(uint32_t);
  141. case kInt2_GrSLType:
  142. return 2 * sizeof(int32_t);
  143. case kInt3_GrSLType:
  144. return 3 * sizeof(int32_t);
  145. case kInt4_GrSLType:
  146. return 4 * sizeof(int32_t);
  147. case kHalf2x2_GrSLType: // fall through
  148. case kFloat2x2_GrSLType:
  149. //TODO: this will be 4 * szof(float) on std430.
  150. return 8 * sizeof(float);
  151. case kHalf3x3_GrSLType: // fall through
  152. case kFloat3x3_GrSLType:
  153. return 12 * sizeof(float);
  154. case kHalf4x4_GrSLType: // fall through
  155. case kFloat4x4_GrSLType:
  156. return 16 * sizeof(float);
  157. // This query is only valid for certain types.
  158. case kVoid_GrSLType:
  159. case kBool_GrSLType:
  160. case kTexture2DSampler_GrSLType:
  161. case kTextureExternalSampler_GrSLType:
  162. case kTexture2DRectSampler_GrSLType:
  163. break;
  164. }
  165. SK_ABORT("Unexpected type");
  166. return 0;
  167. }
  168. // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
  169. // taking into consideration all alignment requirements. The uniformOffset is set to the offset for
  170. // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
  171. static void get_ubo_aligned_offset(uint32_t* uniformOffset,
  172. uint32_t* currentOffset,
  173. GrSLType type,
  174. int arrayCount) {
  175. uint32_t alignmentMask = grsltype_to_alignment_mask(type);
  176. // We want to use the std140 layout here, so we must make arrays align to 16 bytes.
  177. if (arrayCount || type == kFloat2x2_GrSLType) {
  178. alignmentMask = 0xF;
  179. }
  180. uint32_t offsetDiff = *currentOffset & alignmentMask;
  181. if (offsetDiff != 0) {
  182. offsetDiff = alignmentMask - offsetDiff + 1;
  183. }
  184. *uniformOffset = *currentOffset + offsetDiff;
  185. SkASSERT(sizeof(float) == 4);
  186. if (arrayCount) {
  187. uint32_t elementSize = SkTMax<uint32_t>(16, grsltype_to_vk_size(type));
  188. SkASSERT(0 == (elementSize & 0xF));
  189. *currentOffset = *uniformOffset + elementSize * arrayCount;
  190. } else {
  191. *currentOffset = *uniformOffset + grsltype_to_vk_size(type);
  192. }
  193. }
  194. GrGLSLUniformHandler::UniformHandle GrVkUniformHandler::internalAddUniformArray(
  195. uint32_t visibility,
  196. GrSLType type,
  197. const char* name,
  198. bool mangleName,
  199. int arrayCount,
  200. const char** outName) {
  201. SkASSERT(name && strlen(name));
  202. // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
  203. // etc.) or only fragment.
  204. SkASSERT(kVertex_GrShaderFlag == visibility ||
  205. kGeometry_GrShaderFlag == visibility ||
  206. (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
  207. kFragment_GrShaderFlag == visibility);
  208. GrSLTypeIsFloatType(type);
  209. UniformInfo& uni = fUniforms.push_back();
  210. uni.fVariable.setType(type);
  211. // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
  212. // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
  213. // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
  214. // the names will mismatch. I think the correct solution is to have all GPs which need the
  215. // uniform view matrix, they should upload the view matrix in their setData along with regular
  216. // uniforms.
  217. char prefix = 'u';
  218. if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
  219. prefix = '\0';
  220. }
  221. fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
  222. uni.fVariable.setArrayCount(arrayCount);
  223. uni.fVisibility = visibility;
  224. // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
  225. // we set the modifier to none for all uniforms declared inside the block.
  226. uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
  227. uint32_t* currentOffset;
  228. uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
  229. if (geomStages & visibility) {
  230. currentOffset = &fCurrentGeometryUBOOffset;
  231. } else {
  232. SkASSERT(kFragment_GrShaderFlag == visibility);
  233. currentOffset = &fCurrentFragmentUBOOffset;
  234. }
  235. get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, type, arrayCount);
  236. SkString layoutQualifier;
  237. layoutQualifier.appendf("offset=%d", uni.fUBOffset);
  238. uni.fVariable.addLayoutQualifier(layoutQualifier.c_str());
  239. if (outName) {
  240. *outName = uni.fVariable.c_str();
  241. }
  242. return GrGLSLUniformHandler::UniformHandle(fUniforms.count() - 1);
  243. }
  244. GrGLSLUniformHandler::SamplerHandle GrVkUniformHandler::addSampler(const GrTexture* texture,
  245. const GrSamplerState& state,
  246. const GrSwizzle& swizzle,
  247. const char* name,
  248. const GrShaderCaps* shaderCaps) {
  249. SkASSERT(name && strlen(name));
  250. SkString mangleName;
  251. char prefix = 'u';
  252. fProgramBuilder->nameVariable(&mangleName, prefix, name, true);
  253. GrTextureType type = texture->texturePriv().textureType();
  254. UniformInfo& info = fSamplers.push_back();
  255. info.fVariable.setType(GrSLCombinedSamplerTypeForTextureType(type));
  256. info.fVariable.setTypeModifier(GrShaderVar::kUniform_TypeModifier);
  257. info.fVariable.setName(mangleName);
  258. SkString layoutQualifier;
  259. layoutQualifier.appendf("set=%d, binding=%d", kSamplerDescSet, fSamplers.count() - 1);
  260. info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
  261. info.fVisibility = kFragment_GrShaderFlag;
  262. info.fUBOffset = 0;
  263. // Check if we are dealing with an external texture and store the needed information if so
  264. const GrVkTexture* vkTexture = static_cast<const GrVkTexture*>(texture);
  265. if (vkTexture->ycbcrConversionInfo().isValid()) {
  266. SkASSERT(type == GrTextureType::kExternal);
  267. GrVkGpu* gpu = static_cast<GrVkPipelineStateBuilder*>(fProgramBuilder)->gpu();
  268. info.fImmutableSampler = gpu->resourceProvider().findOrCreateCompatibleSampler(
  269. state, vkTexture->ycbcrConversionInfo());
  270. SkASSERT(info.fImmutableSampler);
  271. }
  272. SkASSERT(shaderCaps->textureSwizzleAppliedInShader());
  273. fSamplerSwizzles.push_back(swizzle);
  274. SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
  275. return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
  276. }
  277. void GrVkUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
  278. SkASSERT(kVertex_GrShaderFlag == visibility ||
  279. kGeometry_GrShaderFlag == visibility ||
  280. kFragment_GrShaderFlag == visibility);
  281. for (int i = 0; i < fSamplers.count(); ++i) {
  282. const UniformInfo& sampler = fSamplers[i];
  283. SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
  284. if (visibility == sampler.fVisibility) {
  285. sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
  286. out->append(";\n");
  287. }
  288. }
  289. #ifdef SK_DEBUG
  290. bool firstGeomOffsetCheck = false;
  291. bool firstFragOffsetCheck = false;
  292. for (int i = 0; i < fUniforms.count(); ++i) {
  293. const UniformInfo& localUniform = fUniforms[i];
  294. if (kVertex_GrShaderFlag == localUniform.fVisibility ||
  295. kGeometry_GrShaderFlag == localUniform.fVisibility ||
  296. (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
  297. if (!firstGeomOffsetCheck) {
  298. // Check to make sure we are starting our offset at 0 so the offset qualifier we
  299. // set on each variable in the uniform block is valid.
  300. SkASSERT(0 == localUniform.fUBOffset);
  301. firstGeomOffsetCheck = true;
  302. }
  303. } else {
  304. SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
  305. if (!firstFragOffsetCheck) {
  306. // Check to make sure we are starting our offset at 0 so the offset qualifier we
  307. // set on each variable in the uniform block is valid.
  308. SkASSERT(0 == localUniform.fUBOffset);
  309. firstFragOffsetCheck = true;
  310. }
  311. }
  312. }
  313. #endif
  314. SkString uniformsString;
  315. for (int i = 0; i < fUniforms.count(); ++i) {
  316. const UniformInfo& localUniform = fUniforms[i];
  317. if (visibility & localUniform.fVisibility) {
  318. if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
  319. localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
  320. uniformsString.append(";\n");
  321. }
  322. }
  323. }
  324. if (!uniformsString.isEmpty()) {
  325. uint32_t uniformBinding;
  326. const char* stage;
  327. if (kVertex_GrShaderFlag == visibility) {
  328. uniformBinding = kGeometryBinding;
  329. stage = "vertex";
  330. } else if (kGeometry_GrShaderFlag == visibility) {
  331. uniformBinding = kGeometryBinding;
  332. stage = "geometry";
  333. } else {
  334. SkASSERT(kFragment_GrShaderFlag == visibility);
  335. uniformBinding = kFragBinding;
  336. stage = "fragment";
  337. }
  338. out->appendf("layout (set=%d, binding=%d) uniform %sUniformBuffer\n{\n",
  339. kUniformBufferDescSet, uniformBinding, stage);
  340. out->appendf("%s\n};\n", uniformsString.c_str());
  341. }
  342. }