GrMtlUniformHandler.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright 2018 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 "include/gpu/GrTexture.h"
  8. #include "src/gpu/GrTexturePriv.h"
  9. #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
  10. #include "src/gpu/mtl/GrMtlUniformHandler.h"
  11. #if !__has_feature(objc_arc)
  12. #error This file must be compiled with Arc. Use -fobjc-arc flag
  13. #endif
  14. // TODO: this class is basically copy and pasted from GrVklUniformHandler so that we can have
  15. // some shaders working. The SkSL Metal code generator was written to work with GLSL generated for
  16. // the Ganesh Vulkan backend, so it should all work. There might be better ways to do things in
  17. // Metal and/or some Vulkan GLSLisms left in.
  18. // To determine whether a current offset is aligned, we can just 'and' the lowest bits with the
  19. // alignment mask. A value of 0 means aligned, any other value is how many bytes past alignment we
  20. // are. This works since all alignments are powers of 2. The mask is always (alignment - 1).
  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 Metal buffers for GrSLTypes. */
  89. static inline uint32_t grsltype_to_mtl_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 4 * 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 4 * 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 4 * 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 4 * 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 4 * 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 4 * sizeof(int32_t);
  145. case kInt4_GrSLType:
  146. return 4 * sizeof(int32_t);
  147. case kHalf2x2_GrSLType: // fall through
  148. case kFloat2x2_GrSLType:
  149. return 4 * sizeof(float);
  150. case kHalf3x3_GrSLType: // fall through
  151. case kFloat3x3_GrSLType:
  152. return 12 * sizeof(float);
  153. case kHalf4x4_GrSLType: // fall through
  154. case kFloat4x4_GrSLType:
  155. return 16 * sizeof(float);
  156. // This query is only valid for certain types.
  157. case kVoid_GrSLType:
  158. case kBool_GrSLType:
  159. case kTexture2DSampler_GrSLType:
  160. case kTextureExternalSampler_GrSLType:
  161. case kTexture2DRectSampler_GrSLType:
  162. break;
  163. }
  164. SK_ABORT("Unexpected type");
  165. return 0;
  166. }
  167. // Given the current offset into the ubo, calculate the offset for the uniform we're trying to add
  168. // taking into consideration all alignment requirements. The uniformOffset is set to the offset for
  169. // the new uniform, and currentOffset is updated to be the offset to the end of the new uniform.
  170. static void get_ubo_aligned_offset(uint32_t* uniformOffset,
  171. uint32_t* currentOffset,
  172. uint32_t* maxAlignment,
  173. GrSLType type,
  174. int arrayCount) {
  175. uint32_t alignmentMask = grsltype_to_alignment_mask(type);
  176. if (alignmentMask > *maxAlignment) {
  177. *maxAlignment = alignmentMask;
  178. }
  179. uint32_t offsetDiff = *currentOffset & alignmentMask;
  180. if (offsetDiff != 0) {
  181. offsetDiff = alignmentMask - offsetDiff + 1;
  182. }
  183. *uniformOffset = *currentOffset + offsetDiff;
  184. SkASSERT(sizeof(float) == 4);
  185. if (arrayCount) {
  186. *currentOffset = *uniformOffset + grsltype_to_mtl_size(type) * arrayCount;
  187. } else {
  188. *currentOffset = *uniformOffset + grsltype_to_mtl_size(type);
  189. }
  190. }
  191. GrGLSLUniformHandler::UniformHandle GrMtlUniformHandler::internalAddUniformArray(
  192. uint32_t visibility,
  193. GrSLType type,
  194. const char* name,
  195. bool mangleName,
  196. int arrayCount,
  197. const char** outName) {
  198. SkASSERT(name && strlen(name));
  199. // For now asserting the the visibility is either geometry types (vertex, tesselation, geometry,
  200. // etc.) or only fragment.
  201. SkASSERT(kVertex_GrShaderFlag == visibility ||
  202. kGeometry_GrShaderFlag == visibility ||
  203. (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == visibility ||
  204. kFragment_GrShaderFlag == visibility);
  205. GrSLTypeIsFloatType(type);
  206. UniformInfo& uni = fUniforms.push_back();
  207. uni.fVariable.setType(type);
  208. // TODO this is a bit hacky, lets think of a better way. Basically we need to be able to use
  209. // the uniform view matrix name in the GP, and the GP is immutable so it has to tell the PB
  210. // exactly what name it wants to use for the uniform view matrix. If we prefix anythings, then
  211. // the names will mismatch. I think the correct solution is to have all GPs which need the
  212. // uniform view matrix, they should upload the view matrix in their setData along with regular
  213. // uniforms.
  214. char prefix = 'u';
  215. if ('u' == name[0] || !strncmp(name, GR_NO_MANGLE_PREFIX, strlen(GR_NO_MANGLE_PREFIX))) {
  216. prefix = '\0';
  217. }
  218. fProgramBuilder->nameVariable(uni.fVariable.accessName(), prefix, name, mangleName);
  219. uni.fVariable.setArrayCount(arrayCount);
  220. uni.fVisibility = visibility;
  221. // When outputing the GLSL, only the outer uniform block will get the Uniform modifier. Thus
  222. // we set the modifier to none for all uniforms declared inside the block.
  223. uni.fVariable.setTypeModifier(GrShaderVar::kNone_TypeModifier);
  224. uint32_t* currentOffset;
  225. uint32_t* maxAlignment;
  226. uint32_t geomStages = kVertex_GrShaderFlag | kGeometry_GrShaderFlag;
  227. if (geomStages & visibility) {
  228. currentOffset = &fCurrentGeometryUBOOffset;
  229. maxAlignment = &fCurrentGeometryUBOMaxAlignment;
  230. } else {
  231. SkASSERT(kFragment_GrShaderFlag == visibility);
  232. currentOffset = &fCurrentFragmentUBOOffset;
  233. maxAlignment = &fCurrentFragmentUBOMaxAlignment;
  234. }
  235. get_ubo_aligned_offset(&uni.fUBOffset, currentOffset, maxAlignment, 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 GrMtlUniformHandler::addSampler(const GrTexture* texture,
  245. const GrSamplerState&,
  246. const GrSwizzle& swizzle,
  247. const char* name,
  248. const GrShaderCaps* caps) {
  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("binding=%d", fSamplers.count() - 1);
  260. info.fVariable.addLayoutQualifier(layoutQualifier.c_str());
  261. info.fVisibility = kFragment_GrShaderFlag;
  262. info.fUBOffset = 0;
  263. SkASSERT(caps->textureSwizzleAppliedInShader());
  264. fSamplerSwizzles.push_back(swizzle);
  265. SkASSERT(fSamplerSwizzles.count() == fSamplers.count());
  266. return GrGLSLUniformHandler::SamplerHandle(fSamplers.count() - 1);
  267. }
  268. void GrMtlUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString* out) const {
  269. SkASSERT(kVertex_GrShaderFlag == visibility ||
  270. kGeometry_GrShaderFlag == visibility ||
  271. kFragment_GrShaderFlag == visibility);
  272. for (int i = 0; i < fSamplers.count(); ++i) {
  273. const UniformInfo& sampler = fSamplers[i];
  274. SkASSERT(sampler.fVariable.getType() == kTexture2DSampler_GrSLType);
  275. if (visibility == sampler.fVisibility) {
  276. sampler.fVariable.appendDecl(fProgramBuilder->shaderCaps(), out);
  277. out->append(";\n");
  278. }
  279. }
  280. #ifdef SK_DEBUG
  281. bool firstGeomOffsetCheck = false;
  282. bool firstFragOffsetCheck = false;
  283. for (int i = 0; i < fUniforms.count(); ++i) {
  284. const UniformInfo& localUniform = fUniforms[i];
  285. if (kVertex_GrShaderFlag == localUniform.fVisibility ||
  286. kGeometry_GrShaderFlag == localUniform.fVisibility ||
  287. (kVertex_GrShaderFlag | kGeometry_GrShaderFlag) == localUniform.fVisibility) {
  288. if (!firstGeomOffsetCheck) {
  289. // Check to make sure we are starting our offset at 0 so the offset qualifier we
  290. // set on each variable in the uniform block is valid.
  291. SkASSERT(0 == localUniform.fUBOffset);
  292. firstGeomOffsetCheck = true;
  293. }
  294. } else {
  295. SkASSERT(kFragment_GrShaderFlag == localUniform.fVisibility);
  296. if (!firstFragOffsetCheck) {
  297. // Check to make sure we are starting our offset at 0 so the offset qualifier we
  298. // set on each variable in the uniform block is valid.
  299. SkASSERT(0 == localUniform.fUBOffset);
  300. firstFragOffsetCheck = true;
  301. }
  302. }
  303. }
  304. #endif
  305. SkString uniformsString;
  306. for (int i = 0; i < fUniforms.count(); ++i) {
  307. const UniformInfo& localUniform = fUniforms[i];
  308. if (visibility & localUniform.fVisibility) {
  309. if (GrSLTypeIsFloatType(localUniform.fVariable.getType())) {
  310. localUniform.fVariable.appendDecl(fProgramBuilder->shaderCaps(), &uniformsString);
  311. uniformsString.append(";\n");
  312. }
  313. }
  314. }
  315. if (!uniformsString.isEmpty()) {
  316. uint32_t uniformBinding;
  317. const char* stage;
  318. if (kVertex_GrShaderFlag == visibility) {
  319. uniformBinding = kGeometryBinding;
  320. stage = "vertex";
  321. } else if (kGeometry_GrShaderFlag == visibility) {
  322. uniformBinding = kGeometryBinding;
  323. stage = "geometry";
  324. } else {
  325. SkASSERT(kFragment_GrShaderFlag == visibility);
  326. uniformBinding = kFragBinding;
  327. stage = "fragment";
  328. }
  329. out->appendf("layout (binding=%d) uniform %sUniformBuffer\n{\n", uniformBinding, stage);
  330. out->appendf("%s\n};\n", uniformsString.c_str());
  331. }
  332. }