libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
VertexShader.h
1/****************************************************************************
2 * Platform Abstraction Layer (WUT driver)
3 * Daryl Borth 2026
4 * VertexShader.h
5 ***************************************************************************/
6#ifndef WUT_VERTEX_SHADER_H_
7#define WUT_VERTEX_SHADER_H_
8
9#include <string.h>
10#include <malloc.h>
11#include "Shader.h"
12
17class VertexShader : public Shader
18{
19 public:
21 VertexShader(uint32_t numAttr)
22 : attributesCount(numAttr)
23 , attributes(new GX2AttribStream[attributesCount])
24 , vertexShader(static_cast<GX2VertexShader *>(memalign(0x40, sizeof(GX2VertexShader))))
25 {
26 if(vertexShader)
27 {
28 memset(vertexShader, 0, sizeof(GX2VertexShader));
29 vertexShader->mode = GX2_SHADER_MODE_UNIFORM_REGISTER;
30 }
31 }
32
33 virtual ~VertexShader()
34 {
35 delete [] attributes;
36
37 if(vertexShader)
38 {
39 if(vertexShader->program)
40 free(vertexShader->program);
41
42 for(uint32_t i = 0; i < vertexShader->uniformVarCount; i++)
43 free(const_cast<char *>(vertexShader->uniformVars[i].name));
44 if(vertexShader->uniformVars)
45 free(vertexShader->uniformVars);
46
47 for(uint32_t i = 0; i < vertexShader->attribVarCount; i++)
48 free(const_cast<char *>(vertexShader->attribVars[i].name));
49 if(vertexShader->attribVars)
50 free(vertexShader->attribVars);
51
52 free(vertexShader);
53 }
54 }
55
61 void setProgram(const uint32_t * program, uint32_t programSize, const uint32_t * regs, uint32_t regsSize)
62 {
63 if(!vertexShader)
64 return;
65
66 // This must live where the GPU can see it, aligned to 0x100.
67 vertexShader->size = programSize;
68 vertexShader->program = memalign(GX2_SHADER_PROGRAM_ALIGNMENT, vertexShader->size);
69 if(vertexShader->program)
70 {
71 memcpy(vertexShader->program, program, vertexShader->size);
72 GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, vertexShader->program, vertexShader->size);
73 }
74
75 memcpy(&vertexShader->regs, regs, regsSize);
76 }
77
79 void addUniformVar(const GX2UniformVar & var)
80 {
81 if(!vertexShader)
82 return;
83
84 uint32_t idx = vertexShader->uniformVarCount;
85 GX2UniformVar * newVar = static_cast<GX2UniformVar *>(malloc((idx + 1) * sizeof(GX2UniformVar)));
86 if(!newVar)
87 return;
88
89 if(idx > 0)
90 {
91 memcpy(newVar, vertexShader->uniformVars, idx * sizeof(GX2UniformVar));
92 free(vertexShader->uniformVars);
93 }
94 vertexShader->uniformVars = newVar;
95 memcpy(vertexShader->uniformVars + idx, &var, sizeof(GX2UniformVar));
96 vertexShader->uniformVars[idx].name = strdup(var.name);
97 vertexShader->uniformVarCount++;
98 }
99
101 void addAttribVar(const GX2AttribVar & var)
102 {
103 if(!vertexShader)
104 return;
105
106 uint32_t idx = vertexShader->attribVarCount;
107 GX2AttribVar * newVar = static_cast<GX2AttribVar *>(malloc((idx + 1) * sizeof(GX2AttribVar)));
108 if(!newVar)
109 return;
110
111 if(idx > 0)
112 {
113 memcpy(newVar, vertexShader->attribVars, idx * sizeof(GX2AttribVar));
114 free(vertexShader->attribVars);
115 }
116 vertexShader->attribVars = newVar;
117 memcpy(vertexShader->attribVars + idx, &var, sizeof(GX2AttribVar));
118 vertexShader->attribVars[idx].name = strdup(var.name);
119 vertexShader->attribVarCount++;
120 }
121
127 static inline void setAttributeBuffer(uint32_t bufferIdx, uint32_t bufferSize, uint32_t stride, const void * buffer)
128 {
129 GX2SetAttribBuffer(bufferIdx, bufferSize, stride, buffer);
130 }
131
133 void setShader() const
134 {
135 GX2SetVertexShader(vertexShader);
136 }
137
139 GX2AttribStream * getAttributeBuffer(uint32_t idx = 0) const
140 {
141 if(idx >= attributesCount)
142 return nullptr;
143 return &attributes[idx];
144 }
145
147 uint32_t getAttributesCount() const { return attributesCount; }
148
153 static void setUniformReg(uint32_t location, uint32_t size, const void * reg)
154 {
155 GX2SetVertexUniformReg(location, size, reg);
156 }
157
158 protected:
160 GX2AttribStream * attributes;
161 GX2VertexShader * vertexShader;
162};
163
164#endif // WUT_VERTEX_SHADER_H_
Owns a GX2VertexShader's compiled program and uniform/attribute metadata.
uint32_t attributesCount
Number of attribute streams.
void addUniformVar(const GX2UniformVar &var)
Registers a uniform variable so it can be set by location.
void addAttribVar(const GX2AttribVar &var)
Registers an input attribute variable.
void setShader() const
Binds this vertex shader for subsequent draws.
GX2VertexShader * vertexShader
The GX2 vertex shader object.
VertexShader(uint32_t numAttr)
static void setUniformReg(uint32_t location, uint32_t size, const void *reg)
Writes uniform registers for the bound vertex shader.
static void setAttributeBuffer(uint32_t bufferIdx, uint32_t bufferSize, uint32_t stride, const void *buffer)
Binds a vertex data buffer to an attribute buffer slot.
uint32_t getAttributesCount() const
GX2AttribStream * attributes
Attribute stream descriptions handed to the fetch shader.
void setProgram(const uint32_t *program, uint32_t programSize, const uint32_t *regs, uint32_t regsSize)
Uploads the compiled shader program.
GX2AttribStream * getAttributeBuffer(uint32_t idx=0) const