ac: add ac_choose_spi_color_formats() to common code
[mesa.git] / src / amd / vulkan / radv_pipeline.c
index ae584328ab4b7f88417b4b7783c6d0294f57ec77..f6365292f5185ac6fe8f4ef8d650013db99ef77c 100644 (file)
@@ -191,7 +191,9 @@ radv_pipeline_destroy(struct radv_device *device,
 
        if(pipeline->cs.buf)
                free(pipeline->cs.buf);
-       vk_free2(&device->alloc, allocator, pipeline);
+
+       vk_object_base_finish(&pipeline->base);
+       vk_free2(&device->vk.alloc, allocator, pipeline);
 }
 
 void radv_DestroyPipeline(
@@ -451,128 +453,29 @@ static bool is_dual_src(VkBlendFactor factor)
        }
 }
 
-static unsigned si_choose_spi_color_format(VkFormat vk_format,
-                                           bool blend_enable,
-                                           bool blend_need_alpha)
+static unsigned radv_choose_spi_color_format(VkFormat vk_format,
+                                            bool blend_enable,
+                                            bool blend_need_alpha)
 {
        const struct vk_format_description *desc = vk_format_description(vk_format);
+       struct ac_spi_color_formats formats = {};
        unsigned format, ntype, swap;
 
-       /* Alpha is needed for alpha-to-coverage.
-        * Blending may be with or without alpha.
-        */
-       unsigned normal = 0; /* most optimal, may not support blending or export alpha */
-       unsigned alpha = 0; /* exports alpha, but may not support blending */
-       unsigned blend = 0; /* supports blending, but may not export alpha */
-       unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
-
        format = radv_translate_colorformat(vk_format);
        ntype = radv_translate_color_numformat(vk_format, desc,
                                               vk_format_get_first_non_void_channel(vk_format));
        swap = radv_translate_colorswap(vk_format, false);
 
-       /* Choose the SPI color formats. These are required values for Stoney/RB+.
-        * Other chips have multiple choices, though they are not necessarily better.
-        */
-       switch (format) {
-       case V_028C70_COLOR_5_6_5:
-       case V_028C70_COLOR_1_5_5_5:
-       case V_028C70_COLOR_5_5_5_1:
-       case V_028C70_COLOR_4_4_4_4:
-       case V_028C70_COLOR_10_11_11:
-       case V_028C70_COLOR_11_11_10:
-       case V_028C70_COLOR_8:
-       case V_028C70_COLOR_8_8:
-       case V_028C70_COLOR_8_8_8_8:
-       case V_028C70_COLOR_10_10_10_2:
-       case V_028C70_COLOR_2_10_10_10:
-               if (ntype == V_028C70_NUMBER_UINT)
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
-               else if (ntype == V_028C70_NUMBER_SINT)
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
-               else
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
-               break;
-
-       case V_028C70_COLOR_16:
-       case V_028C70_COLOR_16_16:
-       case V_028C70_COLOR_16_16_16_16:
-               if (ntype == V_028C70_NUMBER_UNORM ||
-                   ntype == V_028C70_NUMBER_SNORM) {
-                       /* UNORM16 and SNORM16 don't support blending */
-                       if (ntype == V_028C70_NUMBER_UNORM)
-                               normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
-                       else
-                               normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
-
-                       /* Use 32 bits per channel for blending. */
-                       if (format == V_028C70_COLOR_16) {
-                               if (swap == V_028C70_SWAP_STD) { /* R */
-                                       blend = V_028714_SPI_SHADER_32_R;
-                                       blend_alpha = V_028714_SPI_SHADER_32_AR;
-                               } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
-                                       blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
-                               else
-                                       assert(0);
-                       } else if (format == V_028C70_COLOR_16_16) {
-                               if (swap == V_028C70_SWAP_STD) { /* RG */
-                                       blend = V_028714_SPI_SHADER_32_GR;
-                                       blend_alpha = V_028714_SPI_SHADER_32_ABGR;
-                               } else if (swap == V_028C70_SWAP_ALT) /* RA */
-                                       blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
-                               else
-                                       assert(0);
-                       } else /* 16_16_16_16 */
-                               blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
-               } else if (ntype == V_028C70_NUMBER_UINT)
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
-               else if (ntype == V_028C70_NUMBER_SINT)
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
-               else if (ntype == V_028C70_NUMBER_FLOAT)
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
-               else
-                       assert(0);
-               break;
-
-       case V_028C70_COLOR_32:
-               if (swap == V_028C70_SWAP_STD) { /* R */
-                       blend = normal = V_028714_SPI_SHADER_32_R;
-                       alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
-               } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
-               else
-                       assert(0);
-               break;
-
-       case V_028C70_COLOR_32_32:
-               if (swap == V_028C70_SWAP_STD) { /* RG */
-                       blend = normal = V_028714_SPI_SHADER_32_GR;
-                       alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
-               } else if (swap == V_028C70_SWAP_ALT) /* RA */
-                       alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
-               else
-                       assert(0);
-               break;
-
-       case V_028C70_COLOR_32_32_32_32:
-       case V_028C70_COLOR_8_24:
-       case V_028C70_COLOR_24_8:
-       case V_028C70_COLOR_X24_8_32_FLOAT:
-               alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
-               break;
-
-       default:
-               unreachable("unhandled blend format");
-       }
+       ac_choose_spi_color_formats(format, swap, ntype, false, &formats);
 
        if (blend_enable && blend_need_alpha)
-               return blend_alpha;
+               return formats.blend_alpha;
        else if(blend_need_alpha)
-               return alpha;
+               return formats.alpha;
        else if(blend_enable)
-               return blend;
+               return formats.blend;
        else
-               return normal;
+               return formats.normal;
 }
 
 static void
