libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
PixelShader.h
1/****************************************************************************
2 * Platform Abstraction Layer (WUT driver)
3 * Daryl Borth 2026
4 * PixelShader.h
5 ***************************************************************************/
6#ifndef WUT_PIXEL_SHADER_H_
7#define WUT_PIXEL_SHADER_H_
8
9#include <malloc.h>
10#include <string.h>
11#include "Shader.h"
12
16class PixelShader : public Shader
17{
18 public:
19 PixelShader()
20 : pixelShader(static_cast<GX2PixelShader *>(memalign(0x40, sizeof(GX2PixelShader))))
21 {
22 if(pixelShader)
23 {
24 memset(pixelShader, 0, sizeof(GX2PixelShader));
25 pixelShader->mode = GX2_SHADER_MODE_UNIFORM_REGISTER;
26 }
27 }
28
29 virtual ~PixelShader()
30 {
31 if(pixelShader)
32 {
33 if(pixelShader->program)
34 free(pixelShader->program);
35
36 for(uint32_t i = 0; i < pixelShader->uniformVarCount; i++)
37 free(const_cast<char *>(pixelShader->uniformVars[i].name));
38 if(pixelShader->uniformVars)
39 free(pixelShader->uniformVars);
40
41 for(uint32_t i = 0; i < pixelShader->samplerVarCount; i++)
42 free(const_cast<char *>(pixelShader->samplerVars[i].name));
43 if(pixelShader->samplerVars)
44 free(pixelShader->samplerVars);
45
46 free(pixelShader);
47 }
48 }
49
55 void setProgram(const uint32_t * program, uint32_t programSize, const uint32_t * regs, uint32_t regsSize)
56 {
57 if(!pixelShader)
58 return;
59
60 pixelShader->size = programSize;
61 pixelShader->program = memalign(GX2_SHADER_PROGRAM_ALIGNMENT, pixelShader->size);
62 if(pixelShader->program)
63 {
64 memcpy(pixelShader->program, program, pixelShader->size);
65 GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, pixelShader->program, pixelShader->size);
66 }
67
68 memcpy(&pixelShader->regs, regs, regsSize);
69 }
70
72 void addUniformVar(const GX2UniformVar & var)
73 {
74 if(!pixelShader)
75 return;
76
77 uint32_t idx = pixelShader->uniformVarCount;
78 GX2UniformVar * newVar = static_cast<GX2UniformVar *>(malloc((idx + 1) * sizeof(GX2UniformVar)));
79 if(!newVar)
80 return;
81
82 if(idx > 0)
83 {
84 memcpy(newVar, pixelShader->uniformVars, idx * sizeof(GX2UniformVar));
85 free(pixelShader->uniformVars);
86 }
87 pixelShader->uniformVars = newVar;
88 memcpy(pixelShader->uniformVars + idx, &var, sizeof(GX2UniformVar));
89 pixelShader->uniformVars[idx].name = strdup(var.name);
90 pixelShader->uniformVarCount++;
91 }
92
94 void addSamplerVar(const GX2SamplerVar & var)
95 {
96 if(!pixelShader)
97 return;
98
99 uint32_t idx = pixelShader->samplerVarCount;
100 GX2SamplerVar * newVar = static_cast<GX2SamplerVar *>(malloc((idx + 1) * sizeof(GX2SamplerVar)));
101 if(!newVar)
102 return;
103
104 if(idx > 0)
105 {
106 memcpy(newVar, pixelShader->samplerVars, idx * sizeof(GX2SamplerVar));
107 free(pixelShader->samplerVars);
108 }
109 pixelShader->samplerVars = newVar;
110 memcpy(pixelShader->samplerVars + idx, &var, sizeof(GX2SamplerVar));
111 pixelShader->samplerVars[idx].name = strdup(var.name);
112 pixelShader->samplerVarCount++;
113 }
114
116 GX2PixelShader * getPixelShader() const { return pixelShader; }
117
119 void setShader() const
120 {
121 GX2SetPixelShader(pixelShader);
122 }
123
128 static inline void setUniformReg(uint32_t location, uint32_t size, const void * reg)
129 {
130 GX2SetPixelUniformReg(location, size, reg);
131 }
132
133 protected:
134 GX2PixelShader * pixelShader;
135};
136
137#endif // WUT_PIXEL_SHADER_H_
void addUniformVar(const GX2UniformVar &var)
Registers a uniform variable so it can be set by location.
Definition PixelShader.h:72
void setProgram(const uint32_t *program, uint32_t programSize, const uint32_t *regs, uint32_t regsSize)
Uploads the compiled shader program.
Definition PixelShader.h:55
void setShader() const
Binds this pixel shader for subsequent draws.
GX2PixelShader * getPixelShader() const
GX2PixelShader * pixelShader
The GX2 pixel shader object.
void addSamplerVar(const GX2SamplerVar &var)
Registers a texture sampler variable.
Definition PixelShader.h:94
static void setUniformReg(uint32_t location, uint32_t size, const void *reg)
Writes uniform registers for the bound pixel shader.