GrVkPipeline.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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/GrGeometryProcessor.h"
  8. #include "src/gpu/GrPipeline.h"
  9. #include "src/gpu/GrStencilSettings.h"
  10. #include "src/gpu/vk/GrVkCommandBuffer.h"
  11. #include "src/gpu/vk/GrVkGpu.h"
  12. #include "src/gpu/vk/GrVkPipeline.h"
  13. #include "src/gpu/vk/GrVkRenderTarget.h"
  14. #include "src/gpu/vk/GrVkUtil.h"
  15. #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
  16. #include <sanitizer/lsan_interface.h>
  17. #endif
  18. static inline VkFormat attrib_type_to_vkformat(GrVertexAttribType type) {
  19. switch (type) {
  20. case kFloat_GrVertexAttribType:
  21. return VK_FORMAT_R32_SFLOAT;
  22. case kFloat2_GrVertexAttribType:
  23. return VK_FORMAT_R32G32_SFLOAT;
  24. case kFloat3_GrVertexAttribType:
  25. return VK_FORMAT_R32G32B32_SFLOAT;
  26. case kFloat4_GrVertexAttribType:
  27. return VK_FORMAT_R32G32B32A32_SFLOAT;
  28. case kHalf_GrVertexAttribType:
  29. return VK_FORMAT_R16_SFLOAT;
  30. case kHalf2_GrVertexAttribType:
  31. return VK_FORMAT_R16G16_SFLOAT;
  32. case kHalf3_GrVertexAttribType:
  33. return VK_FORMAT_R16G16B16_SFLOAT;
  34. case kHalf4_GrVertexAttribType:
  35. return VK_FORMAT_R16G16B16A16_SFLOAT;
  36. case kInt2_GrVertexAttribType:
  37. return VK_FORMAT_R32G32_SINT;
  38. case kInt3_GrVertexAttribType:
  39. return VK_FORMAT_R32G32B32_SINT;
  40. case kInt4_GrVertexAttribType:
  41. return VK_FORMAT_R32G32B32A32_SINT;
  42. case kByte_GrVertexAttribType:
  43. return VK_FORMAT_R8_SINT;
  44. case kByte2_GrVertexAttribType:
  45. return VK_FORMAT_R8G8_SINT;
  46. case kByte3_GrVertexAttribType:
  47. return VK_FORMAT_R8G8B8_SINT;
  48. case kByte4_GrVertexAttribType:
  49. return VK_FORMAT_R8G8B8A8_SINT;
  50. case kUByte_GrVertexAttribType:
  51. return VK_FORMAT_R8_UINT;
  52. case kUByte2_GrVertexAttribType:
  53. return VK_FORMAT_R8G8_UINT;
  54. case kUByte3_GrVertexAttribType:
  55. return VK_FORMAT_R8G8B8_UINT;
  56. case kUByte4_GrVertexAttribType:
  57. return VK_FORMAT_R8G8B8A8_UINT;
  58. case kUByte_norm_GrVertexAttribType:
  59. return VK_FORMAT_R8_UNORM;
  60. case kUByte4_norm_GrVertexAttribType:
  61. return VK_FORMAT_R8G8B8A8_UNORM;
  62. case kShort2_GrVertexAttribType:
  63. return VK_FORMAT_R16G16_SINT;
  64. case kShort4_GrVertexAttribType:
  65. return VK_FORMAT_R16G16B16A16_SINT;
  66. case kUShort2_GrVertexAttribType:
  67. return VK_FORMAT_R16G16_UINT;
  68. case kUShort2_norm_GrVertexAttribType:
  69. return VK_FORMAT_R16G16_UNORM;
  70. case kInt_GrVertexAttribType:
  71. return VK_FORMAT_R32_SINT;
  72. case kUint_GrVertexAttribType:
  73. return VK_FORMAT_R32_UINT;
  74. case kUShort_norm_GrVertexAttribType:
  75. return VK_FORMAT_R16_UNORM;
  76. // Experimental (for Y416)
  77. case kUShort4_norm_GrVertexAttribType:
  78. return VK_FORMAT_R16G16B16A16_UNORM;
  79. }
  80. SK_ABORT("Unknown vertex attrib type");
  81. return VK_FORMAT_UNDEFINED;
  82. }
  83. static void setup_vertex_input_state(const GrPrimitiveProcessor& primProc,
  84. VkPipelineVertexInputStateCreateInfo* vertexInputInfo,
  85. SkSTArray<2, VkVertexInputBindingDescription, true>* bindingDescs,
  86. VkVertexInputAttributeDescription* attributeDesc) {
  87. uint32_t vertexBinding = 0, instanceBinding = 0;
  88. int nextBinding = bindingDescs->count();
  89. if (primProc.hasVertexAttributes()) {
  90. vertexBinding = nextBinding++;
  91. }
  92. if (primProc.hasInstanceAttributes()) {
  93. instanceBinding = nextBinding;
  94. }
  95. // setup attribute descriptions
  96. int vaCount = primProc.numVertexAttributes();
  97. int attribIndex = 0;
  98. size_t vertexAttributeOffset = 0;
  99. for (const auto& attrib : primProc.vertexAttributes()) {
  100. VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
  101. vkAttrib.location = attribIndex++; // for now assume location = attribIndex
  102. vkAttrib.binding = vertexBinding;
  103. vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
  104. vkAttrib.offset = vertexAttributeOffset;
  105. vertexAttributeOffset += attrib.sizeAlign4();
  106. }
  107. SkASSERT(vertexAttributeOffset == primProc.vertexStride());
  108. int iaCount = primProc.numInstanceAttributes();
  109. size_t instanceAttributeOffset = 0;
  110. for (const auto& attrib : primProc.instanceAttributes()) {
  111. VkVertexInputAttributeDescription& vkAttrib = attributeDesc[attribIndex];
  112. vkAttrib.location = attribIndex++; // for now assume location = attribIndex
  113. vkAttrib.binding = instanceBinding;
  114. vkAttrib.format = attrib_type_to_vkformat(attrib.cpuType());
  115. vkAttrib.offset = instanceAttributeOffset;
  116. instanceAttributeOffset += attrib.sizeAlign4();
  117. }
  118. SkASSERT(instanceAttributeOffset == primProc.instanceStride());
  119. if (primProc.hasVertexAttributes()) {
  120. bindingDescs->push_back() = {
  121. vertexBinding,
  122. (uint32_t) vertexAttributeOffset,
  123. VK_VERTEX_INPUT_RATE_VERTEX
  124. };
  125. }
  126. if (primProc.hasInstanceAttributes()) {
  127. bindingDescs->push_back() = {
  128. instanceBinding,
  129. (uint32_t) instanceAttributeOffset,
  130. VK_VERTEX_INPUT_RATE_INSTANCE
  131. };
  132. }
  133. memset(vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo));
  134. vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  135. vertexInputInfo->pNext = nullptr;
  136. vertexInputInfo->flags = 0;
  137. vertexInputInfo->vertexBindingDescriptionCount = bindingDescs->count();
  138. vertexInputInfo->pVertexBindingDescriptions = bindingDescs->begin();
  139. vertexInputInfo->vertexAttributeDescriptionCount = vaCount + iaCount;
  140. vertexInputInfo->pVertexAttributeDescriptions = attributeDesc;
  141. }
  142. static VkPrimitiveTopology gr_primitive_type_to_vk_topology(GrPrimitiveType primitiveType) {
  143. switch (primitiveType) {
  144. case GrPrimitiveType::kTriangles:
  145. return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  146. case GrPrimitiveType::kTriangleStrip:
  147. return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
  148. case GrPrimitiveType::kPoints:
  149. return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
  150. case GrPrimitiveType::kLines:
  151. return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
  152. case GrPrimitiveType::kLineStrip:
  153. return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
  154. case GrPrimitiveType::kLinesAdjacency:
  155. return VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY;
  156. }
  157. SK_ABORT("invalid GrPrimitiveType");
  158. return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  159. }
  160. static void setup_input_assembly_state(GrPrimitiveType primitiveType,
  161. VkPipelineInputAssemblyStateCreateInfo* inputAssemblyInfo) {
  162. memset(inputAssemblyInfo, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo));
  163. inputAssemblyInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  164. inputAssemblyInfo->pNext = nullptr;
  165. inputAssemblyInfo->flags = 0;
  166. inputAssemblyInfo->primitiveRestartEnable = false;
  167. inputAssemblyInfo->topology = gr_primitive_type_to_vk_topology(primitiveType);
  168. }
  169. static VkStencilOp stencil_op_to_vk_stencil_op(GrStencilOp op) {
  170. static const VkStencilOp gTable[] = {
  171. VK_STENCIL_OP_KEEP, // kKeep
  172. VK_STENCIL_OP_ZERO, // kZero
  173. VK_STENCIL_OP_REPLACE, // kReplace
  174. VK_STENCIL_OP_INVERT, // kInvert
  175. VK_STENCIL_OP_INCREMENT_AND_WRAP, // kIncWrap
  176. VK_STENCIL_OP_DECREMENT_AND_WRAP, // kDecWrap
  177. VK_STENCIL_OP_INCREMENT_AND_CLAMP, // kIncClamp
  178. VK_STENCIL_OP_DECREMENT_AND_CLAMP, // kDecClamp
  179. };
  180. GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilOpCount);
  181. GR_STATIC_ASSERT(0 == (int)GrStencilOp::kKeep);
  182. GR_STATIC_ASSERT(1 == (int)GrStencilOp::kZero);
  183. GR_STATIC_ASSERT(2 == (int)GrStencilOp::kReplace);
  184. GR_STATIC_ASSERT(3 == (int)GrStencilOp::kInvert);
  185. GR_STATIC_ASSERT(4 == (int)GrStencilOp::kIncWrap);
  186. GR_STATIC_ASSERT(5 == (int)GrStencilOp::kDecWrap);
  187. GR_STATIC_ASSERT(6 == (int)GrStencilOp::kIncClamp);
  188. GR_STATIC_ASSERT(7 == (int)GrStencilOp::kDecClamp);
  189. SkASSERT(op < (GrStencilOp)kGrStencilOpCount);
  190. return gTable[(int)op];
  191. }
  192. static VkCompareOp stencil_func_to_vk_compare_op(GrStencilTest test) {
  193. static const VkCompareOp gTable[] = {
  194. VK_COMPARE_OP_ALWAYS, // kAlways
  195. VK_COMPARE_OP_NEVER, // kNever
  196. VK_COMPARE_OP_GREATER, // kGreater
  197. VK_COMPARE_OP_GREATER_OR_EQUAL, // kGEqual
  198. VK_COMPARE_OP_LESS, // kLess
  199. VK_COMPARE_OP_LESS_OR_EQUAL, // kLEqual
  200. VK_COMPARE_OP_EQUAL, // kEqual
  201. VK_COMPARE_OP_NOT_EQUAL, // kNotEqual
  202. };
  203. GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrStencilTestCount);
  204. GR_STATIC_ASSERT(0 == (int)GrStencilTest::kAlways);
  205. GR_STATIC_ASSERT(1 == (int)GrStencilTest::kNever);
  206. GR_STATIC_ASSERT(2 == (int)GrStencilTest::kGreater);
  207. GR_STATIC_ASSERT(3 == (int)GrStencilTest::kGEqual);
  208. GR_STATIC_ASSERT(4 == (int)GrStencilTest::kLess);
  209. GR_STATIC_ASSERT(5 == (int)GrStencilTest::kLEqual);
  210. GR_STATIC_ASSERT(6 == (int)GrStencilTest::kEqual);
  211. GR_STATIC_ASSERT(7 == (int)GrStencilTest::kNotEqual);
  212. SkASSERT(test < (GrStencilTest)kGrStencilTestCount);
  213. return gTable[(int)test];
  214. }
  215. static void setup_stencil_op_state(
  216. VkStencilOpState* opState, const GrStencilSettings::Face& stencilFace) {
  217. opState->failOp = stencil_op_to_vk_stencil_op(stencilFace.fFailOp);
  218. opState->passOp = stencil_op_to_vk_stencil_op(stencilFace.fPassOp);
  219. opState->depthFailOp = opState->failOp;
  220. opState->compareOp = stencil_func_to_vk_compare_op(stencilFace.fTest);
  221. opState->compareMask = stencilFace.fTestMask;
  222. opState->writeMask = stencilFace.fWriteMask;
  223. opState->reference = stencilFace.fRef;
  224. }
  225. static void setup_depth_stencil_state(
  226. const GrStencilSettings& stencilSettings, GrSurfaceOrigin origin,
  227. VkPipelineDepthStencilStateCreateInfo* stencilInfo) {
  228. memset(stencilInfo, 0, sizeof(VkPipelineDepthStencilStateCreateInfo));
  229. stencilInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  230. stencilInfo->pNext = nullptr;
  231. stencilInfo->flags = 0;
  232. // set depth testing defaults
  233. stencilInfo->depthTestEnable = VK_FALSE;
  234. stencilInfo->depthWriteEnable = VK_FALSE;
  235. stencilInfo->depthCompareOp = VK_COMPARE_OP_ALWAYS;
  236. stencilInfo->depthBoundsTestEnable = VK_FALSE;
  237. stencilInfo->stencilTestEnable = !stencilSettings.isDisabled();
  238. if (!stencilSettings.isDisabled()) {
  239. if (!stencilSettings.isTwoSided()) {
  240. setup_stencil_op_state(&stencilInfo->front, stencilSettings.frontAndBack());
  241. stencilInfo->back = stencilInfo->front;
  242. } else {
  243. setup_stencil_op_state(&stencilInfo->front, stencilSettings.front(origin));
  244. setup_stencil_op_state(&stencilInfo->back, stencilSettings.back(origin));
  245. }
  246. }
  247. stencilInfo->minDepthBounds = 0.0f;
  248. stencilInfo->maxDepthBounds = 1.0f;
  249. }
  250. static void setup_viewport_scissor_state(VkPipelineViewportStateCreateInfo* viewportInfo) {
  251. memset(viewportInfo, 0, sizeof(VkPipelineViewportStateCreateInfo));
  252. viewportInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  253. viewportInfo->pNext = nullptr;
  254. viewportInfo->flags = 0;
  255. viewportInfo->viewportCount = 1;
  256. viewportInfo->pViewports = nullptr; // This is set dynamically
  257. viewportInfo->scissorCount = 1;
  258. viewportInfo->pScissors = nullptr; // This is set dynamically
  259. SkASSERT(viewportInfo->viewportCount == viewportInfo->scissorCount);
  260. }
  261. static void setup_multisample_state(int numColorSamples,
  262. const GrPrimitiveProcessor& primProc,
  263. const GrPipeline& pipeline,
  264. const GrCaps* caps,
  265. VkPipelineMultisampleStateCreateInfo* multisampleInfo) {
  266. memset(multisampleInfo, 0, sizeof(VkPipelineMultisampleStateCreateInfo));
  267. multisampleInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  268. multisampleInfo->pNext = nullptr;
  269. multisampleInfo->flags = 0;
  270. SkAssertResult(GrSampleCountToVkSampleCount(numColorSamples,
  271. &multisampleInfo->rasterizationSamples));
  272. multisampleInfo->sampleShadingEnable = VK_FALSE;
  273. multisampleInfo->minSampleShading = 0.0f;
  274. multisampleInfo->pSampleMask = nullptr;
  275. multisampleInfo->alphaToCoverageEnable = VK_FALSE;
  276. multisampleInfo->alphaToOneEnable = VK_FALSE;
  277. }
  278. static VkBlendFactor blend_coeff_to_vk_blend(GrBlendCoeff coeff) {
  279. static const VkBlendFactor gTable[] = {
  280. VK_BLEND_FACTOR_ZERO, // kZero_GrBlendCoeff
  281. VK_BLEND_FACTOR_ONE, // kOne_GrBlendCoeff
  282. VK_BLEND_FACTOR_SRC_COLOR, // kSC_GrBlendCoeff
  283. VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, // kISC_GrBlendCoeff
  284. VK_BLEND_FACTOR_DST_COLOR, // kDC_GrBlendCoeff
  285. VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, // kIDC_GrBlendCoeff
  286. VK_BLEND_FACTOR_SRC_ALPHA, // kSA_GrBlendCoeff
  287. VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, // kISA_GrBlendCoeff
  288. VK_BLEND_FACTOR_DST_ALPHA, // kDA_GrBlendCoeff
  289. VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, // kIDA_GrBlendCoeff
  290. VK_BLEND_FACTOR_CONSTANT_COLOR, // kConstC_GrBlendCoeff
  291. VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, // kIConstC_GrBlendCoeff
  292. VK_BLEND_FACTOR_CONSTANT_ALPHA, // kConstA_GrBlendCoeff
  293. VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA, // kIConstA_GrBlendCoeff
  294. VK_BLEND_FACTOR_SRC1_COLOR, // kS2C_GrBlendCoeff
  295. VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, // kIS2C_GrBlendCoeff
  296. VK_BLEND_FACTOR_SRC1_ALPHA, // kS2A_GrBlendCoeff
  297. VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, // kIS2A_GrBlendCoeff
  298. VK_BLEND_FACTOR_ZERO, // kIllegal_GrBlendCoeff
  299. };
  300. GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendCoeffCnt);
  301. GR_STATIC_ASSERT(0 == kZero_GrBlendCoeff);
  302. GR_STATIC_ASSERT(1 == kOne_GrBlendCoeff);
  303. GR_STATIC_ASSERT(2 == kSC_GrBlendCoeff);
  304. GR_STATIC_ASSERT(3 == kISC_GrBlendCoeff);
  305. GR_STATIC_ASSERT(4 == kDC_GrBlendCoeff);
  306. GR_STATIC_ASSERT(5 == kIDC_GrBlendCoeff);
  307. GR_STATIC_ASSERT(6 == kSA_GrBlendCoeff);
  308. GR_STATIC_ASSERT(7 == kISA_GrBlendCoeff);
  309. GR_STATIC_ASSERT(8 == kDA_GrBlendCoeff);
  310. GR_STATIC_ASSERT(9 == kIDA_GrBlendCoeff);
  311. GR_STATIC_ASSERT(10 == kConstC_GrBlendCoeff);
  312. GR_STATIC_ASSERT(11 == kIConstC_GrBlendCoeff);
  313. GR_STATIC_ASSERT(12 == kConstA_GrBlendCoeff);
  314. GR_STATIC_ASSERT(13 == kIConstA_GrBlendCoeff);
  315. GR_STATIC_ASSERT(14 == kS2C_GrBlendCoeff);
  316. GR_STATIC_ASSERT(15 == kIS2C_GrBlendCoeff);
  317. GR_STATIC_ASSERT(16 == kS2A_GrBlendCoeff);
  318. GR_STATIC_ASSERT(17 == kIS2A_GrBlendCoeff);
  319. SkASSERT((unsigned)coeff < kGrBlendCoeffCnt);
  320. return gTable[coeff];
  321. }
  322. static VkBlendOp blend_equation_to_vk_blend_op(GrBlendEquation equation) {
  323. static const VkBlendOp gTable[] = {
  324. // Basic blend ops
  325. VK_BLEND_OP_ADD,
  326. VK_BLEND_OP_SUBTRACT,
  327. VK_BLEND_OP_REVERSE_SUBTRACT,
  328. // Advanced blend ops
  329. VK_BLEND_OP_SCREEN_EXT,
  330. VK_BLEND_OP_OVERLAY_EXT,
  331. VK_BLEND_OP_DARKEN_EXT,
  332. VK_BLEND_OP_LIGHTEN_EXT,
  333. VK_BLEND_OP_COLORDODGE_EXT,
  334. VK_BLEND_OP_COLORBURN_EXT,
  335. VK_BLEND_OP_HARDLIGHT_EXT,
  336. VK_BLEND_OP_SOFTLIGHT_EXT,
  337. VK_BLEND_OP_DIFFERENCE_EXT,
  338. VK_BLEND_OP_EXCLUSION_EXT,
  339. VK_BLEND_OP_MULTIPLY_EXT,
  340. VK_BLEND_OP_HSL_HUE_EXT,
  341. VK_BLEND_OP_HSL_SATURATION_EXT,
  342. VK_BLEND_OP_HSL_COLOR_EXT,
  343. VK_BLEND_OP_HSL_LUMINOSITY_EXT,
  344. // Illegal.
  345. VK_BLEND_OP_ADD,
  346. };
  347. GR_STATIC_ASSERT(0 == kAdd_GrBlendEquation);
  348. GR_STATIC_ASSERT(1 == kSubtract_GrBlendEquation);
  349. GR_STATIC_ASSERT(2 == kReverseSubtract_GrBlendEquation);
  350. GR_STATIC_ASSERT(3 == kScreen_GrBlendEquation);
  351. GR_STATIC_ASSERT(4 == kOverlay_GrBlendEquation);
  352. GR_STATIC_ASSERT(5 == kDarken_GrBlendEquation);
  353. GR_STATIC_ASSERT(6 == kLighten_GrBlendEquation);
  354. GR_STATIC_ASSERT(7 == kColorDodge_GrBlendEquation);
  355. GR_STATIC_ASSERT(8 == kColorBurn_GrBlendEquation);
  356. GR_STATIC_ASSERT(9 == kHardLight_GrBlendEquation);
  357. GR_STATIC_ASSERT(10 == kSoftLight_GrBlendEquation);
  358. GR_STATIC_ASSERT(11 == kDifference_GrBlendEquation);
  359. GR_STATIC_ASSERT(12 == kExclusion_GrBlendEquation);
  360. GR_STATIC_ASSERT(13 == kMultiply_GrBlendEquation);
  361. GR_STATIC_ASSERT(14 == kHSLHue_GrBlendEquation);
  362. GR_STATIC_ASSERT(15 == kHSLSaturation_GrBlendEquation);
  363. GR_STATIC_ASSERT(16 == kHSLColor_GrBlendEquation);
  364. GR_STATIC_ASSERT(17 == kHSLLuminosity_GrBlendEquation);
  365. GR_STATIC_ASSERT(SK_ARRAY_COUNT(gTable) == kGrBlendEquationCnt);
  366. SkASSERT((unsigned)equation < kGrBlendCoeffCnt);
  367. return gTable[equation];
  368. }
  369. static bool blend_coeff_refs_constant(GrBlendCoeff coeff) {
  370. static const bool gCoeffReferencesBlendConst[] = {
  371. false,
  372. false,
  373. false,
  374. false,
  375. false,
  376. false,
  377. false,
  378. false,
  379. false,
  380. false,
  381. true,
  382. true,
  383. true,
  384. true,
  385. // extended blend coeffs
  386. false,
  387. false,
  388. false,
  389. false,
  390. // Illegal
  391. false,
  392. };
  393. return gCoeffReferencesBlendConst[coeff];
  394. GR_STATIC_ASSERT(kGrBlendCoeffCnt == SK_ARRAY_COUNT(gCoeffReferencesBlendConst));
  395. // Individual enum asserts already made in blend_coeff_to_vk_blend
  396. }
  397. static void setup_color_blend_state(const GrPipeline& pipeline,
  398. VkPipelineColorBlendStateCreateInfo* colorBlendInfo,
  399. VkPipelineColorBlendAttachmentState* attachmentState) {
  400. const GrXferProcessor::BlendInfo& blendInfo = pipeline.getXferProcessor().getBlendInfo();
  401. GrBlendEquation equation = blendInfo.fEquation;
  402. GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
  403. GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
  404. bool blendOff = (kAdd_GrBlendEquation == equation || kSubtract_GrBlendEquation == equation) &&
  405. kOne_GrBlendCoeff == srcCoeff && kZero_GrBlendCoeff == dstCoeff;
  406. memset(attachmentState, 0, sizeof(VkPipelineColorBlendAttachmentState));
  407. attachmentState->blendEnable = !blendOff;
  408. if (!blendOff) {
  409. attachmentState->srcColorBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
  410. attachmentState->dstColorBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
  411. attachmentState->colorBlendOp = blend_equation_to_vk_blend_op(equation);
  412. attachmentState->srcAlphaBlendFactor = blend_coeff_to_vk_blend(srcCoeff);
  413. attachmentState->dstAlphaBlendFactor = blend_coeff_to_vk_blend(dstCoeff);
  414. attachmentState->alphaBlendOp = blend_equation_to_vk_blend_op(equation);
  415. }
  416. if (!blendInfo.fWriteColor) {
  417. attachmentState->colorWriteMask = 0;
  418. } else {
  419. attachmentState->colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
  420. VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  421. }
  422. memset(colorBlendInfo, 0, sizeof(VkPipelineColorBlendStateCreateInfo));
  423. colorBlendInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  424. colorBlendInfo->pNext = nullptr;
  425. colorBlendInfo->flags = 0;
  426. colorBlendInfo->logicOpEnable = VK_FALSE;
  427. colorBlendInfo->attachmentCount = 1;
  428. colorBlendInfo->pAttachments = attachmentState;
  429. // colorBlendInfo->blendConstants is set dynamically
  430. }
  431. static void setup_raster_state(const GrPipeline& pipeline,
  432. const GrCaps* caps,
  433. VkPipelineRasterizationStateCreateInfo* rasterInfo) {
  434. memset(rasterInfo, 0, sizeof(VkPipelineRasterizationStateCreateInfo));
  435. rasterInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  436. rasterInfo->pNext = nullptr;
  437. rasterInfo->flags = 0;
  438. rasterInfo->depthClampEnable = VK_FALSE;
  439. rasterInfo->rasterizerDiscardEnable = VK_FALSE;
  440. rasterInfo->polygonMode = caps->wireframeMode() ? VK_POLYGON_MODE_LINE
  441. : VK_POLYGON_MODE_FILL;
  442. rasterInfo->cullMode = VK_CULL_MODE_NONE;
  443. rasterInfo->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  444. rasterInfo->depthBiasEnable = VK_FALSE;
  445. rasterInfo->depthBiasConstantFactor = 0.0f;
  446. rasterInfo->depthBiasClamp = 0.0f;
  447. rasterInfo->depthBiasSlopeFactor = 0.0f;
  448. rasterInfo->lineWidth = 1.0f;
  449. }
  450. static void setup_dynamic_state(VkPipelineDynamicStateCreateInfo* dynamicInfo,
  451. VkDynamicState* dynamicStates) {
  452. memset(dynamicInfo, 0, sizeof(VkPipelineDynamicStateCreateInfo));
  453. dynamicInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  454. dynamicInfo->pNext = VK_NULL_HANDLE;
  455. dynamicInfo->flags = 0;
  456. dynamicStates[0] = VK_DYNAMIC_STATE_VIEWPORT;
  457. dynamicStates[1] = VK_DYNAMIC_STATE_SCISSOR;
  458. dynamicStates[2] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
  459. dynamicInfo->dynamicStateCount = 3;
  460. dynamicInfo->pDynamicStates = dynamicStates;
  461. }
  462. GrVkPipeline* GrVkPipeline::Create(
  463. GrVkGpu* gpu, int numColorSamples, const GrPrimitiveProcessor& primProc,
  464. const GrPipeline& pipeline, const GrStencilSettings& stencil, GrSurfaceOrigin origin,
  465. VkPipelineShaderStageCreateInfo* shaderStageInfo, int shaderStageCount,
  466. GrPrimitiveType primitiveType, VkRenderPass compatibleRenderPass, VkPipelineLayout layout,
  467. VkPipelineCache cache) {
  468. VkPipelineVertexInputStateCreateInfo vertexInputInfo;
  469. SkSTArray<2, VkVertexInputBindingDescription, true> bindingDescs;
  470. SkSTArray<16, VkVertexInputAttributeDescription> attributeDesc;
  471. int totalAttributeCnt = primProc.numVertexAttributes() + primProc.numInstanceAttributes();
  472. SkASSERT(totalAttributeCnt <= gpu->vkCaps().maxVertexAttributes());
  473. VkVertexInputAttributeDescription* pAttribs = attributeDesc.push_back_n(totalAttributeCnt);
  474. setup_vertex_input_state(primProc, &vertexInputInfo, &bindingDescs, pAttribs);
  475. VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo;
  476. setup_input_assembly_state(primitiveType, &inputAssemblyInfo);
  477. VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
  478. setup_depth_stencil_state(stencil, origin, &depthStencilInfo);
  479. VkPipelineViewportStateCreateInfo viewportInfo;
  480. setup_viewport_scissor_state(&viewportInfo);
  481. VkPipelineMultisampleStateCreateInfo multisampleInfo;
  482. setup_multisample_state(numColorSamples, primProc, pipeline, gpu->caps(), &multisampleInfo);
  483. // We will only have one color attachment per pipeline.
  484. VkPipelineColorBlendAttachmentState attachmentStates[1];
  485. VkPipelineColorBlendStateCreateInfo colorBlendInfo;
  486. setup_color_blend_state(pipeline, &colorBlendInfo, attachmentStates);
  487. VkPipelineRasterizationStateCreateInfo rasterInfo;
  488. setup_raster_state(pipeline, gpu->caps(), &rasterInfo);
  489. VkDynamicState dynamicStates[3];
  490. VkPipelineDynamicStateCreateInfo dynamicInfo;
  491. setup_dynamic_state(&dynamicInfo, dynamicStates);
  492. VkGraphicsPipelineCreateInfo pipelineCreateInfo;
  493. memset(&pipelineCreateInfo, 0, sizeof(VkGraphicsPipelineCreateInfo));
  494. pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  495. pipelineCreateInfo.pNext = nullptr;
  496. pipelineCreateInfo.flags = 0;
  497. pipelineCreateInfo.stageCount = shaderStageCount;
  498. pipelineCreateInfo.pStages = shaderStageInfo;
  499. pipelineCreateInfo.pVertexInputState = &vertexInputInfo;
  500. pipelineCreateInfo.pInputAssemblyState = &inputAssemblyInfo;
  501. pipelineCreateInfo.pTessellationState = nullptr;
  502. pipelineCreateInfo.pViewportState = &viewportInfo;
  503. pipelineCreateInfo.pRasterizationState = &rasterInfo;
  504. pipelineCreateInfo.pMultisampleState = &multisampleInfo;
  505. pipelineCreateInfo.pDepthStencilState = &depthStencilInfo;
  506. pipelineCreateInfo.pColorBlendState = &colorBlendInfo;
  507. pipelineCreateInfo.pDynamicState = &dynamicInfo;
  508. pipelineCreateInfo.layout = layout;
  509. pipelineCreateInfo.renderPass = compatibleRenderPass;
  510. pipelineCreateInfo.subpass = 0;
  511. pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE;
  512. pipelineCreateInfo.basePipelineIndex = -1;
  513. VkPipeline vkPipeline;
  514. VkResult err;
  515. {
  516. #if defined(SK_ENABLE_SCOPED_LSAN_SUPPRESSIONS)
  517. // skia:8712
  518. __lsan::ScopedDisabler lsanDisabler;
  519. #endif
  520. err = GR_VK_CALL(gpu->vkInterface(), CreateGraphicsPipelines(gpu->device(),
  521. cache, 1,
  522. &pipelineCreateInfo,
  523. nullptr, &vkPipeline));
  524. }
  525. if (err) {
  526. SkDebugf("Failed to create pipeline. Error: %d\n", err);
  527. return nullptr;
  528. }
  529. return new GrVkPipeline(vkPipeline);
  530. }
  531. void GrVkPipeline::freeGPUData(GrVkGpu* gpu) const {
  532. GR_VK_CALL(gpu->vkInterface(), DestroyPipeline(gpu->device(), fPipeline, nullptr));
  533. }
  534. void GrVkPipeline::SetDynamicScissorRectState(GrVkGpu* gpu,
  535. GrVkCommandBuffer* cmdBuffer,
  536. const GrRenderTarget* renderTarget,
  537. GrSurfaceOrigin rtOrigin,
  538. SkIRect scissorRect) {
  539. if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
  540. scissorRect.setEmpty();
  541. }
  542. VkRect2D scissor;
  543. scissor.offset.x = scissorRect.fLeft;
  544. scissor.extent.width = scissorRect.width();
  545. if (kTopLeft_GrSurfaceOrigin == rtOrigin) {
  546. scissor.offset.y = scissorRect.fTop;
  547. } else {
  548. SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
  549. scissor.offset.y = renderTarget->height() - scissorRect.fBottom;
  550. }
  551. scissor.extent.height = scissorRect.height();
  552. SkASSERT(scissor.offset.x >= 0);
  553. SkASSERT(scissor.offset.y >= 0);
  554. cmdBuffer->setScissor(gpu, 0, 1, &scissor);
  555. }
  556. void GrVkPipeline::SetDynamicViewportState(GrVkGpu* gpu,
  557. GrVkCommandBuffer* cmdBuffer,
  558. const GrRenderTarget* renderTarget) {
  559. // We always use one viewport the size of the RT
  560. VkViewport viewport;
  561. viewport.x = 0.0f;
  562. viewport.y = 0.0f;
  563. viewport.width = SkIntToScalar(renderTarget->width());
  564. viewport.height = SkIntToScalar(renderTarget->height());
  565. viewport.minDepth = 0.0f;
  566. viewport.maxDepth = 1.0f;
  567. cmdBuffer->setViewport(gpu, 0, 1, &viewport);
  568. }
  569. void GrVkPipeline::SetDynamicBlendConstantState(GrVkGpu* gpu,
  570. GrVkCommandBuffer* cmdBuffer,
  571. const GrSwizzle& swizzle,
  572. const GrXferProcessor& xferProcessor) {
  573. const GrXferProcessor::BlendInfo& blendInfo = xferProcessor.getBlendInfo();
  574. GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
  575. GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
  576. float floatColors[4];
  577. if (blend_coeff_refs_constant(srcCoeff) || blend_coeff_refs_constant(dstCoeff)) {
  578. // Swizzle the blend to match what the shader will output.
  579. SkPMColor4f blendConst = swizzle.applyTo(blendInfo.fBlendConstant);
  580. floatColors[0] = blendConst.fR;
  581. floatColors[1] = blendConst.fG;
  582. floatColors[2] = blendConst.fB;
  583. floatColors[3] = blendConst.fA;
  584. } else {
  585. memset(floatColors, 0, 4 * sizeof(float));
  586. }
  587. cmdBuffer->setBlendConstants(gpu, floatColors);
  588. }