st/nine: Partial software vertex processing support
[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 /* ALPHACOVERAGE:
37 * bit 0: enable alpha coverage
38 * bit 1: ATOC is on
39 */
40 #define NINED3DRS_ALPHACOVERAGE (D3DRS_BLENDOPALPHA + 3)
41 #define NINED3DRS_MULTISAMPLE (D3DRS_BLENDOPALPHA + 4)
42
43 #define D3DRS_LAST D3DRS_BLENDOPALPHA
44 #define NINED3DRS_LAST NINED3DRS_MULTISAMPLE /* 214 */
45 #define NINED3DSAMP_LAST NINED3DSAMP_SHADOW /* 15 */
46 #define NINED3DTSS_LAST D3DTSS_CONSTANT
47 #define NINED3DTS_LAST D3DTS_WORLDMATRIX(255)
48
49 #define D3DRS_COUNT (D3DRS_LAST + 1)
50 #define NINED3DRS_COUNT (NINED3DRS_LAST + 1)
51 #define NINED3DSAMP_COUNT (NINED3DSAMP_LAST + 1)
52 #define NINED3DTSS_COUNT (NINED3DTSS_LAST + 1)
53 #define NINED3DTS_COUNT (NINED3DTS_LAST + 1)
54
55 #define NINE_STATE_FB (1 << 0)
56 #define NINE_STATE_VIEWPORT (1 << 1)
57 #define NINE_STATE_SCISSOR (1 << 2)
58 #define NINE_STATE_RASTERIZER (1 << 3)
59 #define NINE_STATE_BLEND (1 << 4)
60 #define NINE_STATE_DSA (1 << 5)
61 #define NINE_STATE_VS (1 << 6)
62 #define NINE_STATE_VS_CONST (1 << 7)
63 #define NINE_STATE_PS (1 << 8)
64 #define NINE_STATE_PS_CONST (1 << 9)
65 #define NINE_STATE_TEXTURE (1 << 10)
66 #define NINE_STATE_SAMPLER (1 << 11)
67 #define NINE_STATE_VDECL (1 << 12)
68 #define NINE_STATE_IDXBUF (1 << 13)
69 #define NINE_STATE_STREAMFREQ (1 << 14)
70 #define NINE_STATE_PRIM (1 << 15)
71 #define NINE_STATE_MATERIAL (1 << 16)
72 #define NINE_STATE_BLEND_COLOR (1 << 17)
73 #define NINE_STATE_STENCIL_REF (1 << 18)
74 #define NINE_STATE_SAMPLE_MASK (1 << 19)
75 #define NINE_STATE_FF (0x1f << 20)
76 #define NINE_STATE_FF_VS (0x17 << 20)
77 #define NINE_STATE_FF_PS (0x18 << 20)
78 #define NINE_STATE_FF_LIGHTING (1 << 20)
79 #define NINE_STATE_FF_MATERIAL (1 << 21)
80 #define NINE_STATE_FF_VSTRANSF (1 << 22)
81 #define NINE_STATE_FF_PSSTAGES (1 << 23)
82 #define NINE_STATE_FF_OTHER (1 << 24)
83 #define NINE_STATE_FOG_SHADER (1 << 25)
84 #define NINE_STATE_PS1X_SHADER (1 << 26)
85 #define NINE_STATE_POINTSIZE_SHADER (1 << 27)
86 #define NINE_STATE_MULTISAMPLE (1 << 28)
87 #define NINE_STATE_SWVP (1 << 29)
88 #define NINE_STATE_ALL 0x3fffffff
89 #define NINE_STATE_UNHANDLED (1 << 30)
90
91 #define NINE_STATE_COMMIT_DSA (1 << 0)
92 #define NINE_STATE_COMMIT_RASTERIZER (1 << 1)
93 #define NINE_STATE_COMMIT_BLEND (1 << 2)
94 #define NINE_STATE_COMMIT_CONST_VS (1 << 3)
95 #define NINE_STATE_COMMIT_CONST_PS (1 << 4)
96 #define NINE_STATE_COMMIT_VS (1 << 5)
97 #define NINE_STATE_COMMIT_PS (1 << 6)
98
99
100 #define NINE_MAX_SIMULTANEOUS_RENDERTARGETS 4
101 #define NINE_MAX_CONST_F_PS3 224
102 #define NINE_MAX_CONST_F 256
103 #define NINE_MAX_CONST_I 16
104 #define NINE_MAX_CONST_B 16
105 #define NINE_MAX_CONST_F_SWVP 8192
106 #define NINE_MAX_CONST_I_SWVP 2048
107 #define NINE_MAX_CONST_B_SWVP 2048
108 #define NINE_MAX_CONST_ALL 276 /* B consts count only 1/4 th */
109
110 #define NINE_CONST_I_BASE(nconstf) \
111 ((nconstf) * 4 * sizeof(float))
112 #define NINE_CONST_B_BASE(nconstf) \
113 ((nconstf) * 4 * sizeof(float) + \
114 NINE_MAX_CONST_I * 4 * sizeof(int))
115
116
117 #define NINE_MAX_TEXTURE_STAGES 8
118
119 #define NINE_MAX_LIGHTS 65536
120 #define NINE_MAX_LIGHTS_ACTIVE 8
121
122 #define NINED3DLIGHT_INVALID (D3DLIGHT_DIRECTIONAL + 1)
123
124 #define NINE_MAX_SAMPLERS_PS 16
125 #define NINE_MAX_SAMPLERS_VS 4
126 #define NINE_MAX_SAMPLERS 21 /* PS + DMAP + VS */
127 #define NINE_SAMPLER_PS(s) ( 0 + (s))
128 #define NINE_SAMPLER_DMAP 16
129 #define NINE_SAMPLER_VS(s) (17 + (s))
130 #define NINE_PS_SAMPLERS_MASK 0x00ffff
131 #define NINE_VS_SAMPLERS_MASK 0x1e0000
132
133 struct nine_state
134 {
135 struct {
136 uint32_t group;
137 uint32_t rs[(NINED3DRS_COUNT + 31) / 32];
138 uint32_t vtxbuf;
139 uint32_t stream_freq;
140 uint32_t texture;
141 uint16_t sampler[NINE_MAX_SAMPLERS];
142 struct nine_range *vs_const_f;
143 struct nine_range *ps_const_f;
144 struct nine_range *vs_const_i;
145 uint16_t ps_const_i; /* NINE_MAX_CONST_I == 16 */
146 struct nine_range *vs_const_b;
147 uint16_t ps_const_b; /* NINE_MAX_CONST_B == 16 */
148 uint8_t ucp;
149 } changed;
150
151 struct NineSurface9 *rt[NINE_MAX_SIMULTANEOUS_RENDERTARGETS];
152 struct NineSurface9 *ds;
153
154 D3DVIEWPORT9 viewport;
155
156 struct pipe_scissor_state scissor;
157
158 /* NOTE: vs, ps will be NULL for FF and are set in device->ff.vs,ps instead
159 * (XXX: or is it better to reference FF shaders here, too ?)
160 * NOTE: const_f contains extra space for const_i,b to use as user constbuf
161 */
162 struct NineVertexShader9 *vs;
163 float *vs_const_f;
164 float *vs_const_f_swvp;
165 int *vs_const_i;
166 BOOL *vs_const_b;
167 float *vs_lconstf_temp;
168 BOOL programmable_vs;
169
170 struct NinePixelShader9 *ps;
171 float *ps_const_f;
172 int ps_const_i[NINE_MAX_CONST_I][4];
173 BOOL ps_const_b[NINE_MAX_CONST_B];
174 float *ps_lconstf_temp;
175 uint32_t bumpmap_vars[6 * NINE_MAX_TEXTURE_STAGES];
176
177 struct {
178 void *vs;
179 void *ps;
180 } cso;
181
182 struct NineVertexDeclaration9 *vdecl;
183
184 struct NineIndexBuffer9 *idxbuf;
185 struct NineVertexBuffer9 *stream[PIPE_MAX_ATTRIBS];
186 struct pipe_vertex_buffer vtxbuf[PIPE_MAX_ATTRIBS];
187 UINT stream_freq[PIPE_MAX_ATTRIBS];
188 uint32_t stream_instancedata_mask; /* derived from stream_freq */
189 uint32_t stream_usage_mask; /* derived from VS and vdecl */
190
191 struct pipe_clip_state clip;
192 struct pipe_framebuffer_state fb;
193 uint8_t rt_mask;
194
195 DWORD rs[NINED3DRS_COUNT];
196 DWORD rs_advertised[NINED3DRS_COUNT]; /* the ones apps get with GetRenderState */
197
198 struct NineBaseTexture9 *texture[NINE_MAX_SAMPLERS]; /* PS, DMAP, VS */
199
200 DWORD samp[NINE_MAX_SAMPLERS][NINED3DSAMP_COUNT];
201 uint32_t samplers_shadow;
202 uint8_t bound_samplers_mask_vs;
203 uint16_t bound_samplers_mask_ps;
204
205 int dummy_vbo_bound_at; /* -1 = not bound , >= 0 = bound index */
206 boolean vbo_bound_done;
207
208 struct {
209 struct {
210 uint32_t group;
211 uint32_t tex_stage[NINE_MAX_TEXTURE_STAGES][(NINED3DTSS_COUNT + 31) / 32];
212 uint32_t transform[(NINED3DTS_COUNT + 31) / 32];
213 } changed;
214
215 D3DMATRIX *transform; /* access only via nine_state_access_transform */
216 unsigned num_transforms;
217
218 /* XXX: Do state blocks just change the set of active lights or do we
219 * have to store which lights have been disabled, too ?
220 */
221 D3DLIGHT9 *light;
222 uint16_t active_light[NINE_MAX_LIGHTS_ACTIVE]; /* 8 */
223 unsigned num_lights;
224 unsigned num_lights_active;
225
226 D3DMATERIAL9 material;
227
228 DWORD tex_stage[NINE_MAX_TEXTURE_STAGES][NINED3DTSS_COUNT];
229 } ff;
230
231 uint32_t commit;
232 struct {
233 struct pipe_depth_stencil_alpha_state dsa;
234 struct pipe_rasterizer_state rast;
235 struct pipe_blend_state blend;
236 struct pipe_constant_buffer cb_vs;
237 struct pipe_constant_buffer cb0_swvp;
238 struct pipe_constant_buffer cb1_swvp;
239 struct pipe_constant_buffer cb2_swvp;
240 struct pipe_constant_buffer cb3_swvp;
241 struct pipe_constant_buffer cb_ps;
242 struct pipe_constant_buffer cb_vs_ff;
243 struct pipe_constant_buffer cb_ps_ff;
244 } pipe;
245 };
246
247 /* map D3DRS -> NINE_STATE_x
248 */
249 extern const uint32_t nine_render_state_group[NINED3DRS_COUNT];
250
251 /* for D3DSBT_PIXEL/VERTEX:
252 */
253 extern const uint32_t nine_render_states_pixel[(NINED3DRS_COUNT + 31) / 32];
254 extern const uint32_t nine_render_states_vertex[(NINED3DRS_COUNT + 31) / 32];
255
256 struct NineDevice9;
257
258 void nine_update_state_framebuffer_clear(struct NineDevice9 *);
259 boolean nine_update_state(struct NineDevice9 *);
260
261 void nine_state_restore_non_cso(struct NineDevice9 *device);
262 void nine_state_set_defaults(struct NineDevice9 *, const D3DCAPS9 *,
263 boolean is_reset);
264 void nine_state_clear(struct nine_state *, const boolean device);
265
266 /* If @alloc is FALSE, the return value may be a const identity matrix.
267 * Therefore, do not modify if you set alloc to FALSE !
268 */
269 D3DMATRIX *
270 nine_state_access_transform(struct nine_state *, D3DTRANSFORMSTATETYPE,
271 boolean alloc);
272
273 const char *nine_d3drs_to_string(DWORD State);
274
275 #endif /* _NINE_STATE_H_ */