blob: 607fafd11e2c9fc422734516cf6c5de049746625 [file] [log] [blame]
Michael Lentine83ab3412015-11-03 16:20:30 -08001// VK tests
2//
Nathaniel Cesario022e7b22021-04-06 00:06:17 -06003// 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 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"
Nathaniel Cesarioe1f91ba2021-08-19 14:06:11 -060021#include "vkrenderframework.h"
Cody Northrop8e54a402016-03-08 22:25:52 -070022#include "shaderc/shaderc.hpp"
23#include <android/log.h>
Michael Lentine83ab3412015-11-03 16:20:30 -080024
25VkTestFramework::VkTestFramework() {}
26VkTestFramework::~VkTestFramework() {}
27
Tobin Ehlis72888642017-11-15 09:43:56 -070028// Define static elements
29bool VkTestFramework::m_devsim_layer = false;
Nathaniel Cesario022e7b22021-04-06 00:06:17 -060030int VkTestFramework::m_phys_device_index = -1;
Locke5ddafd92019-03-20 10:11:33 -060031ANativeWindow *VkTestFramework::window = nullptr;
Tobin Ehlis72888642017-11-15 09:43:56 -070032
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060033VkFormat VkTestFramework::GetFormat(VkInstance instance, vk_testing::Device *device) {
Michael Lentineb62cc052015-12-09 08:44:25 -080034 VkFormatProperties format_props;
Mark Lobodzinski1ad400e2019-09-27 13:53:29 -060035 vk::GetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_B8G8R8A8_UNORM, &format_props);
Michael Lentineb62cc052015-12-09 08:44:25 -080036 if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT ||
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060037 format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Michael Lentineb62cc052015-12-09 08:44:25 -080038 return VK_FORMAT_B8G8R8A8_UNORM;
39 }
Mark Lobodzinski1ad400e2019-09-27 13:53:29 -060040 vk::GetPhysicalDeviceFormatProperties(device->phy().handle(), VK_FORMAT_R8G8B8A8_UNORM, &format_props);
Michael Lentineb62cc052015-12-09 08:44:25 -080041 if (format_props.linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT ||
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060042 format_props.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) {
Michael Lentineb62cc052015-12-09 08:44:25 -080043 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 Lentine83ab3412015-11-03 16:20:30 -080049void VkTestFramework::InitArgs(int *argc, char *argv[]) {}
50void VkTestFramework::Finish() {}
51
Mark Lobodzinski91ee16c2019-09-27 11:44:52 -060052void TestEnvironment::SetUp() {
53 vk_testing::set_error_callback(test_error_callback);
54
55 vk::InitDispatchTable();
56}
Michael Lentine83ab3412015-11-03 16:20:30 -080057
58void TestEnvironment::TearDown() {}
Cody Northrop8e54a402016-03-08 22:25:52 -070059
Cody Northrop8e54a402016-03-08 22:25:52 -070060// Android specific helper functions for shaderc.
61struct shader_type_mapping {
62 VkShaderStageFlagBits vkshader_type;
63 shaderc_shader_kind shaderc_type;
64};
65
66static const shader_type_mapping shader_map_table[] = {
Mark Lobodzinskic6a62142016-09-07 16:35:55 -060067 {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 Northrop8e54a402016-03-08 22:25:52 -070073};
74
75shaderc_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 Lobodzinskif0003442019-10-28 16:05:32 -060087bool VkTestFramework::GLSLtoSPV(VkPhysicalDeviceLimits const *const device_limits, const VkShaderStageFlagBits shader_type,
Nathaniel Cesarioe1f91ba2021-08-19 14:06:11 -060088 const char *pshader, std::vector<unsigned int> &spirv, bool debug, const spv_target_env spv_env) {
Cody Northrop8e54a402016-03-08 22:25:52 -070089 // On Android, use shaderc instead.
90 shaderc::Compiler compiler;
Karl Schultz6c009e52019-01-14 19:21:05 -070091 shaderc::CompileOptions options;
92 if (debug) {
93 options.SetOptimizationLevel(shaderc_optimization_level_zero);
94 options.SetGenerateDebugInfo();
95 }
Jeff Bolz49d64982019-09-18 13:18:14 -050096
Nathaniel Cesarioe1f91ba2021-08-19 14:06:11 -060097 switch (VkShaderObj::FromSpvTargetEnv(spv_env)) {
Jeff Bolz49d64982019-09-18 13:18:14 -050098 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 Lobodzinskif0003442019-10-28 16:05:32 -0600116 // 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 Lobodzinskic6a62142016-09-07 16:35:55 -0600133 shaderc::SpvCompilationResult result =
Karl Schultz6c009e52019-01-14 19:21:05 -0700134 compiler.CompileGlslToSpv(pshader, strlen(pshader), MapShadercType(shader_type), "shader", options);
Cody Northrop8e54a402016-03-08 22:25:52 -0700135 if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
Mark Lobodzinski4e1a2f32019-10-23 11:17:41 -0600136 __android_log_print(ANDROID_LOG_ERROR, "VulkanLayerValidationTests", "GLSLtoSPV compilation failed: %s",
Cody Northrop10133102018-09-25 09:02:03 -0600137 result.GetErrorMessage().c_str());
Cody Northrop8e54a402016-03-08 22:25:52 -0700138 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 Schultz23707622018-08-22 10:20:33 -0600147
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//
152bool 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}