citro3d
texenv.h
Go to the documentation of this file.
1 /**
2  * @file texenv.h
3  * @brief Configure texture combiner stages (TexEnv)
4  * @see https://www.khronos.org/opengl/wiki/Texture_Combiners
5  * @see https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTexEnv.xml
6  */
7 #pragma once
8 #include "types.h"
9 
10 /// TexEnv stage configuration
11 typedef struct
12 {
13  u16 srcRgb, srcAlpha;
14  union
15  {
16  u32 opAll;
17  struct { u32 opRgb:12, opAlpha:12; };
18  };
19  u16 funcRgb, funcAlpha;
20  u32 color;
21  u16 scaleRgb, scaleAlpha;
22 } C3D_TexEnv;
23 
24 /// TexEnv operation mode
25 typedef enum
26 {
27  C3D_RGB = BIT(0), ///< RGB mode
28  C3D_Alpha = BIT(1), ///< Alpha mode
29  C3D_Both = C3D_RGB | C3D_Alpha, ///< Both
31 
32 /**
33  * @brief Gets the global TexEnv for a given stage.
34  * @param[in] id TexEnv stage between 0-5 to return.
35  * @return TexEnv of the given stage.
36  */
38 
39 /**
40  * @brief Sets the global TexEnv for a given stage.
41  * @param[in] id TexEnv stage between 0-5 to set.
42  * @param[in] env Pointer to user TexEnv.
43  */
44 void C3D_SetTexEnv(int id, C3D_TexEnv* env);
45 
46 /**
47  * @brief Marks a TexEnv as needing to be updated.
48  * @note One must use this if they are continuing to use a TexEnv pointer they got from \ref C3D_GetTexEnv() after performing an action that flushes state.
49  * @param[in] env Pointer to a TexEnv struct.
50  */
52 
53 /**
54  * @brief Configures the stages where the GPU_PREVIOUS_BUFFER source value should be updated with the output of that stage.
55  * @param[in] mode TexEnv update modes (see \ref C3D_TexEnvMode)
56  * @param[in] mask Bitmask containing which stages update GPU_PREVIOUS_BUFFER (bitmask can be created using \ref GPU_TEV_BUFFER_WRITE_CONFIG())
57  */
58 void C3D_TexEnvBufUpdate(int mode, int mask);
59 
60 /**
61  * @brief Configure the initial value of GPU_PREVIOUS_BUFFER. This value will be kept until it is updated; see \ref C3D_TexEnvBufUpdate().
62  * @param[in] color Color value.
63  */
64 void C3D_TexEnvBufColor(u32 color);
65 
66 /**
67  * @brief Resets a TexEnv to its default values.
68  * @param[out] env TexEnv to initialize.
69  */
70 static inline void C3D_TexEnvInit(C3D_TexEnv* env)
71 {
72  env->srcRgb = GPU_TEVSOURCES(GPU_PREVIOUS, 0, 0);
73  env->srcAlpha = env->srcRgb;
74  env->opAll = 0;
75  env->funcRgb = GPU_REPLACE;
76  env->funcAlpha = env->funcRgb;
77  env->color = 0xFFFFFFFF;
78  env->scaleRgb = GPU_TEVSCALE_1;
79  env->scaleAlpha = GPU_TEVSCALE_1;
80 }
81 
82 #ifdef __cplusplus
83 #define _C3D_DEFAULT(x) = x
84 #else
85 #define _C3D_DEFAULT(x)
86 #endif
87 
88 /**
89  * @brief Sets the input source of a TexEnv.
90  * @param[out] env Pointer to TexEnv struct.
91  * @param[in] mode TexEnv update modes (see \ref C3D_TexEnvMode)
92  * @param[in] s1 First source.
93  * @param[in] s2 Second source.
94  * @param[in] s3 Third source.
95  */
96 static inline void C3D_TexEnvSrc(C3D_TexEnv* env, C3D_TexEnvMode mode,
97  GPU_TEVSRC s1,
98  GPU_TEVSRC s2 _C3D_DEFAULT(GPU_PRIMARY_COLOR),
99  GPU_TEVSRC s3 _C3D_DEFAULT(GPU_PRIMARY_COLOR))
100 {
101  int param = GPU_TEVSOURCES((int)s1, (int)s2, (int)s3);
102  if ((int)mode & C3D_RGB)
103  env->srcRgb = param;
104  if ((int)mode & C3D_Alpha)
105  env->srcAlpha = param;
106 }
107 
108 /**
109  * @brief Configures the operation to be applied to the input color of a TexEnv before the function is applied.
110  * @param[out] env Pointer to TexEnv struct.
111  * @param[in] o1 Operation to perform on the first source.
112  * @param[in] o2 Operation to perform on the second source.
113  * @param[in] o3 Operation to perform on the third source.
114  */
115 static inline void C3D_TexEnvOpRgb(C3D_TexEnv* env,
116  GPU_TEVOP_RGB o1,
117  GPU_TEVOP_RGB o2 _C3D_DEFAULT(GPU_TEVOP_RGB_SRC_COLOR),
118  GPU_TEVOP_RGB o3 _C3D_DEFAULT(GPU_TEVOP_RGB_SRC_COLOR))
119 {
120  env->opRgb = GPU_TEVOPERANDS((int)o1, (int)o2, (int)o3);
121 }
122 
123 /**
124  * @brief Configures the operation to be applied to the input alpha of a TexEnv before the function is applied.
125  * @param[out] env Pointer to TexEnv struct.
126  * @param[in] o1 Operation to perform on the first source.
127  * @param[in] o2 Operation to perform on the second source.
128  * @param[in] o3 Operation to perform on the third source.
129  */
130 static inline void C3D_TexEnvOpAlpha(C3D_TexEnv* env,
131  GPU_TEVOP_A o1,
132  GPU_TEVOP_A o2 _C3D_DEFAULT(GPU_TEVOP_A_SRC_ALPHA),
133  GPU_TEVOP_A o3 _C3D_DEFAULT(GPU_TEVOP_A_SRC_ALPHA))
134 {
135  env->opAlpha = GPU_TEVOPERANDS((int)o1, (int)o2, (int)o3);
136 }
137 
138 /**
139  * @brief Sets the combiner function to perform in this TexEnv.
140  * @param[out] env Pointer to TexEnv struct.
141  * @param[in] mode TexEnv update modes (see \ref C3D_TexEnvMode)
142  * @param[in] param Function to use.
143  */
144 static inline void C3D_TexEnvFunc(C3D_TexEnv* env, C3D_TexEnvMode mode, GPU_COMBINEFUNC param)
145 {
146  if ((int)mode & C3D_RGB)
147  env->funcRgb = param;
148  if ((int)mode & C3D_Alpha)
149  env->funcAlpha = param;
150 }
151 
152 /**
153  * @brief Sets the value of the GPU_CONSTANT source for a TexEnv stage.
154  * @param[out] env Pointer to TexEnv struct.
155  * @param[in] color RGBA color value to apply.
156  */
157 static inline void C3D_TexEnvColor(C3D_TexEnv* env, u32 color)
158 {
159  env->color = color;
160 }
161 
162 /**
163  * @brief Configures the scaling to be applied to the output of a TexEnv.
164  * @param[out] env Pointer to TexEnv struct.
165  * @param[in] mode TexEnv update modes (see \ref C3D_TexEnvMode)
166  * @param[in] param Scale factor to apply.
167  */
168 static inline void C3D_TexEnvScale(C3D_TexEnv* env, int mode, GPU_TEVSCALE param)
169 {
170  if (mode & C3D_RGB)
171  env->scaleRgb = param;
172  if (mode & C3D_Alpha)
173  env->scaleAlpha = param;
174 }
175 
176 #undef _C3D_DEFAULT
GPU_COMBINEFUNC
GPU_REPLACE
GPU_TEVSCALE
GPU_TEVSCALE_1
GPU_TEVOP_RGB
GPU_TEVOP_A
GPU_TEVSRC
GPU_PREVIOUS
#define GPU_TEVOPERANDS(a, b, c)
#define GPU_TEVSOURCES(a, b, c)
TexEnv stage configuration.
Definition: texenv.h:12
static void C3D_TexEnvOpRgb(C3D_TexEnv *env, GPU_TEVOP_RGB o1, GPU_TEVOP_RGB o2, GPU_TEVOP_RGB o3)
Configures the operation to be applied to the input color of a TexEnv before the function is applied.
Definition: texenv.h:115
void C3D_SetTexEnv(int id, C3D_TexEnv *env)
Sets the global TexEnv for a given stage.
static void C3D_TexEnvColor(C3D_TexEnv *env, u32 color)
Sets the value of the GPU_CONSTANT source for a TexEnv stage.
Definition: texenv.h:157
static void C3D_TexEnvOpAlpha(C3D_TexEnv *env, GPU_TEVOP_A o1, GPU_TEVOP_A o2, GPU_TEVOP_A o3)
Configures the operation to be applied to the input alpha of a TexEnv before the function is applied.
Definition: texenv.h:130
void C3D_TexEnvBufColor(u32 color)
Configure the initial value of GPU_PREVIOUS_BUFFER. This value will be kept until it is updated; see ...
static void C3D_TexEnvSrc(C3D_TexEnv *env, C3D_TexEnvMode mode, GPU_TEVSRC s1, GPU_TEVSRC s2, GPU_TEVSRC s3)
Sets the input source of a TexEnv.
Definition: texenv.h:96
C3D_TexEnv * C3D_GetTexEnv(int id)
Gets the global TexEnv for a given stage.
static void C3D_TexEnvFunc(C3D_TexEnv *env, C3D_TexEnvMode mode, GPU_COMBINEFUNC param)
Sets the combiner function to perform in this TexEnv.
Definition: texenv.h:144
void C3D_DirtyTexEnv(C3D_TexEnv *env)
Marks a TexEnv as needing to be updated.
static void C3D_TexEnvScale(C3D_TexEnv *env, int mode, GPU_TEVSCALE param)
Configures the scaling to be applied to the output of a TexEnv.
Definition: texenv.h:168
static void C3D_TexEnvInit(C3D_TexEnv *env)
Resets a TexEnv to its default values.
Definition: texenv.h:70
C3D_TexEnvMode
TexEnv operation mode.
Definition: texenv.h:26
@ C3D_RGB
RGB mode.
Definition: texenv.h:27
@ C3D_Both
Both.
Definition: texenv.h:29
@ C3D_Alpha
Alpha mode.
Definition: texenv.h:28
void C3D_TexEnvBufUpdate(int mode, int mask)
Configures the stages where the GPU_PREVIOUS_BUFFER source value should be updated with the output of...
#define BIT(n)
uint16_t u16
uint32_t u32