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 unsigned w
= 0, h
= 0; /* no surface can have width or height 0 */
49 const int sRGB
= state
->rs
[D3DRS_SRGBWRITEENABLE
] ? 1 : 0;
56 for (i
= 0; i
< device
->caps
.NumSimultaneousRTs
; ++i
) {
57 if (state
->rt
[i
] && state
->rt
[i
]->desc
.Format
!= D3DFMT_NULL
) {
58 struct NineSurface9
*rt
= state
->rt
[i
];
59 fb
->cbufs
[i
] = NineSurface9_GetSurface(rt
, sRGB
);
60 state
->rt_mask
|= 1 << i
;
63 w
= MIN2(w
, rt
->desc
.Width
);
64 h
= MIN2(h
, rt
->desc
.Height
);
69 if (unlikely(rt
->desc
.Usage
& D3DUSAGE_AUTOGENMIPMAP
)) {
70 assert(rt
->texture
== D3DRTYPE_TEXTURE
||
71 rt
->texture
== D3DRTYPE_CUBETEXTURE
);
72 NineBaseTexture9(rt
->base
.base
.container
)->dirty_mip
= TRUE
;
75 /* Color outputs must match RT slot,
76 * drivers will have to handle NULL entries for GL, too.
83 fb
->zsbuf
= NineSurface9_GetSurface(state
->ds
, 0);
85 w
= MIN2(w
, state
->ds
->desc
.Width
);
86 h
= MIN2(h
, state
->ds
->desc
.Height
);
88 w
= state
->ds
->desc
.Width
;
89 h
= state
->ds
->desc
.Height
;
98 pipe
->set_framebuffer_state(pipe
, fb
); /* XXX: cso ? */
102 switch (fb
->zsbuf
->format
) {
103 case PIPE_FORMAT_Z32_FLOAT
:
104 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
107 case PIPE_FORMAT_Z16_UNORM
:
108 scale
= fui((float)(1 << 16));
111 scale
= fui((float)(1 << 24));
114 if (state
->rs
[NINED3DRS_ZBIASSCALE
] != scale
) {
115 state
->rs
[NINED3DRS_ZBIASSCALE
] = scale
;
116 state
->changed
.group
|= NINE_STATE_RASTERIZER
;
121 if (state
->rt_mask
& (state
->ps
? ~state
->ps
->rt_mask
: 0))
122 WARN_ONCE("FIXME: writing undefined values to cbufs 0x%x\n",
123 state
->rt_mask
& ~state
->ps
->rt_mask
);
126 return state
->changed
.group
;
130 update_viewport(struct NineDevice9
*device
)
132 struct pipe_context
*pipe
= device
->pipe
;
133 const D3DVIEWPORT9
*vport
= &device
->state
.viewport
;
134 struct pipe_viewport_state pvport
;
137 * I hope D3D clip coordinates are still
138 * -1 .. +1 for X,Y and
139 * 0 .. +1 for Z (use pipe_rasterizer_state.clip_halfz)
141 pvport
.scale
[0] = (float)vport
->Width
* 0.5f
;
142 pvport
.scale
[1] = (float)vport
->Height
* -0.5f
;
143 pvport
.scale
[2] = vport
->MaxZ
- vport
->MinZ
;
144 pvport
.translate
[0] = (float)vport
->Width
* 0.5f
+ (float)vport
->X
;
145 pvport
.translate
[1] = (float)vport
->Height
* 0.5f
+ (float)vport
->Y
;
146 pvport
.translate
[2] = vport
->MinZ
;
148 pipe
->set_viewport_states(pipe
, 0, 1, &pvport
);
152 update_scissor(struct NineDevice9
*device
)
154 struct pipe_context
*pipe
= device
->pipe
;
156 pipe
->set_scissor_states(pipe
, 0, 1, &device
->state
.scissor
);
160 update_blend(struct NineDevice9
*device
)
162 nine_convert_blend_state(device
->cso
, device
->state
.rs
);
166 update_dsa(struct NineDevice9
*device
)
168 nine_convert_dsa_state(device
->cso
, device
->state
.rs
);
172 update_rasterizer(struct NineDevice9
*device
)
174 nine_convert_rasterizer_state(device
->cso
, device
->state
.rs
);
177 /* Loop through VS inputs and pick the vertex elements with the declared
178 * usage from the vertex declaration, then insert the instance divisor from
179 * the stream source frequency setting.
182 update_vertex_elements(struct NineDevice9
*device
)
184 struct nine_state
*state
= &device
->state
;
185 const struct NineVertexDeclaration9
*vdecl
= device
->state
.vdecl
;
186 const struct NineVertexShader9
*vs
;
189 struct pipe_vertex_element ve
[PIPE_MAX_ATTRIBS
];
191 state
->stream_usage_mask
= 0;
193 vs
= device
->state
.vs
? device
->state
.vs
: device
->ff
.vs
;
195 if (!vdecl
) /* no inputs */
197 for (n
= 0; n
< vs
->num_inputs
; ++n
) {
198 DBG("looking up input %u (usage %u) from vdecl(%p)\n",
199 n
, vs
->input_map
[n
].ndecl
, vdecl
);
202 for (i
= 0; i
< vdecl
->nelems
; i
++) {
203 if (vdecl
->usage_map
[i
] == vs
->input_map
[n
].ndecl
) {
210 ve
[n
] = vdecl
->elems
[index
];
211 b
= ve
[n
].vertex_buffer_index
;
212 state
->stream_usage_mask
|= 1 << b
;
213 /* XXX wine just uses 1 here: */
214 if (state
->stream_freq
[b
] & D3DSTREAMSOURCE_INSTANCEDATA
)
215 ve
[n
].instance_divisor
= state
->stream_freq
[b
] & 0x7FFFFF;
217 /* TODO: msdn doesn't specify what should happen when the vertex
218 * declaration doesn't match the vertex shader inputs.
219 * Some websites say the code will pass but nothing will get rendered.
220 * We should check and implement the correct behaviour. */
221 /* Put PIPE_FORMAT_NONE.
222 * Some drivers (r300) are very unhappy with that */
223 ve
[n
].src_format
= PIPE_FORMAT_NONE
;
224 ve
[n
].src_offset
= 0;
225 ve
[n
].instance_divisor
= 0;
226 ve
[n
].vertex_buffer_index
= 0;
229 cso_set_vertex_elements(device
->cso
, vs
->num_inputs
, ve
);
231 state
->changed
.stream_freq
= 0;
234 static INLINE
uint32_t
235 update_shader_variant_keys(struct NineDevice9
*device
)
237 struct nine_state
*state
= &device
->state
;
239 uint32_t vs_key
= state
->samplers_shadow
;
240 uint32_t ps_key
= state
->samplers_shadow
;
242 vs_key
= (vs_key
& NINE_VS_SAMPLERS_MASK
) >> NINE_SAMPLER_VS(0);
243 ps_key
= (ps_key
& NINE_PS_SAMPLERS_MASK
) >> NINE_SAMPLER_PS(0);
245 if (state
->vs
) vs_key
&= state
->vs
->sampler_mask
;
247 if (unlikely(state
->ps
->byte_code
.version
< 0x20)) {
248 /* no depth textures, but variable targets */
249 uint32_t m
= state
->ps
->sampler_mask
;
254 ps_key
|= (state
->texture
[s
] ? state
->texture
[s
]->pstype
: 1) << (s
* 2);
257 ps_key
&= state
->ps
->sampler_mask
;
261 if (state
->vs
&& state
->vs_key
!= vs_key
) {
262 state
->vs_key
= vs_key
;
263 mask
|= NINE_STATE_VS
;
265 if (state
->ps
&& state
->ps_key
!= ps_key
) {
266 state
->ps_key
= ps_key
;
267 mask
|= NINE_STATE_PS
;
272 static INLINE
uint32_t
273 update_vs(struct NineDevice9
*device
)
275 struct nine_state
*state
= &device
->state
;
276 struct NineVertexShader9
*vs
= state
->vs
;
278 /* likely because we dislike FF */
280 state
->cso
.vs
= NineVertexShader9_GetVariant(vs
, state
->vs_key
);
283 state
->cso
.vs
= vs
->variant
.cso
;
285 device
->pipe
->bind_vs_state(device
->pipe
, state
->cso
.vs
);
287 if (state
->rs
[NINED3DRS_VSPOINTSIZE
] != vs
->point_size
) {
288 state
->rs
[NINED3DRS_VSPOINTSIZE
] = vs
->point_size
;
289 return NINE_STATE_RASTERIZER
;
293 unsigned s
, mask
= vs
->sampler_mask
;
294 for (s
= 0; mask
; ++s
, mask
>>= 1)
295 if ((mask
& 1) && !(device
->state
.texture
[NINE_SAMPLER_VS(s
)]))
296 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
302 static INLINE
uint32_t
303 update_ps(struct NineDevice9
*device
)
305 struct nine_state
*state
= &device
->state
;
306 struct NinePixelShader9
*ps
= state
->ps
;
309 state
->cso
.ps
= NinePixelShader9_GetVariant(ps
, state
->ps_key
);
312 state
->cso
.ps
= ps
->variant
.cso
;
314 device
->pipe
->bind_fs_state(device
->pipe
, state
->cso
.ps
);
318 unsigned s
, mask
= ps
->sampler_mask
;
319 for (s
= 0; mask
; ++s
, mask
>>= 1)
320 if ((mask
& 1) && !(device
->state
.texture
[NINE_SAMPLER_PS(s
)]))
321 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
322 if (device
->state
.rt_mask
& ~ps
->rt_mask
)
323 WARN_ONCE("FIXME: writing undefined values to cbufs 0x%x\n",
324 device
->state
.rt_mask
& ~ps
->rt_mask
);
330 #define DO_UPLOAD_CONST_F(buf,p,c,d) \
332 DBG("upload ConstantF [%u .. %u]\n", x, (x) + (c) - 1); \
333 box.x = (p) * 4 * sizeof(float); \
334 box.width = (c) * 4 * sizeof(float); \
335 pipe->transfer_inline_write(pipe, buf, 0, usage, &box, &((d)[p * 4]), \
339 /* OK, this is a bit ugly ... */
341 update_constants(struct NineDevice9
*device
, unsigned shader_type
)
343 struct pipe_context
*pipe
= device
->pipe
;
344 struct pipe_resource
*buf
;
347 const float *const_f
;
350 uint32_t data_b
[NINE_MAX_CONST_B
];
353 const unsigned usage
= PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD_RANGE
;
354 unsigned x
= 0; /* silence warning */
356 struct nine_range
*r
, *p
, *lconstf_ranges
;
364 if (shader_type
== PIPE_SHADER_VERTEX
) {
366 buf
= device
->constbuf_vs
;
368 const_f
= device
->state
.vs_const_f
;
369 for (p
= r
= device
->state
.changed
.vs_const_f
; r
; p
= r
, r
= r
->next
)
370 DO_UPLOAD_CONST_F(buf
, r
->bgn
, r
->end
- r
->bgn
, const_f
);
372 nine_range_pool_put_chain(&device
->range_pool
,
373 device
->state
.changed
.vs_const_f
, p
);
374 device
->state
.changed
.vs_const_f
= NULL
;
377 dirty_i
= device
->state
.changed
.vs_const_i
;
378 device
->state
.changed
.vs_const_i
= 0;
379 const_i
= &device
->state
.vs_const_i
[0][0];
381 dirty_b
= device
->state
.changed
.vs_const_b
;
382 device
->state
.changed
.vs_const_b
= 0;
383 const_b
= device
->state
.vs_const_b
;
385 lconstf_ranges
= device
->state
.vs
->lconstf
.ranges
;
386 lconstf_data
= device
->state
.vs
->lconstf
.data
;
388 device
->state
.ff
.clobber
.vs_const
= TRUE
;
389 device
->state
.changed
.group
&= ~NINE_STATE_VS_CONST
;
392 buf
= device
->constbuf_ps
;
394 const_f
= device
->state
.ps_const_f
;
395 for (p
= r
= device
->state
.changed
.ps_const_f
; r
; p
= r
, r
= r
->next
)
396 DO_UPLOAD_CONST_F(buf
, r
->bgn
, r
->end
- r
->bgn
, const_f
);
398 nine_range_pool_put_chain(&device
->range_pool
,
399 device
->state
.changed
.ps_const_f
, p
);
400 device
->state
.changed
.ps_const_f
= NULL
;
403 dirty_i
= device
->state
.changed
.ps_const_i
;
404 device
->state
.changed
.ps_const_i
= 0;
405 const_i
= &device
->state
.ps_const_i
[0][0];
407 dirty_b
= device
->state
.changed
.ps_const_b
;
408 device
->state
.changed
.ps_const_b
= 0;
409 const_b
= device
->state
.ps_const_b
;
411 lconstf_ranges
= NULL
;
414 device
->state
.ff
.clobber
.ps_const
= TRUE
;
415 device
->state
.changed
.group
&= ~NINE_STATE_PS_CONST
;
418 /* write range from min to max changed, it's not much data */
421 c
= util_last_bit(dirty_b
);
422 i
= ffs(dirty_b
) - 1;
423 x
= buf
->width0
- (NINE_MAX_CONST_B
- i
) * 4;
425 memcpy(data_b
, &(const_b
[i
]), c
* sizeof(uint32_t));
428 DBG("upload ConstantB [%u .. %u]\n", x
, x
+ c
- 1);
429 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data_b
, 0, 0);
433 for (c
= 0, i
= 0; dirty_i
; i
++, dirty_i
>>= 1) {
440 DBG("upload ConstantI [%u .. %u]\n", x
, x
+ c
- 1);
441 data
= &const_i
[x
* 4];
442 box
.x
= buf
->width0
- (NINE_MAX_CONST_I
* 4 + NINE_MAX_CONST_B
) * 4;
443 box
.x
+= x
* 4 * sizeof(int);
444 box
.width
= c
* 4 * sizeof(int);
446 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
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);
455 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
458 /* TODO: only upload these when shader itself changes */
459 if (lconstf_ranges
) {
461 struct nine_range
*r
= lconstf_ranges
;
463 box
.x
= r
->bgn
* 4 * sizeof(float);
464 n
+= r
->end
- r
->bgn
;
465 box
.width
= (r
->end
- r
->bgn
) * 4 * sizeof(float);
466 data
= &lconstf_data
[4 * n
];
467 pipe
->transfer_inline_write(pipe
, buf
, 0, usage
, &box
, data
, 0, 0);
474 update_vs_constants_userbuf(struct NineDevice9
*device
)
476 struct nine_state
*state
= &device
->state
;
477 struct pipe_context
*pipe
= device
->pipe
;
478 struct pipe_constant_buffer cb
;
480 cb
.buffer_offset
= 0;
481 cb
.buffer_size
= device
->state
.vs
->const_used_size
;
482 cb
.user_buffer
= device
->state
.vs_const_f
;
487 if (state
->changed
.vs_const_i
) {
488 int *idst
= (int *)&state
->vs_const_f
[4 * device
->max_vs_const_f
];
489 memcpy(idst
, state
->vs_const_i
, sizeof(state
->vs_const_i
));
490 state
->changed
.vs_const_i
= 0;
492 if (state
->changed
.vs_const_b
) {
493 int *idst
= (int *)&state
->vs_const_f
[4 * device
->max_vs_const_f
];
494 uint32_t *bdst
= (uint32_t *)&idst
[4 * NINE_MAX_CONST_I
];
495 memcpy(bdst
, state
->vs_const_b
, sizeof(state
->vs_const_b
));
496 state
->changed
.vs_const_b
= 0;
499 if (device
->state
.vs
->lconstf
.ranges
) {
500 /* TODO: Can we make it so that we don't have to copy everything ? */
501 const struct nine_lconstf
*lconstf
= &device
->state
.vs
->lconstf
;
502 const struct nine_range
*r
= lconstf
->ranges
;
504 float *dst
= device
->state
.vs_lconstf_temp
;
505 float *src
= (float *)cb
.user_buffer
;
506 memcpy(dst
, src
, cb
.buffer_size
);
509 unsigned c
= r
->end
- r
->bgn
;
510 memcpy(&dst
[p
* 4], &lconstf
->data
[n
* 4], c
* 4 * sizeof(float));
514 cb
.user_buffer
= dst
;
517 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_VERTEX
, 0, &cb
);
519 if (device
->state
.changed
.vs_const_f
) {
520 struct nine_range
*r
= device
->state
.changed
.vs_const_f
;
521 struct nine_range
*p
= r
;
524 nine_range_pool_put_chain(&device
->range_pool
, r
, p
);
525 device
->state
.changed
.vs_const_f
= NULL
;
527 state
->changed
.group
&= ~NINE_STATE_VS_CONST
;
531 update_ps_constants_userbuf(struct NineDevice9
*device
)
533 struct nine_state
*state
= &device
->state
;
534 struct pipe_context
*pipe
= device
->pipe
;
535 struct pipe_constant_buffer cb
;
537 cb
.buffer_offset
= 0;
538 cb
.buffer_size
= device
->state
.ps
->const_used_size
;
539 cb
.user_buffer
= device
->state
.ps_const_f
;
544 if (state
->changed
.ps_const_i
) {
545 int *idst
= (int *)&state
->ps_const_f
[4 * device
->max_ps_const_f
];
546 memcpy(idst
, state
->ps_const_i
, sizeof(state
->ps_const_i
));
547 state
->changed
.ps_const_i
= 0;
549 if (state
->changed
.ps_const_b
) {
550 int *idst
= (int *)&state
->ps_const_f
[4 * device
->max_ps_const_f
];
551 uint32_t *bdst
= (uint32_t *)&idst
[4 * NINE_MAX_CONST_I
];
552 memcpy(bdst
, state
->ps_const_b
, sizeof(state
->ps_const_b
));
553 state
->changed
.ps_const_b
= 0;
556 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
558 if (device
->state
.changed
.ps_const_f
) {
559 struct nine_range
*r
= device
->state
.changed
.ps_const_f
;
560 struct nine_range
*p
= r
;
563 nine_range_pool_put_chain(&device
->range_pool
, r
, p
);
564 device
->state
.changed
.ps_const_f
= NULL
;
566 state
->changed
.group
&= ~NINE_STATE_PS_CONST
;
570 update_vertex_buffers(struct NineDevice9
*device
)
572 struct pipe_context
*pipe
= device
->pipe
;
573 struct nine_state
*state
= &device
->state
;
574 uint32_t mask
= state
->changed
.vtxbuf
;
579 DBG("mask=%x\n", mask
);
581 for (i
= 0; mask
; mask
>>= 1, ++i
) {
588 pipe
->set_vertex_buffers(pipe
,
589 start
, count
, &state
->vtxbuf
[start
]);
594 pipe
->set_vertex_buffers(pipe
, start
, count
, &state
->vtxbuf
[start
]);
596 state
->changed
.vtxbuf
= 0;
600 update_index_buffer(struct NineDevice9
*device
)
602 struct pipe_context
*pipe
= device
->pipe
;
603 if (device
->state
.idxbuf
)
604 pipe
->set_index_buffer(pipe
, &device
->state
.idxbuf
->buffer
);
606 pipe
->set_index_buffer(pipe
, NULL
);
609 /* TODO: only go through dirty textures */
611 validate_textures(struct NineDevice9
*device
)
613 struct NineBaseTexture9
*tex
, *ptr
;
614 LIST_FOR_EACH_ENTRY_SAFE(tex
, ptr
, &device
->update_textures
, list
) {
615 list_delinit(&tex
->list
);
616 NineBaseTexture9_Validate(tex
);
620 static INLINE boolean
621 update_sampler_derived(struct nine_state
*state
, unsigned s
)
623 boolean changed
= FALSE
;
625 if (state
->samp
[s
][NINED3DSAMP_SHADOW
] != state
->texture
[s
]->shadow
) {
627 state
->samp
[s
][NINED3DSAMP_SHADOW
] = state
->texture
[s
]->shadow
;
630 if (state
->samp
[s
][D3DSAMP_MIPFILTER
] != D3DTEXF_NONE
) {
631 int lod
= state
->samp
[s
][D3DSAMP_MAXMIPLEVEL
] - state
->texture
[s
]->lod
;
634 if (state
->samp
[s
][NINED3DSAMP_MINLOD
] != lod
) {
636 state
->samp
[s
][NINED3DSAMP_MINLOD
] = lod
;
639 state
->changed
.sampler
[s
] &= ~0x300; /* lod changes irrelevant */
645 /* TODO: add sRGB override to pipe_sampler_state ? */
647 update_textures_and_samplers(struct NineDevice9
*device
)
649 struct pipe_context
*pipe
= device
->pipe
;
650 struct nine_state
*state
= &device
->state
;
651 struct pipe_sampler_view
*view
[NINE_MAX_SAMPLERS
];
652 unsigned num_textures
;
654 boolean commit_samplers
;
656 /* TODO: Can we reduce iterations here ? */
658 commit_samplers
= FALSE
;
659 for (num_textures
= 0, i
= 0; i
< NINE_MAX_SAMPLERS_PS
; ++i
) {
660 const unsigned s
= NINE_SAMPLER_PS(i
);
662 if (!state
->texture
[s
]) {
665 if (state
->ps
&& state
->ps
->sampler_mask
& (1 << i
))
666 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
670 sRGB
= state
->samp
[s
][D3DSAMP_SRGBTEXTURE
] ? 1 : 0;
672 view
[i
] = NineBaseTexture9_GetSamplerView(state
->texture
[s
], sRGB
);
673 num_textures
= i
+ 1;
675 if (update_sampler_derived(state
, s
) || (state
->changed
.sampler
[s
] & 0x05fe)) {
676 state
->changed
.sampler
[s
] = 0;
677 commit_samplers
= TRUE
;
678 nine_convert_sampler_state(device
->cso
, s
, state
->samp
[s
]);
681 if (state
->changed
.texture
& NINE_PS_SAMPLERS_MASK
|| state
->changed
.srgb
)
682 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
686 cso_single_sampler_done(device
->cso
, PIPE_SHADER_FRAGMENT
);
688 commit_samplers
= FALSE
;
689 for (num_textures
= 0, i
= 0; i
< NINE_MAX_SAMPLERS_VS
; ++i
) {
690 const unsigned s
= NINE_SAMPLER_VS(i
);
692 if (!state
->texture
[s
]) {
695 if (state
->vs
&& state
->vs
->sampler_mask
& (1 << i
))
696 WARN_ONCE("FIXME: unbound sampler should return alpha=1\n");
700 sRGB
= state
->samp
[s
][D3DSAMP_SRGBTEXTURE
] ? 1 : 0;
702 view
[i
] = NineBaseTexture9_GetSamplerView(state
->texture
[s
], sRGB
);
703 num_textures
= i
+ 1;
705 if (update_sampler_derived(state
, s
) || (state
->changed
.sampler
[s
] & 0x05fe)) {
706 state
->changed
.sampler
[s
] = 0;
707 commit_samplers
= TRUE
;
708 nine_convert_sampler_state(device
->cso
, s
, state
->samp
[s
]);
711 if (state
->changed
.texture
& NINE_VS_SAMPLERS_MASK
|| state
->changed
.srgb
)
712 pipe
->set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, 0,
716 cso_single_sampler_done(device
->cso
, PIPE_SHADER_VERTEX
);
718 state
->changed
.srgb
= FALSE
;
719 state
->changed
.texture
= 0;
723 #define NINE_STATE_FREQ_GROUP_0 \
725 NINE_STATE_VIEWPORT | \
726 NINE_STATE_SCISSOR | \
729 NINE_STATE_RASTERIZER | \
732 NINE_STATE_BLEND_COLOR | \
733 NINE_STATE_STENCIL_REF | \
734 NINE_STATE_SAMPLE_MASK)
736 #define NINE_STATE_FREQ_GROUP_1 ~NINE_STATE_FREQ_GROUP_0
738 #define NINE_STATE_SHADER_VARIANT_GROUP \
739 (NINE_STATE_TEXTURE | \
744 nine_update_state(struct NineDevice9
*device
, uint32_t mask
)
746 struct pipe_context
*pipe
= device
->pipe
;
747 struct nine_state
*state
= &device
->state
;
750 DBG("changed state groups: %x | %x\n",
751 state
->changed
.group
& NINE_STATE_FREQ_GROUP_0
,
752 state
->changed
.group
& NINE_STATE_FREQ_GROUP_1
);
754 /* NOTE: We may want to use the cso cache for everything, or let
755 * NineDevice9.RestoreNonCSOState actually set the states, then we wouldn't
756 * have to care about state being clobbered here and could merge this back
757 * into update_textures. Except, we also need to re-validate textures that
758 * may be dirty anyway, even if no texture bindings changed.
760 validate_textures(device
); /* may clobber state */
762 /* ff_update may change VS/PS dirty bits */
763 if ((mask
& NINE_STATE_FF
) && unlikely(!state
->vs
|| !state
->ps
))
764 nine_ff_update(device
);
765 group
= state
->changed
.group
& mask
;
767 if (group
& NINE_STATE_SHADER_VARIANT_GROUP
)
768 group
|= update_shader_variant_keys(device
);
770 if (group
& NINE_STATE_FREQ_GROUP_0
) {
771 if (group
& NINE_STATE_FB
)
772 group
= update_framebuffer(device
) & mask
;
773 if (group
& NINE_STATE_VIEWPORT
)
774 update_viewport(device
);
775 if (group
& NINE_STATE_SCISSOR
)
776 update_scissor(device
);
778 if (group
& NINE_STATE_DSA
)
780 if (group
& NINE_STATE_BLEND
)
781 update_blend(device
);
783 if (group
& NINE_STATE_VS
)
784 group
|= update_vs(device
);
786 if (group
& NINE_STATE_RASTERIZER
)
787 update_rasterizer(device
);
789 if (group
& NINE_STATE_PS
)
790 group
|= update_ps(device
);
792 if (group
& NINE_STATE_BLEND_COLOR
) {
793 struct pipe_blend_color color
;
794 d3dcolor_to_rgba(&color
.color
[0], state
->rs
[D3DRS_BLENDFACTOR
]);
795 pipe
->set_blend_color(pipe
, &color
);
797 if (group
& NINE_STATE_SAMPLE_MASK
) {
798 pipe
->set_sample_mask(pipe
, state
->rs
[D3DRS_MULTISAMPLEMASK
]);
800 if (group
& NINE_STATE_STENCIL_REF
) {
801 struct pipe_stencil_ref ref
;
802 ref
.ref_value
[0] = state
->rs
[D3DRS_STENCILREF
];
803 ref
.ref_value
[1] = ref
.ref_value
[0];
804 pipe
->set_stencil_ref(pipe
, &ref
);
808 if (state
->changed
.ucp
) {
809 pipe
->set_clip_state(pipe
, &state
->clip
);
810 state
->changed
.ucp
= 0;
813 if (group
& (NINE_STATE_FREQ_GROUP_1
| NINE_STATE_VS
)) {
814 if (group
& (NINE_STATE_TEXTURE
| NINE_STATE_SAMPLER
))
815 update_textures_and_samplers(device
);
817 if (group
& NINE_STATE_IDXBUF
)
818 update_index_buffer(device
);
820 if ((group
& (NINE_STATE_VDECL
| NINE_STATE_VS
)) ||
821 state
->changed
.stream_freq
& ~1)
822 update_vertex_elements(device
);
824 if (device
->prefer_user_constbuf
) {
825 if ((group
& (NINE_STATE_VS_CONST
| NINE_STATE_VS
)) && state
->vs
)
826 update_vs_constants_userbuf(device
);
827 if ((group
& (NINE_STATE_PS_CONST
| NINE_STATE_PS
)) && state
->ps
)
828 update_ps_constants_userbuf(device
);
830 if ((group
& NINE_STATE_VS_CONST
) && state
->vs
)
831 update_constants(device
, PIPE_SHADER_VERTEX
);
832 if ((group
& NINE_STATE_PS_CONST
) && state
->ps
)
833 update_constants(device
, PIPE_SHADER_FRAGMENT
);
836 if (state
->changed
.vtxbuf
)
837 update_vertex_buffers(device
);
839 device
->state
.changed
.group
&= ~mask
|
840 (NINE_STATE_FF
| NINE_STATE_VS_CONST
| NINE_STATE_PS_CONST
);
848 static const DWORD nine_render_state_defaults
[NINED3DRS_LAST
+ 1] =
850 /* [D3DRS_ZENABLE] = D3DZB_TRUE; wine: auto_depth_stencil */
851 [D3DRS_ZENABLE
] = D3DZB_FALSE
,
852 [D3DRS_FILLMODE
] = D3DFILL_SOLID
,
853 [D3DRS_SHADEMODE
] = D3DSHADE_GOURAUD
,
854 /* [D3DRS_LINEPATTERN] = 0x00000000, */
855 [D3DRS_ZWRITEENABLE
] = TRUE
,
856 [D3DRS_ALPHATESTENABLE
] = FALSE
,
857 [D3DRS_LASTPIXEL
] = TRUE
,
858 [D3DRS_SRCBLEND
] = D3DBLEND_ONE
,
859 [D3DRS_DESTBLEND
] = D3DBLEND_ZERO
,
860 [D3DRS_CULLMODE
] = D3DCULL_CCW
,
861 [D3DRS_ZFUNC
] = D3DCMP_LESSEQUAL
,
862 [D3DRS_ALPHAFUNC
] = D3DCMP_ALWAYS
,
863 [D3DRS_ALPHAREF
] = 0,
864 [D3DRS_DITHERENABLE
] = FALSE
,
865 [D3DRS_ALPHABLENDENABLE
] = FALSE
,
866 [D3DRS_FOGENABLE
] = FALSE
,
867 [D3DRS_SPECULARENABLE
] = FALSE
,
868 /* [D3DRS_ZVISIBLE] = 0, */
869 [D3DRS_FOGCOLOR
] = 0,
870 [D3DRS_FOGTABLEMODE
] = D3DFOG_NONE
,
871 [D3DRS_FOGSTART
] = 0x00000000,
872 [D3DRS_FOGEND
] = 0x3F800000,
873 [D3DRS_FOGDENSITY
] = 0x3F800000,
874 /* [D3DRS_EDGEANTIALIAS] = FALSE, */
875 [D3DRS_RANGEFOGENABLE
] = FALSE
,
876 [D3DRS_STENCILENABLE
] = FALSE
,
877 [D3DRS_STENCILFAIL
] = D3DSTENCILOP_KEEP
,
878 [D3DRS_STENCILZFAIL
] = D3DSTENCILOP_KEEP
,
879 [D3DRS_STENCILPASS
] = D3DSTENCILOP_KEEP
,
880 [D3DRS_STENCILREF
] = 0,
881 [D3DRS_STENCILMASK
] = 0xFFFFFFFF,
882 [D3DRS_STENCILFUNC
] = D3DCMP_ALWAYS
,
883 [D3DRS_STENCILWRITEMASK
] = 0xFFFFFFFF,
884 [D3DRS_TEXTUREFACTOR
] = 0xFFFFFFFF,
893 [D3DRS_CLIPPING
] = TRUE
,
894 [D3DRS_LIGHTING
] = TRUE
,
896 [D3DRS_FOGVERTEXMODE
] = D3DFOG_NONE
,
897 [D3DRS_COLORVERTEX
] = TRUE
,
898 [D3DRS_LOCALVIEWER
] = TRUE
,
899 [D3DRS_NORMALIZENORMALS
] = FALSE
,
900 [D3DRS_DIFFUSEMATERIALSOURCE
] = D3DMCS_COLOR1
,
901 [D3DRS_SPECULARMATERIALSOURCE
] = D3DMCS_COLOR2
,
902 [D3DRS_AMBIENTMATERIALSOURCE
] = D3DMCS_MATERIAL
,
903 [D3DRS_EMISSIVEMATERIALSOURCE
] = D3DMCS_MATERIAL
,
904 [D3DRS_VERTEXBLEND
] = D3DVBF_DISABLE
,
905 [D3DRS_CLIPPLANEENABLE
] = 0,
906 /* [D3DRS_SOFTWAREVERTEXPROCESSING] = FALSE, */
907 [D3DRS_POINTSIZE
] = 0x3F800000,
908 [D3DRS_POINTSIZE_MIN
] = 0x3F800000,
909 [D3DRS_POINTSPRITEENABLE
] = FALSE
,
910 [D3DRS_POINTSCALEENABLE
] = FALSE
,
911 [D3DRS_POINTSCALE_A
] = 0x3F800000,
912 [D3DRS_POINTSCALE_B
] = 0x00000000,
913 [D3DRS_POINTSCALE_C
] = 0x00000000,
914 [D3DRS_MULTISAMPLEANTIALIAS
] = TRUE
,
915 [D3DRS_MULTISAMPLEMASK
] = 0xFFFFFFFF,
916 [D3DRS_PATCHEDGESTYLE
] = D3DPATCHEDGE_DISCRETE
,
917 /* [D3DRS_PATCHSEGMENTS] = 0x3F800000, */
918 [D3DRS_DEBUGMONITORTOKEN
] = 0xDEADCAFE,
919 [D3DRS_POINTSIZE_MAX
] = 0x3F800000, /* depends on cap */
920 [D3DRS_INDEXEDVERTEXBLENDENABLE
] = FALSE
,
921 [D3DRS_COLORWRITEENABLE
] = 0x0000000f,
922 [D3DRS_TWEENFACTOR
] = 0x00000000,
923 [D3DRS_BLENDOP
] = D3DBLENDOP_ADD
,
924 [D3DRS_POSITIONDEGREE
] = D3DDEGREE_CUBIC
,
925 [D3DRS_NORMALDEGREE
] = D3DDEGREE_LINEAR
,
926 [D3DRS_SCISSORTESTENABLE
] = FALSE
,
927 [D3DRS_SLOPESCALEDEPTHBIAS
] = 0,
928 [D3DRS_MINTESSELLATIONLEVEL
] = 0x3F800000,
929 [D3DRS_MAXTESSELLATIONLEVEL
] = 0x3F800000,
930 [D3DRS_ANTIALIASEDLINEENABLE
] = FALSE
,
931 [D3DRS_ADAPTIVETESS_X
] = 0x00000000,
932 [D3DRS_ADAPTIVETESS_Y
] = 0x00000000,
933 [D3DRS_ADAPTIVETESS_Z
] = 0x3F800000,
934 [D3DRS_ADAPTIVETESS_W
] = 0x00000000,
935 [D3DRS_ENABLEADAPTIVETESSELLATION
] = FALSE
,
936 [D3DRS_TWOSIDEDSTENCILMODE
] = FALSE
,
937 [D3DRS_CCW_STENCILFAIL
] = D3DSTENCILOP_KEEP
,
938 [D3DRS_CCW_STENCILZFAIL
] = D3DSTENCILOP_KEEP
,
939 [D3DRS_CCW_STENCILPASS
] = D3DSTENCILOP_KEEP
,
940 [D3DRS_CCW_STENCILFUNC
] = D3DCMP_ALWAYS
,
941 [D3DRS_COLORWRITEENABLE1
] = 0x0000000F,
942 [D3DRS_COLORWRITEENABLE2
] = 0x0000000F,
943 [D3DRS_COLORWRITEENABLE3
] = 0x0000000F,
944 [D3DRS_BLENDFACTOR
] = 0xFFFFFFFF,
945 [D3DRS_SRGBWRITEENABLE
] = 0,
946 [D3DRS_DEPTHBIAS
] = 0,
955 [D3DRS_SEPARATEALPHABLENDENABLE
] = FALSE
,
956 [D3DRS_SRCBLENDALPHA
] = D3DBLEND_ONE
,
957 [D3DRS_DESTBLENDALPHA
] = D3DBLEND_ZERO
,
958 [D3DRS_BLENDOPALPHA
] = D3DBLENDOP_ADD
,
959 [NINED3DRS_VSPOINTSIZE
] = FALSE
,
960 [NINED3DRS_RTMASK
] = 0xf
962 static const DWORD nine_tex_stage_state_defaults
[NINED3DTSS_LAST
+ 1] =
964 [D3DTSS_COLOROP
] = D3DTOP_DISABLE
,
965 [D3DTSS_ALPHAOP
] = D3DTOP_DISABLE
,
966 [D3DTSS_COLORARG1
] = D3DTA_TEXTURE
,
967 [D3DTSS_COLORARG2
] = D3DTA_CURRENT
,
968 [D3DTSS_COLORARG0
] = D3DTA_CURRENT
,
969 [D3DTSS_ALPHAARG1
] = D3DTA_TEXTURE
,
970 [D3DTSS_ALPHAARG2
] = D3DTA_CURRENT
,
971 [D3DTSS_ALPHAARG0
] = D3DTA_CURRENT
,
972 [D3DTSS_RESULTARG
] = D3DTA_CURRENT
,
973 [D3DTSS_BUMPENVMAT00
] = 0,
974 [D3DTSS_BUMPENVMAT01
] = 0,
975 [D3DTSS_BUMPENVMAT10
] = 0,
976 [D3DTSS_BUMPENVMAT11
] = 0,
977 [D3DTSS_BUMPENVLSCALE
] = 0,
978 [D3DTSS_BUMPENVLOFFSET
] = 0,
979 [D3DTSS_TEXCOORDINDEX
] = 0,
980 [D3DTSS_TEXTURETRANSFORMFLAGS
] = D3DTTFF_DISABLE
,
982 static const DWORD nine_samp_state_defaults
[NINED3DSAMP_LAST
+ 1] =
984 [D3DSAMP_ADDRESSU
] = D3DTADDRESS_WRAP
,
985 [D3DSAMP_ADDRESSV
] = D3DTADDRESS_WRAP
,
986 [D3DSAMP_ADDRESSW
] = D3DTADDRESS_WRAP
,
987 [D3DSAMP_BORDERCOLOR
] = 0,
988 [D3DSAMP_MAGFILTER
] = D3DTEXF_POINT
,
989 [D3DSAMP_MINFILTER
] = D3DTEXF_POINT
,
990 [D3DSAMP_MIPFILTER
] = D3DTEXF_NONE
,
991 [D3DSAMP_MIPMAPLODBIAS
] = 0,
992 [D3DSAMP_MAXMIPLEVEL
] = 0,
993 [D3DSAMP_MAXANISOTROPY
] = 1,
994 [D3DSAMP_SRGBTEXTURE
] = 0,
995 [D3DSAMP_ELEMENTINDEX
] = 0,
996 [D3DSAMP_DMAPOFFSET
] = 0,
997 [NINED3DSAMP_MINLOD
] = 0,
998 [NINED3DSAMP_SHADOW
] = 0
1001 nine_state_set_defaults(struct NineDevice9
*device
, const D3DCAPS9
*caps
,
1004 struct nine_state
*state
= &device
->state
;
1007 /* Initialize defaults.
1009 memcpy(state
->rs
, nine_render_state_defaults
, sizeof(state
->rs
));
1011 for (s
= 0; s
< Elements(state
->ff
.tex_stage
); ++s
) {
1012 memcpy(&state
->ff
.tex_stage
[s
], nine_tex_stage_state_defaults
,
1013 sizeof(state
->ff
.tex_stage
[s
]));
1014 state
->ff
.tex_stage
[s
][D3DTSS_TEXCOORDINDEX
] = s
;
1016 state
->ff
.tex_stage
[0][D3DTSS_COLOROP
] = D3DTOP_MODULATE
;
1017 state
->ff
.tex_stage
[0][D3DTSS_ALPHAOP
] = D3DTOP_SELECTARG1
;
1019 for (s
= 0; s
< Elements(state
->samp
); ++s
) {
1020 memcpy(&state
->samp
[s
], nine_samp_state_defaults
,
1021 sizeof(state
->samp
[s
]));
1024 if (state
->vs_const_f
)
1025 memset(state
->vs_const_f
, 0, device
->vs_const_size
);
1026 if (state
->ps_const_f
)
1027 memset(state
->ps_const_f
, 0, device
->ps_const_size
);
1029 /* Cap dependent initial state:
1031 state
->rs
[D3DRS_POINTSIZE_MAX
] = fui(caps
->MaxPointSize
);
1033 /* Set changed flags to initialize driver.
1035 state
->changed
.group
= NINE_STATE_ALL
;
1037 state
->ff
.changed
.transform
[0] = ~0;
1038 state
->ff
.changed
.transform
[D3DTS_WORLD
/ 32] |= 1 << (D3DTS_WORLD
% 32);
1041 state
->viewport
.MinZ
= 0.0f
;
1042 state
->viewport
.MaxZ
= 1.0f
;
1045 for (s
= 0; s
< Elements(state
->changed
.sampler
); ++s
)
1046 state
->changed
.sampler
[s
] = ~0;
1050 nine_state_clear(struct nine_state
*state
, const boolean device
)
1054 for (i
= 0; i
< Elements(state
->rt
); ++i
)
1055 nine_bind(&state
->rt
[i
], NULL
);
1056 nine_bind(&state
->ds
, NULL
);
1057 nine_bind(&state
->vs
, NULL
);
1058 nine_bind(&state
->ps
, NULL
);
1059 nine_bind(&state
->vdecl
, NULL
);
1060 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; ++i
)
1061 nine_bind(&state
->stream
[i
], NULL
);
1062 nine_bind(&state
->idxbuf
, NULL
);
1063 for (i
= 0; i
< NINE_MAX_SAMPLERS
; ++i
) {
1065 state
->texture
[i
] &&
1066 --state
->texture
[i
]->bind_count
== 0)
1067 list_delinit(&state
->texture
[i
]->list
);
1068 nine_bind(&state
->texture
[i
], NULL
);
1073 static const DWORD nine_render_states_pixel[] =
1075 D3DRS_ALPHABLENDENABLE,
1078 D3DRS_ALPHATESTENABLE,
1079 D3DRS_ANTIALIASEDLINEENABLE,
1083 D3DRS_CCW_STENCILFAIL,
1084 D3DRS_CCW_STENCILPASS,
1085 D3DRS_CCW_STENCILZFAIL,
1086 D3DRS_COLORWRITEENABLE,
1087 D3DRS_COLORWRITEENABLE1,
1088 D3DRS_COLORWRITEENABLE2,
1089 D3DRS_COLORWRITEENABLE3,
1092 D3DRS_DESTBLENDALPHA,
1099 D3DRS_SCISSORTESTENABLE,
1100 D3DRS_SEPARATEALPHABLENDENABLE,
1102 D3DRS_SLOPESCALEDEPTHBIAS,
1104 D3DRS_SRCBLENDALPHA,
1105 D3DRS_SRGBWRITEENABLE,
1106 D3DRS_STENCILENABLE,
1112 D3DRS_STENCILWRITEMASK,
1114 D3DRS_TEXTUREFACTOR,
1115 D3DRS_TWOSIDEDSTENCILMODE,
1137 const uint32_t nine_render_states_pixel
[(NINED3DRS_LAST
+ 31) / 32] =
1139 0x0f99c380, 0x1ff00070, 0x00000000, 0x00000000,
1140 0x000000ff, 0xde01c900, 0x0003ffcf
1144 static const DWORD nine_render_states_vertex[] =
1146 D3DRS_ADAPTIVETESS_W,
1147 D3DRS_ADAPTIVETESS_X,
1148 D3DRS_ADAPTIVETESS_Y,
1149 D3DRS_ADAPTIVETESS_Z,
1151 D3DRS_AMBIENTMATERIALSOURCE,
1153 D3DRS_CLIPPLANEENABLE,
1156 D3DRS_DIFFUSEMATERIALSOURCE,
1157 D3DRS_EMISSIVEMATERIALSOURCE,
1158 D3DRS_ENABLEADAPTIVETESSELLATION,
1165 D3DRS_FOGVERTEXMODE,
1166 D3DRS_INDEXEDVERTEXBLENDENABLE,
1169 D3DRS_MAXTESSELLATIONLEVEL,
1170 D3DRS_MINTESSELLATIONLEVEL,
1171 D3DRS_MULTISAMPLEANTIALIAS,
1172 D3DRS_MULTISAMPLEMASK,
1174 D3DRS_NORMALIZENORMALS,
1175 D3DRS_PATCHEDGESTYLE,
1179 D3DRS_POINTSCALEENABLE,
1181 D3DRS_POINTSIZE_MAX,
1182 D3DRS_POINTSIZE_MIN,
1183 D3DRS_POINTSPRITEENABLE,
1184 D3DRS_POSITIONDEGREE,
1185 D3DRS_RANGEFOGENABLE,
1187 D3DRS_SPECULARENABLE,
1188 D3DRS_SPECULARMATERIALSOURCE,
1193 const uint32_t nine_render_states_vertex
[(NINED3DRS_LAST
+ 31) / 32] =
1195 0x30400200, 0x0001007c, 0x00000000, 0x00000000,
1196 0xfd9efb00, 0x01fc34cf, 0x00000000
1199 /* TODO: put in the right values */
1200 const uint32_t nine_render_state_group
[NINED3DRS_LAST
+ 1] =
1202 [D3DRS_ZENABLE
] = NINE_STATE_DSA
,
1203 [D3DRS_FILLMODE
] = NINE_STATE_RASTERIZER
,
1204 [D3DRS_SHADEMODE
] = NINE_STATE_RASTERIZER
,
1205 [D3DRS_ZWRITEENABLE
] = NINE_STATE_DSA
,
1206 [D3DRS_ALPHATESTENABLE
] = NINE_STATE_DSA
,
1207 [D3DRS_LASTPIXEL
] = NINE_STATE_RASTERIZER
,
1208 [D3DRS_SRCBLEND
] = NINE_STATE_BLEND
,
1209 [D3DRS_DESTBLEND
] = NINE_STATE_BLEND
,
1210 [D3DRS_CULLMODE
] = NINE_STATE_RASTERIZER
,
1211 [D3DRS_ZFUNC
] = NINE_STATE_DSA
,
1212 [D3DRS_ALPHAREF
] = NINE_STATE_DSA
,
1213 [D3DRS_ALPHAFUNC
] = NINE_STATE_DSA
,
1214 [D3DRS_DITHERENABLE
] = NINE_STATE_RASTERIZER
,
1215 [D3DRS_ALPHABLENDENABLE
] = NINE_STATE_BLEND
,
1216 [D3DRS_FOGENABLE
] = NINE_STATE_FF_OTHER
,
1217 [D3DRS_SPECULARENABLE
] = NINE_STATE_FF_LIGHTING
,
1218 [D3DRS_FOGCOLOR
] = NINE_STATE_FF_OTHER
,
1219 [D3DRS_FOGTABLEMODE
] = NINE_STATE_FF_OTHER
,
1220 [D3DRS_FOGSTART
] = NINE_STATE_FF_OTHER
,
1221 [D3DRS_FOGEND
] = NINE_STATE_FF_OTHER
,
1222 [D3DRS_FOGDENSITY
] = NINE_STATE_FF_OTHER
,
1223 [D3DRS_RANGEFOGENABLE
] = NINE_STATE_FF_OTHER
,
1224 [D3DRS_STENCILENABLE
] = NINE_STATE_DSA
,
1225 [D3DRS_STENCILFAIL
] = NINE_STATE_DSA
,
1226 [D3DRS_STENCILZFAIL
] = NINE_STATE_DSA
,
1227 [D3DRS_STENCILPASS
] = NINE_STATE_DSA
,
1228 [D3DRS_STENCILFUNC
] = NINE_STATE_DSA
,
1229 [D3DRS_STENCILREF
] = NINE_STATE_STENCIL_REF
,
1230 [D3DRS_STENCILMASK
] = NINE_STATE_DSA
,
1231 [D3DRS_STENCILWRITEMASK
] = NINE_STATE_DSA
,
1232 [D3DRS_TEXTUREFACTOR
] = NINE_STATE_FF_PSSTAGES
,
1233 [D3DRS_WRAP0
] = NINE_STATE_UNHANDLED
, /* cylindrical wrap is crazy */
1234 [D3DRS_WRAP1
] = NINE_STATE_UNHANDLED
,
1235 [D3DRS_WRAP2
] = NINE_STATE_UNHANDLED
,
1236 [D3DRS_WRAP3
] = NINE_STATE_UNHANDLED
,
1237 [D3DRS_WRAP4
] = NINE_STATE_UNHANDLED
,
1238 [D3DRS_WRAP5
] = NINE_STATE_UNHANDLED
,
1239 [D3DRS_WRAP6
] = NINE_STATE_UNHANDLED
,
1240 [D3DRS_WRAP7
] = NINE_STATE_UNHANDLED
,
1241 [D3DRS_CLIPPING
] = 0, /* software vertex processing only */
1242 [D3DRS_LIGHTING
] = NINE_STATE_FF_LIGHTING
,
1243 [D3DRS_AMBIENT
] = NINE_STATE_FF_LIGHTING
| NINE_STATE_FF_MATERIAL
,
1244 [D3DRS_FOGVERTEXMODE
] = NINE_STATE_FF_OTHER
,
1245 [D3DRS_COLORVERTEX
] = NINE_STATE_FF_LIGHTING
,
1246 [D3DRS_LOCALVIEWER
] = NINE_STATE_FF_LIGHTING
,
1247 [D3DRS_NORMALIZENORMALS
] = NINE_STATE_FF_OTHER
,
1248 [D3DRS_DIFFUSEMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1249 [D3DRS_SPECULARMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1250 [D3DRS_AMBIENTMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1251 [D3DRS_EMISSIVEMATERIALSOURCE
] = NINE_STATE_FF_LIGHTING
,
1252 [D3DRS_VERTEXBLEND
] = NINE_STATE_FF_OTHER
,
1253 [D3DRS_CLIPPLANEENABLE
] = NINE_STATE_RASTERIZER
,
1254 [D3DRS_POINTSIZE
] = NINE_STATE_RASTERIZER
,
1255 [D3DRS_POINTSIZE_MIN
] = NINE_STATE_MISC_CONST
,
1256 [D3DRS_POINTSPRITEENABLE
] = NINE_STATE_RASTERIZER
,
1257 [D3DRS_POINTSCALEENABLE
] = NINE_STATE_FF_OTHER
,
1258 [D3DRS_POINTSCALE_A
] = NINE_STATE_FF_OTHER
,
1259 [D3DRS_POINTSCALE_B
] = NINE_STATE_FF_OTHER
,
1260 [D3DRS_POINTSCALE_C
] = NINE_STATE_FF_OTHER
,
1261 [D3DRS_MULTISAMPLEANTIALIAS
] = NINE_STATE_RASTERIZER
,
1262 [D3DRS_MULTISAMPLEMASK
] = NINE_STATE_SAMPLE_MASK
,
1263 [D3DRS_PATCHEDGESTYLE
] = NINE_STATE_UNHANDLED
,
1264 [D3DRS_DEBUGMONITORTOKEN
] = NINE_STATE_UNHANDLED
,
1265 [D3DRS_POINTSIZE_MAX
] = NINE_STATE_MISC_CONST
,
1266 [D3DRS_INDEXEDVERTEXBLENDENABLE
] = NINE_STATE_FF_OTHER
,
1267 [D3DRS_COLORWRITEENABLE
] = NINE_STATE_BLEND
,
1268 [D3DRS_TWEENFACTOR
] = NINE_STATE_FF_OTHER
,
1269 [D3DRS_BLENDOP
] = NINE_STATE_BLEND
,
1270 [D3DRS_POSITIONDEGREE
] = NINE_STATE_UNHANDLED
,
1271 [D3DRS_NORMALDEGREE
] = NINE_STATE_UNHANDLED
,
1272 [D3DRS_SCISSORTESTENABLE
] = NINE_STATE_RASTERIZER
,
1273 [D3DRS_SLOPESCALEDEPTHBIAS
] = NINE_STATE_RASTERIZER
,
1274 [D3DRS_ANTIALIASEDLINEENABLE
] = NINE_STATE_RASTERIZER
,
1275 [D3DRS_MINTESSELLATIONLEVEL
] = NINE_STATE_UNHANDLED
,
1276 [D3DRS_MAXTESSELLATIONLEVEL
] = NINE_STATE_UNHANDLED
,
1277 [D3DRS_ADAPTIVETESS_X
] = NINE_STATE_UNHANDLED
,
1278 [D3DRS_ADAPTIVETESS_Y
] = NINE_STATE_UNHANDLED
,
1279 [D3DRS_ADAPTIVETESS_Z
] = NINE_STATE_UNHANDLED
,
1280 [D3DRS_ADAPTIVETESS_W
] = NINE_STATE_UNHANDLED
,
1281 [D3DRS_ENABLEADAPTIVETESSELLATION
] = NINE_STATE_UNHANDLED
,
1282 [D3DRS_TWOSIDEDSTENCILMODE
] = NINE_STATE_DSA
,
1283 [D3DRS_CCW_STENCILFAIL
] = NINE_STATE_DSA
,
1284 [D3DRS_CCW_STENCILZFAIL
] = NINE_STATE_DSA
,
1285 [D3DRS_CCW_STENCILPASS
] = NINE_STATE_DSA
,
1286 [D3DRS_CCW_STENCILFUNC
] = NINE_STATE_DSA
,
1287 [D3DRS_COLORWRITEENABLE1
] = NINE_STATE_BLEND
,
1288 [D3DRS_COLORWRITEENABLE2
] = NINE_STATE_BLEND
,
1289 [D3DRS_COLORWRITEENABLE3
] = NINE_STATE_BLEND
,
1290 [D3DRS_BLENDFACTOR
] = NINE_STATE_BLEND_COLOR
,
1291 [D3DRS_SRGBWRITEENABLE
] = NINE_STATE_FB
,
1292 [D3DRS_DEPTHBIAS
] = NINE_STATE_RASTERIZER
,
1293 [D3DRS_WRAP8
] = NINE_STATE_UNHANDLED
, /* cylwrap has to be done via GP */
1294 [D3DRS_WRAP9
] = NINE_STATE_UNHANDLED
,
1295 [D3DRS_WRAP10
] = NINE_STATE_UNHANDLED
,
1296 [D3DRS_WRAP11
] = NINE_STATE_UNHANDLED
,
1297 [D3DRS_WRAP12
] = NINE_STATE_UNHANDLED
,
1298 [D3DRS_WRAP13
] = NINE_STATE_UNHANDLED
,
1299 [D3DRS_WRAP14
] = NINE_STATE_UNHANDLED
,
1300 [D3DRS_WRAP15
] = NINE_STATE_UNHANDLED
,
1301 [D3DRS_SEPARATEALPHABLENDENABLE
] = NINE_STATE_BLEND
,
1302 [D3DRS_SRCBLENDALPHA
] = NINE_STATE_BLEND
,
1303 [D3DRS_DESTBLENDALPHA
] = NINE_STATE_BLEND
,
1304 [D3DRS_BLENDOPALPHA
] = NINE_STATE_BLEND
1308 nine_state_access_transform(struct nine_state
*state
, D3DTRANSFORMSTATETYPE t
,
1311 static D3DMATRIX Identity
= { .m
[0] = { 1, 0, 0, 0 },
1312 .m
[1] = { 0, 1, 0, 0 },
1313 .m
[2] = { 0, 0, 1, 0 },
1314 .m
[3] = { 0, 0, 0, 1 } };
1318 case D3DTS_VIEW
: index
= 0; break;
1319 case D3DTS_PROJECTION
: index
= 1; break;
1320 case D3DTS_TEXTURE0
: index
= 2; break;
1321 case D3DTS_TEXTURE1
: index
= 3; break;
1322 case D3DTS_TEXTURE2
: index
= 4; break;
1323 case D3DTS_TEXTURE3
: index
= 5; break;
1324 case D3DTS_TEXTURE4
: index
= 6; break;
1325 case D3DTS_TEXTURE5
: index
= 7; break;
1326 case D3DTS_TEXTURE6
: index
= 8; break;
1327 case D3DTS_TEXTURE7
: index
= 9; break;
1329 if (!(t
>= D3DTS_WORLDMATRIX(0) && t
<= D3DTS_WORLDMATRIX(255)))
1331 index
= 10 + (t
- D3DTS_WORLDMATRIX(0));
1335 if (index
>= state
->ff
.num_transforms
) {
1336 unsigned N
= index
+ 1;
1337 unsigned n
= state
->ff
.num_transforms
;
1341 state
->ff
.transform
= REALLOC(state
->ff
.transform
,
1342 n
* sizeof(D3DMATRIX
),
1343 N
* sizeof(D3DMATRIX
));
1345 state
->ff
.transform
[n
] = Identity
;
1346 state
->ff
.num_transforms
= N
;
1348 return &state
->ff
.transform
[index
];
1351 #define D3DRS_TO_STRING_CASE(n) case D3DRS_##n: return "D3DRS_"#n
1352 const char *nine_d3drs_to_string(DWORD State
)
1355 D3DRS_TO_STRING_CASE(ZENABLE
);
1356 D3DRS_TO_STRING_CASE(FILLMODE
);
1357 D3DRS_TO_STRING_CASE(SHADEMODE
);
1358 D3DRS_TO_STRING_CASE(ZWRITEENABLE
);
1359 D3DRS_TO_STRING_CASE(ALPHATESTENABLE
);
1360 D3DRS_TO_STRING_CASE(LASTPIXEL
);
1361 D3DRS_TO_STRING_CASE(SRCBLEND
);
1362 D3DRS_TO_STRING_CASE(DESTBLEND
);
1363 D3DRS_TO_STRING_CASE(CULLMODE
);
1364 D3DRS_TO_STRING_CASE(ZFUNC
);
1365 D3DRS_TO_STRING_CASE(ALPHAREF
);
1366 D3DRS_TO_STRING_CASE(ALPHAFUNC
);
1367 D3DRS_TO_STRING_CASE(DITHERENABLE
);
1368 D3DRS_TO_STRING_CASE(ALPHABLENDENABLE
);
1369 D3DRS_TO_STRING_CASE(FOGENABLE
);
1370 D3DRS_TO_STRING_CASE(SPECULARENABLE
);
1371 D3DRS_TO_STRING_CASE(FOGCOLOR
);
1372 D3DRS_TO_STRING_CASE(FOGTABLEMODE
);
1373 D3DRS_TO_STRING_CASE(FOGSTART
);
1374 D3DRS_TO_STRING_CASE(FOGEND
);
1375 D3DRS_TO_STRING_CASE(FOGDENSITY
);
1376 D3DRS_TO_STRING_CASE(RANGEFOGENABLE
);
1377 D3DRS_TO_STRING_CASE(STENCILENABLE
);
1378 D3DRS_TO_STRING_CASE(STENCILFAIL
);
1379 D3DRS_TO_STRING_CASE(STENCILZFAIL
);
1380 D3DRS_TO_STRING_CASE(STENCILPASS
);
1381 D3DRS_TO_STRING_CASE(STENCILFUNC
);
1382 D3DRS_TO_STRING_CASE(STENCILREF
);
1383 D3DRS_TO_STRING_CASE(STENCILMASK
);
1384 D3DRS_TO_STRING_CASE(STENCILWRITEMASK
);
1385 D3DRS_TO_STRING_CASE(TEXTUREFACTOR
);
1386 D3DRS_TO_STRING_CASE(WRAP0
);
1387 D3DRS_TO_STRING_CASE(WRAP1
);
1388 D3DRS_TO_STRING_CASE(WRAP2
);
1389 D3DRS_TO_STRING_CASE(WRAP3
);
1390 D3DRS_TO_STRING_CASE(WRAP4
);
1391 D3DRS_TO_STRING_CASE(WRAP5
);
1392 D3DRS_TO_STRING_CASE(WRAP6
);
1393 D3DRS_TO_STRING_CASE(WRAP7
);
1394 D3DRS_TO_STRING_CASE(CLIPPING
);
1395 D3DRS_TO_STRING_CASE(LIGHTING
);
1396 D3DRS_TO_STRING_CASE(AMBIENT
);
1397 D3DRS_TO_STRING_CASE(FOGVERTEXMODE
);
1398 D3DRS_TO_STRING_CASE(COLORVERTEX
);
1399 D3DRS_TO_STRING_CASE(LOCALVIEWER
);
1400 D3DRS_TO_STRING_CASE(NORMALIZENORMALS
);
1401 D3DRS_TO_STRING_CASE(DIFFUSEMATERIALSOURCE
);
1402 D3DRS_TO_STRING_CASE(SPECULARMATERIALSOURCE
);
1403 D3DRS_TO_STRING_CASE(AMBIENTMATERIALSOURCE
);
1404 D3DRS_TO_STRING_CASE(EMISSIVEMATERIALSOURCE
);
1405 D3DRS_TO_STRING_CASE(VERTEXBLEND
);
1406 D3DRS_TO_STRING_CASE(CLIPPLANEENABLE
);
1407 D3DRS_TO_STRING_CASE(POINTSIZE
);
1408 D3DRS_TO_STRING_CASE(POINTSIZE_MIN
);
1409 D3DRS_TO_STRING_CASE(POINTSPRITEENABLE
);
1410 D3DRS_TO_STRING_CASE(POINTSCALEENABLE
);
1411 D3DRS_TO_STRING_CASE(POINTSCALE_A
);
1412 D3DRS_TO_STRING_CASE(POINTSCALE_B
);
1413 D3DRS_TO_STRING_CASE(POINTSCALE_C
);
1414 D3DRS_TO_STRING_CASE(MULTISAMPLEANTIALIAS
);
1415 D3DRS_TO_STRING_CASE(MULTISAMPLEMASK
);
1416 D3DRS_TO_STRING_CASE(PATCHEDGESTYLE
);
1417 D3DRS_TO_STRING_CASE(DEBUGMONITORTOKEN
);
1418 D3DRS_TO_STRING_CASE(POINTSIZE_MAX
);
1419 D3DRS_TO_STRING_CASE(INDEXEDVERTEXBLENDENABLE
);
1420 D3DRS_TO_STRING_CASE(COLORWRITEENABLE
);
1421 D3DRS_TO_STRING_CASE(TWEENFACTOR
);
1422 D3DRS_TO_STRING_CASE(BLENDOP
);
1423 D3DRS_TO_STRING_CASE(POSITIONDEGREE
);
1424 D3DRS_TO_STRING_CASE(NORMALDEGREE
);
1425 D3DRS_TO_STRING_CASE(SCISSORTESTENABLE
);
1426 D3DRS_TO_STRING_CASE(SLOPESCALEDEPTHBIAS
);
1427 D3DRS_TO_STRING_CASE(ANTIALIASEDLINEENABLE
);
1428 D3DRS_TO_STRING_CASE(MINTESSELLATIONLEVEL
);
1429 D3DRS_TO_STRING_CASE(MAXTESSELLATIONLEVEL
);
1430 D3DRS_TO_STRING_CASE(ADAPTIVETESS_X
);
1431 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Y
);
1432 D3DRS_TO_STRING_CASE(ADAPTIVETESS_Z
);
1433 D3DRS_TO_STRING_CASE(ADAPTIVETESS_W
);
1434 D3DRS_TO_STRING_CASE(ENABLEADAPTIVETESSELLATION
);
1435 D3DRS_TO_STRING_CASE(TWOSIDEDSTENCILMODE
);
1436 D3DRS_TO_STRING_CASE(CCW_STENCILFAIL
);
1437 D3DRS_TO_STRING_CASE(CCW_STENCILZFAIL
);
1438 D3DRS_TO_STRING_CASE(CCW_STENCILPASS
);
1439 D3DRS_TO_STRING_CASE(CCW_STENCILFUNC
);
1440 D3DRS_TO_STRING_CASE(COLORWRITEENABLE1
);
1441 D3DRS_TO_STRING_CASE(COLORWRITEENABLE2
);
1442 D3DRS_TO_STRING_CASE(COLORWRITEENABLE3
);
1443 D3DRS_TO_STRING_CASE(BLENDFACTOR
);
1444 D3DRS_TO_STRING_CASE(SRGBWRITEENABLE
);
1445 D3DRS_TO_STRING_CASE(DEPTHBIAS
);
1446 D3DRS_TO_STRING_CASE(WRAP8
);
1447 D3DRS_TO_STRING_CASE(WRAP9
);
1448 D3DRS_TO_STRING_CASE(WRAP10
);
1449 D3DRS_TO_STRING_CASE(WRAP11
);
1450 D3DRS_TO_STRING_CASE(WRAP12
);
1451 D3DRS_TO_STRING_CASE(WRAP13
);
1452 D3DRS_TO_STRING_CASE(WRAP14
);
1453 D3DRS_TO_STRING_CASE(WRAP15
);
1454 D3DRS_TO_STRING_CASE(SEPARATEALPHABLENDENABLE
);
1455 D3DRS_TO_STRING_CASE(SRCBLENDALPHA
);
1456 D3DRS_TO_STRING_CASE(DESTBLENDALPHA
);
1457 D3DRS_TO_STRING_CASE(BLENDOPALPHA
);