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

153 lines
5.2 KiB
C
Raw Normal View History

2018-06-21 20:44:58 +02: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 PIXEL_SHADER_H
#define PIXEL_SHADER_H
2019-08-14 23:24:55 +02:00
#include <gui/video/shaders/Shader.h>
2018-06-21 20:44:58 +02:00
class PixelShader : public Shader {
public:
PixelShader()
2020-08-13 12:38:07 +02:00
: pixelShader((GX2PixelShader *) memalign(0x40, sizeof(GX2PixelShader))) {
if (pixelShader) {
2018-06-21 20:44:58 +02:00
memset(pixelShader, 0, sizeof(GX2PixelShader));
pixelShader->mode = GX2_SHADER_MODE_UNIFORM_REGISTER;
}
}
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
virtual ~PixelShader() {
2020-08-13 12:38:07 +02:00
if (pixelShader) {
2020-08-13 12:58:19 +02:00
if (pixelShader->program) {
2018-06-21 20:44:58 +02:00
free(pixelShader->program);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
for (uint32_t i = 0; i < pixelShader->uniformBlockCount; i++) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->uniformBlocks[i].name);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
if (pixelShader->uniformBlocks) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->uniformBlocks);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
for (uint32_t i = 0; i < pixelShader->uniformVarCount; i++) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->uniformVars[i].name);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
if (pixelShader->uniformVars) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->uniformVars);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
if (pixelShader->initialValues) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->initialValues);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
for (uint32_t i = 0; i < pixelShader->samplerVarCount; i++) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->samplerVars[i].name);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
if (pixelShader->samplerVars) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->samplerVars);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:58:19 +02:00
if (pixelShader->loopVars) {
2020-08-13 12:38:07 +02:00
free((void *) pixelShader->loopVars);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
free(pixelShader);
}
}
2020-08-13 12:38:07 +02:00
void setProgram(const uint32_t *program, const uint32_t &programSize, const uint32_t *regs, const uint32_t &regsSize) {
2020-08-13 12:58:19 +02:00
if (!pixelShader) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
//! this must be moved into an area where the graphic engine has access to and must be aligned to 0x100
pixelShader->size = programSize;
2020-08-13 12:38:07 +02:00
pixelShader->program = (uint8_t *) memalign(GX2_SHADER_PROGRAM_ALIGNMENT, pixelShader->size);
if (pixelShader->program) {
2018-06-21 20:44:58 +02:00
memcpy(pixelShader->program, program, pixelShader->size);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, pixelShader->program, pixelShader->size);
}
memcpy(&pixelShader->regs, regs, regsSize);
}
2020-08-13 12:38:07 +02:00
void addUniformVar(const GX2UniformVar &var) {
2020-08-13 12:58:19 +02:00
if (!pixelShader) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
uint32_t idx = pixelShader->uniformVarCount;
2020-08-13 12:38:07 +02:00
GX2UniformVar *newVar = (GX2UniformVar *) malloc((pixelShader->uniformVarCount + 1) * sizeof(GX2UniformVar));
if (newVar) {
if (pixelShader->uniformVars) {
2018-06-21 20:44:58 +02:00
memcpy(newVar, pixelShader->uniformVars, pixelShader->uniformVarCount * sizeof(GX2UniformVar));
free(pixelShader->uniformVars);
}
pixelShader->uniformVars = newVar;
memcpy(pixelShader->uniformVars + idx, &var, sizeof(GX2UniformVar));
2020-08-13 12:38:07 +02:00
pixelShader->uniformVars[idx].name = (char *) malloc(strlen(var.name) + 1);
strcpy((char *) pixelShader->uniformVars[idx].name, var.name);
2018-06-21 20:44:58 +02:00
pixelShader->uniformVarCount++;
}
}
2020-08-13 12:38:07 +02:00
void addSamplerVar(const GX2SamplerVar &var) {
2020-08-13 12:58:19 +02:00
if (!pixelShader) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
uint32_t idx = pixelShader->samplerVarCount;
2020-08-13 12:38:07 +02:00
GX2SamplerVar *newVar = (GX2SamplerVar *) malloc((pixelShader->samplerVarCount + 1) * sizeof(GX2SamplerVar));
if (newVar) {
if (pixelShader->samplerVars) {
2018-06-21 20:44:58 +02:00
memcpy(newVar, pixelShader->samplerVars, pixelShader->samplerVarCount * sizeof(GX2SamplerVar));
free(pixelShader->samplerVars);
}
pixelShader->samplerVars = newVar;
memcpy(pixelShader->samplerVars + idx, &var, sizeof(GX2SamplerVar));
2020-08-13 12:38:07 +02:00
pixelShader->samplerVars[idx].name = (char *) malloc(strlen(var.name) + 1);
strcpy((char *) pixelShader->samplerVars[idx].name, var.name);
2018-06-21 20:44:58 +02:00
pixelShader->samplerVarCount++;
}
}
2020-08-13 12:38:07 +02:00
GX2PixelShader *getPixelShader() const {
2018-06-21 20:44:58 +02:00
return pixelShader;
}
void setShader(void) const {
GX2SetPixelShader(pixelShader);
}
2020-08-13 12:38:07 +02:00
static inline void setUniformReg(uint32_t location, uint32_t size, const void *reg) {
GX2SetPixelUniformReg(location, size, (uint32_t *) reg);
2018-06-21 20:44:58 +02:00
}
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
protected:
GX2PixelShader *pixelShader;
};
#endif // PIXEL_SHADER_H