Advertisement
Guest User

Vulkan shader testing

a guest
Feb 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Vulkan Engine
  3.  *
  4.  * Copyright (C) 2016-2017 Pawel Kaczynski
  5.  */
  6.  
  7. #pragma once
  8.  
  9. #include "VulkanCore.h"
  10.  
  11. #ifdef __ANDROID__
  12. #include "shaderc/shaderc.hpp"
  13. #else
  14. #include <shaderc/shaderc.hpp>
  15. #endif
  16.  
  17. /**
  18.  * @file Shaders.h
  19.  */
  20.  
  21. VULKAN_NS_BEGIN
  22.  
  23. static const char* VULKAN_TESSELATION_CONTROL_SHADER_TEXT =
  24.     "#version 450\n"
  25.     "layout (vertices = 3) out;\n"
  26.     "layout (location = 0) in vec4 inColor[];\n"
  27.     "layout (location = 3) in mat4 inProjMatrix[];\n"
  28.     "layout (location = 6) in mat4 inViewMatrix[];\n"
  29.     "layout (location = 9) in vec4 inUndistortion[];\n"
  30.     "layout (location = 0) out vec4 outColor[3];\n"
  31.     "layout (location = 3) out mat4 outProjMatrix[3];\n"
  32.     "layout (location = 6) out mat4 outViewMatrix[3];\n"
  33.     "layout (location = 9) out mat4 outUndistortion[3];\n"
  34.     "void main()\n"
  35.     "{\n"
  36.     "    outColor[gl_InvocationID] = inColor[gl_InvocationID];\n"
  37.     "    outProjMatrix[gl_InvocationID] = inProjMatrix[gl_InvocationID];\n"
  38.     "    outViewMatrix[gl_InvocationID] = inViewMatrix[gl_InvocationID];\n"
  39.     "    outUndistortion[gl_InvocationID] = inUndistortion[gl_InvocationID];\n"
  40.     "    if (gl_InvocationID == 0)\n"
  41.     "    {\n"
  42.     "        gl_TessLevelInner[0] = 4.f;\n"
  43.     "        gl_TessLevelOuter[0] = 4.f;\n"
  44.     "        gl_TessLevelOuter[1] = 4.f;\n"
  45.     "        gl_TessLevelOuter[2] = 4.f;\n"
  46.     "    }\n"
  47.     "   gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n"
  48.     "}\n";
  49.  
  50. static const char* VULKAN_TESSELATION_EVALUATION_SHADER_TEXT =
  51.     "#version 450 core\n"
  52.     "layout (triangles, equal_spacing, cw) in;\n"
  53.     "layout (location = 0) in vec4 inColor[];\n"
  54.     "layout (location = 3) in vec4 inProjMatrix[];\n"
  55.     "layout (location = 6) in mat4 inViewMatrix[];\n"
  56.     "layout (location = 9) in vec4 inUndistortion[];\n"
  57.     "layout (location = 0) out vec4 outColor;\n"
  58.     "vec4 undistort(vec4 pos) {\n"
  59.     "    float maxRadiusSquared = 100.0;\n"
  60.     "    pos = myUniformValues.modelViewMatrix * pos;\n"
  61.     "    float r2 = clamp(dot(pos.xy, pos.xy) / (pos.z * pos.z), 0, maxRadiusSquared);\n"
  62.     "    pos.xy *= 1 + (myUniformValues.undistortion.x + myUniformValues.undistortion.y * r2) * r2;\n"
  63.     "    return pos;\n"
  64.     "}\n"
  65.     "void main()\n"
  66.     "{\n"
  67.     "   outColor = inColor[0];\n"
  68.     "   gl_Position = gl_in[0].gl_Position * gl_TessCoord.x +\n"
  69.     "               gl_in[1].gl_Position * gl_TessCoord.y +\n"
  70.     "               gl_in[2].gl_Position * gl_TessCoord.z;\n"
  71.     "   gl_Position = inProjMatrix[0] * inViewMatrix[0] * gl_Position;\n"
  72.     "}\n";
  73.  
  74. static const char* VULKAN_VERTEX_SHADER_TEXT =
  75.     "#version 400\n"
  76.     "#extension GL_ARB_separate_shader_objects : enable\n"
  77.     "#extension GL_ARB_shading_language_420pack : enable\n"
  78.     "layout (std140, binding = 0) uniform uniformValues {\n"
  79.     "    mat4 modelViewMatrix;\n"
  80.     "    mat4 projectionMatrix;\n"
  81.     "    vec4 undistortion;\n"
  82.     "} myUniformValues;\n"
  83.     "layout (location = 0) in vec4 pos;\n"
  84.     "layout (location = 1) in vec4 inColor;\n"
  85.     "layout (location = 0) out vec4 outColor;\n"
  86.     "layout (location = 1) out mat4 outProjMatrix;\n"
  87.     "layout (location = 5) out mat4 outViewMatrix;\n"
  88.     "layout (location = 9) out mat4 outUndistortion;\n"
  89.     "vec4 undistort(vec4 pos) {\n"
  90.     "    float maxRadiusSquared = 100.0;\n"
  91.     "    pos = myUniformValues.modelViewMatrix * pos;\n"
  92.     "    float r2 = clamp(dot(pos.xy, pos.xy) / (pos.z * pos.z), 0, maxRadiusSquared);\n"
  93.     "    pos.xy *= 1 + (myUniformValues.undistortion.x + myUniformValues.undistortion.y * r2) * r2;\n"
  94.     "    return pos;\n"
  95.     "}\n"
  96.     "void main() {\n"
  97.     "   outColor = inColor;\n"
  98.     "   outProjMatrix = myUniformValues.projectionMatrix;\n"
  99.     "   outViewMatrix = myUniformValues.modelViewMatrix;\n"
  100.     "   outUndistortion = myUniformValues.undistortion;\n"
  101.     "//   gl_Position = myUniformValues.projectionMatrix * undistort(pos);\n"
  102.     "   gl_Position = pos;\n"
  103.     "}\n";
  104.  
  105. static const char* VULKAN_FRAGMENT_SHADER_TEXT =
  106.     "#version 400\n"
  107.     "#extension GL_ARB_separate_shader_objects : enable\n"
  108.     "#extension GL_ARB_shading_language_420pack : enable\n"
  109.     "layout (location = 0) in vec4 inColor;\n"
  110.     "layout (location = 0) out vec4 outColor;\n"
  111.     "void main() {\n"
  112.     "   outColor = inColor;\n"
  113.     "}\n";
  114.  
  115. struct ShaderEntry
  116. {
  117.     std::vector<const char*> vertexShaders;
  118.     std::vector<const char*> fragmentShaders;
  119.     std::vector<const char*> tesselationControlShaders;
  120.     std::vector<const char*> tesselationEvaluationShaders;
  121. };
  122.  
  123. // #REFACTOR
  124. #ifdef __ANDROID__
  125. struct shader_type_mapping {
  126.     VkShaderStageFlagBits vkshader_type;
  127.     shaderc_shader_kind shaderc_type;
  128. };
  129. static const shader_type_mapping shader_map_table[] = {
  130.     {VK_SHADER_STAGE_VERTEX_BIT, shaderc_glsl_vertex_shader},
  131.     {VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, shaderc_glsl_tess_control_shader},
  132.     {VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, shaderc_glsl_tess_evaluation_shader},
  133.     {VK_SHADER_STAGE_GEOMETRY_BIT, shaderc_glsl_geometry_shader},
  134.     {VK_SHADER_STAGE_FRAGMENT_BIT, shaderc_glsl_fragment_shader},
  135.     {VK_SHADER_STAGE_COMPUTE_BIT, shaderc_glsl_compute_shader},
  136. };
  137. #endif
  138.  
  139. VULKAN_NS_END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement