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