Changeset View
Changeset View
Standalone View
Standalone View
effects/blur/blurshader.h
| Show All 15 Lines | |||||
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| 17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
| 18 | */ | 18 | */ | ||
| 19 | 19 | | |||
| 20 | #ifndef BLURSHADER_H | 20 | #ifndef BLURSHADER_H | ||
| 21 | #define BLURSHADER_H | 21 | #define BLURSHADER_H | ||
| 22 | 22 | | |||
| 23 | #include <kwinglutils.h> | 23 | #include <kwinglutils.h> | ||
| 24 | #include <QVector2D> | ||||
| 25 | #include <QVector4D> | ||||
| 26 | | ||||
| 24 | 27 | | |||
| 25 | class QMatrix4x4; | 28 | class QMatrix4x4; | ||
| 26 | 29 | | |||
| 27 | namespace KWin | 30 | namespace KWin | ||
| 28 | { | 31 | { | ||
| 29 | 32 | | |||
| 30 | struct KernelValue | | |||
| 31 | { | | |||
| 32 | KernelValue() {} | | |||
| 33 | KernelValue(float x, float g) : x(x), g(g) {} | | |||
| 34 | bool operator < (const KernelValue &other) const { return x < other.x; } | | |||
| 35 | | ||||
| 36 | float x; | | |||
| 37 | float g; | | |||
| 38 | }; | | |||
| 39 | | ||||
| 40 | class BlurShader | 33 | class BlurShader | ||
| 41 | { | 34 | { | ||
| 42 | public: | 35 | public: | ||
| 43 | BlurShader(); | 36 | BlurShader(); | ||
| 44 | virtual ~BlurShader(); | 37 | virtual ~BlurShader(); | ||
| 45 | 38 | | |||
| 46 | static BlurShader *create(); | 39 | static BlurShader *create(); | ||
| 47 | 40 | | |||
| 48 | bool isValid() const { | 41 | bool isValid() const { | ||
| 49 | return mValid; | 42 | return mValid; | ||
| 50 | } | 43 | } | ||
| 51 | 44 | | |||
| 52 | // Sets the radius in pixels | | |||
| 53 | void setRadius(int radius); | | |||
| 54 | int radius() const { | | |||
| 55 | return mRadius; | | |||
| 56 | } | | |||
| 57 | | ||||
| 58 | // Sets the blur direction | | |||
| 59 | void setDirection(Qt::Orientation direction); | | |||
| 60 | Qt::Orientation direction() const { | | |||
| 61 | return mDirection; | | |||
| 62 | } | | |||
| 63 | | ||||
| 64 | // Sets the distance between two pixels | | |||
| 65 | virtual void setPixelDistance(float val) = 0; | | |||
| 66 | virtual void setTextureMatrix(const QMatrix4x4 &matrix) = 0; | | |||
| 67 | virtual void setModelViewProjectionMatrix(const QMatrix4x4 &matrix) = 0; | 45 | virtual void setModelViewProjectionMatrix(const QMatrix4x4 &matrix) = 0; | ||
| 46 | virtual void setOffset(float offset) = 0; | ||||
| 47 | virtual void setTextureSize(QSize textureSize) = 0; | ||||
fredrik: I suggest changing the name to setTargetSize() to make it clear that the size is the size of… | |||||
| 48 | virtual void setBlurRect(QRect blurRect, QSize screenSize) = 0; | ||||
| 68 | 49 | | |||
| 69 | virtual void bind() = 0; | 50 | virtual void bind(int sampleType) = 0; | ||
fredrik: sampleTypeEnum sampleType | |||||
| 70 | virtual void unbind() = 0; | 51 | virtual void unbind() = 0; | ||
| 71 | 52 | | |||
| 53 | enum sampleTypeEnum { | ||||
The first letter in enums should be capitalized. I think SampleType would also be a better name. fredrik: The first letter in enums should be capitalized. I think SampleType would also be a better name. | |||||
fredrik: The first letters in the enumerators should also be capitalized. | |||||
| 54 | downSampleType, | ||||
| 55 | upSampleType, | ||||
| 56 | copySampleType | ||||
| 57 | }; | ||||
| 58 | | ||||
| 72 | protected: | 59 | protected: | ||
| 73 | float gaussian(float x, float sigma) const; | | |||
| 74 | QList<KernelValue> gaussianKernel() const; | | |||
| 75 | void setIsValid(bool value) { | 60 | void setIsValid(bool value) { | ||
| 76 | mValid = value; | 61 | mValid = value; | ||
| 77 | } | 62 | } | ||
| 78 | virtual void init() = 0; | 63 | virtual void init() = 0; | ||
| 79 | virtual void reset() = 0; | 64 | virtual void reset() = 0; | ||
| 80 | virtual int maxKernelSize() const = 0; | | |||
| 81 | 65 | | |||
| 82 | private: | 66 | private: | ||
| 83 | int mRadius; | | |||
| 84 | Qt::Orientation mDirection; | | |||
| 85 | bool mValid; | 67 | bool mValid; | ||
| 86 | }; | 68 | }; | ||
| 87 | 69 | | |||
| 88 | 70 | | |||
| 89 | // ---------------------------------------------------------------------------- | 71 | // ---------------------------------------------------------------------------- | ||
| 90 | 72 | | |||
| 91 | 73 | | |||
| 92 | 74 | | |||
| 93 | class GLSLBlurShader : public BlurShader | 75 | class GLSLBlurShader : public BlurShader | ||
| 94 | { | 76 | { | ||
| 95 | public: | 77 | public: | ||
| 96 | GLSLBlurShader(); | 78 | GLSLBlurShader(); | ||
| 97 | ~GLSLBlurShader(); | 79 | ~GLSLBlurShader(); | ||
| 98 | 80 | | |||
| 99 | void setPixelDistance(float val); | 81 | void bind(int sampleType); | ||
| 100 | void bind(); | | |||
| 101 | void unbind(); | 82 | void unbind(); | ||
| 102 | void setTextureMatrix(const QMatrix4x4 &matrix); | | |||
| 103 | void setModelViewProjectionMatrix(const QMatrix4x4 &matrix); | 83 | void setModelViewProjectionMatrix(const QMatrix4x4 &matrix); | ||
| 84 | void setOffset(float offset); | ||||
fredrik: override final
This goes for all reimplemented virtual functions.
| |||||
| 85 | void setTextureSize(QSize textureSize); | ||||
| 86 | void setBlurRect(QRect blurRect, QSize screenSize); | ||||
| 104 | 87 | | |||
| 105 | protected: | 88 | protected: | ||
| 106 | void init(); | 89 | void init(); | ||
| 107 | void reset(); | 90 | void reset(); | ||
| 108 | int maxKernelSize() const; | | |||
| 109 | 91 | | |||
| 110 | private: | 92 | private: | ||
| 111 | GLShader *shader; | 93 | GLShader *shaderDownsample; | ||
| 112 | int mvpMatrixLocation; | 94 | GLShader *shaderUpsample; | ||
| 113 | int textureMatrixLocation; | 95 | GLShader *shaderCopysample; | ||
I think you could simplify the code quite a bit by having an array of struct {
GLShader *shader;
int mvpMatrixLocation;
...
};and use m_activeSampleType as an index into that array. fredrik: I think you could simplify the code quite a bit by having an array of
```
struct {
GLShader… | |||||
Only the downsample and upsample have the same uniforms, copysample uses different ones. anemeth: Only the downsample and upsample have the same uniforms, copysample uses different ones.
This… | |||||
| 114 | int pixelSizeLocation; | 96 | | ||
| 97 | int mvpMatrixLocationDownsample; | ||||
| 98 | int offsetLocationDownsample; | ||||
| 99 | int textureSizeLocationDownsample; | ||||
| 100 | | ||||
| 101 | int mvpMatrixLocationUpsample; | ||||
| 102 | int offsetLocationUpsample; | ||||
| 103 | int textureSizeLocationUpsample; | ||||
| 104 | | ||||
| 105 | int mvpMatrixLocationCopysample; | ||||
| 106 | int textureSizeLocationCopysample; | ||||
| 107 | int blurRectLocationCopysample; | ||||
Coding style + consistency: GLShader *m_shaderDownsample; GLShader *m_shaderUpsample; GLShader *m_shaderCopysample; int m_mvpMatrixLocationDownsample; int m_offsetLocationDownsample; int m_textureSizeLocationDownsample; int m_mvpMatrixLocationUpsample; int m_offsetLocationUpsample; int m_textureSizeLocationUpsample; int m_mvpMatrixLocationCopysample; int m_textureSizeLocationCopysample; int m_blurRectLocationCopysample; zzag: Coding style + consistency:
```
GLShader *m_shaderDownsample;
GLShader… | |||||
| 108 | | ||||
| 109 | | ||||
| 110 | //Caching uniform values to aviod unnecessary setUniform calls | ||||
| 111 | int activeSampleType; | ||||
zzag: `m_` | |||||
| 112 | | ||||
| 113 | float m_offsetDownsample; | ||||
| 114 | QMatrix4x4 m_matrixDownsample; | ||||
| 115 | QSize m_textureSizeDownsample; | ||||
| 116 | | ||||
| 117 | float m_offsetUpsample; | ||||
| 118 | QMatrix4x4 m_matrixUpsample; | ||||
| 119 | QSize m_textureSizeUpsample; | ||||
| 120 | | ||||
| 121 | QMatrix4x4 m_matrixCopysample; | ||||
| 122 | QSize m_textureSizeCopysample; | ||||
| 123 | QRect m_blurRectCopysample; | ||||
| 124 | | ||||
| 115 | }; | 125 | }; | ||
| 116 | 126 | | |||
| 117 | } // namespace KWin | 127 | } // namespace KWin | ||
| 118 | 128 | | |||
| 119 | #endif | 129 | #endif | ||
| 120 | | ||||
I suggest changing the name to setTargetSize() to make it clear that the size is the size of the render target, and not the size of the texture being sampled.