Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

radv_meta_clear.c 66KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. /*
  2. * Copyright © 2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include "radv_debug.h"
  24. #include "radv_meta.h"
  25. #include "radv_private.h"
  26. #include "nir/nir_builder.h"
  27. #include "util/format_rgb9e5.h"
  28. #include "vk_format.h"
  29. enum {
  30. DEPTH_CLEAR_SLOW,
  31. DEPTH_CLEAR_FAST_EXPCLEAR,
  32. DEPTH_CLEAR_FAST_NO_EXPCLEAR
  33. };
  34. static void
  35. build_color_shaders(struct nir_shader **out_vs,
  36. struct nir_shader **out_fs,
  37. uint32_t frag_output)
  38. {
  39. nir_builder vs_b;
  40. nir_builder fs_b;
  41. nir_builder_init_simple_shader(&vs_b, NULL, MESA_SHADER_VERTEX, NULL);
  42. nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
  43. vs_b.shader->info.name = ralloc_strdup(vs_b.shader, "meta_clear_color_vs");
  44. fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "meta_clear_color_fs");
  45. const struct glsl_type *position_type = glsl_vec4_type();
  46. const struct glsl_type *color_type = glsl_vec4_type();
  47. nir_variable *vs_out_pos =
  48. nir_variable_create(vs_b.shader, nir_var_shader_out, position_type,
  49. "gl_Position");
  50. vs_out_pos->data.location = VARYING_SLOT_POS;
  51. nir_intrinsic_instr *in_color_load = nir_intrinsic_instr_create(fs_b.shader, nir_intrinsic_load_push_constant);
  52. nir_intrinsic_set_base(in_color_load, 0);
  53. nir_intrinsic_set_range(in_color_load, 16);
  54. in_color_load->src[0] = nir_src_for_ssa(nir_imm_int(&fs_b, 0));
  55. in_color_load->num_components = 4;
  56. nir_ssa_dest_init(&in_color_load->instr, &in_color_load->dest, 4, 32, "clear color");
  57. nir_builder_instr_insert(&fs_b, &in_color_load->instr);
  58. nir_variable *fs_out_color =
  59. nir_variable_create(fs_b.shader, nir_var_shader_out, color_type,
  60. "f_color");
  61. fs_out_color->data.location = FRAG_RESULT_DATA0 + frag_output;
  62. nir_store_var(&fs_b, fs_out_color, &in_color_load->dest.ssa, 0xf);
  63. nir_ssa_def *outvec = radv_meta_gen_rect_vertices(&vs_b);
  64. nir_store_var(&vs_b, vs_out_pos, outvec, 0xf);
  65. const struct glsl_type *layer_type = glsl_int_type();
  66. nir_variable *vs_out_layer =
  67. nir_variable_create(vs_b.shader, nir_var_shader_out, layer_type,
  68. "v_layer");
  69. vs_out_layer->data.location = VARYING_SLOT_LAYER;
  70. vs_out_layer->data.interpolation = INTERP_MODE_FLAT;
  71. nir_ssa_def *inst_id = nir_load_instance_id(&vs_b);
  72. nir_ssa_def *base_instance = nir_load_base_instance(&vs_b);
  73. nir_ssa_def *layer_id = nir_iadd(&vs_b, inst_id, base_instance);
  74. nir_store_var(&vs_b, vs_out_layer, layer_id, 0x1);
  75. *out_vs = vs_b.shader;
  76. *out_fs = fs_b.shader;
  77. }
  78. static VkResult
  79. create_pipeline(struct radv_device *device,
  80. struct radv_render_pass *render_pass,
  81. uint32_t samples,
  82. struct nir_shader *vs_nir,
  83. struct nir_shader *fs_nir,
  84. const VkPipelineVertexInputStateCreateInfo *vi_state,
  85. const VkPipelineDepthStencilStateCreateInfo *ds_state,
  86. const VkPipelineColorBlendStateCreateInfo *cb_state,
  87. const VkPipelineLayout layout,
  88. const struct radv_graphics_pipeline_create_info *extra,
  89. const VkAllocationCallbacks *alloc,
  90. VkPipeline *pipeline)
  91. {
  92. VkDevice device_h = radv_device_to_handle(device);
  93. VkResult result;
  94. struct radv_shader_module vs_m = { .nir = vs_nir };
  95. struct radv_shader_module fs_m = { .nir = fs_nir };
  96. result = radv_graphics_pipeline_create(device_h,
  97. radv_pipeline_cache_to_handle(&device->meta_state.cache),
  98. &(VkGraphicsPipelineCreateInfo) {
  99. .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
  100. .stageCount = fs_nir ? 2 : 1,
  101. .pStages = (VkPipelineShaderStageCreateInfo[]) {
  102. {
  103. .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
  104. .stage = VK_SHADER_STAGE_VERTEX_BIT,
  105. .module = radv_shader_module_to_handle(&vs_m),
  106. .pName = "main",
  107. },
  108. {
  109. .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
  110. .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
  111. .module = radv_shader_module_to_handle(&fs_m),
  112. .pName = "main",
  113. },
  114. },
  115. .pVertexInputState = vi_state,
  116. .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
  117. .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
  118. .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
  119. .primitiveRestartEnable = false,
  120. },
  121. .pViewportState = &(VkPipelineViewportStateCreateInfo) {
  122. .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
  123. .viewportCount = 1,
  124. .scissorCount = 1,
  125. },
  126. .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
  127. .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
  128. .rasterizerDiscardEnable = false,
  129. .polygonMode = VK_POLYGON_MODE_FILL,
  130. .cullMode = VK_CULL_MODE_NONE,
  131. .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
  132. .depthBiasEnable = false,
  133. },
  134. .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
  135. .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
  136. .rasterizationSamples = samples,
  137. .sampleShadingEnable = false,
  138. .pSampleMask = NULL,
  139. .alphaToCoverageEnable = false,
  140. .alphaToOneEnable = false,
  141. },
  142. .pDepthStencilState = ds_state,
  143. .pColorBlendState = cb_state,
  144. .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
  145. /* The meta clear pipeline declares all state as dynamic.
  146. * As a consequence, vkCmdBindPipeline writes no dynamic state
  147. * to the cmd buffer. Therefore, at the end of the meta clear,
  148. * we need only restore dynamic state was vkCmdSet.
  149. */
  150. .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
  151. .dynamicStateCount = 8,
  152. .pDynamicStates = (VkDynamicState[]) {
  153. /* Everything except stencil write mask */
  154. VK_DYNAMIC_STATE_VIEWPORT,
  155. VK_DYNAMIC_STATE_SCISSOR,
  156. VK_DYNAMIC_STATE_LINE_WIDTH,
  157. VK_DYNAMIC_STATE_DEPTH_BIAS,
  158. VK_DYNAMIC_STATE_BLEND_CONSTANTS,
  159. VK_DYNAMIC_STATE_DEPTH_BOUNDS,
  160. VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
  161. VK_DYNAMIC_STATE_STENCIL_REFERENCE,
  162. },
  163. },
  164. .layout = layout,
  165. .flags = 0,
  166. .renderPass = radv_render_pass_to_handle(render_pass),
  167. .subpass = 0,
  168. },
  169. extra,
  170. alloc,
  171. pipeline);
  172. ralloc_free(vs_nir);
  173. ralloc_free(fs_nir);
  174. return result;
  175. }
  176. static VkResult
  177. create_color_renderpass(struct radv_device *device,
  178. VkFormat vk_format,
  179. uint32_t samples,
  180. VkRenderPass *pass)
  181. {
  182. mtx_lock(&device->meta_state.mtx);
  183. if (*pass) {
  184. mtx_unlock (&device->meta_state.mtx);
  185. return VK_SUCCESS;
  186. }
  187. VkResult result = radv_CreateRenderPass(radv_device_to_handle(device),
  188. &(VkRenderPassCreateInfo) {
  189. .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
  190. .attachmentCount = 1,
  191. .pAttachments = &(VkAttachmentDescription) {
  192. .format = vk_format,
  193. .samples = samples,
  194. .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
  195. .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
  196. .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
  197. .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
  198. },
  199. .subpassCount = 1,
  200. .pSubpasses = &(VkSubpassDescription) {
  201. .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
  202. .inputAttachmentCount = 0,
  203. .colorAttachmentCount = 1,
  204. .pColorAttachments = &(VkAttachmentReference) {
  205. .attachment = 0,
  206. .layout = VK_IMAGE_LAYOUT_GENERAL,
  207. },
  208. .pResolveAttachments = NULL,
  209. .pDepthStencilAttachment = &(VkAttachmentReference) {
  210. .attachment = VK_ATTACHMENT_UNUSED,
  211. .layout = VK_IMAGE_LAYOUT_GENERAL,
  212. },
  213. .preserveAttachmentCount = 0,
  214. .pPreserveAttachments = NULL,
  215. },
  216. .dependencyCount = 0,
  217. }, &device->meta_state.alloc, pass);
  218. mtx_unlock(&device->meta_state.mtx);
  219. return result;
  220. }
  221. static VkResult
  222. create_color_pipeline(struct radv_device *device,
  223. uint32_t samples,
  224. uint32_t frag_output,
  225. VkPipeline *pipeline,
  226. VkRenderPass pass)
  227. {
  228. struct nir_shader *vs_nir;
  229. struct nir_shader *fs_nir;
  230. VkResult result;
  231. mtx_lock(&device->meta_state.mtx);
  232. if (*pipeline) {
  233. mtx_unlock(&device->meta_state.mtx);
  234. return VK_SUCCESS;
  235. }
  236. build_color_shaders(&vs_nir, &fs_nir, frag_output);
  237. const VkPipelineVertexInputStateCreateInfo vi_state = {
  238. .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  239. .vertexBindingDescriptionCount = 0,
  240. .vertexAttributeDescriptionCount = 0,
  241. };
  242. const VkPipelineDepthStencilStateCreateInfo ds_state = {
  243. .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
  244. .depthTestEnable = false,
  245. .depthWriteEnable = false,
  246. .depthBoundsTestEnable = false,
  247. .stencilTestEnable = false,
  248. };
  249. VkPipelineColorBlendAttachmentState blend_attachment_state[MAX_RTS] = { 0 };
  250. blend_attachment_state[frag_output] = (VkPipelineColorBlendAttachmentState) {
  251. .blendEnable = false,
  252. .colorWriteMask = VK_COLOR_COMPONENT_A_BIT |
  253. VK_COLOR_COMPONENT_R_BIT |
  254. VK_COLOR_COMPONENT_G_BIT |
  255. VK_COLOR_COMPONENT_B_BIT,
  256. };
  257. const VkPipelineColorBlendStateCreateInfo cb_state = {
  258. .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
  259. .logicOpEnable = false,
  260. .attachmentCount = MAX_RTS,
  261. .pAttachments = blend_attachment_state
  262. };
  263. struct radv_graphics_pipeline_create_info extra = {
  264. .use_rectlist = true,
  265. };
  266. result = create_pipeline(device, radv_render_pass_from_handle(pass),
  267. samples, vs_nir, fs_nir, &vi_state, &ds_state, &cb_state,
  268. device->meta_state.clear_color_p_layout,
  269. &extra, &device->meta_state.alloc, pipeline);
  270. mtx_unlock(&device->meta_state.mtx);
  271. return result;
  272. }
  273. static void
  274. finish_meta_clear_htile_mask_state(struct radv_device *device)
  275. {
  276. struct radv_meta_state *state = &device->meta_state;
  277. radv_DestroyPipeline(radv_device_to_handle(device),
  278. state->clear_htile_mask_pipeline,
  279. &state->alloc);
  280. radv_DestroyPipelineLayout(radv_device_to_handle(device),
  281. state->clear_htile_mask_p_layout,
  282. &state->alloc);
  283. radv_DestroyDescriptorSetLayout(radv_device_to_handle(device),
  284. state->clear_htile_mask_ds_layout,
  285. &state->alloc);
  286. }
  287. void
  288. radv_device_finish_meta_clear_state(struct radv_device *device)
  289. {
  290. struct radv_meta_state *state = &device->meta_state;
  291. for (uint32_t i = 0; i < ARRAY_SIZE(state->clear); ++i) {
  292. for (uint32_t j = 0; j < ARRAY_SIZE(state->clear[i].color_pipelines); ++j) {
  293. radv_DestroyPipeline(radv_device_to_handle(device),
  294. state->clear[i].color_pipelines[j],
  295. &state->alloc);
  296. radv_DestroyRenderPass(radv_device_to_handle(device),
  297. state->clear[i].render_pass[j],
  298. &state->alloc);
  299. }
  300. for (uint32_t j = 0; j < NUM_DEPTH_CLEAR_PIPELINES; j++) {
  301. radv_DestroyPipeline(radv_device_to_handle(device),
  302. state->clear[i].depth_only_pipeline[j],
  303. &state->alloc);
  304. radv_DestroyPipeline(radv_device_to_handle(device),
  305. state->clear[i].stencil_only_pipeline[j],
  306. &state->alloc);
  307. radv_DestroyPipeline(radv_device_to_handle(device),
  308. state->clear[i].depthstencil_pipeline[j],
  309. &state->alloc);
  310. }
  311. radv_DestroyRenderPass(radv_device_to_handle(device),
  312. state->clear[i].depthstencil_rp,
  313. &state->alloc);
  314. }
  315. radv_DestroyPipelineLayout(radv_device_to_handle(device),
  316. state->clear_color_p_layout,
  317. &state->alloc);
  318. radv_DestroyPipelineLayout(radv_device_to_handle(device),
  319. state->clear_depth_p_layout,
  320. &state->alloc);
  321. finish_meta_clear_htile_mask_state(device);
  322. }
  323. static void
  324. emit_color_clear(struct radv_cmd_buffer *cmd_buffer,
  325. const VkClearAttachment *clear_att,
  326. const VkClearRect *clear_rect,
  327. uint32_t view_mask)
  328. {
  329. struct radv_device *device = cmd_buffer->device;
  330. const struct radv_subpass *subpass = cmd_buffer->state.subpass;
  331. const struct radv_framebuffer *fb = cmd_buffer->state.framebuffer;
  332. const uint32_t subpass_att = clear_att->colorAttachment;
  333. const uint32_t pass_att = subpass->color_attachments[subpass_att].attachment;
  334. const struct radv_image_view *iview = fb ? fb->attachments[pass_att].attachment : NULL;
  335. uint32_t samples, samples_log2;
  336. VkFormat format;
  337. unsigned fs_key;
  338. VkClearColorValue clear_value = clear_att->clearValue.color;
  339. VkCommandBuffer cmd_buffer_h = radv_cmd_buffer_to_handle(cmd_buffer);
  340. VkPipeline pipeline;
  341. /* When a framebuffer is bound to the current command buffer, get the
  342. * number of samples from it. Otherwise, get the number of samples from
  343. * the render pass because it's likely a secondary command buffer.
  344. */
  345. if (iview) {
  346. samples = iview->image->info.samples;
  347. format = iview->vk_format;
  348. } else {
  349. samples = cmd_buffer->state.pass->attachments[pass_att].samples;
  350. format = cmd_buffer->state.pass->attachments[pass_att].format;
  351. }
  352. samples_log2 = ffs(samples) - 1;
  353. fs_key = radv_format_meta_fs_key(format);
  354. if (fs_key == -1) {
  355. radv_finishme("color clears incomplete");
  356. return;
  357. }
  358. if (device->meta_state.clear[samples_log2].render_pass[fs_key] == VK_NULL_HANDLE) {
  359. VkResult ret = create_color_renderpass(device, radv_fs_key_format_exemplars[fs_key],
  360. samples,
  361. &device->meta_state.clear[samples_log2].render_pass[fs_key]);
  362. if (ret != VK_SUCCESS) {
  363. cmd_buffer->record_result = ret;
  364. return;
  365. }
  366. }
  367. if (device->meta_state.clear[samples_log2].color_pipelines[fs_key] == VK_NULL_HANDLE) {
  368. VkResult ret = create_color_pipeline(device, samples, 0,
  369. &device->meta_state.clear[samples_log2].color_pipelines[fs_key],
  370. device->meta_state.clear[samples_log2].render_pass[fs_key]);
  371. if (ret != VK_SUCCESS) {
  372. cmd_buffer->record_result = ret;
  373. return;
  374. }
  375. }
  376. pipeline = device->meta_state.clear[samples_log2].color_pipelines[fs_key];
  377. if (!pipeline) {
  378. radv_finishme("color clears incomplete");
  379. return;
  380. }
  381. assert(samples_log2 < ARRAY_SIZE(device->meta_state.clear));
  382. assert(pipeline);
  383. assert(clear_att->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
  384. assert(clear_att->colorAttachment < subpass->color_count);
  385. radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),
  386. device->meta_state.clear_color_p_layout,
  387. VK_SHADER_STAGE_FRAGMENT_BIT, 0, 16,
  388. &clear_value);
  389. struct radv_subpass clear_subpass = {
  390. .color_count = 1,
  391. .color_attachments = (struct radv_subpass_attachment[]) {
  392. subpass->color_attachments[clear_att->colorAttachment]
  393. },
  394. .depth_stencil_attachment = NULL,
  395. };
  396. radv_cmd_buffer_set_subpass(cmd_buffer, &clear_subpass);
  397. radv_CmdBindPipeline(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
  398. pipeline);
  399. radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &(VkViewport) {
  400. .x = clear_rect->rect.offset.x,
  401. .y = clear_rect->rect.offset.y,
  402. .width = clear_rect->rect.extent.width,
  403. .height = clear_rect->rect.extent.height,
  404. .minDepth = 0.0f,
  405. .maxDepth = 1.0f
  406. });
  407. radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &clear_rect->rect);
  408. if (view_mask) {
  409. unsigned i;
  410. for_each_bit(i, view_mask)
  411. radv_CmdDraw(cmd_buffer_h, 3, 1, 0, i);
  412. } else {
  413. radv_CmdDraw(cmd_buffer_h, 3, clear_rect->layerCount, 0, clear_rect->baseArrayLayer);
  414. }
  415. radv_cmd_buffer_set_subpass(cmd_buffer, subpass);
  416. }
  417. static void
  418. build_depthstencil_shader(struct nir_shader **out_vs, struct nir_shader **out_fs)
  419. {
  420. nir_builder vs_b, fs_b;
  421. nir_builder_init_simple_shader(&vs_b, NULL, MESA_SHADER_VERTEX, NULL);
  422. nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
  423. vs_b.shader->info.name = ralloc_strdup(vs_b.shader, "meta_clear_depthstencil_vs");
  424. fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "meta_clear_depthstencil_fs");
  425. const struct glsl_type *position_out_type = glsl_vec4_type();
  426. nir_variable *vs_out_pos =
  427. nir_variable_create(vs_b.shader, nir_var_shader_out, position_out_type,
  428. "gl_Position");
  429. vs_out_pos->data.location = VARYING_SLOT_POS;
  430. nir_intrinsic_instr *in_color_load = nir_intrinsic_instr_create(vs_b.shader, nir_intrinsic_load_push_constant);
  431. nir_intrinsic_set_base(in_color_load, 0);
  432. nir_intrinsic_set_range(in_color_load, 4);
  433. in_color_load->src[0] = nir_src_for_ssa(nir_imm_int(&vs_b, 0));
  434. in_color_load->num_components = 1;
  435. nir_ssa_dest_init(&in_color_load->instr, &in_color_load->dest, 1, 32, "depth value");
  436. nir_builder_instr_insert(&vs_b, &in_color_load->instr);
  437. nir_ssa_def *outvec = radv_meta_gen_rect_vertices_comp2(&vs_b, &in_color_load->dest.ssa);
  438. nir_store_var(&vs_b, vs_out_pos, outvec, 0xf);
  439. const struct glsl_type *layer_type = glsl_int_type();
  440. nir_variable *vs_out_layer =
  441. nir_variable_create(vs_b.shader, nir_var_shader_out, layer_type,
  442. "v_layer");
  443. vs_out_layer->data.location = VARYING_SLOT_LAYER;
  444. vs_out_layer->data.interpolation = INTERP_MODE_FLAT;
  445. nir_ssa_def *inst_id = nir_load_instance_id(&vs_b);
  446. nir_ssa_def *base_instance = nir_load_base_instance(&vs_b);
  447. nir_ssa_def *layer_id = nir_iadd(&vs_b, inst_id, base_instance);
  448. nir_store_var(&vs_b, vs_out_layer, layer_id, 0x1);
  449. *out_vs = vs_b.shader;
  450. *out_fs = fs_b.shader;
  451. }
  452. static VkResult
  453. create_depthstencil_renderpass(struct radv_device *device,
  454. uint32_t samples,
  455. VkRenderPass *render_pass)
  456. {
  457. mtx_lock(&device->meta_state.mtx);
  458. if (*render_pass) {
  459. mtx_unlock(&device->meta_state.mtx);
  460. return VK_SUCCESS;
  461. }
  462. VkResult result = radv_CreateRenderPass(radv_device_to_handle(device),
  463. &(VkRenderPassCreateInfo) {
  464. .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
  465. .attachmentCount = 1,
  466. .pAttachments = &(VkAttachmentDescription) {
  467. .format = VK_FORMAT_D32_SFLOAT_S8_UINT,
  468. .samples = samples,
  469. .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
  470. .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
  471. .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
  472. .finalLayout = VK_IMAGE_LAYOUT_GENERAL,
  473. },
  474. .subpassCount = 1,
  475. .pSubpasses = &(VkSubpassDescription) {
  476. .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
  477. .inputAttachmentCount = 0,
  478. .colorAttachmentCount = 0,
  479. .pColorAttachments = NULL,
  480. .pResolveAttachments = NULL,
  481. .pDepthStencilAttachment = &(VkAttachmentReference) {
  482. .attachment = 0,
  483. .layout = VK_IMAGE_LAYOUT_GENERAL,
  484. },
  485. .preserveAttachmentCount = 0,
  486. .pPreserveAttachments = NULL,
  487. },
  488. .dependencyCount = 0,
  489. }, &device->meta_state.alloc, render_pass);
  490. mtx_unlock(&device->meta_state.mtx);
  491. return result;
  492. }
  493. static VkResult
  494. create_depthstencil_pipeline(struct radv_device *device,
  495. VkImageAspectFlags aspects,
  496. uint32_t samples,
  497. int index,
  498. VkPipeline *pipeline,
  499. VkRenderPass render_pass)
  500. {
  501. struct nir_shader *vs_nir, *fs_nir;
  502. VkResult result;
  503. mtx_lock(&device->meta_state.mtx);
  504. if (*pipeline) {
  505. mtx_unlock(&device->meta_state.mtx);
  506. return VK_SUCCESS;
  507. }
  508. build_depthstencil_shader(&vs_nir, &fs_nir);
  509. const VkPipelineVertexInputStateCreateInfo vi_state = {
  510. .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
  511. .vertexBindingDescriptionCount = 0,
  512. .vertexAttributeDescriptionCount = 0,
  513. };
  514. const VkPipelineDepthStencilStateCreateInfo ds_state = {
  515. .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
  516. .depthTestEnable = (aspects & VK_IMAGE_ASPECT_DEPTH_BIT),
  517. .depthCompareOp = VK_COMPARE_OP_ALWAYS,
  518. .depthWriteEnable = (aspects & VK_IMAGE_ASPECT_DEPTH_BIT),
  519. .depthBoundsTestEnable = false,
  520. .stencilTestEnable = (aspects & VK_IMAGE_ASPECT_STENCIL_BIT),
  521. .front = {
  522. .passOp = VK_STENCIL_OP_REPLACE,
  523. .compareOp = VK_COMPARE_OP_ALWAYS,
  524. .writeMask = UINT32_MAX,
  525. .reference = 0, /* dynamic */
  526. },
  527. .back = { 0 /* dont care */ },
  528. };
  529. const VkPipelineColorBlendStateCreateInfo cb_state = {
  530. .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
  531. .logicOpEnable = false,
  532. .attachmentCount = 0,
  533. .pAttachments = NULL,
  534. };
  535. struct radv_graphics_pipeline_create_info extra = {
  536. .use_rectlist = true,
  537. };
  538. if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
  539. extra.db_depth_clear = index == DEPTH_CLEAR_SLOW ? false : true;
  540. extra.db_depth_disable_expclear = index == DEPTH_CLEAR_FAST_NO_EXPCLEAR ? true : false;
  541. }
  542. if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
  543. extra.db_stencil_clear = index == DEPTH_CLEAR_SLOW ? false : true;
  544. extra.db_stencil_disable_expclear = index == DEPTH_CLEAR_FAST_NO_EXPCLEAR ? true : false;
  545. }
  546. result = create_pipeline(device, radv_render_pass_from_handle(render_pass),
  547. samples, vs_nir, fs_nir, &vi_state, &ds_state, &cb_state,
  548. device->meta_state.clear_depth_p_layout,
  549. &extra, &device->meta_state.alloc, pipeline);
  550. mtx_unlock(&device->meta_state.mtx);
  551. return result;
  552. }
  553. static bool depth_view_can_fast_clear(struct radv_cmd_buffer *cmd_buffer,
  554. const struct radv_image_view *iview,
  555. VkImageAspectFlags aspects,
  556. VkImageLayout layout,
  557. const VkClearRect *clear_rect,
  558. VkClearDepthStencilValue clear_value)
  559. {
  560. if (!iview)
  561. return false;
  562. uint32_t queue_mask = radv_image_queue_family_mask(iview->image,
  563. cmd_buffer->queue_family_index,
  564. cmd_buffer->queue_family_index);
  565. if (clear_rect->rect.offset.x || clear_rect->rect.offset.y ||
  566. clear_rect->rect.extent.width != iview->extent.width ||
  567. clear_rect->rect.extent.height != iview->extent.height)
  568. return false;
  569. if (radv_image_is_tc_compat_htile(iview->image) &&
  570. (((aspects & VK_IMAGE_ASPECT_DEPTH_BIT) && clear_value.depth != 0.0 &&
  571. clear_value.depth != 1.0) ||
  572. ((aspects & VK_IMAGE_ASPECT_STENCIL_BIT) && clear_value.stencil != 0)))
  573. return false;
  574. if (radv_image_has_htile(iview->image) &&
  575. iview->base_mip == 0 &&
  576. iview->base_layer == 0 &&
  577. radv_layout_is_htile_compressed(iview->image, layout, queue_mask) &&
  578. !radv_image_extent_compare(iview->image, &iview->extent))
  579. return true;
  580. return false;
  581. }
  582. static VkPipeline
  583. pick_depthstencil_pipeline(struct radv_cmd_buffer *cmd_buffer,
  584. struct radv_meta_state *meta_state,
  585. const struct radv_image_view *iview,
  586. int samples_log2,
  587. VkImageAspectFlags aspects,
  588. VkImageLayout layout,
  589. const VkClearRect *clear_rect,
  590. VkClearDepthStencilValue clear_value)
  591. {
  592. bool fast = depth_view_can_fast_clear(cmd_buffer, iview, aspects, layout, clear_rect, clear_value);
  593. int index = DEPTH_CLEAR_SLOW;
  594. VkPipeline *pipeline;
  595. if (fast) {
  596. /* we don't know the previous clear values, so we always have
  597. * the NO_EXPCLEAR path */
  598. index = DEPTH_CLEAR_FAST_NO_EXPCLEAR;
  599. }
  600. switch (aspects) {
  601. case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
  602. pipeline = &meta_state->clear[samples_log2].depthstencil_pipeline[index];
  603. break;
  604. case VK_IMAGE_ASPECT_DEPTH_BIT:
  605. pipeline = &meta_state->clear[samples_log2].depth_only_pipeline[index];
  606. break;
  607. case VK_IMAGE_ASPECT_STENCIL_BIT:
  608. pipeline = &meta_state->clear[samples_log2].stencil_only_pipeline[index];
  609. break;
  610. default:
  611. unreachable("expected depth or stencil aspect");
  612. }
  613. if (cmd_buffer->device->meta_state.clear[samples_log2].depthstencil_rp == VK_NULL_HANDLE) {
  614. VkResult ret = create_depthstencil_renderpass(cmd_buffer->device, 1u << samples_log2,
  615. &cmd_buffer->device->meta_state.clear[samples_log2].depthstencil_rp);
  616. if (ret != VK_SUCCESS) {
  617. cmd_buffer->record_result = ret;
  618. return VK_NULL_HANDLE;
  619. }
  620. }
  621. if (*pipeline == VK_NULL_HANDLE) {
  622. VkResult ret = create_depthstencil_pipeline(cmd_buffer->device, aspects, 1u << samples_log2, index,
  623. pipeline, cmd_buffer->device->meta_state.clear[samples_log2].depthstencil_rp);
  624. if (ret != VK_SUCCESS) {
  625. cmd_buffer->record_result = ret;
  626. return VK_NULL_HANDLE;
  627. }
  628. }
  629. return *pipeline;
  630. }
  631. static void
  632. emit_depthstencil_clear(struct radv_cmd_buffer *cmd_buffer,
  633. const VkClearAttachment *clear_att,
  634. const VkClearRect *clear_rect,
  635. uint32_t view_mask)
  636. {
  637. struct radv_device *device = cmd_buffer->device;
  638. struct radv_meta_state *meta_state = &device->meta_state;
  639. const struct radv_subpass *subpass = cmd_buffer->state.subpass;
  640. const struct radv_framebuffer *fb = cmd_buffer->state.framebuffer;
  641. const uint32_t pass_att = subpass->depth_stencil_attachment->attachment;
  642. VkClearDepthStencilValue clear_value = clear_att->clearValue.depthStencil;
  643. VkImageAspectFlags aspects = clear_att->aspectMask;
  644. const struct radv_image_view *iview = fb ? fb->attachments[pass_att].attachment : NULL;
  645. uint32_t samples, samples_log2;
  646. VkCommandBuffer cmd_buffer_h = radv_cmd_buffer_to_handle(cmd_buffer);
  647. /* When a framebuffer is bound to the current command buffer, get the
  648. * number of samples from it. Otherwise, get the number of samples from
  649. * the render pass because it's likely a secondary command buffer.
  650. */
  651. if (iview) {
  652. samples = iview->image->info.samples;
  653. } else {
  654. samples = cmd_buffer->state.pass->attachments[pass_att].samples;
  655. }
  656. samples_log2 = ffs(samples) - 1;
  657. assert(pass_att != VK_ATTACHMENT_UNUSED);
  658. if (!(aspects & VK_IMAGE_ASPECT_DEPTH_BIT))
  659. clear_value.depth = 1.0f;
  660. radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),
  661. device->meta_state.clear_depth_p_layout,
  662. VK_SHADER_STAGE_VERTEX_BIT, 0, 4,
  663. &clear_value.depth);
  664. uint32_t prev_reference = cmd_buffer->state.dynamic.stencil_reference.front;
  665. if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
  666. radv_CmdSetStencilReference(cmd_buffer_h, VK_STENCIL_FACE_FRONT_BIT,
  667. clear_value.stencil);
  668. }
  669. VkPipeline pipeline = pick_depthstencil_pipeline(cmd_buffer,
  670. meta_state,
  671. iview,
  672. samples_log2,
  673. aspects,
  674. subpass->depth_stencil_attachment->layout,
  675. clear_rect,
  676. clear_value);
  677. if (!pipeline)
  678. return;
  679. radv_CmdBindPipeline(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
  680. pipeline);
  681. if (depth_view_can_fast_clear(cmd_buffer, iview, aspects,
  682. subpass->depth_stencil_attachment->layout,
  683. clear_rect, clear_value))
  684. radv_update_ds_clear_metadata(cmd_buffer, iview->image,
  685. clear_value, aspects);
  686. radv_CmdSetViewport(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &(VkViewport) {
  687. .x = clear_rect->rect.offset.x,
  688. .y = clear_rect->rect.offset.y,
  689. .width = clear_rect->rect.extent.width,
  690. .height = clear_rect->rect.extent.height,
  691. .minDepth = 0.0f,
  692. .maxDepth = 1.0f
  693. });
  694. radv_CmdSetScissor(radv_cmd_buffer_to_handle(cmd_buffer), 0, 1, &clear_rect->rect);
  695. if (view_mask) {
  696. unsigned i;
  697. for_each_bit(i, view_mask)
  698. radv_CmdDraw(cmd_buffer_h, 3, 1, 0, i);
  699. } else {
  700. radv_CmdDraw(cmd_buffer_h, 3, clear_rect->layerCount, 0, clear_rect->baseArrayLayer);
  701. }
  702. if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
  703. radv_CmdSetStencilReference(cmd_buffer_h, VK_STENCIL_FACE_FRONT_BIT,
  704. prev_reference);
  705. }
  706. }
  707. static uint32_t
  708. clear_htile_mask(struct radv_cmd_buffer *cmd_buffer,
  709. struct radeon_winsys_bo *bo, uint64_t offset, uint64_t size,
  710. uint32_t htile_value, uint32_t htile_mask)
  711. {
  712. struct radv_device *device = cmd_buffer->device;
  713. struct radv_meta_state *state = &device->meta_state;
  714. uint64_t block_count = round_up_u64(size, 1024);
  715. struct radv_meta_saved_state saved_state;
  716. radv_meta_save(&saved_state, cmd_buffer,
  717. RADV_META_SAVE_COMPUTE_PIPELINE |
  718. RADV_META_SAVE_CONSTANTS |
  719. RADV_META_SAVE_DESCRIPTORS);
  720. struct radv_buffer dst_buffer = {
  721. .bo = bo,
  722. .offset = offset,
  723. .size = size
  724. };
  725. radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer),
  726. VK_PIPELINE_BIND_POINT_COMPUTE,
  727. state->clear_htile_mask_pipeline);
  728. radv_meta_push_descriptor_set(cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
  729. state->clear_htile_mask_p_layout,
  730. 0, /* set */
  731. 1, /* descriptorWriteCount */
  732. (VkWriteDescriptorSet[]) {
  733. {
  734. .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
  735. .dstBinding = 0,
  736. .dstArrayElement = 0,
  737. .descriptorCount = 1,
  738. .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
  739. .pBufferInfo = &(VkDescriptorBufferInfo) {
  740. .buffer = radv_buffer_to_handle(&dst_buffer),
  741. .offset = 0,
  742. .range = size
  743. }
  744. }
  745. });
  746. const unsigned constants[2] = {
  747. htile_value & htile_mask,
  748. ~htile_mask,
  749. };
  750. radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),
  751. state->clear_htile_mask_p_layout,
  752. VK_SHADER_STAGE_COMPUTE_BIT, 0, 8,
  753. constants);
  754. radv_CmdDispatch(radv_cmd_buffer_to_handle(cmd_buffer), block_count, 1, 1);
  755. radv_meta_restore(&saved_state, cmd_buffer);
  756. return RADV_CMD_FLAG_CS_PARTIAL_FLUSH |
  757. RADV_CMD_FLAG_INV_VMEM_L1 |
  758. RADV_CMD_FLAG_WRITEBACK_GLOBAL_L2;
  759. }
  760. static uint32_t
  761. radv_get_htile_fast_clear_value(const struct radv_image *image,
  762. VkClearDepthStencilValue value)
  763. {
  764. uint32_t clear_value;
  765. if (!image->surface.has_stencil) {
  766. clear_value = value.depth ? 0xfffffff0 : 0;
  767. } else {
  768. clear_value = value.depth ? 0xfffc0000 : 0;
  769. }
  770. return clear_value;
  771. }
  772. static uint32_t
  773. radv_get_htile_mask(const struct radv_image *image, VkImageAspectFlags aspects)
  774. {
  775. uint32_t mask = 0;
  776. if (!image->surface.has_stencil) {
  777. /* All the HTILE buffer is used when there is no stencil. */
  778. mask = UINT32_MAX;
  779. } else {
  780. if (aspects & VK_IMAGE_ASPECT_DEPTH_BIT)
  781. mask |= 0xfffffc0f;
  782. if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT)
  783. mask |= 0x000003f0;
  784. }
  785. return mask;
  786. }
  787. static bool
  788. radv_is_fast_clear_depth_allowed(VkClearDepthStencilValue value)
  789. {
  790. return value.depth == 1.0f || value.depth == 0.0f;
  791. }
  792. static bool
  793. radv_is_fast_clear_stencil_allowed(VkClearDepthStencilValue value)
  794. {
  795. return value.stencil == 0;
  796. }
  797. /**
  798. * Determine if the given image can be fast cleared.
  799. */
  800. static bool
  801. radv_image_can_fast_clear(struct radv_device *device, struct radv_image *image)
  802. {
  803. if (device->instance->debug_flags & RADV_DEBUG_NO_FAST_CLEARS)
  804. return false;
  805. if (vk_format_is_color(image->vk_format)) {
  806. if (!radv_image_has_cmask(image) && !radv_image_has_dcc(image))
  807. return false;
  808. /* RB+ doesn't work with CMASK fast clear on Stoney. */
  809. if (!radv_image_has_dcc(image) &&
  810. device->physical_device->rad_info.family == CHIP_STONEY)
  811. return false;
  812. } else {
  813. if (!radv_image_has_htile(image))
  814. return false;
  815. }
  816. /* Do not fast clears 3D images. */
  817. if (image->type == VK_IMAGE_TYPE_3D)
  818. return false;
  819. return true;
  820. }
  821. /**
  822. * Determine if the given image view can be fast cleared.
  823. */
  824. static bool
  825. radv_image_view_can_fast_clear(struct radv_device *device,
  826. const struct radv_image_view *iview)
  827. {
  828. struct radv_image *image;
  829. if (!iview)
  830. return false;
  831. image = iview->image;
  832. /* Only fast clear if the image itself can be fast cleared. */
  833. if (!radv_image_can_fast_clear(device, image))
  834. return false;
  835. /* Only fast clear if all layers are bound. */
  836. if (iview->base_layer > 0 ||
  837. iview->layer_count != image->info.array_size)
  838. return false;
  839. /* Only fast clear if the view covers the whole image. */
  840. if (!radv_image_extent_compare(image, &iview->extent))
  841. return false;
  842. return true;
  843. }
  844. static bool
  845. radv_can_fast_clear_depth(struct radv_cmd_buffer *cmd_buffer,
  846. const struct radv_image_view *iview,
  847. VkImageLayout image_layout,
  848. VkImageAspectFlags aspects,
  849. const VkClearRect *clear_rect,
  850. const VkClearDepthStencilValue clear_value,
  851. uint32_t view_mask)
  852. {
  853. if (!radv_image_view_can_fast_clear(cmd_buffer->device, iview))
  854. return false;
  855. if (!radv_layout_is_htile_compressed(iview->image, image_layout, radv_image_queue_family_mask(iview->image, cmd_buffer->queue_family_index, cmd_buffer->queue_family_index)))
  856. return false;
  857. if (clear_rect->rect.offset.x || clear_rect->rect.offset.y ||
  858. clear_rect->rect.extent.width != iview->image->info.width ||
  859. clear_rect->rect.extent.height != iview->image->info.height)
  860. return false;
  861. if (view_mask && (iview->image->info.array_size >= 32 ||
  862. (1u << iview->image->info.array_size) - 1u != view_mask))
  863. return false;
  864. if (!view_mask && clear_rect->baseArrayLayer != 0)
  865. return false;
  866. if (!view_mask && clear_rect->layerCount != iview->image->info.array_size)
  867. return false;
  868. if (cmd_buffer->device->physical_device->rad_info.chip_class < GFX9 &&
  869. (!(aspects & VK_IMAGE_ASPECT_DEPTH_BIT) ||
  870. ((vk_format_aspects(iview->image->vk_format) & VK_IMAGE_ASPECT_STENCIL_BIT) &&
  871. !(aspects & VK_IMAGE_ASPECT_STENCIL_BIT))))
  872. return false;
  873. if (((aspects & VK_IMAGE_ASPECT_DEPTH_BIT) &&
  874. !radv_is_fast_clear_depth_allowed(clear_value)) ||
  875. ((aspects & VK_IMAGE_ASPECT_STENCIL_BIT) &&
  876. !radv_is_fast_clear_stencil_allowed(clear_value)))
  877. return false;
  878. return true;
  879. }
  880. static void
  881. radv_fast_clear_depth(struct radv_cmd_buffer *cmd_buffer,
  882. const struct radv_image_view *iview,
  883. const VkClearAttachment *clear_att,
  884. enum radv_cmd_flush_bits *pre_flush,
  885. enum radv_cmd_flush_bits *post_flush)
  886. {
  887. VkClearDepthStencilValue clear_value = clear_att->clearValue.depthStencil;
  888. VkImageAspectFlags aspects = clear_att->aspectMask;
  889. uint32_t clear_word, flush_bits;
  890. uint32_t htile_mask;
  891. clear_word = radv_get_htile_fast_clear_value(iview->image, clear_value);
  892. htile_mask = radv_get_htile_mask(iview->image, aspects);
  893. if (pre_flush) {
  894. cmd_buffer->state.flush_bits |= (RADV_CMD_FLAG_FLUSH_AND_INV_DB |
  895. RADV_CMD_FLAG_FLUSH_AND_INV_DB_META) & ~ *pre_flush;
  896. *pre_flush |= cmd_buffer->state.flush_bits;
  897. }
  898. if (htile_mask == UINT_MAX) {
  899. /* Clear the whole HTILE buffer. */
  900. flush_bits = radv_fill_buffer(cmd_buffer, iview->image->bo,
  901. iview->image->offset + iview->image->htile_offset,
  902. iview->image->surface.htile_size, clear_word);
  903. } else {
  904. /* Only clear depth or stencil bytes in the HTILE buffer. */
  905. assert(cmd_buffer->device->physical_device->rad_info.chip_class >= GFX9);
  906. flush_bits = clear_htile_mask(cmd_buffer, iview->image->bo,
  907. iview->image->offset + iview->image->htile_offset,
  908. iview->image->surface.htile_size, clear_word,
  909. htile_mask);
  910. }
  911. radv_update_ds_clear_metadata(cmd_buffer, iview->image, clear_value, aspects);
  912. if (post_flush) {
  913. *post_flush |= flush_bits;
  914. }
  915. }
  916. static nir_shader *
  917. build_clear_htile_mask_shader()
  918. {
  919. nir_builder b;
  920. nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_COMPUTE, NULL);
  921. b.shader->info.name = ralloc_strdup(b.shader, "meta_clear_htile_mask");
  922. b.shader->info.cs.local_size[0] = 64;
  923. b.shader->info.cs.local_size[1] = 1;
  924. b.shader->info.cs.local_size[2] = 1;
  925. nir_ssa_def *invoc_id = nir_load_local_invocation_id(&b);
  926. nir_ssa_def *wg_id = nir_load_work_group_id(&b);
  927. nir_ssa_def *block_size = nir_imm_ivec4(&b,
  928. b.shader->info.cs.local_size[0],
  929. b.shader->info.cs.local_size[1],
  930. b.shader->info.cs.local_size[2], 0);
  931. nir_ssa_def *global_id = nir_iadd(&b, nir_imul(&b, wg_id, block_size), invoc_id);
  932. nir_ssa_def *offset = nir_imul(&b, global_id, nir_imm_int(&b, 16));
  933. offset = nir_channel(&b, offset, 0);
  934. nir_intrinsic_instr *buf =
  935. nir_intrinsic_instr_create(b.shader,
  936. nir_intrinsic_vulkan_resource_index);
  937. buf->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
  938. buf->num_components = 1;
  939. nir_intrinsic_set_desc_set(buf, 0);
  940. nir_intrinsic_set_binding(buf, 0);
  941. nir_ssa_dest_init(&buf->instr, &buf->dest, buf->num_components, 32, NULL);
  942. nir_builder_instr_insert(&b, &buf->instr);
  943. nir_intrinsic_instr *constants =
  944. nir_intrinsic_instr_create(b.shader,
  945. nir_intrinsic_load_push_constant);
  946. nir_intrinsic_set_base(constants, 0);
  947. nir_intrinsic_set_range(constants, 8);
  948. constants->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
  949. constants->num_components = 2;
  950. nir_ssa_dest_init(&constants->instr, &constants->dest, 2, 32, "constants");
  951. nir_builder_instr_insert(&b, &constants->instr);
  952. nir_intrinsic_instr *load =
  953. nir_intrinsic_instr_create(b.shader, nir_intrinsic_load_ssbo);
  954. load->src[0] = nir_src_for_ssa(&buf->dest.ssa);
  955. load->src[1] = nir_src_for_ssa(offset);
  956. nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
  957. load->num_components = 4;
  958. nir_builder_instr_insert(&b, &load->instr);
  959. /* data = (data & ~htile_mask) | (htile_value & htile_mask) */
  960. nir_ssa_def *data =
  961. nir_iand(&b, &load->dest.ssa,
  962. nir_channel(&b, &constants->dest.ssa, 1));
  963. data = nir_ior(&b, data, nir_channel(&b, &constants->dest.ssa, 0));
  964. nir_intrinsic_instr *store =
  965. nir_intrinsic_instr_create(b.shader, nir_intrinsic_store_ssbo);
  966. store->src[0] = nir_src_for_ssa(data);
  967. store->src[1] = nir_src_for_ssa(&buf->dest.ssa);
  968. store->src[2] = nir_src_for_ssa(offset);
  969. nir_intrinsic_set_write_mask(store, 0xf);
  970. store->num_components = 4;
  971. nir_builder_instr_insert(&b, &store->instr);
  972. return b.shader;
  973. }
  974. static VkResult
  975. init_meta_clear_htile_mask_state(struct radv_device *device)
  976. {
  977. struct radv_meta_state *state = &device->meta_state;
  978. struct radv_shader_module cs = { .nir = NULL };
  979. VkResult result;
  980. cs.nir = build_clear_htile_mask_shader();
  981. VkDescriptorSetLayoutCreateInfo ds_layout_info = {
  982. .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
  983. .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR,
  984. .bindingCount = 1,
  985. .pBindings = (VkDescriptorSetLayoutBinding[]) {
  986. {
  987. .binding = 0,
  988. .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
  989. .descriptorCount = 1,
  990. .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
  991. .pImmutableSamplers = NULL
  992. },
  993. }
  994. };
  995. result = radv_CreateDescriptorSetLayout(radv_device_to_handle(device),
  996. &ds_layout_info, &state->alloc,
  997. &state->clear_htile_mask_ds_layout);
  998. if (result != VK_SUCCESS)
  999. goto fail;
  1000. VkPipelineLayoutCreateInfo p_layout_info = {
  1001. .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
  1002. .setLayoutCount = 1,
  1003. .pSetLayouts = &state->clear_htile_mask_ds_layout,
  1004. .pushConstantRangeCount = 1,
  1005. .pPushConstantRanges = &(VkPushConstantRange){
  1006. VK_SHADER_STAGE_COMPUTE_BIT, 0, 8,
  1007. },
  1008. };
  1009. result = radv_CreatePipelineLayout(radv_device_to_handle(device),
  1010. &p_layout_info, &state->alloc,
  1011. &state->clear_htile_mask_p_layout);
  1012. if (result != VK_SUCCESS)
  1013. goto fail;
  1014. VkPipelineShaderStageCreateInfo shader_stage = {
  1015. .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
  1016. .stage = VK_SHADER_STAGE_COMPUTE_BIT,
  1017. .module = radv_shader_module_to_handle(&cs),
  1018. .pName = "main",
  1019. .pSpecializationInfo = NULL,
  1020. };
  1021. VkComputePipelineCreateInfo pipeline_info = {
  1022. .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
  1023. .stage = shader_stage,
  1024. .flags = 0,
  1025. .layout = state->clear_htile_mask_p_layout,
  1026. };
  1027. result = radv_CreateComputePipelines(radv_device_to_handle(device),
  1028. radv_pipeline_cache_to_handle(&state->cache),
  1029. 1, &pipeline_info, NULL,
  1030. &state->clear_htile_mask_pipeline);
  1031. ralloc_free(cs.nir);
  1032. return result;
  1033. fail:
  1034. ralloc_free(cs.nir);
  1035. return result;
  1036. }
  1037. VkResult
  1038. radv_device_init_meta_clear_state(struct radv_device *device, bool on_demand)
  1039. {
  1040. VkResult res;
  1041. struct radv_meta_state *state = &device->meta_state;
  1042. VkPipelineLayoutCreateInfo pl_color_create_info = {
  1043. .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
  1044. .setLayoutCount = 0,
  1045. .pushConstantRangeCount = 1,
  1046. .pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_FRAGMENT_BIT, 0, 16},
  1047. };
  1048. res = radv_CreatePipelineLayout(radv_device_to_handle(device),
  1049. &pl_color_create_info,
  1050. &device->meta_state.alloc,
  1051. &device->meta_state.clear_color_p_layout);
  1052. if (res != VK_SUCCESS)
  1053. goto fail;
  1054. VkPipelineLayoutCreateInfo pl_depth_create_info = {
  1055. .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
  1056. .setLayoutCount = 0,
  1057. .pushConstantRangeCount = 1,
  1058. .pPushConstantRanges = &(VkPushConstantRange){VK_SHADER_STAGE_VERTEX_BIT, 0, 4},
  1059. };
  1060. res = radv_CreatePipelineLayout(radv_device_to_handle(device),
  1061. &pl_depth_create_info,
  1062. &device->meta_state.alloc,
  1063. &device->meta_state.clear_depth_p_layout);
  1064. if (res != VK_SUCCESS)
  1065. goto fail;
  1066. res = init_meta_clear_htile_mask_state(device);
  1067. if (res != VK_SUCCESS)
  1068. goto fail;
  1069. if (on_demand)
  1070. return VK_SUCCESS;
  1071. for (uint32_t i = 0; i < ARRAY_SIZE(state->clear); ++i) {
  1072. uint32_t samples = 1 << i;
  1073. for (uint32_t j = 0; j < NUM_META_FS_KEYS; ++j) {
  1074. VkFormat format = radv_fs_key_format_exemplars[j];
  1075. unsigned fs_key = radv_format_meta_fs_key(format);
  1076. assert(!state->clear[i].color_pipelines[fs_key]);
  1077. res = create_color_renderpass(device, format, samples,
  1078. &state->clear[i].render_pass[fs_key]);
  1079. if (res != VK_SUCCESS)
  1080. goto fail;
  1081. res = create_color_pipeline(device, samples, 0, &state->clear[i].color_pipelines[fs_key],
  1082. state->clear[i].render_pass[fs_key]);
  1083. if (res != VK_SUCCESS)
  1084. goto fail;
  1085. }
  1086. res = create_depthstencil_renderpass(device,
  1087. samples,
  1088. &state->clear[i].depthstencil_rp);
  1089. if (res != VK_SUCCESS)
  1090. goto fail;
  1091. for (uint32_t j = 0; j < NUM_DEPTH_CLEAR_PIPELINES; j++) {
  1092. res = create_depthstencil_pipeline(device,
  1093. VK_IMAGE_ASPECT_DEPTH_BIT,
  1094. samples,
  1095. j,
  1096. &state->clear[i].depth_only_pipeline[j],
  1097. state->clear[i].depthstencil_rp);
  1098. if (res != VK_SUCCESS)
  1099. goto fail;
  1100. res = create_depthstencil_pipeline(device,
  1101. VK_IMAGE_ASPECT_STENCIL_BIT,
  1102. samples,
  1103. j,
  1104. &state->clear[i].stencil_only_pipeline[j],
  1105. state->clear[i].depthstencil_rp);
  1106. if (res != VK_SUCCESS)
  1107. goto fail;
  1108. res = create_depthstencil_pipeline(device,
  1109. VK_IMAGE_ASPECT_DEPTH_BIT |
  1110. VK_IMAGE_ASPECT_STENCIL_BIT,
  1111. samples,
  1112. j,
  1113. &state->clear[i].depthstencil_pipeline[j],
  1114. state->clear[i].depthstencil_rp);
  1115. if (res != VK_SUCCESS)
  1116. goto fail;
  1117. }
  1118. }
  1119. return VK_SUCCESS;
  1120. fail:
  1121. radv_device_finish_meta_clear_state(device);
  1122. return res;
  1123. }
  1124. static uint32_t
  1125. radv_get_cmask_fast_clear_value(const struct radv_image *image)
  1126. {
  1127. uint32_t value = 0; /* Default value when no DCC. */
  1128. /* The fast-clear value is different for images that have both DCC and
  1129. * CMASK metadata.
  1130. */
  1131. if (radv_image_has_dcc(image)) {
  1132. /* DCC fast clear with MSAA should clear CMASK to 0xC. */
  1133. return image->info.samples > 1 ? 0xcccccccc : 0xffffffff;
  1134. }
  1135. return value;
  1136. }
  1137. uint32_t
  1138. radv_clear_cmask(struct radv_cmd_buffer *cmd_buffer,
  1139. struct radv_image *image, uint32_t value)
  1140. {
  1141. return radv_fill_buffer(cmd_buffer, image->bo,
  1142. image->offset + image->cmask.offset,
  1143. image->cmask.size, value);
  1144. }
  1145. uint32_t
  1146. radv_clear_fmask(struct radv_cmd_buffer *cmd_buffer,
  1147. struct radv_image *image, uint32_t value)
  1148. {
  1149. return radv_fill_buffer(cmd_buffer, image->bo,
  1150. image->offset + image->fmask.offset,
  1151. image->fmask.size, value);
  1152. }
  1153. uint32_t
  1154. radv_clear_dcc(struct radv_cmd_buffer *cmd_buffer,
  1155. struct radv_image *image, uint32_t value)
  1156. {
  1157. /* Mark the image as being compressed. */
  1158. radv_update_dcc_metadata(cmd_buffer, image, true);
  1159. return radv_fill_buffer(cmd_buffer, image->bo,
  1160. image->offset + image->dcc_offset,
  1161. image->surface.dcc_size, value);
  1162. }
  1163. static void vi_get_fast_clear_parameters(VkFormat format,
  1164. const VkClearColorValue *clear_value,
  1165. uint32_t* reset_value,
  1166. bool *can_avoid_fast_clear_elim)
  1167. {
  1168. bool values[4] = {};
  1169. int extra_channel;
  1170. bool main_value = false;
  1171. bool extra_value = false;
  1172. int i;
  1173. *can_avoid_fast_clear_elim = false;
  1174. *reset_value = 0x20202020U;
  1175. const struct vk_format_description *desc = vk_format_description(format);
  1176. if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32 ||
  1177. format == VK_FORMAT_R5G6B5_UNORM_PACK16 ||
  1178. format == VK_FORMAT_B5G6R5_UNORM_PACK16)
  1179. extra_channel = -1;
  1180. else if (desc->layout == VK_FORMAT_LAYOUT_PLAIN) {
  1181. if (radv_translate_colorswap(format, false) <= 1)
  1182. extra_channel = desc->nr_channels - 1;
  1183. else
  1184. extra_channel = 0;
  1185. } else
  1186. return;
  1187. for (i = 0; i < 4; i++) {
  1188. int index = desc->swizzle[i] - VK_SWIZZLE_X;
  1189. if (desc->swizzle[i] < VK_SWIZZLE_X ||
  1190. desc->swizzle[i] > VK_SWIZZLE_W)
  1191. continue;
  1192. if (desc->channel[i].pure_integer &&
  1193. desc->channel[i].type == VK_FORMAT_TYPE_SIGNED) {
  1194. /* Use the maximum value for clamping the clear color. */
  1195. int max = u_bit_consecutive(0, desc->channel[i].size - 1);
  1196. values[i] = clear_value->int32[i] != 0;
  1197. if (clear_value->int32[i] != 0 && MIN2(clear_value->int32[i], max) != max)
  1198. return;
  1199. } else if (desc->channel[i].pure_integer &&
  1200. desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED) {
  1201. /* Use the maximum value for clamping the clear color. */
  1202. unsigned max = u_bit_consecutive(0, desc->channel[i].size);
  1203. values[i] = clear_value->uint32[i] != 0U;
  1204. if (clear_value->uint32[i] != 0U && MIN2(clear_value->uint32[i], max) != max)
  1205. return;
  1206. } else {
  1207. values[i] = clear_value->float32[i] != 0.0F;
  1208. if (clear_value->float32[i] != 0.0F && clear_value->float32[i] != 1.0F)
  1209. return;
  1210. }
  1211. if (index == extra_channel)
  1212. extra_value = values[i];
  1213. else
  1214. main_value = values[i];
  1215. }
  1216. for (int i = 0; i < 4; ++i)
  1217. if (values[i] != main_value &&
  1218. desc->swizzle[i] - VK_SWIZZLE_X != extra_channel &&
  1219. desc->swizzle[i] >= VK_SWIZZLE_X &&
  1220. desc->swizzle[i] <= VK_SWIZZLE_W)
  1221. return;
  1222. *can_avoid_fast_clear_elim = true;
  1223. if (main_value)
  1224. *reset_value |= 0x80808080U;
  1225. if (extra_value)
  1226. *reset_value |= 0x40404040U;
  1227. return;
  1228. }
  1229. static bool
  1230. radv_can_fast_clear_color(struct radv_cmd_buffer *cmd_buffer,
  1231. const struct radv_image_view *iview,
  1232. VkImageLayout image_layout,
  1233. const VkClearRect *clear_rect,
  1234. VkClearColorValue clear_value,
  1235. uint32_t view_mask)
  1236. {
  1237. uint32_t clear_color[2];
  1238. if (!radv_image_view_can_fast_clear(cmd_buffer->device, iview))
  1239. return false;
  1240. if (!radv_layout_can_fast_clear(iview->image, image_layout, radv_image_queue_family_mask(iview->image, cmd_buffer->queue_family_index, cmd_buffer->queue_family_index)))
  1241. return false;
  1242. if (clear_rect->rect.offset.x || clear_rect->rect.offset.y ||
  1243. clear_rect->rect.extent.width != iview->image->info.width ||
  1244. clear_rect->rect.extent.height != iview->image->info.height)
  1245. return false;
  1246. if (view_mask && (iview->image->info.array_size >= 32 ||
  1247. (1u << iview->image->info.array_size) - 1u != view_mask))
  1248. return false;
  1249. if (!view_mask && clear_rect->baseArrayLayer != 0)
  1250. return false;
  1251. if (!view_mask && clear_rect->layerCount != iview->image->info.array_size)
  1252. return false;
  1253. /* DCC */
  1254. if (!radv_format_pack_clear_color(iview->vk_format,
  1255. clear_color, &clear_value))
  1256. return false;
  1257. if (radv_image_has_dcc(iview->image)) {
  1258. bool can_avoid_fast_clear_elim;
  1259. uint32_t reset_value;
  1260. vi_get_fast_clear_parameters(iview->vk_format,
  1261. &clear_value, &reset_value,
  1262. &can_avoid_fast_clear_elim);
  1263. if (iview->image->info.samples > 1) {
  1264. /* DCC fast clear with MSAA should clear CMASK. */
  1265. /* FIXME: This doesn't work for now. There is a
  1266. * hardware bug with fast clears and DCC for MSAA
  1267. * textures. AMDVLK has a workaround but it doesn't
  1268. * seem to work here. Note that we might emit useless
  1269. * CB flushes but that shouldn't matter.
  1270. */
  1271. if (!can_avoid_fast_clear_elim)
  1272. return false;
  1273. }
  1274. }
  1275. return true;
  1276. }
  1277. static void
  1278. radv_fast_clear_color(struct radv_cmd_buffer *cmd_buffer,
  1279. const struct radv_image_view *iview,
  1280. const VkClearAttachment *clear_att,
  1281. uint32_t subpass_att,
  1282. enum radv_cmd_flush_bits *pre_flush,
  1283. enum radv_cmd_flush_bits *post_flush)
  1284. {
  1285. VkClearColorValue clear_value = clear_att->clearValue.color;
  1286. uint32_t clear_color[2], flush_bits = 0;
  1287. uint32_t cmask_clear_value;
  1288. if (pre_flush) {
  1289. cmd_buffer->state.flush_bits |= (RADV_CMD_FLAG_FLUSH_AND_INV_CB |
  1290. RADV_CMD_FLAG_FLUSH_AND_INV_CB_META) & ~ *pre_flush;
  1291. *pre_flush |= cmd_buffer->state.flush_bits;
  1292. }
  1293. /* DCC */
  1294. radv_format_pack_clear_color(iview->vk_format, clear_color, &clear_value);
  1295. cmask_clear_value = radv_get_cmask_fast_clear_value(iview->image);
  1296. /* clear cmask buffer */
  1297. if (radv_image_has_dcc(iview->image)) {
  1298. uint32_t reset_value;
  1299. bool can_avoid_fast_clear_elim;
  1300. bool need_decompress_pass = false;
  1301. vi_get_fast_clear_parameters(iview->vk_format,
  1302. &clear_value, &reset_value,
  1303. &can_avoid_fast_clear_elim);
  1304. if (radv_image_has_cmask(iview->image)) {
  1305. flush_bits = radv_clear_cmask(cmd_buffer, iview->image,
  1306. cmask_clear_value);
  1307. need_decompress_pass = true;
  1308. }
  1309. if (!can_avoid_fast_clear_elim)
  1310. need_decompress_pass = true;
  1311. flush_bits |= radv_clear_dcc(cmd_buffer, iview->image, reset_value);
  1312. radv_update_fce_metadata(cmd_buffer, iview->image,
  1313. need_decompress_pass);
  1314. } else {
  1315. flush_bits = radv_clear_cmask(cmd_buffer, iview->image,
  1316. cmask_clear_value);
  1317. }
  1318. if (post_flush) {
  1319. *post_flush |= flush_bits;
  1320. }
  1321. radv_update_color_clear_metadata(cmd_buffer, iview->image, subpass_att,
  1322. clear_color);
  1323. }
  1324. /**
  1325. * The parameters mean that same as those in vkCmdClearAttachments.
  1326. */
  1327. static void
  1328. emit_clear(struct radv_cmd_buffer *cmd_buffer,
  1329. const VkClearAttachment *clear_att,
  1330. const VkClearRect *clear_rect,
  1331. enum radv_cmd_flush_bits *pre_flush,
  1332. enum radv_cmd_flush_bits *post_flush,
  1333. uint32_t view_mask)
  1334. {
  1335. const struct radv_framebuffer *fb = cmd_buffer->state.framebuffer;
  1336. const struct radv_subpass *subpass = cmd_buffer->state.subpass;
  1337. VkImageAspectFlags aspects = clear_att->aspectMask;
  1338. if (aspects & VK_IMAGE_ASPECT_COLOR_BIT) {
  1339. const uint32_t subpass_att = clear_att->colorAttachment;
  1340. assert(subpass_att < subpass->color_count);
  1341. const uint32_t pass_att = subpass->color_attachments[subpass_att].attachment;
  1342. if (pass_att == VK_ATTACHMENT_UNUSED)
  1343. return;
  1344. VkImageLayout image_layout = subpass->color_attachments[subpass_att].layout;
  1345. const struct radv_image_view *iview = fb ? fb->attachments[pass_att].attachment : NULL;
  1346. VkClearColorValue clear_value = clear_att->clearValue.color;
  1347. if (radv_can_fast_clear_color(cmd_buffer, iview, image_layout,
  1348. clear_rect, clear_value, view_mask)) {
  1349. radv_fast_clear_color(cmd_buffer, iview, clear_att,
  1350. subpass_att, pre_flush,
  1351. post_flush);
  1352. } else {
  1353. emit_color_clear(cmd_buffer, clear_att, clear_rect, view_mask);
  1354. }
  1355. } else {
  1356. const uint32_t pass_att = subpass->depth_stencil_attachment->attachment;
  1357. if (pass_att == VK_ATTACHMENT_UNUSED)
  1358. return;
  1359. VkImageLayout image_layout = subpass->depth_stencil_attachment->layout;
  1360. const struct radv_image_view *iview = fb ? fb->attachments[pass_att].attachment : NULL;
  1361. VkClearDepthStencilValue clear_value = clear_att->clearValue.depthStencil;
  1362. assert(aspects & (VK_IMAGE_ASPECT_DEPTH_BIT |
  1363. VK_IMAGE_ASPECT_STENCIL_BIT));
  1364. if (radv_can_fast_clear_depth(cmd_buffer, iview, image_layout,
  1365. aspects, clear_rect, clear_value,
  1366. view_mask)) {
  1367. radv_fast_clear_depth(cmd_buffer, iview, clear_att,
  1368. pre_flush, post_flush);
  1369. } else {
  1370. emit_depthstencil_clear(cmd_buffer, clear_att, clear_rect,
  1371. view_mask);
  1372. }
  1373. }
  1374. }
  1375. static inline bool
  1376. radv_attachment_needs_clear(struct radv_cmd_state *cmd_state, uint32_t a)
  1377. {
  1378. uint32_t view_mask = cmd_state->subpass->view_mask;
  1379. return (a != VK_ATTACHMENT_UNUSED &&
  1380. cmd_state->attachments[a].pending_clear_aspects &&
  1381. (!view_mask || (view_mask & ~cmd_state->attachments[a].cleared_views)));
  1382. }
  1383. static bool
  1384. radv_subpass_needs_clear(struct radv_cmd_buffer *cmd_buffer)
  1385. {
  1386. struct radv_cmd_state *cmd_state = &cmd_buffer->state;
  1387. uint32_t a;
  1388. if (!cmd_state->subpass)
  1389. return false;
  1390. for (uint32_t i = 0; i < cmd_state->subpass->color_count; ++i) {
  1391. a = cmd_state->subpass->color_attachments[i].attachment;
  1392. if (radv_attachment_needs_clear(cmd_state, a))
  1393. return true;
  1394. }
  1395. if (!cmd_state->subpass->depth_stencil_attachment)
  1396. return false;
  1397. a = cmd_state->subpass->depth_stencil_attachment->attachment;
  1398. return radv_attachment_needs_clear(cmd_state, a);
  1399. }
  1400. static void
  1401. radv_subpass_clear_attachment(struct radv_cmd_buffer *cmd_buffer,
  1402. struct radv_attachment_state *attachment,
  1403. const VkClearAttachment *clear_att,
  1404. enum radv_cmd_flush_bits *pre_flush,
  1405. enum radv_cmd_flush_bits *post_flush)
  1406. {
  1407. struct radv_cmd_state *cmd_state = &cmd_buffer->state;
  1408. uint32_t view_mask = cmd_state->subpass->view_mask;
  1409. VkClearRect clear_rect = {
  1410. .rect = cmd_state->render_area,
  1411. .baseArrayLayer = 0,
  1412. .layerCount = cmd_state->framebuffer->layers,
  1413. };
  1414. emit_clear(cmd_buffer, clear_att, &clear_rect, pre_flush, post_flush,
  1415. view_mask & ~attachment->cleared_views);
  1416. if (view_mask)
  1417. attachment->cleared_views |= view_mask;
  1418. else
  1419. attachment->pending_clear_aspects = 0;
  1420. }
  1421. /**
  1422. * Emit any pending attachment clears for the current subpass.
  1423. *
  1424. * @see radv_attachment_state::pending_clear_aspects
  1425. */
  1426. void
  1427. radv_cmd_buffer_clear_subpass(struct radv_cmd_buffer *cmd_buffer)
  1428. {
  1429. struct radv_cmd_state *cmd_state = &cmd_buffer->state;
  1430. struct radv_meta_saved_state saved_state;
  1431. enum radv_cmd_flush_bits pre_flush = 0;
  1432. enum radv_cmd_flush_bits post_flush = 0;
  1433. if (!radv_subpass_needs_clear(cmd_buffer))
  1434. return;
  1435. radv_meta_save(&saved_state, cmd_buffer,
  1436. RADV_META_SAVE_GRAPHICS_PIPELINE |
  1437. RADV_META_SAVE_CONSTANTS);
  1438. for (uint32_t i = 0; i < cmd_state->subpass->color_count; ++i) {
  1439. uint32_t a = cmd_state->subpass->color_attachments[i].attachment;
  1440. if (!radv_attachment_needs_clear(cmd_state, a))
  1441. continue;
  1442. assert(cmd_state->attachments[a].pending_clear_aspects ==
  1443. VK_IMAGE_ASPECT_COLOR_BIT);
  1444. VkClearAttachment clear_att = {
  1445. .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
  1446. .colorAttachment = i, /* Use attachment index relative to subpass */
  1447. .clearValue = cmd_state->attachments[a].clear_value,
  1448. };
  1449. radv_subpass_clear_attachment(cmd_buffer,
  1450. &cmd_state->attachments[a],
  1451. &clear_att, &pre_flush,
  1452. &post_flush);
  1453. }
  1454. if (cmd_state->subpass->depth_stencil_attachment) {
  1455. uint32_t ds = cmd_state->subpass->depth_stencil_attachment->attachment;
  1456. if (radv_attachment_needs_clear(cmd_state, ds)) {
  1457. VkClearAttachment clear_att = {
  1458. .aspectMask = cmd_state->attachments[ds].pending_clear_aspects,
  1459. .clearValue = cmd_state->attachments[ds].clear_value,
  1460. };
  1461. radv_subpass_clear_attachment(cmd_buffer,
  1462. &cmd_state->attachments[ds],
  1463. &clear_att, &pre_flush,
  1464. &post_flush);
  1465. }
  1466. }
  1467. radv_meta_restore(&saved_state, cmd_buffer);
  1468. cmd_buffer->state.flush_bits |= post_flush;
  1469. }
  1470. static void
  1471. radv_clear_image_layer(struct radv_cmd_buffer *cmd_buffer,
  1472. struct radv_image *image,
  1473. VkImageLayout image_layout,
  1474. const VkImageSubresourceRange *range,
  1475. VkFormat format, int level, int layer,
  1476. const VkClearValue *clear_val)
  1477. {
  1478. VkDevice device_h = radv_device_to_handle(cmd_buffer->device);
  1479. struct radv_image_view iview;
  1480. uint32_t width = radv_minify(image->info.width, range->baseMipLevel + level);
  1481. uint32_t height = radv_minify(image->info.height, range->baseMipLevel + level);
  1482. radv_image_view_init(&iview, cmd_buffer->device,
  1483. &(VkImageViewCreateInfo) {
  1484. .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
  1485. .image = radv_image_to_handle(image),
  1486. .viewType = radv_meta_get_view_type(image),
  1487. .format = format,
  1488. .subresourceRange = {
  1489. .aspectMask = range->aspectMask,
  1490. .baseMipLevel = range->baseMipLevel + level,
  1491. .levelCount = 1,
  1492. .baseArrayLayer = range->baseArrayLayer + layer,
  1493. .layerCount = 1
  1494. },
  1495. });
  1496. VkFramebuffer fb;
  1497. radv_CreateFramebuffer(device_h,
  1498. &(VkFramebufferCreateInfo) {
  1499. .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
  1500. .attachmentCount = 1,
  1501. .pAttachments = (VkImageView[]) {
  1502. radv_image_view_to_handle(&iview),
  1503. },
  1504. .width = width,
  1505. .height = height,
  1506. .layers = 1
  1507. },
  1508. &cmd_buffer->pool->alloc,
  1509. &fb);
  1510. VkAttachmentDescription att_desc = {
  1511. .format = iview.vk_format,
  1512. .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
  1513. .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
  1514. .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
  1515. .stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE,
  1516. .initialLayout = image_layout,
  1517. .finalLayout = image_layout,
  1518. };
  1519. VkSubpassDescription subpass_desc = {
  1520. .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
  1521. .inputAttachmentCount = 0,
  1522. .colorAttachmentCount = 0,
  1523. .pColorAttachments = NULL,
  1524. .pResolveAttachments = NULL,
  1525. .pDepthStencilAttachment = NULL,
  1526. .preserveAttachmentCount = 0,
  1527. .pPreserveAttachments = NULL,
  1528. };
  1529. const VkAttachmentReference att_ref = {
  1530. .attachment = 0,
  1531. .layout = image_layout,
  1532. };
  1533. if (range->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
  1534. subpass_desc.colorAttachmentCount = 1;
  1535. subpass_desc.pColorAttachments = &att_ref;
  1536. } else {
  1537. subpass_desc.pDepthStencilAttachment = &att_ref;
  1538. }
  1539. VkRenderPass pass;
  1540. radv_CreateRenderPass(device_h,
  1541. &(VkRenderPassCreateInfo) {
  1542. .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
  1543. .attachmentCount = 1,
  1544. .pAttachments = &att_desc,
  1545. .subpassCount = 1,
  1546. .pSubpasses = &subpass_desc,
  1547. },
  1548. &cmd_buffer->pool->alloc,
  1549. &pass);
  1550. radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer),
  1551. &(VkRenderPassBeginInfo) {
  1552. .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
  1553. .renderArea = {
  1554. .offset = { 0, 0, },
  1555. .extent = {
  1556. .width = width,
  1557. .height = height,
  1558. },
  1559. },
  1560. .renderPass = pass,
  1561. .framebuffer = fb,
  1562. .clearValueCount = 0,
  1563. .pClearValues = NULL,
  1564. },
  1565. VK_SUBPASS_CONTENTS_INLINE);
  1566. VkClearAttachment clear_att = {
  1567. .aspectMask = range->aspectMask,
  1568. .colorAttachment = 0,
  1569. .clearValue = *clear_val,
  1570. };
  1571. VkClearRect clear_rect = {
  1572. .rect = {
  1573. .offset = { 0, 0 },
  1574. .extent = { width, height },
  1575. },
  1576. .baseArrayLayer = range->baseArrayLayer,
  1577. .layerCount = 1, /* FINISHME: clear multi-layer framebuffer */
  1578. };
  1579. emit_clear(cmd_buffer, &clear_att, &clear_rect, NULL, NULL, 0);
  1580. radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer));
  1581. radv_DestroyRenderPass(device_h, pass,
  1582. &cmd_buffer->pool->alloc);
  1583. radv_DestroyFramebuffer(device_h, fb,
  1584. &cmd_buffer->pool->alloc);
  1585. }
  1586. /**
  1587. * Return TRUE if a fast color or depth clear has been performed.
  1588. */
  1589. static bool
  1590. radv_fast_clear_range(struct radv_cmd_buffer *cmd_buffer,
  1591. struct radv_image *image,
  1592. VkFormat format,
  1593. VkImageLayout image_layout,
  1594. const VkImageSubresourceRange *range,
  1595. const VkClearValue *clear_val)
  1596. {
  1597. struct radv_image_view iview;
  1598. radv_image_view_init(&iview, cmd_buffer->device,
  1599. &(VkImageViewCreateInfo) {
  1600. .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
  1601. .image = radv_image_to_handle(image),
  1602. .viewType = radv_meta_get_view_type(image),
  1603. .format = image->vk_format,
  1604. .subresourceRange = {
  1605. .aspectMask = range->aspectMask,
  1606. .baseMipLevel = range->baseMipLevel,
  1607. .levelCount = range->levelCount,
  1608. .baseArrayLayer = range->baseArrayLayer,
  1609. .layerCount = range->layerCount,
  1610. },
  1611. });
  1612. VkClearRect clear_rect = {
  1613. .rect = {
  1614. .offset = { 0, 0 },
  1615. .extent = {
  1616. radv_minify(image->info.width, range->baseMipLevel),
  1617. radv_minify(image->info.height, range->baseMipLevel),
  1618. },
  1619. },
  1620. .baseArrayLayer = range->baseArrayLayer,
  1621. .layerCount = range->layerCount,
  1622. };
  1623. VkClearAttachment clear_att = {
  1624. .aspectMask = range->aspectMask,
  1625. .colorAttachment = 0,
  1626. .clearValue = *clear_val,
  1627. };
  1628. if (vk_format_is_color(format)) {
  1629. if (radv_can_fast_clear_color(cmd_buffer, &iview,
  1630. image_layout, &clear_rect,
  1631. clear_att.clearValue.color, 0)) {
  1632. radv_fast_clear_color(cmd_buffer, &iview, &clear_att,
  1633. clear_att.colorAttachment,
  1634. NULL, NULL);
  1635. return true;
  1636. }
  1637. } else {
  1638. if (radv_can_fast_clear_depth(cmd_buffer, &iview, image_layout,
  1639. range->aspectMask, &clear_rect,
  1640. clear_att.clearValue.depthStencil, 0)) {
  1641. radv_fast_clear_depth(cmd_buffer, &iview, &clear_att,
  1642. NULL, NULL);
  1643. return true;
  1644. }
  1645. }
  1646. return false;
  1647. }
  1648. static void
  1649. radv_cmd_clear_image(struct radv_cmd_buffer *cmd_buffer,
  1650. struct radv_image *image,
  1651. VkImageLayout image_layout,
  1652. const VkClearValue *clear_value,
  1653. uint32_t range_count,
  1654. const VkImageSubresourceRange *ranges,
  1655. bool cs)
  1656. {
  1657. VkFormat format = image->vk_format;
  1658. VkClearValue internal_clear_value = *clear_value;
  1659. if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
  1660. uint32_t value;
  1661. format = VK_FORMAT_R32_UINT;
  1662. value = float3_to_rgb9e5(clear_value->color.float32);
  1663. internal_clear_value.color.uint32[0] = value;
  1664. }
  1665. if (format == VK_FORMAT_R4G4_UNORM_PACK8) {
  1666. uint8_t r, g;
  1667. format = VK_FORMAT_R8_UINT;
  1668. r = float_to_ubyte(clear_value->color.float32[0]) >> 4;
  1669. g = float_to_ubyte(clear_value->color.float32[1]) >> 4;
  1670. internal_clear_value.color.uint32[0] = (r << 4) | (g & 0xf);
  1671. }
  1672. if (format == VK_FORMAT_R32G32B32_UINT ||
  1673. format == VK_FORMAT_R32G32B32_SINT ||
  1674. format == VK_FORMAT_R32G32B32_SFLOAT)
  1675. cs = true;
  1676. for (uint32_t r = 0; r < range_count; r++) {
  1677. const VkImageSubresourceRange *range = &ranges[r];
  1678. /* Try to perform a fast clear first, otherwise fallback to
  1679. * the legacy path.
  1680. */
  1681. if (!cs &&
  1682. radv_fast_clear_range(cmd_buffer, image, format,
  1683. image_layout, range,
  1684. &internal_clear_value)) {
  1685. continue;
  1686. }
  1687. for (uint32_t l = 0; l < radv_get_levelCount(image, range); ++l) {
  1688. const uint32_t layer_count = image->type == VK_IMAGE_TYPE_3D ?
  1689. radv_minify(image->info.depth, range->baseMipLevel + l) :
  1690. radv_get_layerCount(image, range);
  1691. for (uint32_t s = 0; s < layer_count; ++s) {
  1692. if (cs) {
  1693. struct radv_meta_blit2d_surf surf;
  1694. surf.format = format;
  1695. surf.image = image;
  1696. surf.level = range->baseMipLevel + l;
  1697. surf.layer = range->baseArrayLayer + s;
  1698. surf.aspect_mask = range->aspectMask;
  1699. radv_meta_clear_image_cs(cmd_buffer, &surf,
  1700. &internal_clear_value.color);
  1701. } else {
  1702. radv_clear_image_layer(cmd_buffer, image, image_layout,
  1703. range, format, l, s, &internal_clear_value);
  1704. }
  1705. }
  1706. }
  1707. }
  1708. }
  1709. void radv_CmdClearColorImage(
  1710. VkCommandBuffer commandBuffer,
  1711. VkImage image_h,
  1712. VkImageLayout imageLayout,
  1713. const VkClearColorValue* pColor,
  1714. uint32_t rangeCount,
  1715. const VkImageSubresourceRange* pRanges)
  1716. {
  1717. RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
  1718. RADV_FROM_HANDLE(radv_image, image, image_h);
  1719. struct radv_meta_saved_state saved_state;
  1720. bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
  1721. if (cs) {
  1722. radv_meta_save(&saved_state, cmd_buffer,
  1723. RADV_META_SAVE_COMPUTE_PIPELINE |
  1724. RADV_META_SAVE_CONSTANTS |
  1725. RADV_META_SAVE_DESCRIPTORS);
  1726. } else {
  1727. radv_meta_save(&saved_state, cmd_buffer,
  1728. RADV_META_SAVE_GRAPHICS_PIPELINE |
  1729. RADV_META_SAVE_CONSTANTS);
  1730. }
  1731. radv_cmd_clear_image(cmd_buffer, image, imageLayout,
  1732. (const VkClearValue *) pColor,
  1733. rangeCount, pRanges, cs);
  1734. radv_meta_restore(&saved_state, cmd_buffer);
  1735. }
  1736. void radv_CmdClearDepthStencilImage(
  1737. VkCommandBuffer commandBuffer,
  1738. VkImage image_h,
  1739. VkImageLayout imageLayout,
  1740. const VkClearDepthStencilValue* pDepthStencil,
  1741. uint32_t rangeCount,
  1742. const VkImageSubresourceRange* pRanges)
  1743. {
  1744. RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
  1745. RADV_FROM_HANDLE(radv_image, image, image_h);
  1746. struct radv_meta_saved_state saved_state;
  1747. radv_meta_save(&saved_state, cmd_buffer,
  1748. RADV_META_SAVE_GRAPHICS_PIPELINE |
  1749. RADV_META_SAVE_CONSTANTS);
  1750. radv_cmd_clear_image(cmd_buffer, image, imageLayout,
  1751. (const VkClearValue *) pDepthStencil,
  1752. rangeCount, pRanges, false);
  1753. radv_meta_restore(&saved_state, cmd_buffer);
  1754. }
  1755. void radv_CmdClearAttachments(
  1756. VkCommandBuffer commandBuffer,
  1757. uint32_t attachmentCount,
  1758. const VkClearAttachment* pAttachments,
  1759. uint32_t rectCount,
  1760. const VkClearRect* pRects)
  1761. {
  1762. RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
  1763. struct radv_meta_saved_state saved_state;
  1764. enum radv_cmd_flush_bits pre_flush = 0;
  1765. enum radv_cmd_flush_bits post_flush = 0;
  1766. if (!cmd_buffer->state.subpass)
  1767. return;
  1768. radv_meta_save(&saved_state, cmd_buffer,
  1769. RADV_META_SAVE_GRAPHICS_PIPELINE |
  1770. RADV_META_SAVE_CONSTANTS);
  1771. /* FINISHME: We can do better than this dumb loop. It thrashes too much
  1772. * state.
  1773. */
  1774. for (uint32_t a = 0; a < attachmentCount; ++a) {
  1775. for (uint32_t r = 0; r < rectCount; ++r) {
  1776. emit_clear(cmd_buffer, &pAttachments[a], &pRects[r], &pre_flush, &post_flush,
  1777. cmd_buffer->state.subpass->view_mask);
  1778. }
  1779. }
  1780. radv_meta_restore(&saved_state, cmd_buffer);
  1781. cmd_buffer->state.flush_bits |= post_flush;
  1782. }