2172a63d2c303d0aabfd4bef0675c7621cf42ad3
[mesa.git] / src / gallium / state_trackers / nine / nine_state.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 * Copyright 2013 Christoph Bumiller
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "device9.h"
25 #include "basetexture9.h"
26 #include "buffer9.h"
27 #include "indexbuffer9.h"
28 #include "surface9.h"
29 #include "vertexdeclaration9.h"
30 #include "vertexshader9.h"
31 #include "pixelshader9.h"
32 #include "nine_pipe.h"
33 #include "nine_ff.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_state.h"
36 #include "cso_cache/cso_context.h"
37 #include "util/u_upload_mgr.h"
38 #include "util/u_math.h"
39
40 #define DBG_CHANNEL DBG_DEVICE
41
42 /* State preparation only */
43
44 static inline void
45 prepare_blend(struct NineDevice9 *device)
46 {
47 nine_convert_blend_state(&device->state.pipe.blend, device->state.rs);
48 device->state.commit |= NINE_STATE_COMMIT_BLEND;
49 }
50
51 static inline void
52 prepare_dsa(struct NineDevice9 *device)
53 {
54 nine_convert_dsa_state(&device->state.pipe.dsa, device->state.rs);
55 device->state.commit |= NINE_STATE_COMMIT_DSA;
56 }
57
58 static inline void
59 prepare_rasterizer(struct NineDevice9 *device)
60 {
61 nine_convert_rasterizer_state(&device->state.pipe.rast, device->state.rs);
62 device->state.commit |= NINE_STATE_COMMIT_RASTERIZER;
63 }
64
65 static void
66 prepare_ps_constants_userbuf(struct NineDevice9 *device);
67
68 #define DO_UPLOAD_CONST_F(buf,p,c,d) \
69 do { \
70 DBG("upload ConstantF [%u .. %u]\n", x, (x) + (c) - 1); \
71 box.x = (p) * 4 * sizeof(float); \
72 box.width = (c) * 4 * sizeof(float); \
73 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, &((d)[p * 4]), \
74 0, 0); \
75 } while(0)
76
77 /* OK, this is a bit ugly ... */
78 static void
79 upload_constants(struct NineDevice9 *device, unsigned shader_type)
80 {
81 struct pipe_context *pipe = device->pipe;
82 struct pipe_resource *buf;
83 struct pipe_box box;
84 const void *data;
85 const float *const_f;
86 const int *const_i;
87 const BOOL *const_b;
88 uint32_t data_b[NINE_MAX_CONST_B];
89 uint16_t dirty_i;
90 uint16_t dirty_b;
91 const unsigned usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE;
92 unsigned x = 0; /* silence warning */
93 unsigned i, c;
94 struct nine_range *r, *p, *lconstf_ranges;
95 float *lconstf_data;
96
97 box.y = 0;
98 box.z = 0;
99 box.height = 1;
100 box.depth = 1;
101
102 if (shader_type == PIPE_SHADER_VERTEX) {
103 DBG("VS\n");
104 buf = device->constbuf_vs;
105
106 const_f = device->state.vs_const_f;
107 for (p = r = device->state.changed.vs_const_f; r; p = r, r = r->next)
108 DO_UPLOAD_CONST_F(buf, r->bgn, r->end - r->bgn, const_f);
109 if (p) {
110 nine_range_pool_put_chain(&device->range_pool,
111 device->state.changed.vs_const_f, p);
112 device->state.changed.vs_const_f = NULL;
113 }
114
115 dirty_i = device->state.changed.vs_const_i;
116 device->state.changed.vs_const_i = 0;
117 const_i = &device->state.vs_const_i[0][0];
118
119 dirty_b = device->state.changed.vs_const_b;
120 device->state.changed.vs_const_b = 0;
121 const_b = device->state.vs_const_b;
122
123 lconstf_ranges = device->state.vs->lconstf.ranges;
124 lconstf_data = device->state.vs->lconstf.data;
125
126 device->state.changed.group &= ~NINE_STATE_VS_CONST;
127 } else {
128 DBG("PS\n");
129 /* features only implemented on the userbuf path */
130 if (device->state.ps->bumpenvmat_needed || (
131 device->state.ps->byte_code.version < 0x30 &&
132 device->state.rs[D3DRS_FOGENABLE])) {
133 device->prefer_user_constbuf = TRUE;
134 prepare_ps_constants_userbuf(device);
135 return;
136 }
137 buf = device->constbuf_ps;
138
139 const_f = device->state.ps_const_f;
140 for (p = r = device->state.changed.ps_const_f; r; p = r, r = r->next)
141 DO_UPLOAD_CONST_F(buf, r->bgn, r->end - r->bgn, const_f);
142 if (p) {
143 nine_range_pool_put_chain(&device->range_pool,
144 device->state.changed.ps_const_f, p);
145 device->state.changed.ps_const_f = NULL;
146 }
147
148 dirty_i = device->state.changed.ps_const_i;
149 device->state.changed.ps_const_i = 0;
150 const_i = &device->state.ps_const_i[0][0];
151
152 dirty_b = device->state.changed.ps_const_b;
153 device->state.changed.ps_const_b = 0;
154 const_b = device->state.ps_const_b;
155
156 lconstf_ranges = NULL;
157 lconstf_data = NULL;
158
159 device->state.changed.group &= ~NINE_STATE_PS_CONST;
160 }
161
162 /* write range from min to max changed, it's not much data */
163 /* bool1 */
164 if (dirty_b) {
165 c = util_last_bit(dirty_b);
166 i = ffs(dirty_b) - 1;
167 x = buf->width0 - (NINE_MAX_CONST_B - i) * 4;
168 c -= i;
169 memcpy(data_b, &(const_b[i]), c * sizeof(uint32_t));
170 box.x = x;
171 box.width = c * 4;
172 DBG("upload ConstantB [%u .. %u]\n", x, x + c - 1);
173 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, data_b, 0, 0);
174 }
175
176 /* int4 */
177 for (c = 0, i = 0; dirty_i; i++, dirty_i >>= 1) {
178 if (dirty_i & 1) {
179 if (!c)
180 x = i;
181 ++c;
182 } else
183 if (c) {
184 DBG("upload ConstantI [%u .. %u]\n", x, x + c - 1);
185 data = &const_i[x * 4];
186 box.x = buf->width0 - (NINE_MAX_CONST_I * 4 + NINE_MAX_CONST_B) * 4;
187 box.x += x * 4 * sizeof(int);
188 box.width = c * 4 * sizeof(int);
189 c = 0;
190 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, data, 0, 0);
191 }
192 }
193 if (c) {
194 DBG("upload ConstantI [%u .. %u]\n", x, x + c - 1);
195 data = &const_i[x * 4];
196 box.x = buf->width0 - (NINE_MAX_CONST_I * 4 + NINE_MAX_CONST_B) * 4;
197 box.x += x * 4 * sizeof(int);
198 box.width = c * 4 * sizeof(int);
199 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, data, 0, 0);
200 }
201
202 /* TODO: only upload these when shader itself changes */
203 if (lconstf_ranges) {
204 unsigned n = 0;
205 struct nine_range *r = lconstf_ranges;
206 while (r) {
207 box.x = r->bgn * 4 * sizeof(float);
208 n += r->end - r->bgn;
209 box.width = (r->end - r->bgn) * 4 * sizeof(float);
210 data = &lconstf_data[4 * n];
211 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, data, 0, 0);
212 r = r->next;
213 }
214 }
215 }
216
217 static void
218 prepare_vs_constants_userbuf(struct NineDevice9 *device)
219 {
220 struct nine_state *state = &device->state;
221 struct pipe_constant_buffer cb;
222 cb.buffer = NULL;
223 cb.buffer_offset = 0;
224 cb.buffer_size = device->state.vs->const_used_size;
225 cb.user_buffer = device->state.vs_const_f;
226
227 if (!cb.buffer_size)
228 return;
229
230 if (state->changed.vs_const_i) {
231 int *idst = (int *)&state->vs_const_f[4 * device->max_vs_const_f];
232 memcpy(idst, state->vs_const_i, sizeof(state->vs_const_i));
233 state->changed.vs_const_i = 0;
234 }
235 if (state->changed.vs_const_b) {
236 int *idst = (int *)&state->vs_const_f[4 * device->max_vs_const_f];
237 uint32_t *bdst = (uint32_t *)&idst[4 * NINE_MAX_CONST_I];
238 memcpy(bdst, state->vs_const_b, sizeof(state->vs_const_b));
239 state->changed.vs_const_b = 0;
240 }
241
242 if (device->state.vs->lconstf.ranges) {
243 /* TODO: Can we make it so that we don't have to copy everything ? */
244 const struct nine_lconstf *lconstf = &device->state.vs->lconstf;
245 const struct nine_range *r = lconstf->ranges;
246 unsigned n = 0;
247 float *dst = device->state.vs_lconstf_temp;
248 float *src = (float *)cb.user_buffer;
249 memcpy(dst, src, cb.buffer_size);
250 while (r) {
251 unsigned p = r->bgn;
252 unsigned c = r->end - r->bgn;
253 memcpy(&dst[p * 4], &lconstf->data[n * 4], c * 4 * sizeof(float));
254 n += c;
255 r = r->next;
256 }
257 cb.user_buffer = dst;
258 }
259
260 if (!device->driver_caps.user_cbufs) {
261 u_upload_data(device->constbuf_uploader,
262 0,
263 cb.buffer_size,
264 device->constbuf_alignment,
265 cb.user_buffer,
266 &cb.buffer_offset,
267 &cb.buffer);
268 u_upload_unmap(device->constbuf_uploader);
269 cb.user_buffer = NULL;
270 }
271
272 state->pipe.cb_vs = cb;
273
274 if (device->state.changed.vs_const_f) {
275 struct nine_range *r = device->state.changed.vs_const_f;
276 struct nine_range *p = r;
277 while (p->next)
278 p = p->next;
279 nine_range_pool_put_chain(&device->range_pool, r, p);
280 device->state.changed.vs_const_f = NULL;
281 }
282 state->changed.group &= ~NINE_STATE_VS_CONST;
283 state->commit |= NINE_STATE_COMMIT_CONST_VS;
284 }
285
286 static void
287 prepare_ps_constants_userbuf(struct NineDevice9 *device)
288 {
289 struct nine_state *state = &device->state;
290 struct pipe_constant_buffer cb;
291 cb.buffer = NULL;
292 cb.buffer_offset = 0;
293 cb.buffer_size = device->state.ps->const_used_size;
294 cb.user_buffer = device->state.ps_const_f;
295
296 if (state->changed.ps_const_i) {
297 int *idst = (int *)&state->ps_const_f[4 * device->max_ps_const_f];
298 memcpy(idst, state->ps_const_i, sizeof(state->ps_const_i));
299 state->changed.ps_const_i = 0;
300 }
301 if (state->changed.ps_const_b) {
302 int *idst = (int *)&state->ps_const_f[4 * device->max_ps_const_f];
303 uint32_t *bdst = (uint32_t *)&idst[4 * NINE_MAX_CONST_I];
304 memcpy(bdst, state->ps_const_b, sizeof(state->ps_const_b));
305 state->changed.ps_const_b = 0;
306 }
307
308 /* Upload special constants needed to implement PS1.x instructions like TEXBEM,TEXBEML and BEM */
309 if (device->state.ps->bumpenvmat_needed) {
310 memcpy(device->state.ps_lconstf_temp, cb.user_buffer, cb.buffer_size);
311 memcpy(&device->state.ps_lconstf_temp[4 * 8], &device->state.bumpmap_vars, sizeof(device->state.bumpmap_vars));
312
313 cb.user_buffer = device->state.ps_lconstf_temp;
314 }
315
316 if (state->ps->byte_code.version < 0x30 &&
317 state->rs[D3DRS_FOGENABLE]) {
318 float *dst = &state->ps_lconstf_temp[4 * 32];
319 if (cb.user_buffer != state->ps_lconstf_temp) {
320 memcpy(state->ps_lconstf_temp, cb.user_buffer, cb.buffer_size);
321 cb.user_buffer = state->ps_lconstf_temp;
322 }
323
324 d3dcolor_to_rgba(dst, state->rs[D3DRS_FOGCOLOR]);
325 if (state->rs[D3DRS_FOGTABLEMODE] == D3DFOG_LINEAR) {
326 dst[4] = asfloat(state->rs[D3DRS_FOGEND]);
327 dst[5] = 1.0f / (asfloat(state->rs[D3DRS_FOGEND]) - asfloat(state->rs[D3DRS_FOGSTART]));
328 } else if (state->rs[D3DRS_FOGTABLEMODE] != D3DFOG_NONE) {
329 dst[4] = asfloat(state->rs[D3DRS_FOGDENSITY]);
330 }
331 cb.buffer_size = 4 * 4 * 34;
332 }
333
334 if (!cb.buffer_size)
335 return;
336
337 if (!device->driver_caps.user_cbufs) {
338 u_upload_data(device->constbuf_uploader,
339 0,
340 cb.buffer_size,
341 device->constbuf_alignment,
342 cb.user_buffer,
343 &cb.buffer_offset,
344 &cb.buffer);
345 u_upload_unmap(device->constbuf_uploader);
346 cb.user_buffer = NULL;
347 }
348
349 state->pipe.cb_ps = cb;
350
351 if (device->state.changed.ps_const_f) {
352 struct nine_range *r = device->state.changed.ps_const_f;
353 struct nine_range *p = r;
354 while (p->next)
355 p = p->next;
356 nine_range_pool_put_chain(&device->range_pool, r, p);
357 device->state.changed.ps_const_f = NULL;
358 }
359 state->changed.group &= ~NINE_STATE_PS_CONST;
360 state->commit |= NINE_STATE_COMMIT_CONST_PS;
361 }
362
363 static inline uint32_t
364 prepare_vs(struct NineDevice9 *device, uint8_t shader_changed)
365 {
366 struct nine_state *state = &device->state;
367 struct NineVertexShader9 *vs = state->vs;
368 uint32_t changed_group = 0;
369 int has_key_changed = 0;
370
371 if (likely(state->programmable_vs))
372 has_key_changed = NineVertexShader9_UpdateKey(vs, state);
373
374 if (!shader_changed && !has_key_changed)
375 return 0;
376
377 /* likely because we dislike FF */
378 if (likely(state->programmable_vs)) {
379 state->cso.vs = NineVertexShader9_GetVariant(vs);
380 } else {
381 vs = device->ff.vs;
382 state->cso.vs = vs->ff_cso;
383 }
384
385 if (state->rs[NINED3DRS_VSPOINTSIZE] != vs->point_size) {
386 state->rs[NINED3DRS_VSPOINTSIZE] = vs->point_size;
387 changed_group |= NINE_STATE_RASTERIZER;
388 }
389
390 if ((state->bound_samplers_mask_vs & vs->sampler_mask) != vs->sampler_mask)
391 /* Bound dummy sampler. */
392 changed_group |= NINE_STATE_SAMPLER;
393
394 state->commit |= NINE_STATE_COMMIT_VS;
395 return changed_group;
396 }
397
398 static inline uint32_t
399 prepare_ps(struct NineDevice9 *device, uint8_t shader_changed)
400 {
401 struct nine_state *state = &device->state;
402 struct NinePixelShader9 *ps = state->ps;
403 uint32_t changed_group = 0;
404 int has_key_changed = 0;
405
406 if (likely(ps))
407 has_key_changed = NinePixelShader9_UpdateKey(ps, state);
408
409 if (!shader_changed && !has_key_changed)
410 return 0;
411
412 if (likely(ps)) {
413 state->cso.ps = NinePixelShader9_GetVariant(ps);
414 } else {
415 ps = device->ff.ps;
416 state->cso.ps = ps->ff_cso;
417 }
418
419 if ((state->bound_samplers_mask_ps & ps->sampler_mask) != ps->sampler_mask)
420 /* Bound dummy sampler. */
421 changed_group |= NINE_STATE_SAMPLER;
422
423 state->commit |= NINE_STATE_COMMIT_PS;
424 return changed_group;
425 }
426
427 /* State preparation incremental */
428
429 /* State preparation + State commit */
430
431 static void
432 update_framebuffer(struct NineDevice9 *device, bool is_clear)
433 {
434 struct pipe_context *pipe = device->pipe;
435 struct nine_state *state = &device->state;
436 struct pipe_framebuffer_state *fb = &device->state.fb;
437 unsigned i;
438 struct NineSurface9 *rt0 = state->rt[0];
439 unsigned w = rt0->desc.Width;
440 unsigned h = rt0->desc.Height;
441 D3DMULTISAMPLE_TYPE nr_samples = rt0->desc.MultiSampleType;
442 unsigned ps_mask = state->ps ? state->ps->rt_mask : 1;
443 unsigned mask = is_clear ? 0xf : ps_mask;
444 const int sRGB = state->rs[D3DRS_SRGBWRITEENABLE] ? 1 : 0;
445
446 DBG("\n");
447
448 state->rt_mask = 0x0;
449 fb->nr_cbufs = 0;
450
451 /* all render targets must have the same size and the depth buffer must be
452 * bigger. Multisample has to match, according to spec. But some apps do
453 * things wrong there, and no error is returned. The behaviour they get
454 * apparently is that depth buffer is disabled if it doesn't match.
455 * Surely the same for render targets. */
456
457 /* Special case: D3DFMT_NULL is used to bound no real render target,
458 * but render to depth buffer. We have to not take into account the render
459 * target info. TODO: know what should happen when there are several render targers
460 * and the first one is D3DFMT_NULL */
461 if (rt0->desc.Format == D3DFMT_NULL && state->ds) {
462 w = state->ds->desc.Width;
463 h = state->ds->desc.Height;
464 nr_samples = state->ds->desc.MultiSampleType;
465 }
466
467 for (i = 0; i < device->caps.NumSimultaneousRTs; ++i) {
468 struct NineSurface9 *rt = state->rt[i];
469
470 if (rt && rt->desc.Format != D3DFMT_NULL && (mask & (1 << i)) &&
471 rt->desc.Width == w && rt->desc.Height == h &&
472 rt->desc.MultiSampleType == nr_samples) {
473 fb->cbufs[i] = NineSurface9_GetSurface(rt, sRGB);
474 state->rt_mask |= 1 << i;
475 fb->nr_cbufs = i + 1;
476
477 if (unlikely(rt->desc.Usage & D3DUSAGE_AUTOGENMIPMAP)) {
478 assert(rt->texture == D3DRTYPE_TEXTURE ||
479 rt->texture == D3DRTYPE_CUBETEXTURE);
480 NineBaseTexture9(rt->base.base.container)->dirty_mip = TRUE;
481 }
482 } else {
483 /* Color outputs must match RT slot,
484 * drivers will have to handle NULL entries for GL, too.
485 */
486 fb->cbufs[i] = NULL;
487 }
488 }
489
490 if (state->ds && state->ds->desc.Width >= w &&
491 state->ds->desc.Height >= h &&
492 state->ds->desc.MultiSampleType == nr_samples) {
493 fb->zsbuf = NineSurface9_GetSurface(state->ds, 0);
494 } else {
495 fb->zsbuf = NULL;
496 }
497
498 fb->width = w;
499 fb->height = h;
500
501 pipe->set_framebuffer_state(pipe, fb); /* XXX: cso ? */
502
503 if (is_clear && state->rt_mask == ps_mask)
504 state->changed.group &= ~NINE_STATE_FB;
505 }
506
507 static void
508 update_viewport(struct NineDevice9 *device)
509 {
510 const D3DVIEWPORT9 *vport = &device->state.viewport;
511 struct pipe_viewport_state pvport;
512
513 /* D3D coordinates are:
514 * -1 .. +1 for X,Y and
515 * 0 .. +1 for Z (we use pipe_rasterizer_state.clip_halfz)
516 */
517 pvport.scale[0] = (float)vport->Width * 0.5f;
518 pvport.scale[1] = (float)vport->Height * -0.5f;
519 pvport.scale[2] = vport->MaxZ - vport->MinZ;
520 pvport.translate[0] = (float)vport->Width * 0.5f + (float)vport->X;
521 pvport.translate[1] = (float)vport->Height * 0.5f + (float)vport->Y;
522 pvport.translate[2] = vport->MinZ;
523
524 /* We found R600 and SI cards have some imprecision
525 * on the barycentric coordinates used for interpolation.
526 * Some shaders rely on having something precise.
527 * We found that the proprietary driver has the imprecision issue,
528 * except when the render target width and height are powers of two.
529 * It is using some sort of workaround for these cases
530 * which covers likely all the cases the applications rely
531 * on something precise.
532 * We haven't found the workaround, but it seems like it's better
533 * for applications if the imprecision is biased towards infinity
534 * instead of -infinity (which is what measured). So shift slightly
535 * the viewport: not enough to change rasterization result (in particular
536 * for multisampling), but enough to make the imprecision biased
537 * towards infinity. We do this shift only if render target width and
538 * height are powers of two.
539 * Solves 'red shadows' bug on UE3 games.
540 */
541 if (device->driver_bugs.buggy_barycentrics &&
542 ((vport->Width & (vport->Width-1)) == 0) &&
543 ((vport->Height & (vport->Height-1)) == 0)) {
544 pvport.translate[0] -= 1.0f / 128.0f;
545 pvport.translate[1] -= 1.0f / 128.0f;
546 }
547
548 cso_set_viewport(device->cso, &pvport);
549 }
550
551 /* Loop through VS inputs and pick the vertex elements with the declared
552 * usage from the vertex declaration, then insert the instance divisor from
553 * the stream source frequency setting.
554 */
555 static void
556 update_vertex_elements(struct NineDevice9 *device)
557 {
558 struct nine_state *state = &device->state;
559 const struct NineVertexDeclaration9 *vdecl = device->state.vdecl;
560 const struct NineVertexShader9 *vs;
561 unsigned n, b, i;
562 int index;
563 char vdecl_index_map[16]; /* vs->num_inputs <= 16 */
564 char used_streams[device->caps.MaxStreams];
565 int dummy_vbo_stream = -1;
566 BOOL need_dummy_vbo = FALSE;
567 struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS];
568
569 state->stream_usage_mask = 0;
570 memset(vdecl_index_map, -1, 16);
571 memset(used_streams, 0, device->caps.MaxStreams);
572 vs = state->programmable_vs ? device->state.vs : device->ff.vs;
573
574 if (vdecl) {
575 for (n = 0; n < vs->num_inputs; ++n) {
576 DBG("looking up input %u (usage %u) from vdecl(%p)\n",
577 n, vs->input_map[n].ndecl, vdecl);
578
579 for (i = 0; i < vdecl->nelems; i++) {
580 if (vdecl->usage_map[i] == vs->input_map[n].ndecl) {
581 vdecl_index_map[n] = i;
582 used_streams[vdecl->elems[i].vertex_buffer_index] = 1;
583 break;
584 }
585 }
586 if (vdecl_index_map[n] < 0)
587 need_dummy_vbo = TRUE;
588 }
589 } else {
590 /* No vertex declaration. Likely will never happen in practice,
591 * but we need not crash on this */
592 need_dummy_vbo = TRUE;
593 }
594
595 if (need_dummy_vbo) {
596 for (i = 0; i < device->caps.MaxStreams; i++ ) {
597 if (!used_streams[i]) {
598 dummy_vbo_stream = i;
599 break;
600 }
601 }
602 }
603 /* there are less vertex shader inputs than stream slots,
604 * so if we need a slot for the dummy vbo, we should have found one */
605 assert (!need_dummy_vbo || dummy_vbo_stream != -1);
606
607 for (n = 0; n < vs->num_inputs; ++n) {
608 index = vdecl_index_map[n];
609 if (index >= 0) {
610 ve[n] = vdecl->elems[index];
611 b = ve[n].vertex_buffer_index;
612 state->stream_usage_mask |= 1 << b;
613 /* XXX wine just uses 1 here: */
614 if (state->stream_freq[b] & D3DSTREAMSOURCE_INSTANCEDATA)
615 ve[n].instance_divisor = state->stream_freq[b] & 0x7FFFFF;
616 } else {
617 /* if the vertex declaration is incomplete compared to what the
618 * vertex shader needs, we bind a dummy vbo with 0 0 0 0.
619 * This is not precised by the spec, but is the behaviour
620 * tested on win */
621 ve[n].vertex_buffer_index = dummy_vbo_stream;
622 ve[n].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
623 ve[n].src_offset = 0;
624 ve[n].instance_divisor = 0;
625 }
626 }
627
628 if (state->dummy_vbo_bound_at != dummy_vbo_stream) {
629 if (state->dummy_vbo_bound_at >= 0)
630 state->changed.vtxbuf |= 1 << state->dummy_vbo_bound_at;
631 if (dummy_vbo_stream >= 0) {
632 state->changed.vtxbuf |= 1 << dummy_vbo_stream;
633 state->vbo_bound_done = FALSE;
634 }
635 state->dummy_vbo_bound_at = dummy_vbo_stream;
636 }
637
638 cso_set_vertex_elements(device->cso, vs->num_inputs, ve);
639
640 state->changed.stream_freq = 0;
641 }
642
643 static void
644 update_vertex_buffers(struct NineDevice9 *device)
645 {
646 struct pipe_context *pipe = device->pipe;
647 struct nine_state *state = &device->state;
648 struct pipe_vertex_buffer dummy_vtxbuf;
649 uint32_t mask = state->changed.vtxbuf;
650 unsigned i;
651
652 DBG("mask=%x\n", mask);
653
654 if (state->dummy_vbo_bound_at >= 0) {
655 if (!state->vbo_bound_done) {
656 dummy_vtxbuf.buffer = device->dummy_vbo;
657 dummy_vtxbuf.stride = 0;
658 dummy_vtxbuf.user_buffer = NULL;
659 dummy_vtxbuf.buffer_offset = 0;
660 pipe->set_vertex_buffers(pipe, state->dummy_vbo_bound_at,
661 1, &dummy_vtxbuf);
662 state->vbo_bound_done = TRUE;
663 }
664 mask &= ~(1 << state->dummy_vbo_bound_at);
665 }
666
667 for (i = 0; mask; mask >>= 1, ++i) {
668 if (mask & 1) {
669 if (state->vtxbuf[i].buffer)
670 pipe->set_vertex_buffers(pipe, i, 1, &state->vtxbuf[i]);
671 else
672 pipe->set_vertex_buffers(pipe, i, 1, NULL);
673 }
674 }
675
676 state->changed.vtxbuf = 0;
677 }
678
679 static inline boolean
680 update_sampler_derived(struct nine_state *state, unsigned s)
681 {
682 boolean changed = FALSE;
683
684 if (state->samp[s][NINED3DSAMP_SHADOW] != state->texture[s]->shadow) {
685 changed = TRUE;
686 state->samp[s][NINED3DSAMP_SHADOW] = state->texture[s]->shadow;
687 }
688
689 if (state->samp[s][D3DSAMP_MIPFILTER] != D3DTEXF_NONE) {
690 int lod = state->samp[s][D3DSAMP_MAXMIPLEVEL] - state->texture[s]->managed.lod;
691 if (lod < 0)
692 lod = 0;
693 if (state->samp[s][NINED3DSAMP_MINLOD] != lod) {
694 changed = TRUE;
695 state->samp[s][NINED3DSAMP_MINLOD] = lod;
696 }
697 } else {
698 state->changed.sampler[s] &= ~0x300; /* lod changes irrelevant */
699 }
700
701 return changed;
702 }
703
704 /* TODO: add sRGB override to pipe_sampler_state ? */
705 static void
706 update_textures_and_samplers(struct NineDevice9 *device)
707 {
708 struct nine_state *state = &device->state;
709 struct pipe_sampler_view *view[NINE_MAX_SAMPLERS];
710 unsigned num_textures;
711 unsigned i;
712 boolean commit_samplers;
713 uint16_t sampler_mask = state->ps ? state->ps->sampler_mask :
714 device->ff.ps->sampler_mask;
715
716 /* TODO: Can we reduce iterations here ? */
717
718 commit_samplers = FALSE;
719 state->bound_samplers_mask_ps = 0;
720 for (num_textures = 0, i = 0; i < NINE_MAX_SAMPLERS_PS; ++i) {
721 const unsigned s = NINE_SAMPLER_PS(i);
722 int sRGB;
723
724 if (!state->texture[s] && !(sampler_mask & (1 << i))) {
725 view[i] = NULL;
726 continue;
727 }
728
729 if (state->texture[s]) {
730 sRGB = state->samp[s][D3DSAMP_SRGBTEXTURE] ? 1 : 0;
731
732 view[i] = NineBaseTexture9_GetSamplerView(state->texture[s], sRGB);
733 num_textures = i + 1;
734
735 if (update_sampler_derived(state, s) || (state->changed.sampler[s] & 0x05fe)) {
736 state->changed.sampler[s] = 0;
737 commit_samplers = TRUE;
738 nine_convert_sampler_state(device->cso, s, state->samp[s]);
739 }
740 } else {
741 /* Bind dummy sampler. We do not bind dummy sampler when
742 * it is not needed because it could add overhead. The
743 * dummy sampler should have r=g=b=0 and a=1. We do not
744 * unbind dummy sampler directly when they are not needed
745 * anymore, but they're going to be removed as long as texture
746 * or sampler states are changed. */
747 view[i] = device->dummy_sampler_view;
748 num_textures = i + 1;
749
750 cso_single_sampler(device->cso, PIPE_SHADER_FRAGMENT,
751 s - NINE_SAMPLER_PS(0), &device->dummy_sampler_state);
752
753 commit_samplers = TRUE;
754 state->changed.sampler[s] = ~0;
755 }
756
757 state->bound_samplers_mask_ps |= (1 << s);
758 }
759
760 cso_set_sampler_views(device->cso, PIPE_SHADER_FRAGMENT, num_textures, view);
761
762 if (commit_samplers)
763 cso_single_sampler_done(device->cso, PIPE_SHADER_FRAGMENT);
764
765 commit_samplers = FALSE;
766 sampler_mask = state->programmable_vs ? state->vs->sampler_mask : 0;
767 state->bound_samplers_mask_vs = 0;
768 for (num_textures = 0, i = 0; i < NINE_MAX_SAMPLERS_VS; ++i) {
769 const unsigned s = NINE_SAMPLER_VS(i);
770 int sRGB;
771
772 if (!state->texture[s] && !(sampler_mask & (1 << i))) {
773 view[i] = NULL;
774 continue;
775 }
776
777 if (state->texture[s]) {
778 sRGB = state->samp[s][D3DSAMP_SRGBTEXTURE] ? 1 : 0;
779
780 view[i] = NineBaseTexture9_GetSamplerView(state->texture[s], sRGB);
781 num_textures = i + 1;
782
783 if (update_sampler_derived(state, s) || (state->changed.sampler[s] & 0x05fe)) {
784 state->changed.sampler[s] = 0;
785 commit_samplers = TRUE;
786 nine_convert_sampler_state(device->cso, s, state->samp[s]);
787 }
788 } else {
789 /* Bind dummy sampler. We do not bind dummy sampler when
790 * it is not needed because it could add overhead. The
791 * dummy sampler should have r=g=b=0 and a=1. We do not
792 * unbind dummy sampler directly when they are not needed
793 * anymore, but they're going to be removed as long as texture
794 * or sampler states are changed. */
795 view[i] = device->dummy_sampler_view;
796 num_textures = i + 1;
797
798 cso_single_sampler(device->cso, PIPE_SHADER_VERTEX,
799 s - NINE_SAMPLER_VS(0), &device->dummy_sampler_state);
800
801 commit_samplers = TRUE;
802 state->changed.sampler[s] = ~0;
803 }
804
805 state->bound_samplers_mask_vs |= (1 << s);
806 }
807
808 cso_set_sampler_views(device->cso, PIPE_SHADER_VERTEX, num_textures, view);
809
810 if (commit_samplers)
811 cso_single_sampler_done(device->cso, PIPE_SHADER_VERTEX);
812
813 state->changed.texture = 0;
814 }
815
816 /* State commit only */
817
818 static inline void
819 commit_blend(struct NineDevice9 *device)
820 {
821 cso_set_blend(device->cso, &device->state.pipe.blend);
822 }
823
824 static inline void
825 commit_dsa(struct NineDevice9 *device)
826 {
827 cso_set_depth_stencil_alpha(device->cso, &device->state.pipe.dsa);
828 }
829
830 static inline void
831 commit_scissor(struct NineDevice9 *device)
832 {
833 struct pipe_context *pipe = device->pipe;
834
835 pipe->set_scissor_states(pipe, 0, 1, &device->state.scissor);
836 }
837
838 static inline void
839 commit_rasterizer(struct NineDevice9 *device)
840 {
841 cso_set_rasterizer(device->cso, &device->state.pipe.rast);
842 }
843
844 static inline void
845 commit_index_buffer(struct NineDevice9 *device)
846 {
847 struct pipe_context *pipe = device->pipe;
848 if (device->state.idxbuf)
849 pipe->set_index_buffer(pipe, &device->state.idxbuf->buffer);
850 else
851 pipe->set_index_buffer(pipe, NULL);
852 }
853
854 static inline void
855 commit_vs_constants(struct NineDevice9 *device)
856 {
857 struct pipe_context *pipe = device->pipe;
858
859 if (unlikely(!device->state.programmable_vs))
860 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, &device->state.pipe.cb_vs_ff);
861 else
862 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, &device->state.pipe.cb_vs);
863 }
864
865 static inline void
866 commit_ps_constants(struct NineDevice9 *device)
867 {
868 struct pipe_context *pipe = device->pipe;
869
870 if (unlikely(!device->state.ps))
871 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, &device->state.pipe.cb_ps_ff);
872 else
873 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, &device->state.pipe.cb_ps);
874 }
875
876 static inline void
877 commit_vs(struct NineDevice9 *device)
878 {
879 struct nine_state *state = &device->state;
880
881 device->pipe->bind_vs_state(device->pipe, state->cso.vs);
882 }
883
884
885 static inline void
886 commit_ps(struct NineDevice9 *device)
887 {
888 struct nine_state *state = &device->state;
889
890 device->pipe->bind_fs_state(device->pipe, state->cso.ps);
891 }
892 /* State Update */
893
894 #define NINE_STATE_SHADER_CHANGE_VS \
895 (NINE_STATE_VS | \
896 NINE_STATE_TEXTURE | \
897 NINE_STATE_FOG_SHADER)
898
899 #define NINE_STATE_SHADER_CHANGE_PS \
900 (NINE_STATE_PS | \
901 NINE_STATE_TEXTURE | \
902 NINE_STATE_FOG_SHADER | \
903 NINE_STATE_PS1X_SHADER)
904
905 #define NINE_STATE_FREQUENT \
906 (NINE_STATE_RASTERIZER | \
907 NINE_STATE_TEXTURE | \
908 NINE_STATE_SAMPLER | \
909 NINE_STATE_VS_CONST | \
910 NINE_STATE_PS_CONST)
911
912 #define NINE_STATE_COMMON \
913 (NINE_STATE_FB | \
914 NINE_STATE_BLEND | \
915 NINE_STATE_DSA | \
916 NINE_STATE_VIEWPORT | \
917 NINE_STATE_VDECL | \
918 NINE_STATE_IDXBUF | \
919 NINE_STATE_STREAMFREQ)
920
921 #define NINE_STATE_RARE \
922 (NINE_STATE_SCISSOR | \
923 NINE_STATE_BLEND_COLOR | \
924 NINE_STATE_STENCIL_REF | \
925 NINE_STATE_SAMPLE_MASK)
926
927
928 /* TODO: only go through dirty textures */
929 static void
930 validate_textures(struct NineDevice9 *device)
931 {
932 struct NineBaseTexture9 *tex, *ptr;
933 LIST_FOR_EACH_ENTRY_SAFE(tex, ptr, &device->update_textures, list) {
934 list_delinit(&tex->list);
935 NineBaseTexture9_Validate(tex);
936 }
937 }
938
939 static void
940 update_managed_buffers(struct NineDevice9 *device)
941 {
942 struct NineBuffer9 *buf, *ptr;
943 LIST_FOR_EACH_ENTRY_SAFE(buf, ptr, &device->update_buffers, managed.list) {
944 list_delinit(&buf->managed.list);
945 NineBuffer9_Upload(buf);
946 }
947 }
948
949 void
950 nine_update_state_framebuffer_clear(struct NineDevice9 *device)
951 {
952 struct nine_state *state = &device->state;
953
954 validate_textures(device);
955
956 if (state->changed.group & NINE_STATE_FB)
957 update_framebuffer(device, TRUE);
958 }
959
960 boolean
961 nine_update_state(struct NineDevice9 *device)
962 {
963 struct pipe_context *pipe = device->pipe;
964 struct nine_state *state = &device->state;
965 uint32_t group;
966
967 DBG("changed state groups: %x\n", state->changed.group);
968
969 /* NOTE: We may want to use the cso cache for everything, or let
970 * NineDevice9.RestoreNonCSOState actually set the states, then we wouldn't
971 * have to care about state being clobbered here and could merge this back
972 * into update_textures. Except, we also need to re-validate textures that
973 * may be dirty anyway, even if no texture bindings changed.
974 */
975 validate_textures(device); /* may clobber state */
976 update_managed_buffers(device);
977
978 /* ff_update may change VS/PS dirty bits */
979 if (unlikely(!state->programmable_vs || !state->ps))
980 nine_ff_update(device);
981 group = state->changed.group;
982
983 if (group & (NINE_STATE_SHADER_CHANGE_VS | NINE_STATE_SHADER_CHANGE_PS)) {
984 if (group & NINE_STATE_SHADER_CHANGE_VS)
985 group |= prepare_vs(device, (group & NINE_STATE_VS) != 0); /* may set NINE_STATE_RASTERIZER and NINE_STATE_SAMPLER*/
986 if (group & NINE_STATE_SHADER_CHANGE_PS)
987 group |= prepare_ps(device, (group & NINE_STATE_PS) != 0);
988 }
989
990 if (group & (NINE_STATE_COMMON | NINE_STATE_VS)) {
991 if (group & NINE_STATE_FB)
992 update_framebuffer(device, FALSE);
993 if (group & NINE_STATE_BLEND)
994 prepare_blend(device);
995 if (group & NINE_STATE_DSA)
996 prepare_dsa(device);
997 if (group & NINE_STATE_VIEWPORT)
998 update_viewport(device);
999 if (group & (NINE_STATE_VDECL | NINE_STATE_VS | NINE_STATE_STREAMFREQ))
1000 update_vertex_elements(device);
1001 if (group & NINE_STATE_IDXBUF)
1002 commit_index_buffer(device);
1003 }
1004
1005 if (likely(group & (NINE_STATE_FREQUENT | NINE_STATE_VS | NINE_STATE_PS))) {
1006 if (group & NINE_STATE_RASTERIZER)
1007 prepare_rasterizer(device);
1008 if (group & (NINE_STATE_TEXTURE | NINE_STATE_SAMPLER))
1009 update_textures_and_samplers(device);
1010 if (device->prefer_user_constbuf) {
1011 if ((group & (NINE_STATE_VS_CONST | NINE_STATE_VS)) && state->programmable_vs)
1012 prepare_vs_constants_userbuf(device);
1013 if ((group & (NINE_STATE_PS_CONST | NINE_STATE_PS)) && state->ps)
1014 prepare_ps_constants_userbuf(device);
1015 } else {
1016 if ((group & NINE_STATE_VS_CONST) && state->programmable_vs)
1017 upload_constants(device, PIPE_SHADER_VERTEX);
1018 if ((group & NINE_STATE_PS_CONST) && state->ps)
1019 upload_constants(device, PIPE_SHADER_FRAGMENT);
1020 }
1021 }
1022
1023 if (state->changed.vtxbuf)
1024 update_vertex_buffers(device);
1025
1026 if (state->commit & NINE_STATE_COMMIT_BLEND)
1027 commit_blend(device);
1028 if (state->commit & NINE_STATE_COMMIT_DSA)
1029 commit_dsa(device);
1030 if (state->commit & NINE_STATE_COMMIT_RASTERIZER)
1031 commit_rasterizer(device);
1032 if (state->commit & NINE_STATE_COMMIT_CONST_VS)
1033 commit_vs_constants(device);
1034 if (state->commit & NINE_STATE_COMMIT_CONST_PS)
1035 commit_ps_constants(device);
1036 if (state->commit & NINE_STATE_COMMIT_VS)
1037 commit_vs(device);
1038 if (state->commit & NINE_STATE_COMMIT_PS)
1039 commit_ps(device);
1040
1041 state->commit = 0;
1042
1043 if (unlikely(state->changed.ucp)) {
1044 pipe->set_clip_state(pipe, &state->clip);
1045 state->changed.ucp = 0;
1046 }
1047
1048 if (unlikely(group & NINE_STATE_RARE)) {
1049 if (group & NINE_STATE_SCISSOR)
1050 commit_scissor(device);
1051 if (group & NINE_STATE_BLEND_COLOR) {
1052 struct pipe_blend_color color;
1053 d3dcolor_to_rgba(&color.color[0], state->rs[D3DRS_BLENDFACTOR]);
1054 pipe->set_blend_color(pipe, &color);
1055 }
1056 if (group & NINE_STATE_SAMPLE_MASK) {
1057 pipe->set_sample_mask(pipe, state->rs[D3DRS_MULTISAMPLEMASK]);
1058 }
1059 if (group & NINE_STATE_STENCIL_REF) {
1060 struct pipe_stencil_ref ref;
1061 ref.ref_value[0] = state->rs[D3DRS_STENCILREF];
1062 ref.ref_value[1] = ref.ref_value[0];
1063 pipe->set_stencil_ref(pipe, &ref);
1064 }
1065 }
1066
1067 device->state.changed.group &=
1068 (NINE_STATE_FF | NINE_STATE_VS_CONST | NINE_STATE_PS_CONST);
1069
1070 DBG("finished\n");
1071
1072 return TRUE;
1073 }
1074
1075 /* State defaults */
1076
1077 static const DWORD nine_render_state_defaults[NINED3DRS_LAST + 1] =
1078 {
1079 /* [D3DRS_ZENABLE] = D3DZB_TRUE; wine: auto_depth_stencil */
1080 [D3DRS_ZENABLE] = D3DZB_FALSE,
1081 [D3DRS_FILLMODE] = D3DFILL_SOLID,
1082 [D3DRS_SHADEMODE] = D3DSHADE_GOURAUD,
1083 /* [D3DRS_LINEPATTERN] = 0x00000000, */
1084 [D3DRS_ZWRITEENABLE] = TRUE,
1085 [D3DRS_ALPHATESTENABLE] = FALSE,
1086 [D3DRS_LASTPIXEL] = TRUE,
1087 [D3DRS_SRCBLEND] = D3DBLEND_ONE,
1088 [D3DRS_DESTBLEND] = D3DBLEND_ZERO,
1089 [D3DRS_CULLMODE] = D3DCULL_CCW,
1090 [D3DRS_ZFUNC] = D3DCMP_LESSEQUAL,
1091 [D3DRS_ALPHAFUNC] = D3DCMP_ALWAYS,
1092 [D3DRS_ALPHAREF] = 0,
1093 [D3DRS_DITHERENABLE] = FALSE,
1094 [D3DRS_ALPHABLENDENABLE] = FALSE,
1095 [D3DRS_FOGENABLE] = FALSE,
1096 [D3DRS_SPECULARENABLE] = FALSE,
1097 /* [D3DRS_ZVISIBLE] = 0, */
1098 [D3DRS_FOGCOLOR] = 0,
1099 [D3DRS_FOGTABLEMODE] = D3DFOG_NONE,
1100 [D3DRS_FOGSTART] = 0x00000000,
1101 [D3DRS_FOGEND] = 0x3F800000,
1102 [D3DRS_FOGDENSITY] = 0x3F800000,
1103 /* [D3DRS_EDGEANTIALIAS] = FALSE, */
1104 [D3DRS_RANGEFOGENABLE] = FALSE,
1105 [D3DRS_STENCILENABLE] = FALSE,
1106 [D3DRS_STENCILFAIL] = D3DSTENCILOP_KEEP,
1107 [D3DRS_STENCILZFAIL] = D3DSTENCILOP_KEEP,
1108 [D3DRS_STENCILPASS] = D3DSTENCILOP_KEEP,
1109 [D3DRS_STENCILREF] = 0,
1110 [D3DRS_STENCILMASK] = 0xFFFFFFFF,
1111 [D3DRS_STENCILFUNC] = D3DCMP_ALWAYS,
1112 [D3DRS_STENCILWRITEMASK] = 0xFFFFFFFF,
1113 [D3DRS_TEXTUREFACTOR] = 0xFFFFFFFF,
1114 [D3DRS_WRAP0] = 0,
1115 [D3DRS_WRAP1] = 0,
1116 [D3DRS_WRAP2] = 0,
1117 [D3DRS_WRAP3] = 0,
1118 [D3DRS_WRAP4] = 0,
1119 [D3DRS_WRAP5] = 0,
1120 [D3DRS_WRAP6] = 0,
1121 [D3DRS_WRAP7] = 0,
1122 [D3DRS_CLIPPING] = TRUE,
1123 [D3DRS_LIGHTING] = TRUE,
1124 [D3DRS_AMBIENT] = 0,
1125 [D3DRS_FOGVERTEXMODE] = D3DFOG_NONE,
1126 [D3DRS_COLORVERTEX] = TRUE,
1127 [D3DRS_LOCALVIEWER] = TRUE,
1128 [D3DRS_NORMALIZENORMALS] = FALSE,
1129 [D3DRS_DIFFUSEMATERIALSOURCE] = D3DMCS_COLOR1,
1130 [D3DRS_SPECULARMATERIALSOURCE] = D3DMCS_COLOR2,
1131 [D3DRS_AMBIENTMATERIALSOURCE] = D3DMCS_MATERIAL,
1132 [D3DRS_EMISSIVEMATERIALSOURCE] = D3DMCS_MATERIAL,
1133 [D3DRS_VERTEXBLEND] = D3DVBF_DISABLE,
1134 [D3DRS_CLIPPLANEENABLE] = 0,
1135 /* [D3DRS_SOFTWAREVERTEXPROCESSING] = FALSE, */
1136 [D3DRS_POINTSIZE] = 0x3F800000,
1137 [D3DRS_POINTSIZE_MIN] = 0x3F800000,
1138 [D3DRS_POINTSPRITEENABLE] = FALSE,
1139 [D3DRS_POINTSCALEENABLE] = FALSE,
1140 [D3DRS_POINTSCALE_A] = 0x3F800000,
1141 [D3DRS_POINTSCALE_B] = 0x00000000,
1142 [D3DRS_POINTSCALE_C] = 0x00000000,
1143 [D3DRS_MULTISAMPLEANTIALIAS] = TRUE,
1144 [D3DRS_MULTISAMPLEMASK] = 0xFFFFFFFF,
1145 [D3DRS_PATCHEDGESTYLE] = D3DPATCHEDGE_DISCRETE,
1146 /* [D3DRS_PATCHSEGMENTS] = 0x3F800000, */
1147 [D3DRS_DEBUGMONITORTOKEN] = 0xDEADCAFE,
1148 [D3DRS_POINTSIZE_MAX] = 0x3F800000, /* depends on cap */
1149 [D3DRS_INDEXEDVERTEXBLENDENABLE] = FALSE,
1150 [D3DRS_COLORWRITEENABLE] = 0x0000000f,
1151 [D3DRS_TWEENFACTOR] = 0x00000000,
1152 [D3DRS_BLENDOP] = D3DBLENDOP_ADD,
1153 [D3DRS_POSITIONDEGREE] = D3DDEGREE_CUBIC,
1154 [D3DRS_NORMALDEGREE] = D3DDEGREE_LINEAR,
1155 [D3DRS_SCISSORTESTENABLE] = FALSE,
1156 [D3DRS_SLOPESCALEDEPTHBIAS] = 0,
1157 [D3DRS_MINTESSELLATIONLEVEL] = 0x3F800000,
1158 [D3DRS_MAXTESSELLATIONLEVEL] = 0x3F800000,
1159 [D3DRS_ANTIALIASEDLINEENABLE] = FALSE,
1160 [D3DRS_ADAPTIVETESS_X] = 0x00000000,
1161 [D3DRS_ADAPTIVETESS_Y] = 0x00000000,
1162 [D3DRS_ADAPTIVETESS_Z] = 0x3F800000,
1163 [D3DRS_ADAPTIVETESS_W] = 0x00000000,
1164 [D3DRS_ENABLEADAPTIVETESSELLATION] = FALSE,
1165 [D3DRS_TWOSIDEDSTENCILMODE] = FALSE,
1166 [D3DRS_CCW_STENCILFAIL] = D3DSTENCILOP_KEEP,
1167 [D3DRS_CCW_STENCILZFAIL] = D3DSTENCILOP_KEEP,
1168 [D3DRS_CCW_STENCILPASS] = D3DSTENCILOP_KEEP,
1169 [D3DRS_CCW_STENCILFUNC] = D3DCMP_ALWAYS,
1170 [D3DRS_COLORWRITEENABLE1] = 0x0000000F,
1171 [D3DRS_COLORWRITEENABLE2] = 0x0000000F,
1172 [D3DRS_COLORWRITEENABLE3] = 0x0000000F,
1173 [D3DRS_BLENDFACTOR] = 0xFFFFFFFF,
1174 [D3DRS_SRGBWRITEENABLE] = 0,
1175 [D3DRS_DEPTHBIAS] = 0,
1176 [D3DRS_WRAP8] = 0,
1177 [D3DRS_WRAP9] = 0,
1178 [D3DRS_WRAP10] = 0,
1179 [D3DRS_WRAP11] = 0,
1180 [D3DRS_WRAP12] = 0,
1181 [D3DRS_WRAP13] = 0,
1182 [D3DRS_WRAP14] = 0,
1183 [D3DRS_WRAP15] = 0,
1184 [D3DRS_SEPARATEALPHABLENDENABLE] = FALSE,
1185 [D3DRS_SRCBLENDALPHA] = D3DBLEND_ONE,
1186 [D3DRS_DESTBLENDALPHA] = D3DBLEND_ZERO,
1187 [D3DRS_BLENDOPALPHA] = D3DBLENDOP_ADD,
1188 [NINED3DRS_VSPOINTSIZE] = FALSE,
1189 [NINED3DRS_RTMASK] = 0xf,
1190 [NINED3DRS_ALPHACOVERAGE] = FALSE
1191 };
1192 static const DWORD nine_tex_stage_state_defaults[NINED3DTSS_LAST + 1] =
1193 {
1194 [D3DTSS_COLOROP] = D3DTOP_DISABLE,
1195 [D3DTSS_ALPHAOP] = D3DTOP_DISABLE,
1196 [D3DTSS_COLORARG1] = D3DTA_TEXTURE,
1197 [D3DTSS_COLORARG2] = D3DTA_CURRENT,
1198 [D3DTSS_COLORARG0] = D3DTA_CURRENT,
1199 [D3DTSS_ALPHAARG1] = D3DTA_TEXTURE,
1200 [D3DTSS_ALPHAARG2] = D3DTA_CURRENT,
1201 [D3DTSS_ALPHAARG0] = D3DTA_CURRENT,
1202 [D3DTSS_RESULTARG] = D3DTA_CURRENT,
1203 [D3DTSS_BUMPENVMAT00] = 0,
1204 [D3DTSS_BUMPENVMAT01] = 0,
1205 [D3DTSS_BUMPENVMAT10] = 0,
1206 [D3DTSS_BUMPENVMAT11] = 0,
1207 [D3DTSS_BUMPENVLSCALE] = 0,
1208 [D3DTSS_BUMPENVLOFFSET] = 0,
1209 [D3DTSS_TEXCOORDINDEX] = 0,
1210 [D3DTSS_TEXTURETRANSFORMFLAGS] = D3DTTFF_DISABLE,
1211 };
1212 static const DWORD nine_samp_state_defaults[NINED3DSAMP_LAST + 1] =
1213 {
1214 [D3DSAMP_ADDRESSU] = D3DTADDRESS_WRAP,
1215 [D3DSAMP_ADDRESSV] = D3DTADDRESS_WRAP,
1216 [D3DSAMP_ADDRESSW] = D3DTADDRESS_WRAP,
1217 [D3DSAMP_BORDERCOLOR] = 0,
1218 [D3DSAMP_MAGFILTER] = D3DTEXF_POINT,
1219 [D3DSAMP_MINFILTER] = D3DTEXF_POINT,
1220 [D3DSAMP_MIPFILTER] = D3DTEXF_NONE,
1221 [D3DSAMP_MIPMAPLODBIAS] = 0,
1222 [D3DSAMP_MAXMIPLEVEL] = 0,
1223 [D3DSAMP_MAXANISOTROPY] = 1,
1224 [D3DSAMP_SRGBTEXTURE] = 0,
1225 [D3DSAMP_ELEMENTINDEX] = 0,
1226 [D3DSAMP_DMAPOFFSET] = 0,
1227 [NINED3DSAMP_MINLOD] = 0,
1228 [NINED3DSAMP_SHADOW] = 0
1229 };
1230
1231 void nine_state_restore_non_cso(struct NineDevice9 *device)
1232 {
1233 struct nine_state *state = &device->state;
1234
1235 state->changed.group = NINE_STATE_ALL;
1236 state->changed.vtxbuf = (1ULL << device->caps.MaxStreams) - 1;
1237 state->changed.ucp = (1 << PIPE_MAX_CLIP_PLANES) - 1;
1238 state->changed.texture = NINE_PS_SAMPLERS_MASK | NINE_VS_SAMPLERS_MASK;
1239 state->commit |= NINE_STATE_COMMIT_CONST_VS | NINE_STATE_COMMIT_CONST_PS;
1240 }
1241
1242 void
1243 nine_state_set_defaults(struct NineDevice9 *device, const D3DCAPS9 *caps,
1244 boolean is_reset)
1245 {
1246 struct nine_state *state = &device->state;
1247 unsigned s;
1248
1249 /* Initialize defaults.
1250 */
1251 memcpy(state->rs, nine_render_state_defaults, sizeof(state->rs));
1252
1253 for (s = 0; s < ARRAY_SIZE(state->ff.tex_stage); ++s) {
1254 memcpy(&state->ff.tex_stage[s], nine_tex_stage_state_defaults,
1255 sizeof(state->ff.tex_stage[s]));
1256 state->ff.tex_stage[s][D3DTSS_TEXCOORDINDEX] = s;
1257 }
1258 state->ff.tex_stage[0][D3DTSS_COLOROP] = D3DTOP_MODULATE;
1259 state->ff.tex_stage[0][D3DTSS_ALPHAOP] = D3DTOP_SELECTARG1;
1260 memset(&state->bumpmap_vars, 0, sizeof(state->bumpmap_vars));
1261
1262 for (s = 0; s < ARRAY_SIZE(state->samp); ++s) {
1263 memcpy(&state->samp[s], nine_samp_state_defaults,
1264 sizeof(state->samp[s]));
1265 }
1266
1267 if (state->vs_const_f)
1268 memset(state->vs_const_f, 0, device->vs_const_size);
1269 if (state->ps_const_f)
1270 memset(state->ps_const_f, 0, device->ps_const_size);
1271
1272 /* Cap dependent initial state:
1273 */
1274 state->rs[D3DRS_POINTSIZE_MAX] = fui(caps->MaxPointSize);
1275
1276 memcpy(state->rs_advertised, state->rs, sizeof(state->rs));
1277
1278 /* Set changed flags to initialize driver.
1279 */
1280 state->changed.group = NINE_STATE_ALL;
1281 state->changed.vtxbuf = (1ULL << device->caps.MaxStreams) - 1;
1282 state->changed.ucp = (1 << PIPE_MAX_CLIP_PLANES) - 1;
1283 state->changed.texture = NINE_PS_SAMPLERS_MASK | NINE_VS_SAMPLERS_MASK;
1284
1285 state->ff.changed.transform[0] = ~0;
1286 state->ff.changed.transform[D3DTS_WORLD / 32] |= 1 << (D3DTS_WORLD % 32);
1287
1288 if (!is_reset) {
1289 state->viewport.MinZ = 0.0f;
1290 state->viewport.MaxZ = 1.0f;
1291 }
1292
1293 for (s = 0; s < ARRAY_SIZE(state->changed.sampler); ++s)
1294 state->changed.sampler[s] = ~0;
1295
1296 if (!is_reset) {
1297 state->dummy_vbo_bound_at = -1;
1298 state->vbo_bound_done = FALSE;
1299 }
1300
1301 if (!device->prefer_user_constbuf) {
1302 /* fill cb_vs and cb_ps for the non user constbuf path */
1303 struct pipe_constant_buffer cb;
1304
1305 cb.buffer_offset = 0;
1306 cb.buffer_size = device->vs_const_size;
1307 cb.buffer = device->constbuf_vs;
1308 cb.user_buffer = NULL;
1309 state->pipe.cb_vs = cb;
1310
1311 cb.buffer_size = device->ps_const_size;
1312 cb.buffer = device->constbuf_ps;
1313 state->pipe.cb_ps = cb;
1314
1315 state->commit |= NINE_STATE_COMMIT_CONST_VS | NINE_STATE_COMMIT_CONST_PS;
1316 }
1317 }
1318
1319 void
1320 nine_state_clear(struct nine_state *state, const boolean device)
1321 {
1322 unsigned i;
1323
1324 for (i = 0; i < ARRAY_SIZE(state->rt); ++i)
1325 nine_bind(&state->rt[i], NULL);
1326 nine_bind(&state->ds, NULL);
1327 nine_bind(&state->vs, NULL);
1328 nine_bind(&state->ps, NULL);
1329 nine_bind(&state->vdecl, NULL);
1330 for (i = 0; i < PIPE_MAX_ATTRIBS; ++i) {
1331 nine_bind(&state->stream[i], NULL);
1332 pipe_resource_reference(&state->vtxbuf[i].buffer, NULL);
1333 }
1334 nine_bind(&state->idxbuf, NULL);
1335 for (i = 0; i < NINE_MAX_SAMPLERS; ++i) {
1336 if (device &&
1337 state->texture[i] &&
1338 --state->texture[i]->bind_count == 0)
1339 list_delinit(&state->texture[i]->list);
1340 nine_bind(&state->texture[i], NULL);
1341 }
1342 }
1343
1344 /*
1345 static const DWORD nine_render_states_pixel[] =
1346 {
1347 D3DRS_ALPHABLENDENABLE,
1348 D3DRS_ALPHAFUNC,
1349 D3DRS_ALPHAREF,
1350 D3DRS_ALPHATESTENABLE,
1351 D3DRS_ANTIALIASEDLINEENABLE,
1352 D3DRS_BLENDFACTOR,
1353 D3DRS_BLENDOP,
1354 D3DRS_BLENDOPALPHA,
1355 D3DRS_CCW_STENCILFAIL,
1356 D3DRS_CCW_STENCILPASS,
1357 D3DRS_CCW_STENCILZFAIL,
1358 D3DRS_COLORWRITEENABLE,
1359 D3DRS_COLORWRITEENABLE1,
1360 D3DRS_COLORWRITEENABLE2,
1361 D3DRS_COLORWRITEENABLE3,
1362 D3DRS_DEPTHBIAS,
1363 D3DRS_DESTBLEND,
1364 D3DRS_DESTBLENDALPHA,
1365 D3DRS_DITHERENABLE,
1366 D3DRS_FILLMODE,
1367 D3DRS_FOGDENSITY,
1368 D3DRS_FOGEND,
1369 D3DRS_FOGSTART,
1370 D3DRS_LASTPIXEL,
1371 D3DRS_SCISSORTESTENABLE,
1372 D3DRS_SEPARATEALPHABLENDENABLE,
1373 D3DRS_SHADEMODE,
1374 D3DRS_SLOPESCALEDEPTHBIAS,
1375 D3DRS_SRCBLEND,
1376 D3DRS_SRCBLENDALPHA,
1377 D3DRS_SRGBWRITEENABLE,
1378 D3DRS_STENCILENABLE,
1379 D3DRS_STENCILFAIL,
1380 D3DRS_STENCILFUNC,
1381 D3DRS_STENCILMASK,
1382 D3DRS_STENCILPASS,
1383 D3DRS_STENCILREF,
1384 D3DRS_STENCILWRITEMASK,
1385 D3DRS_STENCILZFAIL,
1386 D3DRS_TEXTUREFACTOR,
1387 D3DRS_TWOSIDEDSTENCILMODE,
1388 D3DRS_WRAP0,
1389 D3DRS_WRAP1,
1390 D3DRS_WRAP10,
1391 D3DRS_WRAP11,
1392 D3DRS_WRAP12,
1393 D3DRS_WRAP13,
1394 D3DRS_WRAP14,
1395 D3DRS_WRAP15,
1396 D3DRS_WRAP2,
1397 D3DRS_WRAP3,
1398 D3DRS_WRAP4,
1399 D3DRS_WRAP5,
1400 D3DRS_WRAP6,
1401 D3DRS_WRAP7,
1402 D3DRS_WRAP8,
1403 D3DRS_WRAP9,
1404 D3DRS_ZENABLE,
1405 D3DRS_ZFUNC,
1406 D3DRS_ZWRITEENABLE
1407 };
1408 */
1409 const uint32_t nine_render_states_pixel[(NINED3DRS_LAST + 31) / 32] =
1410 {
1411 0x0f99c380, 0x1ff00070, 0x00000000, 0x00000000,
1412 0x000000ff, 0xde01c900, 0x0003ffcf
1413 };
1414
1415 /*
1416 static const DWORD nine_render_states_vertex[] =
1417 {
1418 D3DRS_ADAPTIVETESS_W,
1419 D3DRS_ADAPTIVETESS_X,
1420 D3DRS_ADAPTIVETESS_Y,
1421 D3DRS_ADAPTIVETESS_Z,
1422 D3DRS_AMBIENT,
1423 D3DRS_AMBIENTMATERIALSOURCE,
1424 D3DRS_CLIPPING,
1425 D3DRS_CLIPPLANEENABLE,
1426 D3DRS_COLORVERTEX,
1427 D3DRS_CULLMODE,
1428 D3DRS_DIFFUSEMATERIALSOURCE,
1429 D3DRS_EMISSIVEMATERIALSOURCE,
1430 D3DRS_ENABLEADAPTIVETESSELLATION,
1431 D3DRS_FOGCOLOR,
1432 D3DRS_FOGDENSITY,
1433 D3DRS_FOGENABLE,
1434 D3DRS_FOGEND,
1435 D3DRS_FOGSTART,
1436 D3DRS_FOGTABLEMODE,
1437 D3DRS_FOGVERTEXMODE,
1438 D3DRS_INDEXEDVERTEXBLENDENABLE,
1439 D3DRS_LIGHTING,
1440 D3DRS_LOCALVIEWER,
1441 D3DRS_MAXTESSELLATIONLEVEL,
1442 D3DRS_MINTESSELLATIONLEVEL,
1443 D3DRS_MULTISAMPLEANTIALIAS,
1444 D3DRS_MULTISAMPLEMASK,
1445 D3DRS_NORMALDEGREE,
1446 D3DRS_NORMALIZENORMALS,
1447 D3DRS_PATCHEDGESTYLE,
1448 D3DRS_POINTSCALE_A,
1449 D3DRS_POINTSCALE_B,
1450 D3DRS_POINTSCALE_C,
1451 D3DRS_POINTSCALEENABLE,
1452 D3DRS_POINTSIZE,
1453 D3DRS_POINTSIZE_MAX,
1454 D3DRS_POINTSIZE_MIN,
1455 D3DRS_POINTSPRITEENABLE,
1456 D3DRS_POSITIONDEGREE,
1457 D3DRS_RANGEFOGENABLE,
1458 D3DRS_SHADEMODE,
1459 D3DRS_SPECULARENABLE,
1460 D3DRS_SPECULARMATERIALSOURCE,
1461 D3DRS_TWEENFACTOR,
1462 D3DRS_VERTEXBLEND
1463 };
1464 */
1465 const uint32_t nine_render_states_vertex[(NINED3DRS_LAST + 31) / 32] =
1466 {
1467 0x30400200, 0x0001007c, 0x00000000, 0x00000000,
1468 0xfd9efb00, 0x01fc34cf, 0x00000000
1469 };
1470
1471 /* TODO: put in the right values */
1472 const uint32_t nine_render_state_group[NINED3DRS_LAST + 1] =
1473 {
1474 [D3DRS_ZENABLE] = NINE_STATE_DSA,
1475 [D3DRS_FILLMODE] = NINE_STATE_RASTERIZER,
1476 [D3DRS_SHADEMODE] = NINE_STATE_RASTERIZER,
1477 [D3DRS_ZWRITEENABLE] = NINE_STATE_DSA,
1478 [D3DRS_ALPHATESTENABLE] = NINE_STATE_DSA,
1479 [D3DRS_LASTPIXEL] = NINE_STATE_RASTERIZER,
1480 [D3DRS_SRCBLEND] = NINE_STATE_BLEND,
1481 [D3DRS_DESTBLEND] = NINE_STATE_BLEND,
1482 [D3DRS_CULLMODE] = NINE_STATE_RASTERIZER,
1483 [D3DRS_ZFUNC] = NINE_STATE_DSA,
1484 [D3DRS_ALPHAREF] = NINE_STATE_DSA,
1485 [D3DRS_ALPHAFUNC] = NINE_STATE_DSA,
1486 [D3DRS_DITHERENABLE] = NINE_STATE_BLEND,
1487 [D3DRS_ALPHABLENDENABLE] = NINE_STATE_BLEND,
1488 [D3DRS_FOGENABLE] = NINE_STATE_FF_OTHER | NINE_STATE_FOG_SHADER | NINE_STATE_PS_CONST,
1489 [D3DRS_SPECULARENABLE] = NINE_STATE_FF_LIGHTING,
1490 [D3DRS_FOGCOLOR] = NINE_STATE_FF_OTHER | NINE_STATE_PS_CONST,
1491 [D3DRS_FOGTABLEMODE] = NINE_STATE_FF_OTHER | NINE_STATE_FOG_SHADER | NINE_STATE_PS_CONST,
1492 [D3DRS_FOGSTART] = NINE_STATE_FF_OTHER | NINE_STATE_PS_CONST,
1493 [D3DRS_FOGEND] = NINE_STATE_FF_OTHER | NINE_STATE_PS_CONST,
1494 [D3DRS_FOGDENSITY] = NINE_STATE_FF_OTHER | NINE_STATE_PS_CONST,
1495 [D3DRS_RANGEFOGENABLE] = NINE_STATE_FF_OTHER,
1496 [D3DRS_STENCILENABLE] = NINE_STATE_DSA,
1497 [D3DRS_STENCILFAIL] = NINE_STATE_DSA,
1498 [D3DRS_STENCILZFAIL] = NINE_STATE_DSA,
1499 [D3DRS_STENCILPASS] = NINE_STATE_DSA,
1500 [D3DRS_STENCILFUNC] = NINE_STATE_DSA,
1501 [D3DRS_STENCILREF] = NINE_STATE_STENCIL_REF,
1502 [D3DRS_STENCILMASK] = NINE_STATE_DSA,
1503 [D3DRS_STENCILWRITEMASK] = NINE_STATE_DSA,
1504 [D3DRS_TEXTUREFACTOR] = NINE_STATE_FF_PSSTAGES,
1505 [D3DRS_WRAP0] = NINE_STATE_UNHANDLED, /* cylindrical wrap is crazy */
1506 [D3DRS_WRAP1] = NINE_STATE_UNHANDLED,
1507 [D3DRS_WRAP2] = NINE_STATE_UNHANDLED,
1508 [D3DRS_WRAP3] = NINE_STATE_UNHANDLED,
1509 [D3DRS_WRAP4] = NINE_STATE_UNHANDLED,
1510 [D3DRS_WRAP5] = NINE_STATE_UNHANDLED,
1511 [D3DRS_WRAP6] = NINE_STATE_UNHANDLED,
1512 [D3DRS_WRAP7] = NINE_STATE_UNHANDLED,
1513 [D3DRS_CLIPPING] = 0, /* software vertex processing only */
1514 [D3DRS_LIGHTING] = NINE_STATE_FF_LIGHTING,
1515 [D3DRS_AMBIENT] = NINE_STATE_FF_LIGHTING | NINE_STATE_FF_MATERIAL,
1516 [D3DRS_FOGVERTEXMODE] = NINE_STATE_FF_OTHER,
1517 [D3DRS_COLORVERTEX] = NINE_STATE_FF_LIGHTING,
1518 [D3DRS_LOCALVIEWER] = NINE_STATE_FF_LIGHTING,
1519 [D3DRS_NORMALIZENORMALS] = NINE_STATE_FF_OTHER,
1520 [D3DRS_DIFFUSEMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
1521 [D3DRS_SPECULARMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
1522 [D3DRS_AMBIENTMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
1523 [D3DRS_EMISSIVEMATERIALSOURCE] = NINE_STATE_FF_LIGHTING,
1524 [D3DRS_VERTEXBLEND] = NINE_STATE_FF_OTHER,
1525 [D3DRS_CLIPPLANEENABLE] = NINE_STATE_RASTERIZER,
1526 [D3DRS_POINTSIZE] = NINE_STATE_RASTERIZER,
1527 [D3DRS_POINTSIZE_MIN] = NINE_STATE_RASTERIZER,
1528 [D3DRS_POINTSPRITEENABLE] = NINE_STATE_RASTERIZER,
1529 [D3DRS_POINTSCALEENABLE] = NINE_STATE_FF_OTHER,
1530 [D3DRS_POINTSCALE_A] = NINE_STATE_FF_OTHER,
1531 [D3DRS_POINTSCALE_B] = NINE_STATE_FF_OTHER,
1532 [D3DRS_POINTSCALE_C] = NINE_STATE_FF_OTHER,
1533 [D3DRS_MULTISAMPLEANTIALIAS] = NINE_STATE_RASTERIZER,
1534 [D3DRS_MULTISAMPLEMASK] = NINE_STATE_SAMPLE_MASK,
1535 [D3DRS_PATCHEDGESTYLE] = NINE_STATE_UNHANDLED,
1536 [D3DRS_DEBUGMONITORTOKEN] = NINE_STATE_UNHANDLED,
1537 [D3DRS_POINTSIZE_MAX] = NINE_STATE_RASTERIZER,
1538 [D3DRS_INDEXEDVERTEXBLENDENABLE] = NINE_STATE_FF_OTHER,
1539 [D3DRS_COLORWRITEENABLE] = NINE_STATE_BLEND,
1540 [D3DRS_TWEENFACTOR] = NINE_STATE_FF_OTHER,
1541 [D3DRS_BLENDOP] = NINE_STATE_BLEND,
1542 [D3DRS_POSITIONDEGREE] = NINE_STATE_UNHANDLED,
1543 [D3DRS_NORMALDEGREE] = NINE_STATE_UNHANDLED,
1544 [D3DRS_SCISSORTESTENABLE] = NINE_STATE_RASTERIZER,
1545 [D3DRS_SLOPESCALEDEPTHBIAS] = NINE_STATE_RASTERIZER,
1546 [D3DRS_ANTIALIASEDLINEENABLE] = NINE_STATE_RASTERIZER,
1547 [D3DRS_MINTESSELLATIONLEVEL] = NINE_STATE_UNHANDLED,
1548 [D3DRS_MAXTESSELLATIONLEVEL] = NINE_STATE_UNHANDLED,
1549 [D3DRS_ADAPTIVETESS_X] = NINE_STATE_UNHANDLED,
1550 [D3DRS_ADAPTIVETESS_Y] = NINE_STATE_UNHANDLED,
1551 [D3DRS_ADAPTIVETESS_Z] = NINE_STATE_UNHANDLED,
1552 [D3DRS_ADAPTIVETESS_W] = NINE_STATE_UNHANDLED,
1553 [D3DRS_ENABLEADAPTIVETESSELLATION] = NINE_STATE_UNHANDLED,
1554 [D3DRS_TWOSIDEDSTENCILMODE] = NINE_STATE_DSA,
1555 [D3DRS_CCW_STENCILFAIL] = NINE_STATE_DSA,
1556 [D3DRS_CCW_STENCILZFAIL] = NINE_STATE_DSA,
1557 [D3DRS_CCW_STENCILPASS] = NINE_STATE_DSA,
1558 [D3DRS_CCW_STENCILFUNC] = NINE_STATE_DSA,
1559 [D3DRS_COLORWRITEENABLE1] = NINE_STATE_BLEND,
1560 [D3DRS_COLORWRITEENABLE2] = NINE_STATE_BLEND,
1561 [D3DRS_COLORWRITEENABLE3] = NINE_STATE_BLEND,
1562 [D3DRS_BLENDFACTOR] = NINE_STATE_BLEND_COLOR,
1563 [D3DRS_SRGBWRITEENABLE] = NINE_STATE_FB,
1564 [D3DRS_DEPTHBIAS] = NINE_STATE_RASTERIZER,
1565 [D3DRS_WRAP8] = NINE_STATE_UNHANDLED, /* cylwrap has to be done via GP */
1566 [D3DRS_WRAP9] = NINE_STATE_UNHANDLED,
1567 [D3DRS_WRAP10] = NINE_STATE_UNHANDLED,
1568 [D3DRS_WRAP11] = NINE_STATE_UNHANDLED,
1569 [D3DRS_WRAP12] = NINE_STATE_UNHANDLED,
1570 [D3DRS_WRAP13] = NINE_STATE_UNHANDLED,
1571 [D3DRS_WRAP14] = NINE_STATE_UNHANDLED,
1572 [D3DRS_WRAP15] = NINE_STATE_UNHANDLED,
1573 [D3DRS_SEPARATEALPHABLENDENABLE] = NINE_STATE_BLEND,
1574 [D3DRS_SRCBLENDALPHA] = NINE_STATE_BLEND,
1575 [D3DRS_DESTBLENDALPHA] = NINE_STATE_BLEND,
1576 [D3DRS_BLENDOPALPHA] = NINE_STATE_BLEND
1577 };
1578
1579 /* Misc */
1580
1581 D3DMATRIX *
1582 nine_state_access_transform(struct nine_state *state, D3DTRANSFORMSTATETYPE t,
1583 boolean alloc)
1584 {
1585 static D3DMATRIX Identity = { .m[0] = { 1, 0, 0, 0 },
1586 .m[1] = { 0, 1, 0, 0 },
1587 .m[2] = { 0, 0, 1, 0 },
1588 .m[3] = { 0, 0, 0, 1 } };
1589 unsigned index;
1590
1591 switch (t) {
1592 case D3DTS_VIEW: index = 0; break;
1593 case D3DTS_PROJECTION: index = 1; break;
1594 case D3DTS_TEXTURE0: index = 2; break;
1595 case D3DTS_TEXTURE1: index = 3; break;
1596 case D3DTS_TEXTURE2: index = 4; break;
1597 case D3DTS_TEXTURE3: index = 5; break;
1598 case D3DTS_TEXTURE4: index = 6; break;
1599 case D3DTS_TEXTURE5: index = 7; break;
1600 case D3DTS_TEXTURE6: index = 8; break;
1601 case D3DTS_TEXTURE7: index = 9; break;
1602 default:
1603 if (!(t >= D3DTS_WORLDMATRIX(0) && t <= D3DTS_WORLDMATRIX(255)))
1604 return NULL;
1605 index = 10 + (t - D3DTS_WORLDMATRIX(0));
1606 break;
1607 }
1608
1609 if (index >= state->ff.num_transforms) {
1610 unsigned N = index + 1;
1611 unsigned n = state->ff.num_transforms;
1612
1613 if (!alloc)
1614 return &Identity;
1615 state->ff.transform = REALLOC(state->ff.transform,
1616 n * sizeof(D3DMATRIX),
1617 N * sizeof(D3DMATRIX));
1618 for (; n < N; ++n)
1619 state->ff.transform[n] = Identity;
1620 state->ff.num_transforms = N;
1621 }
1622 return &state->ff.transform[index];
1623 }
1624
1625 #define D3DRS_TO_STRING_CASE(n) case D3DRS_##n: return "D3DRS_"#n
1626 const char *nine_d3drs_to_string(DWORD State)
1627 {
1628 switch (State) {
1629 D3DRS_TO_STRING_CASE(ZENABLE);
1630 D3DRS_TO_STRING_CASE(FILLMODE);
1631 D3DRS_TO_STRING_CASE(SHADEMODE);
1632 D3DRS_TO_STRING_CASE(ZWRITEENABLE);
1633 D3DRS_TO_STRING_CASE(ALPHATESTENABLE);
1634 D3DRS_TO_STRING_CASE(LASTPIXEL);
1635 D3DRS_TO_STRING_CASE(SRCBLEND);
1636 D3DRS_TO_STRING_CASE(DESTBLEND);
1637 D3DRS_TO_STRING_CASE(CULLMODE);
1638 D3DRS_TO_STRING_CASE(ZFUNC);
1639 D3DRS_TO_STRING_CASE(ALPHAREF);
1640 D3DRS_TO_STRING_CASE(ALPHAFUNC);
1641 D3DRS_TO_STRING_CASE(DITHERENABLE);
1642 D3DRS_TO_STRING_CASE(ALPHABLENDENABLE);
1643 D3DRS_TO_STRING_CASE(FOGENABLE);
1644 D3DRS_TO_STRING_CASE(SPECULARENABLE);
1645 D3DRS_TO_STRING_CASE(FOGCOLOR);
1646 D3DRS_TO_STRING_CASE(FOGTABLEMODE);
1647 D3DRS_TO_STRING_CASE(FOGSTART);
1648 D3DRS_TO_STRING_CASE(FOGEND);
1649 D3DRS_TO_STRING_CASE(FOGDENSITY);
1650 D3DRS_TO_STRING_CASE(RANGEFOGENABLE);
1651 D3DRS_TO_STRING_CASE(STENCILENABLE);
1652 D3DRS_TO_STRING_CASE(STENCILFAIL);
1653 D3DRS_TO_STRING_CASE(STENCILZFAIL);
1654 D3DRS_TO_STRING_CASE(STENCILPASS);
1655 D3DRS_TO_STRING_CASE(STENCILFUNC);
1656 D3DRS_TO_STRING_CASE(STENCILREF);
1657 D3DRS_TO_STRING_CASE(STENCILMASK);
1658 D3DRS_TO_STRING_CASE(STENCILWRITEMASK);
1659 D3DRS_TO_STRING_CASE(TEXTUREFACTOR);
1660 D3DRS_TO_STRING_CASE(WRAP0);
1661 D3DRS_TO_STRING_CASE(WRAP1);
1662 D3DRS_TO_STRING_CASE(WRAP2);
1663 D3DRS_TO_STRING_CASE(WRAP3);
1664 D3DRS_TO_STRING_CASE(WRAP4);
1665 D3DRS_TO_STRING_CASE(WRAP5);
1666 D3DRS_TO_STRING_CASE(WRAP6);
1667 D3DRS_TO_STRING_CASE(WRAP7);
1668 D3DRS_TO_STRING_CASE(CLIPPING);
1669 D3DRS_TO_STRING_CASE(LIGHTING);
1670 D3DRS_TO_STRING_CASE(AMBIENT);
1671 D3DRS_TO_STRING_CASE(FOGVERTEXMODE);
1672 D3DRS_TO_STRING_CASE(COLORVERTEX);
1673 D3DRS_TO_STRING_CASE(LOCALVIEWER);
1674 D3DRS_TO_STRING_CASE(NORMALIZENORMALS);
1675 D3DRS_TO_STRING_CASE(DIFFUSEMATERIALSOURCE);
1676 D3DRS_TO_STRING_CASE(SPECULARMATERIALSOURCE);
1677 D3DRS_TO_STRING_CASE(AMBIENTMATERIALSOURCE);
1678 D3DRS_TO_STRING_CASE(EMISSIVEMATERIALSOURCE);
1679 D3DRS_TO_STRING_CASE(VERTEXBLEND);
1680 D3DRS_TO_STRING_CASE(CLIPPLANEENABLE);
1681 D3DRS_TO_STRING_CASE(POINTSIZE);
1682 D3DRS_TO_STRING_CASE(POINTSIZE_MIN);
1683 D3DRS_TO_STRING_CASE(POINTSPRITEENABLE);
1684 D3DRS_TO_STRING_CASE(POINTSCALEENABLE);
1685 D3DRS_TO_STRING_CASE(POINTSCALE_A);
1686 D3DRS_TO_STRING_CASE(POINTSCALE_B);
1687 D3DRS_TO_STRING_CASE(POINTSCALE_C);
1688 D3DRS_TO_STRING_CASE(MULTISAMPLEANTIALIAS);
1689 D3DRS_TO_STRING_CASE(MULTISAMPLEMASK);
1690 D3DRS_TO_STRING_CASE(PATCHEDGESTYLE);
1691 D3DRS_TO_STRING_CASE(DEBUGMONITORTOKEN);
1692 D3DRS_TO_STRING_CASE(POINTSIZE_MAX);
1693 D3DRS_TO_STRING_CASE(INDEXEDVERTEXBLENDENABLE);
1694 D3DRS_TO_STRING_CASE(COLORWRITEENABLE);
1695 D3DRS_TO_STRING_CASE(TWEENFACTOR);
1696 D3DRS_TO_STRING_CASE(BLENDOP);
1697 D3DRS_TO_STRING_CASE(POSITIONDEGREE);
1698 D3DRS_TO_STRING_CASE(NORMALDEGREE);
1699 D3DRS_TO_STRING_CASE(SCISSORTESTENABLE);
1700 D3DRS_TO_STRING_CASE(SLOPESCALEDEPTHBIAS);
1701 D3DRS_TO_STRING_CASE(ANTIALIASEDLINEENABLE);
1702 D3DRS_TO_STRING_CASE(MINTESSELLATIONLEVEL);
1703 D3DRS_TO_STRING_CASE(MAXTESSELLATIONLEVEL);
1704 D3DRS_TO_STRING_CASE(ADAPTIVETESS_X);
1705 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Y);
1706 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Z);
1707 D3DRS_TO_STRING_CASE(ADAPTIVETESS_W);
1708 D3DRS_TO_STRING_CASE(ENABLEADAPTIVETESSELLATION);
1709 D3DRS_TO_STRING_CASE(TWOSIDEDSTENCILMODE);
1710 D3DRS_TO_STRING_CASE(CCW_STENCILFAIL);
1711 D3DRS_TO_STRING_CASE(CCW_STENCILZFAIL);
1712 D3DRS_TO_STRING_CASE(CCW_STENCILPASS);
1713 D3DRS_TO_STRING_CASE(CCW_STENCILFUNC);
1714 D3DRS_TO_STRING_CASE(COLORWRITEENABLE1);
1715 D3DRS_TO_STRING_CASE(COLORWRITEENABLE2);
1716 D3DRS_TO_STRING_CASE(COLORWRITEENABLE3);
1717 D3DRS_TO_STRING_CASE(BLENDFACTOR);
1718 D3DRS_TO_STRING_CASE(SRGBWRITEENABLE);
1719 D3DRS_TO_STRING_CASE(DEPTHBIAS);
1720 D3DRS_TO_STRING_CASE(WRAP8);
1721 D3DRS_TO_STRING_CASE(WRAP9);
1722 D3DRS_TO_STRING_CASE(WRAP10);
1723 D3DRS_TO_STRING_CASE(WRAP11);
1724 D3DRS_TO_STRING_CASE(WRAP12);
1725 D3DRS_TO_STRING_CASE(WRAP13);
1726 D3DRS_TO_STRING_CASE(WRAP14);
1727 D3DRS_TO_STRING_CASE(WRAP15);
1728 D3DRS_TO_STRING_CASE(SEPARATEALPHABLENDENABLE);
1729 D3DRS_TO_STRING_CASE(SRCBLENDALPHA);
1730 D3DRS_TO_STRING_CASE(DESTBLENDALPHA);
1731 D3DRS_TO_STRING_CASE(BLENDOPALPHA);
1732 default:
1733 return "(invalid)";
1734 }
1735 }