Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 1 | // VK tests |
| 2 | // |
Nathaniel Cesario | 022e7b2 | 2021-04-06 00:06:17 -0600 | [diff] [blame] | 3 | // Copyright (c) 2015-2021 The Khronos Group Inc. |
| 4 | // Copyright (c) 2015-2021 Valve Corporation |
| 5 | // Copyright (c) 2015-2021 LunarG, Inc. |
| 6 | // Copyright (c) 2015-2021 Google, Inc. |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 7 | // |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 8 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | // you may not use this file except in compliance with the License. |
| 10 | // You may obtain a copy of the License at |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 11 | // |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 12 | // http://www.apache.org/licenses/LICENSE-2.0 |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 13 | // |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 14 | // Unless required by applicable law or agreed to in writing, software |
| 15 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | // See the License for the specific language governing permissions and |
| 18 | // limitations under the License. |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 19 | |
| 20 | #include "vktestframeworkandroid.h" |
Nathaniel Cesario | e1f91ba | 2021-08-19 14:06:11 -0600 | [diff] [blame] | 21 | #include "vkrenderframework.h" |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 22 | #include "shaderc/shaderc.hpp" |
| 23 | #include <android/log.h> |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 24 | |
| 25 | VkTestFramework::VkTestFramework() {} |
| 26 | VkTestFramework::~VkTestFramework() {} |
| 27 | |
Tobin Ehlis | 7288864 | 2017-11-15 09:43:56 -0700 | [diff] [blame] | 28 | // Define static elements |
| 29 | bool VkTestFramework::m_devsim_layer = false; |
Nathaniel Cesario | 022e7b2 | 2021-04-06 00:06:17 -0600 | [diff] [blame] | 30 | int VkTestFramework::m_phys_device_index = -1; |
Locke | 5ddafd9 | 2019-03-20 10:11:33 -0600 | [diff] [blame] | 31 | ANativeWindow *VkTestFramework::window = nullptr; |
Tobin Ehlis | 7288864 | 2017-11-15 09:43:56 -0700 | [diff] [blame] | 32 | |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 33 | VkFormat VkTestFramework::GetFormat(VkInstance instance, vk_testing::Device *device) { |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 34 | VkFormatProperties format_props; |
Mark Lobodzinski | 1ad400e | 2019-09-27 13:53:29 -0600 | [diff] [blame] | 35 | vk::GetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_B8G8R8A8_UNORM, &format_props); |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 36 | if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT || |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 37 | format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 38 | return VK_FORMAT_B8G8R8A8_UNORM; |
| 39 | } |
Mark Lobodzinski | 1ad400e | 2019-09-27 13:53:29 -0600 | [diff] [blame] | 40 | vk::GetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_R8G8B8A8_UNORM, &format_props); |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 41 | if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT || |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 42 | format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 43 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 44 | } |
| 45 | printf("Error - device does not support VK_FORMAT_B8G8R8A8_UNORM nor VK_FORMAT_R8G8B8A8_UNORM - exiting\n"); |
| 46 | exit(0); |
| 47 | } |
| 48 | |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 49 | void VkTestFramework::InitArgs(int *argc, char *argv[]) {} |
| 50 | void VkTestFramework::Finish() {} |
| 51 | |
Mark Lobodzinski | 91ee16c | 2019-09-27 11:44:52 -0600 | [diff] [blame] | 52 | void TestEnvironment::SetUp() { |
| 53 | vk_testing::set_error_callback(test_error_callback); |
| 54 | |
| 55 | vk::InitDispatchTable(); |
| 56 | } |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 57 | |
| 58 | void TestEnvironment::TearDown() {} |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 59 | |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 60 | // Android specific helper functions for shaderc. |
| 61 | struct shader_type_mapping { |
| 62 | VkShaderStageFlagBits vkshader_type; |
| 63 | shaderc_shader_kind shaderc_type; |
| 64 | }; |
| 65 | |
| 66 | static const shader_type_mapping shader_map_table[] = { |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 67 | {VK_SHADER_STAGE_VERTEX_BIT, shaderc_glsl_vertex_shader}, |
| 68 | {VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, shaderc_glsl_tess_control_shader}, |
| 69 | {VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, shaderc_glsl_tess_evaluation_shader}, |
| 70 | {VK_SHADER_STAGE_GEOMETRY_BIT, shaderc_glsl_geometry_shader}, |
| 71 | {VK_SHADER_STAGE_FRAGMENT_BIT, shaderc_glsl_fragment_shader}, |
| 72 | {VK_SHADER_STAGE_COMPUTE_BIT, shaderc_glsl_compute_shader}, |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | shaderc_shader_kind MapShadercType(VkShaderStageFlagBits vkShader) { |
| 76 | for (auto shader : shader_map_table) { |
| 77 | if (shader.vkshader_type == vkShader) { |
| 78 | return shader.shaderc_type; |
| 79 | } |
| 80 | } |
| 81 | assert(false); |
| 82 | return shaderc_glsl_infer_from_source; |
| 83 | } |
| 84 | |
| 85 | // Compile a given string containing GLSL into SPIR-V |
| 86 | // Return value of false means an error was encountered |
Mark Lobodzinski | f000344 | 2019-10-28 16:05:32 -0600 | [diff] [blame] | 87 | bool VkTestFramework::GLSLtoSPV(VkPhysicalDeviceLimits const *const device_limits, const VkShaderStageFlagBits shader_type, |
Nathaniel Cesario | e1f91ba | 2021-08-19 14:06:11 -0600 | [diff] [blame] | 88 | const char *pshader, std::vector<unsigned int> &spirv, bool debug, const spv_target_env spv_env) { |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 89 | // On Android, use shaderc instead. |
| 90 | shaderc::Compiler compiler; |
Karl Schultz | 6c009e5 | 2019-01-14 19:21:05 -0700 | [diff] [blame] | 91 | shaderc::CompileOptions options; |
| 92 | if (debug) { |
| 93 | options.SetOptimizationLevel(shaderc_optimization_level_zero); |
| 94 | options.SetGenerateDebugInfo(); |
| 95 | } |
Jeff Bolz | 49d6498 | 2019-09-18 13:18:14 -0500 | [diff] [blame] | 96 | |
Nathaniel Cesario | e1f91ba | 2021-08-19 14:06:11 -0600 | [diff] [blame] | 97 | switch (VkShaderObj::FromSpvTargetEnv(spv_env)) { |
Jeff Bolz | 49d6498 | 2019-09-18 13:18:14 -0500 | [diff] [blame] | 98 | default: |
| 99 | case 0: |
| 100 | options.SetTargetSpirv(shaderc_spirv_version_1_0); |
| 101 | break; |
| 102 | case 1: |
| 103 | options.SetTargetSpirv(shaderc_spirv_version_1_1); |
| 104 | break; |
| 105 | case 2: |
| 106 | options.SetTargetSpirv(shaderc_spirv_version_1_2); |
| 107 | break; |
| 108 | case 3: |
| 109 | options.SetTargetSpirv(shaderc_spirv_version_1_3); |
| 110 | break; |
| 111 | case 4: |
| 112 | options.SetTargetSpirv(shaderc_spirv_version_1_4); |
| 113 | break; |
| 114 | } |
| 115 | |
Mark Lobodzinski | f000344 | 2019-10-28 16:05:32 -0600 | [diff] [blame] | 116 | // Override glslang built-in GL settings with actual hardware values |
| 117 | options.SetLimit(shaderc_limit_max_clip_distances, device_limits->maxClipDistances); |
| 118 | options.SetLimit(shaderc_limit_max_compute_work_group_count_x, device_limits->maxComputeWorkGroupCount[0]); |
| 119 | options.SetLimit(shaderc_limit_max_compute_work_group_count_y, device_limits->maxComputeWorkGroupCount[1]); |
| 120 | options.SetLimit(shaderc_limit_max_compute_work_group_count_z, device_limits->maxComputeWorkGroupCount[2]); |
| 121 | options.SetLimit(shaderc_limit_max_compute_work_group_size_x, device_limits->maxComputeWorkGroupSize[0]); |
| 122 | options.SetLimit(shaderc_limit_max_compute_work_group_size_y, device_limits->maxComputeWorkGroupSize[1]); |
| 123 | options.SetLimit(shaderc_limit_max_compute_work_group_size_z, device_limits->maxComputeWorkGroupSize[2]); |
| 124 | options.SetLimit(shaderc_limit_max_cull_distances, device_limits->maxCullDistances); |
| 125 | options.SetLimit(shaderc_limit_max_fragment_input_components, device_limits->maxFragmentInputComponents); |
| 126 | options.SetLimit(shaderc_limit_max_geometry_input_components, device_limits->maxGeometryInputComponents); |
| 127 | options.SetLimit(shaderc_limit_max_geometry_output_components, device_limits->maxGeometryOutputComponents); |
| 128 | options.SetLimit(shaderc_limit_max_geometry_output_vertices, device_limits->maxGeometryOutputVertices); |
| 129 | options.SetLimit(shaderc_limit_max_geometry_total_output_components, device_limits->maxGeometryTotalOutputComponents); |
| 130 | options.SetLimit(shaderc_limit_max_vertex_output_components, device_limits->maxVertexOutputComponents); |
| 131 | options.SetLimit(shaderc_limit_max_viewports, device_limits->maxViewports); |
| 132 | |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 133 | shaderc::SpvCompilationResult result = |
Karl Schultz | 6c009e5 | 2019-01-14 19:21:05 -0700 | [diff] [blame] | 134 | compiler.CompileGlslToSpv(pshader, strlen(pshader), MapShadercType(shader_type), "shader", options); |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 135 | if (result.GetCompilationStatus() != shaderc_compilation_status_success) { |
Mark Lobodzinski | 4e1a2f3 | 2019-10-23 11:17:41 -0600 | [diff] [blame] | 136 | __android_log_print(ANDROID_LOG_ERROR, "VulkanLayerValidationTests", "GLSLtoSPV compilation failed: %s", |
Cody Northrop | 1013310 | 2018-09-25 09:02:03 -0600 | [diff] [blame] | 137 | result.GetErrorMessage().c_str()); |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
| 141 | for (auto iter = result.begin(); iter != result.end(); iter++) { |
| 142 | spirv.push_back(*iter); |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
Karl Schultz | 2370762 | 2018-08-22 10:20:33 -0600 | [diff] [blame] | 147 | |
| 148 | // |
| 149 | // Compile a given string containing SPIR-V assembly into SPV for use by VK |
| 150 | // Return value of false means an error was encountered. |
| 151 | // |
| 152 | bool VkTestFramework::ASMtoSPV(const spv_target_env target_env, const uint32_t options, const char *pasm, |
| 153 | std::vector<unsigned int> &spv) { |
| 154 | spv_binary binary; |
| 155 | spv_diagnostic diagnostic = nullptr; |
| 156 | spv_context context = spvContextCreate(target_env); |
| 157 | spv_result_t error = spvTextToBinaryWithOptions(context, pasm, strlen(pasm), options, &binary, &diagnostic); |
| 158 | spvContextDestroy(context); |
| 159 | if (error) { |
| 160 | __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTest", "ASMtoSPV compilation failed"); |
| 161 | spvDiagnosticDestroy(diagnostic); |
| 162 | return false; |
| 163 | } |
| 164 | spv.insert(spv.end(), binary->code, binary->code + binary->wordCount); |
| 165 | spvBinaryDestroy(binary); |
| 166 | |
| 167 | return true; |
| 168 | } |