citro3d
uniforms.h
Go to the documentation of this file.
1 /**
2  * @file uniforms.h
3  * @brief Write to shader uniforms
4  */
5 #pragma once
6 #include "maths.h"
7 
8 #define C3D_FVUNIF_COUNT 96
9 #define C3D_IVUNIF_COUNT 4
10 
11 extern C3D_FVec C3D_FVUnif[2][C3D_FVUNIF_COUNT];
12 extern C3D_IVec C3D_IVUnif[2][C3D_IVUNIF_COUNT];
13 extern u16 C3D_BoolUnifs[2];
14 
15 extern bool C3D_FVUnifDirty[2][C3D_FVUNIF_COUNT];
16 extern bool C3D_IVUnifDirty[2][C3D_IVUNIF_COUNT];
17 extern bool C3D_BoolUnifsDirty[2];
18 
19 /**
20  * @brief Provides a writable pointer for a floating-point uniform.
21  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
22  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
23  * @param[in] size Number of registers allocated for this uniform.
24  * @return Writable pointer. This should not be freed.
25  */
26 static inline C3D_FVec* C3D_FVUnifWritePtr(GPU_SHADER_TYPE type, int id, int size)
27 {
28  int i;
29  for (i = 0; i < size; i ++)
30  C3D_FVUnifDirty[type][id+i] = true;
31  return &C3D_FVUnif[type][id];
32 }
33 
34 /**
35  * @brief Provides a writable pointer for an integer uniform.
36  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
37  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
38  * @return Writable pointer. This should not be freed.
39  */
40 static inline C3D_IVec* C3D_IVUnifWritePtr(GPU_SHADER_TYPE type, int id)
41 {
42  id -= 0x60;
43  C3D_IVUnifDirty[type][id] = true;
44  return &C3D_IVUnif[type][id];
45 }
46 
47 /**
48  * @brief Writes an Nx4 matrix to the uniform registers.
49  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
50  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
51  * @param[in] mtx Matrix to be written.
52  * @param[in] num Row count of the matrix.
53  * @remark Usually, one should use the helper functions for 4x4, 3x4, and 2x4 matrices listed below.
54  */
55 static inline void C3D_FVUnifMtxNx4(GPU_SHADER_TYPE type, int id, const C3D_Mtx* mtx, int num)
56 {
57  int i;
58  C3D_FVec* ptr = C3D_FVUnifWritePtr(type, id, num);
59  for (i = 0; i < num; i ++)
60  ptr[i] = mtx->r[i]; // Struct copy.
61 }
62 
63 /**
64  * @brief Writes a 4x4 matrix to the uniform registers.
65  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
66  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
67  * @param[in] mtx Matrix to be written.
68  */
69 static inline void C3D_FVUnifMtx4x4(GPU_SHADER_TYPE type, int id, const C3D_Mtx* mtx)
70 {
71  C3D_FVUnifMtxNx4(type, id, mtx, 4);
72 }
73 
74 /**
75  * @brief Writes a 3x4 matrix to the uniform registers.
76  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
77  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
78  * @param[in] mtx Matrix to be written.
79  */
80 static inline void C3D_FVUnifMtx3x4(GPU_SHADER_TYPE type, int id, const C3D_Mtx* mtx)
81 {
82  C3D_FVUnifMtxNx4(type, id, mtx, 3);
83 }
84 
85 /**
86  * @brief Writes a 2x4 matrix to the uniform registers.
87  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
88  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
89  * @param[in] mtx Matrix to be written.
90  */
91 static inline void C3D_FVUnifMtx2x4(GPU_SHADER_TYPE type, int id, const C3D_Mtx* mtx)
92 {
93  C3D_FVUnifMtxNx4(type, id, mtx, 2);
94 }
95 
96 /**
97  * @brief Writes a 4-component floating-point vector to the uniform registers.
98  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
99  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
100  * @param[in] x X component of the vector.
101  * @param[in] y Y component of the vector.
102  * @param[in] z Z component of the vector.
103  * @param[in] w W component of the vector.
104  */
105 static inline void C3D_FVUnifSet(GPU_SHADER_TYPE type, int id, float x, float y, float z, float w)
106 {
107  C3D_FVec* ptr = C3D_FVUnifWritePtr(type, id, 1);
108  ptr->x = x;
109  ptr->y = y;
110  ptr->z = z;
111  ptr->w = w;
112 }
113 
114 /**
115  * @brief Writes a 4-component integer vector to the uniform registers.
116  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
117  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
118  * @param[in] x X component of the vector.
119  * @param[in] y Y component of the vector.
120  * @param[in] z Z component of the vector.
121  * @param[in] w W component of the vector.
122  */
123 static inline void C3D_IVUnifSet(GPU_SHADER_TYPE type, int id, int x, int y, int z, int w)
124 {
125  C3D_IVec* ptr = C3D_IVUnifWritePtr(type, id);
126  *ptr = IVec_Pack(x, y, z, w);
127 }
128 
129 /**
130  * @brief Writes a boolean value to the uniform registers.
131  * @param[in] type \ref GPU_SHADER_TYPE of the uniform to be set.
132  * @param[in] id ID of the uniform (retrievable using libctru's \ref shaderInstanceGetUniformLocation())
133  * @param[in] value Boolean value to write.
134  */
135 static inline void C3D_BoolUnifSet(GPU_SHADER_TYPE type, int id, bool value)
136 {
137  id -= 0x68;
138  C3D_BoolUnifsDirty[type] = true;
139  if (value)
140  C3D_BoolUnifs[type] |= BIT(id);
141  else
142  C3D_BoolUnifs[type] &= ~BIT(id);
143 }
144 
145 /**
146  * @brief Flushes newly-updated uniforms to the uniform registers.
147  * @param[in] type \ref GPU_SHADER_TYPE of the uniforms to be flushed.
148  * @remark This is called internally, and generally does not need to be handled by the user.
149  */
GPU_SHADER_TYPE
Basic math library for matrix, vector, and quaternion operations.
#define BIT(n)
static C3D_IVec IVec_Pack(u8 x, u8 y, u8 z, u8 w)
Packs 4 u8 integers into a vector.
Definition: types.h:34
uint16_t u16
u32 C3D_IVec
Integer vector.
Definition: types.h:24
static void C3D_BoolUnifSet(GPU_SHADER_TYPE type, int id, bool value)
Writes a boolean value to the uniform registers.
Definition: uniforms.h:135
static C3D_FVec * C3D_FVUnifWritePtr(GPU_SHADER_TYPE type, int id, int size)
Provides a writable pointer for a floating-point uniform.
Definition: uniforms.h:26
static C3D_IVec * C3D_IVUnifWritePtr(GPU_SHADER_TYPE type, int id)
Provides a writable pointer for an integer uniform.
Definition: uniforms.h:40
static void C3D_FVUnifMtxNx4(GPU_SHADER_TYPE type, int id, const C3D_Mtx *mtx, int num)
Writes an Nx4 matrix to the uniform registers.
Definition: uniforms.h:55
static void C3D_FVUnifSet(GPU_SHADER_TYPE type, int id, float x, float y, float z, float w)
Writes a 4-component floating-point vector to the uniform registers.
Definition: uniforms.h:105
static void C3D_IVUnifSet(GPU_SHADER_TYPE type, int id, int x, int y, int z, int w)
Writes a 4-component integer vector to the uniform registers.
Definition: uniforms.h:123
static void C3D_FVUnifMtx2x4(GPU_SHADER_TYPE type, int id, const C3D_Mtx *mtx)
Writes a 2x4 matrix to the uniform registers.
Definition: uniforms.h:91
static void C3D_FVUnifMtx4x4(GPU_SHADER_TYPE type, int id, const C3D_Mtx *mtx)
Writes a 4x4 matrix to the uniform registers.
Definition: uniforms.h:69
static void C3D_FVUnifMtx3x4(GPU_SHADER_TYPE type, int id, const C3D_Mtx *mtx)
Writes a 3x4 matrix to the uniform registers.
Definition: uniforms.h:80
void C3D_UpdateUniforms(GPU_SHADER_TYPE type)
Flushes newly-updated uniforms to the uniform registers.
Float vector.
Definition: types.h:52
float x
X-component.
Definition: types.h:61
float z
Z-component.
Definition: types.h:59
float w
W-component.
Definition: types.h:58
float y
Y-component.
Definition: types.h:60
Row-major 4x4 matrix.
Definition: types.h:91
C3D_FVec r[4]
Rows are vectors.
Definition: types.h:92