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