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