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

168 lines
6.0 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 VERTEX_SHADER_H
#define VERTEX_SHADER_H
#include <string.h>
2019-08-14 23:24:55 +02:00
#include <gui/video/shaders/Shader.h>
2018-06-21 20:44:58 +02:00
#include <gui/gx2_ext.h>
class VertexShader : public Shader {
public:
VertexShader(uint32_t numAttr)
2020-08-13 12:38:07 +02:00
: attributesCount(numAttr), attributes(new GX2AttribStream[attributesCount]), vertexShader((GX2VertexShader *) memalign(0x40, sizeof(GX2VertexShader))) {
if (vertexShader) {
2018-06-21 20:44:58 +02:00
memset(vertexShader, 0, sizeof(GX2VertexShader));
vertexShader->mode = GX2_SHADER_MODE_UNIFORM_REGISTER;
}
}
virtual ~VertexShader() {
2020-08-13 12:38:07 +02:00
delete[] attributes;
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader) {
if (vertexShader->program)
2018-06-21 20:44:58 +02:00
free(vertexShader->program);
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < vertexShader->uniformBlockCount; i++)
free((void *) vertexShader->uniformBlocks[i].name);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader->uniformBlocks)
free((void *) vertexShader->uniformBlocks);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < vertexShader->uniformVarCount; i++)
free((void *) vertexShader->uniformVars[i].name);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader->uniformVars)
free((void *) vertexShader->uniformVars);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader->initialValues)
free((void *) vertexShader->initialValues);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < vertexShader->samplerVarCount; i++)
free((void *) vertexShader->samplerVars[i].name);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader->samplerVars)
free((void *) vertexShader->samplerVars);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < vertexShader->attribVarCount; i++)
free((void *) vertexShader->attribVars[i].name);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader->attribVars)
free((void *) vertexShader->attribVars);
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (vertexShader->loopVars)
free((void *) vertexShader->loopVars);
2018-06-21 20:44:58 +02:00
free(vertexShader);
}
}
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) {
if (!vertexShader)
2018-06-21 20:44:58 +02:00
return;
//! this must be moved into an area where the graphic engine has access to and must be aligned to 0x100
vertexShader->size = programSize;
2020-08-13 12:38:07 +02:00
vertexShader->program = (uint8_t *) memalign(GX2_SHADER_PROGRAM_ALIGNMENT, vertexShader->size);
if (vertexShader->program) {
2018-06-21 20:44:58 +02:00
memcpy(vertexShader->program, program, vertexShader->size);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, vertexShader->program, vertexShader->size);
}
memcpy(&vertexShader->regs, regs, regsSize);
}
2020-08-13 12:38:07 +02:00
void addUniformVar(const GX2UniformVar &var) {
if (!vertexShader)
2018-06-21 20:44:58 +02:00
return;
uint32_t idx = vertexShader->uniformVarCount;
2020-08-13 12:38:07 +02:00
GX2UniformVar *newVar = (GX2UniformVar *) malloc((vertexShader->uniformVarCount + 1) * sizeof(GX2UniformVar));
if (newVar) {
if (vertexShader->uniformVarCount > 0) {
2018-06-21 20:44:58 +02:00
memcpy(newVar, vertexShader->uniformVars, vertexShader->uniformVarCount * sizeof(GX2UniformVar));
free(vertexShader->uniformVars);
}
vertexShader->uniformVars = newVar;
memcpy(vertexShader->uniformVars + idx, &var, sizeof(GX2UniformVar));
2020-08-13 12:38:07 +02:00
vertexShader->uniformVars[idx].name = (char *) malloc(strlen(var.name) + 1);
strcpy((char *) vertexShader->uniformVars[idx].name, var.name);
2018-06-21 20:44:58 +02:00
vertexShader->uniformVarCount++;
}
}
2020-08-13 12:38:07 +02:00
void addAttribVar(const GX2AttribVar &var) {
if (!vertexShader)
2018-06-21 20:44:58 +02:00
return;
uint32_t idx = vertexShader->attribVarCount;
2020-08-13 12:38:07 +02:00
GX2AttribVar *newVar = (GX2AttribVar *) malloc((vertexShader->attribVarCount + 1) * sizeof(GX2AttribVar));
if (newVar) {
if (vertexShader->attribVarCount > 0) {
2018-06-21 20:44:58 +02:00
memcpy(newVar, vertexShader->attribVars, vertexShader->attribVarCount * sizeof(GX2AttribVar));
free(vertexShader->attribVars);
}
vertexShader->attribVars = newVar;
memcpy(vertexShader->attribVars + idx, &var, sizeof(GX2AttribVar));
2020-08-13 12:38:07 +02:00
vertexShader->attribVars[idx].name = (char *) malloc(strlen(var.name) + 1);
strcpy((char *) vertexShader->attribVars[idx].name, var.name);
2018-06-21 20:44:58 +02:00
vertexShader->attribVarCount++;
}
}
2020-08-13 12:38:07 +02:00
static inline void setAttributeBuffer(uint32_t bufferIdx, uint32_t bufferSize, uint32_t stride, const void *buffer) {
GX2SetAttribBuffer(bufferIdx, bufferSize, stride, (void *) buffer);
2018-06-21 20:44:58 +02:00
}
GX2VertexShader *getVertexShader() const {
return vertexShader;
}
void setShader(void) const {
GX2SetVertexShader(vertexShader);
}
2020-08-13 12:38:07 +02:00
GX2AttribStream *getAttributeBuffer(uint32_t idx = 0) const {
if (idx >= attributesCount) {
2018-06-21 20:44:58 +02:00
return NULL;
}
return &attributes[idx];
}
2020-08-13 12:38:07 +02:00
2018-06-21 20:44:58 +02:00
uint32_t getAttributesCount() const {
return attributesCount;
}
2020-08-13 12:38:07 +02:00
static void setUniformReg(uint32_t location, uint32_t size, const void *reg) {
GX2SetVertexUniformReg(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:
uint32_t attributesCount;
GX2AttribStream *attributes;
GX2VertexShader *vertexShader;
};
#endif // VERTEX_SHADER_H