Skip to content

Commit

Permalink
add glslang
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Sep 18, 2016
1 parent 48c2d95 commit df57487
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -1,3 +1,6 @@
[submodule "glfw"]
path = glfw
url = https://github.com/glfw/glfw.git
[submodule "glslang"]
path = glslang
url = https://github.com/KhronosGroup/glslang.git
53 changes: 51 additions & 2 deletions glfwvulkan/main.cpp
@@ -1,5 +1,6 @@
#include "glfwmanager.h"
#include "cube_vertices.h"
#include "util.hpp"
#include <string>
#include <vector>
#include <memory>
Expand All @@ -13,6 +14,40 @@
#include <glm/gtc/matrix_transform.hpp>


// For this sample, we'll start with GLSL so the shader function is plain
// and then use the glslang GLSLtoSPV utility to convert it to SPIR-V for
// the driver. We do this for clarity rather than using pre-compiled
// SPIR-V

auto vertShaderText =
"#version 400\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_ARB_shading_language_420pack : enable\n"
"layout (std140, binding = 0) uniform bufferVals {\n"
" mat4 mvp;\n"
"} myBufferVals;\n"
"layout (location = 0) in vec4 pos;\n"
"layout (location = 1) in vec4 inColor;\n"
"layout (location = 0) out vec4 outColor;\n"
"out gl_PerVertex { \n"
" vec4 gl_Position;\n"
"};\n"
"void main() {\n"
" outColor = inColor;\n"
" gl_Position = myBufferVals.mvp * pos;\n"
"}\n";

auto fragShaderText =
"#version 400\n"
"#extension GL_ARB_separate_shader_objects : enable\n"
"#extension GL_ARB_shading_language_420pack : enable\n"
"layout (location = 0) in vec4 color;\n"
"layout (location = 0) out vec4 outColor;\n"
"void main() {\n"
" outColor = color;\n"
"}\n";


template<typename T>
const T* data_or_null(const std::vector<T> &v)
{
Expand All @@ -37,6 +72,14 @@ static std::vector<unsigned int> read(const std::string &path)
}


std::vector<uint32_t> GL2SPV(VkShaderStageFlagBits stage, const char *src)
{
std::vector<uint32_t> spv;
auto retVal = GLSLtoSPV(stage, src, spv);
return spv;
}


