radv: fix possible stack corruption
[mesa.git] / src / amd / vulkan / radv_device.c
index 5c48be1d11ad25aa3c20956080d649cae5ad37a9..9e66faf7ab89875655aa5c9a2846670124bb7c94 100644 (file)
@@ -91,10 +91,22 @@ static const VkExtensionProperties instance_extensions[] = {
 };
 
 static const VkExtensionProperties common_device_extensions[] = {
+       {
+               .extensionName = VK_KHR_DESCRIPTOR_UPDATE_TEMPLATE_EXTENSION_NAME,
+               .specVersion = 1,
+       },
+       {
+               .extensionName = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
+               .specVersion = 1,
+       },
        {
                .extensionName = VK_KHR_MAINTENANCE1_EXTENSION_NAME,
                .specVersion = 1,
        },
+       {
+               .extensionName = VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
+               .specVersion = 1,
+       },
        {
                .extensionName = VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
                .specVersion = 1,
@@ -389,7 +401,7 @@ radv_enumerate_devices(struct radv_instance *instance)
 
        instance->physicalDeviceCount = 0;
 
-       max_devices = drmGetDevices2(0, devices, sizeof(devices));
+       max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
        if (max_devices < 1)
                return VK_ERROR_INCOMPATIBLE_DRIVER;
 
@@ -471,7 +483,7 @@ void radv_GetPhysicalDeviceFeatures(
                .textureCompressionASTC_LDR               = false,
                .textureCompressionBC                     = true,
                .occlusionQueryPrecise                    = true,
-               .pipelineStatisticsQuery                  = false,
+               .pipelineStatisticsQuery                  = true,
                .vertexPipelineStoresAndAtomics           = true,
                .fragmentStoresAndAtomics                 = true,
                .shaderTessellationAndGeometryPointSize   = true,
@@ -490,8 +502,8 @@ void radv_GetPhysicalDeviceFeatures(
                .shaderInt64                              = false,
                .shaderInt16                              = false,
                .sparseBinding                            = true,
-               .variableMultisampleRate                  = false,
-               .inheritedQueries                         = false,
+               .variableMultisampleRate                  = true,
+               .inheritedQueries                         = true,
        };
 }
 
@@ -530,6 +542,20 @@ void radv_GetPhysicalDeviceProperties(
 {
        RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
        VkSampleCountFlags sample_counts = 0xf;
+
+       /* make sure that the entire descriptor set is addressable with a signed
+        * 32-bit int. So the sum of all limits scaled by descriptor size has to
+        * be at most 2 GiB. the combined image & samples object count as one of
+        * both. This limit is for the pipeline layout, not for the set layout, but
+        * there is no set limit, so we just set a pipeline limit. I don't think
+        * any app is going to hit this soon. */
+       size_t max_descriptor_set_size = ((1ull << 31) - 16 * MAX_DYNAMIC_BUFFERS) /
+                 (32 /* uniform buffer, 32 due to potential space wasted on alignement */ +
+                  32 /* storage buffer, 32 due to potential space wasted on alignement */ +
+                  32 /* sampler, largest when combined with image */ +
+                  64 /* sampled image */ +
+                  64 /* storage image */);
+
        VkPhysicalDeviceLimits limits = {
                .maxImageDimension1D                      = (1 << 14),
                .maxImageDimension2D                      = (1 << 14),
@@ -545,21 +571,21 @@ void radv_GetPhysicalDeviceProperties(
                .bufferImageGranularity                   = 64, /* A cache line */
                .sparseAddressSpaceSize                   = 0xffffffffu, /* buffer max size */
                .maxBoundDescriptorSets                   = MAX_SETS,
-               .maxPerStageDescriptorSamplers            = 64,
-               .maxPerStageDescriptorUniformBuffers      = 64,
-               .maxPerStageDescriptorStorageBuffers      = 64,
-               .maxPerStageDescriptorSampledImages       = 64,
-               .maxPerStageDescriptorStorageImages       = 64,
-               .maxPerStageDescriptorInputAttachments    = 64,
-               .maxPerStageResources                     = 128,
-               .maxDescriptorSetSamplers                 = 256,
-               .maxDescriptorSetUniformBuffers           = 256,
-               .maxDescriptorSetUniformBuffersDynamic    = 256,
-               .maxDescriptorSetStorageBuffers           = 256,
-               .maxDescriptorSetStorageBuffersDynamic    = 256,
-               .maxDescriptorSetSampledImages            = 256,
-               .maxDescriptorSetStorageImages            = 256,
-               .maxDescriptorSetInputAttachments         = 256,
+               .maxPerStageDescriptorSamplers            = max_descriptor_set_size,
+               .maxPerStageDescriptorUniformBuffers      = max_descriptor_set_size,
+               .maxPerStageDescriptorStorageBuffers      = max_descriptor_set_size,
+               .maxPerStageDescriptorSampledImages       = max_descriptor_set_size,
+               .maxPerStageDescriptorStorageImages       = max_descriptor_set_size,
+               .maxPerStageDescriptorInputAttachments    = max_descriptor_set_size,
+               .maxPerStageResources                     = max_descriptor_set_size,
+               .maxDescriptorSetSamplers                 = max_descriptor_set_size,
+               .maxDescriptorSetUniformBuffers           = max_descriptor_set_size,
+               .maxDescriptorSetUniformBuffersDynamic    = MAX_DYNAMIC_BUFFERS / 2,
+               .maxDescriptorSetStorageBuffers           = max_descriptor_set_size,
+               .maxDescriptorSetStorageBuffersDynamic    = MAX_DYNAMIC_BUFFERS / 2,
+               .maxDescriptorSetSampledImages            = max_descriptor_set_size,
+               .maxDescriptorSetStorageImages            = max_descriptor_set_size,
+               .maxDescriptorSetInputAttachments         = max_descriptor_set_size,
                .maxVertexInputAttributes                 = 32,
                .maxVertexInputBindings                   = 32,
                .maxVertexInputAttributeOffset            = 2047,
@@ -573,7 +599,7 @@ void radv_GetPhysicalDeviceProperties(
                .maxTessellationControlTotalOutputComponents = 4096,
                .maxTessellationEvaluationInputComponents = 128,
                .maxTessellationEvaluationOutputComponents = 128,
-               .maxGeometryShaderInvocations             = 32,
+               .maxGeometryShaderInvocations             = 127,
                .maxGeometryInputComponents               = 64,
                .maxGeometryOutputComponents              = 128,
                .maxGeometryOutputVertices                = 256,
@@ -626,8 +652,8 @@ void radv_GetPhysicalDeviceProperties(
                .sampledImageStencilSampleCounts          = sample_counts,
                .storageImageSampleCounts                 = VK_SAMPLE_COUNT_1_BIT,
                .maxSampleMaskWords                       = 1,
-               .timestampComputeAndGraphics              = false,
-               .timestampPeriod                          = 100000.0 / pdevice->rad_info.clock_crystal_freq,
+               .timestampComputeAndGraphics              = true,
+               .timestampPeriod                          = 1000000.0 / pdevice->rad_info.clock_crystal_freq,
                .maxClipDistances                         = 8,
                .maxCullDistances                         = 8,
                .maxCombinedClipAndCullDistances          = 8,
@@ -650,7 +676,7 @@ void radv_GetPhysicalDeviceProperties(
                .deviceID = pdevice->rad_info.pci_id,
                .deviceType = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU,
                .limits = limits,
-               .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
+               .sparseProperties = {0},
        };
 
        strcpy(pProperties->deviceName, pdevice->name);
@@ -661,7 +687,20 @@ void radv_GetPhysicalDeviceProperties2KHR(
        VkPhysicalDevice                            physicalDevice,
        VkPhysicalDeviceProperties2KHR             *pProperties)
 {
-       return radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
+       radv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
+
+       vk_foreach_struct(ext, pProperties->pNext) {
+               switch (ext->sType) {
+               case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
+                       VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
+                               (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
+                       properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
+                       break;
+               }
+               default:
+                       break;
+               }
+       }
 }
 
 static void radv_get_physical_device_queue_family_properties(
@@ -1007,6 +1046,22 @@ VkResult radv_CreateDevice(
                        break;
                }
                device->ws->cs_finalize(device->flush_cs[family]);
+
+               device->flush_shader_cs[family] = device->ws->cs_create(device->ws, family);
+               switch (family) {
+               case RADV_QUEUE_GENERAL:
+               case RADV_QUEUE_COMPUTE:
+                       si_cs_emit_cache_flush(device->flush_shader_cs[family],
+                                              device->physical_device->rad_info.chip_class,
+                                              family == RADV_QUEUE_COMPUTE && device->physical_device->rad_info.chip_class >= CIK,
+                                              family == RADV_QUEUE_COMPUTE ? RADV_CMD_FLAG_CS_PARTIAL_FLUSH : (RADV_CMD_FLAG_CS_PARTIAL_FLUSH | RADV_CMD_FLAG_PS_PARTIAL_FLUSH) |
+                                              RADV_CMD_FLAG_INV_ICACHE |
+                                              RADV_CMD_FLAG_INV_SMEM_L1 |
+                                              RADV_CMD_FLAG_INV_VMEM_L1 |
+                                              RADV_CMD_FLAG_INV_GLOBAL_L2);
+                       break;
+               }
+               device->ws->cs_finalize(device->flush_shader_cs[family]);
        }
 
        if (getenv("RADV_TRACE_FILE")) {
@@ -1082,6 +1137,8 @@ void radv_DestroyDevice(
                        device->ws->cs_destroy(device->empty_cs[i]);
                if (device->flush_cs[i])
                        device->ws->cs_destroy(device->flush_cs[i]);
+               if (device->flush_shader_cs[i])
+                       device->ws->cs_destroy(device->flush_shader_cs[i]);
        }
        radv_device_finish_meta(device);
 
@@ -1188,6 +1245,7 @@ static void radv_dump_trace(struct radv_device *device,
 static void
 fill_geom_tess_rings(struct radv_queue *queue,
                     uint32_t *map,
+                    bool add_sample_positions,
                     uint32_t esgs_ring_size,
                     struct radeon_winsys_bo *esgs_ring_bo,
                     uint32_t gsvs_ring_size,
@@ -1315,6 +1373,18 @@ fill_geom_tess_rings(struct radv_queue *queue,
                S_008F0C_ELEMENT_SIZE(0) |
                S_008F0C_INDEX_STRIDE(0) |
                S_008F0C_ADD_TID_ENABLE(false);
+       desc += 4;
+
+       /* add sample positions after all rings */
+       memcpy(desc, queue->device->sample_locations_1x, 8);
+       desc += 2;
+       memcpy(desc, queue->device->sample_locations_2x, 16);
+       desc += 4;
+       memcpy(desc, queue->device->sample_locations_4x, 32);
+       desc += 8;
+       memcpy(desc, queue->device->sample_locations_8x, 64);
+       desc += 16;
+       memcpy(desc, queue->device->sample_locations_16x, 128);
 }
 
 static unsigned
@@ -1374,6 +1444,7 @@ radv_get_preamble_cs(struct radv_queue *queue,
                     uint32_t esgs_ring_size,
                     uint32_t gsvs_ring_size,
                     bool needs_tess_rings,
+                    bool needs_sample_positions,
                      struct radeon_winsys_cs **initial_preamble_cs,
                      struct radeon_winsys_cs **continue_preamble_cs)
 {
@@ -1385,7 +1456,7 @@ radv_get_preamble_cs(struct radv_queue *queue,
        struct radeon_winsys_bo *tess_factor_ring_bo = NULL;
        struct radeon_winsys_bo *tess_offchip_ring_bo = NULL;
        struct radeon_winsys_cs *dest_cs[2] = {0};
-       bool add_tess_rings = false;
+       bool add_tess_rings = false, add_sample_positions = false;
        unsigned tess_factor_ring_size = 0, tess_offchip_ring_size = 0;
        unsigned max_offchip_buffers;
        unsigned hs_offchip_param = 0;
@@ -1393,6 +1464,10 @@ radv_get_preamble_cs(struct radv_queue *queue,
                if (needs_tess_rings)
                        add_tess_rings = true;
        }
+       if (!queue->has_sample_positions) {
+               if (needs_sample_positions)
+                       add_sample_positions = true;
+       }
        tess_factor_ring_size = 32768 * queue->device->physical_device->rad_info.max_se;
        hs_offchip_param = radv_get_hs_offchip_param(queue->device,
                                                     &max_offchip_buffers);
@@ -1403,7 +1478,7 @@ radv_get_preamble_cs(struct radv_queue *queue,
            compute_scratch_size <= queue->compute_scratch_size &&
            esgs_ring_size <= queue->esgs_ring_size &&
            gsvs_ring_size <= queue->gsvs_ring_size &&
-           !add_tess_rings &&
+           !add_tess_rings && !add_sample_positions &&
            queue->initial_preamble_cs) {
                *initial_preamble_cs = queue->initial_preamble_cs;
                *continue_preamble_cs = queue->continue_preamble_cs;
@@ -1485,11 +1560,14 @@ radv_get_preamble_cs(struct radv_queue *queue,
            esgs_ring_bo != queue->esgs_ring_bo ||
            gsvs_ring_bo != queue->gsvs_ring_bo ||
            tess_factor_ring_bo != queue->tess_factor_ring_bo ||
-           tess_offchip_ring_bo != queue->tess_offchip_ring_bo) {
+           tess_offchip_ring_bo != queue->tess_offchip_ring_bo || add_sample_positions) {
                uint32_t size = 0;
                if (gsvs_ring_bo || esgs_ring_bo ||
-                   tess_factor_ring_bo || tess_offchip_ring_bo)
+                   tess_factor_ring_bo || tess_offchip_ring_bo || add_sample_positions) {
                        size = 112; /* 2 dword + 2 padding + 4 dword * 6 */
+                       if (add_sample_positions)
+                               size += 256; /* 32+16+8+4+2+1 samples * 4 * 2 = 248 bytes. */
+               }
                else if (scratch_bo)
                        size = 8; /* 2 dword */
 
@@ -1541,8 +1619,9 @@ radv_get_preamble_cs(struct radv_queue *queue,
                                map[1] = rsrc1;
                        }
 
-                       if (esgs_ring_bo || gsvs_ring_bo || tess_factor_ring_bo || tess_offchip_ring_bo)
-                               fill_geom_tess_rings(queue, map,
+                       if (esgs_ring_bo || gsvs_ring_bo || tess_factor_ring_bo || tess_offchip_ring_bo ||
+                           add_sample_positions)
+                               fill_geom_tess_rings(queue, map, add_sample_positions,
                                                     esgs_ring_size, esgs_ring_bo,
                                                     gsvs_ring_size, gsvs_ring_bo,
                                                     tess_factor_ring_size, tess_factor_ring_bo,
@@ -1685,6 +1764,9 @@ radv_get_preamble_cs(struct radv_queue *queue,
                queue->descriptor_bo = descriptor_bo;
        }
 
+       if (add_sample_positions)
+               queue->has_sample_positions = true;
+
        *initial_preamble_cs = queue->initial_preamble_cs;
        *continue_preamble_cs = queue->continue_preamble_cs;
        if (!scratch_size && !compute_scratch_size && !esgs_ring_size && !gsvs_ring_size)
@@ -1730,6 +1812,7 @@ VkResult radv_QueueSubmit(
        VkResult result;
        bool fence_emitted = false;
        bool tess_rings_needed = false;
+       bool sample_positions_needed = false;
 
        /* Do this first so failing to allocate scratch buffers can't result in
         * partially executed submissions. */
@@ -1744,18 +1827,20 @@ VkResult radv_QueueSubmit(
                        esgs_ring_size = MAX2(esgs_ring_size, cmd_buffer->esgs_ring_size_needed);
                        gsvs_ring_size = MAX2(gsvs_ring_size, cmd_buffer->gsvs_ring_size_needed);
                        tess_rings_needed |= cmd_buffer->tess_rings_needed;
+                       sample_positions_needed |= cmd_buffer->sample_positions_needed;
                }
        }
 
        result = radv_get_preamble_cs(queue, scratch_size, compute_scratch_size,
                                      esgs_ring_size, gsvs_ring_size, tess_rings_needed,
+                                     sample_positions_needed,
                                      &initial_preamble_cs, &continue_preamble_cs);
        if (result != VK_SUCCESS)
                return result;
 
        for (uint32_t i = 0; i < submitCount; i++) {
                struct radeon_winsys_cs **cs_array;
-               bool do_flush = !i;
+               bool do_flush = !i || pSubmits[i].pWaitDstStageMask;
                bool can_patch = !do_flush;
                uint32_t advance;
 
@@ -1782,7 +1867,9 @@ VkResult radv_QueueSubmit(
                                                (pSubmits[i].commandBufferCount + do_flush));
 
                if(do_flush)
-                       cs_array[0] = queue->device->flush_cs[queue->queue_family_index];
+                       cs_array[0] = pSubmits[i].waitSemaphoreCount ?
+                               queue->device->flush_shader_cs[queue->queue_family_index] :
+                               queue->device->flush_cs[queue->queue_family_index];
 
                for (uint32_t j = 0; j < pSubmits[i].commandBufferCount; j++) {
                        RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer,
@@ -2573,8 +2660,8 @@ radv_initialise_color_surface(struct radv_device *device,
        cb->cb_color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == VK_SWIZZLE_1) |
                S_028C74_TILE_MODE_INDEX(tile_mode_index);
 
-       if (iview->image->samples > 1) {
-               unsigned log_samples = util_logbase2(iview->image->samples);
+       if (iview->image->info.samples > 1) {
+               unsigned log_samples = util_logbase2(iview->image->info.samples);
 
                cb->cb_color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
                        S_028C74_NUM_FRAGMENTS(log_samples);
@@ -2638,7 +2725,7 @@ radv_initialise_color_surface(struct radv_device *device,
                                    format != V_028C70_COLOR_24_8) |
                S_028C70_NUMBER_TYPE(ntype) |
                S_028C70_ENDIAN(endian);
-       if (iview->image->samples > 1)
+       if (iview->image->info.samples > 1)
                if (iview->image->fmask.size)
                        cb->cb_color_info |= S_028C70_COMPRESSION(1);
 
@@ -2651,7 +2738,7 @@ radv_initialise_color_surface(struct radv_device *device,
 
        if (device->physical_device->rad_info.chip_class >= VI) {
                unsigned max_uncompressed_block_size = 2;
-               if (iview->image->samples > 1) {
+               if (iview->image->info.samples > 1) {
                        if (iview->image->surface.bpe == 1)
                                max_uncompressed_block_size = 0;
                        else if (iview->image->surface.bpe == 2)
@@ -2679,6 +2766,7 @@ radv_initialise_ds_surface(struct radv_device *device,
        unsigned format;
        uint64_t va, s_offs, z_offs;
        const struct radeon_surf_level *level_info = &iview->image->surface.level[level];
+       bool stencil_only = false;
        memset(ds, 0, sizeof(*ds));
        switch (iview->vk_format) {
        case VK_FORMAT_D24_UNORM_S8_UINT:
@@ -2697,6 +2785,10 @@ radv_initialise_ds_surface(struct radv_device *device,
                        S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
                ds->offset_scale = 1.0f;
                break;
+       case VK_FORMAT_S8_UINT:
+               stencil_only = true;
+               level_info = &iview->image->surface.stencil_level[level];
+               break;
        default:
                break;
        }
@@ -2714,8 +2806,8 @@ radv_initialise_ds_surface(struct radv_device *device,
        ds->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(1);
        ds->db_z_info = S_028040_FORMAT(format) | S_028040_ZRANGE_PRECISION(1);
 
-       if (iview->image->samples > 1)
-               ds->db_z_info |= S_028040_NUM_SAMPLES(util_logbase2(iview->image->samples));
+       if (iview->image->info.samples > 1)
+               ds->db_z_info |= S_028040_NUM_SAMPLES(util_logbase2(iview->image->info.samples));
 
        if (iview->image->surface.flags & RADEON_SURF_SBUFFER)
                ds->db_stencil_info = S_028044_FORMAT(V_028044_STENCIL_8);
@@ -2731,6 +2823,9 @@ radv_initialise_ds_surface(struct radv_device *device,
                unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
                unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
 
+               if (stencil_only)
+                       tile_mode = stencil_tile_mode;
+
                ds->db_depth_info |=
                        S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
                        S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
@@ -2763,7 +2858,7 @@ radv_initialise_ds_surface(struct radv_device *device,
                         * Check piglit's arb_texture_multisample-stencil-clear
                         * test if you want to try changing this.
                         */
-                       if (iview->image->samples <= 1)
+                       if (iview->image->info.samples <= 1)
                                ds->db_stencil_info |= S_028044_ALLOW_EXPCLEAR(1);
                } else
                        /* Use all of the htile_buffer for depth if there's no stencil. */