Hash :
061188a7
Author :
Date :
2021-08-04T10:07:47
Translator: General clean up General clean up done as part of other changes, split to simplify review. Bug: angleproject:4889 Bug: angleproject:6132 Change-Id: Iade9954d187a759be9edd0e3754be007f4133c56 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3071598 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Tim Van Patten <timvp@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// glslang_wrapper:
// A wrapper to compile GLSL strings to SPIR-V blobs. glslang here refers to the Khronos
// compiler.
//
// This file is separated as glslang's header contains conflicting macro definitions with ANGLE's.
//
#include "compiler/translator/glslang_wrapper.h"
// glslang has issues with some specific warnings.
ANGLE_DISABLE_SHADOWING_WARNING
ANGLE_DISABLE_SUGGEST_OVERRIDE_WARNINGS
// glslang's version of ShaderLang.h, not to be confused with ANGLE's.
#include <glslang/Public/ShaderLang.h>
// Other glslang includes.
#include <SPIRV/GlslangToSpv.h>
#include <StandAlone/ResourceLimits.h>
// SPIR-V tools include for disassembly
#include <spirv-tools/libspirv.hpp>
// Enable this for debug logging of pre-transform SPIR-V:
#if !defined(ANGLE_DEBUG_SPIRV_GENERATION)
# define ANGLE_DEBUG_SPIRV_GENERATION 0
#endif // !defined(ANGLE_DEBUG_SPIRV_GENERATION)
ANGLE_REENABLE_SUGGEST_OVERRIDE_WARNINGS
ANGLE_REENABLE_SHADOWING_WARNING
namespace sh
{
namespace
{
// Run at startup to warm up glslang's internals to avoid hitches on first shader compile.
void Warmup()
{
EShMessages messages = static_cast<EShMessages>(EShMsgSpvRules | EShMsgVulkanRules);
const TBuiltInResource builtInResources(glslang::DefaultTBuiltInResource);
glslang::TShader warmUpShader(EShLangVertex);
const char *kShaderString = R"(#version 450 core
void main(){}
)";
const int kShaderLength = static_cast<int>(strlen(kShaderString));
warmUpShader.setStringsWithLengths(&kShaderString, &kShaderLength, 1);
warmUpShader.setEntryPoint("main");
bool result = warmUpShader.parse(&builtInResources, 450, ECoreProfile, false, false, messages);
ASSERT(result);
}
// Generate glslang resources from ANGLE translator resources.
void GetBuiltInResources(const ShBuiltInResources &resources, TBuiltInResource *outResources)
{
outResources->maxDrawBuffers = resources.MaxDrawBuffers;
outResources->maxAtomicCounterBindings = resources.MaxAtomicCounterBindings;
outResources->maxAtomicCounterBufferSize = resources.MaxAtomicCounterBufferSize;
outResources->maxCombinedAtomicCounterBuffers = resources.MaxCombinedAtomicCounterBuffers;
outResources->maxCombinedAtomicCounters = resources.MaxCombinedAtomicCounters;
outResources->maxCombinedImageUniforms = resources.MaxCombinedImageUniforms;
outResources->maxCombinedTextureImageUnits = resources.MaxCombinedTextureImageUnits;
outResources->maxCombinedShaderOutputResources = resources.MaxCombinedShaderOutputResources;
outResources->maxComputeWorkGroupCountX = resources.MaxComputeWorkGroupCount[0];
outResources->maxComputeWorkGroupCountY = resources.MaxComputeWorkGroupCount[1];
outResources->maxComputeWorkGroupCountZ = resources.MaxComputeWorkGroupCount[2];
outResources->maxComputeWorkGroupSizeX = resources.MaxComputeWorkGroupSize[0];
outResources->maxComputeWorkGroupSizeY = resources.MaxComputeWorkGroupSize[1];
outResources->maxComputeWorkGroupSizeZ = resources.MaxComputeWorkGroupSize[2];
outResources->minProgramTexelOffset = resources.MinProgramTexelOffset;
outResources->maxFragmentUniformVectors = resources.MaxFragmentUniformVectors;
outResources->maxGeometryInputComponents = resources.MaxGeometryInputComponents;
outResources->maxGeometryOutputComponents = resources.MaxGeometryOutputComponents;
outResources->maxGeometryOutputVertices = resources.MaxGeometryOutputVertices;
outResources->maxGeometryTotalOutputComponents = resources.MaxGeometryTotalOutputComponents;
outResources->maxPatchVertices = resources.MaxPatchVertices;
outResources->maxProgramTexelOffset = resources.MaxProgramTexelOffset;
outResources->maxVaryingVectors = resources.MaxVaryingVectors;
outResources->maxVertexUniformVectors = resources.MaxVertexUniformVectors;
outResources->maxClipDistances = resources.MaxClipDistances;
outResources->maxSamples = resources.MaxSamples;
outResources->maxCullDistances = resources.MaxCullDistances;
outResources->maxCombinedClipAndCullDistances = resources.MaxCombinedClipAndCullDistances;
}
} // anonymous namespace
void GlslangInitialize()
{
int result = ShInitialize();
ASSERT(result != 0);
Warmup();
}
void GlslangFinalize()
{
int result = ShFinalize();
ASSERT(result != 0);
}
ANGLE_NO_DISCARD bool GlslangCompileToSpirv(const ShBuiltInResources &resources,
sh::GLenum shaderType,
const std::string &shaderSource,
angle::spirv::Blob *spirvBlobOut)
{
TBuiltInResource builtInResources(glslang::DefaultTBuiltInResource);
GetBuiltInResources(resources, &builtInResources);
EShLanguage language = EShLangVertex;
switch (shaderType)
{
case GL_VERTEX_SHADER:
language = EShLangVertex;
break;
case GL_TESS_CONTROL_SHADER:
language = EShLangTessControl;
break;
case GL_TESS_EVALUATION_SHADER_EXT:
language = EShLangTessEvaluation;
break;
case GL_GEOMETRY_SHADER:
language = EShLangGeometry;
break;
case GL_FRAGMENT_SHADER:
language = EShLangFragment;
break;
case GL_COMPUTE_SHADER:
language = EShLangCompute;
break;
default:
UNREACHABLE();
}
glslang::TShader shader(language);
glslang::TProgram program;
// Enable SPIR-V and Vulkan rules when parsing GLSL
constexpr EShMessages messages = static_cast<EShMessages>(EShMsgSpvRules | EShMsgVulkanRules);
const char *shaderString = shaderSource.c_str();
int shaderLength = static_cast<int>(shaderSource.size());
shader.setStringsWithLengths(&shaderString, &shaderLength, 1);
shader.setEntryPoint("main");
#if ANGLE_DEBUG_SPIRV_GENERATION
fprintf(stderr, "%s\n", shaderString);
#endif // ANGLE_DEBUG_SPIRV_GENERATION
bool result = shader.parse(&builtInResources, 450, ECoreProfile, false, false, messages);
if (!result)
{
WARN() << "Internal error parsing Vulkan shader corresponding to " << shaderType << ":\n"
<< shader.getInfoLog() << "\n"
<< shader.getInfoDebugLog() << "\n";
return false;
}
program.addShader(&shader);
bool linkResult = program.link(messages);
if (!linkResult)
{
WARN() << "Internal error linking Vulkan shader:\n" << program.getInfoLog() << "\n";
}
glslang::TIntermediate *intermediate = program.getIntermediate(language);
glslang::GlslangToSpv(*intermediate, *spirvBlobOut);
#if ANGLE_DEBUG_SPIRV_GENERATION
spvtools::SpirvTools spirvTools(SPV_ENV_VULKAN_1_1);
std::string readableSpirv;
spirvTools.Disassemble(*spirvBlobOut, &readableSpirv, 0);
fprintf(stderr, "%s\n", readableSpirv.c_str());
#endif // ANGLE_DEBUG_SPIRV_GENERATION
return true;
}
} // namespace sh