@@ -595,9 +498,9 @@ radv_pipeline_compute_spi_color_formats(struct radv_pipeline *pipeline,
                        bool blend_enable =
                                blend->blend_enable_4bit & (0xfu << (i * 4));
 
-                       cf = si_choose_spi_color_format(attachment->format,
-                                                       blend_enable,
-                                                       blend->need_src_alpha & (1 << i));
+                       cf = radv_choose_spi_color_format(attachment->format,
+                                                         blend_enable,
+                                                         blend->need_src_alpha & (1 << i));
                }
 
                col_format |= cf << (4 * i);
@@ -676,7 +579,7 @@ const VkFormat radv_fs_key_format_exemplars[NUM_META_FS_KEYS] = {
 
 unsigned radv_format_meta_fs_key(VkFormat format)
 {
-       unsigned col_format = si_choose_spi_color_format(format, false, false);
+       unsigned col_format = radv_choose_spi_color_format(format, false, false);
 
        assert(col_format != V_028714_SPI_SHADER_32_AR);
        if (col_format >= V_028714_SPI_SHADER_32_AR)
@@ -2228,9 +2131,9 @@ radv_link_shaders(struct radv_pipeline *pipeline, nir_shader **shaders)
                        radv_optimize_nir(ordered_shaders[i - 1], false, false);
 
                nir_remove_dead_variables(ordered_shaders[i],
-                                         nir_var_shader_out);
+                                         nir_var_shader_out, NULL);
                nir_remove_dead_variables(ordered_shaders[i - 1],
-                                         nir_var_shader_in);
+                                         nir_var_shader_in, NULL);
 
                bool progress = nir_remove_unused_varyings(ordered_shaders[i],
                                                           ordered_shaders[i - 1]);
@@ -2617,7 +2520,8 @@ radv_fill_shader_info(struct radv_pipeline *pipeline,
                radv_nir_shader_info_pass(nir[MESA_SHADER_FRAGMENT],
                                          pipeline->layout,
                                          &keys[MESA_SHADER_FRAGMENT],
-                                         &infos[MESA_SHADER_FRAGMENT]);
+                                         &infos[MESA_SHADER_FRAGMENT],
+                                         pipeline->device->physical_device->use_aco);
 
                /* TODO: These are no longer used as keys we should refactor this */
                keys[MESA_SHADER_VERTEX].vs_common_out.export_prim_id =
@@ -2668,7 +2572,8 @@ radv_fill_shader_info(struct radv_pipeline *pipeline,
                for (int i = 0; i < 2; i++) {
                        radv_nir_shader_info_pass(combined_nir[i],
                                                  pipeline->layout, &key,
-                                                 &infos[MESA_SHADER_TESS_CTRL]);
+                                                 &infos[MESA_SHADER_TESS_CTRL],
+                                                 pipeline->device->physical_device->use_aco);
                }
 
                keys[MESA_SHADER_TESS_EVAL].tes.num_patches =
@@ -2691,7 +2596,8 @@ radv_fill_shader_info(struct radv_pipeline *pipeline,
                        radv_nir_shader_info_pass(combined_nir[i],
                                                  pipeline->layout,
                                                  &keys[pre_stage],
-                                                 &infos[MESA_SHADER_GEOMETRY]);
+                                                 &infos[MESA_SHADER_GEOMETRY],
+                                                 pipeline->device->physical_device->use_aco);
                }
 
                filled_stages |= (1 << pre_stage);
