floooh / sokol-tools

Command line tools for use with sokol headers
MIT License
229 stars 57 forks source link

use option --reflection --format sokol_impl together problem #72

Closed likun123687 closed 2 years ago

likun123687 commented 2 years ago

my glsl file:

@module ant2d
@vs vs
in vec4 position;

void main() {
    gl_Position = vec4(position.x, position.y, position.z, 1.0);
}
@end

@fs fs
uniform fs_params {
    vec4 ourColor;
};

out vec4 FragColor;

void main() {
    FragColor = ourColor;
}
@end

@program simple vs fs

i use the command: sokol-shdc --input .\2-uniforms.glsl --output uniforms3.glsl.h --slang hlsl5 --reflection --format sokol_impl

the generate content:

/*
    #version:1# (machine generated, don't edit!)

    Generated by sokol-shdc (https://github.com/floooh/sokol-tools)

    Cmdline: sokol-shdc --input .\2-uniforms.glsl --output uniforms3.glsl.h --slang hlsl5 --reflection --format sokol_impl

    Overview:

        Shader program 'simple':
            Get shader desc: ant2d_simple_shader_desc(sg_query_backend());
            Vertex shader: vs
                Attribute slots:
                    ATTR_ant2d_vs_position = 0
            Fragment shader: fs
                Uniform block 'fs_params':
                    C struct: ant2d_fs_params_t
                    Bind slot: SLOT_ant2d_fs_params = 0

    Shader descriptor structs:

        sg_shader simple = sg_make_shader(ant2d_simple_shader_desc(sg_query_backend()));

    Vertex attribute locations for vertex shader 'vs':

        sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
            .layout = {
                .attrs = {
                    [ATTR_ant2d_vs_position] = { ... },
                },
            },
            ...});

    Image bind slots, use as index in sg_bindings.vs_images[] or .fs_images[]

    Bind slot and C-struct for uniform block 'fs_params':

        ant2d_fs_params_t fs_params = {
            .ourColor = ...;
        };
        sg_apply_uniforms(SG_SHADERSTAGE_[VS|FS], SLOT_ant2d_fs_params, &SG_RANGE(fs_params));

*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stddef.h>
#if !defined(SOKOL_GFX_INCLUDED)
  #error "Please include sokol_gfx.h before uniforms3.glsl.h"
#endif
#if !defined(SOKOL_SHDC_ALIGN)
  #if defined(_MSC_VER)
    #define SOKOL_SHDC_ALIGN(a) __declspec(align(a))
  #else
    #define SOKOL_SHDC_ALIGN(a) __attribute__((aligned(a)))
  #endif
#endif
const sg_shader_desc* ant2d_simple_shader_desc(sg_backend backend);
#define ATTR_ant2d_vs_position (0)
#define SLOT_ant2d_fs_params (0)
#pragma pack(push,1)
SOKOL_SHDC_ALIGN(16) typedef struct ant2d_fs_params_t {
    float ourColor[4];
} ant2d_fs_params_t;
#pragma pack(pop)
#if defined(SOKOL_SHDC_IMPL)
/*
    static float4 gl_Position;
    static float4 position;

    struct SPIRV_Cross_Input
    {
        float4 position : TEXCOORD0;
    };

    struct SPIRV_Cross_Output
    {
        float4 gl_Position : SV_Position;
    };

    #line 9 ".\2-uniforms.glsl"
    void vert_main()
    {
    #line 9 ".\2-uniforms.glsl"
        gl_Position = float4(position.x, position.y, position.z, 1.0f);
    }

    SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input)
    {
        position = stage_input.position;
        vert_main();
        SPIRV_Cross_Output stage_output;
        stage_output.gl_Position = gl_Position;
        return stage_output;
    }
*/
static const char ant2d_vs_source_hlsl5[552] = {
    0x73,0x74,0x61,0x74,0x69,0x63,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x67,0x6c,
    0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x73,0x74,0x61,0x74,0x69,
    0x63,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,
    0x6e,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x53,0x50,0x49,0x52,0x56,
    0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x49,0x6e,0x70,0x75,0x74,0x0a,0x7b,0x0a,0x20,
    0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x70,0x6f,0x73,0x69,0x74,0x69,
    0x6f,0x6e,0x20,0x3a,0x20,0x54,0x45,0x58,0x43,0x4f,0x4f,0x52,0x44,0x30,0x3b,0x0a,
    0x7d,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,0x63,0x74,0x20,0x53,0x50,0x49,0x52,0x56,
    0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x4f,0x75,0x74,0x70,0x75,0x74,0x0a,0x7b,0x0a,
    0x20,0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x67,0x6c,0x5f,0x50,0x6f,
    0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x3a,0x20,0x53,0x56,0x5f,0x50,0x6f,0x73,0x69,
    0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x7d,0x3b,0x0a,0x0a,0x23,0x6c,0x69,0x6e,0x65,0x20,
    0x39,0x20,0x22,0x2e,0x5c,0x32,0x2d,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x73,0x2e,
    0x67,0x6c,0x73,0x6c,0x22,0x0a,0x76,0x6f,0x69,0x64,0x20,0x76,0x65,0x72,0x74,0x5f,
    0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x23,0x6c,0x69,0x6e,0x65,0x20,0x39,
    0x20,0x22,0x2e,0x5c,0x32,0x2d,0x75,0x6e,0x69,0x66,0x6f,0x72,0x6d,0x73,0x2e,0x67,
    0x6c,0x73,0x6c,0x22,0x0a,0x20,0x20,0x20,0x20,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,
    0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x28,0x70,0x6f,
    0x73,0x69,0x74,0x69,0x6f,0x6e,0x2e,0x78,0x2c,0x20,0x70,0x6f,0x73,0x69,0x74,0x69,
    0x6f,0x6e,0x2e,0x79,0x2c,0x20,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x2e,0x7a,
    0x2c,0x20,0x31,0x2e,0x30,0x66,0x29,0x3b,0x0a,0x7d,0x0a,0x0a,0x53,0x50,0x49,0x52,
    0x56,0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x4f,0x75,0x74,0x70,0x75,0x74,0x20,0x6d,
    0x61,0x69,0x6e,0x28,0x53,0x50,0x49,0x52,0x56,0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,
    0x49,0x6e,0x70,0x75,0x74,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x69,0x6e,0x70,0x75,
    0x74,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,
    0x6e,0x20,0x3d,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x69,0x6e,0x70,0x75,0x74,0x2e,
    0x70,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x20,0x20,0x20,0x20,0x76,0x65,
    0x72,0x74,0x5f,0x6d,0x61,0x69,0x6e,0x28,0x29,0x3b,0x0a,0x20,0x20,0x20,0x20,0x53,
    0x50,0x49,0x52,0x56,0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x4f,0x75,0x74,0x70,0x75,
    0x74,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x6f,0x75,0x74,0x70,0x75,0x74,0x3b,0x0a,
    0x20,0x20,0x20,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x6f,0x75,0x74,0x70,0x75,0x74,
    0x2e,0x67,0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x20,0x3d,0x20,0x67,
    0x6c,0x5f,0x50,0x6f,0x73,0x69,0x74,0x69,0x6f,0x6e,0x3b,0x0a,0x20,0x20,0x20,0x20,
    0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x6f,0x75,0x74,
    0x70,0x75,0x74,0x3b,0x0a,0x7d,0x0a,0x00,
};
/*
    cbuffer fs_params : register(b0)
    {
        float4 _14_ourColor : packoffset(c0);
    };

    static float4 FragColor;

    struct SPIRV_Cross_Output
    {
        float4 FragColor : SV_Target0;
    };

    #line 13 ".\2-uniforms.glsl"
    void frag_main()
    {
    #line 13 ".\2-uniforms.glsl"
        FragColor = _14_ourColor;
    }

    SPIRV_Cross_Output main()
    {
        frag_main();
        SPIRV_Cross_Output stage_output;
        stage_output.FragColor = FragColor;
        return stage_output;
    }
*/
static const char ant2d_fs_source_hlsl5[435] = {
    0x63,0x62,0x75,0x66,0x66,0x65,0x72,0x20,0x66,0x73,0x5f,0x70,0x61,0x72,0x61,0x6d,
    0x73,0x20,0x3a,0x20,0x72,0x65,0x67,0x69,0x73,0x74,0x65,0x72,0x28,0x62,0x30,0x29,
    0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,0x5f,0x31,
    0x34,0x5f,0x6f,0x75,0x72,0x43,0x6f,0x6c,0x6f,0x72,0x20,0x3a,0x20,0x70,0x61,0x63,
    0x6b,0x6f,0x66,0x66,0x73,0x65,0x74,0x28,0x63,0x30,0x29,0x3b,0x0a,0x7d,0x3b,0x0a,
    0x0a,0x0a,0x73,0x74,0x61,0x74,0x69,0x63,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x20,
    0x46,0x72,0x61,0x67,0x43,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x0a,0x73,0x74,0x72,0x75,
    0x63,0x74,0x20,0x53,0x50,0x49,0x52,0x56,0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x4f,
    0x75,0x74,0x70,0x75,0x74,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x6c,0x6f,0x61,
    0x74,0x34,0x20,0x46,0x72,0x61,0x67,0x43,0x6f,0x6c,0x6f,0x72,0x20,0x3a,0x20,0x53,
    0x56,0x5f,0x54,0x61,0x72,0x67,0x65,0x74,0x30,0x3b,0x0a,0x7d,0x3b,0x0a,0x0a,0x23,
    0x6c,0x69,0x6e,0x65,0x20,0x31,0x33,0x20,0x22,0x2e,0x5c,0x32,0x2d,0x75,0x6e,0x69,
    0x66,0x6f,0x72,0x6d,0x73,0x2e,0x67,0x6c,0x73,0x6c,0x22,0x0a,0x76,0x6f,0x69,0x64,
    0x20,0x66,0x72,0x61,0x67,0x5f,0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x23,
    0x6c,0x69,0x6e,0x65,0x20,0x31,0x33,0x20,0x22,0x2e,0x5c,0x32,0x2d,0x75,0x6e,0x69,
    0x66,0x6f,0x72,0x6d,0x73,0x2e,0x67,0x6c,0x73,0x6c,0x22,0x0a,0x20,0x20,0x20,0x20,
    0x46,0x72,0x61,0x67,0x43,0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x5f,0x31,0x34,0x5f,
    0x6f,0x75,0x72,0x43,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x7d,0x0a,0x0a,0x53,0x50,0x49,
    0x52,0x56,0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x4f,0x75,0x74,0x70,0x75,0x74,0x20,
    0x6d,0x61,0x69,0x6e,0x28,0x29,0x0a,0x7b,0x0a,0x20,0x20,0x20,0x20,0x66,0x72,0x61,
    0x67,0x5f,0x6d,0x61,0x69,0x6e,0x28,0x29,0x3b,0x0a,0x20,0x20,0x20,0x20,0x53,0x50,
    0x49,0x52,0x56,0x5f,0x43,0x72,0x6f,0x73,0x73,0x5f,0x4f,0x75,0x74,0x70,0x75,0x74,
    0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x6f,0x75,0x74,0x70,0x75,0x74,0x3b,0x0a,0x20,
    0x20,0x20,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x6f,0x75,0x74,0x70,0x75,0x74,0x2e,
    0x46,0x72,0x61,0x67,0x43,0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x46,0x72,0x61,0x67,
    0x43,0x6f,0x6c,0x6f,0x72,0x3b,0x0a,0x20,0x20,0x20,0x20,0x72,0x65,0x74,0x75,0x72,
    0x6e,0x20,0x73,0x74,0x61,0x67,0x65,0x5f,0x6f,0x75,0x74,0x70,0x75,0x74,0x3b,0x0a,
    0x7d,0x0a,0x00,
};
const sg_shader_desc* ant2d_simple_shader_desc(sg_backend backend) {
  if (backend == SG_BACKEND_D3D11) {
    static sg_shader_desc desc;
    static bool valid;
    if (!valid) {
      valid = true;
      desc.attrs[0].sem_name = "TEXCOORD";
      desc.attrs[0].sem_index = 0;
      desc.vs.source = ant2d_vs_source_hlsl5;
      desc.vs.d3d11_target = "vs_5_0";
      desc.vs.entry = "main";
      desc.fs.source = ant2d_fs_source_hlsl5;
      desc.fs.d3d11_target = "ps_5_0";
      desc.fs.entry = "main";
      desc.fs.uniform_blocks[0].size = 16;
      desc.fs.uniform_blocks[0].layout = SG_UNIFORMLAYOUT_STD140;
      desc.label = "ant2d_simple_shader";
    }
    return &desc;
  }
  return 0;
}
int ant2d_simple_attr_slot(const char* attr_name) {
  (void)attr_name;
  if (0 == strcmp(attr_name, "position")) {
    return 0;
  }
  return -1;
}
int ant2d_simple_image_slot(sg_shader_stage stage, const char* img_name) {
  (void)stage; (void)img_name;
  return -1;
}
int ant2d_simple_uniformblock_slot(sg_shader_stage stage, const char* ub_name) {
  (void)stage; (void)ub_name;
  if (SG_SHADERSTAGE_FS == stage) {
    if (0 == strcmp(ub_name, "fs_params")) {
      return 0;
    }
  }
  return -1;
}
size_t ant2d_simple_uniformblock_size(sg_shader_stage stage, const char* ub_name) {
  (void)stage; (void)ub_name;
  if (SG_SHADERSTAGE_FS == stage) {
    if (0 == strcmp(ub_name, "fs_params")) {
      return sizeof(ant2d_fs_params_t);
    }
  }
  return 0;
}
int ant2d_simple_uniform_offset(sg_shader_stage stage, const char* ub_name, const char* u_name) {
  (void)stage; (void)ub_name; (void)u_name;
  if (SG_SHADERSTAGE_FS == stage) {
    if (0 == strcmp(ub_name, "fs_params")) {
      if (0 == strcmp(u_name, "ourColor")) {
        return 0;
      }
    }
  }
  return -1;
}
sg_shader_uniform_desc ant2d_simple_uniform_desc(sg_shader_stage stage, const char* ub_name, const char* u_name) {
  (void)stage; (void)ub_name; (void)u_name;
  #if defined(__cplusplus)
  sg_shader_uniform_desc desc = {};
  #else
  sg_shader_uniform_desc desc = {0};
  #endif
  if (SG_SHADERSTAGE_FS == stage) {
    if (0 == strcmp(ub_name, "fs_params")) {
      if (0 == strcmp(u_name, "ourColor")) {
        desc.name = "ourColor";
        desc.type = SG_UNIFORMTYPE_FLOAT4;
        desc.array_count = 1;
        return desc;
      }
    }
  }
  return desc;
}
#endif /* SOKOL_SHDC_IMPL */

