yum-mirror/glslang
git clone https://git.yummers.dev/yum-mirror/glslang
104bd85d
main
1// 2// Copyright (C) 2025 The Khronos Group Inc. 3// All rights reserved. 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions 7// are met: 8// 9// Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// 12// Redistributions in binary form must reproduce the above 13// copyright notice, this list of conditions and the following 14// disclaimer in the documentation and/or other materials provided 15// with the distribution. 16// 17// Neither the name of 3Dlabs Inc. Ltd. nor the names of its 18// contributors may be used to endorse or promote products derived 19// from this software without specific prior written permission. 20// 21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32// POSSIBILITY OF SUCH DAMAGE. 33// 34 35#include <stdint.h> 36#include <stdio.h> 37 38#include <glslang/Include/glslang_c_interface.h> 39 40// Required for use of glslang_default_resource 41#include <glslang/Public/resource_limits_c.h> 42 43typedef struct SpirVBinary { 44uint32_t * words ;// SPIR-V words 45int size ;// number of words in SPIR-V binary 46}SpirVBinary ; 47 48SpirVBinary compileShaderToSPIRV_Vulkan (glslang_stage_t stage ,const char * shaderSource ,const char * fileName ) 49{ 50const glslang_input_t input = { 51 .language = GLSLANG_SOURCE_GLSL , 52 .stage = stage , 53 .client = GLSLANG_CLIENT_VULKAN , 54 .client_version = GLSLANG_TARGET_VULKAN_1_2 , 55 .target_language = GLSLANG_TARGET_SPV , 56 .target_language_version = GLSLANG_TARGET_SPV_1_5 , 57 .code = shaderSource , 58 .default_version = 100 , 59 .default_profile = GLSLANG_NO_PROFILE , 60 .force_default_version_and_profile = false, 61 .forward_compatible = false, 62 .messages = GLSLANG_MSG_DEFAULT_BIT , 63 .resource = glslang_default_resource (), 64 }; 65 66glslang_shader_t * shader = glslang_shader_create (& input ); 67 68SpirVBinary bin = { 69 .words = NULL , 70 .size = 0 , 71 }; 72if (!glslang_shader_preprocess (shader ,& input )) { 73printf ("GLSL preprocessing failed %s\n" ,fileName ); 74printf ("%s\n" ,glslang_shader_get_info_log (shader )); 75printf ("%s\n" ,glslang_shader_get_info_debug_log (shader )); 76printf ("%s\n" ,input .code ); 77glslang_shader_delete (shader ); 78return bin ; 79 } 80 81if (!glslang_shader_parse (shader ,& input )) { 82printf ("GLSL parsing failed %s\n" ,fileName ); 83printf ("%s\n" ,glslang_shader_get_info_log (shader )); 84printf ("%s\n" ,glslang_shader_get_info_debug_log (shader )); 85printf ("%s\n" ,glslang_shader_get_preprocessed_code (shader )); 86glslang_shader_delete (shader ); 87return bin ; 88 } 89 90glslang_program_t * program = glslang_program_create (); 91glslang_program_add_shader (program ,shader ); 92 93if (!glslang_program_link (program ,GLSLANG_MSG_SPV_RULES_BIT |GLSLANG_MSG_VULKAN_RULES_BIT )) { 94printf ("GLSL linking failed %s\n" ,fileName ); 95printf ("%s\n" ,glslang_program_get_info_log (program )); 96printf ("%s\n" ,glslang_program_get_info_debug_log (program )); 97glslang_program_delete (program ); 98glslang_shader_delete (shader ); 99return bin ; 100 } 101 102glslang_program_SPIRV_generate (program ,stage ); 103 104bin .size = glslang_program_SPIRV_get_size (program ); 105bin .words = malloc (bin .size * sizeof (uint32_t )); 106glslang_program_SPIRV_get (program ,bin .words ); 107 108const char * spirv_messages = glslang_program_SPIRV_get_messages (program ); 109if (spirv_messages ) 110printf ("(%s) %s\b" ,fileName ,spirv_messages ); 111 112glslang_program_delete (program ); 113glslang_shader_delete (shader ); 114 115return bin ; 116} 117 118int main () {return 0 ; }