@@ -2716,7 +2622,7 @@ radv_fill_shader_info(struct radv_pipeline *pipeline,
 
                radv_nir_shader_info_init(&infos[i]);
                radv_nir_shader_info_pass(nir[i], pipeline->layout,
-                                         &keys[i], &infos[i]);
+                                         &keys[i], &infos[i], pipeline->device->physical_device->use_aco);
        }
 
        for (int i = 0; i < MESA_SHADER_STAGES; i++) {
@@ -2809,14 +2715,14 @@ void radv_stop_feedback(VkPipelineCreationFeedbackEXT *feedback, bool cache_hit)
                           (cache_hit ? VK_PIPELINE_CREATION_FEEDBACK_APPLICATION_PIPELINE_CACHE_HIT_BIT_EXT : 0);
 }
 
-void radv_create_shaders(struct radv_pipeline *pipeline,
-                         struct radv_device *device,
-                         struct radv_pipeline_cache *cache,
-                         const struct radv_pipeline_key *key,
-                         const VkPipelineShaderStageCreateInfo **pStages,
-                         const VkPipelineCreateFlags flags,
-                         VkPipelineCreationFeedbackEXT *pipeline_feedback,
-                         VkPipelineCreationFeedbackEXT **stage_feedbacks)
+VkResult radv_create_shaders(struct radv_pipeline *pipeline,
+                             struct radv_device *device,
+                             struct radv_pipeline_cache *cache,
+                             const struct radv_pipeline_key *key,
+                             const VkPipelineShaderStageCreateInfo **pStages,
+                             const VkPipelineCreateFlags flags,
+                             VkPipelineCreationFeedbackEXT *pipeline_feedback,
+                             VkPipelineCreationFeedbackEXT **stage_feedbacks)
 {
        struct radv_shader_module fs_m = {0};
        struct radv_shader_module *modules[MESA_SHADER_STAGES] = { 0, };
@@ -2826,7 +2732,9 @@ void radv_create_shaders(struct radv_pipeline *pipeline,
        struct radv_shader_info infos[MESA_SHADER_STAGES] = {0};
        unsigned char hash[20], gs_copy_hash[20];
        bool keep_executable_info = (flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR) || device->keep_shader_info;
-       bool keep_statistic_info = (flags & VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR) || device->keep_shader_info;
+       bool keep_statistic_info = (flags & VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR) ||
+                                  (device->instance->debug_flags & RADV_DEBUG_DUMP_SHADER_STATS) ||
+                                  device->keep_shader_info;
 
        radv_start_feedback(pipeline_feedback);
 
@@ -2859,7 +2767,12 @@ void radv_create_shaders(struct radv_pipeline *pipeline,
                                                            &found_in_application_cache) &&
            (!modules[MESA_SHADER_GEOMETRY] || pipeline->gs_copy_shader)) {
                radv_stop_feedback(pipeline_feedback, found_in_application_cache);
-               return;
+               return VK_SUCCESS;
+       }
+
+       if (flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT_EXT) {
+               radv_stop_feedback(pipeline_feedback, found_in_application_cache);
+               return VK_PIPELINE_COMPILE_REQUIRED_EXT;
        }
 
        if (!modules[MESA_SHADER_FRAGMENT] && !modules[MESA_SHADER_COMPUTE]) {
@@ -2975,7 +2888,7 @@ void radv_create_shaders(struct radv_pipeline *pipeline,
 
                        radv_nir_shader_info_pass(nir[MESA_SHADER_GEOMETRY],
                                                  pipeline->layout, &key,
-                                                 &info);
+                                                 &info, pipeline->device->physical_device->use_aco);
                        info.wave_size = 64; /* Wave32 not supported. */
                        info.ballot_bit_size = 64;
 
@@ -3094,6 +3007,7 @@ void radv_create_shaders(struct radv_pipeline *pipeline,
                ralloc_free(fs_m.nir);
 
        radv_stop_feedback(pipeline_feedback, false);
+       return VK_SUCCESS;
 }
 
 static uint32_t
@@ -4479,12 +4393,18 @@ radv_compute_db_shader_control(const struct radv_device *device,
                               const struct radv_pipeline *pipeline,
                                const struct radv_shader_variant *ps)
 {
+       unsigned conservative_z_export = V_02880C_EXPORT_ANY_Z;
        unsigned z_order;
        if (ps->info.ps.early_fragment_test || !ps->info.ps.writes_memory)
                z_order = V_02880C_EARLY_Z_THEN_LATE_Z;
        else
                z_order = V_02880C_LATE_Z;
 
+       if (ps->info.ps.depth_layout == FRAG_DEPTH_LAYOUT_GREATER)
+               conservative_z_export = V_02880C_EXPORT_GREATER_THAN_Z;
+       else if (ps->info.ps.depth_layout == FRAG_DEPTH_LAYOUT_LESS)
+               conservative_z_export = V_02880C_EXPORT_LESS_THAN_Z;
+
        bool disable_rbplus = device->physical_device->rad_info.has_rbplus &&
                              !device->physical_device->rad_info.rbplus_allowed;
 
@@ -4498,6 +4418,7 @@ radv_compute_db_shader_control(const struct radv_device *device,
                S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(ps->info.ps.writes_stencil) |
                S_02880C_KILL_ENABLE(!!ps->info.ps.can_discard) |
                S_02880C_MASK_EXPORT_ENABLE(mask_export_enable) |
+               S_02880C_CONSERVATIVE_Z_EXPORT(conservative_z_export) |
                S_02880C_Z_ORDER(z_order) |
                S_02880C_DEPTH_BEFORE_SHADER(ps->info.ps.early_fragment_test) |
                S_02880C_PRE_SHADER_DEPTH_COVERAGE_ENABLE(ps->info.ps.post_depth_coverage) |
@@ -5133,7 +5054,11 @@ radv_pipeline_init(struct radv_pipeline *pipeline,
        if (radv_device_use_secure_compile(device->instance)) {
                return radv_secure_compile(pipeline, device, &key, pStages, pCreateInfo->flags, pCreateInfo->stageCount);
        } else {
-               radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags, pipeline_feedback, stage_feedbacks);
+               result = radv_create_shaders(pipeline, device, cache, &key, pStages,
+                                            pCreateInfo->flags, pipeline_feedback,
+                                            stage_feedbacks);
+               if (result != VK_SUCCESS)
+                       return result;
        }
 
        pipeline->graphics.spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1);
@@ -5257,11 +5182,14 @@ radv_graphics_pipeline_create(
        struct radv_pipeline *pipeline;
        VkResult result;
 
-       pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
+       pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
                              VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (pipeline == NULL)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &pipeline->base,
+                           VK_OBJECT_TYPE_PIPELINE);
+
        result = radv_pipeline_init(pipeline, device, cache,
                                    pCreateInfo, extra);
        if (result != VK_SUCCESS) {
@@ -5294,9 +5222,15 @@ VkResult radv_CreateGraphicsPipelines(
                if (r != VK_SUCCESS) {
                        result = r;
                        pPipelines[i] = VK_NULL_HANDLE;
+
+                       if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)
+                               break;
                }
        }
 
+       for (; i < count; ++i)
+               pPipelines[i] = VK_NULL_HANDLE;
+
        return result;
 }
 
@@ -5395,11 +5329,14 @@ static VkResult radv_compute_pipeline_create(
        struct radv_pipeline *pipeline;
        VkResult result;
 
-       pipeline = vk_zalloc2(&device->alloc, pAllocator, sizeof(*pipeline), 8,
+       pipeline = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*pipeline), 8,
                              VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (pipeline == NULL)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &pipeline->base,
+                           VK_OBJECT_TYPE_PIPELINE);
+
        pipeline->device = device;
        pipeline->layout = radv_pipeline_layout_from_handle(pCreateInfo->layout);
        assert(pipeline->layout);
@@ -5423,7 +5360,13 @@ static VkResult radv_compute_pipeline_create(
 
                return result;
        } else {
-               radv_create_shaders(pipeline, device, cache, &key, pStages, pCreateInfo->flags, pipeline_feedback, stage_feedbacks);
+               result = radv_create_shaders(pipeline, device, cache, &key, pStages,
+                                            pCreateInfo->flags, pipeline_feedback,
+                                            stage_feedbacks);
+               if (result != VK_SUCCESS) {
+                       radv_pipeline_destroy(device, pipeline, pAllocator);
+                       return result;
+               }
        }
 
        pipeline->user_data_0[MESA_SHADER_COMPUTE] = radv_pipeline_stage_to_user_data_0(pipeline, MESA_SHADER_COMPUTE, device->physical_device->rad_info.chip_class);
@@ -5460,9 +5403,15 @@ VkResult radv_CreateComputePipelines(
                if (r != VK_SUCCESS) {
                        result = r;
                        pPipelines[i] = VK_NULL_HANDLE;
+
+                       if (pCreateInfos[i].flags & VK_PIPELINE_CREATE_EARLY_RETURN_ON_FAILURE_BIT_EXT)
+                               break;
                }
        }
 
+       for (; i < count; ++i)
+               pPipelines[i] = VK_NULL_HANDLE;
+
        return result;
 }