diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2014-09-04 13:55:03 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2014-09-04 16:22:25 -0700 |
commit | 1fedf0918aa5c529207ba8f2f1312d403a8ba7fc (patch) | |
tree | b33bbb21744719b2f4b159051bcbe614caddd191 /include | |
parent | c14ae3c477617d284d50daff668ab5716ff9a771 (diff) | |
download | qtlocation-mapboxgl-1fedf0918aa5c529207ba8f2f1312d403a8ba7fc.tar.gz |
Less shader boilerplate
Diffstat (limited to 'include')
-rw-r--r-- | include/mbgl/renderer/painter.hpp | 2 | ||||
-rw-r--r-- | include/mbgl/shader/dot_shader.hpp | 29 | ||||
-rw-r--r-- | include/mbgl/shader/gaussian_shader.hpp | 12 | ||||
-rw-r--r-- | include/mbgl/shader/icon_shader.hpp | 52 | ||||
-rw-r--r-- | include/mbgl/shader/line_shader.hpp | 33 | ||||
-rw-r--r-- | include/mbgl/shader/linejoin_shader.hpp | 22 | ||||
-rw-r--r-- | include/mbgl/shader/linepattern_shader.hpp | 47 | ||||
-rw-r--r-- | include/mbgl/shader/outline_shader.hpp | 12 | ||||
-rw-r--r-- | include/mbgl/shader/pattern_shader.hpp | 32 | ||||
-rw-r--r-- | include/mbgl/shader/plain_shader.hpp | 8 | ||||
-rw-r--r-- | include/mbgl/shader/raster_shader.hpp | 42 | ||||
-rw-r--r-- | include/mbgl/shader/shader.hpp | 6 | ||||
-rw-r--r-- | include/mbgl/shader/text_shader.hpp | 65 | ||||
-rw-r--r-- | include/mbgl/shader/uniform.hpp | 53 |
14 files changed, 147 insertions, 268 deletions
diff --git a/include/mbgl/renderer/painter.hpp b/include/mbgl/renderer/painter.hpp index 52536e0a75..7a245a58da 100644 --- a/include/mbgl/renderer/painter.hpp +++ b/include/mbgl/renderer/painter.hpp @@ -88,6 +88,8 @@ public: void renderRaster(RasterBucket& bucket, std::shared_ptr<StyleLayer> layer_desc, const Tile::ID& id, const mat4 &matrix); void renderBackground(std::shared_ptr<StyleLayer> layer_desc); + float saturationFactor(float saturation); + float contrastFactor(float contrast); std::array<float, 3> spinWeights(float spin_value); void preparePrerender(RasterBucket &bucket); diff --git a/include/mbgl/shader/dot_shader.hpp b/include/mbgl/shader/dot_shader.hpp index d8d649db9c..2c4176f364 100644 --- a/include/mbgl/shader/dot_shader.hpp +++ b/include/mbgl/shader/dot_shader.hpp @@ -2,30 +2,23 @@ #define MBGL_SHADER_SHADER_DOT #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { - class DotShader : public Shader { - public: - DotShader(); +class DotShader : public Shader { +public: + DotShader(); - void bind(char *offset); + void bind(char *offset); - void setColor(const std::array<float, 4>& color); - void setSize(float size); - void setBlur(float blur); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; + Uniform<float> u_size = {"u_size", *this}; + Uniform<float> u_blur = {"u_blur", *this}; - private: - int32_t a_pos = -1; - - std::array<float, 4> color = {{}}; - int32_t u_color = -1; - - float size = 0; - int32_t u_size = -1; - - float blur = 0; - int32_t u_blur = -1; +private: + int32_t a_pos = -1; }; } diff --git a/include/mbgl/shader/gaussian_shader.hpp b/include/mbgl/shader/gaussian_shader.hpp index a4b9d09f3a..0f494f5c7e 100644 --- a/include/mbgl/shader/gaussian_shader.hpp +++ b/include/mbgl/shader/gaussian_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_RENDERER_SHADER_GAUSSIAN #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,17 +12,12 @@ public: void bind(char *offset); - void setImage(int32_t image); - void setOffset(const std::array<float, 2>& offset); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<std::array<float, 2>> u_offset = {"u_offset", *this}; + Uniform<int32_t> u_image = {"u_image", *this}; private: int32_t a_pos = -1; - - int32_t image = 0; - int32_t u_image = -1; - - std::array<float, 2> offset = {{}}; - int32_t u_offset = -1; }; } diff --git a/include/mbgl/shader/icon_shader.hpp b/include/mbgl/shader/icon_shader.hpp index 54869fe48d..645d7e21b6 100644 --- a/include/mbgl/shader/icon_shader.hpp +++ b/include/mbgl/shader/icon_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_ICON #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,16 +12,17 @@ public: void bind(char *offset); - void setExtrudeMatrix(const std::array<float, 16>& exmatrix); - void setAngle(float angle); - void setZoom(float zoom); - void setFlip(float flip); - void setFadeDist(float fadedist); - void setMinFadeZoom(float minfadezoom); - void setMaxFadeZoom(float maxfadezoom); - void setFadeZoom(float fadezoom); - void setOpacity(float opacity); - void setTextureSize(const std::array<float, 2> &texsize); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this}; + Uniform<float> u_angle = {"u_angle", *this}; + Uniform<float> u_zoom = {"u_zoom", *this}; + Uniform<float> u_flip = {"u_flip", *this}; + Uniform<float> u_fadedist = {"u_fadedist", *this}; + Uniform<float> u_minfadezoom = {"u_minfadezoom", *this}; + Uniform<float> u_maxfadezoom = {"u_maxfadezoom", *this}; + Uniform<float> u_fadezoom = {"u_fadezoom", *this}; + Uniform<float> u_opacity = {"u_opacity", *this}; + Uniform<std::array<float, 2>> u_texsize = {"u_texsize", *this}; private: int32_t a_pos = -1; @@ -32,36 +34,6 @@ private: int32_t a_rangeend = -1; int32_t a_rangestart = -1; int32_t a_labelminzoom = -1; - - std::array<float, 16> exmatrix = {{}}; - int32_t u_exmatrix = -1; - - float angle = 0; - int32_t u_angle = -1; - - float zoom = 0; - int32_t u_zoom = -1; - - float flip = 0; - int32_t u_flip = -1; - - float fadedist = 0; - int32_t u_fadedist = -1; - - float minfadezoom = 0; - int32_t u_minfadezoom = -1; - - float maxfadezoom = 0; - int32_t u_maxfadezoom = -1; - - float fadezoom = 0; - int32_t u_fadezoom = -1; - - float opacity = 0; - int32_t u_opacity = -1; - - std::array<float, 2> texsize = {{}}; - int32_t u_texsize = -1; }; } diff --git a/include/mbgl/shader/line_shader.hpp b/include/mbgl/shader/line_shader.hpp index c476b5c4c6..b789330882 100644 --- a/include/mbgl/shader/line_shader.hpp +++ b/include/mbgl/shader/line_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_LINE #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,36 +12,18 @@ public: void bind(char *offset); - void setExtrudeMatrix(const std::array<float, 16>& exmatrix); - void setColor(const std::array<float, 4>& color); - void setLineWidth(const std::array<float, 2>& linewidth); - void setRatio(float ratio); - void setDashArray(const std::array<float, 2>& dasharray); - void setBlur(float blur); - void setDebug(float debug); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; + Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this}; + Uniform<std::array<float, 2>> u_dasharray = {"u_dasharray", *this}; + Uniform<float> u_ratio = {"u_ratio", *this}; + Uniform<float> u_blur = {"u_blur", *this}; private: int32_t a_pos = -1; int32_t a_extrude = -1; int32_t a_linesofar = -1; - - std::array<float, 16> exmatrix = {{}}; - int32_t u_exmatrix = -1; - - std::array<float, 4> color = {{}}; - int32_t u_color = -1; - - std::array<float, 2> linewidth = {{}}; - int32_t u_linewidth = -1; - - float ratio = 0; - int32_t u_ratio = -1; - - std::array<float, 2> dasharray = {{}}; - int32_t u_dasharray = -1; - - float blur = 0.0f; - int32_t u_blur = -1; }; diff --git a/include/mbgl/shader/linejoin_shader.hpp b/include/mbgl/shader/linejoin_shader.hpp index 5a5c97e921..61406fd45c 100644 --- a/include/mbgl/shader/linejoin_shader.hpp +++ b/include/mbgl/shader/linejoin_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_LINEJOIN #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,25 +12,14 @@ public: void bind(char *offset); - void setColor(const std::array<float, 4>& color); - void setWorld(const std::array<float, 2>& world); - void setLineWidth(const std::array<float, 2>& linewidth); - void setSize(float size); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; + Uniform<std::array<float, 2>> u_world = {"u_world", *this}; + Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this}; + Uniform<float> u_size = {"u_size", *this}; private: int32_t a_pos = -1; - - std::array<float, 4> color = {{}}; - int32_t u_color = -1; - - std::array<float, 2> world = {{}}; - int32_t u_world = -1; - - std::array<float, 2> linewidth = {{}}; - int32_t u_linewidth = -1; - - float size = 0; - int32_t u_size = -1; }; } diff --git a/include/mbgl/shader/linepattern_shader.hpp b/include/mbgl/shader/linepattern_shader.hpp index f8d50b2376..bd22c99a19 100644 --- a/include/mbgl/shader/linepattern_shader.hpp +++ b/include/mbgl/shader/linepattern_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_LINEPATTERN #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,47 +12,21 @@ public: void bind(char *offset); - void setExtrudeMatrix(const std::array<float, 16>& exmatrix); - void setLineWidth(const std::array<float, 2>& linewidth); - void setRatio(float ratio); - void setPatternSize(const std::array<float, 2>& new_pattern_size); - void setPatternTopLeft(const std::array<float, 2>& new_pattern_tl); - void setPatternBottomRight(const std::array<float, 2>& new_pattern_br); - void setPoint(float new_point); - void setBlur(float new_blur); - void setFade(float new_fade); + UniformMatrix<4> u_matrix = {"u_posmatrix", *this}; + UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this}; + Uniform<std::array<float, 2>> u_linewidth = {"u_linewidth", *this}; + Uniform<std::array<float, 2>> u_pattern_size = {"u_pattern_size", *this}; + Uniform<std::array<float, 2>> u_pattern_tl = {"u_pattern_tl", *this}; + Uniform<std::array<float, 2>> u_pattern_br = {"u_pattern_br", *this}; + Uniform<float> u_ratio = {"u_ratio", *this}; + Uniform<float> u_point = {"u_point", *this}; + Uniform<float> u_blur = {"u_blur", *this}; + Uniform<float> u_fade = {"u_fade", *this}; private: int32_t a_pos = -1; int32_t a_extrude = -1; int32_t a_linesofar = -1; - - std::array<float, 16> exmatrix = {{}}; - int32_t u_exmatrix = -1; - - std::array<float, 2> linewidth = {{}}; - int32_t u_linewidth = -1; - - float ratio = 0; - int32_t u_ratio = -1; - - std::array<float, 2> pattern_size = {{}}; - int32_t u_pattern_size = -1; - - std::array<float, 2> pattern_tl = {{}}; - int32_t u_pattern_tl = -1; - - std::array<float, 2> pattern_br = {{}}; - int32_t u_pattern_br = -1; - - float point = 0.0f; - int32_t u_point = -1; - - float blur = 0.0f; - int32_t u_blur = -1; - - float fade = 0; - int32_t u_fade = -1; }; } diff --git a/include/mbgl/shader/outline_shader.hpp b/include/mbgl/shader/outline_shader.hpp index 551e31ca57..f3e8175fd7 100644 --- a/include/mbgl/shader/outline_shader.hpp +++ b/include/mbgl/shader/outline_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_OUTLINE #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,17 +12,12 @@ public: void bind(char *offset); - void setColor(const std::array<float, 4>& color); - void setWorld(const std::array<float, 2>& world); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; + Uniform<std::array<float, 2>> u_world = {"u_world", *this}; private: int32_t a_pos = -1; - - std::array<float, 4> color = {{}}; - int32_t u_color = -1; - - std::array<float, 2> world = {{}}; - int32_t u_world = -1; }; } diff --git a/include/mbgl/shader/pattern_shader.hpp b/include/mbgl/shader/pattern_shader.hpp index e574b755dc..9fabd8e18a 100644 --- a/include/mbgl/shader/pattern_shader.hpp +++ b/include/mbgl/shader/pattern_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_PATTERN #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,33 +12,16 @@ public: void bind(char *offset); - void setPatternTopLeft(const std::array<float, 2>& pattern_tl); - void setPatternBottomRight(const std::array<float, 2>& pattern_br); - void setOpacity(float opacity); - void setImage(int image); - void setMix(float mix); - void setPatternMatrix(const std::array<float, 9> &patternmatrix); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<std::array<float, 2>> u_pattern_tl = {"u_pattern_tl", *this}; + Uniform<std::array<float, 2>> u_pattern_br = {"u_pattern_br", *this}; + Uniform<float> u_opacity = {"u_opacity", *this}; + Uniform<float> u_mix = {"u_mix", *this}; + Uniform<int32_t> u_image = {"u_image", *this}; + UniformMatrix<3> u_patternmatrix = {"u_patternmatrix", *this}; private: int32_t a_pos = -1; - - std::array<float, 2> pattern_tl = {{}}; - int32_t u_pattern_tl = -1; - - std::array<float, 2> pattern_br = {{}}; - int32_t u_pattern_br = -1; - - float opacity = 0; - int32_t u_opacity = -1; - - int image = 0; - int32_t u_image = -1; - - float mix = 0; - int32_t u_mix = -1; - - std::array<float, 9> patternmatrix = {{}}; - int32_t u_patternmatrix = -1; }; } diff --git a/include/mbgl/shader/plain_shader.hpp b/include/mbgl/shader/plain_shader.hpp index 277788431f..051501c3c9 100644 --- a/include/mbgl/shader/plain_shader.hpp +++ b/include/mbgl/shader/plain_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_SHADER_SHADER_PLAIN #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,14 +12,11 @@ public: void bind(char *offset); - void setColor(float r, float g, float b, float a); - void setColor(const std::array<float, 4>& color); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; private: int32_t a_pos = -1; - - std::array<float, 4> color = {{}}; - int32_t u_color = -1; }; } diff --git a/include/mbgl/shader/raster_shader.hpp b/include/mbgl/shader/raster_shader.hpp index 11765413f4..8cf97055a2 100644 --- a/include/mbgl/shader/raster_shader.hpp +++ b/include/mbgl/shader/raster_shader.hpp @@ -2,6 +2,7 @@ #define MBGL_RENDERER_SHADER_RASTER #include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,41 +12,18 @@ public: void bind(char *offset); - void setImage(int32_t image); - void setOpacity(float opacity); - void setBuffer(float buffer); - void setBrightness(float brightness_low, float brightness_high); - void setSaturation(float saturation_factor); - void setContrast(float contrast_factor); - void setSpin(std::array<float, 3> spin_weights); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + Uniform<int32_t> u_image = {"u_image", *this}; + Uniform<float> u_opacity = {"u_opacity", *this}; + Uniform<float> u_buffer = {"u_buffer", *this}; + Uniform<float> u_brightness_low = {"u_brightness_low", *this}; + Uniform<float> u_brightness_high = {"u_brightness_high", *this}; + Uniform<float> u_saturation_factor = {"u_saturation_factor", *this}; + Uniform<float> u_contrast_factor = {"u_contrast_factor", *this}; + Uniform<std::array<float, 3>> u_spin_weights = {"u_spin_weights", *this}; private: int32_t a_pos = -1; - - int32_t image = 0; - int32_t u_image = -1; - - float opacity = 0; - int32_t u_opacity = -1; - - float buffer = 0; - int32_t u_buffer = -1; - - float brightness_low = 0; - int32_t u_brightness_low = -1; - - float brightness_high = 0; - int32_t u_brightness_high = -1; - - float saturation_factor = 0; - int32_t u_saturation_factor = -1; - - float contrast_factor = 1; - int32_t u_contrast_factor = -1; - - std::array<float, 3> spin_weights = {{}}; - int32_t u_spin_weights = -1; - }; } diff --git a/include/mbgl/shader/shader.hpp b/include/mbgl/shader/shader.hpp index fa2d5a0a8e..27e831a510 100644 --- a/include/mbgl/shader/shader.hpp +++ b/include/mbgl/shader/shader.hpp @@ -15,18 +15,12 @@ public: bool valid; uint32_t program; - void setMatrix(const std::array<float, 16>& matrix); - inline uint32_t getID() const { return program; } private: bool compileShader(uint32_t *shader, uint32_t type, const char *source); - -protected: - std::array<float, 16> matrix = {{}}; - int32_t u_matrix = -1; }; } diff --git a/include/mbgl/shader/text_shader.hpp b/include/mbgl/shader/text_shader.hpp index 554b890fb0..05360b9fcf 100644 --- a/include/mbgl/shader/text_shader.hpp +++ b/include/mbgl/shader/text_shader.hpp @@ -1,7 +1,8 @@ #ifndef MBGL_SHADER_TEXT_SHADER #define MBGL_SHADER_TEXT_SHADER -#include "shader.hpp" +#include <mbgl/shader/shader.hpp> +#include <mbgl/shader/uniform.hpp> namespace mbgl { @@ -11,61 +12,25 @@ public: void bind(char *offset); - void setColor(float r, float g, float b, float a); - void setColor(const std::array<float, 4> &color); - void setBuffer(float buffer); - void setGamma(float gamma); - void setExtrudeMatrix(const std::array<float, 16> &exmatrix); - void setAngle(float angle); - void setZoom(float zoom); - void setFlip(float flip); - void setFadeDist(float fadedist); - void setMinFadeZoom(float minfadezoom); - void setMaxFadeZoom(float maxfadezoom); - void setFadeZoom(float fadezoom); - void setTextureSize(const std::array<float, 2> &texsize); + UniformMatrix<4> u_matrix = {"u_matrix", *this}; + UniformMatrix<4> u_exmatrix = {"u_exmatrix", *this}; + Uniform<std::array<float, 4>> u_color = {"u_color", *this}; + Uniform<std::array<float, 2>> u_texsize = {"u_texsize", *this}; + Uniform<float> u_buffer = {"u_buffer", *this}; + Uniform<float> u_gamma = {"u_gamma", *this}; + Uniform<float> u_angle = {"u_angle", *this}; + Uniform<float> u_zoom = {"u_zoom", *this}; + Uniform<float> u_flip = {"u_flip", *this}; + Uniform<float> u_fadedist = {"u_fadedist", *this}; + Uniform<float> u_minfadezoom = {"u_minfadezoom", *this}; + Uniform<float> u_maxfadezoom = {"u_maxfadezoom", *this}; + Uniform<float> u_fadezoom = {"u_fadezoom", *this}; private: int32_t a_pos = -1; int32_t a_offset = -1; int32_t a_data1 = -1; int32_t a_data2 = -1; - - std::array<float, 4> color = {{}}; - int32_t u_color = -1; - - float buffer = 0.0f; - int32_t u_buffer = -1; - - float gamma = 0.0f; - int32_t u_gamma = -1; - - std::array<float, 16> exmatrix = {{}}; - int32_t u_exmatrix = -1; - - float angle = 0.0f; - int32_t u_angle = -1; - - float zoom = 0.0f; - int32_t u_zoom = -1; - - float flip = 0.0f; - int32_t u_flip = -1; - - float fadedist = 0.0f; - int32_t u_fadedist = -1; - - float minfadezoom = 0.0f; - int32_t u_minfadezoom = -1; - - float maxfadezoom = 0.0f; - int32_t u_maxfadezoom = -1; - - float fadezoom = 0.0f; - int32_t u_fadezoom = -1; - - std::array<float, 2> texsize = {{}}; - int32_t u_texsize = -1; }; } diff --git a/include/mbgl/shader/uniform.hpp b/include/mbgl/shader/uniform.hpp new file mode 100644 index 0000000000..a87bbd7aa3 --- /dev/null +++ b/include/mbgl/shader/uniform.hpp @@ -0,0 +1,53 @@ +#ifndef MBGL_SHADER_UNIFORM +#define MBGL_SHADER_UNIFORM + +#include <mbgl/shader/shader.hpp> +#include <mbgl/platform/gl.hpp> + +namespace mbgl { + +template <typename T> +class Uniform { +public: + Uniform(const GLchar* name, const Shader& shader) + : location(glGetUniformLocation(shader.program, name)) {} + + void operator=(const T& t) { + if (current != t) { + current = t; + bind(t); + } + } + +private: + void bind(const T&); + + T current; + GLint location; +}; + +template <size_t C, size_t R = C> +class UniformMatrix { +public: + typedef std::array<float, C*R> T; + + UniformMatrix(const GLchar* name, const Shader& shader) + : location(glGetUniformLocation(shader.program, name)) {} + + void operator=(const T& t) { + if (current != t) { + current = t; + bind(t); + } + } + +private: + void bind(const T&); + + T current; + GLint location; +}; + +} + +#endif |