1//  VK tests
2//
3//  Copyright (c) 2015-2016 The Khronos Group Inc.
4//  Copyright (c) 2015-2016 Valve Corporation
5//  Copyright (c) 2015-2016 LunarG, Inc.
6//  Copyright (c) 2015-2016 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
24VkTestFramework::VkTestFramework() {}
25VkTestFramework::~VkTestFramework() {}
26
27// Define static elements
28bool VkTestFramework::m_devsim_layer = false;
29
30VkFormat 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
46void VkTestFramework::InitArgs(int *argc, char *argv[]) {}
47void VkTestFramework::Finish() {}
48
49void TestEnvironment::SetUp() { vk_testing::set_error_callback(test_error_callback); }
50
51void TestEnvironment::TearDown() {}
52
53// Android specific helper functions for shaderc.
54struct shader_type_mapping {
55    VkShaderStageFlagBits vkshader_type;
56    shaderc_shader_kind shaderc_type;
57};
58
59static 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
68shaderc_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
80bool VkTestFramework::GLSLtoSPV(const VkShaderStageFlagBits shader_type, const char *pshader, std::vector<unsigned int> &spirv) {
81    // On Android, use shaderc instead.
82    shaderc::Compiler compiler;
83    shaderc::SpvCompilationResult result =
84        compiler.CompileGlslToSpv(pshader, strlen(pshader), MapShadercType(shader_type), "shader");
85    if (result.GetCompilationStatus() != shaderc_compilation_status_success) {
86        __android_log_print(ANDROID_LOG_ERROR, "VkLayerValidationTest", "GLSLtoSPV compilation failed");
87        return false;
88    }
89
90    for (auto iter = result.begin(); iter != result.end(); iter++) {
91        spirv.push_back(*iter);
92    }
93
94    return true;
95}
96