]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/module.h
Fix reflection of image types from Spir-V modules
[libs/gl.git] / source / core / module.h
index b1c0f8abebda67ac56742507277bae9ccb92af22..c669af0506ab64d26b25bf504ec28533e7e1cf92 100644 (file)
 #ifndef MSP_GL_MODULE_H_
 #define MSP_GL_MODULE_H_
 
+#include <map>
 #include <string>
+#include <vector>
 #include <msp/io/base.h>
+#include "datatype.h"
+#include "module_backend.h"
+#include "glsl/compiler.h"
 #include "glsl/sourcemap.h"
 
 namespace Msp {
 namespace GL {
 
+class invalid_module: public std::runtime_error
+{
+public:
+       invalid_module(const std::string &w): runtime_error(w) { }
+       virtual ~invalid_module() throw() { }
+};
+
 class Resources;
 
+/**
+Base class for shader modules.  Internal representation depends on the
+concrete type.
+
+Modules can be loaded from files.
+
+Applications normally use the Program class to access shaders.
+*/
 class Module
 {
+public:
+       enum Format
+       {
+               GLSL,
+               SPIR_V
+       };
+
+protected:
+       Module() = default;
+public:
+       virtual ~Module() = default;
+
+       virtual Format get_format() const = 0;
+
+       /** Sets the module's content from GLSL source code. */
+       void set_source(const std::string &);
+
+       /** Loads GLSL source from a file or other I/O object.  Any import
+       statements are resolved using res. */
+       void load_source(IO::Base &, Resources *res, const std::string &name);
+
+       /** Loads GLSL source from a file or other I/O object.  Only builtin
+       shader fragments can be imported. */
+       void load_source(IO::Base &, const std::string &);
+
+private:
+       virtual void compile(SL::Compiler &) = 0;
+
+       SL::Features create_features() const;
+};
+
+/**
+A shader module in GLSL source code format.
+*/
+class GlslModule: public Module
+{
 private:
        std::string prepared_source;
        SL::SourceMap source_map;
 
 public:
-       Module();
+       virtual Format get_format() const { return GLSL; }
 
-       void set_source(const std::string &);
-       void load_source(IO::Base &, Resources *, const std::string &);
-       void load_source(IO::Base &, const std::string &);
+private:
+       virtual void compile(SL::Compiler &);
 
+public:
        const std::string &get_prepared_source() const { return prepared_source; }
        const SL::SourceMap &get_source_map() const { return source_map; }
 };
 
+/**
+A shader module in SPIR-V binary format.
+
+When the module's contents are set from GLSL source, it will be automatically
+compiled to SPIR-V.  Pre-compiled SPIR-V modules can also be loaded.
+
+Afterwards reflection data is available, providing information about variables
+forming the module's interface.
+*/
+class SpirVModule: public Module, public SpirVModuleBackend
+{
+public:
+       enum Stage
+       {
+               VERTEX = 0,
+               GEOMETRY = 3,
+               FRAGMENT = 4
+       };
+
+       enum StorageClass
+       {
+               UNIFORM_CONSTANT = 0,
+               INPUT = 1,
+               UNIFORM = 2,
+               OUTPUT = 3,
+               PUSH_CONSTANT = 9
+       };
+
+       enum BuiltinSemantic
+       {
+               NOT_BUILTIN = -1,
+               POSITION = 0,
+               CLIP_DISTANCE = 3,
+               LAYER = 9,
+               FRAG_DEPTH = 22
+       };
+
+       struct Constant;
+       struct Structure;
+       struct Variable;
+
+       struct EntryPoint
+       {
+               std::string name;
+               Stage stage = VERTEX;
+               std::vector<const Variable *> globals;
+       };
+
+       struct StructMember
+       {
+               std::string name;
+               DataType type = VOID;
+               const Structure *struct_type = 0;
+               unsigned offset = 0;
+               unsigned array_size = 0;
+               unsigned array_stride = 0;
+               unsigned matrix_stride = 0;
+               BuiltinSemantic builtin = NOT_BUILTIN;
+       };
+
+       struct Structure
+       {
+               std::string name;
+               std::vector<StructMember> members;
+               unsigned size = 0;
+       };
+
+       struct Variable
+       {
+               std::string name;
+               StorageClass storage = static_cast<StorageClass>(-1);
+               DataType type = VOID;
+               const Structure *struct_type = 0;
+               unsigned array_size = 0;
+               int location = -1;
+               unsigned descriptor_set = 0;
+               int binding = -1;
+               BuiltinSemantic builtin = NOT_BUILTIN;
+
+               bool operator==(const Variable &) const;
+       };
+
+       struct Constant
+       {
+               std::string name;
+               int constant_id = -1;
+               DataType type = VOID;
+               union
+               {
+                       int i_value = 0;
+                       float f_value;
+               };
+       };
+
+private:
+       struct TypeInfo
+       {
+               DataType type = VOID;
+               const Structure *struct_type = 0;
+               unsigned array_size = 0;
+               unsigned array_stride = 0;
+               StorageClass storage = static_cast<StorageClass>(-1);
+       };
+
+       struct Reflection
+       {
+               typedef std::vector<std::uint32_t>::const_iterator CodeIterator;
+
+               std::map<unsigned, std::string> names;
+               std::map<unsigned, Constant> constants;
+               std::map<unsigned, TypeInfo> types;
+               std::map<unsigned, EntryPoint> entry_points;
+               std::map<unsigned, Structure> structs;
+               std::map<unsigned, Variable> variables;
+
+               static std::uint32_t get_opcode(std::uint32_t);
+               static CodeIterator get_op_end(const CodeIterator &);
+               static std::string read_string(CodeIterator &, const CodeIterator &);
+
+               void reflect_code(const std::vector<std::uint32_t> &);
+               void reflect_name(CodeIterator);
+               void reflect_member_name(CodeIterator);
+               void reflect_entry_point(CodeIterator);
+               void reflect_void_type(CodeIterator);
+               void reflect_bool_type(CodeIterator);
+               void reflect_int_type(CodeIterator);
+               void reflect_float_type(CodeIterator);
+               void reflect_vector_type(CodeIterator);
+               void reflect_matrix_type(CodeIterator);
+               void reflect_image_type(CodeIterator);
+               void reflect_sampled_image_type(CodeIterator);
+               void reflect_array_type(CodeIterator);
+               void reflect_struct_type(CodeIterator);
+               void reflect_pointer_type(CodeIterator);
+               void reflect_constant(CodeIterator);
+               void reflect_variable(CodeIterator);
+               void reflect_decorate(CodeIterator);
+               void reflect_member_decorate(CodeIterator);
+       };
+
+       std::vector<std::uint32_t> code;
+       std::vector<EntryPoint> entry_points;
+       std::vector<Structure> structs;
+       std::vector<Variable> variables;
+       std::vector<Constant> spec_constants;
+
+public:
+       virtual Format get_format() const { return SPIR_V; }
+
+       /** Loads a SPIR-V binary from a file or other I/O object. */
+       void load_code(IO::Base &);
+private:
+       virtual void compile(SL::Compiler &);
+       void reflect();
+
+public:
+       const std::vector<std::uint32_t> &get_code() const { return code; }
+       const std::vector<EntryPoint> &get_entry_points() const { return entry_points; }
+       const std::vector<Variable> &get_variables() const { return variables; }
+       const std::vector<Constant> &get_spec_constants() const { return spec_constants; }
+};
+
 } // namespace GL
 } // namespace Msp