blob: 87724d7ad09a49d928692c01ba4e941dfab82134 [file] [log] [blame]
Michael Lentine83ab3412015-11-03 16:20:30 -08001// VK tests
2//
Karl Schultz7b024b42018-08-30 16:18:18 -06003// 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 Lentine83ab3412015-11-03 16:20:30 -08007//
Jon Ashburn3ebf1252016-04-19 11:30:31 -06008// 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 Lentine83ab3412015-11-03 16:20:30 -080011//
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012// http://www.apache.org/licenses/LICENSE-2.0
Michael Lentine83ab3412015-11-03 16:20:30 -080013//
Jon Ashburn3ebf1252016-04-19 11:30:31 -060014// 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 Lentine83ab3412015-11-03 16:20:30 -080019
20#include "vktestframeworkandroid.h"
Cody Northrop8e54a402016-03-08 22:25:52 -070021#include "shaderc/shaderc.hpp"
22#include <android/log.h>
Michael Lentine83ab3412015-11-03 16:20:30 -080023
24VkTestFramework::VkTestFramework() {}
25VkTestFramework::~VkTestFramework() {}
26
Tobin Ehlis72888642017-11-15 09:43:56 -070027// Define static elements
28bool VkTestFramework::m_devsim_layer = false;
Locke5ddafd92019-03-20 10:11:33 -060029ANativeWindow *VkTestFramework::window = nullptr;
Tobin Ehlis72888642017-11-15 09:43:56 -070030
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060031VkFormat VkTestFramework::GetFormat(VkInstance instance, vk_testing::Device *device) {
Michael Lentineb62cc052015-12-09 08:44:25 -080032 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 Lobodzinskic6a62142016-09-07 16:35:55 -060035 format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Michael Lentineb62cc052015-12-09 08:44:25 -080036 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 Lobodzinskic6a62142016-09-07 16:35:55 -060040 format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Michael Lentineb62cc052015-12-09 08:44:25 -080041 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 Lentine83ab3412015-11-03 16:20:30 -080047void VkTestFramework::InitArgs(int *argc, char *argv[]) {}
48void VkTestFramework::Finish() {}
49
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060050void TestEnvironment::SetUp() { vk_testing::set_error_callback(test_error_callback); }
Michael Lentine83ab3412015-11-03 16:20:30 -080051
52void TestEnvironment::TearDown() {}
Cody Northrop8e54a402016-03-08 22:25:52 -070053
Cody Northrop8e54a402016-03-08 22:25:52 -070054// Android specific helper functions for shaderc.
55struct shader_type_mapping {
56 VkShaderStageFlagBits vkshader_type;
57 shaderc_shader_kind shaderc_type;
58};
59
60static const shader_type_mapping shader_map_table[] = {
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060061 {VK_SHADER_STAGE_VERTEX_BIT, shaderc_glsl_vertex_shader},
62 {VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, shaderc_glsl_tess_control_shader},
63 {VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, shaderc_glsl_tess_evaluation_shader},
64 {VK_SHADER_STAGE_GEOMETRY_BIT, shaderc_glsl_geometry_shader},
65 {VK_SHADER_STAGE_FRAGMENT_BIT, shaderc_glsl_fragment_shader},
66 {VK_SHADER_STAGE_COMPUTE_BIT, shaderc_glsl_compute_shader},
Cody Northrop8e54a402016-03-08 22:25:52 -070067};
68
69shaderc_shader_kind MapShadercType(VkShaderStageFlagBits vkShader) {
70 for (auto shader : shader_map_table) {
71 if (shader.vkshader_type == vkShader) {
72 return shader.shaderc_type;
73 }
74 }
75 assert(false);
76 return shaderc_glsl_infer_from_source;
77}
78
79// Compile a given string containing GLSL into SPIR-V
80// Return value of false means an error was encountered
Karl Schultz7b024b42018-08-30 16:18:18 -060081bool VkTestFramework::GLSLtoSPV(const VkShaderStageFlagBits shader_type, const char *pshader, std::vector<unsigned int> &spirv,
Jeff Bolz49d64982019-09-18 13:18:14 -050082 bool debug, uint32_t spirv_minor_version) {
Cody Northrop8e54a402016-03-08 22:25:52 -070083 // On Android, use shaderc instead.
84 shaderc::Compiler compiler;
Karl Schultz6c009e52019-01-14 19:21:05 -070085 shaderc::CompileOptions options;
86 if (debug) {
87 options.SetOptimizationLevel(shaderc_optimization_level_zero);
88 options.SetGenerateDebugInfo();
89 }
Jeff Bolz49d64982019-09-18 13:18:14 -050090
91 switch (spirv_minor_version) {
92 default:
93 case 0:
94 options.SetTargetSpirv(shaderc_spirv_version_1_0);
95 break;
96 case 1:
97 options.SetTargetSpirv(shaderc_spirv_version_1_1);
98 break;
99 case 2:
100 options.SetTargetSpirv(shaderc_spirv_version_1_2);
101 break;
102 case 3:
103 options.SetTargetSpirv(shaderc_spirv_version_1_3);
104 break;
105 case 4:
106 options.SetTargetSpirv(shaderc_spirv_version_1_4);
107 break;
108 }
109
Mark Lobodzinskic6a62142016-09-07 16:35:55 -0600110 shaderc::SpvCompilationResult result =
Karl Schultz6c009e52019-01-14 19:21:05 -0700111 compiler.CompileGlslToSpv(pshader, strlen(pshader), MapShadercType(shader_type), "shader", options);
Cody Northrop8e54a402016-03-08 22:25:52 -0700112 if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
林昆毅 Kunyi(Locke) Lin7fa7d062019-06-10 20:36:29 -0600113 __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTests", "GLSLtoSPV compilation failed: %s",
Cody Northrop10133102018-09-25 09:02:03 -0600114 result.GetErrorMessage().c_str());
Cody Northrop8e54a402016-03-08 22:25:52 -0700115 return false;
116 }
117
118 for (auto iter = result.begin(); iter != result.end(); iter++) {
119 spirv.push_back(*iter);
120 }
121
122 return true;
123}
Karl Schultz23707622018-08-22 10:20:33 -0600124
125//
126// Compile a given string containing SPIR-V assembly into SPV for use by VK
127// Return value of false means an error was encountered.
128//
129bool VkTestFramework::ASMtoSPV(const spv_target_env target_env, const uint32_t options, const char *pasm,
130 std::vector<unsigned int> &spv) {
131 spv_binary binary;
132 spv_diagnostic diagnostic = nullptr;
133 spv_context context = spvContextCreate(target_env);
134 spv_result_t error = spvTextToBinaryWithOptions(context, pasm, strlen(pasm), options, &binary, &diagnostic);
135 spvContextDestroy(context);
136 if (error) {
137 __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTest", "ASMtoSPV compilation failed");
138 spvDiagnosticDestroy(diagnostic);
139 return false;
140 }
141 spv.insert(spv.end(), binary->code, binary->code + binary->wordCount);
142 spvBinaryDestroy(binary);
143
144 return true;
145}