st/nine: Move stream freq data to nine_context
[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 #define NINED3DSAMP_CUBETEX (D3DSAMP_DMAPOFFSET + 3)
34
35 #define NINED3DRS_VSPOINTSIZE (D3DRS_BLENDOPALPHA + 1)
36 #define NINED3DRS_RTMASK (D3DRS_BLENDOPALPHA + 2)
37 /* ALPHACOVERAGE:
38 * bit 0: enable alpha coverage
39 * bit 1: ATOC is on
40 */
41 #define NINED3DRS_ALPHACOVERAGE (D3DRS_BLENDOPALPHA + 3)
42 #define NINED3DRS_MULTISAMPLE (D3DRS_BLENDOPALPHA + 4)
43
44 #define D3DRS_LAST D3DRS_BLENDOPALPHA
45 #define NINED3DRS_LAST NINED3DRS_MULTISAMPLE /* 214 */
46 #define NINED3DSAMP_LAST NINED3DSAMP_CUBETEX /* 16 */
47 #define NINED3DTSS_LAST D3DTSS_CONSTANT
48 #define NINED3DTS_LAST D3DTS_WORLDMATRIX(255)
49
50 #define D3DRS_COUNT (D3DRS_LAST + 1)
51 #define NINED3DRS_COUNT (NINED3DRS_LAST + 1)
52 #define NINED3DSAMP_COUNT (NINED3DSAMP_LAST + 1)
53 #define NINED3DTSS_COUNT (NINED3DTSS_LAST + 1)
54 #define NINED3DTS_COUNT (NINED3DTS_LAST + 1)
55
56 #define NINE_STATE_FB (1 << 0)
57 #define NINE_STATE_VIEWPORT (1 << 1)
58 #define NINE_STATE_SCISSOR (1 << 2)
59 #define NINE_STATE_RASTERIZER (1 << 3)
60 #define NINE_STATE_BLEND (1 << 4)
61 #define NINE_STATE_DSA (1 << 5)
62 #define NINE_STATE_VS (1 << 6)
63 #define NINE_STATE_VS_CONST (1 << 7)
64 #define NINE_STATE_PS (1 << 8)
65 #define NINE_STATE_PS_CONST (1 << 9)
66 #define NINE_STATE_TEXTURE (1 << 10)
67 #define NINE_STATE_SAMPLER (1 << 11)
68 #define NINE_STATE_VDECL (1 << 12)
69 #define NINE_STATE_IDXBUF (1 << 13)
70 #define NINE_STATE_STREAMFREQ (1 << 14)
71 #define NINE_STATE_PRIM (1 << 15)
72 #define NINE_STATE_MATERIAL (1 << 16)
73 #define NINE_STATE_BLEND_COLOR (1 << 17)
74 #define NINE_STATE_STENCIL_REF (1 << 18)
75 #define NINE_STATE_SAMPLE_MASK (1 << 19)
76 #define NINE_STATE_FF (0x1f << 20)
77 #define NINE_STATE_FF_VS (0x17 << 20)
78 #define NINE_STATE_FF_PS (0x18 << 20)
79 #define NINE_STATE_FF_LIGHTING (1 << 20)
80 #define NINE_STATE_FF_MATERIAL (1 << 21)
81 #define NINE_STATE_FF_VSTRANSF (1 << 22)
82 #define NINE_STATE_FF_PSSTAGES (1 << 23)
83 #define NINE_STATE_FF_OTHER (1 << 24)
84 #define NINE_STATE_FOG_SHADER (1 << 25)
85 #define NINE_STATE_PS1X_SHADER (1 << 26)
86 #define NINE_STATE_POINTSIZE_SHADER (1 << 27)
87 #define NINE_STATE_MULTISAMPLE (1 << 28)
88 #define NINE_STATE_SWVP (1 << 29)
89 #define NINE_STATE_ALL 0x3fffffff
90 #define NINE_STATE_UNHANDLED (1 << 30)
91
92 #define NINE_STATE_COMMIT_DSA (1 << 0)
93 #define NINE_STATE_COMMIT_RASTERIZER (1 << 1)
94 #define NINE_STATE_COMMIT_BLEND (1 << 2)
95 #define NINE_STATE_COMMIT_CONST_VS (1 << 3)
96 #define NINE_STATE_COMMIT_CONST_PS (1 << 4)
97 #define NINE_STATE_COMMIT_VS (1 << 5)
98 #define NINE_STATE_COMMIT_PS (1 << 6)
99
100
101 #define NINE_MAX_SIMULTANEOUS_RENDERTARGETS 4
102 #define NINE_MAX_CONST_F_PS3 224
103 #define NINE_MAX_CONST_F 256
104 #define NINE_MAX_CONST_I 16
105 #define NINE_MAX_CONST_B 16
106 #define NINE_MAX_CONST_F_SWVP 8192
107 #define NINE_MAX_CONST_I_SWVP 2048
108 #define NINE_MAX_CONST_B_SWVP 2048
109 #define NINE_MAX_CONST_ALL 276 /* B consts count only 1/4 th */
110
111 #define NINE_CONST_I_BASE(nconstf) \
112 ((nconstf) * 4 * sizeof(float))
113 #define NINE_CONST_B_BASE(nconstf) \
114 ((nconstf) * 4 * sizeof(float) + \
115 NINE_MAX_CONST_I * 4 * sizeof(int))
116
117
118 #define NINE_MAX_TEXTURE_STAGES 8
119
120 #define NINE_MAX_LIGHTS 65536
121 #define NINE_MAX_LIGHTS_ACTIVE 8
122
123 #define NINED3DLIGHT_INVALID (D3DLIGHT_DIRECTIONAL + 1)
124
125 #define NINE_MAX_SAMPLERS_PS 16
126 #define NINE_MAX_SAMPLERS_VS 4
127 #define NINE_MAX_SAMPLERS 21 /* PS + DMAP + VS */
128 #define NINE_SAMPLER_PS(s) ( 0 + (s))
129 #define NINE_SAMPLER_DMAP 16
130 #define NINE_SAMPLER_VS(s) (17 + (s))
131 #define NINE_PS_SAMPLERS_MASK 0x00ffff
132 #define NINE_VS_SAMPLERS_MASK 0x1e0000
133
134 struct nine_state
135 {
136 struct {
137 uint32_t group;
138 uint32_t rs[(NINED3DRS_COUNT + 31) / 32]; /* stateblocks only */
139 uint32_t vtxbuf; /* stateblocks only */
140 uint32_t stream_freq; /* stateblocks only */
141 uint32_t texture; /* stateblocks only */
142 uint16_t sampler[NINE_MAX_SAMPLERS];
143 struct nine_range *vs_const_f;
144 struct nine_range *ps_const_f;
145 struct nine_range *vs_const_i;
146 uint16_t ps_const_i; /* NINE_MAX_CONST_I == 16 */
147 struct nine_range *vs_const_b;
148 uint16_t ps_const_b; /* NINE_MAX_CONST_B == 16 */
149 uint8_t ucp;
150 } changed;
151
152 struct NineSurface9 *rt[NINE_MAX_SIMULTANEOUS_RENDERTARGETS];
153 struct NineSurface9 *ds;
154
155 D3DVIEWPORT9 viewport;
156
157 struct pipe_scissor_state scissor;
158
159 /* NOTE: vs, ps will be NULL for FF and are set in device->ff.vs,ps instead
160 * (XXX: or is it better to reference FF shaders here, too ?)
161 * NOTE: const_f contains extra space for const_i,b to use as user constbuf
162 */
163 struct NineVertexShader9 *vs;
164 float *vs_const_f;
165 float *vs_const_f_swvp;
166 int *vs_const_i;
167 BOOL *vs_const_b;
168 float *vs_lconstf_temp;
169 BOOL programmable_vs;
170
171 struct NinePixelShader9 *ps;
172 float *ps_const_f;
173 int ps_const_i[NINE_MAX_CONST_I][4];
174 BOOL ps_const_b[NINE_MAX_CONST_B];
175 float *ps_lconstf_temp;
176
177 struct NineVertexDeclaration9 *vdecl;
178
179 struct NineIndexBuffer9 *idxbuf;
180 struct NineVertexBuffer9 *stream[PIPE_MAX_ATTRIBS];
181 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS]; /* vtxbuf.buffer unused */
182 UINT stream_freq[PIPE_MAX_ATTRIBS];
183
184 struct pipe_clip_state clip;
185
186 DWORD rs_advertised[NINED3DRS_COUNT]; /* the ones apps get with GetRenderState */
187
188 struct NineBaseTexture9 *texture[NINE_MAX_SAMPLERS]; /* PS, DMAP, VS */
189
190 DWORD samp[NINE_MAX_SAMPLERS][NINED3DSAMP_COUNT];
191 DWORD samp_advertised[NINE_MAX_SAMPLERS][NINED3DSAMP_COUNT];
192
193 struct {
194 struct {
195 uint32_t tex_stage[NINE_MAX_TEXTURE_STAGES][(NINED3DTSS_COUNT + 31) / 32];
196 uint32_t transform[(NINED3DTS_COUNT + 31) / 32];
197 } changed;
198
199 D3DMATRIX *transform; /* access only via nine_state_access_transform */
200 unsigned num_transforms;
201
202 /* XXX: Do state blocks just change the set of active lights or do we
203 * have to store which lights have been disabled, too ?
204 */
205 D3DLIGHT9 *light;
206 uint16_t active_light[NINE_MAX_LIGHTS_ACTIVE]; /* 8 */
207 unsigned num_lights;
208 unsigned num_lights_active;
209
210 D3DMATERIAL9 material;
211
212 DWORD tex_stage[NINE_MAX_TEXTURE_STAGES][NINED3DTSS_COUNT];
213 } ff;
214 };
215
216 struct nine_context {
217 struct {
218 uint32_t vtxbuf;
219 } changed;
220
221 uint32_t bumpmap_vars[6 * NINE_MAX_TEXTURE_STAGES];
222
223 struct {
224 void *vs;
225 void *ps;
226 } cso;
227
228 uint8_t rt_mask;
229
230 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
231 UINT stream_freq[PIPE_MAX_ATTRIBS];
232 uint32_t stream_instancedata_mask; /* derived from stream_freq */
233 uint32_t stream_usage_mask; /* derived from VS and vdecl */
234
235 DWORD rs[NINED3DRS_COUNT];
236
237 struct NineBaseTexture9 *texture[NINE_MAX_SAMPLERS];
238
239 uint32_t samplers_shadow;
240
241 uint8_t bound_samplers_mask_vs;
242 uint16_t bound_samplers_mask_ps;
243
244 int dummy_vbo_bound_at; /* -1 = not bound , >= 0 = bound index */
245 boolean vbo_bound_done;
246
247 uint32_t commit;
248 struct {
249 struct pipe_framebuffer_state fb;
250 struct pipe_depth_stencil_alpha_state dsa;
251 struct pipe_rasterizer_state rast;
252 struct pipe_blend_state blend;
253 struct pipe_constant_buffer cb_vs;
254 struct pipe_constant_buffer cb0_swvp;
255 struct pipe_constant_buffer cb1_swvp;
256 struct pipe_constant_buffer cb2_swvp;
257 struct pipe_constant_buffer cb3_swvp;
258 struct pipe_constant_buffer cb_ps;
259 struct pipe_constant_buffer cb_vs_ff;
260 struct pipe_constant_buffer cb_ps_ff;
261 } pipe;
262 };
263
264 struct nine_state_sw_internal {
265 struct pipe_transfer *transfers_so[4];
266 };
267
268 /* map D3DRS -> NINE_STATE_x
269 */
270 extern const uint32_t nine_render_state_group[NINED3DRS_COUNT];
271
272 /* for D3DSBT_PIXEL/VERTEX:
273 */
274 extern const uint32_t nine_render_states_pixel[(NINED3DRS_COUNT + 31) / 32];
275 extern const uint32_t nine_render_states_vertex[(NINED3DRS_COUNT + 31) / 32];
276
277 struct NineDevice9;
278
279 void
280 nine_context_set_render_state(struct NineDevice9 *device,
281 D3DRENDERSTATETYPE State,
282 DWORD Value);
283
284 void
285 nine_context_set_texture(struct NineDevice9 *device,
286 DWORD Stage,
287 struct NineBaseTexture9 *tex);
288
289 void
290 nine_context_set_stream_source(struct NineDevice9 *device,
291 UINT StreamNumber,
292 struct NineVertexBuffer9 *pVBuf9,
293 UINT OffsetInBytes,
294 UINT Stride);
295
296 void
297 nine_context_set_stream_source_freq(struct NineDevice9 *device,
298 UINT StreamNumber,
299 UINT Setting);
300
301 void
302 nine_context_apply_stateblock(struct NineDevice9 *device,
303 const struct nine_state *src);
304
305 void
306 nine_context_clear_fb(struct NineDevice9 *device, DWORD Count,
307 const D3DRECT *pRects, DWORD Flags,
308 D3DCOLOR Color, float Z, DWORD Stencil);
309
310 void
311 nine_context_draw_primitive(struct NineDevice9 *device,
312 D3DPRIMITIVETYPE PrimitiveType,
313 UINT StartVertex,
314 UINT PrimitiveCount);
315
316 void
317 nine_context_draw_indexed_primitive(struct NineDevice9 *device,
318 D3DPRIMITIVETYPE PrimitiveType,
319 INT BaseVertexIndex,
320 UINT MinVertexIndex,
321 UINT NumVertices,
322 UINT StartIndex,
323 UINT PrimitiveCount);
324
325 void
326 nine_context_draw_primitive_from_vtxbuf(struct NineDevice9 *device,
327 D3DPRIMITIVETYPE PrimitiveType,
328 UINT PrimitiveCount,
329 struct pipe_vertex_buffer *vtxbuf);
330
331 void
332 nine_context_draw_indexed_primitive_from_vtxbuf_idxbuf(struct NineDevice9 *device,
333 D3DPRIMITIVETYPE PrimitiveType,
334 UINT MinVertexIndex,
335 UINT NumVertices,
336 UINT PrimitiveCount,
337 struct pipe_vertex_buffer *vbuf,
338 struct pipe_index_buffer *ibuf);
339
340 void nine_state_restore_non_cso(struct NineDevice9 *device);
341 void nine_state_set_defaults(struct NineDevice9 *, const D3DCAPS9 *,
342 boolean is_reset);
343 void nine_state_clear(struct nine_state *, const boolean device);
344 void nine_context_clear(struct nine_context *);
345
346 void nine_state_init_sw(struct NineDevice9 *device);
347 void nine_state_prepare_draw_sw(struct NineDevice9 *device,
348 struct NineVertexDeclaration9 *vdecl_out,
349 int start_vertice,
350 int num_vertices,
351 struct pipe_stream_output_info *so);
352 void nine_state_after_draw_sw(struct NineDevice9 *device);
353 void nine_state_destroy_sw(struct NineDevice9 *device);
354
355 /* If @alloc is FALSE, the return value may be a const identity matrix.
356 * Therefore, do not modify if you set alloc to FALSE !
357 */
358 D3DMATRIX *
359 nine_state_access_transform(struct nine_state *, D3DTRANSFORMSTATETYPE,
360 boolean alloc);
361
362 const char *nine_d3drs_to_string(DWORD State);
363
364 #endif /* _NINE_STATE_H_ */