Path: blob/main/libshaderc/src/common_shaders_for_test.h
1558 views
// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef COMMON_SHADERS_FOR_TESTS_H_15#define COMMON_SHADERS_FOR_TESTS_H_1617#ifdef __cplusplus18extern "C" {19#endif2021// The minimal shader, without a version directive.22const char kMinimalShaderWithoutVersion[] = "void main(){}";23// The minimal shader, with a version directive.24const char kMinimalShader[] =25"#version 140\n"26"void main(){}";27const char kMinimalHlslShader[] =28"float4 EntryPoint(uint index : SV_VERTEXID) : SV_POSITION\n"29"{ return float4(1.0, 2.0, 3.0, 4.0); }";30const char kMinimalShaderWithMacro[] =31"#version 140\n"32"#define E main\n"33"void E(){}\n";3435// The minimal shader that needs valueless predefinition of 'E' to compile.36const char kValuelessPredefinitionShader[] =37"#version 140\n"38"#ifdef E\n"39"void main(){}\n"40"#else\n"41"#error\n"42"#endif";4344// By default the compiler will emit a warning on line 2 complaining45// that 'float' is a deprecated attribute in version 130. Use verison 14046// because some versions of glslang will error out for a too-low version47// when generating SPIR-V.48const char kDeprecatedAttributeShader[] =49"#version 400\n"50"layout(location = 0) attribute float x;\n"51"void main() {}\n";5253// By default the compiler will emit a warning as version 550 is an unknown54// version.55const char kMinimalUnknownVersionShader[] =56"#version 550\n"57"void main() {}\n";5859// gl_ClipDistance doesn't exist in es profile (at least until 3.10).60const char kCoreVertShaderWithoutVersion[] =61"void main() {\n"62"gl_ClipDistance[0] = 5.;\n"63"}\n";6465// Generated debug information should contain the name of the vector:66// debug_info_sample.67const char kMinimalDebugInfoShader[] =68"#version 140\n"69"void main(){\n"70"vec2 debug_info_sample = vec2(1.0,1.0);\n"71"}\n";7273// Compiler should generate two errors.74const char kTwoErrorsShader[] =75"#version 150\n"76"#error\n"77"#error\n"78"void main(){}\n";7980// Compiler should generate two warnings.81const char kTwoWarningsShader[] =82"#version 400\n"83"layout(location = 0) attribute float x;\n"84"layout(location = 1) attribute float y;\n"85"void main(){}\n";8687// A shader that compiles under OpenGL compatibility profile rules,88// but not OpenGL core profile rules.89const char kOpenGLCompatibilityFragmentShader[] =90R"(#version 10091uniform highp sampler2D tex;92void main() {93gl_FragColor = texture2D(tex, vec2(0.0,0.0));94})";9596// A shader that compiles under OpenGL core profile rules.97const char kOpenGLVertexShader[] =98R"(#version 33099void main() { int t = gl_VertexID; })";100101// Empty 310 es shader. It is valid for vertex, fragment, compute shader kind.102const char kEmpty310ESShader[] =103"#version 310 es\n"104"void main() {}\n";105106// Vertex only shader.107const char kVertexOnlyShader[] =108"#version 310 es\n"109"void main() {\n"110" gl_Position = vec4(1.);\n"111"}";112113// TessControl only shader.114const char kTessControlOnlyShader[] =115"#version 440 core\n"116"layout(vertices = 3) out;\n"117"void main() { }";118119// TessEvaluation only shader.120const char kTessEvaluationOnlyShader[] =121"#version 440 core\n"122"layout(triangles) in;\n"123"void main() { }";124125// Geometry only shader.126const char kGeometryOnlyShader[] =127"#version 150 core\n"128"layout (triangles) in;\n"129"layout (line_strip, max_vertices = 4) out;\n"130"void main() { }";131132// Vertex only shader with #pragma annotation.133const char kVertexOnlyShaderWithPragma[] =134"#version 310 es\n"135"#pragma shader_stage(vertex)\n"136"void main() {\n"137" gl_Position = vec4(1.);\n"138"}";139140// Fragment only shader with #pragma annotation.141const char kFragmentOnlyShaderWithPragma[] =142"#version 310 es\n"143"#pragma shader_stage(fragment)\n"144"void main() {\n"145" gl_FragDepth = 10.;\n"146"}";147148// TessControl only shader with #pragma annotation.149const char kTessControlOnlyShaderWithPragma[] =150"#version 440 core\n"151"#pragma shader_stage(tesscontrol)\n"152"layout(vertices = 3) out;\n"153"void main() { }";154155// TessEvaluation only shader with #pragma annotation.156const char kTessEvaluationOnlyShaderWithPragma[] =157"#version 440 core\n"158"#pragma shader_stage(tesseval)\n"159"layout(triangles) in;\n"160"void main() { }";161162// Geometry only shader with #pragma annotation.163const char kGeometryOnlyShaderWithPragma[] =164"#version 150 core\n"165"#pragma shader_stage(geometry)\n"166"layout (triangles) in;\n"167"layout (line_strip, max_vertices = 4) out;\n"168"void main() { }";169170// Compute only shader with #pragma annotation.171const char kComputeOnlyShaderWithPragma[] =172"#version 310 es\n"173"#pragma shader_stage(compute)\n"174"void main() {\n"175" uvec3 temp = gl_WorkGroupID;\n"176"}";177178// NV mesh shader without #pragma.179const char kNVMeshShader[] =180"#version 450\n"181"#extension GL_NV_mesh_shader : enable\n"182"layout(local_size_x=8) in;\n"183"layout(max_vertices=5) out;\n"184"layout(max_primitives=10) out;\n"185"layout(triangles) out;\n"186"void main() {\n"187" gl_MeshVerticesNV[gl_LocalInvocationID.x].gl_Position = vec4(0.0);\n"188"}\n";189190// NV mesh shader with #pragma annotation.191const char kNVMeshShaderWithPragma[] =192"#version 450\n"193"#extension GL_NV_mesh_shader : enable\n"194"#pragma shader_stage(mesh)\n"195"layout(local_size_x=8) in;\n"196"layout(max_vertices=5) out;\n"197"layout(max_primitives=10) out;\n"198"layout(triangles) out;\n"199"void main() {\n"200" gl_MeshVerticesNV[gl_LocalInvocationID.x].gl_Position = vec4(0.0);\n"201"}\n";202203// NV task shader without #pragma annotation.204const char kNVTaskShader[] =205"#version 450\n"206"#extension GL_NV_mesh_shader : enable\n"207"layout(local_size_x=8) in;\n"208"void main() {\n"209" gl_TaskCountNV = 2;\n"210"}\n";211212// NV task shader with #pragma annotation.213const char kNVTaskShaderWithPragma[] =214"#version 450\n"215"#extension GL_NV_mesh_shader : enable\n"216"#pragma shader_stage(task)\n"217"layout(local_size_x=8) in;\n"218"void main() {\n"219" gl_TaskCountNV = 2;\n"220"}\n";221222// Vertex only shader with invalid #pragma annotation.223const char kVertexOnlyShaderWithInvalidPragma[] =224"#version 310 es\n"225"#pragma shader_stage(fragment)\n"226"void main() {\n"227" gl_Position = vec4(1.);\n"228"}";229230// Parts of a valid disassembly of a minimal shader. We only check certain231// parts since Glslang code generation changes in incidental ways.232const char* kMinimalShaderDisassemblySubstrings[] = {233"; SPIR-V\n"234"; Version: 1.0\n"235"; Generator: Google Shaderc over Glslang; 11\n"236"; Bound:",237238" OpCapability Shader\n",239" %1 = OpExtInstImport \"GLSL.std.450\"\n",240" OpMemoryModel Logical GLSL450\n",241" OpReturn\n",242" OpFunctionEnd\n"};243244const char* kMinimalShaderDebugInfoDisassemblySubstrings[] = {245"; SPIR-V\n"246"; Version: 1.0\n"247"; Generator: Google Shaderc over Glslang; 11\n"248"; Bound:",249250" OpCapability Shader\n",251" %2 = OpExtInstImport \"GLSL.std.450\"\n",252" OpMemoryModel Logical GLSL450\n",253" OpReturn\n",254" OpFunctionEnd\n"};255256const char kMinimalShaderAssembly[] = R"(257; SPIR-V258; Version: 1.0259; Generator: Google Shaderc over Glslang; 11260; Bound: 6261; Schema: 0262263OpCapability Shader264%1 = OpExtInstImport "GLSL.std.450"265OpMemoryModel Logical GLSL450266OpEntryPoint Vertex %4 "main"267OpSource ESSL 310268OpName %4 "main"269%2 = OpTypeVoid270%3 = OpTypeFunction %2271%4 = OpFunction %2 None %3272%5 = OpLabel273OpReturn274OpFunctionEnd)";275276const char kShaderWithUniformsWithoutBindings[] =277R"(#version 450278#extension GL_ARB_sparse_texture2 : enable279uniform texture2D my_tex;280uniform sampler my_sam;281layout(rgba32f) uniform image2D my_img;282layout(rgba32f) uniform imageBuffer my_imbuf;283uniform block { float x; float y; } my_ubo;284void main() {285texture(sampler2D(my_tex,my_sam),vec2(1.0));286vec4 t;287sparseImageLoadARB(my_img,ivec2(0),t);288imageLoad(my_imbuf,42);289float x = my_ubo.x;290})";291292// A GLSL vertex shader with a weirdly packed block.293const char kGlslShaderWeirdPacking[] =294R"(#version 450295layout(set=0, binding=0)296buffer B { float x; vec3 foo; } my_ssbo;297void main() { my_ssbo.x = 1.0; })";298299// A HLSL fragment shader with a weirdly packed block.300const char kHlslFragShaderWithRegisters[] =301R"(Buffer<float> t4 : register(t4);302Buffer<float> t5 : register(t5);303float4 main() : SV_Target0 {304return float4(t4.Load(0) + t5.Load(1));305})";306307// A GLSL compute shader using a regular barrier.308const char kGlslShaderComputeBarrier[] =309R"(#version 450310void main() { barrier(); })";311312// A GLSL compute shader using the Subgroups feature.313const char kGlslShaderComputeSubgroupBarrier[] =314R"(#version 450315#extension GL_KHR_shader_subgroup_basic : enable316void main() { subgroupBarrier(); })";317318// A GLSL task shader using a regular barrier.319const char kGlslShaderTaskBarrier[] =320R"(#version 450321#extension GL_NV_mesh_shader : enable322layout(local_size_x = 32) in;323void main() { barrier(); })";324325// A GLSL task shader using the Subgroups feature.326const char kGlslShaderTaskSubgroupBarrier[] =327R"(#version 450328#extension GL_NV_mesh_shader : enable329#extension GL_KHR_shader_subgroup_basic : enable330layout(local_size_x = 32) in;331void main() { subgroupBarrier(); })";332333// A GLSL mesh shader using a regular barrier.334const char kGlslShaderMeshBarrier[] =335R"(#version 450336#extension GL_NV_mesh_shader : enable337layout(local_size_x = 32) in;338layout(max_vertices=81) out;339layout(max_primitives=32) out;340layout(triangles) out;341void main() { barrier(); })";342343// A GLSL mesh shader using the Subgroups feature.344const char kGlslShaderMeshSubgroupBarrier[] =345R"(#version 450346#extension GL_NV_mesh_shader : enable347#extension GL_KHR_shader_subgroup_basic : enable348layout(local_size_x = 32) in;349layout(max_vertices=81) out;350layout(max_primitives=32) out;351layout(triangles) out;352void main() { subgroupBarrier(); })";353354const char kGlslMultipleFnShader[] =355R"(#version 450356layout(location=0) flat in int inVal;357layout(location=0) out int outVal;358int foo(int a) { return a; }359void main() { outVal = foo(inVal); })";360361const char kHlslShaderWithCounterBuffer[] =362R"(RWStructuredBuffer<uint> Ainc;363float4 main() : SV_Target0 {364return float4(Ainc.IncrementCounter(), 0, 0, 0);365})";366367const char kHlslWaveActiveSumeComputeShader[] =368R"(struct S { uint val; uint result; };369370[[vk::binding(0,0)]]371RWStructuredBuffer<S> MyBuffer;372373[numthreads(32, 1, 1)]374void main(uint3 id : SV_DispatchThreadID) {375MyBuffer[id.x].result = WaveActiveSum(MyBuffer[id.x].val);376})";377378const char kHlslMemLayoutResourceSelect[] =379R"(cbuffer Foo { float a; float3 b; }380381[[vk::binding(0,0)]]382Texture2D Tex;383[[vk::binding(1,0)]]384SamplerState Sampler1;385[[vk::binding(2,0)]]386SamplerState Sampler2;387388static const int val = 42;389390float4 main() : SV_Target {391SamplerState samp;392393if (val > 5)394samp = Sampler1;395else396samp = Sampler2;397398return Tex.Sample(samp, float2(0.5, 0.5)) + float4(a, b);399})";400401const char kGlslShaderWithClamp[] =402R"(#version 450403layout(location=0) in vec4 i;404layout(location=0) out vec4 o;405void main() { o = clamp(i, vec4(0.5), vec4(1.0)); }406)";407408#ifdef __cplusplus409}410#endif // __cplusplus411412#endif // COMMON_SHADERS_FOR_TESTS_H_413414415