コード例 #1
0
ファイル: shaderc.hpp プロジェクト: antiagainst/shaderc
 // Compiles the given source GLSL and returns a SPIR-V binary module
 // compilation result.
 // Like the first CompileGlslToSpv method but uses default options.
 SpvCompilationResult CompileGlslToSpv(const char* source_text,
                                       size_t source_text_size,
                                       shaderc_shader_kind shader_kind,
                                       const char* input_file_name) const {
     shaderc_compilation_result_t compilation_result =
         shaderc_compile_into_spv(compiler_, source_text, source_text_size,
                                  shader_kind, input_file_name, "main", nullptr);
     return SpvCompilationResult(compilation_result);
 }
コード例 #2
0
ファイル: shaderc.hpp プロジェクト: KHeresy/openvr
 // Compiles the given source GLSL and returns a SPIR-V binary module
 // compilation result.
 // The source_text parameter must be a valid pointer.
 // The source_text_size parameter must be the length of the source text.
 // The shader_kind parameter either forces the compilation to be done with a
 // specified shader kind, or hint the compiler how to determine the exact
 // shader kind. If the shader kind is set to shaderc_glslc_infer_from_source,
 // the compiler will try to deduce the shader kind from the source string and
 // a failure in this proess will generate an error. Currently only #pragma
 // annotation is supported. If the shader kind is set to one of the default
 // shader kinds, the compiler will fall back to the specified default shader
 // kind in case it failed to deduce the shader kind from the source string.
 // The input_file_name is a null-termintated string. It is used as a tag to
 // identify the source string in cases like emitting error messages. It
 // doesn't have to be a 'file name'.
 // The entry_point_name parameter is a null-terminated string specifying
 // the entry point name for HLSL compilation.  For GLSL compilation, the
 // entry point name is assumed to be "main".
 // The compilation is passed any options specified in the CompileOptions
 // parameter.
 // It is valid for the returned CompilationResult object to outlive this
 // compiler object.
 // Note when the options_ has disassembly mode or preprocessing only mode set
 // on, the returned CompilationResult will hold a text string, instead of a
 // SPIR-V binary generated with default options.
 SpvCompilationResult CompileGlslToSpv(const char* source_text,
                                       size_t source_text_size,
                                       shaderc_shader_kind shader_kind,
                                       const char* input_file_name,
                                       const char* entry_point_name,
                                       const CompileOptions& options) const {
   shaderc_compilation_result_t compilation_result = shaderc_compile_into_spv(
       compiler_, source_text, source_text_size, shader_kind, input_file_name,
       entry_point_name, options.options_);
   return SpvCompilationResult(compilation_result);
 }
コード例 #3
0
bool GrVkPipelineStateBuilder::CreateVkShaderModule(const GrVkGpu* gpu,
                                                    VkShaderStageFlagBits stage,
                                                    const GrGLSLShaderBuilder& builder,
                                                    VkShaderModule* shaderModule,
                                                    VkPipelineShaderStageCreateInfo* stageInfo) {
    SkString shaderString;
    for (int i = 0; i < builder.fCompilerStrings.count(); ++i) {
        if (builder.fCompilerStrings[i]) {
            shaderString.append(builder.fCompilerStrings[i]);
            shaderString.append("\n");
        }
    }

    VkShaderModuleCreateInfo moduleCreateInfo;
    memset(&moduleCreateInfo, 0, sizeof(VkShaderModuleCreateInfo));
    moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
    moduleCreateInfo.pNext = nullptr;
    moduleCreateInfo.flags = 0;

    shaderc_compilation_result_t result = nullptr;

    if (gpu->vkCaps().canUseGLSLForShaderModule()) {
        moduleCreateInfo.codeSize = strlen(shaderString.c_str());
        moduleCreateInfo.pCode = (const uint32_t*)shaderString.c_str();
    } else {

        shaderc_compiler_t compiler = gpu->shadercCompiler();

        shaderc_compile_options_t options = shaderc_compile_options_initialize();

        shaderc_shader_kind shadercStage = vk_shader_stage_to_shaderc_kind(stage);
        result = shaderc_compile_into_spv(compiler,
                                          shaderString.c_str(),
                                          strlen(shaderString.c_str()),
                                          shadercStage,
                                          "shader",
                                          "main",
                                          options);
        shaderc_compile_options_release(options);
#ifdef SK_DEBUG
        if (shaderc_result_get_num_errors(result)) {
            SkDebugf("%s\n", shaderString.c_str());
            SkDebugf("%s\n", shaderc_result_get_error_message(result));
            return false;
        }
#endif

        moduleCreateInfo.codeSize = shaderc_result_get_length(result);
        moduleCreateInfo.pCode = (const uint32_t*)shaderc_result_get_bytes(result);
    }

    VkResult err = GR_VK_CALL(gpu->vkInterface(), CreateShaderModule(gpu->device(),
                                                                     &moduleCreateInfo,
                                                                     nullptr,
                                                                     shaderModule));
    if (!gpu->vkCaps().canUseGLSLForShaderModule()) {
        shaderc_result_release(result);
    }
    if (err) {
        return false;
    }

    memset(stageInfo, 0, sizeof(VkPipelineShaderStageCreateInfo));
    stageInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
    stageInfo->pNext = nullptr;
    stageInfo->flags = 0;
    stageInfo->stage = stage;
    stageInfo->module = *shaderModule;
    stageInfo->pName = "main";
    stageInfo->pSpecializationInfo = nullptr;

    return true;
}