st/nine: Add Render state validation layer
[mesa.git] / src / gallium / state_trackers / nine / nine_state.h
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #ifndef _NINE_STATE_H_
24 #define _NINE_STATE_H_
25
26 #include "d3d9.h"
27 #include "nine_defines.h"
28 #include "pipe/p_state.h"
29 #include "util/list.h"
30
31 #define NINED3DSAMP_MINLOD (D3DSAMP_DMAPOFFSET + 1)
32 #define NINED3DSAMP_SHADOW (D3DSAMP_DMAPOFFSET + 2)
33
34 #define NINED3DRS_VSPOINTSIZE (D3DRS_BLENDOPALPHA + 1)
35 #define NINED3DRS_RTMASK (D3DRS_BLENDOPALPHA + 2)
36 #define NINED3DRS_ALPHACOVERAGE (D3DRS_BLENDOPALPHA + 3)
37
38 #define D3DRS_LAST D3DRS_BLENDOPALPHA
39 #define NINED3DRS_LAST NINED3DRS_ALPHACOVERAGE /* 213 */
40 #define NINED3DSAMP_LAST NINED3DSAMP_SHADOW /* 15 */
41 #define NINED3DTSS_LAST D3DTSS_CONSTANT
42 #define NINED3DTS_LAST D3DTS_WORLDMATRIX(255)
43
44 #define D3DRS_COUNT (D3DRS_LAST + 1)
45 #define NINED3DRS_COUNT (NINED3DRS_LAST + 1)
46 #define NINED3DSAMP_COUNT (NINED3DSAMP_LAST + 1)
47 #define NINED3DTSS_COUNT (NINED3DTSS_LAST + 1)
48 #define NINED3DTS_COUNT (NINED3DTS_LAST + 1)
49
50 #define NINE_STATE_FB (1 << 0)
51 #define NINE_STATE_VIEWPORT (1 << 1)
52 #define NINE_STATE_SCISSOR (1 << 2)
53 #define NINE_STATE_RASTERIZER (1 << 3)
54 #define NINE_STATE_BLEND (1 << 4)
55 #define NINE_STATE_DSA (1 << 5)
56 #define NINE_STATE_VS (1 << 6)
57 #define NINE_STATE_VS_CONST (1 << 7)
58 #define NINE_STATE_PS (1 << 8)
59 #define NINE_STATE_PS_CONST (1 << 9)
60 #define NINE_STATE_TEXTURE (1 << 10)
61 #define NINE_STATE_SAMPLER (1 << 11)
62 #define NINE_STATE_VDECL (1 << 12)
63 #define NINE_STATE_IDXBUF (1 << 13)
64 #define NINE_STATE_STREAMFREQ (1 << 14)
65 #define NINE_STATE_PRIM (1 << 15)
66 #define NINE_STATE_MATERIAL (1 << 16)
67 #define NINE_STATE_BLEND_COLOR (1 << 17)
68 #define NINE_STATE_STENCIL_REF (1 << 18)
69 #define NINE_STATE_SAMPLE_MASK (1 << 19)
70 #define NINE_STATE_FF (0x1f << 20)
71 #define NINE_STATE_FF_VS (0x17 << 20)
72 #define NINE_STATE_FF_PS (0x18 << 20)
73 #define NINE_STATE_FF_LIGHTING (1 << 20)
74 #define NINE_STATE_FF_MATERIAL (1 << 21)
75 #define NINE_STATE_FF_VSTRANSF (1 << 22)
76 #define NINE_STATE_FF_PSSTAGES (1 << 23)
77 #define NINE_STATE_FF_OTHER (1 << 24)
78 #define NINE_STATE_FOG_SHADER (1 << 25)
79 #define NINE_STATE_PS1X_SHADER (1 << 26)
80 #define NINE_STATE_ALL 0x7ffffff
81 #define NINE_STATE_UNHANDLED (1 << 27)
82
83 #define NINE_STATE_COMMIT_DSA (1 << 0)
84 #define NINE_STATE_COMMIT_RASTERIZER (1 << 1)
85 #define NINE_STATE_COMMIT_BLEND (1 << 2)
86 #define NINE_STATE_COMMIT_CONST_VS (1 << 3)
87 #define NINE_STATE_COMMIT_CONST_PS (1 << 4)
88 #define NINE_STATE_COMMIT_VS (1 << 5)
89 #define NINE_STATE_COMMIT_PS (1 << 6)
90
91
92 #define NINE_MAX_SIMULTANEOUS_RENDERTARGETS 4
93 #define NINE_MAX_CONST_F_PS3 224
94 #define NINE_MAX_CONST_F 256
95 #define NINE_MAX_CONST_I 16
96 #define NINE_MAX_CONST_B 16
97 #define NINE_MAX_CONST_ALL 276 /* B consts count only 1/4 th */
98
99 #define NINE_CONST_I_BASE(nconstf) \
100 ((nconstf) * 4 * sizeof(float))
101 #define NINE_CONST_B_BASE(nconstf) \
102 ((nconstf) * 4 * sizeof(float) + \
103 NINE_MAX_CONST_I * 4 * sizeof(int))
104
105
106 #define NINE_MAX_TEXTURE_STAGES 8
107
108 #define NINE_MAX_LIGHTS 65536
109 #define NINE_MAX_LIGHTS_ACTIVE 8
110
111 #define NINED3DLIGHT_INVALID (D3DLIGHT_DIRECTIONAL + 1)
112
113 #define NINE_MAX_SAMPLERS_PS 16
114 #define NINE_MAX_SAMPLERS_VS 4
115 #define NINE_MAX_SAMPLERS 21 /* PS + DMAP + VS */
116 #define NINE_SAMPLER_PS(s) ( 0 + (s))
117 #define NINE_SAMPLER_DMAP 16
118 #define NINE_SAMPLER_VS(s) (17 + (s))
119 #define NINE_PS_SAMPLERS_MASK 0x00ffff
120 #define NINE_VS_SAMPLERS_MASK 0x1e0000
121
122 struct nine_state
123 {
124 struct {
125 uint32_t group;
126 uint32_t rs[(NINED3DRS_COUNT + 31) / 32];
127 uint32_t vtxbuf;
128 uint32_t stream_freq;
129 uint32_t texture;
130 uint16_t sampler[NINE_MAX_SAMPLERS];
131 struct nine_range *vs_const_f;
132 struct nine_range *ps_const_f;
133 uint16_t vs_const_i; /* NINE_MAX_CONST_I == 16 */
134 uint16_t ps_const_i;
135 uint16_t vs_const_b; /* NINE_MAX_CONST_B == 16 */
136 uint16_t ps_const_b;
137 uint8_t ucp;
138 } changed;
139
140 struct NineSurface9 *rt[NINE_MAX_SIMULTANEOUS_RENDERTARGETS];
141 struct NineSurface9 *ds;
142
143 D3DVIEWPORT9 viewport;
144
145 struct pipe_scissor_state scissor;
146
147 /* NOTE: vs, ps will be NULL for FF and are set in device->ff.vs,ps instead
148 * (XXX: or is it better to reference FF shaders here, too ?)
149 * NOTE: const_f contains extra space for const_i,b to use as user constbuf
150 */
151 struct NineVertexShader9 *vs;
152 float *vs_const_f;
153 int vs_const_i[NINE_MAX_CONST_I][4];
154 BOOL vs_const_b[NINE_MAX_CONST_B];
155 float *vs_lconstf_temp;
156 BOOL programmable_vs;
157
158 struct NinePixelShader9 *ps;
159 float *ps_const_f;
160 int ps_const_i[NINE_MAX_CONST_I][4];
161 BOOL ps_const_b[NINE_MAX_CONST_B];
162 float *ps_lconstf_temp;
163 uint32_t bumpmap_vars[6 * NINE_MAX_TEXTURE_STAGES];
164
165 struct {
166 void *vs;
167 void *ps;
168 } cso;
169
170 struct NineVertexDeclaration9 *vdecl;
171
172 struct NineIndexBuffer9 *idxbuf;
173 struct NineVertexBuffer9 *stream[PIPE_MAX_ATTRIBS];
174 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
175 UINT stream_freq[PIPE_MAX_ATTRIBS];
176 uint32_t stream_instancedata_mask; /* derived from stream_freq */
177 uint32_t stream_usage_mask; /* derived from VS and vdecl */
178
179 struct pipe_clip_state clip;
180 struct pipe_framebuffer_state fb;
181 uint8_t rt_mask;
182
183 DWORD rs[NINED3DRS_COUNT];
184 DWORD rs_advertised[NINED3DRS_COUNT]; /* the ones apps get with GetRenderState */
185
186 struct NineBaseTexture9 *texture[NINE_MAX_SAMPLERS]; /* PS, DMAP, VS */
187
188 DWORD samp[NINE_MAX_SAMPLERS][NINED3DSAMP_COUNT];
189 uint32_t samplers_shadow;
190 uint8_t bound_samplers_mask_vs;
191 uint16_t bound_samplers_mask_ps;
192
193 int dummy_vbo_bound_at; /* -1 = not bound , >= 0 = bound index */
194 boolean vbo_bound_done;
195
196 struct {
197 struct {
198 uint32_t group;
199 uint32_t tex_stage[NINE_MAX_TEXTURE_STAGES][(NINED3DTSS_COUNT + 31) / 32];
200 uint32_t transform[(NINED3DTS_COUNT + 31) / 32];
201 } changed;
202
203 D3DMATRIX *transform; /* access only via nine_state_access_transform */
204 unsigned num_transforms;
205
206 /* XXX: Do state blocks just change the set of active lights or do we
207 * have to store which lights have been disabled, too ?
208 */
209 D3DLIGHT9 *light;
210 uint16_t active_light[NINE_MAX_LIGHTS_ACTIVE]; /* 8 */
211 unsigned num_lights;
212 unsigned num_lights_active;
213
214 D3DMATERIAL9 material;
215
216 DWORD tex_stage[NINE_MAX_TEXTURE_STAGES][NINED3DTSS_COUNT];
217 } ff;
218
219 uint32_t commit;
220 struct {
221 struct pipe_depth_stencil_alpha_state dsa;
222 struct pipe_rasterizer_state rast;
223 struct pipe_blend_state blend;
224 struct pipe_constant_buffer cb_vs;
225 struct pipe_constant_buffer cb_ps;
226 struct pipe_constant_buffer cb_vs_ff;
227 struct pipe_constant_buffer cb_ps_ff;
228 } pipe;
229 };
230
231 /* map D3DRS -> NINE_STATE_x
232 */
233 extern const uint32_t nine_render_state_group[NINED3DRS_COUNT];
234
235 /* for D3DSBT_PIXEL/VERTEX:
236 */
237 extern const uint32_t nine_render_states_pixel[(NINED3DRS_COUNT + 31) / 32];
238 extern const uint32_t nine_render_states_vertex[(NINED3DRS_COUNT + 31) / 32];
239
240 struct NineDevice9;
241
242 void nine_update_state_framebuffer(struct NineDevice9 *);
243 boolean nine_update_state(struct NineDevice9 *);
244
245 void nine_state_restore_non_cso(struct NineDevice9 *device);
246 void nine_state_set_defaults(struct NineDevice9 *, const D3DCAPS9 *,
247 boolean is_reset);
248 void nine_state_clear(struct nine_state *, const boolean device);
249
250 /* If @alloc is FALSE, the return value may be a const identity matrix.
251 * Therefore, do not modify if you set alloc to FALSE !
252 */
253 D3DMATRIX *
254 nine_state_access_transform(struct nine_state *, D3DTRANSFORMSTATETYPE,
255 boolean alloc);
256
257 const char *nine_d3drs_to_string(DWORD State);
258
259 #endif /* _NINE_STATE_H_ */