Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 1 | // VK tests |
| 2 | // |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 3 | // Copyright (c) 2015-2019 The Khronos Group Inc. |
| 4 | // Copyright (c) 2015-2019 Valve Corporation |
| 5 | // Copyright (c) 2015-2019 LunarG, Inc. |
| 6 | // Copyright (c) 2015-2019 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" |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 21 | #include "shaderc/shaderc.hpp" |
| 22 | #include <android/log.h> |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 23 | |
| 24 | VkTestFramework::VkTestFramework() {} |
| 25 | VkTestFramework::~VkTestFramework() {} |
| 26 | |
Tobin Ehlis | 7288864 | 2017-11-15 09:43:56 -0700 | [diff] [blame] | 27 | // Define static elements |
| 28 | bool VkTestFramework::m_devsim_layer = false; |
Locke | 5ddafd9 | 2019-03-20 10:11:33 -0600 | [diff] [blame] | 29 | ANativeWindow *VkTestFramework::window = nullptr; |
Tobin Ehlis | 7288864 | 2017-11-15 09:43:56 -0700 | [diff] [blame] | 30 | |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 31 | VkFormat VkTestFramework::GetFormat(VkInstance instance, vk_testing::Device *device) { |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 32 | VkFormatProperties format_props; |
| 33 | vkGetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_B8G8R8A8_UNORM, &format_props); |
| 34 | if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT || |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 35 | format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 36 | return VK_FORMAT_B8G8R8A8_UNORM; |
| 37 | } |
| 38 | vkGetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_R8G8B8A8_UNORM, &format_props); |
| 39 | if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT || |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 40 | format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) { |
Michael Lentine | b62cc05 | 2015-12-09 08:44:25 -0800 | [diff] [blame] | 41 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 42 | } |
| 43 | printf("Error - device does not support VK_FORMAT_B8G8R8A8_UNORM nor VK_FORMAT_R8G8B8A8_UNORM - exiting\n"); |
| 44 | exit(0); |
| 45 | } |
| 46 | |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 47 | void VkTestFramework::InitArgs(int *argc, char *argv[]) {} |
| 48 | void VkTestFramework::Finish() {} |
| 49 | |
Mark Lobodzinski | 91ee16c | 2019-09-27 11:44:52 -0600 | [diff] [blame^] | 50 | void TestEnvironment::SetUp() { |
| 51 | vk_testing::set_error_callback(test_error_callback); |
| 52 | |
| 53 | vk::InitDispatchTable(); |
| 54 | } |
Michael Lentine | 83ab341 | 2015-11-03 16:20:30 -0800 | [diff] [blame] | 55 | |
| 56 | void TestEnvironment::TearDown() {} |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 57 | |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 58 | // Android specific helper functions for shaderc. |
| 59 | struct shader_type_mapping { |
| 60 | VkShaderStageFlagBits vkshader_type; |
| 61 | shaderc_shader_kind shaderc_type; |
| 62 | }; |
| 63 | |
| 64 | static const shader_type_mapping shader_map_table[] = { |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 65 | {VK_SHADER_STAGE_VERTEX_BIT, shaderc_glsl_vertex_shader}, |
| 66 | {VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, shaderc_glsl_tess_control_shader}, |
| 67 | {VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, shaderc_glsl_tess_evaluation_shader}, |
| 68 | {VK_SHADER_STAGE_GEOMETRY_BIT, shaderc_glsl_geometry_shader}, |
| 69 | {VK_SHADER_STAGE_FRAGMENT_BIT, shaderc_glsl_fragment_shader}, |
| 70 | {VK_SHADER_STAGE_COMPUTE_BIT, shaderc_glsl_compute_shader}, |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | shaderc_shader_kind MapShadercType(VkShaderStageFlagBits vkShader) { |
| 74 | for (auto shader : shader_map_table) { |
| 75 | if (shader.vkshader_type == vkShader) { |
| 76 | return shader.shaderc_type; |
| 77 | } |
| 78 | } |
| 79 | assert(false); |
| 80 | return shaderc_glsl_infer_from_source; |
| 81 | } |
| 82 | |
| 83 | // Compile a given string containing GLSL into SPIR-V |
| 84 | // Return value of false means an error was encountered |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 85 | bool VkTestFramework::GLSLtoSPV(const VkShaderStageFlagBits shader_type, const char *pshader, std::vector<unsigned int> &spirv, |
Jeff Bolz | 49d6498 | 2019-09-18 13:18:14 -0500 | [diff] [blame] | 86 | bool debug, uint32_t spirv_minor_version) { |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 87 | // On Android, use shaderc instead. |
| 88 | shaderc::Compiler compiler; |
Karl Schultz | 6c009e5 | 2019-01-14 19:21:05 -0700 | [diff] [blame] | 89 | shaderc::CompileOptions options; |
| 90 | if (debug) { |
| 91 | options.SetOptimizationLevel(shaderc_optimization_level_zero); |
| 92 | options.SetGenerateDebugInfo(); |
| 93 | } |
Jeff Bolz | 49d6498 | 2019-09-18 13:18:14 -0500 | [diff] [blame] | 94 | |
| 95 | switch (spirv_minor_version) { |
| 96 | default: |
| 97 | case 0: |
| 98 | options.SetTargetSpirv(shaderc_spirv_version_1_0); |
| 99 | break; |
| 100 | case 1: |
| 101 | options.SetTargetSpirv(shaderc_spirv_version_1_1); |
| 102 | break; |
| 103 | case 2: |
| 104 | options.SetTargetSpirv(shaderc_spirv_version_1_2); |
| 105 | break; |
| 106 | case 3: |
| 107 | options.SetTargetSpirv(shaderc_spirv_version_1_3); |
| 108 | break; |
| 109 | case 4: |
| 110 | options.SetTargetSpirv(shaderc_spirv_version_1_4); |
| 111 | break; |
| 112 | } |
| 113 | |
Mark Lobodzinski | c6a6214 | 2016-09-07 16:35:55 -0600 | [diff] [blame] | 114 | shaderc::SpvCompilationResult result = |
Karl Schultz | 6c009e5 | 2019-01-14 19:21:05 -0700 | [diff] [blame] | 115 | compiler.CompileGlslToSpv(pshader, strlen(pshader), MapShadercType(shader_type), "shader", options); |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 116 | if (result.GetCompilationStatus() != shaderc_compilation_status_success) { |
林昆毅 Kunyi(Locke) Lin | 7fa7d06 | 2019-06-10 20:36:29 -0600 | [diff] [blame] | 117 | __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTests", "GLSLtoSPV compilation failed: %s", |
Cody Northrop | 1013310 | 2018-09-25 09:02:03 -0600 | [diff] [blame] | 118 | result.GetErrorMessage().c_str()); |
Cody Northrop | 8e54a40 | 2016-03-08 22:25:52 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | for (auto iter = result.begin(); iter != result.end(); iter++) { |
| 123 | spirv.push_back(*iter); |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
Karl Schultz | 2370762 | 2018-08-22 10:20:33 -0600 | [diff] [blame] | 128 | |
| 129 | // |
| 130 | // Compile a given string containing SPIR-V assembly into SPV for use by VK |
| 131 | // Return value of false means an error was encountered. |
| 132 | // |
| 133 | bool VkTestFramework::ASMtoSPV(const spv_target_env target_env, const uint32_t options, const char *pasm, |
| 134 | std::vector<unsigned int> &spv) { |
| 135 | spv_binary binary; |
| 136 | spv_diagnostic diagnostic = nullptr; |
| 137 | spv_context context = spvContextCreate(target_env); |
| 138 | spv_result_t error = spvTextToBinaryWithOptions(context, pasm, strlen(pasm), options, &binary, &diagnostic); |
| 139 | spvContextDestroy(context); |
| 140 | if (error) { |
| 141 | __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTest", "ASMtoSPV compilation failed"); |
| 142 | spvDiagnosticDestroy(diagnostic); |
| 143 | return false; |
| 144 | } |
| 145 | spv.insert(spv.end(), binary->code, binary->code + binary->wordCount); |
| 146 | spvBinaryDestroy(binary); |
| 147 | |
| 148 | return true; |
| 149 | } |