FlatShader.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Shader.hpp"
4 #include "Texture.hpp"
5 #include "CameraState.hpp"
6 #include "GlSystem.hpp"
7 #include "../OpenGL/GlMesh.hpp"
8 #include "../OpenGL/RenderTarget.hpp"
11 
12 namespace iv
13 {
14 
16 {
17 public:
19 
21  void each_resource( std::function< void( ResourcePath const & ) > const & ) const;
22  bool has_resource( ResourcePath const & ) const;
23 
24  //
25  Instance * instance() const;
26 
27 private:
28  Instance * inst;
29 };
30 
31 
33 {
36  bool enabled;
37 
38  //ShaderScissor() : model( ( memset( this, 0, sizeof( ShaderScissor ) ), 1 ) ), size( 0, 0, 0 ), enabled( false ){}
39  ShaderScissor() : model( 1 ), size( 0, 0, 0 ), enabled( false ){}
40 
41  ShaderScissor( ShaderScissor const & right ) :
42  //model( ( memset( this, 0, sizeof( ShaderScissor ) ), right.model ) ),
43  model( right.model ),
44  size( right.size ),
45  enabled( right.enabled )
46  {}
47 
48  ShaderScissor const & operator*( ShaderScissor const & right ) const
49  {
50  return right.enabled ? right : *this;
51  }
52 
54  {
55  if( right.enabled )
56  *this = right;
57  return *this;
58  }
59 
60  bool operator==( ShaderScissor const & right ) const
61  {
62  if( !this->enabled && !right.enabled )
63  return true;
64 
65  return std::tuple( this->enabled, this->model, this->size ) == std::tuple( right.enabled, right.model, right.size );
66  }
67 };
68 
69 
70 class FlatShader : private GlListener
71 {
72 public:
73  enum class FittingStage
74  {
75  None
76  };
77 
78  enum class PixelizeStage
79  {
80  None,
81  Squares,
82  Circles
83  };
84 
85  enum class ResizeStage
86  {
87  Scale,
88  Fixed,
89  Repeat,
90  Frame
91  };
92 
93  enum class FilteringStage
94  {
95  Plain,
96  Msdf,
100  };
101 
102  struct Params
103  {
105  fittingStage( FittingStage::None ),
106  pixelizeStage( PixelizeStage::None ), pixelizeSize( owner, float2( 1, 1 ) ), pixelizeOffset( owner, float2( 0, 0 ) ),
107  resizeStage( ResizeStage::Scale ), resizeAnchor( owner, float2( 0, 0 ) ),
108  filteringStage( FilteringStage::Plain ), filteringAlphaThreshold( owner, 0 ), filteringAlphaWidth( owner, 0 ),
109  alpha( owner, 1.0f ),
110  colorTransform( owner, float4x4( 1.0f ) )
111  {}
112 
113  // fitting stage
115 
116  // pixelize stage
120 
121  // resize stage
124 
125  // filtering stage
129 
130  // alpha stage
132 
133  // color stage
135  };
136 
137 public:
139 using GlInfo::instance;
140 
141  FlatShader( Instance * inst, VirtualResourceProvider const *, FlatShader_Subprovider const *, ResourcePath const & path );
142 
146  GLuint program_id() const;
147 
185  void Render(
186  ClientMarker const & log_target,
187 
188  // Renderable
189  CameraState const & camera,
190  std::optional< float > depth_override,
191 
192  // Elem
193  float4x4 const & model,
194  ShaderScissor const & scissor,
195 
196  // Translucent
197  float4 preblend,
198  bool translucent,
199 
200  // mesh
201  GlMesh const * mesh,
202  float3 mesh_resize,
203  float2 mesh_texcoord_density,
204 
205  // texture
206  GlTexture const * texture,
207  float texture_density_rel,
208  float texture_msdf_pixelRange,
209 
210  // parametrization
211  Params const & params
212  ) const;
213 
214 private:
215  virtual void GlEnable() override;
216  virtual void GlDisable() override;
217  virtual void GlDrop() override;
218 
219 private:
220  Shader _shader;
221 
222  // mesh
223  GLint mesh_resize;
224  GLint mesh_texcoord_density;
225 
226  // texture
227  GLint tex0;
228  GLint tex0_size;
229  GLint tex0_density_rel;
230  GLint tex0_color_space;
231 
232  // translucent
233  GLint translucent;
234  GLint preblend;
235 
236  // space transform
237  GLint projection_view_model;
238  GLint local_to_pixel_space;
239  GLint pixel_to_local_space;
240  GLint depth_override_enabled;
241  GLint depth_override;
242 
243  // scissor
244  GLint scissor_enabled;
245  GLint local_to_scissor_space;
246  GLint scissor_size;
247 
248  // shader params - fitting stage
249  GLint fitting_stage;
250 
251  // shader params - pixelize stage
252  GLint pixelize_stage;
253  GLint pixelize_size;
254  GLint pixelize_offset;
255 
256  // shader params - resize stage
257  GLint resize_stage;
258  GLint resize_anchor;
259 
260  // shader params - filtering stage
261  GLint filtering_stage;
262  GLint filtering_msdf_pixel_range;
263  GLint filtering_alpha_threshold;
264  GLint filtering_alpha_width;
265 
266  // shader params - color stage
267  GLint color_transform;
268 
269  // shader params - alpha stage
270  GLint alpha;
271 
272  //
273  GLint framebuffer_color_space;
274 };
275 
278 class FlatShader_Resource : public SingularResource< FlatShader >
279 {
280 public:
283 
284  FlatShader_Resource( Instance * inst );
285 };
286 
287 
288 //=========================================================================================================
289 //--------------------------- StringIO --------------------------------------------------------------------
290 template<>
291 struct StringIO< FlatShader::FittingStage > : public StringIO_Table< FlatShader::FittingStage >
292 {
293  static const ValuesType Values;
294 };
295 
296 template<>
297 struct StringIO< FlatShader::PixelizeStage > : public StringIO_Table< FlatShader::PixelizeStage >
298 {
299  static const ValuesType Values;
300 };
301 
302 template<>
303 struct StringIO< FlatShader::ResizeStage > : public StringIO_Table< FlatShader::ResizeStage >
304 {
305  static const ValuesType Values;
306 };
307 
308 template<>
309 struct StringIO< FlatShader::FilteringStage > : public StringIO_Table< FlatShader::FilteringStage >
310 {
311  static const ValuesType Values;
312 };
313 
314 }