1 // VK tests
2 //
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.
7 //
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
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
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.
19
20 #include "vktestframeworkandroid.h"
21 #include "shaderc/shaderc.hpp"
22 #include <android/log.h>
23
VkTestFramework()24 VkTestFramework::VkTestFramework() {}
~VkTestFramework()25 VkTestFramework::~VkTestFramework() {}
26
27 // Define static elements
28 bool VkTestFramework::m_devsim_layer = false;
29 bool VkTestFramework::m_khronos_layer_disable = false;
30 ANativeWindow *VkTestFramework::window = nullptr;
31
GetFormat(VkInstance instance,vk_testing::Device * device)32 VkFormat VkTestFramework::GetFormat(VkInstance instance, vk_testing::Device *device) {
33 VkFormatProperties format_props;
34 vkGetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_B8G8R8A8_UNORM, &format_props);
35 if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT ||
36 format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
37 return VK_FORMAT_B8G8R8A8_UNORM;
38 }
39 vkGetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_R8G8B8A8_UNORM, &format_props);
40 if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT ||
41 format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
42 return VK_FORMAT_R8G8B8A8_UNORM;
43 }
44 printf("Error - device does not support VK_FORMAT_B8G8R8A8_UNORM nor VK_FORMAT_R8G8B8A8_UNORM - exiting\n");
45 exit(0);
46 }
47
InitArgs(int * argc,char * argv[])48 void VkTestFramework::InitArgs(int *argc, char *argv[]) {}
Finish()49 void VkTestFramework::Finish() {}
50
SetUp()51 void TestEnvironment::SetUp() { vk_testing::set_error_callback(test_error_callback); }
52
TearDown()53 void TestEnvironment::TearDown() {}
54
55 // Android specific helper functions for shaderc.
56 struct shader_type_mapping {
57 VkShaderStageFlagBits vkshader_type;
58 shaderc_shader_kind shaderc_type;
59 };
60
61 static const shader_type_mapping shader_map_table[] = {
62 {VK_SHADER_STAGE_VERTEX_BIT, shaderc_glsl_vertex_shader},
63 {VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, shaderc_glsl_tess_control_shader},
64 {VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, shaderc_glsl_tess_evaluation_shader},
65 {VK_SHADER_STAGE_GEOMETRY_BIT, shaderc_glsl_geometry_shader},
66 {VK_SHADER_STAGE_FRAGMENT_BIT, shaderc_glsl_fragment_shader},
67 {VK_SHADER_STAGE_COMPUTE_BIT, shaderc_glsl_compute_shader},
68 };
69
MapShadercType(VkShaderStageFlagBits vkShader)70 shaderc_shader_kind MapShadercType(VkShaderStageFlagBits vkShader) {
71 for (auto shader : shader_map_table) {
72 if (shader.vkshader_type == vkShader) {
73 return shader.shaderc_type;
74 }
75 }
76 assert(false);
77 return shaderc_glsl_infer_from_source;
78 }
79
80 // Compile a given string containing GLSL into SPIR-V
81 // Return value of false means an error was encountered
GLSLtoSPV(const VkShaderStageFlagBits shader_type,const char * pshader,std::vector<unsigned int> & spirv,bool debug)82 bool VkTestFramework::GLSLtoSPV(const VkShaderStageFlagBits shader_type, const char *pshader, std::vector<unsigned int> &spirv,
83 bool debug) {
84 // On Android, use shaderc instead.
85 shaderc::Compiler compiler;
86 shaderc::CompileOptions options;
87 if (debug) {
88 options.SetOptimizationLevel(shaderc_optimization_level_zero);
89 options.SetGenerateDebugInfo();
90 }
91 shaderc::SpvCompilationResult result =
92 compiler.CompileGlslToSpv(pshader, strlen(pshader), MapShadercType(shader_type), "shader", options);
93 if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
94 __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTests", "GLSLtoSPV compilation failed: %s",
95 result.GetErrorMessage().c_str());
96 return false;
97 }
98
99 for (auto iter = result.begin(); iter != result.end(); iter++) {
100 spirv.push_back(*iter);
101 }
102
103 return true;
104 }
105
106 //
107 // Compile a given string containing SPIR-V assembly into SPV for use by VK
108 // Return value of false means an error was encountered.
109 //
ASMtoSPV(const spv_target_env target_env,const uint32_t options,const char * pasm,std::vector<unsigned int> & spv)110 bool VkTestFramework::ASMtoSPV(const spv_target_env target_env, const uint32_t options, const char *pasm,
111 std::vector<unsigned int> &spv) {
112 spv_binary binary;
113 spv_diagnostic diagnostic = nullptr;
114 spv_context context = spvContextCreate(target_env);
115 spv_result_t error = spvTextToBinaryWithOptions(context, pasm, strlen(pasm), options, &binary, &diagnostic);
116 spvContextDestroy(context);
117 if (error) {
118 __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTest", "ASMtoSPV compilation failed");
119 spvDiagnosticDestroy(diagnostic);
120 return false;
121 }
122 spv.insert(spv.end(), binary->code, binary->code + binary->wordCount);
123 spvBinaryDestroy(binary);
124
125 return true;
126 }
127