static glm::mat4 calcMVP(const int width, int height)
{
float fov = glm::radians(45.0f);
Expand Down Expand Up @@ -1931,8 +1974,14 @@ int WINAPI WinMain(
}

PipelineResource pipeline(device);
pipeline.addShader(VK_SHADER_STAGE_VERTEX_BIT, read("../15-draw_cube.vert.spv"), "main");
pipeline.addShader(VK_SHADER_STAGE_FRAGMENT_BIT, read("../15-draw_cube.frag.spv"), "main");

init_glslang();
pipeline.addShader(VK_SHADER_STAGE_VERTEX_BIT
, GL2SPV(VK_SHADER_STAGE_VERTEX_BIT, vertShaderText), "main");
pipeline.addShader(VK_SHADER_STAGE_FRAGMENT_BIT
, GL2SPV(VK_SHADER_STAGE_FRAGMENT_BIT, fragShaderText), "main");
finalize_glslang();

if (!pipeline.createShader()) {
return 13;
}
Expand Down
3 changes: 3 additions & 0 deletions glfwvulkan/premake5.lua
Expand Up @@ -17,11 +17,13 @@ flags{
files {
"*.cpp",
"*.h",
"*.hpp",
}
includedirs {
"../glfw/include",
"../glm/include",
"C:/VulkanSDK/1.0.26.0/Include",
"../glslang",
}
defines {
"WIN32",
Expand All @@ -36,5 +38,6 @@ libdirs {
links {
"glfw3",
"vulkan-1",
"glslang",
}

193 changes: 193 additions & 0 deletions glfwvulkan/util.cpp
@@ -0,0 +1,193 @@
/*
* Vulkan Samples
*
* Copyright (C) 2015-2016 Valve Corporation
* Copyright (C) 2015-2016 LunarG, Inc.
* Copyright (C) 2015-2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "util.hpp"
#include "SPIRV/GlslangToSpv.h"

void init_glslang() {
glslang::InitializeProcess();
}

void finalize_glslang()
{
glslang::FinalizeProcess();
}

void init_resources(TBuiltInResource &Resources) {
Resources.maxLights = 32;
Resources.maxClipPlanes = 6;
Resources.maxTextureUnits = 32;
Resources.maxTextureCoords = 32;
Resources.maxVertexAttribs = 64;
Resources.maxVertexUniformComponents = 4096;
Resources.maxVaryingFloats = 64;
Resources.maxVertexTextureImageUnits = 32;
Resources.maxCombinedTextureImageUnits = 80;
Resources.maxTextureImageUnits = 32;
Resources.maxFragmentUniformComponents = 4096;
Resources.maxDrawBuffers = 32;
Resources.maxVertexUniformVectors = 128;
Resources.maxVaryingVectors = 8;
Resources.maxFragmentUniformVectors = 16;
Resources.maxVertexOutputVectors = 16;
Resources.maxFragmentInputVectors = 15;
Resources.minProgramTexelOffset = -8;
Resources.maxProgramTexelOffset = 7;
Resources.maxClipDistances = 8;
Resources.maxComputeWorkGroupCountX = 65535;
Resources.maxComputeWorkGroupCountY = 65535;
Resources.maxComputeWorkGroupCountZ = 65535;
Resources.maxComputeWorkGroupSizeX = 1024;
Resources.maxComputeWorkGroupSizeY = 1024;
Resources.maxComputeWorkGroupSizeZ = 64;
Resources.maxComputeUniformComponents = 1024;
Resources.maxComputeTextureImageUnits = 16;
Resources.maxComputeImageUniforms = 8;
Resources.maxComputeAtomicCounters = 8;
Resources.maxComputeAtomicCounterBuffers = 1;
Resources.maxVaryingComponents = 60;
Resources.maxVertexOutputComponents = 64;
Resources.maxGeometryInputComponents = 64;
Resources.maxGeometryOutputComponents = 128;
Resources.maxFragmentInputComponents = 128;
Resources.maxImageUnits = 8;
Resources.maxCombinedImageUnitsAndFragmentOutputs = 8;
Resources.maxCombinedShaderOutputResources = 8;
Resources.maxImageSamples = 0;
Resources.maxVertexImageUniforms = 0;
Resources.maxTessControlImageUniforms = 0;
Resources.maxTessEvaluationImageUniforms = 0;
Resources.maxGeometryImageUniforms = 0;
Resources.maxFragmentImageUniforms = 8;
Resources.maxCombinedImageUniforms = 8;
Resources.maxGeometryTextureImageUnits = 16;
Resources.maxGeometryOutputVertices = 256;
Resources.maxGeometryTotalOutputComponents = 1024;
Resources.maxGeometryUniformComponents = 1024;
Resources.maxGeometryVaryingComponents = 64;
Resources.maxTessControlInputComponents = 128;
Resources.maxTessControlOutputComponents = 128;
Resources.maxTessControlTextureImageUnits = 16;
Resources.maxTessControlUniformComponents = 1024;
Resources.maxTessControlTotalOutputComponents = 4096;
Resources.maxTessEvaluationInputComponents = 128;
Resources.maxTessEvaluationOutputComponents = 128;
Resources.maxTessEvaluationTextureImageUnits = 16;
Resources.maxTessEvaluationUniformComponents = 1024;
Resources.maxTessPatchComponents = 120;
Resources.maxPatchVertices = 32;
Resources.maxTessGenLevel = 64;
Resources.maxViewports = 16;
Resources.maxVertexAtomicCounters = 0;
Resources.maxTessControlAtomicCounters = 0;
Resources.maxTessEvaluationAtomicCounters = 0;
Resources.maxGeometryAtomicCounters = 0;
Resources.maxFragmentAtomicCounters = 8;
Resources.maxCombinedAtomicCounters = 8;
Resources.maxAtomicCounterBindings = 1;
Resources.maxVertexAtomicCounterBuffers = 0;
Resources.maxTessControlAtomicCounterBuffers = 0;
Resources.maxTessEvaluationAtomicCounterBuffers = 0;
Resources.maxGeometryAtomicCounterBuffers = 0;
Resources.maxFragmentAtomicCounterBuffers = 1;
Resources.maxCombinedAtomicCounterBuffers = 1;
Resources.maxAtomicCounterBufferSize = 16384;
Resources.maxTransformFeedbackBuffers = 4;
Resources.maxTransformFeedbackInterleavedComponents = 64;
Resources.maxCullDistances = 8;
Resources.maxCombinedClipAndCullDistances = 8;
Resources.maxSamples = 4;
Resources.limits.nonInductiveForLoops = 1;
Resources.limits.whileLoops = 1;
Resources.limits.doWhileLoops = 1;
Resources.limits.generalUniformIndexing = 1;
Resources.limits.generalAttributeMatrixVectorIndexing = 1;
Resources.limits.generalVaryingIndexing = 1;
Resources.limits.generalSamplerIndexing = 1;
Resources.limits.generalVariableIndexing = 1;
Resources.limits.generalConstantMatrixVectorIndexing = 1;
}

EShLanguage FindLanguage(const VkShaderStageFlagBits shader_type) {
switch (shader_type) {
case VK_SHADER_STAGE_VERTEX_BIT:
return EShLangVertex;

case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
return EShLangTessControl;

case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
return EShLangTessEvaluation;

case VK_SHADER_STAGE_GEOMETRY_BIT:
return EShLangGeometry;

case VK_SHADER_STAGE_FRAGMENT_BIT:
return EShLangFragment;

case VK_SHADER_STAGE_COMPUTE_BIT:
return EShLangCompute;

default:
return EShLangVertex;
}
}

//
// Compile a given string containing GLSL into SPV for use by VK
// Return value of false means an error was encountered.
//
bool GLSLtoSPV(const VkShaderStageFlagBits shader_type, const char *pshader,
std::vector<unsigned int> &spirv)
{
EShLanguage stage = FindLanguage(shader_type);
glslang::TShader shader(stage);
glslang::TProgram program;
const char *shaderStrings[1];
TBuiltInResource Resources;
init_resources(Resources);

// Enable SPIR-V and Vulkan rules when parsing GLSL
EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);

shaderStrings[0] = pshader;
shader.setStrings(shaderStrings, 1);

if (!shader.parse(&Resources, 100, false, messages)) {
puts(shader.getInfoLog());
puts(shader.getInfoDebugLog());
return false; // something didn't work
}

program.addShader(&shader);

//
// Program-level processing...
//

if (!program.link(messages)) {
puts(shader.getInfoLog());
puts(shader.getInfoDebugLog());
fflush(stdout);
return false;
}

glslang::GlslangToSpv(*program.getIntermediate(stage), spirv);
return true;
}
31 changes: 31 additions & 0 deletions glfwvulkan/util.hpp
@@ -0,0 +1,31 @@
/*
* Vulkan Samples
*
* Copyright (C) 2015-2016 Valve Corporation
* Copyright (C) 2015-2016 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

#include <vulkan/vulkan.h>


bool GLSLtoSPV(const VkShaderStageFlagBits shader_type, const char *pshader,
std::vector<unsigned int> &spirv);
void init_glslang();
void finalize_glslang();
1 change: 1 addition & 0 deletions glslang
Submodule glslang added at fcea30

0 comments on commit df57487

Please sign in to comment.