blob: 90cd716047a4a4627a30040690b6638ec3c38351 [file] [log] [blame]
Dave Airlief4e499e2016-10-07 09:16:09 +10001/*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28#include "util/mesa-sha1.h"
Eric Engestroma2ae2d12017-06-26 12:14:37 +010029#include "util/u_atomic.h"
Dave Airlief4e499e2016-10-07 09:16:09 +100030#include "radv_private.h"
31#include "nir/nir.h"
32#include "nir/nir_builder.h"
33#include "spirv/nir_spirv.h"
34
35#include <llvm-c/Core.h>
36#include <llvm-c/TargetMachine.h>
37
38#include "sid.h"
Dave Airlie0063da82017-06-06 08:33:53 +100039#include "gfx9d.h"
Dave Airlief4e499e2016-10-07 09:16:09 +100040#include "r600d_common.h"
41#include "ac_binary.h"
42#include "ac_llvm_util.h"
43#include "ac_nir_to_llvm.h"
44#include "vk_format.h"
45#include "util/debug.h"
Dave Airlief205e192017-04-21 03:38:05 +010046#include "ac_exp_param.h"
Timothy Arceri304b35b2017-03-14 15:50:34 +110047
Dave Airlief4e499e2016-10-07 09:16:09 +100048void radv_shader_variant_destroy(struct radv_device *device,
49 struct radv_shader_variant *variant);
50
51static const struct nir_shader_compiler_options nir_options = {
52 .vertex_id_zero_based = true,
53 .lower_scmp = true,
54 .lower_flrp32 = true,
55 .lower_fsat = true,
Bas Nieuwenhuizen18947fd2017-04-23 01:31:05 +020056 .lower_fdiv = true,
Dave Airlie2a2a2142017-05-03 08:59:56 +100057 .lower_sub = true,
Dave Airlief4e499e2016-10-07 09:16:09 +100058 .lower_pack_snorm_2x16 = true,
59 .lower_pack_snorm_4x8 = true,
60 .lower_pack_unorm_2x16 = true,
61 .lower_pack_unorm_4x8 = true,
62 .lower_unpack_snorm_2x16 = true,
63 .lower_unpack_snorm_4x8 = true,
64 .lower_unpack_unorm_2x16 = true,
65 .lower_unpack_unorm_4x8 = true,
66 .lower_extract_byte = true,
67 .lower_extract_word = true,
Bas Nieuwenhuizen14ae0bf2017-04-26 22:29:01 +020068 .max_unroll_iterations = 32
Dave Airlief4e499e2016-10-07 09:16:09 +100069};
70
71VkResult radv_CreateShaderModule(
72 VkDevice _device,
73 const VkShaderModuleCreateInfo* pCreateInfo,
74 const VkAllocationCallbacks* pAllocator,
75 VkShaderModule* pShaderModule)
76{
77 RADV_FROM_HANDLE(radv_device, device, _device);
78 struct radv_shader_module *module;
79
80 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
81 assert(pCreateInfo->flags == 0);
82
Dave Airlie4450f402016-10-14 13:36:45 +100083 module = vk_alloc2(&device->alloc, pAllocator,
Dave Airlief4e499e2016-10-07 09:16:09 +100084 sizeof(*module) + pCreateInfo->codeSize, 8,
85 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
86 if (module == NULL)
87 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
88
89 module->nir = NULL;
90 module->size = pCreateInfo->codeSize;
91 memcpy(module->data, pCreateInfo->pCode, module->size);
92
93 _mesa_sha1_compute(module->data, module->size, module->sha1);
94
95 *pShaderModule = radv_shader_module_to_handle(module);
96
97 return VK_SUCCESS;
98}
99
100void radv_DestroyShaderModule(
101 VkDevice _device,
102 VkShaderModule _module,
103 const VkAllocationCallbacks* pAllocator)
104{
105 RADV_FROM_HANDLE(radv_device, device, _device);
106 RADV_FROM_HANDLE(radv_shader_module, module, _module);
107
108 if (!module)
109 return;
110
Dave Airlie4450f402016-10-14 13:36:45 +1000111 vk_free2(&device->alloc, pAllocator, module);
Dave Airlief4e499e2016-10-07 09:16:09 +1000112}
113
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +0100114
115static void
116radv_pipeline_destroy(struct radv_device *device,
117 struct radv_pipeline *pipeline,
118 const VkAllocationCallbacks* allocator)
119{
120 for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i)
121 if (pipeline->shaders[i])
122 radv_shader_variant_destroy(device, pipeline->shaders[i]);
123
Dave Airlie99936d32017-01-20 09:55:37 +1000124 if (pipeline->gs_copy_shader)
125 radv_shader_variant_destroy(device, pipeline->gs_copy_shader);
126
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +0100127 vk_free2(&device->alloc, allocator, pipeline);
128}
129
Dave Airlief4e499e2016-10-07 09:16:09 +1000130void radv_DestroyPipeline(
131 VkDevice _device,
132 VkPipeline _pipeline,
133 const VkAllocationCallbacks* pAllocator)
134{
135 RADV_FROM_HANDLE(radv_device, device, _device);
136 RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
137
138 if (!_pipeline)
139 return;
140
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +0100141 radv_pipeline_destroy(device, pipeline, pAllocator);
Dave Airlief4e499e2016-10-07 09:16:09 +1000142}
143
144
145static void
146radv_optimize_nir(struct nir_shader *shader)
147{
148 bool progress;
149
150 do {
151 progress = false;
152
153 NIR_PASS_V(shader, nir_lower_vars_to_ssa);
Connor Abbott71684252017-06-07 14:16:31 -0700154 NIR_PASS_V(shader, nir_lower_64bit_pack);
Dave Airlief4e499e2016-10-07 09:16:09 +1000155 NIR_PASS_V(shader, nir_lower_alu_to_scalar);
156 NIR_PASS_V(shader, nir_lower_phis_to_scalar);
157
158 NIR_PASS(progress, shader, nir_copy_prop);
159 NIR_PASS(progress, shader, nir_opt_remove_phis);
160 NIR_PASS(progress, shader, nir_opt_dce);
Bas Nieuwenhuizen14ae0bf2017-04-26 22:29:01 +0200161 if (nir_opt_trivial_continues(shader)) {
162 progress = true;
163 NIR_PASS(progress, shader, nir_copy_prop);
164 NIR_PASS(progress, shader, nir_opt_dce);
165 }
166 NIR_PASS(progress, shader, nir_opt_if);
Dave Airlief4e499e2016-10-07 09:16:09 +1000167 NIR_PASS(progress, shader, nir_opt_dead_cf);
168 NIR_PASS(progress, shader, nir_opt_cse);
169 NIR_PASS(progress, shader, nir_opt_peephole_select, 8);
170 NIR_PASS(progress, shader, nir_opt_algebraic);
171 NIR_PASS(progress, shader, nir_opt_constant_folding);
172 NIR_PASS(progress, shader, nir_opt_undef);
Dave Airlie3c9af752016-11-02 01:23:11 +0000173 NIR_PASS(progress, shader, nir_opt_conditional_discard);
Bas Nieuwenhuizen14ae0bf2017-04-26 22:29:01 +0200174 if (shader->options->max_unroll_iterations) {
175 NIR_PASS(progress, shader, nir_opt_loop_unroll, 0);
176 }
Dave Airlief4e499e2016-10-07 09:16:09 +1000177 } while (progress);
178}
179
180static nir_shader *
181radv_shader_compile_to_nir(struct radv_device *device,
182 struct radv_shader_module *module,
183 const char *entrypoint_name,
184 gl_shader_stage stage,
185 const VkSpecializationInfo *spec_info,
186 bool dump)
187{
188 if (strcmp(entrypoint_name, "main") != 0) {
189 radv_finishme("Multiple shaders per module not really supported");
190 }
191
192 nir_shader *nir;
193 nir_function *entry_point;
194 if (module->nir) {
195 /* Some things such as our meta clear/blit code will give us a NIR
196 * shader directly. In that case, we just ignore the SPIR-V entirely
197 * and just use the NIR shader */
198 nir = module->nir;
199 nir->options = &nir_options;
200 nir_validate_shader(nir);
201
202 assert(exec_list_length(&nir->functions) == 1);
203 struct exec_node *node = exec_list_get_head(&nir->functions);
204 entry_point = exec_node_data(nir_function, node, node);
205 } else {
206 uint32_t *spirv = (uint32_t *) module->data;
207 assert(module->size % 4 == 0);
208
209 uint32_t num_spec_entries = 0;
210 struct nir_spirv_specialization *spec_entries = NULL;
211 if (spec_info && spec_info->mapEntryCount > 0) {
212 num_spec_entries = spec_info->mapEntryCount;
213 spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
214 for (uint32_t i = 0; i < num_spec_entries; i++) {
215 VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
216 const void *data = spec_info->pData + entry.offset;
217 assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
218
219 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
Samuel Iglesias Gonsálvezcc4ff6c2016-11-14 12:08:32 +0100220 if (spec_info->dataSize == 8)
221 spec_entries[i].data64 = *(const uint64_t *)data;
222 else
223 spec_entries[i].data32 = *(const uint32_t *)data;
Dave Airlief4e499e2016-10-07 09:16:09 +1000224 }
225 }
Dave Airlie44f833a2016-12-27 23:28:05 +0000226 const struct nir_spirv_supported_extensions supported_ext = {
Bas Nieuwenhuizenf5f8eb22017-01-31 21:37:48 +0100227 .draw_parameters = true,
Bas Nieuwenhuizen53873692017-02-15 00:55:19 +0100228 .float64 = true,
Bas Nieuwenhuizen4e6095f2017-02-15 01:00:07 +0100229 .image_read_without_format = true,
Bas Nieuwenhuizen53873692017-02-15 00:55:19 +0100230 .image_write_without_format = true,
Dave Airliec011fe72017-03-30 08:48:49 +0100231 .tessellation = true,
Dave Airlie1bc40ae2017-02-15 04:58:48 +0000232 .int64 = true,
Dave Airlie44f833a2016-12-27 23:28:05 +0000233 };
Dave Airlief4e499e2016-10-07 09:16:09 +1000234 entry_point = spirv_to_nir(spirv, module->size / 4,
235 spec_entries, num_spec_entries,
Dave Airlie44f833a2016-12-27 23:28:05 +0000236 stage, entrypoint_name, &supported_ext, &nir_options);
Dave Airlief4e499e2016-10-07 09:16:09 +1000237 nir = entry_point->shader;
238 assert(nir->stage == stage);
239 nir_validate_shader(nir);
240
241 free(spec_entries);
242
Bas Nieuwenhuizen65cbb992017-01-08 23:17:38 +0100243 /* We have to lower away local constant initializers right before we
244 * inline functions. That way they get properly initialized at the top
245 * of the function and not at the top of its caller.
246 */
Bas Nieuwenhuizen8aaca382017-01-08 23:23:52 +0100247 NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_local);
248 NIR_PASS_V(nir, nir_lower_returns);
249 NIR_PASS_V(nir, nir_inline_functions);
Dave Airlief4e499e2016-10-07 09:16:09 +1000250
251 /* Pick off the single entrypoint that we want */
252 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
253 if (func != entry_point)
254 exec_node_remove(&func->node);
255 }
256 assert(exec_list_length(&nir->functions) == 1);
257 entry_point->name = ralloc_strdup(entry_point, "main");
258
Bas Nieuwenhuizen8aaca382017-01-08 23:23:52 +0100259 NIR_PASS_V(nir, nir_remove_dead_variables,
260 nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
Dave Airlief4e499e2016-10-07 09:16:09 +1000261
Bas Nieuwenhuizen65cbb992017-01-08 23:17:38 +0100262 /* Now that we've deleted all but the main function, we can go ahead and
263 * lower the rest of the constant initializers.
264 */
Bas Nieuwenhuizen8aaca382017-01-08 23:23:52 +0100265 NIR_PASS_V(nir, nir_lower_constant_initializers, ~0);
266 NIR_PASS_V(nir, nir_lower_system_values);
Dave Airlie2b35b602017-03-29 15:12:31 +1000267 NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
Dave Airlief4e499e2016-10-07 09:16:09 +1000268 }
269
270 /* Vulkan uses the separate-shader linking model */
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700271 nir->info.separate_shader = true;
Dave Airlief4e499e2016-10-07 09:16:09 +1000272
Dave Airlief4e499e2016-10-07 09:16:09 +1000273 nir_shader_gather_info(nir, entry_point->impl);
274
275 nir_variable_mode indirect_mask = 0;
Dave Airlief4e499e2016-10-07 09:16:09 +1000276 indirect_mask |= nir_var_shader_in;
Dave Airlief4e499e2016-10-07 09:16:09 +1000277 indirect_mask |= nir_var_local;
278
279 nir_lower_indirect_derefs(nir, indirect_mask);
280
281 static const nir_lower_tex_options tex_options = {
282 .lower_txp = ~0,
283 };
284
285 nir_lower_tex(nir, &tex_options);
286
287 nir_lower_vars_to_ssa(nir);
288 nir_lower_var_copies(nir);
289 nir_lower_global_vars_to_local(nir);
290 nir_remove_dead_variables(nir, nir_var_local);
291 radv_optimize_nir(nir);
292
293 if (dump)
294 nir_print_shader(nir, stderr);
295
296 return nir;
297}
298
Dave Airlief395e342016-11-22 04:17:49 +0000299static const char *radv_get_shader_name(struct radv_shader_variant *var,
300 gl_shader_stage stage)
301{
302 switch (stage) {
Dave Airlief239f592017-03-30 08:14:45 +0100303 case MESA_SHADER_VERTEX: return var->info.vs.as_ls ? "Vertex Shader as LS" : var->info.vs.as_es ? "Vertex Shader as ES" : "Vertex Shader as VS";
Dave Airlie99936d32017-01-20 09:55:37 +1000304 case MESA_SHADER_GEOMETRY: return "Geometry Shader";
Dave Airlief395e342016-11-22 04:17:49 +0000305 case MESA_SHADER_FRAGMENT: return "Pixel Shader";
306 case MESA_SHADER_COMPUTE: return "Compute Shader";
Dave Airlief239f592017-03-30 08:14:45 +0100307 case MESA_SHADER_TESS_CTRL: return "Tessellation Control Shader";
308 case MESA_SHADER_TESS_EVAL: return var->info.tes.as_es ? "Tessellation Evaluation Shader as ES" : "Tessellation Evaluation Shader as VS";
Dave Airlief395e342016-11-22 04:17:49 +0000309 default:
310 return "Unknown shader";
311 };
312
313}
314static void radv_dump_pipeline_stats(struct radv_device *device, struct radv_pipeline *pipeline)
315{
Bas Nieuwenhuizen8406f792017-01-16 21:23:48 +0100316 unsigned lds_increment = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
Dave Airlief395e342016-11-22 04:17:49 +0000317 struct radv_shader_variant *var;
318 struct ac_shader_config *conf;
319 int i;
320 FILE *file = stderr;
321 unsigned max_simd_waves = 10;
322 unsigned lds_per_wave = 0;
323
324 for (i = 0; i < MESA_SHADER_STAGES; i++) {
325 if (!pipeline->shaders[i])
326 continue;
327 var = pipeline->shaders[i];
328
329 conf = &var->config;
330
331 if (i == MESA_SHADER_FRAGMENT) {
332 lds_per_wave = conf->lds_size * lds_increment +
333 align(var->info.fs.num_interp * 48, lds_increment);
334 }
335
336 if (conf->num_sgprs) {
Bas Nieuwenhuizen8406f792017-01-16 21:23:48 +0100337 if (device->physical_device->rad_info.chip_class >= VI)
Dave Airlief395e342016-11-22 04:17:49 +0000338 max_simd_waves = MIN2(max_simd_waves, 800 / conf->num_sgprs);
339 else
340 max_simd_waves = MIN2(max_simd_waves, 512 / conf->num_sgprs);
341 }
342
343 if (conf->num_vgprs)
344 max_simd_waves = MIN2(max_simd_waves, 256 / conf->num_vgprs);
345
346 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
347 * that PS can use.
348 */
349 if (lds_per_wave)
350 max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
351
352 fprintf(file, "\n%s:\n",
353 radv_get_shader_name(var, i));
354 if (i == MESA_SHADER_FRAGMENT) {
355 fprintf(file, "*** SHADER CONFIG ***\n"
356 "SPI_PS_INPUT_ADDR = 0x%04x\n"
357 "SPI_PS_INPUT_ENA = 0x%04x\n",
358 conf->spi_ps_input_addr, conf->spi_ps_input_ena);
359 }
360 fprintf(file, "*** SHADER STATS ***\n"
361 "SGPRS: %d\n"
362 "VGPRS: %d\n"
363 "Spilled SGPRs: %d\n"
364 "Spilled VGPRs: %d\n"
365 "Code Size: %d bytes\n"
366 "LDS: %d blocks\n"
367 "Scratch: %d bytes per wave\n"
368 "Max Waves: %d\n"
369 "********************\n\n\n",
370 conf->num_sgprs, conf->num_vgprs,
371 conf->spilled_sgprs, conf->spilled_vgprs, var->code_size,
372 conf->lds_size, conf->scratch_bytes_per_wave,
373 max_simd_waves);
374 }
375}
376
Dave Airlief4e499e2016-10-07 09:16:09 +1000377void radv_shader_variant_destroy(struct radv_device *device,
378 struct radv_shader_variant *variant)
379{
Eric Engestroma2ae2d12017-06-26 12:14:37 +0100380 if (!p_atomic_dec_zero(&variant->ref_count))
Dave Airlief4e499e2016-10-07 09:16:09 +1000381 return;
382
383 device->ws->buffer_destroy(variant->bo);
384 free(variant);
385}
386
Dave Airlie257866a2016-12-08 00:03:38 +0000387static void radv_fill_shader_variant(struct radv_device *device,
388 struct radv_shader_variant *variant,
389 struct ac_shader_binary *binary,
390 gl_shader_stage stage)
Dave Airlief4e499e2016-10-07 09:16:09 +1000391{
Dave Airlief4e499e2016-10-07 09:16:09 +1000392 bool scratch_enabled = variant->config.scratch_bytes_per_wave > 0;
393 unsigned vgpr_comp_cnt = 0;
394
Bas Nieuwenhuizenc4d7b9c2017-01-29 13:53:05 +0100395 if (scratch_enabled && !device->llvm_supports_spill)
396 radv_finishme("shader scratch support only available with LLVM 4.0");
397
398 variant->code_size = binary->code_size;
Dave Airlie823b55a2017-03-30 08:15:43 +0100399 variant->rsrc2 = S_00B12C_USER_SGPR(variant->info.num_user_sgprs) |
400 S_00B12C_SCRATCH_EN(scratch_enabled);
Dave Airlie257866a2016-12-08 00:03:38 +0000401
402 switch (stage) {
Dave Airlie823b55a2017-03-30 08:15:43 +0100403 case MESA_SHADER_TESS_EVAL:
404 vgpr_comp_cnt = 3;
405 /* fallthrough */
406 case MESA_SHADER_TESS_CTRL:
407 variant->rsrc2 |= S_00B42C_OC_LDS_EN(1);
408 break;
Dave Airlief4e499e2016-10-07 09:16:09 +1000409 case MESA_SHADER_VERTEX:
Dave Airlie99936d32017-01-20 09:55:37 +1000410 case MESA_SHADER_GEOMETRY:
Dave Airlief4e499e2016-10-07 09:16:09 +1000411 vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
412 break;
413 case MESA_SHADER_FRAGMENT:
Dave Airlief4e499e2016-10-07 09:16:09 +1000414 break;
415 case MESA_SHADER_COMPUTE:
Dave Airlie823b55a2017-03-30 08:15:43 +0100416 variant->rsrc2 |=
Dave Airlief4e499e2016-10-07 09:16:09 +1000417 S_00B84C_TGID_X_EN(1) | S_00B84C_TGID_Y_EN(1) |
418 S_00B84C_TGID_Z_EN(1) | S_00B84C_TIDIG_COMP_CNT(2) |
419 S_00B84C_TG_SIZE_EN(1) |
420 S_00B84C_LDS_SIZE(variant->config.lds_size);
421 break;
422 default:
423 unreachable("unsupported shader type");
424 break;
425 }
426
427 variant->rsrc1 = S_00B848_VGPRS((variant->config.num_vgprs - 1) / 4) |
428 S_00B848_SGPRS((variant->config.num_sgprs - 1) / 8) |
429 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt) |
430 S_00B848_DX10_CLAMP(1) |
431 S_00B848_FLOAT_MODE(variant->config.float_mode);
432
Dave Airlie257866a2016-12-08 00:03:38 +0000433 variant->bo = device->ws->buffer_create(device->ws, binary->code_size, 256,
Bas Nieuwenhuizen7c282b32017-03-12 14:12:19 +0100434 RADEON_DOMAIN_VRAM, RADEON_FLAG_CPU_ACCESS);
Dave Airlief4e499e2016-10-07 09:16:09 +1000435
436 void *ptr = device->ws->buffer_map(variant->bo);
Dave Airlie257866a2016-12-08 00:03:38 +0000437 memcpy(ptr, binary->code, binary->code_size);
Dave Airlief4e499e2016-10-07 09:16:09 +1000438 device->ws->buffer_unmap(variant->bo);
439
Dave Airlie257866a2016-12-08 00:03:38 +0000440
441}
442
443static struct radv_shader_variant *radv_shader_variant_create(struct radv_device *device,
444 struct nir_shader *shader,
445 struct radv_pipeline_layout *layout,
446 const union ac_shader_variant_key *key,
447 void** code_out,
448 unsigned *code_size_out,
449 bool dump)
450{
451 struct radv_shader_variant *variant = calloc(1, sizeof(struct radv_shader_variant));
Bas Nieuwenhuizen8406f792017-01-16 21:23:48 +0100452 enum radeon_family chip_family = device->physical_device->rad_info.family;
Dave Airlie257866a2016-12-08 00:03:38 +0000453 LLVMTargetMachineRef tm;
454 if (!variant)
455 return NULL;
456
457 struct ac_nir_compiler_options options = {0};
458 options.layout = layout;
459 if (key)
460 options.key = *key;
461
462 struct ac_shader_binary binary;
Dave Airlie9d9f0512017-07-06 02:56:21 +0100463 enum ac_target_machine_options tm_options = 0;
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +0100464 options.unsafe_math = !!(device->debug_flags & RADV_DEBUG_UNSAFE_MATH);
Dave Airlie257866a2016-12-08 00:03:38 +0000465 options.family = chip_family;
Bas Nieuwenhuizen8406f792017-01-16 21:23:48 +0100466 options.chip_class = device->physical_device->rad_info.chip_class;
Bas Nieuwenhuizenc4d7b9c2017-01-29 13:53:05 +0100467 options.supports_spill = device->llvm_supports_spill;
Dave Airlie9d9f0512017-07-06 02:56:21 +0100468 if (options.supports_spill)
469 tm_options |= AC_TM_SUPPORTS_SPILL;
Dave Airlie09d7c7b2017-07-06 03:06:01 +0100470 if (device->instance->perftest_flags & RADV_PERFTEST_SISCHED)
471 tm_options |= AC_TM_SISCHED;
Dave Airlie9d9f0512017-07-06 02:56:21 +0100472 tm = ac_create_target_machine(chip_family, tm_options);
Dave Airlie257866a2016-12-08 00:03:38 +0000473 ac_compile_nir_shader(tm, &binary, &variant->config,
474 &variant->info, shader, &options, dump);
475 LLVMDisposeTargetMachine(tm);
476
477 radv_fill_shader_variant(device, variant, &binary, shader->stage);
478
Dave Airlief4e499e2016-10-07 09:16:09 +1000479 if (code_out) {
480 *code_out = binary.code;
481 *code_size_out = binary.code_size;
482 } else
483 free(binary.code);
484 free(binary.config);
485 free(binary.rodata);
486 free(binary.global_symbol_offsets);
487 free(binary.relocs);
488 free(binary.disasm_string);
489 variant->ref_count = 1;
490 return variant;
491}
492
Dave Airlie99936d32017-01-20 09:55:37 +1000493static struct radv_shader_variant *
494radv_pipeline_create_gs_copy_shader(struct radv_pipeline *pipeline,
495 struct nir_shader *nir,
496 void** code_out,
497 unsigned *code_size_out,
498 bool dump_shader)
499{
500 struct radv_shader_variant *variant = calloc(1, sizeof(struct radv_shader_variant));
501 enum radeon_family chip_family = pipeline->device->physical_device->rad_info.family;
502 LLVMTargetMachineRef tm;
503 if (!variant)
504 return NULL;
505
506 struct ac_nir_compiler_options options = {0};
507 struct ac_shader_binary binary;
Dave Airlie9d9f0512017-07-06 02:56:21 +0100508 enum ac_target_machine_options tm_options = 0;
Dave Airlie99936d32017-01-20 09:55:37 +1000509 options.family = chip_family;
510 options.chip_class = pipeline->device->physical_device->rad_info.chip_class;
Dave Airlie9d9f0512017-07-06 02:56:21 +0100511 if (options.supports_spill)
512 tm_options |= AC_TM_SUPPORTS_SPILL;
Dave Airlie09d7c7b2017-07-06 03:06:01 +0100513 if (pipeline->device->instance->perftest_flags & RADV_PERFTEST_SISCHED)
514 tm_options |= AC_TM_SISCHED;
Dave Airlie9d9f0512017-07-06 02:56:21 +0100515 tm = ac_create_target_machine(chip_family, tm_options);
Dave Airlie99936d32017-01-20 09:55:37 +1000516 ac_create_gs_copy_shader(tm, nir, &binary, &variant->config, &variant->info, &options, dump_shader);
517 LLVMDisposeTargetMachine(tm);
518
519 radv_fill_shader_variant(pipeline->device, variant, &binary, MESA_SHADER_VERTEX);
520
521 if (code_out) {
522 *code_out = binary.code;
523 *code_size_out = binary.code_size;
524 } else
525 free(binary.code);
526 free(binary.config);
527 free(binary.rodata);
528 free(binary.global_symbol_offsets);
529 free(binary.relocs);
530 free(binary.disasm_string);
531 variant->ref_count = 1;
532 return variant;
533}
Dave Airlief4e499e2016-10-07 09:16:09 +1000534
535static struct radv_shader_variant *
536radv_pipeline_compile(struct radv_pipeline *pipeline,
537 struct radv_pipeline_cache *cache,
538 struct radv_shader_module *module,
539 const char *entrypoint,
540 gl_shader_stage stage,
541 const VkSpecializationInfo *spec_info,
542 struct radv_pipeline_layout *layout,
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +0100543 const union ac_shader_variant_key *key)
Dave Airlief4e499e2016-10-07 09:16:09 +1000544{
545 unsigned char sha1[20];
Dave Airlie99936d32017-01-20 09:55:37 +1000546 unsigned char gs_copy_sha1[20];
Dave Airlief4e499e2016-10-07 09:16:09 +1000547 struct radv_shader_variant *variant;
548 nir_shader *nir;
549 void *code = NULL;
Grigori Goronzy0b539ab2016-10-12 00:47:19 +0200550 unsigned code_size = 0;
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +0100551 bool dump = (pipeline->device->debug_flags & RADV_DEBUG_DUMP_SHADERS);
Dave Airlief4e499e2016-10-07 09:16:09 +1000552
553 if (module->nir)
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700554 _mesa_sha1_compute(module->nir->info.name,
555 strlen(module->nir->info.name),
Dave Airlief4e499e2016-10-07 09:16:09 +1000556 module->sha1);
557
Dave Airlie99936d32017-01-20 09:55:37 +1000558 radv_hash_shader(sha1, module, entrypoint, spec_info, layout, key, 0);
559 if (stage == MESA_SHADER_GEOMETRY)
560 radv_hash_shader(gs_copy_sha1, module, entrypoint, spec_info,
561 layout, key, 1);
Dave Airlief4e499e2016-10-07 09:16:09 +1000562
Timothy Arceri4ffdab72017-03-15 12:40:53 +1100563 variant = radv_create_shader_variant_from_pipeline_cache(pipeline->device,
564 cache,
565 sha1);
Dave Airlie99936d32017-01-20 09:55:37 +1000566
Timothy Arceri4ffdab72017-03-15 12:40:53 +1100567 if (stage == MESA_SHADER_GEOMETRY) {
568 pipeline->gs_copy_shader =
569 radv_create_shader_variant_from_pipeline_cache(
570 pipeline->device,
571 cache,
572 gs_copy_sha1);
Dave Airlief4e499e2016-10-07 09:16:09 +1000573 }
Timothy Arceri72ab7bb2017-03-15 13:07:40 +1100574
575 if (variant &&
576 (stage != MESA_SHADER_GEOMETRY || pipeline->gs_copy_shader))
Timothy Arceri4ffdab72017-03-15 12:40:53 +1100577 return variant;
Dave Airlief4e499e2016-10-07 09:16:09 +1000578
579 nir = radv_shader_compile_to_nir(pipeline->device,
580 module, entrypoint, stage,
581 spec_info, dump);
582 if (nir == NULL)
583 return NULL;
584
Timothy Arceri72ab7bb2017-03-15 13:07:40 +1100585 if (!variant) {
586 variant = radv_shader_variant_create(pipeline->device, nir,
587 layout, key, &code,
588 &code_size, dump);
589 }
Dave Airlie99936d32017-01-20 09:55:37 +1000590
Timothy Arceri72ab7bb2017-03-15 13:07:40 +1100591 if (stage == MESA_SHADER_GEOMETRY && !pipeline->gs_copy_shader) {
Dave Airlie99936d32017-01-20 09:55:37 +1000592 void *gs_copy_code = NULL;
593 unsigned gs_copy_code_size = 0;
594 pipeline->gs_copy_shader = radv_pipeline_create_gs_copy_shader(
595 pipeline, nir, &gs_copy_code, &gs_copy_code_size, dump);
596
Timothy Arceri4ffdab72017-03-15 12:40:53 +1100597 if (pipeline->gs_copy_shader) {
Dave Airlie99936d32017-01-20 09:55:37 +1000598 pipeline->gs_copy_shader =
Dave Airlie10c2b582017-03-20 13:24:02 +1000599 radv_pipeline_cache_insert_shader(cache,
Dave Airlie99936d32017-01-20 09:55:37 +1000600 gs_copy_sha1,
601 pipeline->gs_copy_shader,
602 gs_copy_code,
603 gs_copy_code_size);
604 }
605 }
Dave Airlief4e499e2016-10-07 09:16:09 +1000606 if (!module->nir)
Dave Airlie99936d32017-01-20 09:55:37 +1000607 ralloc_free(nir);
Dave Airlief4e499e2016-10-07 09:16:09 +1000608
Timothy Arceri4ffdab72017-03-15 12:40:53 +1100609 if (variant)
Dave Airlie10c2b582017-03-20 13:24:02 +1000610 variant = radv_pipeline_cache_insert_shader(cache, sha1, variant,
611 code, code_size);
Dave Airlief4e499e2016-10-07 09:16:09 +1000612
613 if (code)
614 free(code);
615 return variant;
616}
617
Dave Airlie60fc0542017-03-30 08:45:42 +0100618static union ac_shader_variant_key
Dave Airlie6a681702017-06-20 13:21:04 +1000619radv_compute_tes_key(bool as_es, bool export_prim_id)
Dave Airlie60fc0542017-03-30 08:45:42 +0100620{
621 union ac_shader_variant_key key;
622 memset(&key, 0, sizeof(key));
623 key.tes.as_es = as_es;
Dave Airlie6a681702017-06-20 13:21:04 +1000624 /* export prim id only happens when no geom shader */
625 if (!as_es)
626 key.tes.export_prim_id = export_prim_id;
Dave Airlie60fc0542017-03-30 08:45:42 +0100627 return key;
628}
629
630static union ac_shader_variant_key
631radv_compute_tcs_key(unsigned primitive_mode, unsigned input_vertices)
632{
633 union ac_shader_variant_key key;
634 memset(&key, 0, sizeof(key));
635 key.tcs.primitive_mode = primitive_mode;
636 key.tcs.input_vertices = input_vertices;
637 return key;
638}
639
640static void
641radv_tess_pipeline_compile(struct radv_pipeline *pipeline,
642 struct radv_pipeline_cache *cache,
643 struct radv_shader_module *tcs_module,
644 struct radv_shader_module *tes_module,
645 const char *tcs_entrypoint,
646 const char *tes_entrypoint,
647 const VkSpecializationInfo *tcs_spec_info,
648 const VkSpecializationInfo *tes_spec_info,
649 struct radv_pipeline_layout *layout,
650 unsigned input_vertices)
651{
652 unsigned char tcs_sha1[20], tes_sha1[20];
653 struct radv_shader_variant *tes_variant = NULL, *tcs_variant = NULL;
654 nir_shader *tes_nir, *tcs_nir;
655 void *tes_code = NULL, *tcs_code = NULL;
656 unsigned tes_code_size = 0, tcs_code_size = 0;
Dave Airlie6a681702017-06-20 13:21:04 +1000657 union ac_shader_variant_key tes_key;
Dave Airlie60fc0542017-03-30 08:45:42 +0100658 union ac_shader_variant_key tcs_key;
659 bool dump = (pipeline->device->debug_flags & RADV_DEBUG_DUMP_SHADERS);
660
Dave Airlie6a681702017-06-20 13:21:04 +1000661 tes_key = radv_compute_tes_key(radv_pipeline_has_gs(pipeline),
662 pipeline->shaders[MESA_SHADER_FRAGMENT]->info.fs.prim_id_input);
Dave Airlie60fc0542017-03-30 08:45:42 +0100663 if (tes_module->nir)
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700664 _mesa_sha1_compute(tes_module->nir->info.name,
665 strlen(tes_module->nir->info.name),
Dave Airlie60fc0542017-03-30 08:45:42 +0100666 tes_module->sha1);
667 radv_hash_shader(tes_sha1, tes_module, tes_entrypoint, tes_spec_info, layout, &tes_key, 0);
668
669 tes_variant = radv_create_shader_variant_from_pipeline_cache(pipeline->device,
670 cache,
671 tes_sha1);
672
673 if (tes_variant) {
674 tcs_key = radv_compute_tcs_key(tes_variant->info.tes.primitive_mode, input_vertices);
675
676 if (tcs_module->nir)
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700677 _mesa_sha1_compute(tcs_module->nir->info.name,
678 strlen(tcs_module->nir->info.name),
Dave Airlie60fc0542017-03-30 08:45:42 +0100679 tcs_module->sha1);
680
681 radv_hash_shader(tcs_sha1, tcs_module, tcs_entrypoint, tcs_spec_info, layout, &tcs_key, 0);
682
683 tcs_variant = radv_create_shader_variant_from_pipeline_cache(pipeline->device,
684 cache,
685 tcs_sha1);
686 }
687
688 if (tcs_variant && tes_variant) {
689 pipeline->shaders[MESA_SHADER_TESS_CTRL] = tcs_variant;
690 pipeline->shaders[MESA_SHADER_TESS_EVAL] = tes_variant;
691 return;
692 }
693
694 tes_nir = radv_shader_compile_to_nir(pipeline->device,
695 tes_module, tes_entrypoint, MESA_SHADER_TESS_EVAL,
696 tes_spec_info, dump);
697 if (tes_nir == NULL)
698 return;
699
700 tcs_nir = radv_shader_compile_to_nir(pipeline->device,
701 tcs_module, tcs_entrypoint, MESA_SHADER_TESS_CTRL,
702 tcs_spec_info, dump);
703 if (tcs_nir == NULL)
704 return;
705
706 nir_lower_tes_patch_vertices(tes_nir,
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700707 tcs_nir->info.tess.tcs_vertices_out);
Dave Airlie60fc0542017-03-30 08:45:42 +0100708
709 tes_variant = radv_shader_variant_create(pipeline->device, tes_nir,
710 layout, &tes_key, &tes_code,
711 &tes_code_size, dump);
712
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700713 tcs_key = radv_compute_tcs_key(tes_nir->info.tess.primitive_mode, input_vertices);
Dave Airlie60fc0542017-03-30 08:45:42 +0100714 if (tcs_module->nir)
Jason Ekstrandb86dba82017-05-08 09:20:21 -0700715 _mesa_sha1_compute(tcs_module->nir->info.name,
716 strlen(tcs_module->nir->info.name),
Dave Airlie60fc0542017-03-30 08:45:42 +0100717 tcs_module->sha1);
718
719 radv_hash_shader(tcs_sha1, tcs_module, tcs_entrypoint, tcs_spec_info, layout, &tcs_key, 0);
720
721 tcs_variant = radv_shader_variant_create(pipeline->device, tcs_nir,
722 layout, &tcs_key, &tcs_code,
723 &tcs_code_size, dump);
724
725 if (!tes_module->nir)
726 ralloc_free(tes_nir);
727
728 if (!tcs_module->nir)
729 ralloc_free(tcs_nir);
730
731 if (tes_variant)
732 tes_variant = radv_pipeline_cache_insert_shader(cache, tes_sha1, tes_variant,
733 tes_code, tes_code_size);
734
735 if (tcs_variant)
736 tcs_variant = radv_pipeline_cache_insert_shader(cache, tcs_sha1, tcs_variant,
737 tcs_code, tcs_code_size);
738
739 if (tes_code)
740 free(tes_code);
741 if (tcs_code)
742 free(tcs_code);
743 pipeline->shaders[MESA_SHADER_TESS_CTRL] = tcs_variant;
744 pipeline->shaders[MESA_SHADER_TESS_EVAL] = tes_variant;
745 return;
746}
747
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +0100748static VkResult
749radv_pipeline_scratch_init(struct radv_device *device,
750 struct radv_pipeline *pipeline)
751{
752 unsigned scratch_bytes_per_wave = 0;
753 unsigned max_waves = 0;
754 unsigned min_waves = 1;
755
756 for (int i = 0; i < MESA_SHADER_STAGES; ++i) {
757 if (pipeline->shaders[i]) {
758 unsigned max_stage_waves = device->scratch_waves;
759
760 scratch_bytes_per_wave = MAX2(scratch_bytes_per_wave,
761 pipeline->shaders[i]->config.scratch_bytes_per_wave);
762
763 max_stage_waves = MIN2(max_stage_waves,
764 4 * device->physical_device->rad_info.num_good_compute_units *
765 (256 / pipeline->shaders[i]->config.num_vgprs));
766 max_waves = MAX2(max_waves, max_stage_waves);
767 }
768 }
769
770 if (pipeline->shaders[MESA_SHADER_COMPUTE]) {
771 unsigned group_size = pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[0] *
772 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[1] *
773 pipeline->shaders[MESA_SHADER_COMPUTE]->info.cs.block_size[2];
774 min_waves = MAX2(min_waves, round_up_u32(group_size, 64));
775 }
776
777 if (scratch_bytes_per_wave)
778 max_waves = MIN2(max_waves, 0xffffffffu / scratch_bytes_per_wave);
779
780 if (scratch_bytes_per_wave && max_waves < min_waves) {
781 /* Not really true at this moment, but will be true on first
782 * execution. Avoid having hanging shaders. */
783 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
784 }
785 pipeline->scratch_bytes_per_wave = scratch_bytes_per_wave;
786 pipeline->max_waves = max_waves;
787 return VK_SUCCESS;
788}
789
Dave Airlief4e499e2016-10-07 09:16:09 +1000790static uint32_t si_translate_blend_function(VkBlendOp op)
791{
792 switch (op) {
793 case VK_BLEND_OP_ADD:
794 return V_028780_COMB_DST_PLUS_SRC;
795 case VK_BLEND_OP_SUBTRACT:
796 return V_028780_COMB_SRC_MINUS_DST;
797 case VK_BLEND_OP_REVERSE_SUBTRACT:
798 return V_028780_COMB_DST_MINUS_SRC;
799 case VK_BLEND_OP_MIN:
800 return V_028780_COMB_MIN_DST_SRC;
801 case VK_BLEND_OP_MAX:
802 return V_028780_COMB_MAX_DST_SRC;
803 default:
804 return 0;
805 }
806}
807
808static uint32_t si_translate_blend_factor(VkBlendFactor factor)
809{
810 switch (factor) {
811 case VK_BLEND_FACTOR_ZERO:
812 return V_028780_BLEND_ZERO;
813 case VK_BLEND_FACTOR_ONE:
814 return V_028780_BLEND_ONE;
815 case VK_BLEND_FACTOR_SRC_COLOR:
816 return V_028780_BLEND_SRC_COLOR;
817 case VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR:
818 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
819 case VK_BLEND_FACTOR_DST_COLOR:
820 return V_028780_BLEND_DST_COLOR;
821 case VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR:
822 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
823 case VK_BLEND_FACTOR_SRC_ALPHA:
824 return V_028780_BLEND_SRC_ALPHA;
825 case VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA:
826 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
827 case VK_BLEND_FACTOR_DST_ALPHA:
828 return V_028780_BLEND_DST_ALPHA;
829 case VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA:
830 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
831 case VK_BLEND_FACTOR_CONSTANT_COLOR:
832 return V_028780_BLEND_CONSTANT_COLOR;
833 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:
834 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
835 case VK_BLEND_FACTOR_CONSTANT_ALPHA:
836 return V_028780_BLEND_CONSTANT_ALPHA;
837 case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:
838 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
839 case VK_BLEND_FACTOR_SRC_ALPHA_SATURATE:
840 return V_028780_BLEND_SRC_ALPHA_SATURATE;
841 case VK_BLEND_FACTOR_SRC1_COLOR:
842 return V_028780_BLEND_SRC1_COLOR;
843 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
844 return V_028780_BLEND_INV_SRC1_COLOR;
845 case VK_BLEND_FACTOR_SRC1_ALPHA:
846 return V_028780_BLEND_SRC1_ALPHA;
847 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
848 return V_028780_BLEND_INV_SRC1_ALPHA;
849 default:
850 return 0;
851 }
852}
853
854static bool is_dual_src(VkBlendFactor factor)
855{
856 switch (factor) {
857 case VK_BLEND_FACTOR_SRC1_COLOR:
858 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR:
859 case VK_BLEND_FACTOR_SRC1_ALPHA:
860 case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA:
861 return true;
862 default:
863 return false;
864 }
865}
866
867static unsigned si_choose_spi_color_format(VkFormat vk_format,
868 bool blend_enable,
869 bool blend_need_alpha)
870{
871 const struct vk_format_description *desc = vk_format_description(vk_format);
872 unsigned format, ntype, swap;
873
874 /* Alpha is needed for alpha-to-coverage.
875 * Blending may be with or without alpha.
876 */
877 unsigned normal = 0; /* most optimal, may not support blending or export alpha */
878 unsigned alpha = 0; /* exports alpha, but may not support blending */
879 unsigned blend = 0; /* supports blending, but may not export alpha */
880 unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
881
882 format = radv_translate_colorformat(vk_format);
883 ntype = radv_translate_color_numformat(vk_format, desc,
884 vk_format_get_first_non_void_channel(vk_format));
885 swap = radv_translate_colorswap(vk_format, false);
886
887 /* Choose the SPI color formats. These are required values for Stoney/RB+.
888 * Other chips have multiple choices, though they are not necessarily better.
889 */
890 switch (format) {
891 case V_028C70_COLOR_5_6_5:
892 case V_028C70_COLOR_1_5_5_5:
893 case V_028C70_COLOR_5_5_5_1:
894 case V_028C70_COLOR_4_4_4_4:
895 case V_028C70_COLOR_10_11_11:
896 case V_028C70_COLOR_11_11_10:
897 case V_028C70_COLOR_8:
898 case V_028C70_COLOR_8_8:
899 case V_028C70_COLOR_8_8_8_8:
900 case V_028C70_COLOR_10_10_10_2:
901 case V_028C70_COLOR_2_10_10_10:
902 if (ntype == V_028C70_NUMBER_UINT)
903 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
904 else if (ntype == V_028C70_NUMBER_SINT)
905 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
906 else
907 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
908 break;
909
910 case V_028C70_COLOR_16:
911 case V_028C70_COLOR_16_16:
912 case V_028C70_COLOR_16_16_16_16:
913 if (ntype == V_028C70_NUMBER_UNORM ||
914 ntype == V_028C70_NUMBER_SNORM) {
915 /* UNORM16 and SNORM16 don't support blending */
916 if (ntype == V_028C70_NUMBER_UNORM)
917 normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
918 else
919 normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
920
921 /* Use 32 bits per channel for blending. */
922 if (format == V_028C70_COLOR_16) {
923 if (swap == V_028C70_SWAP_STD) { /* R */
924 blend = V_028714_SPI_SHADER_32_R;
925 blend_alpha = V_028714_SPI_SHADER_32_AR;
926 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
927 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
928 else
929 assert(0);
930 } else if (format == V_028C70_COLOR_16_16) {
931 if (swap == V_028C70_SWAP_STD) { /* RG */
932 blend = V_028714_SPI_SHADER_32_GR;
933 blend_alpha = V_028714_SPI_SHADER_32_ABGR;
934 } else if (swap == V_028C70_SWAP_ALT) /* RA */
935 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
936 else
937 assert(0);
938 } else /* 16_16_16_16 */
939 blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
940 } else if (ntype == V_028C70_NUMBER_UINT)
941 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
942 else if (ntype == V_028C70_NUMBER_SINT)
943 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
944 else if (ntype == V_028C70_NUMBER_FLOAT)
945 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
946 else
947 assert(0);
948 break;
949
950 case V_028C70_COLOR_32:
951 if (swap == V_028C70_SWAP_STD) { /* R */
952 blend = normal = V_028714_SPI_SHADER_32_R;
953 alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
954 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
955 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
956 else
957 assert(0);
958 break;
959
960 case V_028C70_COLOR_32_32:
961 if (swap == V_028C70_SWAP_STD) { /* RG */
962 blend = normal = V_028714_SPI_SHADER_32_GR;
963 alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
964 } else if (swap == V_028C70_SWAP_ALT) /* RA */
965 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
966 else
967 assert(0);
968 break;
969
970 case V_028C70_COLOR_32_32_32_32:
971 case V_028C70_COLOR_8_24:
972 case V_028C70_COLOR_24_8:
973 case V_028C70_COLOR_X24_8_32_FLOAT:
974 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
975 break;
976
977 default:
978 unreachable("unhandled blend format");
979 }
980
981 if (blend_enable && blend_need_alpha)
982 return blend_alpha;
983 else if(blend_need_alpha)
984 return alpha;
985 else if(blend_enable)
986 return blend;
987 else
988 return normal;
989}
990
991static unsigned si_get_cb_shader_mask(unsigned spi_shader_col_format)
992{
993 unsigned i, cb_shader_mask = 0;
994
995 for (i = 0; i < 8; i++) {
996 switch ((spi_shader_col_format >> (i * 4)) & 0xf) {
997 case V_028714_SPI_SHADER_ZERO:
998 break;
999 case V_028714_SPI_SHADER_32_R:
1000 cb_shader_mask |= 0x1 << (i * 4);
1001 break;
1002 case V_028714_SPI_SHADER_32_GR:
1003 cb_shader_mask |= 0x3 << (i * 4);
1004 break;
1005 case V_028714_SPI_SHADER_32_AR:
1006 cb_shader_mask |= 0x9 << (i * 4);
1007 break;
1008 case V_028714_SPI_SHADER_FP16_ABGR:
1009 case V_028714_SPI_SHADER_UNORM16_ABGR:
1010 case V_028714_SPI_SHADER_SNORM16_ABGR:
1011 case V_028714_SPI_SHADER_UINT16_ABGR:
1012 case V_028714_SPI_SHADER_SINT16_ABGR:
1013 case V_028714_SPI_SHADER_32_ABGR:
1014 cb_shader_mask |= 0xf << (i * 4);
1015 break;
1016 default:
1017 assert(0);
1018 }
1019 }
1020 return cb_shader_mask;
1021}
1022
1023static void
1024radv_pipeline_compute_spi_color_formats(struct radv_pipeline *pipeline,
1025 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1026 uint32_t blend_enable,
1027 uint32_t blend_need_alpha,
Dave Airlie73592b92016-11-02 00:33:33 +00001028 bool single_cb_enable,
1029 bool blend_mrt0_is_dual_src)
Dave Airlief4e499e2016-10-07 09:16:09 +10001030{
1031 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1032 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
1033 struct radv_blend_state *blend = &pipeline->graphics.blend;
1034 unsigned col_format = 0;
1035
1036 for (unsigned i = 0; i < (single_cb_enable ? 1 : subpass->color_count); ++i) {
1037 struct radv_render_pass_attachment *attachment;
1038 unsigned cf;
1039
1040 attachment = pass->attachments + subpass->color_attachments[i].attachment;
1041
1042 cf = si_choose_spi_color_format(attachment->format,
1043 blend_enable & (1 << i),
1044 blend_need_alpha & (1 << i));
1045
1046 col_format |= cf << (4 * i);
1047 }
1048
1049 blend->cb_shader_mask = si_get_cb_shader_mask(col_format);
1050
Dave Airlie73592b92016-11-02 00:33:33 +00001051 if (blend_mrt0_is_dual_src)
1052 col_format |= (col_format & 0xf) << 4;
Dave Airlief4e499e2016-10-07 09:16:09 +10001053 blend->spi_shader_col_format = col_format;
1054}
1055
1056static bool
1057format_is_int8(VkFormat format)
1058{
1059 const struct vk_format_description *desc = vk_format_description(format);
1060 int channel = vk_format_get_first_non_void_channel(format);
1061
1062 return channel >= 0 && desc->channel[channel].pure_integer &&
1063 desc->channel[channel].size == 8;
1064}
1065
1066unsigned radv_format_meta_fs_key(VkFormat format)
1067{
1068 unsigned col_format = si_choose_spi_color_format(format, false, false) - 1;
1069 bool is_int8 = format_is_int8(format);
1070
1071 return col_format + (is_int8 ? 3 : 0);
1072}
1073
1074static unsigned
1075radv_pipeline_compute_is_int8(const VkGraphicsPipelineCreateInfo *pCreateInfo)
1076{
1077 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1078 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
1079 unsigned is_int8 = 0;
1080
1081 for (unsigned i = 0; i < subpass->color_count; ++i) {
1082 struct radv_render_pass_attachment *attachment;
1083
1084 attachment = pass->attachments + subpass->color_attachments[i].attachment;
1085
1086 if (format_is_int8(attachment->format))
1087 is_int8 |= 1 << i;
1088 }
1089
1090 return is_int8;
1091}
1092
1093static void
1094radv_pipeline_init_blend_state(struct radv_pipeline *pipeline,
1095 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1096 const struct radv_graphics_pipeline_create_info *extra)
1097{
1098 const VkPipelineColorBlendStateCreateInfo *vkblend = pCreateInfo->pColorBlendState;
1099 struct radv_blend_state *blend = &pipeline->graphics.blend;
1100 unsigned mode = V_028808_CB_NORMAL;
1101 uint32_t blend_enable = 0, blend_need_alpha = 0;
Dave Airlie73592b92016-11-02 00:33:33 +00001102 bool blend_mrt0_is_dual_src = false;
Dave Airlief4e499e2016-10-07 09:16:09 +10001103 int i;
1104 bool single_cb_enable = false;
Darren Salt9b121512016-10-16 20:32:19 +01001105
1106 if (!vkblend)
1107 return;
1108
Dave Airlief4e499e2016-10-07 09:16:09 +10001109 if (extra && extra->custom_blend_mode) {
1110 single_cb_enable = true;
1111 mode = extra->custom_blend_mode;
1112 }
1113 blend->cb_color_control = 0;
1114 if (vkblend->logicOpEnable)
1115 blend->cb_color_control |= S_028808_ROP3(vkblend->logicOp | (vkblend->logicOp << 4));
1116 else
1117 blend->cb_color_control |= S_028808_ROP3(0xcc);
1118
1119 blend->db_alpha_to_mask = S_028B70_ALPHA_TO_MASK_OFFSET0(2) |
1120 S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
1121 S_028B70_ALPHA_TO_MASK_OFFSET2(2) |
1122 S_028B70_ALPHA_TO_MASK_OFFSET3(2);
1123
1124 blend->cb_target_mask = 0;
1125 for (i = 0; i < vkblend->attachmentCount; i++) {
1126 const VkPipelineColorBlendAttachmentState *att = &vkblend->pAttachments[i];
1127 unsigned blend_cntl = 0;
1128 VkBlendOp eqRGB = att->colorBlendOp;
1129 VkBlendFactor srcRGB = att->srcColorBlendFactor;
1130 VkBlendFactor dstRGB = att->dstColorBlendFactor;
1131 VkBlendOp eqA = att->alphaBlendOp;
1132 VkBlendFactor srcA = att->srcAlphaBlendFactor;
1133 VkBlendFactor dstA = att->dstAlphaBlendFactor;
1134
1135 blend->sx_mrt0_blend_opt[i] = S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) | S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);
1136
1137 if (!att->colorWriteMask)
1138 continue;
1139
1140 blend->cb_target_mask |= (unsigned)att->colorWriteMask << (4 * i);
1141 if (!att->blendEnable) {
1142 blend->cb_blend_control[i] = blend_cntl;
1143 continue;
1144 }
1145
1146 if (is_dual_src(srcRGB) || is_dual_src(dstRGB) || is_dual_src(srcA) || is_dual_src(dstA))
Dave Airlie73592b92016-11-02 00:33:33 +00001147 if (i == 0)
1148 blend_mrt0_is_dual_src = true;
1149
Dave Airlief4e499e2016-10-07 09:16:09 +10001150 if (eqRGB == VK_BLEND_OP_MIN || eqRGB == VK_BLEND_OP_MAX) {
1151 srcRGB = VK_BLEND_FACTOR_ONE;
1152 dstRGB = VK_BLEND_FACTOR_ONE;
1153 }
1154 if (eqA == VK_BLEND_OP_MIN || eqA == VK_BLEND_OP_MAX) {
1155 srcA = VK_BLEND_FACTOR_ONE;
1156 dstA = VK_BLEND_FACTOR_ONE;
1157 }
1158
1159 blend_cntl |= S_028780_ENABLE(1);
1160
1161 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
1162 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
1163 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
1164 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
1165 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
1166 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
1167 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
1168 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
1169 }
1170 blend->cb_blend_control[i] = blend_cntl;
1171
1172 blend_enable |= 1 << i;
1173
1174 if (srcRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
1175 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA ||
1176 srcRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
1177 dstRGB == VK_BLEND_FACTOR_SRC_ALPHA_SATURATE ||
1178 srcRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA ||
1179 dstRGB == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA)
1180 blend_need_alpha |= 1 << i;
1181 }
1182 for (i = vkblend->attachmentCount; i < 8; i++)
1183 blend->cb_blend_control[i] = 0;
1184
1185 if (blend->cb_target_mask)
1186 blend->cb_color_control |= S_028808_MODE(mode);
1187 else
1188 blend->cb_color_control |= S_028808_MODE(V_028808_CB_DISABLE);
1189
1190 radv_pipeline_compute_spi_color_formats(pipeline, pCreateInfo,
Dave Airlie73592b92016-11-02 00:33:33 +00001191 blend_enable, blend_need_alpha, single_cb_enable, blend_mrt0_is_dual_src);
Dave Airlief4e499e2016-10-07 09:16:09 +10001192}
1193
1194static uint32_t si_translate_stencil_op(enum VkStencilOp op)
1195{
1196 switch (op) {
1197 case VK_STENCIL_OP_KEEP:
1198 return V_02842C_STENCIL_KEEP;
1199 case VK_STENCIL_OP_ZERO:
1200 return V_02842C_STENCIL_ZERO;
1201 case VK_STENCIL_OP_REPLACE:
1202 return V_02842C_STENCIL_REPLACE_TEST;
1203 case VK_STENCIL_OP_INCREMENT_AND_CLAMP:
1204 return V_02842C_STENCIL_ADD_CLAMP;
1205 case VK_STENCIL_OP_DECREMENT_AND_CLAMP:
1206 return V_02842C_STENCIL_SUB_CLAMP;
1207 case VK_STENCIL_OP_INVERT:
1208 return V_02842C_STENCIL_INVERT;
1209 case VK_STENCIL_OP_INCREMENT_AND_WRAP:
1210 return V_02842C_STENCIL_ADD_WRAP;
1211 case VK_STENCIL_OP_DECREMENT_AND_WRAP:
1212 return V_02842C_STENCIL_SUB_WRAP;
1213 default:
1214 return 0;
1215 }
1216}
1217static void
1218radv_pipeline_init_depth_stencil_state(struct radv_pipeline *pipeline,
1219 const VkGraphicsPipelineCreateInfo *pCreateInfo,
1220 const struct radv_graphics_pipeline_create_info *extra)
1221{
1222 const VkPipelineDepthStencilStateCreateInfo *vkds = pCreateInfo->pDepthStencilState;
1223 struct radv_depth_stencil_state *ds = &pipeline->graphics.ds;
1224
1225 memset(ds, 0, sizeof(*ds));
1226 if (!vkds)
1227 return;
Dave Airlief4e499e2016-10-07 09:16:09 +10001228
Bas Nieuwenhuizen7c7196e2017-06-29 00:38:29 +02001229 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1230 struct radv_subpass *subpass = pass->subpasses + pCreateInfo->subpass;
1231 if (subpass->depth_stencil_attachment.attachment == VK_ATTACHMENT_UNUSED)
1232 return;
1233
1234 struct radv_render_pass_attachment *attachment = pass->attachments + subpass->depth_stencil_attachment.attachment;
1235 bool has_depth_attachment = vk_format_is_depth(attachment->format);
1236 bool has_stencil_attachment = vk_format_is_stencil(attachment->format);
1237
1238 if (has_depth_attachment) {
1239 ds->db_depth_control = S_028800_Z_ENABLE(vkds->depthTestEnable ? 1 : 0) |
1240 S_028800_Z_WRITE_ENABLE(vkds->depthWriteEnable ? 1 : 0) |
1241 S_028800_ZFUNC(vkds->depthCompareOp) |
1242 S_028800_DEPTH_BOUNDS_ENABLE(vkds->depthBoundsTestEnable ? 1 : 0);
1243 }
1244
1245 if (has_stencil_attachment && vkds->stencilTestEnable) {
Dave Airlief4e499e2016-10-07 09:16:09 +10001246 ds->db_depth_control |= S_028800_STENCIL_ENABLE(1) | S_028800_BACKFACE_ENABLE(1);
1247 ds->db_depth_control |= S_028800_STENCILFUNC(vkds->front.compareOp);
1248 ds->db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(vkds->front.failOp));
1249 ds->db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(vkds->front.passOp));
1250 ds->db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(vkds->front.depthFailOp));
1251
1252 ds->db_depth_control |= S_028800_STENCILFUNC_BF(vkds->back.compareOp);
1253 ds->db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(vkds->back.failOp));
1254 ds->db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(vkds->back.passOp));
1255 ds->db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(vkds->back.depthFailOp));
1256 }
1257
1258 if (extra) {
1259
1260 ds->db_render_control |= S_028000_DEPTH_CLEAR_ENABLE(extra->db_depth_clear);
1261 ds->db_render_control |= S_028000_STENCIL_CLEAR_ENABLE(extra->db_stencil_clear);
1262
1263 ds->db_render_control |= S_028000_RESUMMARIZE_ENABLE(extra->db_resummarize);
1264 ds->db_render_control |= S_028000_DEPTH_COMPRESS_DISABLE(extra->db_flush_depth_inplace);
1265 ds->db_render_control |= S_028000_STENCIL_COMPRESS_DISABLE(extra->db_flush_stencil_inplace);
1266 ds->db_render_override2 |= S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(extra->db_depth_disable_expclear);
1267 ds->db_render_override2 |= S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(extra->db_stencil_disable_expclear);
1268 }
1269}
1270
1271static uint32_t si_translate_fill(VkPolygonMode func)
1272{
1273 switch(func) {
1274 case VK_POLYGON_MODE_FILL:
1275 return V_028814_X_DRAW_TRIANGLES;
1276 case VK_POLYGON_MODE_LINE:
1277 return V_028814_X_DRAW_LINES;
1278 case VK_POLYGON_MODE_POINT:
1279 return V_028814_X_DRAW_POINTS;
1280 default:
1281 assert(0);
1282 return V_028814_X_DRAW_POINTS;
1283 }
1284}
1285static void
1286radv_pipeline_init_raster_state(struct radv_pipeline *pipeline,
1287 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1288{
1289 const VkPipelineRasterizationStateCreateInfo *vkraster = pCreateInfo->pRasterizationState;
1290 struct radv_raster_state *raster = &pipeline->graphics.raster;
1291
1292 memset(raster, 0, sizeof(*raster));
1293
1294 raster->spi_interp_control =
1295 S_0286D4_FLAT_SHADE_ENA(1) |
1296 S_0286D4_PNT_SPRITE_ENA(1) |
1297 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
1298 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
1299 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
1300 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
1301 S_0286D4_PNT_SPRITE_TOP_1(0); // vulkan is top to bottom - 1.0 at bottom
1302
Dave Airlie0232ea82017-03-28 11:48:38 +10001303
Dave Airlief4e499e2016-10-07 09:16:09 +10001304 raster->pa_cl_clip_cntl = S_028810_PS_UCP_MODE(3) |
1305 S_028810_DX_CLIP_SPACE_DEF(1) | // vulkan uses DX conventions.
1306 S_028810_ZCLIP_NEAR_DISABLE(vkraster->depthClampEnable ? 1 : 0) |
1307 S_028810_ZCLIP_FAR_DISABLE(vkraster->depthClampEnable ? 1 : 0) |
1308 S_028810_DX_RASTERIZATION_KILL(vkraster->rasterizerDiscardEnable ? 1 : 0) |
1309 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
1310
1311 raster->pa_su_vtx_cntl =
1312 S_028BE4_PIX_CENTER(1) | // TODO verify
1313 S_028BE4_ROUND_MODE(V_028BE4_X_ROUND_TO_EVEN) |
1314 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH);
1315
1316 raster->pa_su_sc_mode_cntl =
1317 S_028814_FACE(vkraster->frontFace) |
1318 S_028814_CULL_FRONT(!!(vkraster->cullMode & VK_CULL_MODE_FRONT_BIT)) |
1319 S_028814_CULL_BACK(!!(vkraster->cullMode & VK_CULL_MODE_BACK_BIT)) |
1320 S_028814_POLY_MODE(vkraster->polygonMode != VK_POLYGON_MODE_FILL) |
1321 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(vkraster->polygonMode)) |
1322 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(vkraster->polygonMode)) |
1323 S_028814_POLY_OFFSET_FRONT_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
1324 S_028814_POLY_OFFSET_BACK_ENABLE(vkraster->depthBiasEnable ? 1 : 0) |
1325 S_028814_POLY_OFFSET_PARA_ENABLE(vkraster->depthBiasEnable ? 1 : 0);
1326
1327}
1328
1329static void
1330radv_pipeline_init_multisample_state(struct radv_pipeline *pipeline,
1331 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1332{
1333 const VkPipelineMultisampleStateCreateInfo *vkms = pCreateInfo->pMultisampleState;
1334 struct radv_blend_state *blend = &pipeline->graphics.blend;
1335 struct radv_multisample_state *ms = &pipeline->graphics.ms;
Bas Nieuwenhuizen8406f792017-01-16 21:23:48 +01001336 unsigned num_tile_pipes = pipeline->device->physical_device->rad_info.num_tile_pipes;
Dave Airlief4e499e2016-10-07 09:16:09 +10001337 int ps_iter_samples = 1;
1338 uint32_t mask = 0xffff;
1339
Dave Airliea8b8e542017-03-28 05:48:27 +10001340 if (vkms)
1341 ms->num_samples = vkms->rasterizationSamples;
1342 else
1343 ms->num_samples = 1;
Dave Airlief3a3fea2016-11-24 00:18:21 +00001344
1345 if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.fs.force_persample) {
Dave Airliea8b8e542017-03-28 05:48:27 +10001346 ps_iter_samples = ms->num_samples;
Dave Airlief3a3fea2016-11-24 00:18:21 +00001347 }
1348
Dave Airlief4e499e2016-10-07 09:16:09 +10001349 ms->pa_sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);
1350 ms->pa_sc_aa_config = 0;
1351 ms->db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
1352 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
1353 ms->pa_sc_mode_cntl_1 =
1354 S_028A4C_WALK_FENCE_ENABLE(1) | //TODO linear dst fixes
1355 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
1356 /* always 1: */
1357 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
1358 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
1359 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
1360 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
1361 EG_S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
1362 EG_S_028A4C_FORCE_EOV_REZ_ENABLE(1);
Dave Airlie348f6362017-06-06 09:06:21 +10001363 ms->pa_sc_mode_cntl_0 = S_028A48_ALTERNATE_RBS_PER_TILE(pipeline->device->physical_device->rad_info.chip_class >= GFX9);
Dave Airlief4e499e2016-10-07 09:16:09 +10001364
Dave Airliea8b8e542017-03-28 05:48:27 +10001365 if (ms->num_samples > 1) {
1366 unsigned log_samples = util_logbase2(ms->num_samples);
Dave Airlief4e499e2016-10-07 09:16:09 +10001367 unsigned log_ps_iter_samples = util_logbase2(util_next_power_of_two(ps_iter_samples));
Dave Airlie348f6362017-06-06 09:06:21 +10001368 ms->pa_sc_mode_cntl_0 |= S_028A48_MSAA_ENABLE(1);
Dave Airlief4e499e2016-10-07 09:16:09 +10001369 ms->pa_sc_line_cntl |= S_028BDC_EXPAND_LINE_WIDTH(1); /* CM_R_028BDC_PA_SC_LINE_CNTL */
1370 ms->db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_samples) |
1371 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
1372 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
1373 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
1374 ms->pa_sc_aa_config |= S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
1375 S_028BE0_MAX_SAMPLE_DIST(radv_cayman_get_maxdist(log_samples)) |
1376 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples); /* CM_R_028BE0_PA_SC_AA_CONFIG */
1377 ms->pa_sc_mode_cntl_1 |= EG_S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
1378 }
1379
Dave Airliea8b8e542017-03-28 05:48:27 +10001380 if (vkms) {
1381 if (vkms->alphaToCoverageEnable)
1382 blend->db_alpha_to_mask |= S_028B70_ALPHA_TO_MASK_ENABLE(1);
Dave Airlief4e499e2016-10-07 09:16:09 +10001383
Dave Airliea8b8e542017-03-28 05:48:27 +10001384 if (vkms->pSampleMask)
1385 mask = vkms->pSampleMask[0] & 0xffff;
Dave Airlief4e499e2016-10-07 09:16:09 +10001386 }
1387
1388 ms->pa_sc_aa_mask[0] = mask | (mask << 16);
1389 ms->pa_sc_aa_mask[1] = mask | (mask << 16);
1390}
1391
Bas Nieuwenhuizen8a53e6e42017-03-29 22:58:10 +02001392static bool
1393radv_prim_can_use_guardband(enum VkPrimitiveTopology topology)
1394{
1395 switch (topology) {
1396 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1397 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1398 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1399 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1400 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1401 return false;
1402 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1403 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1404 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1405 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1406 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1407 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1408 return true;
1409 default:
1410 unreachable("unhandled primitive type");
1411 }
1412}
1413
Dave Airlief4e499e2016-10-07 09:16:09 +10001414static uint32_t
1415si_translate_prim(enum VkPrimitiveTopology topology)
1416{
1417 switch (topology) {
1418 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1419 return V_008958_DI_PT_POINTLIST;
1420 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1421 return V_008958_DI_PT_LINELIST;
1422 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1423 return V_008958_DI_PT_LINESTRIP;
1424 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1425 return V_008958_DI_PT_TRILIST;
1426 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1427 return V_008958_DI_PT_TRISTRIP;
1428 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1429 return V_008958_DI_PT_TRIFAN;
1430 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1431 return V_008958_DI_PT_LINELIST_ADJ;
1432 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1433 return V_008958_DI_PT_LINESTRIP_ADJ;
1434 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1435 return V_008958_DI_PT_TRILIST_ADJ;
1436 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1437 return V_008958_DI_PT_TRISTRIP_ADJ;
1438 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1439 return V_008958_DI_PT_PATCH;
1440 default:
1441 assert(0);
1442 return 0;
1443 }
1444}
1445
1446static uint32_t
Dave Airlie99936d32017-01-20 09:55:37 +10001447si_conv_gl_prim_to_gs_out(unsigned gl_prim)
1448{
1449 switch (gl_prim) {
1450 case 0: /* GL_POINTS */
1451 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
1452 case 1: /* GL_LINES */
1453 case 3: /* GL_LINE_STRIP */
1454 case 0xA: /* GL_LINE_STRIP_ADJACENCY_ARB */
1455 case 0x8E7A: /* GL_ISOLINES */
1456 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
1457
1458 case 4: /* GL_TRIANGLES */
1459 case 0xc: /* GL_TRIANGLES_ADJACENCY_ARB */
1460 case 5: /* GL_TRIANGLE_STRIP */
1461 case 7: /* GL_QUADS */
1462 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
1463 default:
1464 assert(0);
1465 return 0;
1466 }
1467}
1468
1469static uint32_t
Dave Airlief4e499e2016-10-07 09:16:09 +10001470si_conv_prim_to_gs_out(enum VkPrimitiveTopology topology)
1471{
1472 switch (topology) {
1473 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
1474 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
1475 return V_028A6C_OUTPRIM_TYPE_POINTLIST;
1476 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
1477 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
1478 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
1479 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
1480 return V_028A6C_OUTPRIM_TYPE_LINESTRIP;
1481 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
1482 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
1483 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
1484 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
1485 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
1486 return V_028A6C_OUTPRIM_TYPE_TRISTRIP;
1487 default:
1488 assert(0);
1489 return 0;
1490 }
1491}
1492
1493static unsigned si_map_swizzle(unsigned swizzle)
1494{
1495 switch (swizzle) {
1496 case VK_SWIZZLE_Y:
1497 return V_008F0C_SQ_SEL_Y;
1498 case VK_SWIZZLE_Z:
1499 return V_008F0C_SQ_SEL_Z;
1500 case VK_SWIZZLE_W:
1501 return V_008F0C_SQ_SEL_W;
1502 case VK_SWIZZLE_0:
1503 return V_008F0C_SQ_SEL_0;
1504 case VK_SWIZZLE_1:
1505 return V_008F0C_SQ_SEL_1;
1506 default: /* VK_SWIZZLE_X */
1507 return V_008F0C_SQ_SEL_X;
1508 }
1509}
1510
1511static void
1512radv_pipeline_init_dynamic_state(struct radv_pipeline *pipeline,
1513 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1514{
1515 radv_cmd_dirty_mask_t states = RADV_CMD_DIRTY_DYNAMIC_ALL;
1516 RADV_FROM_HANDLE(radv_render_pass, pass, pCreateInfo->renderPass);
1517 struct radv_subpass *subpass = &pass->subpasses[pCreateInfo->subpass];
1518
1519 pipeline->dynamic_state = default_dynamic_state;
1520
1521 if (pCreateInfo->pDynamicState) {
1522 /* Remove all of the states that are marked as dynamic */
1523 uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
1524 for (uint32_t s = 0; s < count; s++)
1525 states &= ~(1 << pCreateInfo->pDynamicState->pDynamicStates[s]);
1526 }
1527
1528 struct radv_dynamic_state *dynamic = &pipeline->dynamic_state;
1529
Darren Salt9b121512016-10-16 20:32:19 +01001530 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1531 *
1532 * pViewportState is [...] NULL if the pipeline
1533 * has rasterization disabled.
1534 */
1535 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable) {
1536 assert(pCreateInfo->pViewportState);
Dave Airlief4e499e2016-10-07 09:16:09 +10001537
Darren Salt9b121512016-10-16 20:32:19 +01001538 dynamic->viewport.count = pCreateInfo->pViewportState->viewportCount;
1539 if (states & (1 << VK_DYNAMIC_STATE_VIEWPORT)) {
1540 typed_memcpy(dynamic->viewport.viewports,
1541 pCreateInfo->pViewportState->pViewports,
1542 pCreateInfo->pViewportState->viewportCount);
1543 }
1544
1545 dynamic->scissor.count = pCreateInfo->pViewportState->scissorCount;
1546 if (states & (1 << VK_DYNAMIC_STATE_SCISSOR)) {
1547 typed_memcpy(dynamic->scissor.scissors,
1548 pCreateInfo->pViewportState->pScissors,
1549 pCreateInfo->pViewportState->scissorCount);
1550 }
Dave Airlief4e499e2016-10-07 09:16:09 +10001551 }
1552
1553 if (states & (1 << VK_DYNAMIC_STATE_LINE_WIDTH)) {
1554 assert(pCreateInfo->pRasterizationState);
1555 dynamic->line_width = pCreateInfo->pRasterizationState->lineWidth;
1556 }
1557
1558 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BIAS)) {
1559 assert(pCreateInfo->pRasterizationState);
1560 dynamic->depth_bias.bias =
1561 pCreateInfo->pRasterizationState->depthBiasConstantFactor;
1562 dynamic->depth_bias.clamp =
1563 pCreateInfo->pRasterizationState->depthBiasClamp;
1564 dynamic->depth_bias.slope =
1565 pCreateInfo->pRasterizationState->depthBiasSlopeFactor;
1566 }
1567
Darren Salt9b121512016-10-16 20:32:19 +01001568 /* Section 9.2 of the Vulkan 1.0.15 spec says:
1569 *
1570 * pColorBlendState is [...] NULL if the pipeline has rasterization
1571 * disabled or if the subpass of the render pass the pipeline is
1572 * created against does not use any color attachments.
1573 */
1574 bool uses_color_att = false;
1575 for (unsigned i = 0; i < subpass->color_count; ++i) {
1576 if (subpass->color_attachments[i].attachment != VK_ATTACHMENT_UNUSED) {
1577 uses_color_att = true;
1578 break;
1579 }
1580 }
1581
1582 if (uses_color_att && states & (1 << VK_DYNAMIC_STATE_BLEND_CONSTANTS)) {
Dave Airlief4e499e2016-10-07 09:16:09 +10001583 assert(pCreateInfo->pColorBlendState);
1584 typed_memcpy(dynamic->blend_constants,
1585 pCreateInfo->pColorBlendState->blendConstants, 4);
1586 }
1587
1588 /* If there is no depthstencil attachment, then don't read
1589 * pDepthStencilState. The Vulkan spec states that pDepthStencilState may
1590 * be NULL in this case. Even if pDepthStencilState is non-NULL, there is
1591 * no need to override the depthstencil defaults in
1592 * radv_pipeline::dynamic_state when there is no depthstencil attachment.
1593 *
Darren Salt9b121512016-10-16 20:32:19 +01001594 * Section 9.2 of the Vulkan 1.0.15 spec says:
Dave Airlief4e499e2016-10-07 09:16:09 +10001595 *
Darren Salt9b121512016-10-16 20:32:19 +01001596 * pDepthStencilState is [...] NULL if the pipeline has rasterization
1597 * disabled or if the subpass of the render pass the pipeline is created
1598 * against does not use a depth/stencil attachment.
Dave Airlief4e499e2016-10-07 09:16:09 +10001599 */
Darren Salt9b121512016-10-16 20:32:19 +01001600 if (!pCreateInfo->pRasterizationState->rasterizerDiscardEnable &&
1601 subpass->depth_stencil_attachment.attachment != VK_ATTACHMENT_UNUSED) {
1602 assert(pCreateInfo->pDepthStencilState);
1603
Dave Airlief4e499e2016-10-07 09:16:09 +10001604 if (states & (1 << VK_DYNAMIC_STATE_DEPTH_BOUNDS)) {
Dave Airlief4e499e2016-10-07 09:16:09 +10001605 dynamic->depth_bounds.min =
1606 pCreateInfo->pDepthStencilState->minDepthBounds;
1607 dynamic->depth_bounds.max =
1608 pCreateInfo->pDepthStencilState->maxDepthBounds;
1609 }
1610
1611 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK)) {
Dave Airlief4e499e2016-10-07 09:16:09 +10001612 dynamic->stencil_compare_mask.front =
1613 pCreateInfo->pDepthStencilState->front.compareMask;
1614 dynamic->stencil_compare_mask.back =
1615 pCreateInfo->pDepthStencilState->back.compareMask;
1616 }
1617
1618 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_WRITE_MASK)) {
Dave Airlief4e499e2016-10-07 09:16:09 +10001619 dynamic->stencil_write_mask.front =
1620 pCreateInfo->pDepthStencilState->front.writeMask;
1621 dynamic->stencil_write_mask.back =
1622 pCreateInfo->pDepthStencilState->back.writeMask;
1623 }
1624
1625 if (states & (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE)) {
Dave Airlief4e499e2016-10-07 09:16:09 +10001626 dynamic->stencil_reference.front =
1627 pCreateInfo->pDepthStencilState->front.reference;
1628 dynamic->stencil_reference.back =
1629 pCreateInfo->pDepthStencilState->back.reference;
1630 }
1631 }
1632
1633 pipeline->dynamic_state_mask = states;
1634}
1635
1636static union ac_shader_variant_key
Dave Airlie6a681702017-06-20 13:21:04 +10001637radv_compute_vs_key(const VkGraphicsPipelineCreateInfo *pCreateInfo, bool as_es, bool as_ls, bool export_prim_id)
Dave Airlief4e499e2016-10-07 09:16:09 +10001638{
1639 union ac_shader_variant_key key;
1640 const VkPipelineVertexInputStateCreateInfo *input_state =
1641 pCreateInfo->pVertexInputState;
1642
1643 memset(&key, 0, sizeof(key));
1644 key.vs.instance_rate_inputs = 0;
Dave Airlie99936d32017-01-20 09:55:37 +10001645 key.vs.as_es = as_es;
Dave Airlie4c60c682017-03-30 08:18:13 +01001646 key.vs.as_ls = as_ls;
Dave Airlie6a681702017-06-20 13:21:04 +10001647 key.vs.export_prim_id = export_prim_id;
Dave Airlief4e499e2016-10-07 09:16:09 +10001648
1649 for (unsigned i = 0; i < input_state->vertexAttributeDescriptionCount; ++i) {
1650 unsigned binding;
1651 binding = input_state->pVertexAttributeDescriptions[i].binding;
1652 if (input_state->pVertexBindingDescriptions[binding].inputRate)
1653 key.vs.instance_rate_inputs |= 1u << input_state->pVertexAttributeDescriptions[i].location;
1654 }
1655 return key;
1656}
1657
Dave Airlie8f41fe42017-01-20 10:21:19 +10001658static void
1659calculate_gs_ring_sizes(struct radv_pipeline *pipeline)
1660{
1661 struct radv_device *device = pipeline->device;
1662 unsigned num_se = device->physical_device->rad_info.max_se;
1663 unsigned wave_size = 64;
1664 unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */
1665 unsigned gs_vertex_reuse = 16 * num_se; /* GS_VERTEX_REUSE register (per SE) */
1666 unsigned alignment = 256 * num_se;
1667 /* The maximum size is 63.999 MB per SE. */
1668 unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se;
Dave Airlie8f41fe42017-01-20 10:21:19 +10001669 struct ac_shader_variant_info *gs_info = &pipeline->shaders[MESA_SHADER_GEOMETRY]->info;
Dave Airlie4c60c682017-03-30 08:18:13 +01001670 struct ac_es_output_info *es_info = radv_pipeline_has_tess(pipeline) ?
1671 &pipeline->shaders[MESA_SHADER_TESS_EVAL]->info.tes.es_info :
1672 &pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.es_info;
Dave Airlie931a8d02017-03-28 06:13:09 +10001673
Dave Airlie8f41fe42017-01-20 10:21:19 +10001674 /* Calculate the minimum size. */
Dave Airlie931a8d02017-03-28 06:13:09 +10001675 unsigned min_esgs_ring_size = align(es_info->esgs_itemsize * gs_vertex_reuse *
Dave Airlie8f41fe42017-01-20 10:21:19 +10001676 wave_size, alignment);
1677 /* These are recommended sizes, not minimum sizes. */
1678 unsigned esgs_ring_size = max_gs_waves * 2 * wave_size *
Dave Airlie931a8d02017-03-28 06:13:09 +10001679 es_info->esgs_itemsize * gs_info->gs.vertices_in;
Dave Airlie8f41fe42017-01-20 10:21:19 +10001680 unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size *
1681 gs_info->gs.max_gsvs_emit_size * 1; // no streams in VK (gs->max_gs_stream + 1);
1682
1683 min_esgs_ring_size = align(min_esgs_ring_size, alignment);
1684 esgs_ring_size = align(esgs_ring_size, alignment);
1685 gsvs_ring_size = align(gsvs_ring_size, alignment);
1686
1687 pipeline->graphics.esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size);
1688 pipeline->graphics.gsvs_ring_size = MIN2(gsvs_ring_size, max_size);
1689}
1690
Dave Airlie4c60c682017-03-30 08:18:13 +01001691static void si_multiwave_lds_size_workaround(struct radv_device *device,
1692 unsigned *lds_size)
1693{
1694 /* SPI barrier management bug:
1695 * Make sure we have at least 4k of LDS in use to avoid the bug.
1696 * It applies to workgroup sizes of more than one wavefront.
1697 */
1698 if (device->physical_device->rad_info.family == CHIP_BONAIRE ||
1699 device->physical_device->rad_info.family == CHIP_KABINI ||
1700 device->physical_device->rad_info.family == CHIP_MULLINS)
1701 *lds_size = MAX2(*lds_size, 8);
1702}
1703
1704static void
1705calculate_tess_state(struct radv_pipeline *pipeline,
1706 const VkGraphicsPipelineCreateInfo *pCreateInfo)
1707{
1708 unsigned num_tcs_input_cp = pCreateInfo->pTessellationState->patchControlPoints;
1709 unsigned num_tcs_output_cp, num_tcs_inputs, num_tcs_outputs;
1710 unsigned num_tcs_patch_outputs;
1711 unsigned input_vertex_size, output_vertex_size, pervertex_output_patch_size;
1712 unsigned input_patch_size, output_patch_size, output_patch0_offset;
1713 unsigned lds_size, hardware_lds_size;
1714 unsigned perpatch_output_offset;
1715 unsigned num_patches;
1716 struct radv_tessellation_state *tess = &pipeline->graphics.tess;
1717
1718 /* This calculates how shader inputs and outputs among VS, TCS, and TES
1719 * are laid out in LDS. */
1720 num_tcs_inputs = util_last_bit64(pipeline->shaders[MESA_SHADER_VERTEX]->info.vs.outputs_written);
1721
1722 num_tcs_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.outputs_written); //tcs->outputs_written
1723 num_tcs_output_cp = pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.tcs_vertices_out; //TCS VERTICES OUT
1724 num_tcs_patch_outputs = util_last_bit64(pipeline->shaders[MESA_SHADER_TESS_CTRL]->info.tcs.patch_outputs_written);
1725
1726 /* Ensure that we only need one wave per SIMD so we don't need to check
1727 * resource usage. Also ensures that the number of tcs in and out
1728 * vertices per threadgroup are at most 256.
1729 */
1730 input_vertex_size = num_tcs_inputs * 16;
1731 output_vertex_size = num_tcs_outputs * 16;
1732
1733 input_patch_size = num_tcs_input_cp * input_vertex_size;
1734
1735 pervertex_output_patch_size = num_tcs_output_cp * output_vertex_size;
1736 output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
1737 /* Ensure that we only need one wave per SIMD so we don't need to check
1738 * resource usage. Also ensures that the number of tcs in and out
1739 * vertices per threadgroup are at most 256.
1740 */
1741 num_patches = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp) * 4;
1742
1743 /* Make sure that the data fits in LDS. This assumes the shaders only
1744 * use LDS for the inputs and outputs.
1745 */
1746 hardware_lds_size = pipeline->device->physical_device->rad_info.chip_class >= CIK ? 65536 : 32768;
1747 num_patches = MIN2(num_patches, hardware_lds_size / (input_patch_size + output_patch_size));
1748
1749 /* Make sure the output data fits in the offchip buffer */
1750 num_patches = MIN2(num_patches,
1751 (pipeline->device->tess_offchip_block_dw_size * 4) /
1752 output_patch_size);
1753
1754 /* Not necessary for correctness, but improves performance. The
1755 * specific value is taken from the proprietary driver.
1756 */
1757 num_patches = MIN2(num_patches, 40);
1758
1759 /* SI bug workaround - limit LS-HS threadgroups to only one wave. */
1760 if (pipeline->device->physical_device->rad_info.chip_class == SI) {
1761 unsigned one_wave = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp);
1762 num_patches = MIN2(num_patches, one_wave);
1763 }
1764
1765 output_patch0_offset = input_patch_size * num_patches;
1766 perpatch_output_offset = output_patch0_offset + pervertex_output_patch_size;
1767
1768 lds_size = output_patch0_offset + output_patch_size * num_patches;
1769
1770 if (pipeline->device->physical_device->rad_info.chip_class >= CIK) {
1771 assert(lds_size <= 65536);
1772 lds_size = align(lds_size, 512) / 512;
1773 } else {
1774 assert(lds_size <= 32768);
1775 lds_size = align(lds_size, 256) / 256;
1776 }
1777 si_multiwave_lds_size_workaround(pipeline->device, &lds_size);
1778
1779 tess->lds_size = lds_size;
1780
1781 tess->tcs_in_layout = (input_patch_size / 4) |
1782 ((input_vertex_size / 4) << 13);
1783 tess->tcs_out_layout = (output_patch_size / 4) |
1784 ((output_vertex_size / 4) << 13);
1785 tess->tcs_out_offsets = (output_patch0_offset / 16) |
1786 ((perpatch_output_offset / 16) << 16);
1787 tess->offchip_layout = (pervertex_output_patch_size * num_patches << 16) |
1788 (num_tcs_output_cp << 9) | num_patches;
1789
1790 tess->ls_hs_config = S_028B58_NUM_PATCHES(num_patches) |
1791 S_028B58_HS_NUM_INPUT_CP(num_tcs_input_cp) |
1792 S_028B58_HS_NUM_OUTPUT_CP(num_tcs_output_cp);
1793 tess->num_patches = num_patches;
1794 tess->num_tcs_input_cp = num_tcs_input_cp;
1795
1796 struct radv_shader_variant *tes = pipeline->shaders[MESA_SHADER_TESS_EVAL];
1797 unsigned type = 0, partitioning = 0, topology = 0, distribution_mode = 0;
1798
1799 switch (tes->info.tes.primitive_mode) {
1800 case GL_TRIANGLES:
1801 type = V_028B6C_TESS_TRIANGLE;
1802 break;
1803 case GL_QUADS:
1804 type = V_028B6C_TESS_QUAD;
1805 break;
1806 case GL_ISOLINES:
1807 type = V_028B6C_TESS_ISOLINE;
1808 break;
1809 }
1810
1811 switch (tes->info.tes.spacing) {
1812 case TESS_SPACING_EQUAL:
1813 partitioning = V_028B6C_PART_INTEGER;
1814 break;
1815 case TESS_SPACING_FRACTIONAL_ODD:
1816 partitioning = V_028B6C_PART_FRAC_ODD;
1817 break;
1818 case TESS_SPACING_FRACTIONAL_EVEN:
1819 partitioning = V_028B6C_PART_FRAC_EVEN;
1820 break;
1821 default:
1822 break;
1823 }
1824
1825 if (tes->info.tes.point_mode)
1826 topology = V_028B6C_OUTPUT_POINT;
1827 else if (tes->info.tes.primitive_mode == GL_ISOLINES)
1828 topology = V_028B6C_OUTPUT_LINE;
1829 else if (tes->info.tes.ccw)
1830 topology = V_028B6C_OUTPUT_TRIANGLE_CW;
1831 else
1832 topology = V_028B6C_OUTPUT_TRIANGLE_CCW;
1833
1834 if (pipeline->device->has_distributed_tess) {
1835 if (pipeline->device->physical_device->rad_info.family == CHIP_FIJI ||
1836 pipeline->device->physical_device->rad_info.family >= CHIP_POLARIS10)
1837 distribution_mode = V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS;
1838 else
1839 distribution_mode = V_028B6C_DISTRIBUTION_MODE_DONUTS;
1840 } else
1841 distribution_mode = V_028B6C_DISTRIBUTION_MODE_NO_DIST;
1842
1843 tess->tf_param = S_028B6C_TYPE(type) |
1844 S_028B6C_PARTITIONING(partitioning) |
1845 S_028B6C_TOPOLOGY(topology) |
1846 S_028B6C_DISTRIBUTION_MODE(distribution_mode);
1847}
1848
Dave Airlie3360dbe2017-02-13 07:30:29 +00001849static const struct radv_prim_vertex_count prim_size_table[] = {
1850 [V_008958_DI_PT_NONE] = {0, 0},
1851 [V_008958_DI_PT_POINTLIST] = {1, 1},
1852 [V_008958_DI_PT_LINELIST] = {2, 2},
1853 [V_008958_DI_PT_LINESTRIP] = {2, 1},
1854 [V_008958_DI_PT_TRILIST] = {3, 3},
1855 [V_008958_DI_PT_TRIFAN] = {3, 1},
1856 [V_008958_DI_PT_TRISTRIP] = {3, 1},
1857 [V_008958_DI_PT_LINELIST_ADJ] = {4, 4},
1858 [V_008958_DI_PT_LINESTRIP_ADJ] = {4, 1},
1859 [V_008958_DI_PT_TRILIST_ADJ] = {6, 6},
1860 [V_008958_DI_PT_TRISTRIP_ADJ] = {6, 2},
1861 [V_008958_DI_PT_RECTLIST] = {3, 3},
1862 [V_008958_DI_PT_LINELOOP] = {2, 1},
1863 [V_008958_DI_PT_POLYGON] = {3, 1},
1864 [V_008958_DI_PT_2D_TRI_STRIP] = {0, 0},
1865};
1866
Dave Airliecd33a5c2017-03-28 11:33:35 +10001867static uint32_t si_vgt_gs_mode(struct radv_shader_variant *gs)
1868{
1869 unsigned gs_max_vert_out = gs->info.gs.vertices_out;
1870 unsigned cut_mode;
1871
1872 if (gs_max_vert_out <= 128) {
1873 cut_mode = V_028A40_GS_CUT_128;
1874 } else if (gs_max_vert_out <= 256) {
1875 cut_mode = V_028A40_GS_CUT_256;
1876 } else if (gs_max_vert_out <= 512) {
1877 cut_mode = V_028A40_GS_CUT_512;
1878 } else {
1879 assert(gs_max_vert_out <= 1024);
1880 cut_mode = V_028A40_GS_CUT_1024;
1881 }
1882
1883 return S_028A40_MODE(V_028A40_GS_SCENARIO_G) |
1884 S_028A40_CUT_MODE(cut_mode)|
1885 S_028A40_ES_WRITE_OPTIMIZE(1) |
1886 S_028A40_GS_WRITE_OPTIMIZE(1);
1887}
1888
Dave Airlie6a681702017-06-20 13:21:04 +10001889static void calculate_vgt_gs_mode(struct radv_pipeline *pipeline)
1890{
1891 struct radv_shader_variant *vs;
1892 vs = radv_pipeline_has_gs(pipeline) ? pipeline->gs_copy_shader : (radv_pipeline_has_tess(pipeline) ? pipeline->shaders[MESA_SHADER_TESS_EVAL] : pipeline->shaders[MESA_SHADER_VERTEX]);
1893
1894 struct ac_vs_output_info *outinfo = &vs->info.vs.outinfo;
1895
1896 pipeline->graphics.vgt_primitiveid_en = false;
1897 pipeline->graphics.vgt_gs_mode = 0;
1898
1899 if (radv_pipeline_has_gs(pipeline)) {
1900 pipeline->graphics.vgt_gs_mode = si_vgt_gs_mode(pipeline->shaders[MESA_SHADER_GEOMETRY]);
1901 } else if (outinfo->export_prim_id) {
1902 pipeline->graphics.vgt_gs_mode = S_028A40_MODE(V_028A40_GS_SCENARIO_A);
1903 pipeline->graphics.vgt_primitiveid_en = true;
1904 }
1905}
1906
Dave Airlie0232ea82017-03-28 11:48:38 +10001907static void calculate_pa_cl_vs_out_cntl(struct radv_pipeline *pipeline)
1908{
1909 struct radv_shader_variant *vs;
Dave Airlie4c60c682017-03-30 08:18:13 +01001910 vs = radv_pipeline_has_gs(pipeline) ? pipeline->gs_copy_shader : (radv_pipeline_has_tess(pipeline) ? pipeline->shaders[MESA_SHADER_TESS_EVAL] : pipeline->shaders[MESA_SHADER_VERTEX]);
Dave Airlie0232ea82017-03-28 11:48:38 +10001911
1912 struct ac_vs_output_info *outinfo = &vs->info.vs.outinfo;
1913
1914 unsigned clip_dist_mask, cull_dist_mask, total_mask;
1915 clip_dist_mask = outinfo->clip_dist_mask;
1916 cull_dist_mask = outinfo->cull_dist_mask;
1917 total_mask = clip_dist_mask | cull_dist_mask;
1918
1919 bool misc_vec_ena = outinfo->writes_pointsize ||
1920 outinfo->writes_layer ||
1921 outinfo->writes_viewport_index;
1922 pipeline->graphics.pa_cl_vs_out_cntl =
1923 S_02881C_USE_VTX_POINT_SIZE(outinfo->writes_pointsize) |
1924 S_02881C_USE_VTX_RENDER_TARGET_INDX(outinfo->writes_layer) |
1925 S_02881C_USE_VTX_VIEWPORT_INDX(outinfo->writes_viewport_index) |
1926 S_02881C_VS_OUT_MISC_VEC_ENA(misc_vec_ena) |
1927 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(misc_vec_ena) |
1928 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0f) != 0) |
1929 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xf0) != 0) |
1930 cull_dist_mask << 8 |
1931 clip_dist_mask;
1932
1933}
Dave Airlieb2cedb32017-04-21 03:17:23 +01001934
1935static uint32_t offset_to_ps_input(uint32_t offset, bool flat_shade)
1936{
1937 uint32_t ps_input_cntl;
Bas Nieuwenhuizen33ad6222017-05-03 19:52:12 +02001938 if (offset <= AC_EXP_PARAM_OFFSET_31) {
Dave Airlieb2cedb32017-04-21 03:17:23 +01001939 ps_input_cntl = S_028644_OFFSET(offset);
Bas Nieuwenhuizen33ad6222017-05-03 19:52:12 +02001940 if (flat_shade)
1941 ps_input_cntl |= S_028644_FLAT_SHADE(1);
1942 } else {
Dave Airlieb2cedb32017-04-21 03:17:23 +01001943 /* The input is a DEFAULT_VAL constant. */
Dave Airlief205e192017-04-21 03:38:05 +01001944 assert(offset >= AC_EXP_PARAM_DEFAULT_VAL_0000 &&
1945 offset <= AC_EXP_PARAM_DEFAULT_VAL_1111);
1946 offset -= AC_EXP_PARAM_DEFAULT_VAL_0000;
Dave Airlieb2cedb32017-04-21 03:17:23 +01001947 ps_input_cntl = S_028644_OFFSET(0x20) |
1948 S_028644_DEFAULT_VAL(offset);
1949 }
Dave Airlieb2cedb32017-04-21 03:17:23 +01001950 return ps_input_cntl;
1951}
1952
Dave Airlie92e9c142017-03-28 11:43:48 +10001953static void calculate_ps_inputs(struct radv_pipeline *pipeline)
1954{
1955 struct radv_shader_variant *ps, *vs;
1956 struct ac_vs_output_info *outinfo;
1957
1958 ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
Dave Airlie4c60c682017-03-30 08:18:13 +01001959 vs = radv_pipeline_has_gs(pipeline) ? pipeline->gs_copy_shader : (radv_pipeline_has_tess(pipeline) ? pipeline->shaders[MESA_SHADER_TESS_EVAL] : pipeline->shaders[MESA_SHADER_VERTEX]);
Dave Airlie92e9c142017-03-28 11:43:48 +10001960
1961 outinfo = &vs->info.vs.outinfo;
1962
1963 unsigned ps_offset = 0;
Dave Airlie92e9c142017-03-28 11:43:48 +10001964
Dave Airlieb2cedb32017-04-21 03:17:23 +01001965 if (ps->info.fs.prim_id_input) {
1966 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID];
Dave Airlief205e192017-04-21 03:38:05 +01001967 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
Dave Airlieb2cedb32017-04-21 03:17:23 +01001968 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true);
1969 ++ps_offset;
1970 }
Dave Airlie92e9c142017-03-28 11:43:48 +10001971 }
1972
Dave Airlieb2cedb32017-04-21 03:17:23 +01001973 if (ps->info.fs.layer_input) {
1974 unsigned vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_LAYER];
Dave Airlief205e192017-04-21 03:38:05 +01001975 if (vs_offset != AC_EXP_PARAM_UNDEFINED) {
Dave Airlieb2cedb32017-04-21 03:17:23 +01001976 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, true);
1977 ++ps_offset;
1978 }
Dave Airlie92e9c142017-03-28 11:43:48 +10001979 }
1980
Dave Airlieb858cb42017-04-21 03:02:46 +01001981 if (ps->info.fs.has_pcoord) {
1982 unsigned val;
1983 val = S_028644_PT_SPRITE_TEX(1) | S_028644_OFFSET(0x20);
1984 pipeline->graphics.ps_input_cntl[ps_offset] = val;
1985 ps_offset++;
1986 }
1987
Dave Airlie92e9c142017-03-28 11:43:48 +10001988 for (unsigned i = 0; i < 32 && (1u << i) <= ps->info.fs.input_mask; ++i) {
Dave Airlieb2cedb32017-04-21 03:17:23 +01001989 unsigned vs_offset;
1990 bool flat_shade;
Dave Airlie92e9c142017-03-28 11:43:48 +10001991 if (!(ps->info.fs.input_mask & (1u << i)))
1992 continue;
1993
Dave Airlieb2cedb32017-04-21 03:17:23 +01001994 vs_offset = outinfo->vs_output_param_offset[VARYING_SLOT_VAR0 + i];
Dave Airlief205e192017-04-21 03:38:05 +01001995 if (vs_offset == AC_EXP_PARAM_UNDEFINED) {
Dave Airlie92e9c142017-03-28 11:43:48 +10001996 pipeline->graphics.ps_input_cntl[ps_offset] = S_028644_OFFSET(0x20);
1997 ++ps_offset;
1998 continue;
1999 }
2000
Dave Airlie92e9c142017-03-28 11:43:48 +10002001 flat_shade = !!(ps->info.fs.flat_shaded_mask & (1u << ps_offset));
2002
Dave Airlieb2cedb32017-04-21 03:17:23 +01002003 pipeline->graphics.ps_input_cntl[ps_offset] = offset_to_ps_input(vs_offset, flat_shade);
Dave Airlie92e9c142017-03-28 11:43:48 +10002004 ++ps_offset;
2005 }
2006
2007 pipeline->graphics.ps_input_cntl_num = ps_offset;
2008}
2009
Dave Airlief4e499e2016-10-07 09:16:09 +10002010VkResult
2011radv_pipeline_init(struct radv_pipeline *pipeline,
2012 struct radv_device *device,
2013 struct radv_pipeline_cache *cache,
2014 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2015 const struct radv_graphics_pipeline_create_info *extra,
2016 const VkAllocationCallbacks *alloc)
2017{
2018 struct radv_shader_module fs_m = {0};
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +01002019 VkResult result;
Dave Airlief4e499e2016-10-07 09:16:09 +10002020
Dave Airlief4e499e2016-10-07 09:16:09 +10002021 if (alloc == NULL)
2022 alloc = &device->alloc;
2023
2024 pipeline->device = device;
2025 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
2026
2027 radv_pipeline_init_dynamic_state(pipeline, pCreateInfo);
2028 const VkPipelineShaderStageCreateInfo *pStages[MESA_SHADER_STAGES] = { 0, };
2029 struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
2030 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2031 gl_shader_stage stage = ffs(pCreateInfo->pStages[i].stage) - 1;
2032 pStages[stage] = &pCreateInfo->pStages[i];
2033 modules[stage] = radv_shader_module_from_handle(pStages[stage]->module);
2034 }
2035
2036 radv_pipeline_init_blend_state(pipeline, pCreateInfo, extra);
2037
Dave Airlie2a87ddb2017-06-20 12:50:04 +10002038 if (!modules[MESA_SHADER_FRAGMENT]) {
2039 nir_builder fs_b;
2040 nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
2041 fs_b.shader->info.name = ralloc_strdup(fs_b.shader, "noop_fs");
2042 fs_m.nir = fs_b.shader;
2043 modules[MESA_SHADER_FRAGMENT] = &fs_m;
2044 }
2045
2046 if (modules[MESA_SHADER_FRAGMENT]) {
2047 union ac_shader_variant_key key;
2048 key.fs.col_format = pipeline->graphics.blend.spi_shader_col_format;
2049 key.fs.is_int8 = radv_pipeline_compute_is_int8(pCreateInfo);
2050
2051 const VkPipelineShaderStageCreateInfo *stage = pStages[MESA_SHADER_FRAGMENT];
2052
2053 pipeline->shaders[MESA_SHADER_FRAGMENT] =
2054 radv_pipeline_compile(pipeline, cache, modules[MESA_SHADER_FRAGMENT],
2055 stage ? stage->pName : "main",
2056 MESA_SHADER_FRAGMENT,
2057 stage ? stage->pSpecializationInfo : NULL,
2058 pipeline->layout, &key);
2059 pipeline->active_stages |= mesa_to_vk_shader_stage(MESA_SHADER_FRAGMENT);
2060 }
2061
2062 if (fs_m.nir)
2063 ralloc_free(fs_m.nir);
2064
Dave Airlief4e499e2016-10-07 09:16:09 +10002065 if (modules[MESA_SHADER_VERTEX]) {
Dave Airlie4c60c682017-03-30 08:18:13 +01002066 bool as_es = false;
2067 bool as_ls = false;
Dave Airlie6a681702017-06-20 13:21:04 +10002068 bool export_prim_id = false;
Dave Airlie4c60c682017-03-30 08:18:13 +01002069 if (modules[MESA_SHADER_TESS_CTRL])
2070 as_ls = true;
2071 else if (modules[MESA_SHADER_GEOMETRY])
2072 as_es = true;
Dave Airlie6a681702017-06-20 13:21:04 +10002073 else if (pipeline->shaders[MESA_SHADER_FRAGMENT]->info.fs.prim_id_input)
2074 export_prim_id = true;
2075 union ac_shader_variant_key key = radv_compute_vs_key(pCreateInfo, as_es, as_ls, export_prim_id);
Dave Airlief4e499e2016-10-07 09:16:09 +10002076
2077 pipeline->shaders[MESA_SHADER_VERTEX] =
2078 radv_pipeline_compile(pipeline, cache, modules[MESA_SHADER_VERTEX],
2079 pStages[MESA_SHADER_VERTEX]->pName,
2080 MESA_SHADER_VERTEX,
2081 pStages[MESA_SHADER_VERTEX]->pSpecializationInfo,
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +01002082 pipeline->layout, &key);
Dave Airlief4e499e2016-10-07 09:16:09 +10002083
2084 pipeline->active_stages |= mesa_to_vk_shader_stage(MESA_SHADER_VERTEX);
2085 }
2086
Dave Airlie99936d32017-01-20 09:55:37 +10002087 if (modules[MESA_SHADER_GEOMETRY]) {
Dave Airlie6a681702017-06-20 13:21:04 +10002088 union ac_shader_variant_key key = radv_compute_vs_key(pCreateInfo, false, false, false);
Dave Airlie99936d32017-01-20 09:55:37 +10002089
2090 pipeline->shaders[MESA_SHADER_GEOMETRY] =
2091 radv_pipeline_compile(pipeline, cache, modules[MESA_SHADER_GEOMETRY],
2092 pStages[MESA_SHADER_GEOMETRY]->pName,
2093 MESA_SHADER_GEOMETRY,
2094 pStages[MESA_SHADER_GEOMETRY]->pSpecializationInfo,
2095 pipeline->layout, &key);
2096
2097 pipeline->active_stages |= mesa_to_vk_shader_stage(MESA_SHADER_GEOMETRY);
Dave Airlie6a681702017-06-20 13:21:04 +10002098 }
Dave Airlie99936d32017-01-20 09:55:37 +10002099
Dave Airlie60fc0542017-03-30 08:45:42 +01002100 if (modules[MESA_SHADER_TESS_EVAL]) {
2101 assert(modules[MESA_SHADER_TESS_CTRL]);
2102
2103 radv_tess_pipeline_compile(pipeline,
2104 cache,
2105 modules[MESA_SHADER_TESS_CTRL],
2106 modules[MESA_SHADER_TESS_EVAL],
2107 pStages[MESA_SHADER_TESS_CTRL]->pName,
2108 pStages[MESA_SHADER_TESS_EVAL]->pName,
2109 pStages[MESA_SHADER_TESS_CTRL]->pSpecializationInfo,
2110 pStages[MESA_SHADER_TESS_EVAL]->pSpecializationInfo,
2111 pipeline->layout,
2112 pCreateInfo->pTessellationState->patchControlPoints);
2113 pipeline->active_stages |= mesa_to_vk_shader_stage(MESA_SHADER_TESS_EVAL) |
2114 mesa_to_vk_shader_stage(MESA_SHADER_TESS_CTRL);
2115 }
2116
Dave Airlief4e499e2016-10-07 09:16:09 +10002117 radv_pipeline_init_depth_stencil_state(pipeline, pCreateInfo, extra);
2118 radv_pipeline_init_raster_state(pipeline, pCreateInfo);
2119 radv_pipeline_init_multisample_state(pipeline, pCreateInfo);
2120 pipeline->graphics.prim = si_translate_prim(pCreateInfo->pInputAssemblyState->topology);
Bas Nieuwenhuizen8a53e6e42017-03-29 22:58:10 +02002121 pipeline->graphics.can_use_guardband = radv_prim_can_use_guardband(pCreateInfo->pInputAssemblyState->topology);
2122
Dave Airlie99936d32017-01-20 09:55:37 +10002123 if (radv_pipeline_has_gs(pipeline)) {
2124 pipeline->graphics.gs_out = si_conv_gl_prim_to_gs_out(pipeline->shaders[MESA_SHADER_GEOMETRY]->info.gs.output_prim);
Bas Nieuwenhuizen8a53e6e42017-03-29 22:58:10 +02002125 pipeline->graphics.can_use_guardband = pipeline->graphics.gs_out == V_028A6C_OUTPRIM_TYPE_TRISTRIP;
Dave Airlie99936d32017-01-20 09:55:37 +10002126 } else {
2127 pipeline->graphics.gs_out = si_conv_prim_to_gs_out(pCreateInfo->pInputAssemblyState->topology);
2128 }
Dave Airlief4e499e2016-10-07 09:16:09 +10002129 if (extra && extra->use_rectlist) {
2130 pipeline->graphics.prim = V_008958_DI_PT_RECTLIST;
2131 pipeline->graphics.gs_out = V_028A6C_OUTPRIM_TYPE_TRISTRIP;
Bas Nieuwenhuizen8a53e6e42017-03-29 22:58:10 +02002132 pipeline->graphics.can_use_guardband = true;
Dave Airlief4e499e2016-10-07 09:16:09 +10002133 }
2134 pipeline->graphics.prim_restart_enable = !!pCreateInfo->pInputAssemblyState->primitiveRestartEnable;
Dave Airlie3360dbe2017-02-13 07:30:29 +00002135 /* prim vertex count will need TESS changes */
2136 pipeline->graphics.prim_vertex_count = prim_size_table[pipeline->graphics.prim];
Dave Airlieb640dfc2017-03-28 05:45:00 +10002137
2138 /* Ensure that some export memory is always allocated, for two reasons:
2139 *
2140 * 1) Correctness: The hardware ignores the EXEC mask if no export
2141 * memory is allocated, so KILL and alpha test do not work correctly
2142 * without this.
2143 * 2) Performance: Every shader needs at least a NULL export, even when
2144 * it writes no color/depth output. The NULL export instruction
2145 * stalls without this setting.
2146 *
2147 * Don't add this to CB_SHADER_MASK.
2148 */
Dave Airlie8996fdb2017-03-28 11:34:19 +10002149 struct radv_shader_variant *ps = pipeline->shaders[MESA_SHADER_FRAGMENT];
Dave Airlieb640dfc2017-03-28 05:45:00 +10002150 if (!pipeline->graphics.blend.spi_shader_col_format) {
Dave Airlieb640dfc2017-03-28 05:45:00 +10002151 if (!ps->info.fs.writes_z &&
2152 !ps->info.fs.writes_stencil &&
2153 !ps->info.fs.writes_sample_mask)
2154 pipeline->graphics.blend.spi_shader_col_format = V_028714_SPI_SHADER_32_R;
2155 }
Dave Airlie3360dbe2017-02-13 07:30:29 +00002156
Dave Airlie8996fdb2017-03-28 11:34:19 +10002157 unsigned z_order;
2158 pipeline->graphics.db_shader_control = 0;
2159 if (ps->info.fs.early_fragment_test || !ps->info.fs.writes_memory)
2160 z_order = V_02880C_EARLY_Z_THEN_LATE_Z;
2161 else
2162 z_order = V_02880C_LATE_Z;
2163
2164 pipeline->graphics.db_shader_control =
2165 S_02880C_Z_EXPORT_ENABLE(ps->info.fs.writes_z) |
2166 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.fs.writes_stencil) |
2167 S_02880C_KILL_ENABLE(!!ps->info.fs.can_discard) |
2168 S_02880C_MASK_EXPORT_ENABLE(ps->info.fs.writes_sample_mask) |
2169 S_02880C_Z_ORDER(z_order) |
2170 S_02880C_DEPTH_BEFORE_SHADER(ps->info.fs.early_fragment_test) |
2171 S_02880C_EXEC_ON_HIER_FAIL(ps->info.fs.writes_memory) |
2172 S_02880C_EXEC_ON_NOOP(ps->info.fs.writes_memory);
2173
Dave Airlie4b467c72017-03-28 11:34:46 +10002174 pipeline->graphics.shader_z_format =
2175 ps->info.fs.writes_sample_mask ? V_028710_SPI_SHADER_32_ABGR :
2176 ps->info.fs.writes_stencil ? V_028710_SPI_SHADER_32_GR :
2177 ps->info.fs.writes_z ? V_028710_SPI_SHADER_32_R :
2178 V_028710_SPI_SHADER_ZERO;
2179
Dave Airlie6a681702017-06-20 13:21:04 +10002180 calculate_vgt_gs_mode(pipeline);
Dave Airlie0232ea82017-03-28 11:48:38 +10002181 calculate_pa_cl_vs_out_cntl(pipeline);
Dave Airlie92e9c142017-03-28 11:43:48 +10002182 calculate_ps_inputs(pipeline);
Dave Airlie239a9222017-03-28 12:59:17 +10002183
Dave Airlie25a5ee32017-04-18 10:21:59 +10002184 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
2185 if (pipeline->shaders[i]) {
2186 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[i]->info.need_indirect_descriptor_sets;
2187 }
2188 }
2189
Dave Airlie239a9222017-03-28 12:59:17 +10002190 uint32_t stages = 0;
Dave Airlie4c60c682017-03-30 08:18:13 +01002191 if (radv_pipeline_has_tess(pipeline)) {
2192 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
2193 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
2194
2195 if (radv_pipeline_has_gs(pipeline))
2196 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
2197 S_028B54_GS_EN(1) |
2198 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
2199 else
2200 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
Dave Airlie5c8f8ca2017-06-06 09:05:12 +10002201
Dave Airlie4c60c682017-03-30 08:18:13 +01002202 } else if (radv_pipeline_has_gs(pipeline))
Dave Airlie239a9222017-03-28 12:59:17 +10002203 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
2204 S_028B54_GS_EN(1) |
2205 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
Dave Airlie5c8f8ca2017-06-06 09:05:12 +10002206
2207 if (device->physical_device->rad_info.chip_class >= GFX9)
2208 stages |= S_028B54_MAX_PRIMGRP_IN_WAVE(2);
2209
Dave Airlie239a9222017-03-28 12:59:17 +10002210 pipeline->graphics.vgt_shader_stages_en = stages;
2211
Dave Airlie4c60c682017-03-30 08:18:13 +01002212 if (radv_pipeline_has_gs(pipeline))
2213 calculate_gs_ring_sizes(pipeline);
2214
2215 if (radv_pipeline_has_tess(pipeline)) {
2216 if (pipeline->graphics.prim == V_008958_DI_PT_PATCH) {
2217 pipeline->graphics.prim_vertex_count.min = pCreateInfo->pTessellationState->patchControlPoints;
2218 pipeline->graphics.prim_vertex_count.incr = 1;
2219 }
2220 calculate_tess_state(pipeline, pCreateInfo);
2221 }
2222
Dave Airlief4e499e2016-10-07 09:16:09 +10002223 const VkPipelineVertexInputStateCreateInfo *vi_info =
2224 pCreateInfo->pVertexInputState;
2225 for (uint32_t i = 0; i < vi_info->vertexAttributeDescriptionCount; i++) {
2226 const VkVertexInputAttributeDescription *desc =
2227 &vi_info->pVertexAttributeDescriptions[i];
2228 unsigned loc = desc->location;
2229 const struct vk_format_description *format_desc;
2230 int first_non_void;
2231 uint32_t num_format, data_format;
2232 format_desc = vk_format_description(desc->format);
2233 first_non_void = vk_format_get_first_non_void_channel(desc->format);
2234
2235 num_format = radv_translate_buffer_numformat(format_desc, first_non_void);
2236 data_format = radv_translate_buffer_dataformat(format_desc, first_non_void);
2237
2238 pipeline->va_rsrc_word3[loc] = S_008F0C_DST_SEL_X(si_map_swizzle(format_desc->swizzle[0])) |
2239 S_008F0C_DST_SEL_Y(si_map_swizzle(format_desc->swizzle[1])) |
2240 S_008F0C_DST_SEL_Z(si_map_swizzle(format_desc->swizzle[2])) |
2241 S_008F0C_DST_SEL_W(si_map_swizzle(format_desc->swizzle[3])) |
2242 S_008F0C_NUM_FORMAT(num_format) |
2243 S_008F0C_DATA_FORMAT(data_format);
2244 pipeline->va_format_size[loc] = format_desc->block.bits / 8;
2245 pipeline->va_offset[loc] = desc->offset;
2246 pipeline->va_binding[loc] = desc->binding;
2247 pipeline->num_vertex_attribs = MAX2(pipeline->num_vertex_attribs, loc + 1);
2248 }
2249
2250 for (uint32_t i = 0; i < vi_info->vertexBindingDescriptionCount; i++) {
2251 const VkVertexInputBindingDescription *desc =
2252 &vi_info->pVertexBindingDescriptions[i];
2253
2254 pipeline->binding_stride[desc->binding] = desc->stride;
2255 }
2256
Dave Airlie734ea162017-06-07 09:04:30 +10002257 struct ac_userdata_info *loc = radv_lookup_user_sgpr(pipeline, MESA_SHADER_VERTEX,
2258 AC_UD_VS_BASE_VERTEX_START_INSTANCE);
2259 if (loc->sgpr_idx != -1) {
2260 pipeline->graphics.vtx_base_sgpr = radv_shader_stage_to_user_data_0(MESA_SHADER_VERTEX, radv_pipeline_has_gs(pipeline), radv_pipeline_has_tess(pipeline));
2261 pipeline->graphics.vtx_base_sgpr += loc->sgpr_idx * 4;
2262 if (pipeline->shaders[MESA_SHADER_VERTEX]->info.info.vs.needs_draw_id)
2263 pipeline->graphics.vtx_emit_num = 3;
2264 else
2265 pipeline->graphics.vtx_emit_num = 2;
2266 }
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +01002267 if (device->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) {
Dave Airlief395e342016-11-22 04:17:49 +00002268 radv_dump_pipeline_stats(device, pipeline);
2269 }
2270
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +01002271 result = radv_pipeline_scratch_init(device, pipeline);
2272 return result;
Dave Airlief4e499e2016-10-07 09:16:09 +10002273}
2274
2275VkResult
2276radv_graphics_pipeline_create(
2277 VkDevice _device,
2278 VkPipelineCache _cache,
2279 const VkGraphicsPipelineCreateInfo *pCreateInfo,
2280 const struct radv_graphics_pipeline_create_info *extra,
2281 const VkAllocationCallbacks *pAllocator,
2282 VkPipeline *pPipeline)
2283{
2284 RADV_FROM_HANDLE(radv_device, device, _device);
2285 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
2286 struct radv_pipeline *pipeline;
2287 VkResult result;
2288
Dave Airlie4450f402016-10-14 13:36:45 +10002289 pipeline = vk_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
Dave Airlief4e499e2016-10-07 09:16:09 +10002290 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2291 if (pipeline == NULL)
2292 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2293
2294 memset(pipeline, 0, sizeof(*pipeline));
2295 result = radv_pipeline_init(pipeline, device, cache,
2296 pCreateInfo, extra, pAllocator);
2297 if (result != VK_SUCCESS) {
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +01002298 radv_pipeline_destroy(device, pipeline, pAllocator);
Dave Airlief4e499e2016-10-07 09:16:09 +10002299 return result;
2300 }
2301
2302 *pPipeline = radv_pipeline_to_handle(pipeline);
2303
2304 return VK_SUCCESS;
2305}
2306
2307VkResult radv_CreateGraphicsPipelines(
2308 VkDevice _device,
2309 VkPipelineCache pipelineCache,
2310 uint32_t count,
2311 const VkGraphicsPipelineCreateInfo* pCreateInfos,
2312 const VkAllocationCallbacks* pAllocator,
2313 VkPipeline* pPipelines)
2314{
2315 VkResult result = VK_SUCCESS;
2316 unsigned i = 0;
2317
2318 for (; i < count; i++) {
Bas Nieuwenhuizenb2b4f722016-12-16 23:10:31 +01002319 VkResult r;
2320 r = radv_graphics_pipeline_create(_device,
2321 pipelineCache,
2322 &pCreateInfos[i],
2323 NULL, pAllocator, &pPipelines[i]);
2324 if (r != VK_SUCCESS) {
2325 result = r;
2326 pPipelines[i] = VK_NULL_HANDLE;
Dave Airlief4e499e2016-10-07 09:16:09 +10002327 }
2328 }
2329
Bas Nieuwenhuizenb2b4f722016-12-16 23:10:31 +01002330 return result;
Dave Airlief4e499e2016-10-07 09:16:09 +10002331}
2332
2333static VkResult radv_compute_pipeline_create(
2334 VkDevice _device,
2335 VkPipelineCache _cache,
2336 const VkComputePipelineCreateInfo* pCreateInfo,
2337 const VkAllocationCallbacks* pAllocator,
2338 VkPipeline* pPipeline)
2339{
2340 RADV_FROM_HANDLE(radv_device, device, _device);
2341 RADV_FROM_HANDLE(radv_pipeline_cache, cache, _cache);
2342 RADV_FROM_HANDLE(radv_shader_module, module, pCreateInfo->stage.module);
2343 struct radv_pipeline *pipeline;
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +01002344 VkResult result;
Dave Airlief4e499e2016-10-07 09:16:09 +10002345
Dave Airlie4450f402016-10-14 13:36:45 +10002346 pipeline = vk_alloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
Dave Airlief4e499e2016-10-07 09:16:09 +10002347 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2348 if (pipeline == NULL)
2349 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
2350
2351 memset(pipeline, 0, sizeof(*pipeline));
2352 pipeline->device = device;
2353 pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
2354
2355 pipeline->shaders[MESA_SHADER_COMPUTE] =
2356 radv_pipeline_compile(pipeline, cache, module,
2357 pCreateInfo->stage.pName,
2358 MESA_SHADER_COMPUTE,
2359 pCreateInfo->stage.pSpecializationInfo,
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +01002360 pipeline->layout, NULL);
Dave Airlief4e499e2016-10-07 09:16:09 +10002361
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +01002362
Dave Airlie25a5ee32017-04-18 10:21:59 +10002363 pipeline->need_indirect_descriptor_sets |= pipeline->shaders[MESA_SHADER_COMPUTE]->info.need_indirect_descriptor_sets;
Bas Nieuwenhuizenccff93e2017-01-29 15:20:03 +01002364 result = radv_pipeline_scratch_init(device, pipeline);
2365 if (result != VK_SUCCESS) {
2366 radv_pipeline_destroy(device, pipeline, pAllocator);
2367 return result;
2368 }
2369
Dave Airlief4e499e2016-10-07 09:16:09 +10002370 *pPipeline = radv_pipeline_to_handle(pipeline);
Dave Airlief395e342016-11-22 04:17:49 +00002371
Bas Nieuwenhuizen8bc39e22017-01-02 18:57:02 +01002372 if (device->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) {
Dave Airlief395e342016-11-22 04:17:49 +00002373 radv_dump_pipeline_stats(device, pipeline);
2374 }
Dave Airlief4e499e2016-10-07 09:16:09 +10002375 return VK_SUCCESS;
2376}
2377VkResult radv_CreateComputePipelines(
2378 VkDevice _device,
2379 VkPipelineCache pipelineCache,
2380 uint32_t count,
2381 const VkComputePipelineCreateInfo* pCreateInfos,
2382 const VkAllocationCallbacks* pAllocator,
2383 VkPipeline* pPipelines)
2384{
2385 VkResult result = VK_SUCCESS;
2386
2387 unsigned i = 0;
2388 for (; i < count; i++) {
Bas Nieuwenhuizenb2b4f722016-12-16 23:10:31 +01002389 VkResult r;
2390 r = radv_compute_pipeline_create(_device, pipelineCache,
2391 &pCreateInfos[i],
2392 pAllocator, &pPipelines[i]);
2393 if (r != VK_SUCCESS) {
2394 result = r;
2395 pPipelines[i] = VK_NULL_HANDLE;
Dave Airlief4e499e2016-10-07 09:16:09 +10002396 }
2397 }
2398
Bas Nieuwenhuizenb2b4f722016-12-16 23:10:31 +01002399 return result;
Dave Airlief4e499e2016-10-07 09:16:09 +10002400}