shaderModule.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file shaderModule.h
  10. * @author Mitchell Stokes
  11. * @date 2019-02-16
  12. */
  13. #ifndef SHADERMODULE_H
  14. #define SHADERMODULE_H
  15. #include "copyOnWriteObject.h"
  16. #include "bamCacheRecord.h"
  17. #include "shaderType.h"
  18. #include "internalName.h"
  19. /**
  20. * Represents a single shader module in some intermediate representation for
  21. * passing to the driver. This could contain compiled bytecode, or in some
  22. * cases, preprocessed source code to be given directly to the driver.
  23. *
  24. * This class inherits from CopyOnWriteObject so that modules can safely be
  25. * shared between multiple Shader objects, with a unique copy automatically
  26. * being created if the Shader needs to manipulate the module.
  27. */
  28. class EXPCL_PANDA_GOBJ ShaderModule : public CopyOnWriteObject {
  29. PUBLISHED:
  30. enum class Stage {
  31. vertex,
  32. tess_control,
  33. tess_evaluation,
  34. geometry,
  35. fragment,
  36. compute,
  37. };
  38. /**
  39. * Defines an interface variable.
  40. */
  41. struct Variable {
  42. public:
  43. int has_location() const { return _location >= 0; }
  44. int get_location() const { return _location; }
  45. PUBLISHED:
  46. const ShaderType *type;
  47. CPT(InternalName) name;
  48. MAKE_PROPERTY2(location, has_location, get_location);
  49. public:
  50. int _location;
  51. };
  52. public:
  53. ShaderModule(Stage stage);
  54. virtual ~ShaderModule();
  55. INLINE Stage get_stage() const;
  56. INLINE int get_used_capabilities() const;
  57. INLINE const Filename &get_source_filename() const;
  58. INLINE void set_source_filename(const Filename &);
  59. size_t get_num_inputs() const;
  60. const Variable &get_input(size_t i) const;
  61. int find_input(CPT_InternalName name) const;
  62. size_t get_num_outputs() const;
  63. const Variable &get_output(size_t i) const;
  64. int find_output(CPT_InternalName name) const;
  65. size_t get_num_parameters() const;
  66. const Variable &get_parameter(size_t i) const;
  67. int find_parameter(CPT_InternalName name) const;
  68. typedef pmap<CPT_InternalName, Variable *> VariablesByName;
  69. virtual bool link_inputs(const ShaderModule *previous);
  70. virtual void remap_parameter_locations(pmap<int, int> &remap);
  71. PUBLISHED:
  72. MAKE_PROPERTY(stage, get_stage);
  73. MAKE_SEQ_PROPERTY(inputs, get_num_inputs, get_input);
  74. MAKE_SEQ_PROPERTY(outputs, get_num_outputs, get_output);
  75. virtual std::string get_ir() const=0;
  76. public:
  77. /**
  78. * Indicates which features are used by the shader, which can be used by the
  79. * driver to check whether cross-compilation is possible, or whether certain
  80. * transformation steps may need to be applied.
  81. */
  82. enum Capabilities : uint64_t {
  83. C_basic_shader = 1 << 0,
  84. C_vertex_texture = 1 << 1,
  85. C_sampler_shadow = 1 << 2, // 1D and 2D only
  86. // GLSL 1.20
  87. C_invariant = 1 << 3,
  88. C_matrix_non_square = 1 << 4,
  89. // GLSL 1.30
  90. C_integer = 1 << 5,
  91. C_texture_lod = 1 << 6, // textureLod in vshader doesn't count, textureGrad does
  92. C_texture_fetch = 1 << 7, // texelFetch, textureSize, etc.
  93. C_sampler_cube_shadow = 1 << 8,
  94. C_vertex_id = 1 << 9,
  95. C_round_even = 1 << 10, // roundEven function, also in SM 4.0
  96. // GLSL 1.40 / SM 4.0
  97. C_instance_id = 1 << 11,
  98. C_buffer_texture = 1 << 12, // ES 3.20
  99. // GLSL 1.50 / SM 4.0
  100. C_geometry_shader = 1 << 13,
  101. C_primitive_id = 1 << 14,
  102. // GLSL 3.30 / ES 3.00 / SM 4.0
  103. C_bit_encoding = 1 << 15,
  104. // GLSL 4.00 / ES 3.20 / SM 5.0
  105. C_texture_gather = 1 << 16,
  106. C_double = 1 << 17,
  107. C_cube_map_array = 1 << 18,
  108. C_tessellation_shader = 1 << 19,
  109. C_sample_variables = 1 << 20,
  110. C_extended_arithmetic = 1 << 21,
  111. C_texture_query_lod = 1 << 22, // not in ES
  112. // GLSL 4.20 / ES 3.10 / SM 5.0
  113. C_image_load_store = 1 << 23,
  114. // GLSL 4.30 / ES 3.10 / SM 5.0
  115. C_compute_shader = 1 << 24,
  116. C_texture_query_levels = 1 << 25, // not in ES
  117. // GLSL 4.40 / ARB_enhanced_layouts
  118. C_enhanced_layouts = 1 << 26,
  119. // GLSL 4.50
  120. C_derivative_control = 1 << 27,
  121. C_texture_query_samples = 1 << 28,
  122. };
  123. static std::string format_stage(Stage stage);
  124. static void output_capabilities(std::ostream &out, int capabilities);
  125. virtual void output(std::ostream &out) const;
  126. virtual void write_datagram(BamWriter *manager, Datagram &dg) override;
  127. protected:
  128. void fillin(DatagramIterator &scan, BamReader *manager) override;
  129. protected:
  130. Stage _stage;
  131. PT(BamCacheRecord) _record;
  132. //std::pvector<Filename> _source_files;
  133. Filename _source_filename;
  134. //time_t _source_modified = 0;
  135. int _used_caps = 0;
  136. typedef pvector<Variable> Variables;
  137. Variables _inputs;
  138. Variables _outputs;
  139. Variables _parameters;
  140. friend class Shader;
  141. public:
  142. static TypeHandle get_class_type() {
  143. return _type_handle;
  144. }
  145. static void init_type() {
  146. TypedWritableReferenceCount::init_type();
  147. register_type(_type_handle, "ShaderModule",
  148. TypedWritableReferenceCount::get_class_type());
  149. }
  150. virtual TypeHandle get_type() const override {
  151. return get_class_type();
  152. }
  153. virtual TypeHandle force_init_type() override {
  154. init_type();
  155. return get_class_type();
  156. }
  157. private:
  158. static TypeHandle _type_handle;
  159. };
  160. INLINE std::ostream &operator << (std::ostream &out, const ShaderModule &module) {
  161. module.output(out);
  162. return out;
  163. }
  164. INLINE std::ostream &operator << (std::ostream &out, ShaderModule::Stage stage) {
  165. return out << ShaderModule::format_stage(stage);
  166. }
  167. #include "shaderModule.I"
  168. #endif