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