2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 * Copyright 2013 Christoph Bumiller
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:
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
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. */
25 #include "basetexture9.h"
26 #include "indexbuffer9.h"
28 #include "vertexdeclaration9.h"
29 #include "vertexshader9.h"
30 #include "pixelshader9.h"
31 #include "nine_pipe.h"
33 #include "pipe/p_context.h"
34 #include "pipe/p_state.h"
35 #include "cso_cache/cso_context.h"
36 #include "util/u_math.h"
38 #define DBG_CHANNEL DBG_DEVICE
41 update_framebuffer(struct NineDevice9
*device
)
43 struct pipe_context
*pipe
= device
->pipe
;
44 struct nine_state
*state
= &device
->state
;
45 struct pipe_framebuffer_state
*fb
= &device
->state
.fb
;
47 struct NineSurface9
*rt0
= state
->rt
[0];
48 unsigned w
= rt0
->desc
.Width
;
49 unsigned h
= rt0
->desc
.Height
;
50 D3DMULTISAMPLE_TYPE nr_samples
= rt0
->desc
.MultiSampleType
;
52 const int sRGB
= state
->rs
[D3DRS_SRGBWRITEENABLE
] ? 1 : 0;
59 /* all render targets must have the same size and the depth buffer must be
60 * bigger. Multisample has to match, according to spec. But some apps do
61 * things wrong there, and no error is returned. The behaviour they get
62 * apparently is that depth buffer is disabled if it doesn't match.
63 * Surely the same for render targets. */
65 /* Special case: D3DFMT_NULL is used to bound no real render target,
66 * but render to depth buffer. We have to not take into account the render
67 * target info. TODO: know what should happen when there are several render targers
68 * and the first one is D3DFMT_NULL */
69 if (rt0
->desc
.Format
== D3DFMT_NULL
&& state
->ds
) {
70 w
= state
->ds
->desc
.Width
;
71 h
= state
->ds
->desc
.Height
;
72 nr_samples
= state
->ds
->desc
.MultiSampleType
;
75 for (i
= 0; i
< device
->caps
.NumSimultaneousRTs
; ++i
) {
76 struct NineSurface9
*rt
= state
->rt
[i
];
78 if (rt
&& rt
->desc
.Format
!= D3DFMT_NULL
&& rt
->desc
.Width
== w
&&
79 rt
->desc
.Height
== h
&& rt
->desc
.MultiSampleType
== nr_samples
) {
80 fb
->cbufs
[i
] = NineSurface9_GetSurface(rt
, sRGB
);
81 state
->rt_mask
|= 1 << i
;
84 if (unlikely(rt
->desc
.Usage
& D3DUSAGE_AUTOGENMIPMAP
)) {
85 assert(rt
->texture
== D3DRTYPE_TEXTURE
||
86 rt
->texture
== D3DRTYPE_CUBETEXTURE
);
87 NineBaseTexture9(rt
->base
.base
.container
)->dirty_mip
= TRUE
;
90 /* Color outputs must match RT slot,
91 * drivers will have to handle NULL entries for GL, too.
97 if (state
->ds
&& state
->ds
->desc
.Width
>= w
&&
98 state
->ds
->desc
.Height
>= h
&&
99 state
->ds
->desc
.MultiSampleType
== nr_samples
) {
100 fb
->zsbuf
= NineSurface9_GetSurface(state
->ds
, 0);
108 pipe
->set_framebuffer_state(pipe
, fb
); /* XXX: cso ? */
112 switch (fb
->zsbuf
->format
) {
113 case PIPE_FORMAT_Z32_FLOAT
:
114 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
117 case PIPE_FORMAT_Z16_UNORM
:
118 scale
= fui((float)(1 << 16));
121 scale
= fui((float)(1 << 24));
124 if (state
->rs
[NINED3DRS_ZBIASSCALE
] != scale
) {
125 state
->rs
[NINED3DRS_ZBIASSCALE
] = scale
;
126 state
->changed
.group
|= NINE_STATE_RASTERIZER
;
131 if (state
->rt_mask
& (state
->ps
? ~state
->ps
->rt_mask
: 0))
132 WARN_ONCE("FIXME: writing undefined values to cbufs 0x%x\n",
133 state
->rt_mask
& ~state
->ps
->rt_mask
);
136 return state
->changed
.group
;
140 update_viewport(struct NineDevice9
*device
)
142 struct pipe_context
*pipe
= device
->pipe
;
143 const D3DVIEWPORT9
*vport
= &device
->state
.viewport
;
144 struct pipe_viewport_state pvport
;
147 * I hope D3D clip coordinates are still
148 * -1 .. +1 for X,Y and
149 * 0 .. +1 for Z (use pipe_rasterizer_state.clip_halfz)
151 pvport
.scale
[0] = (float)vport
->Width
* 0.5f
;
152 pvport
.scale
[1] = (float)vport
->Height
* -0.5f
;
153 pvport
.scale
[2] = vport
->MaxZ
- vport
->MinZ
;
154 pvport
.translate
[0] = (float)vport
->Width
* 0.5f
+ (float)vport
->X
;
155 pvport
.translate
[1] = (float)vport
->Height
* 0.5f
+ (float)vport
->Y
;
156 pvport
.translate
[2] = vport
->MinZ
;
158 pipe
->set_viewport_states(pipe
, 0, 1, &pvport
);
162 update_scissor(struct NineDevice9
*device
)
164 struct pipe_context
*pipe
= device
->pipe
;
166 pipe
->set_scissor_states(pipe
, 0, 1, &device
->state
.scissor
);
170 update_blend(struct NineDevice9
*device
)
172 nine_convert_blend_state(device
->cso
, device
->state
.rs
);
176 update_dsa(struct NineDevice9
*device
)
178 nine_convert_dsa_state(device
->cso
, device
->state
.rs
);
182 update_rasterizer(struct NineDevice9
*device
)
184 nine_convert_rasterizer_state(device
->cso
, device
->state
.rs
);
187 /* Loop through VS inputs and pick the vertex elements with the declared
188 * usage from the vertex declaration, then insert the instance divisor from
189 * the stream source frequency setting.
192 update_vertex_elements(struct NineDevice9
*device
)
194 struct nine_state
*state
= &device
->state
;
195 const struct NineVertexDeclaration9
*vdecl
= device
->state
.vdecl
;
196 const struct NineVertexShader9
*vs
;
199 struct pipe_vertex_element ve
[PIPE_MAX_ATTRIBS
];
201 state
->stream_usage_mask
= 0;
203 vs
= device
->state
.vs
? device
->state
.vs
: device
->ff
.vs
;
205 if (!vdecl
) /* no inputs */
207 for (n
= 0; n
< vs
->num_inputs
; ++n
) {
208 DBG("looking up input %u (usage %u) from vdecl(%p)\n",
209 n
, vs
->input_map
[n
].ndecl
, vdecl
);
212 for (i
= 0; i
< vdecl
->nelems
; i
++) {
213 if (vdecl
->usage_map
[i
] == vs
->input_map
[n
].ndecl
) {
220 ve
[n
] = vdecl
->elems
[index
];
221 b
= ve
[n
].vertex_buffer_index
;
222 state
->stream_usage_mask
|= 1 << b
;
223 /* XXX wine just uses 1 here: */
224 if (state
->stream_freq
[b
] & D3DSTREAMSOURCE_INSTANCEDATA
)
225 ve
[n
].instance_divisor
= state
->stream_freq
[b
] & 0x7FFFFF;
227 /* TODO: msdn doesn't specify what should happen when the vertex
228 * declaration doesn't match the vertex shader inputs.
229 * Some websites say the code will pass but nothing will get rendered.
230 * We should check and implement the correct behaviour. */
231 /* Put PIPE_FORMAT_NONE.
232 * Some drivers (r300) are very unhappy with that */
233 ve
[n
].src_format
= PIPE_FORMAT_NONE
;
234 ve
[n
].src_offset
= 0;
235 ve
[n
].instance_divisor
= 0;
236 ve
[n
].vertex_buffer_index
= 0;
239 cso_set_vertex_elements(device
->cso
, vs
->num_inputs
, ve
);
241 state
->changed
.stream_freq
= 0;
244 static INLINE
uint32_t
245 update_shader_variant_keys(struct NineDevice9
*device
)
247 struct nine_state
*state
= &device
->state
;
249 uint32_t vs_key
= state
->samplers_shadow
;
250 uint32_t ps_key
= state
->samplers_shadow
;
252 vs_key
= (vs_key
& NINE_VS_SAMPLERS_MASK
) >> NINE_SAMPLER_VS(0);
253 ps_key
= (ps_key
& NINE_PS_SAMPLERS_MASK
) >> NINE_SAMPLER_PS(0);
255 if (state
->vs
) vs_key
&= state
->vs
->sampler_mask
;
257 if (unlikely(state
->ps
->byte_code
.version
< 0x20)) {
258 /* no depth textures, but variable targets */
259 uint32_t m
= state
->ps
->sampler_mask
;
264 ps_key
|= (state
->texture
[s
] ? state
->texture
[s
]->pstype
: 1) << (s
* 2);
267 ps_key
&= state
->ps
->sampler_mask
;
271 if (state
->vs
&& state
->vs_key
!= vs_key
) {
272 state
->vs_key
= vs_key
;
273 mask
|= NINE_STATE_VS
;
275 if (state
->ps
&& state
->ps_key
!= ps_key
) {
276 state
->ps_key
= ps_key
;
277 mask
|= NINE_STATE_PS
;
282 static INLINE
uint32_t
283 update_vs(struct NineDevice9
*device
)
285 struct nine_state
*state
= &device
->state
;
286 struct NineVertexShader9
*vs
= state
->vs
;
288 /* likely because we dislike FF */
290 state
->cso
.vs
= NineVertexShader9_GetVariant(vs
, state
->vs_key
);
293 state
->cso
.vs
= vs
->variant
.cso
;
295 device
->pipe
->bind_vs_state(device
->pipe
, state
->cso
.vs
);
297 if (state
->rs
[NINED3DRS_VSPOINTSIZE
] != vs
->point_size
) {
298 state
->rs
[NINED3DRS_VSPOINTSIZE
] = vs
->point_size
;
299 return NINE_STATE_RASTERIZER
;
303 unsigned s
, mask
= vs
->sampler_mask
;
304 for (s
= 0; mask
; ++s
, mask
>>= 1)
305 if ((mask
& 1) && !(device
->state
.texture
[NINE_SAMPLER_VS(s
)]))
306 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
312 static INLINE
uint32_t
313 update_ps(struct NineDevice9
*device
)
315 struct nine_state
*state
= &device
->state
;
316 struct NinePixelShader9
*ps
= state
->ps
;
319 state
->cso
.ps
= NinePixelShader9_GetVariant(ps
, state
->ps_key
);
322 state
->cso
.ps
= ps
->variant
.cso
;
324 device
->pipe
->bind_fs_state(device
->pipe
, state
->cso
.ps
);
328 unsigned s
, mask
= ps
->sampler_mask
;
329 for (s
= 0; mask
; ++s
, mask
>>= 1)
330 if ((mask
& 1) && !(device
->state
.texture
[NINE_SAMPLER_PS(s
)]))
331 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
332 if (device
->state
.rt_mask
& ~ps
->rt_mask
)
333 WARN_ONCE("FIXME: writing undefined values to cbufs 0x%x\n",
334 device
->state
.rt_mask
& ~ps
->rt_mask
);
340 #define DO_UPLOAD_CONST_F(buf,p,c,d) \
342 DBG("upload ConstantF [%u .. %u]\n", x, (x) + (c) - 1); \
343 box.x = (p) * 4 * sizeof(float); \
344 box.width = (c) * 4 * sizeof(float); \
345 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, &((d)[p * 4]), \
349 /* OK, this is a bit ugly ... */
351 update_constants(struct NineDevice9
*device
, unsigned shader_type
)
353 struct pipe_context
*pipe
= device
->pipe
;
354 struct pipe_resource
*buf
;
357 const float *const_f
;
360 uint32_t data_b
[NINE_MAX_CONST_B
];
363 const unsigned usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
364 unsigned x
= 0; /* silence warning */
366 struct nine_range
*r
, *p
, *lconstf_ranges
;
374 if (shader_type
== PIPE_SHADER_VERTEX
) {
376 buf
= device
->constbuf_vs
;
378 const_f
= device
->state
.vs_const_f
;
379 for (p
= r
= device
->state
.changed
.vs_const_f
; r
; p
= r
, r
= r
->next
)
380 DO_UPLOAD_CONST_F(buf
, r
->bgn
, r
->end
- r
->bgn
, const_f
);
382 nine_range_pool_put_chain(&device
->range_pool
,
383 device
->state
.changed
.vs_const_f
, p
);
384 device
->state
.changed
.vs_const_f
= NULL
;
387 dirty_i
= device
->state
.changed
.vs_const_i
;
388 device
->state
.changed
.vs_const_i
= 0;
389 const_i
= &device
->state
.vs_const_i
[0][0];
391 dirty_b
= device
->state
.changed
.vs_const_b
;
392 device
->state
.changed
.vs_const_b
= 0;
393 const_b
= device
->state
.vs_const_b
;
395 lconstf_ranges
= device
->state
.vs
->lconstf
.ranges
;
396 lconstf_data
= device
->state
.vs
->lconstf
.data
;
398 device
->state
.ff
.clobber
.vs_const
= TRUE
;
399 device
->state
.changed
.group
&= ~NINE_STATE_VS_CONST
;
402 buf
= device
->constbuf_ps
;
404 const_f
= device
->state
.ps_const_f
;
405 for (p
= r
= device
->state
.changed
.ps_const_f
; r
; p
= r
, r
= r
->next
)
406 DO_UPLOAD_CONST_F(buf
, r
->bgn
, r
->end
- r
->bgn
, const_f
);
408 nine_range_pool_put_chain(&device
->range_pool
,
409 device
->state
.changed
.ps_const_f
, p
);
410 device
->state
.changed
.ps_const_f
= NULL
;
413 dirty_i
= device
->state
.changed
.ps_const_i
;
414 device
->state
.changed
.ps_const_i
= 0;
415 const_i
= &device
->state
.ps_const_i
[0][0];
417 dirty_b
= device
->state
.changed
.ps_const_b
;
418 device
->state
.changed
.ps_const_b
= 0;
419 const_b
= device
->state
.ps_const_b
;
421 lconstf_ranges
= NULL
;
424 device
->state
.ff
.clobber
.ps_const
= TRUE
;
425 device
->state
.changed
.group
&= ~NINE_STATE_PS_CONST
;
428 /* write range from min to max changed, it's not much data */
431 c
= util_last_bit(dirty_b
);
432 i
= ffs(dirty_b
) - 1;
433 x
= buf
->width0
- (NINE_MAX_CONST_B
- i
) * 4;
435 memcpy(data_b
, &(const_b
[i
]), c
* sizeof(uint32_t));
438 DBG("upload ConstantB [%u .. %u]\n", x
, x
+ c
- 1);
439 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data_b
, 0, 0);
443 for (c
= 0, i
= 0; dirty_i
; i
++, dirty_i
>>= 1) {
450 DBG("upload ConstantI [%u .. %u]\n", x
, x
+ c
- 1);
451 data
= &const_i
[x
* 4];
452 box
.x
= buf
->width0
- (NINE_MAX_CONST_I
* 4 + NINE_MAX_CONST_B
) * 4;
453 box
.x
+= x
* 4 * sizeof(int);
454 box
.width
= c
* 4 * sizeof(int);
456 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
460 DBG("upload ConstantI [%u .. %u]\n", x
, x
+ c
- 1);
461 data
= &const_i
[x
* 4];
462 box
.x
= buf
->width0
- (NINE_MAX_CONST_I
* 4 + NINE_MAX_CONST_B
) * 4;
463 box
.x
+= x
* 4 * sizeof(int);
464 box
.width
= c
* 4 * sizeof(int);
465 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
468 /* TODO: only upload these when shader itself changes */
469 if (lconstf_ranges
) {
471 struct nine_range
*r
= lconstf_ranges
;
473 box
.x
= r
->bgn
* 4 * sizeof(float);
474 n
+= r
->end
- r
->bgn
;
475 box
.width
= (r
->end
- r
->bgn
) * 4 * sizeof(float);
476 data
= &lconstf_data
[4 * n
];
477 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
484 update_vs_constants_userbuf(struct NineDevice9
*device
)
486 struct nine_state
*state
= &device
->state
;
487 struct pipe_context
*pipe
= device
->pipe
;
488 struct pipe_constant_buffer cb
;
490 cb
.buffer_offset
= 0;
491 cb
.buffer_size
= device
->state
.vs
->const_used_size
;
492 cb
.user_buffer
= device
->state
.vs_const_f
;
497 if (state
->changed
.vs_const_i
) {
498 int *idst
= (int *)&state
->vs_const_f
[4 * device
->max_vs_const_f
];
499 memcpy(idst
, state
->vs_const_i
, sizeof(state
->vs_const_i
));
500 state
->changed
.vs_const_i
= 0;
502 if (state
->changed
.vs_const_b
) {
503 int *idst
= (int *)&state
->vs_const_f
[4 * device
->max_vs_const_f
];
504 uint32_t *bdst
= (uint32_t *)&idst
[4 * NINE_MAX_CONST_I
];
505 memcpy(bdst
, state
->vs_const_b
, sizeof(state
->vs_const_b
));
506 state
->changed
.vs_const_b
= 0;
509 if (device
->state
.vs
->lconstf
.ranges
) {
510 /* TODO: Can we make it so that we don't have to copy everything ? */
511 const struct nine_lconstf
*lconstf
= &device
->state
.vs
->lconstf
;
512 const struct nine_range
*r
= lconstf
->ranges
;
514 float *dst
= device
->state
.vs_lconstf_temp
;
515 float *src
= (float *)cb
.user_buffer
;
516 memcpy(dst
, src
, cb
.buffer_size
);
519 unsigned c
= r
->end
- r
->bgn
;
520 memcpy(&dst
[p
* 4], &lconstf
->data
[n
* 4], c
* 4 * sizeof(float));
524 cb
.user_buffer
= dst
;
527 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_VERTEX
, 0, &cb
);
529 if (device
->state
.changed
.vs_const_f
) {
530 struct nine_range
*r
= device
->state
.changed
.vs_const_f
;
531 struct nine_range
*p
= r
;
534 nine_range_pool_put_chain(&device
->range_pool
, r
, p
);
535 device
->state
.changed
.vs_const_f
= NULL
;
537 state
->changed
.group
&= ~NINE_STATE_VS_CONST
;
541 update_ps_constants_userbuf(struct NineDevice9
*device
)
543 struct nine_state
*state
= &device
->state
;
544 struct pipe_context
*pipe
= device
->pipe
;
545 struct pipe_constant_buffer cb
;
547 cb
.buffer_offset
= 0;
548 cb
.buffer_size
= device
->state
.ps
->const_used_size
;
549 cb
.user_buffer
= device
->state
.ps_const_f
;
554 if (state
->changed
.ps_const_i
) {
555 int *idst
= (int *)&state
->ps_const_f
[4 * device
->max_ps_const_f
];
556 memcpy(idst
, state
->ps_const_i
, sizeof(state
->ps_const_i
));
557 state
->changed
.ps_const_i
= 0;
559 if (state
->changed
.ps_const_b
) {
560 int *idst
= (int *)&state
->ps_const_f
[4 * device
->max_ps_const_f
];
561 uint32_t *bdst
= (uint32_t *)&idst
[4 * NINE_MAX_CONST_I
];
562 memcpy(bdst
, state
->ps_const_b
, sizeof(state
->ps_const_b
));
563 state
->changed
.ps_const_b
= 0;
566 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
568 if (device
->state
.changed
.ps_const_f
) {
569 struct nine_range
*r
= device
->state
.changed
.ps_const_f
;
570 struct nine_range
*p
= r
;
573 nine_range_pool_put_chain(&device
->range_pool
, r
, p
);
574 device
->state
.changed
.ps_const_f
= NULL
;
576 state
->changed
.group
&= ~NINE_STATE_PS_CONST
;
580 update_vertex_buffers(struct NineDevice9
*device
)
582 struct pipe_context
*pipe
= device
->pipe
;
583 struct nine_state
*state
= &device
->state
;
584 uint32_t mask
= state
->changed
.vtxbuf
;
589 DBG("mask=%x\n", mask
);
591 for (i
= 0; mask
; mask
>>= 1, ++i
) {
598 pipe
->set_vertex_buffers(pipe
,
599 start
, count
, &state
->vtxbuf
[start
]);
604 pipe
->set_vertex_buffers(pipe
, start
, count
, &state
->vtxbuf
[start
]);
606 state
->changed
.vtxbuf
= 0;
610 update_index_buffer(struct NineDevice9
*device
)
612 struct pipe_context
*pipe
= device
->pipe
;
613 if (device
->state
.idxbuf
)
614 pipe
->set_index_buffer(pipe
, &device
->state
.idxbuf
->buffer
);
616 pipe
->set_index_buffer(pipe
, NULL
);
619 /* TODO: only go through dirty textures */
621 validate_textures(struct NineDevice9
*device
)
623 struct NineBaseTexture9
*tex
, *ptr
;
624 LIST_FOR_EACH_ENTRY_SAFE(tex
, ptr
, &device
->update_textures
, list
) {
625 list_delinit(&tex
->list
);
626 NineBaseTexture9_Validate(tex
);
630 static INLINE boolean
631 update_sampler_derived(struct nine_state
*state
, unsigned s
)
633 boolean changed
= FALSE
;
635 if (state
->samp
[s
][NINED3DSAMP_SHADOW
] != state
->texture
[s
]->shadow
) {
637 state
->samp
[s
][NINED3DSAMP_SHADOW
] = state
->texture
[s
]->shadow
;
640 if (state
->samp
[s
][D3DSAMP_MIPFILTER
] != D3DTEXF_NONE
) {
641 int lod
= state
->samp
[s
][D3DSAMP_MAXMIPLEVEL
] - state
->texture
[s
]->lod
;
644 if (state
->samp
[s
][NINED3DSAMP_MINLOD
] != lod
) {
646 state
->samp
[s
][NINED3DSAMP_MINLOD
] = lod
;
649 state
->changed
.sampler
[s
] &= ~0x300; /* lod changes irrelevant */
655 /* TODO: add sRGB override to pipe_sampler_state ? */
657 update_textures_and_samplers(struct NineDevice9
*device
)
659 struct pipe_context
*pipe
= device
->pipe
;
660 struct nine_state
*state
= &device
->state
;
661 struct pipe_sampler_view
*view
[NINE_MAX_SAMPLERS
];
662 unsigned num_textures
;
664 boolean commit_samplers
;
666 /* TODO: Can we reduce iterations here ? */
668 commit_samplers
= FALSE
;
669 for (num_textures
= 0, i
= 0; i
< NINE_MAX_SAMPLERS_PS
; ++i
) {
670 const unsigned s
= NINE_SAMPLER_PS(i
);
672 if (!state
->texture
[s
]) {
675 if (state
->ps
&& state
->ps
->sampler_mask
& (1 << i
))
676 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
680 sRGB
= state
->samp
[s
][D3DSAMP_SRGBTEXTURE
] ? 1 : 0;
682 view
[i
] = NineBaseTexture9_GetSamplerView(state
->texture
[s
], sRGB
);
683 num_textures
= i
+ 1;
685 if (update_sampler_derived(state
, s
) || (state
->changed
.sampler
[s
] & 0x05fe)) {
686 state
->changed
.sampler
[s
] = 0;
687 commit_samplers
= TRUE
;
688 nine_convert_sampler_state(device
->cso
, s
, state
->samp
[s
]);
691 if (state
->changed
.texture
& NINE_PS_SAMPLERS_MASK
|| state
->changed
.srgb
)
692 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
696 cso_single_sampler_done(device
->cso
, PIPE_SHADER_FRAGMENT
);
698 commit_samplers
= FALSE
;
699 for (num_textures
= 0, i
= 0; i
< NINE_MAX_SAMPLERS_VS
; ++i
) {
700 const unsigned s
= NINE_SAMPLER_VS(i
);
702 if (!state
->texture
[s
]) {
705 if (state
->vs
&& state
->vs
->sampler_mask
& (1 << i
))
706 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
710 sRGB
= state
->samp
[s
][D3DSAMP_SRGBTEXTURE
] ? 1 : 0;
712 view
[i
] = NineBaseTexture9_GetSamplerView(state
->texture
[s
], sRGB
);
713 num_textures
= i
+ 1;
715 if (update_sampler_derived(state
, s
) || (state
->changed
.sampler
[s
] & 0x05fe)) {
716 state
->changed
.sampler
[s
] = 0;
717 commit_samplers
= TRUE
;
718 nine_convert_sampler_state(device
->cso
, s
, state
->samp
[s
]);
721 if (state
->changed
.texture
& NINE_VS_SAMPLERS_MASK
|| state
->changed
.srgb
)
722 pipe
->set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, 0,
726 cso_single_sampler_done(device
->cso
, PIPE_SHADER_VERTEX
);
728 state
->changed
.srgb
= FALSE
;
729 state
->changed
.texture
= 0;
733 #define NINE_STATE_FREQ_GROUP_0 \
735 NINE_STATE_VIEWPORT | \
736 NINE_STATE_SCISSOR | \
739 NINE_STATE_RASTERIZER | \
742 NINE_STATE_BLEND_COLOR | \
743 NINE_STATE_STENCIL_REF | \
744 NINE_STATE_SAMPLE_MASK)
746 #define NINE_STATE_FREQ_GROUP_1 ~NINE_STATE_FREQ_GROUP_0
748 #define NINE_STATE_SHADER_VARIANT_GROUP \
749 (NINE_STATE_TEXTURE | \
754 nine_update_state(struct NineDevice9
*device
, uint32_t mask
)
756 struct pipe_context
*pipe
= device
->pipe
;
757 struct nine_state
*state
= &device
->state
;
760 DBG("changed state groups: %x | %x\n",
761 state
->changed
.group
& NINE_STATE_FREQ_GROUP_0
,
762 state
->changed
.group
& NINE_STATE_FREQ_GROUP_1
);
764 /* NOTE: We may want to use the cso cache for everything, or let
765 * NineDevice9.RestoreNonCSOState actually set the states, then we wouldn't
766 * have to care about state being clobbered here and could merge this back
767 * into update_textures. Except, we also need to re-validate textures that
768 * may be dirty anyway, even if no texture bindings changed.
770 validate_textures(device
); /* may clobber state */
772 /* ff_update may change VS/PS dirty bits */
773 if ((mask
& NINE_STATE_FF
) && unlikely(!state
->vs
|| !state
->ps
))
774 nine_ff_update(device
);
775 group
= state
->changed
.group
& mask
;
777 if (group
& NINE_STATE_SHADER_VARIANT_GROUP
)
778 group
|= update_shader_variant_keys(device
);
780 if (group
& NINE_STATE_FREQ_GROUP_0
) {
781 if (group
& NINE_STATE_FB
)
782 group
= update_framebuffer(device
) & mask
;
783 if (group
& NINE_STATE_VIEWPORT
)
784 update_viewport(device
);
785 if (group
& NINE_STATE_SCISSOR
)
786 update_scissor(device
);
788 if (group
& NINE_STATE_DSA
)
790 if (group
& NINE_STATE_BLEND
)
791 update_blend(device
);
793 if (group
& NINE_STATE_VS
)
794 group
|= update_vs(device
);
796 if (group
& NINE_STATE_RASTERIZER
)
797 update_rasterizer(device
);
799 if (group
& NINE_STATE_PS
)
800 group
|= update_ps(device
);
802 if (group
& NINE_STATE_BLEND_COLOR
) {
803 struct pipe_blend_color color
;
804 d3dcolor_to_rgba(&color
.color
[0], state
->rs
[D3DRS_BLENDFACTOR
]);
805 pipe
->set_blend_color(pipe
, &color
);
807 if (group
& NINE_STATE_SAMPLE_MASK
) {
808 pipe
->set_sample_mask(pipe
, state
->rs
[D3DRS_MULTISAMPLEMASK
]);
810 if (group
& NINE_STATE_STENCIL_REF
) {
811 struct pipe_stencil_ref ref
;
812 ref
.ref_value
[0] = state
->rs
[D3DRS_STENCILREF
];
813 ref
.ref_value
[1] = ref
.ref_value
[0];
814 pipe
->set_stencil_ref(pipe
, &ref
);
818 if (state
->changed
.ucp
) {
819 pipe
->set_clip_state(pipe
, &state
->clip
);
820 state
->changed
.ucp
= 0;
823 if (group
& (NINE_STATE_FREQ_GROUP_1
| NINE_STATE_VS
)) {
824 if (group
& (NINE_STATE_TEXTURE
| NINE_STATE_SAMPLER
))
825 update_textures_and_samplers(device
);
827 if (group
& NINE_STATE_IDXBUF
)
828 update_index_buffer(device
);
830 if ((group
& (NINE_STATE_VDECL
| NINE_STATE_VS
)) ||
831 state
->changed
.stream_freq
& ~1)
832 update_vertex_elements(device
);
834 if (device
->prefer_user_constbuf
) {
835 if ((group
& (NINE_STATE_VS_CONST
| NINE_STATE_VS
)) && state
->vs
)
836 update_vs_constants_userbuf(device
);
837 if ((group
& (NINE_STATE_PS_CONST
| NINE_STATE_PS
)) && state
->ps
)
838 update_ps_constants_userbuf(device
);
840 if ((group
& NINE_STATE_VS_CONST
) && state
->vs
)
841 update_constants(device
, PIPE_SHADER_VERTEX
);
842 if ((group
& NINE_STATE_PS_CONST
) && state
->ps
)
843 update_constants(device
, PIPE_SHADER_FRAGMENT
);
846 if (state
->changed
.vtxbuf
)
847 update_vertex_buffers(device
);
849 device
->state
.changed
.group
&= ~mask
|
850 (NINE_STATE_FF
| NINE_STATE_VS_CONST
| NINE_STATE_PS_CONST
);
858 static const DWORD nine_render_state_defaults
[NINED3DRS_LAST
+ 1] =
860 /* [D3DRS_ZENABLE] = D3DZB_TRUE; wine: auto_depth_stencil */
861 [D3DRS_ZENABLE
] = D3DZB_FALSE
,
862 [D3DRS_FILLMODE
] = D3DFILL_SOLID
,
863 [D3DRS_SHADEMODE
] = D3DSHADE_GOURAUD
,
864 /* [D3DRS_LINEPATTERN] = 0x00000000, */
865 [D3DRS_ZWRITEENABLE
] = TRUE
,
866 [D3DRS_ALPHATESTENABLE
] = FALSE
,
867 [D3DRS_LASTPIXEL
] = TRUE
,
868 [D3DRS_SRCBLEND
] = D3DBLEND_ONE
,
869 [D3DRS_DESTBLEND
] = D3DBLEND_ZERO
,
870 [D3DRS_CULLMODE
] = D3DCULL_CCW
,
871 [D3DRS_ZFUNC
] = D3DCMP_LESSEQUAL
,
872 [D3DRS_ALPHAFUNC
] = D3DCMP_ALWAYS
,
873 [D3DRS_ALPHAREF
] = 0,
874 [D3DRS_DITHERENABLE
] = FALSE
,
875 [D3DRS_ALPHABLENDENABLE
] = FALSE
,
876 [D3DRS_FOGENABLE
] = FALSE
,
877 [D3DRS_SPECULARENABLE
] = FALSE
,
878 /* [D3DRS_ZVISIBLE] = 0, */
879 [D3DRS_FOGCOLOR
] = 0,
880 [D3DRS_FOGTABLEMODE
] = D3DFOG_NONE
,
881 [D3DRS_FOGSTART
] = 0x00000000,
882 [D3DRS_FOGEND
] = 0x3F800000,
883 [D3DRS_FOGDENSITY
] = 0x3F800000,
884 /* [D3DRS_EDGEANTIALIAS] = FALSE, */
885 [D3DRS_RANGEFOGENABLE
] = FALSE
,
886 [D3DRS_STENCILENABLE
] = FALSE
,
887 [D3DRS_STENCILFAIL
] = D3DSTENCILOP_KEEP
,
888 [D3DRS_STENCILZFAIL
] = D3DSTENCILOP_KEEP
,
889 [D3DRS_STENCILPASS
] = D3DSTENCILOP_KEEP
,
890 [D3DRS_STENCILREF
] = 0,
891 [D3DRS_STENCILMASK
] = 0xFFFFFFFF,
892 [D3DRS_STENCILFUNC
] = D3DCMP_ALWAYS
,
893 [D3DRS_STENCILWRITEMASK
] = 0xFFFFFFFF,
894 [D3DRS_TEXTUREFACTOR
] = 0xFFFFFFFF,
903 [D3DRS_CLIPPING
] = TRUE
,
904 [D3DRS_LIGHTING
] = TRUE
,
906 [D3DRS_FOGVERTEXMODE
] = D3DFOG_NONE
,
907 [D3DRS_COLORVERTEX
] = TRUE
,
908 [D3DRS_LOCALVIEWER
] = TRUE
,
909 [D3DRS_NORMALIZENORMALS
] = FALSE
,
910 [D3DRS_DIFFUSEMATERIALSOURCE
] = D3DMCS_COLOR1
,
911 [D3DRS_SPECULARMATERIALSOURCE
] = D3DMCS_COLOR2
,
912 [D3DRS_AMBIENTMATERIALSOURCE
] = D3DMCS_MATERIAL
,
913 [D3DRS_EMISSIVEMATERIALSOURCE
] = D3DMCS_MATERIAL
,
914 [D3DRS_VERTEXBLEND
] = D3DVBF_DISABLE
,
915 [D3DRS_CLIPPLANEENABLE
] = 0,
916 /* [D3DRS_SOFTWAREVERTEXPROCESSING] = FALSE, */
917 [D3DRS_POINTSIZE
] = 0x3F800000,
918 [D3DRS_POINTSIZE_MIN
] = 0x3F800000,
919 [D3DRS_POINTSPRITEENABLE
] = FALSE
,
920 [D3DRS_POINTSCALEENABLE
] = FALSE
,
921 [D3DRS_POINTSCALE_A
] = 0x3F800000,
922 [D3DRS_POINTSCALE_B
] = 0x00000000,
923 [D3DRS_POINTSCALE_C
] = 0x00000000,
924 [D3DRS_MULTISAMPLEANTIALIAS
] = TRUE
,
925 [D3DRS_MULTISAMPLEMASK
] = 0xFFFFFFFF,
926 [D3DRS_PATCHEDGESTYLE
] = D3DPATCHEDGE_DISCRETE
,
927 /* [D3DRS_PATCHSEGMENTS] = 0x3F800000, */
928 [D3DRS_DEBUGMONITORTOKEN
] = 0xDEADCAFE,
929 [D3DRS_POINTSIZE_MAX
] = 0x3F800000, /* depends on cap */
930 [D3DRS_INDEXEDVERTEXBLENDENABLE
] = FALSE
,
931 [D3DRS_COLORWRITEENABLE
] = 0x0000000f,
932 [D3DRS_TWEENFACTOR
] = 0x00000000,
933 [D3DRS_BLENDOP
] = D3DBLENDOP_ADD
,
934 [D3DRS_POSITIONDEGREE
] = D3DDEGREE_CUBIC
,
935 [D3DRS_NORMALDEGREE
] = D3DDEGREE_LINEAR
,
936 [D3DRS_SCISSORTESTENABLE
] = FALSE
,
937 [D3DRS_SLOPESCALEDEPTHBIAS
] = 0,
938 [D3DRS_MINTESSELLATIONLEVEL
] = 0x3F800000,
939 [D3DRS_MAXTESSELLATIONLEVEL
] = 0x3F800000,
940 [D3DRS_ANTIALIASEDLINEENABLE
] = FALSE
,
941 [D3DRS_ADAPTIVETESS_X
] = 0x00000000,
942 [D3DRS_ADAPTIVETESS_Y
] = 0x00000000,
943 [D3DRS_ADAPTIVETESS_Z
] = 0x3F800000,
944 [D3DRS_ADAPTIVETESS_W
] = 0x00000000,
945 [D3DRS_ENABLEADAPTIVETESSELLATION
] = FALSE
,
946 [D3DRS_TWOSIDEDSTENCILMODE
] = FALSE
,
947 [D3DRS_CCW_STENCILFAIL
] = D3DSTENCILOP_KEEP
,
948 [D3DRS_CCW_STENCILZFAIL
] = D3DSTENCILOP_KEEP
,
949 [D3DRS_CCW_STENCILPASS
] = D3DSTENCILOP_KEEP
,
950 [D3DRS_CCW_STENCILFUNC
] = D3DCMP_ALWAYS
,
951 [D3DRS_COLORWRITEENABLE1
] = 0x0000000F,
952 [D3DRS_COLORWRITEENABLE2
] = 0x0000000F,
953 [D3DRS_COLORWRITEENABLE3
] = 0x0000000F,
954 [D3DRS_BLENDFACTOR
] = 0xFFFFFFFF,
955 [D3DRS_SRGBWRITEENABLE
] = 0,
956 [D3DRS_DEPTHBIAS
] = 0,
965 [D3DRS_SEPARATEALPHABLENDENABLE
] = FALSE
,
966 [D3DRS_SRCBLENDALPHA
] = D3DBLEND_ONE
,
967 [D3DRS_DESTBLENDALPHA
] = D3DBLEND_ZERO
,
968 [D3DRS_BLENDOPALPHA
] = D3DBLENDOP_ADD
,
969 [NINED3DRS_VSPOINTSIZE
] = FALSE
,
970 [NINED3DRS_RTMASK
] = 0xf,
971 [NINED3DRS_ALPHACOVERAGE
] = FALSE
973 static const DWORD nine_tex_stage_state_defaults
[NINED3DTSS_LAST
+ 1] =
975 [D3DTSS_COLOROP
] = D3DTOP_DISABLE
,
976 [D3DTSS_ALPHAOP
] = D3DTOP_DISABLE
,
977 [D3DTSS_COLORARG1
] = D3DTA_TEXTURE
,
978 [D3DTSS_COLORARG2
] = D3DTA_CURRENT
,
979 [D3DTSS_COLORARG0
] = D3DTA_CURRENT
,
980 [D3DTSS_ALPHAARG1
] = D3DTA_TEXTURE
,
981 [D3DTSS_ALPHAARG2
] = D3DTA_CURRENT
,
982 [D3DTSS_ALPHAARG0
] = D3DTA_CURRENT
,
983 [D3DTSS_RESULTARG
] = D3DTA_CURRENT
,
984 [D3DTSS_BUMPENVMAT00
] = 0,
985 [D3DTSS_BUMPENVMAT01
] = 0,
986 [D3DTSS_BUMPENVMAT10
] = 0,
987 [D3DTSS_BUMPENVMAT11
] = 0,
988 [D3DTSS_BUMPENVLSCALE
] = 0,
989 [D3DTSS_BUMPENVLOFFSET
] = 0,
990 [D3DTSS_TEXCOORDINDEX
] = 0,
991 [D3DTSS_TEXTURETRANSFORMFLAGS
] = D3DTTFF_DISABLE
,
993 static const DWORD nine_samp_state_defaults
[NINED3DSAMP_LAST
+ 1] =
995 [D3DSAMP_ADDRESSU
] = D3DTADDRESS_WRAP
,
996 [D3DSAMP_ADDRESSV
] = D3DTADDRESS_WRAP
,
997 [D3DSAMP_ADDRESSW
] = D3DTADDRESS_WRAP
,
998 [D3DSAMP_BORDERCOLOR
] = 0,
999 [D3DSAMP_MAGFILTER
] = D3DTEXF_POINT
,
1000 [D3DSAMP_MINFILTER
] = D3DTEXF_POINT
,
1001 [D3DSAMP_MIPFILTER
] = D3DTEXF_NONE
,
1002 [D3DSAMP_MIPMAPLODBIAS
] = 0,
1003 [D3DSAMP_MAXMIPLEVEL
] = 0,
1004 [D3DSAMP_MAXANISOTROPY
] = 1,
1005 [D3DSAMP_SRGBTEXTURE
] = 0,
1006 [D3DSAMP_ELEMENTINDEX
] = 0,
1007 [D3DSAMP_DMAPOFFSET
] = 0,
1008 [NINED3DSAMP_MINLOD
] = 0,
1009 [NINED3DSAMP_SHADOW
] = 0
1012 nine_state_set_defaults(struct NineDevice9
*device
, const D3DCAPS9
*caps
,
1015 struct nine_state
*state
= &device
->state
;
1018 /* Initialize defaults.
1020 memcpy(state
->rs
, nine_render_state_defaults
, sizeof(state
->rs
));
1022 for (s
= 0; s
< Elements(state
->ff
.tex_stage
); ++s
) {
1023 memcpy(&state
->ff
.tex_stage
[s
], nine_tex_stage_state_defaults
,
1024 sizeof(state
->ff
.tex_stage
[s
]));
1025 state
->ff
.tex_stage
[s
][D3DTSS_TEXCOORDINDEX
] = s
;
1027 state
->ff
.tex_stage
[0][D3DTSS_COLOROP
] = D3DTOP_MODULATE
;
1028 state
->ff
.tex_stage
[0][D3DTSS_ALPHAOP
] = D3DTOP_SELECTARG1
;
1030 for (s
= 0; s
< Elements(state
->samp
); ++s
) {
1031 memcpy(&state
->samp
[s
], nine_samp_state_defaults
,
1032 sizeof(state
->samp
[s
]));
1035 if (state
->vs_const_f
)
1036 memset(state
->vs_const_f
, 0, device
->vs_const_size
);
1037 if (state
->ps_const_f
)
1038 memset(state
->ps_const_f
, 0, device
->ps_const_size
);
1040 /* Cap dependent initial state:
1042 state
->rs
[D3DRS_POINTSIZE_MAX
] = fui(caps
->MaxPointSize
);
1044 /* Set changed flags to initialize driver.
1046 state
->changed
.group
= NINE_STATE_ALL
;
1048 state
->ff
.changed
.transform
[0] = ~0;
1049 state
->ff
.changed
.transform
[D3DTS_WORLD
/ 32] |= 1 << (D3DTS_WORLD
% 32);
1052 state
->viewport
.MinZ
= 0.0f
;
1053 state
->viewport
.MaxZ
= 1.0f
;
1056 for (s
= 0; s
< Elements(state
->changed
.sampler
); ++s
)
1057 state
->changed
.sampler
[s
] = ~0;
1061 nine_state_clear(struct nine_state
*state
, const boolean device
)
1065 for (i
= 0; i
< Elements(state
->rt
); ++i
)
1066 nine_bind(&state
->rt
[i
], NULL
);
1067 nine_bind(&state
->ds
, NULL
);
1068 nine_bind(&state
->vs
, NULL
);
1069 nine_bind(&state
->ps
, NULL
);
1070 nine_bind(&state
->vdecl
, NULL
);
1071 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; ++i
)
1072 nine_bind(&state
->stream
[i
], NULL
);
1073 nine_bind(&state
->idxbuf
, NULL
);
1074 for (i
= 0; i
< NINE_MAX_SAMPLERS
; ++i
) {
1076 state
->texture
[i
] &&
1077 --state
->texture
[i
]->bind_count
== 0)
1078 list_delinit(&state
->texture
[i
]->list
);
1079 nine_bind(&state
->texture
[i
], NULL
);
1084 static const DWORD nine_render_states_pixel[] =
1086 D3DRS_ALPHABLENDENABLE,
1089 D3DRS_ALPHATESTENABLE,
1090 D3DRS_ANTIALIASEDLINEENABLE,
1094 D3DRS_CCW_STENCILFAIL,
1095 D3DRS_CCW_STENCILPASS,
1096 D3DRS_CCW_STENCILZFAIL,
1097 D3DRS_COLORWRITEENABLE,
1098 D3DRS_COLORWRITEENABLE1,
1099 D3DRS_COLORWRITEENABLE2,
1100 D3DRS_COLORWRITEENABLE3,
1103 D3DRS_DESTBLENDALPHA,
1110 D3DRS_SCISSORTESTENABLE,
1111 D3DRS_SEPARATEALPHABLENDENABLE,
1113 D3DRS_SLOPESCALEDEPTHBIAS,
1115 D3DRS_SRCBLENDALPHA,
1116 D3DRS_SRGBWRITEENABLE,
1117 D3DRS_STENCILENABLE,
1123 D3DRS_STENCILWRITEMASK,
1125 D3DRS_TEXTUREFACTOR,
1126 D3DRS_TWOSIDEDSTENCILMODE,
1148 const uint32_t nine_render_states_pixel
[(NINED3DRS_LAST
+ 31) / 32] =
1150 0x0f99c380, 0x1ff00070, 0x00000000, 0x00000000,
1151 0x000000ff, 0xde01c900, 0x0003ffcf
1155 static const DWORD nine_render_states_vertex[] =
1157 D3DRS_ADAPTIVETESS_W,
1158 D3DRS_ADAPTIVETESS_X,
1159 D3DRS_ADAPTIVETESS_Y,
1160 D3DRS_ADAPTIVETESS_Z,
1162 D3DRS_AMBIENTMATERIALSOURCE,
1164 D3DRS_CLIPPLANEENABLE,
1167 D3DRS_DIFFUSEMATERIALSOURCE,
1168 D3DRS_EMISSIVEMATERIALSOURCE,
1169 D3DRS_ENABLEADAPTIVETESSELLATION,
1176 D3DRS_FOGVERTEXMODE,
1177 D3DRS_INDEXEDVERTEXBLENDENABLE,
1180 D3DRS_MAXTESSELLATIONLEVEL,
1181 D3DRS_MINTESSELLATIONLEVEL,
1182 D3DRS_MULTISAMPLEANTIALIAS,
1183 D3DRS_MULTISAMPLEMASK,
1185 D3DRS_NORMALIZENORMALS,
1186 D3DRS_PATCHEDGESTYLE,
1190 D3DRS_POINTSCALEENABLE,
1192 D3DRS_POINTSIZE_MAX,
1193 D3DRS_POINTSIZE_MIN,
1194 D3DRS_POINTSPRITEENABLE,
1195 D3DRS_POSITIONDEGREE,
1196 D3DRS_RANGEFOGENABLE,
1198 D3DRS_SPECULARENABLE,
1199 D3DRS_SPECULARMATERIALSOURCE,
1204 const uint32_t nine_render_states_vertex
[(NINED3DRS_LAST
+ 31) / 32] =
1206 0x30400200, 0x0001007c, 0x00000000, 0x00000000,
1207 0xfd9efb00, 0x01fc34cf, 0x00000000
1210 /* TODO: put in the right values */
1211 const uint32_t nine_render_state_group
[NINED3DRS_LAST
+ 1] =
1213 [D3DRS_ZENABLE
] = NINE_STATE_DSA
,
1214 [D3DRS_FILLMODE
] = NINE_STATE_RASTERIZER
,
1215 [D3DRS_SHADEMODE
] = NINE_STATE_RASTERIZER
,
1216 [D3DRS_ZWRITEENABLE
] = NINE_STATE_DSA
,
1217 [D3DRS_ALPHATESTENABLE
] = NINE_STATE_DSA
,
1218 [D3DRS_LASTPIXEL
] = NINE_STATE_RASTERIZER
,
1219 [D3DRS_SRCBLEND
] = NINE_STATE_BLEND
,
1220 [D3DRS_DESTBLEND
] = NINE_STATE_BLEND
,
1221 [D3DRS_CULLMODE
] = NINE_STATE_RASTERIZER
,
1222 [D3DRS_ZFUNC
] = NINE_STATE_DSA
,
1223 [D3DRS_ALPHAREF
] = NINE_STATE_DSA
,
1224 [D3DRS_ALPHAFUNC
] = NINE_STATE_DSA
,
1225 [D3DRS_DITHERENABLE
] = NINE_STATE_RASTERIZER
,
1226 [D3DRS_ALPHABLENDENABLE
] = NINE_STATE_BLEND
,
1227 [D3DRS_FOGENABLE
] = NINE_STATE_FF_OTHER
,
1228 [D3DRS_SPECULARENABLE
] = NINE_STATE_FF_LIGHTING
,
1229 [D3DRS_FOGCOLOR
] = NINE_STATE_FF_OTHER
,
1230 [D3DRS_FOGTABLEMODE
] = NINE_STATE_FF_OTHER
,
1231 [D3DRS_FOGSTART
] = NINE_STATE_FF_OTHER
,
1232 [D3DRS_FOGEND
] = NINE_STATE_FF_OTHER
,
1233 [D3DRS_FOGDENSITY
] = NINE_STATE_FF_OTHER
,
1234 [D3DRS_RANGEFOGENABLE
] = NINE_STATE_FF_OTHER
,
1235 [D3DRS_STENCILENABLE
] = NINE_STATE_DSA
,
1236 [D3DRS_STENCILFAIL
] = NINE_STATE_DSA
,
1237 [D3DRS_STENCILZFAIL
] = NINE_STATE_DSA
,
1238 [D3DRS_STENCILPASS
] = NINE_STATE_DSA
,
1239 [D3DRS_STENCILFUNC
] = NINE_STATE_DSA
,
1240 [D3DRS_STENCILREF
] = NINE_STATE_STENCIL_REF
,
1241 [D3DRS_STENCILMASK
] = NINE_STATE_DSA
,
1242 [D3DRS_STENCILWRITEMASK
] = NINE_STATE_DSA
,
1243 [D3DRS_TEXTUREFACTOR
] = NINE_STATE_FF_PSSTAGES
,
1244 [D3DRS_WRAP0
] = NINE_STATE_UNHANDLED
, /* cylindrical wrap is crazy */
1245 [D3DRS_WRAP1
] = NINE_STATE_UNHANDLED
,
1246 [D3DRS_WRAP2
] = NINE_STATE_UNHANDLED
,
1247 [D3DRS_WRAP3
] = NINE_STATE_UNHANDLED
,
1248 [D3DRS_WRAP4
] = NINE_STATE_UNHANDLED
,
1249 [D3DRS_WRAP5
] = NINE_STATE_UNHANDLED
,
1250 [D3DRS_WRAP6
] = NINE_STATE_UNHANDLED
,
1251 [D3DRS_WRAP7
] = NINE_STATE_UNHANDLED
,
1252 [D3DRS_CLIPPING
] = 0, /* software vertex processing only */
1253 [D3DRS_LIGHTING
] = NINE_STATE_FF_LIGHTING
,
1254 [D3DRS_AMBIENT
] = NINE_STATE_FF_LIGHTING
| NINE_STATE_FF_MATERIAL
,
1255 [D3DRS_FOGVERTEXMODE
] = NINE_STATE_FF_OTHER
,
1256 [D3DRS_COLORVERTEX
] = NINE_STATE_FF_LIGHTING
,
1257 [D3DRS_LOCALVIEWER
] = NINE_STATE_FF_LIGHTING
,
1258 [D3DRS_NORMALIZENORMALS
] = NINE_STATE_FF_OTHER
,
1259 [D3DRS_DIFFUSEMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1260 [D3DRS_SPECULARMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1261 [D3DRS_AMBIENTMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1262 [D3DRS_EMISSIVEMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1263 [D3DRS_VERTEXBLEND
] = NINE_STATE_FF_OTHER
,
1264 [D3DRS_CLIPPLANEENABLE
] = NINE_STATE_RASTERIZER
,
1265 [D3DRS_POINTSIZE
] = NINE_STATE_RASTERIZER
,
1266 [D3DRS_POINTSIZE_MIN
] = NINE_STATE_MISC_CONST
,
1267 [D3DRS_POINTSPRITEENABLE
] = NINE_STATE_RASTERIZER
,
1268 [D3DRS_POINTSCALEENABLE
] = NINE_STATE_FF_OTHER
,
1269 [D3DRS_POINTSCALE_A
] = NINE_STATE_FF_OTHER
,
1270 [D3DRS_POINTSCALE_B
] = NINE_STATE_FF_OTHER
,
1271 [D3DRS_POINTSCALE_C
] = NINE_STATE_FF_OTHER
,
1272 [D3DRS_MULTISAMPLEANTIALIAS
] = NINE_STATE_RASTERIZER
,
1273 [D3DRS_MULTISAMPLEMASK
] = NINE_STATE_SAMPLE_MASK
,
1274 [D3DRS_PATCHEDGESTYLE
] = NINE_STATE_UNHANDLED
,
1275 [D3DRS_DEBUGMONITORTOKEN
] = NINE_STATE_UNHANDLED
,
1276 [D3DRS_POINTSIZE_MAX
] = NINE_STATE_MISC_CONST
,
1277 [D3DRS_INDEXEDVERTEXBLENDENABLE
] = NINE_STATE_FF_OTHER
,
1278 [D3DRS_COLORWRITEENABLE
] = NINE_STATE_BLEND
,
1279 [D3DRS_TWEENFACTOR
] = NINE_STATE_FF_OTHER
,
1280 [D3DRS_BLENDOP
] = NINE_STATE_BLEND
,
1281 [D3DRS_POSITIONDEGREE
] = NINE_STATE_UNHANDLED
,
1282 [D3DRS_NORMALDEGREE
] = NINE_STATE_UNHANDLED
,
1283 [D3DRS_SCISSORTESTENABLE
] = NINE_STATE_RASTERIZER
,
1284 [D3DRS_SLOPESCALEDEPTHBIAS
] = NINE_STATE_RASTERIZER
,
1285 [D3DRS_ANTIALIASEDLINEENABLE
] = NINE_STATE_RASTERIZER
,
1286 [D3DRS_MINTESSELLATIONLEVEL
] = NINE_STATE_UNHANDLED
,
1287 [D3DRS_MAXTESSELLATIONLEVEL
] = NINE_STATE_UNHANDLED
,
1288 [D3DRS_ADAPTIVETESS_X
] = NINE_STATE_UNHANDLED
,
1289 [D3DRS_ADAPTIVETESS_Y
] = NINE_STATE_UNHANDLED
,
1290 [D3DRS_ADAPTIVETESS_Z
] = NINE_STATE_UNHANDLED
,
1291 [D3DRS_ADAPTIVETESS_W
] = NINE_STATE_UNHANDLED
,
1292 [D3DRS_ENABLEADAPTIVETESSELLATION
] = NINE_STATE_UNHANDLED
,
1293 [D3DRS_TWOSIDEDSTENCILMODE
] = NINE_STATE_DSA
,
1294 [D3DRS_CCW_STENCILFAIL
] = NINE_STATE_DSA
,
1295 [D3DRS_CCW_STENCILZFAIL
] = NINE_STATE_DSA
,
1296 [D3DRS_CCW_STENCILPASS
] = NINE_STATE_DSA
,
1297 [D3DRS_CCW_STENCILFUNC
] = NINE_STATE_DSA
,
1298 [D3DRS_COLORWRITEENABLE1
] = NINE_STATE_BLEND
,
1299 [D3DRS_COLORWRITEENABLE2
] = NINE_STATE_BLEND
,
1300 [D3DRS_COLORWRITEENABLE3
] = NINE_STATE_BLEND
,
1301 [D3DRS_BLENDFACTOR
] = NINE_STATE_BLEND_COLOR
,
1302 [D3DRS_SRGBWRITEENABLE
] = NINE_STATE_FB
,
1303 [D3DRS_DEPTHBIAS
] = NINE_STATE_RASTERIZER
,
1304 [D3DRS_WRAP8
] = NINE_STATE_UNHANDLED
, /* cylwrap has to be done via GP */
1305 [D3DRS_WRAP9
] = NINE_STATE_UNHANDLED
,
1306 [D3DRS_WRAP10
] = NINE_STATE_UNHANDLED
,
1307 [D3DRS_WRAP11
] = NINE_STATE_UNHANDLED
,
1308 [D3DRS_WRAP12
] = NINE_STATE_UNHANDLED
,
1309 [D3DRS_WRAP13
] = NINE_STATE_UNHANDLED
,
1310 [D3DRS_WRAP14
] = NINE_STATE_UNHANDLED
,
1311 [D3DRS_WRAP15
] = NINE_STATE_UNHANDLED
,
1312 [D3DRS_SEPARATEALPHABLENDENABLE
] = NINE_STATE_BLEND
,
1313 [D3DRS_SRCBLENDALPHA
] = NINE_STATE_BLEND
,
1314 [D3DRS_DESTBLENDALPHA
] = NINE_STATE_BLEND
,
1315 [D3DRS_BLENDOPALPHA
] = NINE_STATE_BLEND
1319 nine_state_access_transform(struct nine_state
*state
, D3DTRANSFORMSTATETYPE t
,
1322 static D3DMATRIX Identity
= { .m
[0] = { 1, 0, 0, 0 },
1323 .m
[1] = { 0, 1, 0, 0 },
1324 .m
[2] = { 0, 0, 1, 0 },
1325 .m
[3] = { 0, 0, 0, 1 } };
1329 case D3DTS_VIEW
: index
= 0; break;
1330 case D3DTS_PROJECTION
: index
= 1; break;
1331 case D3DTS_TEXTURE0
: index
= 2; break;
1332 case D3DTS_TEXTURE1
: index
= 3; break;
1333 case D3DTS_TEXTURE2
: index
= 4; break;
1334 case D3DTS_TEXTURE3
: index
= 5; break;
1335 case D3DTS_TEXTURE4
: index
= 6; break;
1336 case D3DTS_TEXTURE5
: index
= 7; break;
1337 case D3DTS_TEXTURE6
: index
= 8; break;
1338 case D3DTS_TEXTURE7
: index
= 9; break;
1340 if (!(t
>= D3DTS_WORLDMATRIX(0) && t
<= D3DTS_WORLDMATRIX(255)))
1342 index
= 10 + (t
- D3DTS_WORLDMATRIX(0));
1346 if (index
>= state
->ff
.num_transforms
) {
1347 unsigned N
= index
+ 1;
1348 unsigned n
= state
->ff
.num_transforms
;
1352 state
->ff
.transform
= REALLOC(state
->ff
.transform
,
1353 n
* sizeof(D3DMATRIX
),
1354 N
* sizeof(D3DMATRIX
));
1356 state
->ff
.transform
[n
] = Identity
;
1357 state
->ff
.num_transforms
= N
;
1359 return &state
->ff
.transform
[index
];
1362 #define D3DRS_TO_STRING_CASE(n) case D3DRS_##n: return "D3DRS_"#n
1363 const char *nine_d3drs_to_string(DWORD State
)
1366 D3DRS_TO_STRING_CASE(ZENABLE
);
1367 D3DRS_TO_STRING_CASE(FILLMODE
);
1368 D3DRS_TO_STRING_CASE(SHADEMODE
);
1369 D3DRS_TO_STRING_CASE(ZWRITEENABLE
);
1370 D3DRS_TO_STRING_CASE(ALPHATESTENABLE
);
1371 D3DRS_TO_STRING_CASE(LASTPIXEL
);
1372 D3DRS_TO_STRING_CASE(SRCBLEND
);
1373 D3DRS_TO_STRING_CASE(DESTBLEND
);
1374 D3DRS_TO_STRING_CASE(CULLMODE
);
1375 D3DRS_TO_STRING_CASE(ZFUNC
);
1376 D3DRS_TO_STRING_CASE(ALPHAREF
);
1377 D3DRS_TO_STRING_CASE(ALPHAFUNC
);
1378 D3DRS_TO_STRING_CASE(DITHERENABLE
);
1379 D3DRS_TO_STRING_CASE(ALPHABLENDENABLE
);
1380 D3DRS_TO_STRING_CASE(FOGENABLE
);
1381 D3DRS_TO_STRING_CASE(SPECULARENABLE
);
1382 D3DRS_TO_STRING_CASE(FOGCOLOR
);
1383 D3DRS_TO_STRING_CASE(FOGTABLEMODE
);
1384 D3DRS_TO_STRING_CASE(FOGSTART
);
1385 D3DRS_TO_STRING_CASE(FOGEND
);
1386 D3DRS_TO_STRING_CASE(FOGDENSITY
);
1387 D3DRS_TO_STRING_CASE(RANGEFOGENABLE
);
1388 D3DRS_TO_STRING_CASE(STENCILENABLE
);
1389 D3DRS_TO_STRING_CASE(STENCILFAIL
);
1390 D3DRS_TO_STRING_CASE(STENCILZFAIL
);
1391 D3DRS_TO_STRING_CASE(STENCILPASS
);
1392 D3DRS_TO_STRING_CASE(STENCILFUNC
);
1393 D3DRS_TO_STRING_CASE(STENCILREF
);
1394 D3DRS_TO_STRING_CASE(STENCILMASK
);
1395 D3DRS_TO_STRING_CASE(STENCILWRITEMASK
);
1396 D3DRS_TO_STRING_CASE(TEXTUREFACTOR
);
1397 D3DRS_TO_STRING_CASE(WRAP0
);
1398 D3DRS_TO_STRING_CASE(WRAP1
);
1399 D3DRS_TO_STRING_CASE(WRAP2
);
1400 D3DRS_TO_STRING_CASE(WRAP3
);
1401 D3DRS_TO_STRING_CASE(WRAP4
);
1402 D3DRS_TO_STRING_CASE(WRAP5
);
1403 D3DRS_TO_STRING_CASE(WRAP6
);
1404 D3DRS_TO_STRING_CASE(WRAP7
);
1405 D3DRS_TO_STRING_CASE(CLIPPING
);
1406 D3DRS_TO_STRING_CASE(LIGHTING
);
1407 D3DRS_TO_STRING_CASE(AMBIENT
);
1408 D3DRS_TO_STRING_CASE(FOGVERTEXMODE
);
1409 D3DRS_TO_STRING_CASE(COLORVERTEX
);
1410 D3DRS_TO_STRING_CASE(LOCALVIEWER
);
1411 D3DRS_TO_STRING_CASE(NORMALIZENORMALS
);
1412 D3DRS_TO_STRING_CASE(DIFFUSEMATERIALSOURCE
);
1413 D3DRS_TO_STRING_CASE(SPECULARMATERIALSOURCE
);
1414 D3DRS_TO_STRING_CASE(AMBIENTMATERIALSOURCE
);
1415 D3DRS_TO_STRING_CASE(EMISSIVEMATERIALSOURCE
);
1416 D3DRS_TO_STRING_CASE(VERTEXBLEND
);
1417 D3DRS_TO_STRING_CASE(CLIPPLANEENABLE
);
1418 D3DRS_TO_STRING_CASE(POINTSIZE
);
1419 D3DRS_TO_STRING_CASE(POINTSIZE_MIN
);
1420 D3DRS_TO_STRING_CASE(POINTSPRITEENABLE
);
1421 D3DRS_TO_STRING_CASE(POINTSCALEENABLE
);
1422 D3DRS_TO_STRING_CASE(POINTSCALE_A
);
1423 D3DRS_TO_STRING_CASE(POINTSCALE_B
);
1424 D3DRS_TO_STRING_CASE(POINTSCALE_C
);
1425 D3DRS_TO_STRING_CASE(MULTISAMPLEANTIALIAS
);
1426 D3DRS_TO_STRING_CASE(MULTISAMPLEMASK
);
1427 D3DRS_TO_STRING_CASE(PATCHEDGESTYLE
);
1428 D3DRS_TO_STRING_CASE(DEBUGMONITORTOKEN
);
1429 D3DRS_TO_STRING_CASE(POINTSIZE_MAX
);
1430 D3DRS_TO_STRING_CASE(INDEXEDVERTEXBLENDENABLE
);
1431 D3DRS_TO_STRING_CASE(COLORWRITEENABLE
);
1432 D3DRS_TO_STRING_CASE(TWEENFACTOR
);
1433 D3DRS_TO_STRING_CASE(BLENDOP
);
1434 D3DRS_TO_STRING_CASE(POSITIONDEGREE
);
1435 D3DRS_TO_STRING_CASE(NORMALDEGREE
);
1436 D3DRS_TO_STRING_CASE(SCISSORTESTENABLE
);
1437 D3DRS_TO_STRING_CASE(SLOPESCALEDEPTHBIAS
);
1438 D3DRS_TO_STRING_CASE(ANTIALIASEDLINEENABLE
);
1439 D3DRS_TO_STRING_CASE(MINTESSELLATIONLEVEL
);
1440 D3DRS_TO_STRING_CASE(MAXTESSELLATIONLEVEL
);
1441 D3DRS_TO_STRING_CASE(ADAPTIVETESS_X
);
1442 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Y
);
1443 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Z
);
1444 D3DRS_TO_STRING_CASE(ADAPTIVETESS_W
);
1445 D3DRS_TO_STRING_CASE(ENABLEADAPTIVETESSELLATION
);
1446 D3DRS_TO_STRING_CASE(TWOSIDEDSTENCILMODE
);
1447 D3DRS_TO_STRING_CASE(CCW_STENCILFAIL
);
1448 D3DRS_TO_STRING_CASE(CCW_STENCILZFAIL
);
1449 D3DRS_TO_STRING_CASE(CCW_STENCILPASS
);
1450 D3DRS_TO_STRING_CASE(CCW_STENCILFUNC
);
1451 D3DRS_TO_STRING_CASE(COLORWRITEENABLE1
);
1452 D3DRS_TO_STRING_CASE(COLORWRITEENABLE2
);
1453 D3DRS_TO_STRING_CASE(COLORWRITEENABLE3
);
1454 D3DRS_TO_STRING_CASE(BLENDFACTOR
);
1455 D3DRS_TO_STRING_CASE(SRGBWRITEENABLE
);
1456 D3DRS_TO_STRING_CASE(DEPTHBIAS
);
1457 D3DRS_TO_STRING_CASE(WRAP8
);
1458 D3DRS_TO_STRING_CASE(WRAP9
);
1459 D3DRS_TO_STRING_CASE(WRAP10
);
1460 D3DRS_TO_STRING_CASE(WRAP11
);
1461 D3DRS_TO_STRING_CASE(WRAP12
);
1462 D3DRS_TO_STRING_CASE(WRAP13
);
1463 D3DRS_TO_STRING_CASE(WRAP14
);
1464 D3DRS_TO_STRING_CASE(WRAP15
);
1465 D3DRS_TO_STRING_CASE(SEPARATEALPHABLENDENABLE
);
1466 D3DRS_TO_STRING_CASE(SRCBLENDALPHA
);
1467 D3DRS_TO_STRING_CASE(DESTBLENDALPHA
);
1468 D3DRS_TO_STRING_CASE(BLENDOPALPHA
);