libgui/include/gui/video/shaders/Texture2DShader.h

111 lines
3.6 KiB
C
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef __TEXTURE_2D_SHADER_H_
#define __TEXTURE_2D_SHADER_H_
2019-08-14 23:24:55 +02:00
#include <gui/video/shaders/FetchShader.h>
2022-02-05 14:28:08 +01:00
#include <gui/video/shaders/PixelShader.h>
#include <gui/video/shaders/VertexShader.h>
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
class Texture2DShader : public Shader {
2017-10-29 10:28:14 +01:00
private:
Texture2DShader();
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
virtual ~Texture2DShader();
2022-02-05 14:28:08 +01:00
static const uint32_t cuAttributeCount = 2;
static const uint32_t ciPositionVtxsSize = 4 * cuVertexAttrSize;
2018-06-21 20:44:58 +02:00
static const uint32_t ciTexCoordsVtxsSize = 4 * cuTexCoordAttrSize;
2017-10-29 10:28:14 +01:00
static Texture2DShader *shaderInstance;
FetchShader *fetchShader;
VertexShader vertexShader;
PixelShader pixelShader;
2018-06-21 20:44:58 +02:00
float *posVtxs;
float *texCoords;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
uint32_t angleLocation;
uint32_t offsetLocation;
uint32_t scaleLocation;
uint32_t colorIntensityLocation;
uint32_t blurLocation;
uint32_t samplerLocation;
uint32_t positionLocation;
uint32_t texCoordLocation;
2022-02-05 14:28:08 +01:00
2017-10-29 10:28:14 +01:00
public:
static Texture2DShader *instance() {
2020-08-13 12:38:07 +02:00
if (!shaderInstance) {
2017-10-29 10:28:14 +01:00
shaderInstance = new Texture2DShader();
}
return shaderInstance;
}
2020-08-13 12:38:07 +02:00
2017-10-29 10:28:14 +01:00
static void destroyInstance() {
2020-08-13 12:38:07 +02:00
if (shaderInstance) {
2017-10-29 10:28:14 +01:00
delete shaderInstance;
shaderInstance = NULL;
}
}
2018-06-21 20:44:58 +02:00
void setShaders(void) const {
2017-10-29 10:28:14 +01:00
fetchShader->setShader();
vertexShader.setShader();
pixelShader.setShader();
}
2020-08-13 12:38:07 +02:00
void setAttributeBuffer(const float *texCoords_in = NULL, const float *posVtxs_in = NULL, const uint32_t &vtxCount = 0) const {
if (posVtxs_in && texCoords_in && vtxCount) {
2017-10-29 10:28:14 +01:00
VertexShader::setAttributeBuffer(0, vtxCount * cuVertexAttrSize, cuVertexAttrSize, posVtxs_in);
VertexShader::setAttributeBuffer(1, vtxCount * cuTexCoordAttrSize, cuTexCoordAttrSize, texCoords_in);
2018-06-21 20:44:58 +02:00
} else {
2017-10-29 10:28:14 +01:00
VertexShader::setAttributeBuffer(0, ciPositionVtxsSize, cuVertexAttrSize, posVtxs);
VertexShader::setAttributeBuffer(1, ciTexCoordsVtxsSize, cuTexCoordAttrSize, texCoords);
}
}
2020-08-13 12:38:07 +02:00
void setAngle(const float &val) {
2017-10-29 10:28:14 +01:00
VertexShader::setUniformReg(angleLocation, 4, &val);
}
2020-08-13 12:38:07 +02:00
void setOffset(const glm::vec3 &vec) {
2017-10-29 10:28:14 +01:00
VertexShader::setUniformReg(offsetLocation, 4, &vec[0]);
}
2020-08-13 12:38:07 +02:00
void setScale(const glm::vec3 &vec) {
2017-10-29 10:28:14 +01:00
VertexShader::setUniformReg(scaleLocation, 4, &vec[0]);
}
2020-08-13 12:38:07 +02:00
void setColorIntensity(const glm::vec4 &vec) {
2017-10-29 10:28:14 +01:00
PixelShader::setUniformReg(colorIntensityLocation, 4, &vec[0]);
}
2020-08-13 12:38:07 +02:00
void setBlurring(const glm::vec3 &vec) {
2017-10-29 10:28:14 +01:00
PixelShader::setUniformReg(blurLocation, 4, &vec[0]);
}
void setTextureAndSampler(const GX2Texture *texture, const GX2Sampler *sampler) const {
2020-08-13 12:38:07 +02:00
GX2SetPixelTexture((GX2Texture *) texture, samplerLocation);
GX2SetPixelSampler((GX2Sampler *) sampler, samplerLocation);
2017-10-29 10:28:14 +01:00
}
};
#endif // __TEXTURE_2D_SHADER_H_