the Declarations part only contain shader_desc function,the Runtime Inspection function is not found.Did i miss something?

floooh commented 2 years ago

No, your're right. That's an oversight. I'll start working on a fix.

floooh commented 2 years ago

Ok, this should be fixed, I tested in the sokol_samples by switching the shdfeatures-sapp sample temporarily over to sokol_impl output. The declaration block then contains the additional function protos:

#endif
const sg_shader_desc* slm_prog_shader_desc(sg_backend backend);
int slm_prog_attr_slot(const char* attr_name);
int slm_prog_image_slot(sg_shader_stage stage, const char* img_name);
int slm_prog_uniformblock_slot(sg_shader_stage stage, const char* ub_name);
size_t slm_prog_uniformblock_size(sg_shader_stage stage, const char* ub_name);
int slm_prog_uniform_offset(sg_shader_stage stage, const char* ub_name, const char* u_name);
sg_shader_uniform_desc slm_prog_uniform_desc(sg_shader_stage stage, const char* ub_name, const char* u_name);
#define ATTR_slm_vs_position (0)

I'll start a GH Action run to build and commit the updated executables, ETA 30 minutes.

floooh commented 2 years ago

GH Action for the new binaries: https://github.com/floooh/sokol-tools/actions/runs/2282914956, once that has finished you can give it a try.

(if you're using fips, just run ./fips update in the main project and it pulls the latest changes to the sokol-tools-bin repository).

likun123687 commented 2 years ago

Thank you, solve my problem