st/nine: Move core of device clear to nine_state
[mesa.git] / src / gallium / state_trackers / nine / device9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "device9.h"
24 #include "stateblock9.h"
25 #include "surface9.h"
26 #include "swapchain9.h"
27 #include "swapchain9ex.h"
28 #include "indexbuffer9.h"
29 #include "vertexbuffer9.h"
30 #include "vertexdeclaration9.h"
31 #include "vertexshader9.h"
32 #include "pixelshader9.h"
33 #include "query9.h"
34 #include "texture9.h"
35 #include "cubetexture9.h"
36 #include "volumetexture9.h"
37 #include "nine_helpers.h"
38 #include "nine_pipe.h"
39 #include "nine_ff.h"
40 #include "nine_dump.h"
41 #include "nine_limits.h"
42
43 #include "pipe/p_screen.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_config.h"
46 #include "util/u_math.h"
47 #include "util/u_inlines.h"
48 #include "util/u_hash_table.h"
49 #include "util/u_format.h"
50 #include "util/u_surface.h"
51 #include "util/u_upload_mgr.h"
52 #include "hud/hud_context.h"
53
54 #include "cso_cache/cso_context.h"
55
56 #define DBG_CHANNEL DBG_DEVICE
57
58 #if defined(PIPE_CC_GCC) && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
59
60 static void nine_setup_fpu()
61 {
62 uint16_t c;
63
64 __asm__ __volatile__ ("fnstcw %0" : "=m" (*&c));
65
66 /* clear the control word */
67 c &= 0xF0C0;
68 /* d3d9 doc/wine tests: mask all exceptions, use single-precision
69 * and round to nearest */
70 c |= 0x003F;
71
72 __asm__ __volatile__ ("fldcw %0" : : "m" (*&c));
73 }
74
75 #else
76
77 static void nine_setup_fpu(void)
78 {
79 WARN_ONCE("FPU setup not supported on non-x86 platforms\n");
80 }
81
82 #endif
83
84 void
85 NineDevice9_SetDefaultState( struct NineDevice9 *This, boolean is_reset )
86 {
87 struct NineSurface9 *refSurf = NULL;
88
89 DBG("This=%p is_reset=%d\n", This, (int) is_reset);
90
91 assert(!This->is_recording);
92
93 nine_state_set_defaults(This, &This->caps, is_reset);
94
95 refSurf = This->swapchains[0]->buffers[0];
96 assert(refSurf);
97
98 This->state.viewport.X = 0;
99 This->state.viewport.Y = 0;
100 This->state.viewport.Width = refSurf->desc.Width;
101 This->state.viewport.Height = refSurf->desc.Height;
102
103 This->state.scissor.minx = 0;
104 This->state.scissor.miny = 0;
105 This->state.scissor.maxx = refSurf->desc.Width;
106 This->state.scissor.maxy = refSurf->desc.Height;
107
108 if (This->nswapchains && This->swapchains[0]->params.EnableAutoDepthStencil) {
109 This->state.rs[D3DRS_ZENABLE] = TRUE;
110 This->state.rs_advertised[D3DRS_ZENABLE] = TRUE;
111 }
112 if (This->state.rs[D3DRS_ZENABLE])
113 NineDevice9_SetDepthStencilSurface(
114 This, (IDirect3DSurface9 *)This->swapchains[0]->zsbuf);
115 }
116
117 #define GET_PCAP(n) pScreen->get_param(pScreen, PIPE_CAP_##n)
118 HRESULT
119 NineDevice9_ctor( struct NineDevice9 *This,
120 struct NineUnknownParams *pParams,
121 struct pipe_screen *pScreen,
122 D3DDEVICE_CREATION_PARAMETERS *pCreationParameters,
123 D3DCAPS9 *pCaps,
124 D3DPRESENT_PARAMETERS *pPresentationParameters,
125 IDirect3D9 *pD3D9,
126 ID3DPresentGroup *pPresentationGroup,
127 struct d3dadapter9_context *pCTX,
128 boolean ex,
129 D3DDISPLAYMODEEX *pFullscreenDisplayMode,
130 int minorVersionNum )
131 {
132 unsigned i;
133 HRESULT hr = NineUnknown_ctor(&This->base, pParams);
134
135 DBG("This=%p pParams=%p pScreen=%p pCreationParameters=%p pCaps=%p pPresentationParameters=%p "
136 "pD3D9=%p pPresentationGroup=%p pCTX=%p ex=%d pFullscreenDisplayMode=%p\n",
137 This, pParams, pScreen, pCreationParameters, pCaps, pPresentationParameters, pD3D9,
138 pPresentationGroup, pCTX, (int) ex, pFullscreenDisplayMode);
139
140 if (FAILED(hr)) { return hr; }
141
142 list_inithead(&This->update_buffers);
143 list_inithead(&This->update_textures);
144 list_inithead(&This->managed_buffers);
145 list_inithead(&This->managed_textures);
146
147 This->screen = pScreen;
148 This->screen_sw = pCTX->ref;
149 This->caps = *pCaps;
150 This->d3d9 = pD3D9;
151 This->params = *pCreationParameters;
152 This->ex = ex;
153 This->present = pPresentationGroup;
154 This->minor_version_num = minorVersionNum;
155
156 IDirect3D9_AddRef(This->d3d9);
157 ID3DPresentGroup_AddRef(This->present);
158
159 if (!(This->params.BehaviorFlags & D3DCREATE_FPU_PRESERVE))
160 nine_setup_fpu();
161
162 if (This->params.BehaviorFlags & D3DCREATE_SOFTWARE_VERTEXPROCESSING) {
163 DBG("Application asked full Software Vertex Processing.\n");
164 This->swvp = true;
165 This->may_swvp = true;
166 } else
167 This->swvp = false;
168 if (This->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING) {
169 DBG("Application asked mixed Software Vertex Processing.\n");
170 This->may_swvp = true;
171 }
172 /* TODO: check if swvp is resetted by device Resets */
173
174 if (This->may_swvp &&
175 (This->screen->get_shader_param(This->screen, PIPE_SHADER_VERTEX,
176 PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE)
177 < (NINE_MAX_CONST_F_SWVP/2) * sizeof(float[4]) ||
178 This->screen->get_shader_param(This->screen, PIPE_SHADER_VERTEX,
179 PIPE_SHADER_CAP_MAX_CONST_BUFFERS) < 5)) {
180 /* Note: We just go on, some apps never use the abilities of
181 * swvp, and just set more constants than allowed at init.
182 * Only cards we support that are affected are the r500 */
183 WARN("Card unable to handle Software Vertex Processing. Game may fail\n");
184 }
185
186 /* When may_swvp, SetConstant* limits are different */
187 if (This->may_swvp)
188 This->caps.MaxVertexShaderConst = NINE_MAX_CONST_F_SWVP;
189
190 This->pipe = This->screen->context_create(This->screen, NULL, 0);
191 if (!This->pipe) { return E_OUTOFMEMORY; } /* guess */
192 This->pipe_sw = This->screen_sw->context_create(This->screen_sw, NULL, 0);
193 if (!This->pipe_sw) { return E_OUTOFMEMORY; }
194
195 This->cso = cso_create_context(This->pipe);
196 if (!This->cso) { return E_OUTOFMEMORY; } /* also a guess */
197 This->cso_sw = cso_create_context(This->pipe_sw);
198 if (!This->cso_sw) { return E_OUTOFMEMORY; }
199
200 /* Create first, it messes up our state. */
201 This->hud = hud_create(This->pipe, This->cso); /* NULL result is fine */
202
203 /* Available memory counter. Updated only for allocations with this device
204 * instance. This is the Win 7 behavior.
205 * Win XP shares this counter across multiple devices. */
206 This->available_texture_mem = This->screen->get_param(This->screen, PIPE_CAP_VIDEO_MEMORY);
207 if (This->available_texture_mem < 4096)
208 This->available_texture_mem <<= 20;
209 else
210 This->available_texture_mem = UINT_MAX;
211 /* We cap texture memory usage to 80% of what is reported free initially
212 * This helps get closer Win behaviour. For example VertexBuffer allocation
213 * still succeeds when texture allocation fails. */
214 This->available_texture_limit = This->available_texture_mem * 20LL / 100LL;
215
216 /* create implicit swapchains */
217 This->nswapchains = ID3DPresentGroup_GetMultiheadCount(This->present);
218 This->swapchains = CALLOC(This->nswapchains,
219 sizeof(struct NineSwapChain9 *));
220 if (!This->swapchains) { return E_OUTOFMEMORY; }
221
222 for (i = 0; i < This->nswapchains; ++i) {
223 ID3DPresent *present;
224
225 hr = ID3DPresentGroup_GetPresent(This->present, i, &present);
226 if (FAILED(hr))
227 return hr;
228
229 if (ex) {
230 D3DDISPLAYMODEEX *mode = NULL;
231 struct NineSwapChain9Ex **ret =
232 (struct NineSwapChain9Ex **)&This->swapchains[i];
233
234 if (pFullscreenDisplayMode) mode = &(pFullscreenDisplayMode[i]);
235 /* when this is a Device9Ex, it should create SwapChain9Exs */
236 hr = NineSwapChain9Ex_new(This, TRUE, present,
237 &pPresentationParameters[i], pCTX,
238 This->params.hFocusWindow, mode, ret);
239 } else {
240 hr = NineSwapChain9_new(This, TRUE, present,
241 &pPresentationParameters[i], pCTX,
242 This->params.hFocusWindow,
243 &This->swapchains[i]);
244 }
245
246 ID3DPresent_Release(present);
247 if (FAILED(hr))
248 return hr;
249 NineUnknown_ConvertRefToBind(NineUnknown(This->swapchains[i]));
250
251 hr = NineSwapChain9_GetBackBuffer(This->swapchains[i], 0,
252 D3DBACKBUFFER_TYPE_MONO,
253 (IDirect3DSurface9 **)
254 &This->state.rt[i]);
255 if (FAILED(hr))
256 return hr;
257 NineUnknown_ConvertRefToBind(NineUnknown(This->state.rt[i]));
258 }
259
260 /* Initialize a dummy VBO to be used when a vertex declaration does not
261 * specify all the inputs needed by vertex shader, on win default behavior
262 * is to pass 0,0,0,0 to the shader */
263 {
264 struct pipe_transfer *transfer;
265 struct pipe_resource tmpl;
266 struct pipe_box box;
267 unsigned char *data;
268
269 memset(&tmpl, 0, sizeof(tmpl));
270 tmpl.target = PIPE_BUFFER;
271 tmpl.format = PIPE_FORMAT_R8_UNORM;
272 tmpl.width0 = 16; /* 4 floats */
273 tmpl.height0 = 1;
274 tmpl.depth0 = 1;
275 tmpl.array_size = 1;
276 tmpl.last_level = 0;
277 tmpl.nr_samples = 0;
278 tmpl.usage = PIPE_USAGE_DEFAULT;
279 tmpl.bind = PIPE_BIND_VERTEX_BUFFER;
280 tmpl.flags = 0;
281 This->dummy_vbo = pScreen->resource_create(pScreen, &tmpl);
282
283 if (!This->dummy_vbo)
284 return D3DERR_OUTOFVIDEOMEMORY;
285
286 u_box_1d(0, 16, &box);
287 data = This->pipe->transfer_map(This->pipe, This->dummy_vbo, 0,
288 PIPE_TRANSFER_WRITE |
289 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE,
290 &box, &transfer);
291 assert(data);
292 assert(transfer);
293 memset(data, 0, 16);
294 This->pipe->transfer_unmap(This->pipe, transfer);
295 }
296
297 This->cursor.software = FALSE;
298 This->cursor.hotspot.x = -1;
299 This->cursor.hotspot.y = -1;
300 {
301 struct pipe_resource tmpl;
302 memset(&tmpl, 0, sizeof(tmpl));
303 tmpl.target = PIPE_TEXTURE_2D;
304 tmpl.format = PIPE_FORMAT_R8G8B8A8_UNORM;
305 tmpl.width0 = 64;
306 tmpl.height0 = 64;
307 tmpl.depth0 = 1;
308 tmpl.array_size = 1;
309 tmpl.last_level = 0;
310 tmpl.nr_samples = 0;
311 tmpl.usage = PIPE_USAGE_DEFAULT;
312 tmpl.bind = PIPE_BIND_CURSOR | PIPE_BIND_SAMPLER_VIEW;
313 tmpl.flags = 0;
314
315 This->cursor.image = pScreen->resource_create(pScreen, &tmpl);
316 if (!This->cursor.image)
317 return D3DERR_OUTOFVIDEOMEMORY;
318
319 /* For uploading 32x32 (argb) cursor */
320 This->cursor.hw_upload_temp = MALLOC(32 * 4 * 32);
321 if (!This->cursor.hw_upload_temp)
322 return D3DERR_OUTOFVIDEOMEMORY;
323 }
324
325 /* Create constant buffers. */
326 {
327 unsigned max_const_vs, max_const_ps;
328
329 /* vs 3.0: >= 256 float constants, but for cards with exactly 256 slots,
330 * we have to take in some more slots for int and bool*/
331 max_const_vs = _min(pScreen->get_shader_param(pScreen, PIPE_SHADER_VERTEX,
332 PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE) /
333 sizeof(float[4]),
334 NINE_MAX_CONST_ALL);
335 /* ps 3.0: 224 float constants. All cards supported support at least
336 * 256 constants for ps */
337 max_const_ps = NINE_MAX_CONST_F_PS3 + (NINE_MAX_CONST_I + NINE_MAX_CONST_B / 4);
338
339 This->max_vs_const_f = max_const_vs -
340 (NINE_MAX_CONST_I + NINE_MAX_CONST_B / 4);
341 This->max_ps_const_f = max_const_ps -
342 (NINE_MAX_CONST_I + NINE_MAX_CONST_B / 4);
343
344 This->vs_const_size = max_const_vs * sizeof(float[4]);
345 This->ps_const_size = max_const_ps * sizeof(float[4]);
346 /* Include space for I,B constants for user constbuf. */
347 if (This->may_swvp) {
348 This->state.vs_const_f_swvp = CALLOC(NINE_MAX_CONST_F_SWVP * sizeof(float[4]),1);
349 if (!This->state.vs_const_f_swvp)
350 return E_OUTOFMEMORY;
351 This->state.vs_lconstf_temp = CALLOC(NINE_MAX_CONST_F_SWVP * sizeof(float[4]),1);
352 This->state.vs_const_i = CALLOC(NINE_MAX_CONST_I_SWVP * sizeof(int[4]), 1);
353 This->state.vs_const_b = CALLOC(NINE_MAX_CONST_B_SWVP * sizeof(BOOL), 1);
354 } else {
355 This->state.vs_const_f_swvp = NULL;
356 This->state.vs_lconstf_temp = CALLOC(This->vs_const_size,1);
357 This->state.vs_const_i = CALLOC(NINE_MAX_CONST_I * sizeof(int[4]), 1);
358 This->state.vs_const_b = CALLOC(NINE_MAX_CONST_B * sizeof(BOOL), 1);
359 }
360 This->state.vs_const_f = CALLOC(This->vs_const_size, 1);
361 This->state.ps_const_f = CALLOC(This->ps_const_size, 1);
362 This->state.ps_lconstf_temp = CALLOC(This->ps_const_size,1);
363 if (!This->state.vs_const_f || !This->state.ps_const_f ||
364 !This->state.vs_lconstf_temp || !This->state.ps_lconstf_temp ||
365 !This->state.vs_const_i || !This->state.vs_const_b)
366 return E_OUTOFMEMORY;
367
368 if (strstr(pScreen->get_name(pScreen), "AMD") ||
369 strstr(pScreen->get_name(pScreen), "ATI")) {
370 This->driver_bugs.buggy_barycentrics = TRUE;
371 }
372 }
373
374 /* allocate dummy texture/sampler for when there are missing ones bound */
375 {
376 struct pipe_resource tmplt;
377 struct pipe_sampler_view templ;
378 struct pipe_sampler_state samp;
379 memset(&tmplt, 0, sizeof(tmplt));
380 memset(&samp, 0, sizeof(samp));
381
382 tmplt.target = PIPE_TEXTURE_2D;
383 tmplt.width0 = 1;
384 tmplt.height0 = 1;
385 tmplt.depth0 = 1;
386 tmplt.last_level = 0;
387 tmplt.array_size = 1;
388 tmplt.usage = PIPE_USAGE_DEFAULT;
389 tmplt.flags = 0;
390 tmplt.format = PIPE_FORMAT_B8G8R8A8_UNORM;
391 tmplt.bind = PIPE_BIND_SAMPLER_VIEW;
392 tmplt.nr_samples = 0;
393
394 This->dummy_texture = This->screen->resource_create(This->screen, &tmplt);
395 if (!This->dummy_texture)
396 return D3DERR_DRIVERINTERNALERROR;
397
398 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
399 templ.u.tex.first_layer = 0;
400 templ.u.tex.last_layer = 0;
401 templ.u.tex.first_level = 0;
402 templ.u.tex.last_level = 0;
403 templ.swizzle_r = PIPE_SWIZZLE_0;
404 templ.swizzle_g = PIPE_SWIZZLE_0;
405 templ.swizzle_b = PIPE_SWIZZLE_0;
406 templ.swizzle_a = PIPE_SWIZZLE_1;
407 templ.target = This->dummy_texture->target;
408
409 This->dummy_sampler_view = This->pipe->create_sampler_view(This->pipe, This->dummy_texture, &templ);
410 if (!This->dummy_sampler_view)
411 return D3DERR_DRIVERINTERNALERROR;
412
413 samp.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
414 samp.max_lod = 15.0f;
415 samp.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
416 samp.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
417 samp.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
418 samp.min_img_filter = PIPE_TEX_FILTER_NEAREST;
419 samp.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
420 samp.compare_mode = PIPE_TEX_COMPARE_NONE;
421 samp.compare_func = PIPE_FUNC_LEQUAL;
422 samp.normalized_coords = 1;
423 samp.seamless_cube_map = 0;
424 This->dummy_sampler_state = samp;
425 }
426
427 /* Allocate upload helper for drivers that suck (from st pov ;). */
428
429 This->driver_caps.user_vbufs = GET_PCAP(USER_VERTEX_BUFFERS);
430 This->driver_caps.user_ibufs = GET_PCAP(USER_INDEX_BUFFERS);
431 This->driver_caps.user_cbufs = GET_PCAP(USER_CONSTANT_BUFFERS);
432 This->driver_caps.user_sw_vbufs = This->screen_sw->get_param(This->screen_sw, PIPE_CAP_USER_VERTEX_BUFFERS);
433 This->driver_caps.user_sw_cbufs = This->screen_sw->get_param(This->screen_sw, PIPE_CAP_USER_CONSTANT_BUFFERS);
434
435 if (!This->driver_caps.user_vbufs)
436 This->vertex_uploader = u_upload_create(This->pipe, 65536,
437 PIPE_BIND_VERTEX_BUFFER, PIPE_USAGE_STREAM);
438 This->vertex_sw_uploader = u_upload_create(This->pipe_sw, 65536,
439 PIPE_BIND_VERTEX_BUFFER, PIPE_USAGE_STREAM);
440 if (!This->driver_caps.user_ibufs)
441 This->index_uploader = u_upload_create(This->pipe, 128 * 1024,
442 PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_STREAM);
443 if (!This->driver_caps.user_cbufs) {
444 This->constbuf_alignment = GET_PCAP(CONSTANT_BUFFER_OFFSET_ALIGNMENT);
445 This->constbuf_uploader = u_upload_create(This->pipe, This->vs_const_size,
446 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM);
447 }
448
449 This->constbuf_sw_uploader = u_upload_create(This->pipe_sw, 128 * 1024,
450 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM);
451
452 This->driver_caps.window_space_position_support = GET_PCAP(TGSI_VS_WINDOW_SPACE_POSITION);
453 This->driver_caps.vs_integer = pScreen->get_shader_param(pScreen, PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_INTEGERS);
454 This->driver_caps.ps_integer = pScreen->get_shader_param(pScreen, PIPE_SHADER_FRAGMENT, PIPE_SHADER_CAP_INTEGERS);
455 This->driver_caps.offset_units_unscaled = GET_PCAP(POLYGON_OFFSET_UNITS_UNSCALED);
456
457 nine_ff_init(This); /* initialize fixed function code */
458
459 NineDevice9_SetDefaultState(This, FALSE);
460
461 {
462 struct pipe_poly_stipple stipple;
463 memset(&stipple, ~0, sizeof(stipple));
464 This->pipe->set_polygon_stipple(This->pipe, &stipple);
465 }
466
467 This->update = &This->state;
468
469 nine_state_init_sw(This);
470
471 ID3DPresentGroup_Release(This->present);
472
473 return D3D_OK;
474 }
475 #undef GET_PCAP
476
477 void
478 NineDevice9_dtor( struct NineDevice9 *This )
479 {
480 unsigned i;
481
482 DBG("This=%p\n", This);
483
484 if (This->pipe && This->cso)
485 nine_pipe_context_clear(This);
486 nine_ff_fini(This);
487 nine_state_destroy_sw(This);
488 nine_state_clear(&This->state, TRUE);
489
490 if (This->vertex_uploader)
491 u_upload_destroy(This->vertex_uploader);
492 if (This->index_uploader)
493 u_upload_destroy(This->index_uploader);
494 if (This->constbuf_uploader)
495 u_upload_destroy(This->constbuf_uploader);
496 if (This->vertex_sw_uploader)
497 u_upload_destroy(This->vertex_sw_uploader);
498 if (This->constbuf_sw_uploader)
499 u_upload_destroy(This->constbuf_sw_uploader);
500
501 nine_bind(&This->record, NULL);
502
503 pipe_sampler_view_reference(&This->dummy_sampler_view, NULL);
504 pipe_resource_reference(&This->dummy_texture, NULL);
505 pipe_resource_reference(&This->dummy_vbo, NULL);
506 FREE(This->state.vs_const_f);
507 FREE(This->state.ps_const_f);
508 FREE(This->state.vs_lconstf_temp);
509 FREE(This->state.ps_lconstf_temp);
510 FREE(This->state.vs_const_i);
511 FREE(This->state.vs_const_b);
512 FREE(This->state.vs_const_f_swvp);
513
514 pipe_resource_reference(&This->cursor.image, NULL);
515 FREE(This->cursor.hw_upload_temp);
516
517 if (This->swapchains) {
518 for (i = 0; i < This->nswapchains; ++i)
519 if (This->swapchains[i])
520 NineUnknown_Unbind(NineUnknown(This->swapchains[i]));
521 FREE(This->swapchains);
522 }
523
524 /* Destroy cso first */
525 if (This->cso) { cso_destroy_context(This->cso); }
526 if (This->cso_sw) { cso_destroy_context(This->cso_sw); }
527 if (This->pipe && This->pipe->destroy) { This->pipe->destroy(This->pipe); }
528 if (This->pipe_sw && This->pipe_sw->destroy) { This->pipe_sw->destroy(This->pipe_sw); }
529
530 if (This->present) { ID3DPresentGroup_Release(This->present); }
531 if (This->d3d9) { IDirect3D9_Release(This->d3d9); }
532
533 NineUnknown_dtor(&This->base);
534 }
535
536 struct pipe_screen *
537 NineDevice9_GetScreen( struct NineDevice9 *This )
538 {
539 return This->screen;
540 }
541
542 struct pipe_context *
543 NineDevice9_GetPipe( struct NineDevice9 *This )
544 {
545 return This->pipe;
546 }
547
548 struct cso_context *
549 NineDevice9_GetCSO( struct NineDevice9 *This )
550 {
551 return This->cso;
552 }
553
554 const D3DCAPS9 *
555 NineDevice9_GetCaps( struct NineDevice9 *This )
556 {
557 return &This->caps;
558 }
559
560 static inline void
561 NineDevice9_PauseRecording( struct NineDevice9 *This )
562 {
563 if (This->record) {
564 This->update = &This->state;
565 This->is_recording = FALSE;
566 }
567 }
568
569 static inline void
570 NineDevice9_ResumeRecording( struct NineDevice9 *This )
571 {
572 if (This->record) {
573 This->update = &This->record->state;
574 This->is_recording = TRUE;
575 }
576 }
577
578 HRESULT NINE_WINAPI
579 NineDevice9_TestCooperativeLevel( struct NineDevice9 *This )
580 {
581 if (NineSwapChain9_GetOccluded(This->swapchains[0])) {
582 This->device_needs_reset = TRUE;
583 return D3DERR_DEVICELOST;
584 } else if (NineSwapChain9_ResolutionMismatch(This->swapchains[0])) {
585 This->device_needs_reset = TRUE;
586 return D3DERR_DEVICENOTRESET;
587 } else if (This->device_needs_reset) {
588 return D3DERR_DEVICENOTRESET;
589 }
590
591 return D3D_OK;
592 }
593
594 UINT NINE_WINAPI
595 NineDevice9_GetAvailableTextureMem( struct NineDevice9 *This )
596 {
597 return This->available_texture_mem;
598 }
599
600 HRESULT NINE_WINAPI
601 NineDevice9_EvictManagedResources( struct NineDevice9 *This )
602 {
603 struct NineBaseTexture9 *tex;
604 struct NineBuffer9 *buf;
605
606 DBG("This=%p\n", This);
607 LIST_FOR_EACH_ENTRY(tex, &This->managed_textures, list2) {
608 NineBaseTexture9_UnLoad(tex);
609 }
610 /* Vertex/index buffers don't take a lot of space and aren't accounted
611 * for d3d memory usage. Instead of actually freeing from memory,
612 * just mark the buffer dirty to trigger a re-upload later. We
613 * could just ignore, but some bad behaving apps could rely on it (if
614 * they write outside the locked regions typically). */
615 LIST_FOR_EACH_ENTRY(buf, &This->managed_buffers, managed.list2) {
616 NineBuffer9_SetDirty(buf);
617 }
618
619 return D3D_OK;
620 }
621
622 HRESULT NINE_WINAPI
623 NineDevice9_GetDirect3D( struct NineDevice9 *This,
624 IDirect3D9 **ppD3D9 )
625 {
626 user_assert(ppD3D9 != NULL, E_POINTER);
627 IDirect3D9_AddRef(This->d3d9);
628 *ppD3D9 = This->d3d9;
629 return D3D_OK;
630 }
631
632 HRESULT NINE_WINAPI
633 NineDevice9_GetDeviceCaps( struct NineDevice9 *This,
634 D3DCAPS9 *pCaps )
635 {
636 user_assert(pCaps != NULL, D3DERR_INVALIDCALL);
637 *pCaps = This->caps;
638 return D3D_OK;
639 }
640
641 HRESULT NINE_WINAPI
642 NineDevice9_GetDisplayMode( struct NineDevice9 *This,
643 UINT iSwapChain,
644 D3DDISPLAYMODE *pMode )
645 {
646 DBG("This=%p iSwapChain=%u pMode=%p\n", This, iSwapChain, pMode);
647
648 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
649
650 return NineSwapChain9_GetDisplayMode(This->swapchains[iSwapChain], pMode);
651 }
652
653 HRESULT NINE_WINAPI
654 NineDevice9_GetCreationParameters( struct NineDevice9 *This,
655 D3DDEVICE_CREATION_PARAMETERS *pParameters )
656 {
657 user_assert(pParameters != NULL, D3DERR_INVALIDCALL);
658 *pParameters = This->params;
659 return D3D_OK;
660 }
661
662 HRESULT NINE_WINAPI
663 NineDevice9_SetCursorProperties( struct NineDevice9 *This,
664 UINT XHotSpot,
665 UINT YHotSpot,
666 IDirect3DSurface9 *pCursorBitmap )
667 {
668 struct NineSurface9 *surf = NineSurface9(pCursorBitmap);
669 struct pipe_context *pipe = This->pipe;
670 struct pipe_box box;
671 struct pipe_transfer *transfer;
672 BOOL hw_cursor;
673 void *ptr;
674
675 DBG_FLAG(DBG_SWAPCHAIN, "This=%p XHotSpot=%u YHotSpot=%u "
676 "pCursorBitmap=%p\n", This, XHotSpot, YHotSpot, pCursorBitmap);
677
678 user_assert(pCursorBitmap, D3DERR_INVALIDCALL);
679 user_assert(surf->desc.Format == D3DFMT_A8R8G8B8, D3DERR_INVALIDCALL);
680
681 if (This->swapchains[0]->params.Windowed) {
682 This->cursor.w = MIN2(surf->desc.Width, 32);
683 This->cursor.h = MIN2(surf->desc.Height, 32);
684 hw_cursor = 1; /* always use hw cursor for windowed mode */
685 } else {
686 This->cursor.w = MIN2(surf->desc.Width, This->cursor.image->width0);
687 This->cursor.h = MIN2(surf->desc.Height, This->cursor.image->height0);
688 hw_cursor = This->cursor.w == 32 && This->cursor.h == 32;
689 }
690
691 u_box_origin_2d(This->cursor.w, This->cursor.h, &box);
692
693 ptr = pipe->transfer_map(pipe, This->cursor.image, 0,
694 PIPE_TRANSFER_WRITE |
695 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE,
696 &box, &transfer);
697 if (!ptr)
698 ret_err("Failed to update cursor image.\n", D3DERR_DRIVERINTERNALERROR);
699
700 This->cursor.hotspot.x = XHotSpot;
701 This->cursor.hotspot.y = YHotSpot;
702
703 /* Copy cursor image to internal storage. */
704 {
705 D3DLOCKED_RECT lock;
706 HRESULT hr;
707 const struct util_format_description *sfmt =
708 util_format_description(surf->base.info.format);
709 assert(sfmt);
710
711 hr = NineSurface9_LockRect(surf, &lock, NULL, D3DLOCK_READONLY);
712 if (FAILED(hr))
713 ret_err("Failed to map cursor source image.\n",
714 D3DERR_DRIVERINTERNALERROR);
715
716 sfmt->unpack_rgba_8unorm(ptr, transfer->stride,
717 lock.pBits, lock.Pitch,
718 This->cursor.w, This->cursor.h);
719
720 if (hw_cursor) {
721 void *data = lock.pBits;
722 /* SetCursor assumes 32x32 argb with pitch 128 */
723 if (lock.Pitch != 128) {
724 sfmt->unpack_rgba_8unorm(This->cursor.hw_upload_temp, 128,
725 lock.pBits, lock.Pitch,
726 32, 32);
727 data = This->cursor.hw_upload_temp;
728 }
729 hw_cursor = ID3DPresent_SetCursor(This->swapchains[0]->present,
730 data,
731 &This->cursor.hotspot,
732 This->cursor.visible) == D3D_OK;
733 }
734
735 NineSurface9_UnlockRect(surf);
736 }
737 pipe->transfer_unmap(pipe, transfer);
738
739 /* hide cursor if we emulate it */
740 if (!hw_cursor)
741 ID3DPresent_SetCursor(This->swapchains[0]->present, NULL, NULL, FALSE);
742 This->cursor.software = !hw_cursor;
743
744 return D3D_OK;
745 }
746
747 void NINE_WINAPI
748 NineDevice9_SetCursorPosition( struct NineDevice9 *This,
749 int X,
750 int Y,
751 DWORD Flags )
752 {
753 struct NineSwapChain9 *swap = This->swapchains[0];
754
755 DBG("This=%p X=%d Y=%d Flags=%d\n", This, X, Y, Flags);
756
757 This->cursor.pos.x = X;
758 This->cursor.pos.y = Y;
759
760 if (!This->cursor.software)
761 This->cursor.software = ID3DPresent_SetCursorPos(swap->present, &This->cursor.pos) != D3D_OK;
762 }
763
764 BOOL NINE_WINAPI
765 NineDevice9_ShowCursor( struct NineDevice9 *This,
766 BOOL bShow )
767 {
768 BOOL old = This->cursor.visible;
769
770 DBG("This=%p bShow=%d\n", This, (int) bShow);
771
772 This->cursor.visible = bShow && (This->cursor.hotspot.x != -1);
773 if (!This->cursor.software)
774 This->cursor.software = ID3DPresent_SetCursor(This->swapchains[0]->present, NULL, NULL, bShow) != D3D_OK;
775
776 return old;
777 }
778
779 HRESULT NINE_WINAPI
780 NineDevice9_CreateAdditionalSwapChain( struct NineDevice9 *This,
781 D3DPRESENT_PARAMETERS *pPresentationParameters,
782 IDirect3DSwapChain9 **pSwapChain )
783 {
784 struct NineSwapChain9 *swapchain, *tmplt = This->swapchains[0];
785 ID3DPresent *present;
786 HRESULT hr;
787
788 DBG("This=%p pPresentationParameters=%p pSwapChain=%p\n",
789 This, pPresentationParameters, pSwapChain);
790
791 user_assert(pPresentationParameters, D3DERR_INVALIDCALL);
792 user_assert(tmplt->params.Windowed && pPresentationParameters->Windowed, D3DERR_INVALIDCALL);
793
794 /* TODO: this deserves more tests */
795 if (!pPresentationParameters->hDeviceWindow)
796 pPresentationParameters->hDeviceWindow = This->params.hFocusWindow;
797
798 hr = ID3DPresentGroup_CreateAdditionalPresent(This->present, pPresentationParameters, &present);
799
800 if (FAILED(hr))
801 return hr;
802
803 hr = NineSwapChain9_new(This, FALSE, present, pPresentationParameters,
804 tmplt->actx,
805 tmplt->params.hDeviceWindow,
806 &swapchain);
807 if (FAILED(hr))
808 return hr;
809
810 *pSwapChain = (IDirect3DSwapChain9 *)swapchain;
811 return D3D_OK;
812 }
813
814 HRESULT NINE_WINAPI
815 NineDevice9_GetSwapChain( struct NineDevice9 *This,
816 UINT iSwapChain,
817 IDirect3DSwapChain9 **pSwapChain )
818 {
819 user_assert(pSwapChain != NULL, D3DERR_INVALIDCALL);
820
821 *pSwapChain = NULL;
822 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
823
824 NineUnknown_AddRef(NineUnknown(This->swapchains[iSwapChain]));
825 *pSwapChain = (IDirect3DSwapChain9 *)This->swapchains[iSwapChain];
826
827 return D3D_OK;
828 }
829
830 UINT NINE_WINAPI
831 NineDevice9_GetNumberOfSwapChains( struct NineDevice9 *This )
832 {
833 return This->nswapchains;
834 }
835
836 HRESULT NINE_WINAPI
837 NineDevice9_Reset( struct NineDevice9 *This,
838 D3DPRESENT_PARAMETERS *pPresentationParameters )
839 {
840 HRESULT hr = D3D_OK;
841 unsigned i;
842
843 DBG("This=%p pPresentationParameters=%p\n", This, pPresentationParameters);
844
845 if (NineSwapChain9_GetOccluded(This->swapchains[0])) {
846 This->device_needs_reset = TRUE;
847 return D3DERR_DEVICELOST;
848 }
849
850 for (i = 0; i < This->nswapchains; ++i) {
851 D3DPRESENT_PARAMETERS *params = &pPresentationParameters[i];
852 hr = NineSwapChain9_Resize(This->swapchains[i], params, NULL);
853 if (hr != D3D_OK)
854 break;
855 }
856
857 nine_pipe_context_clear(This);
858 nine_state_clear(&This->state, TRUE);
859
860 NineDevice9_SetDefaultState(This, TRUE);
861 NineDevice9_SetRenderTarget(
862 This, 0, (IDirect3DSurface9 *)This->swapchains[0]->buffers[0]);
863 /* XXX: better use GetBackBuffer here ? */
864
865 This->device_needs_reset = (hr != D3D_OK);
866 return hr;
867 }
868
869 HRESULT NINE_WINAPI
870 NineDevice9_Present( struct NineDevice9 *This,
871 const RECT *pSourceRect,
872 const RECT *pDestRect,
873 HWND hDestWindowOverride,
874 const RGNDATA *pDirtyRegion )
875 {
876 unsigned i;
877 HRESULT hr;
878
879 DBG("This=%p pSourceRect=%p pDestRect=%p hDestWindowOverride=%p pDirtyRegion=%p\n",
880 This, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
881
882 /* XXX is this right? */
883 for (i = 0; i < This->nswapchains; ++i) {
884 hr = NineSwapChain9_Present(This->swapchains[i], pSourceRect, pDestRect,
885 hDestWindowOverride, pDirtyRegion, 0);
886 if (FAILED(hr)) { return hr; }
887 }
888
889 return D3D_OK;
890 }
891
892 HRESULT NINE_WINAPI
893 NineDevice9_GetBackBuffer( struct NineDevice9 *This,
894 UINT iSwapChain,
895 UINT iBackBuffer,
896 D3DBACKBUFFER_TYPE Type,
897 IDirect3DSurface9 **ppBackBuffer )
898 {
899 user_assert(ppBackBuffer != NULL, D3DERR_INVALIDCALL);
900 /* return NULL on error */
901 *ppBackBuffer = NULL;
902 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
903
904 return NineSwapChain9_GetBackBuffer(This->swapchains[iSwapChain],
905 iBackBuffer, Type, ppBackBuffer);
906 }
907
908 HRESULT NINE_WINAPI
909 NineDevice9_GetRasterStatus( struct NineDevice9 *This,
910 UINT iSwapChain,
911 D3DRASTER_STATUS *pRasterStatus )
912 {
913 user_assert(pRasterStatus != NULL, D3DERR_INVALIDCALL);
914 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
915
916 return NineSwapChain9_GetRasterStatus(This->swapchains[iSwapChain],
917 pRasterStatus);
918 }
919
920 HRESULT NINE_WINAPI
921 NineDevice9_SetDialogBoxMode( struct NineDevice9 *This,
922 BOOL bEnableDialogs )
923 {
924 STUB(D3DERR_INVALIDCALL);
925 }
926
927 void NINE_WINAPI
928 NineDevice9_SetGammaRamp( struct NineDevice9 *This,
929 UINT iSwapChain,
930 DWORD Flags,
931 const D3DGAMMARAMP *pRamp )
932 {
933 DBG("This=%p iSwapChain=%u Flags=%x pRamp=%p\n", This,
934 iSwapChain, Flags, pRamp);
935
936 user_warn(iSwapChain >= This->nswapchains);
937 user_warn(!pRamp);
938
939 if (pRamp && (iSwapChain < This->nswapchains)) {
940 struct NineSwapChain9 *swap = This->swapchains[iSwapChain];
941 swap->gamma = *pRamp;
942 ID3DPresent_SetGammaRamp(swap->present, pRamp, swap->params.hDeviceWindow);
943 }
944 }
945
946 void NINE_WINAPI
947 NineDevice9_GetGammaRamp( struct NineDevice9 *This,
948 UINT iSwapChain,
949 D3DGAMMARAMP *pRamp )
950 {
951 DBG("This=%p iSwapChain=%u pRamp=%p\n", This, iSwapChain, pRamp);
952
953 user_warn(iSwapChain >= This->nswapchains);
954 user_warn(!pRamp);
955
956 if (pRamp && (iSwapChain < This->nswapchains))
957 *pRamp = This->swapchains[iSwapChain]->gamma;
958 }
959
960 HRESULT NINE_WINAPI
961 NineDevice9_CreateTexture( struct NineDevice9 *This,
962 UINT Width,
963 UINT Height,
964 UINT Levels,
965 DWORD Usage,
966 D3DFORMAT Format,
967 D3DPOOL Pool,
968 IDirect3DTexture9 **ppTexture,
969 HANDLE *pSharedHandle )
970 {
971 struct NineTexture9 *tex;
972 HRESULT hr;
973
974 DBG("This=%p Width=%u Height=%u Levels=%u Usage=%s Format=%s Pool=%s "
975 "ppOut=%p pSharedHandle=%p\n", This, Width, Height, Levels,
976 nine_D3DUSAGE_to_str(Usage), d3dformat_to_string(Format),
977 nine_D3DPOOL_to_str(Pool), ppTexture, pSharedHandle);
978
979 Usage &= D3DUSAGE_AUTOGENMIPMAP | D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_DMAP |
980 D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE | D3DUSAGE_RENDERTARGET |
981 D3DUSAGE_SOFTWAREPROCESSING | D3DUSAGE_TEXTAPI;
982
983 *ppTexture = NULL;
984
985 hr = NineTexture9_new(This, Width, Height, Levels, Usage, Format, Pool,
986 &tex, pSharedHandle);
987 if (SUCCEEDED(hr))
988 *ppTexture = (IDirect3DTexture9 *)tex;
989
990 return hr;
991 }
992
993 HRESULT NINE_WINAPI
994 NineDevice9_CreateVolumeTexture( struct NineDevice9 *This,
995 UINT Width,
996 UINT Height,
997 UINT Depth,
998 UINT Levels,
999 DWORD Usage,
1000 D3DFORMAT Format,
1001 D3DPOOL Pool,
1002 IDirect3DVolumeTexture9 **ppVolumeTexture,
1003 HANDLE *pSharedHandle )
1004 {
1005 struct NineVolumeTexture9 *tex;
1006 HRESULT hr;
1007
1008 DBG("This=%p Width=%u Height=%u Depth=%u Levels=%u Usage=%s Format=%s Pool=%s "
1009 "ppOut=%p pSharedHandle=%p\n", This, Width, Height, Depth, Levels,
1010 nine_D3DUSAGE_to_str(Usage), d3dformat_to_string(Format),
1011 nine_D3DPOOL_to_str(Pool), ppVolumeTexture, pSharedHandle);
1012
1013 Usage &= D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE |
1014 D3DUSAGE_SOFTWAREPROCESSING;
1015
1016 *ppVolumeTexture = NULL;
1017
1018 hr = NineVolumeTexture9_new(This, Width, Height, Depth, Levels,
1019 Usage, Format, Pool, &tex, pSharedHandle);
1020 if (SUCCEEDED(hr))
1021 *ppVolumeTexture = (IDirect3DVolumeTexture9 *)tex;
1022
1023 return hr;
1024 }
1025
1026 HRESULT NINE_WINAPI
1027 NineDevice9_CreateCubeTexture( struct NineDevice9 *This,
1028 UINT EdgeLength,
1029 UINT Levels,
1030 DWORD Usage,
1031 D3DFORMAT Format,
1032 D3DPOOL Pool,
1033 IDirect3DCubeTexture9 **ppCubeTexture,
1034 HANDLE *pSharedHandle )
1035 {
1036 struct NineCubeTexture9 *tex;
1037 HRESULT hr;
1038
1039 DBG("This=%p EdgeLength=%u Levels=%u Usage=%s Format=%s Pool=%s ppOut=%p "
1040 "pSharedHandle=%p\n", This, EdgeLength, Levels,
1041 nine_D3DUSAGE_to_str(Usage), d3dformat_to_string(Format),
1042 nine_D3DPOOL_to_str(Pool), ppCubeTexture, pSharedHandle);
1043
1044 Usage &= D3DUSAGE_AUTOGENMIPMAP | D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_DYNAMIC |
1045 D3DUSAGE_NONSECURE | D3DUSAGE_RENDERTARGET |
1046 D3DUSAGE_SOFTWAREPROCESSING;
1047
1048 *ppCubeTexture = NULL;
1049
1050 hr = NineCubeTexture9_new(This, EdgeLength, Levels, Usage, Format, Pool,
1051 &tex, pSharedHandle);
1052 if (SUCCEEDED(hr))
1053 *ppCubeTexture = (IDirect3DCubeTexture9 *)tex;
1054
1055 return hr;
1056 }
1057
1058 HRESULT NINE_WINAPI
1059 NineDevice9_CreateVertexBuffer( struct NineDevice9 *This,
1060 UINT Length,
1061 DWORD Usage,
1062 DWORD FVF,
1063 D3DPOOL Pool,
1064 IDirect3DVertexBuffer9 **ppVertexBuffer,
1065 HANDLE *pSharedHandle )
1066 {
1067 struct NineVertexBuffer9 *buf;
1068 HRESULT hr;
1069 D3DVERTEXBUFFER_DESC desc;
1070
1071 DBG("This=%p Length=%u Usage=%x FVF=%x Pool=%u ppOut=%p pSharedHandle=%p\n",
1072 This, Length, Usage, FVF, Pool, ppVertexBuffer, pSharedHandle);
1073
1074 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_NOTAVAILABLE);
1075
1076 desc.Format = D3DFMT_VERTEXDATA;
1077 desc.Type = D3DRTYPE_VERTEXBUFFER;
1078 desc.Usage = Usage &
1079 (D3DUSAGE_DONOTCLIP | D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE |
1080 D3DUSAGE_NPATCHES | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES |
1081 D3DUSAGE_SOFTWAREPROCESSING | D3DUSAGE_TEXTAPI |
1082 D3DUSAGE_WRITEONLY);
1083 desc.Pool = Pool;
1084 desc.Size = Length;
1085 desc.FVF = FVF;
1086
1087 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1088 user_assert(desc.Usage == Usage, D3DERR_INVALIDCALL);
1089
1090 hr = NineVertexBuffer9_new(This, &desc, &buf);
1091 if (SUCCEEDED(hr))
1092 *ppVertexBuffer = (IDirect3DVertexBuffer9 *)buf;
1093 return hr;
1094 }
1095
1096 HRESULT NINE_WINAPI
1097 NineDevice9_CreateIndexBuffer( struct NineDevice9 *This,
1098 UINT Length,
1099 DWORD Usage,
1100 D3DFORMAT Format,
1101 D3DPOOL Pool,
1102 IDirect3DIndexBuffer9 **ppIndexBuffer,
1103 HANDLE *pSharedHandle )
1104 {
1105 struct NineIndexBuffer9 *buf;
1106 HRESULT hr;
1107 D3DINDEXBUFFER_DESC desc;
1108
1109 DBG("This=%p Length=%u Usage=%x Format=%s Pool=%u ppOut=%p "
1110 "pSharedHandle=%p\n", This, Length, Usage,
1111 d3dformat_to_string(Format), Pool, ppIndexBuffer, pSharedHandle);
1112
1113 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_NOTAVAILABLE);
1114
1115 desc.Format = Format;
1116 desc.Type = D3DRTYPE_INDEXBUFFER;
1117 desc.Usage = Usage &
1118 (D3DUSAGE_DONOTCLIP | D3DUSAGE_DYNAMIC | D3DUSAGE_NONSECURE |
1119 D3DUSAGE_NPATCHES | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES |
1120 D3DUSAGE_SOFTWAREPROCESSING | D3DUSAGE_WRITEONLY);
1121 desc.Pool = Pool;
1122 desc.Size = Length;
1123
1124 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1125 user_assert(desc.Usage == Usage, D3DERR_INVALIDCALL);
1126
1127 hr = NineIndexBuffer9_new(This, &desc, &buf);
1128 if (SUCCEEDED(hr))
1129 *ppIndexBuffer = (IDirect3DIndexBuffer9 *)buf;
1130 return hr;
1131 }
1132
1133 static HRESULT
1134 create_zs_or_rt_surface(struct NineDevice9 *This,
1135 unsigned type, /* 0 = RT, 1 = ZS, 2 = plain */
1136 D3DPOOL Pool,
1137 UINT Width, UINT Height,
1138 D3DFORMAT Format,
1139 D3DMULTISAMPLE_TYPE MultiSample,
1140 DWORD MultisampleQuality,
1141 BOOL Discard_or_Lockable,
1142 IDirect3DSurface9 **ppSurface,
1143 HANDLE *pSharedHandle)
1144 {
1145 struct NineSurface9 *surface;
1146 HRESULT hr;
1147 D3DSURFACE_DESC desc;
1148
1149 DBG("This=%p type=%u Pool=%s Width=%u Height=%u Format=%s MS=%u Quality=%u "
1150 "Discard_or_Lockable=%i ppSurface=%p pSharedHandle=%p\n",
1151 This, type, nine_D3DPOOL_to_str(Pool), Width, Height,
1152 d3dformat_to_string(Format), MultiSample, MultisampleQuality,
1153 Discard_or_Lockable, ppSurface, pSharedHandle);
1154
1155 if (pSharedHandle)
1156 DBG("FIXME Used shared handle! This option isn't probably handled correctly!\n");
1157
1158 user_assert(Width && Height, D3DERR_INVALIDCALL);
1159 user_assert(Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
1160
1161 desc.Format = Format;
1162 desc.Type = D3DRTYPE_SURFACE;
1163 desc.Usage = 0;
1164 desc.Pool = Pool;
1165 desc.MultiSampleType = MultiSample;
1166 desc.MultiSampleQuality = MultisampleQuality;
1167 desc.Width = Width;
1168 desc.Height = Height;
1169 switch (type) {
1170 case 0: desc.Usage = D3DUSAGE_RENDERTARGET; break;
1171 case 1: desc.Usage = D3DUSAGE_DEPTHSTENCIL; break;
1172 default: assert(type == 2); break;
1173 }
1174
1175 hr = NineSurface9_new(This, NULL, NULL, NULL, 0, 0, 0, &desc, &surface);
1176 if (SUCCEEDED(hr)) {
1177 *ppSurface = (IDirect3DSurface9 *)surface;
1178
1179 if (surface->base.resource && Discard_or_Lockable && (type != 1))
1180 surface->base.resource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
1181 }
1182
1183 return hr;
1184 }
1185
1186 HRESULT NINE_WINAPI
1187 NineDevice9_CreateRenderTarget( struct NineDevice9 *This,
1188 UINT Width,
1189 UINT Height,
1190 D3DFORMAT Format,
1191 D3DMULTISAMPLE_TYPE MultiSample,
1192 DWORD MultisampleQuality,
1193 BOOL Lockable,
1194 IDirect3DSurface9 **ppSurface,
1195 HANDLE *pSharedHandle )
1196 {
1197 *ppSurface = NULL;
1198 return create_zs_or_rt_surface(This, 0, D3DPOOL_DEFAULT,
1199 Width, Height, Format,
1200 MultiSample, MultisampleQuality,
1201 Lockable, ppSurface, pSharedHandle);
1202 }
1203
1204 HRESULT NINE_WINAPI
1205 NineDevice9_CreateDepthStencilSurface( struct NineDevice9 *This,
1206 UINT Width,
1207 UINT Height,
1208 D3DFORMAT Format,
1209 D3DMULTISAMPLE_TYPE MultiSample,
1210 DWORD MultisampleQuality,
1211 BOOL Discard,
1212 IDirect3DSurface9 **ppSurface,
1213 HANDLE *pSharedHandle )
1214 {
1215 *ppSurface = NULL;
1216 if (!depth_stencil_format(Format))
1217 return D3DERR_NOTAVAILABLE;
1218 return create_zs_or_rt_surface(This, 1, D3DPOOL_DEFAULT,
1219 Width, Height, Format,
1220 MultiSample, MultisampleQuality,
1221 Discard, ppSurface, pSharedHandle);
1222 }
1223
1224 HRESULT NINE_WINAPI
1225 NineDevice9_UpdateSurface( struct NineDevice9 *This,
1226 IDirect3DSurface9 *pSourceSurface,
1227 const RECT *pSourceRect,
1228 IDirect3DSurface9 *pDestinationSurface,
1229 const POINT *pDestPoint )
1230 {
1231 struct NineSurface9 *dst = NineSurface9(pDestinationSurface);
1232 struct NineSurface9 *src = NineSurface9(pSourceSurface);
1233 int copy_width, copy_height;
1234 RECT destRect;
1235
1236 DBG("This=%p pSourceSurface=%p pDestinationSurface=%p "
1237 "pSourceRect=%p pDestPoint=%p\n", This,
1238 pSourceSurface, pDestinationSurface, pSourceRect, pDestPoint);
1239 if (pSourceRect)
1240 DBG("pSourceRect = (%u,%u)-(%u,%u)\n",
1241 pSourceRect->left, pSourceRect->top,
1242 pSourceRect->right, pSourceRect->bottom);
1243 if (pDestPoint)
1244 DBG("pDestPoint = (%u,%u)\n", pDestPoint->x, pDestPoint->y);
1245
1246 user_assert(dst && src, D3DERR_INVALIDCALL);
1247
1248 user_assert(dst->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1249 user_assert(src->base.pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1250
1251 user_assert(dst->desc.MultiSampleType == D3DMULTISAMPLE_NONE, D3DERR_INVALIDCALL);
1252 user_assert(src->desc.MultiSampleType == D3DMULTISAMPLE_NONE, D3DERR_INVALIDCALL);
1253
1254 user_assert(!src->lock_count, D3DERR_INVALIDCALL);
1255 user_assert(!dst->lock_count, D3DERR_INVALIDCALL);
1256
1257 user_assert(dst->desc.Format == src->desc.Format, D3DERR_INVALIDCALL);
1258 user_assert(!depth_stencil_format(dst->desc.Format), D3DERR_INVALIDCALL);
1259
1260 if (pSourceRect) {
1261 copy_width = pSourceRect->right - pSourceRect->left;
1262 copy_height = pSourceRect->bottom - pSourceRect->top;
1263
1264 user_assert(pSourceRect->left >= 0 &&
1265 copy_width > 0 &&
1266 pSourceRect->right <= src->desc.Width &&
1267 pSourceRect->top >= 0 &&
1268 copy_height > 0 &&
1269 pSourceRect->bottom <= src->desc.Height,
1270 D3DERR_INVALIDCALL);
1271 } else {
1272 copy_width = src->desc.Width;
1273 copy_height = src->desc.Height;
1274 }
1275
1276 destRect.right = copy_width;
1277 destRect.bottom = copy_height;
1278
1279 if (pDestPoint) {
1280 user_assert(pDestPoint->x >= 0 && pDestPoint->y >= 0,
1281 D3DERR_INVALIDCALL);
1282 destRect.right += pDestPoint->x;
1283 destRect.bottom += pDestPoint->y;
1284 }
1285
1286 user_assert(destRect.right <= dst->desc.Width &&
1287 destRect.bottom <= dst->desc.Height,
1288 D3DERR_INVALIDCALL);
1289
1290 if (compressed_format(dst->desc.Format)) {
1291 const unsigned w = util_format_get_blockwidth(dst->base.info.format);
1292 const unsigned h = util_format_get_blockheight(dst->base.info.format);
1293
1294 if (pDestPoint) {
1295 user_assert(!(pDestPoint->x % w) && !(pDestPoint->y % h),
1296 D3DERR_INVALIDCALL);
1297 }
1298
1299 if (pSourceRect) {
1300 user_assert(!(pSourceRect->left % w) && !(pSourceRect->top % h),
1301 D3DERR_INVALIDCALL);
1302 }
1303 if (!(copy_width == src->desc.Width &&
1304 copy_width == dst->desc.Width &&
1305 copy_height == src->desc.Height &&
1306 copy_height == dst->desc.Height)) {
1307 user_assert(!(copy_width % w) && !(copy_height % h),
1308 D3DERR_INVALIDCALL);
1309 }
1310 }
1311
1312 NineSurface9_CopyMemToDefault(dst, src, pDestPoint, pSourceRect);
1313
1314 return D3D_OK;
1315 }
1316
1317 HRESULT NINE_WINAPI
1318 NineDevice9_UpdateTexture( struct NineDevice9 *This,
1319 IDirect3DBaseTexture9 *pSourceTexture,
1320 IDirect3DBaseTexture9 *pDestinationTexture )
1321 {
1322 struct NineBaseTexture9 *dstb = NineBaseTexture9(pDestinationTexture);
1323 struct NineBaseTexture9 *srcb = NineBaseTexture9(pSourceTexture);
1324 unsigned l, m;
1325 unsigned last_src_level, last_dst_level;
1326 RECT rect;
1327
1328 DBG("This=%p pSourceTexture=%p pDestinationTexture=%p\n", This,
1329 pSourceTexture, pDestinationTexture);
1330
1331 user_assert(pSourceTexture && pDestinationTexture, D3DERR_INVALIDCALL);
1332 user_assert(pSourceTexture != pDestinationTexture, D3DERR_INVALIDCALL);
1333
1334 user_assert(dstb->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1335 user_assert(srcb->base.pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1336 user_assert(dstb->base.type == srcb->base.type, D3DERR_INVALIDCALL);
1337 user_assert(!(srcb->base.usage & D3DUSAGE_AUTOGENMIPMAP) ||
1338 dstb->base.usage & D3DUSAGE_AUTOGENMIPMAP, D3DERR_INVALIDCALL);
1339
1340 /* Spec: Failure if
1341 * . Different formats
1342 * . Fewer src levels than dst levels (if the opposite, only matching levels
1343 * are supposed to be copied)
1344 * . Levels do not match
1345 * DDI: Actually the above should pass because of legacy applications
1346 * Do what you want about these, but you shouldn't crash.
1347 * However driver can expect that the top dimension is greater for src than dst.
1348 * Wine tests: Every combination that passes the initial checks should pass.
1349 * . Different formats => conversion driver and format dependent.
1350 * . 1 level, but size not matching => copy is done (and even crash if src bigger
1351 * than dst. For the case where dst bigger, wine doesn't test if a stretch is applied
1352 * or if a subrect is copied).
1353 * . 8x8 4 sublevels -> 7x7 2 sublevels => driver dependent, On NV seems to be 4x4 subrect
1354 * copied to 7x7.
1355 *
1356 * From these, the proposal is:
1357 * . Different formats -> use util_format_translate to translate if possible for surfaces.
1358 * Accept ARGB/XRGB for Volumes. Do nothing for the other combinations
1359 * . First level copied -> the first level such that src is smaller or equal to dst first level
1360 * . number of levels copied -> as long as it fits and textures have levels
1361 * That should satisfy the constraints (and instead of crashing for some cases we return D3D_OK)
1362 */
1363
1364 last_src_level = (srcb->base.usage & D3DUSAGE_AUTOGENMIPMAP) ? 0 : srcb->base.info.last_level;
1365 last_dst_level = (dstb->base.usage & D3DUSAGE_AUTOGENMIPMAP) ? 0 : dstb->base.info.last_level;
1366
1367 for (m = 0; m <= last_src_level; ++m) {
1368 unsigned w = u_minify(srcb->base.info.width0, m);
1369 unsigned h = u_minify(srcb->base.info.height0, m);
1370 unsigned d = u_minify(srcb->base.info.depth0, m);
1371
1372 if (w <= dstb->base.info.width0 &&
1373 h <= dstb->base.info.height0 &&
1374 d <= dstb->base.info.depth0)
1375 break;
1376 }
1377 user_assert(m <= last_src_level, D3D_OK);
1378
1379 last_dst_level = MIN2(srcb->base.info.last_level - m, last_dst_level);
1380
1381 if (dstb->base.type == D3DRTYPE_TEXTURE) {
1382 struct NineTexture9 *dst = NineTexture9(dstb);
1383 struct NineTexture9 *src = NineTexture9(srcb);
1384
1385 if (src->dirty_rect.width == 0)
1386 return D3D_OK;
1387
1388 pipe_box_to_rect(&rect, &src->dirty_rect);
1389 for (l = 0; l < m; ++l)
1390 rect_minify_inclusive(&rect);
1391
1392 for (l = 0; l <= last_dst_level; ++l, ++m) {
1393 fit_rect_format_inclusive(dst->base.base.info.format,
1394 &rect,
1395 dst->surfaces[l]->desc.Width,
1396 dst->surfaces[l]->desc.Height);
1397 NineSurface9_CopyMemToDefault(dst->surfaces[l],
1398 src->surfaces[m],
1399 (POINT *)&rect,
1400 &rect);
1401 rect_minify_inclusive(&rect);
1402 }
1403 u_box_origin_2d(0, 0, &src->dirty_rect);
1404 } else
1405 if (dstb->base.type == D3DRTYPE_CUBETEXTURE) {
1406 struct NineCubeTexture9 *dst = NineCubeTexture9(dstb);
1407 struct NineCubeTexture9 *src = NineCubeTexture9(srcb);
1408 unsigned z;
1409
1410 /* GPUs usually have them stored as arrays of mip-mapped 2D textures. */
1411 for (z = 0; z < 6; ++z) {
1412 if (src->dirty_rect[z].width == 0)
1413 continue;
1414
1415 pipe_box_to_rect(&rect, &src->dirty_rect[z]);
1416 for (l = 0; l < m; ++l)
1417 rect_minify_inclusive(&rect);
1418
1419 for (l = 0; l <= last_dst_level; ++l, ++m) {
1420 fit_rect_format_inclusive(dst->base.base.info.format,
1421 &rect,
1422 dst->surfaces[l * 6 + z]->desc.Width,
1423 dst->surfaces[l * 6 + z]->desc.Height);
1424 NineSurface9_CopyMemToDefault(dst->surfaces[l * 6 + z],
1425 src->surfaces[m * 6 + z],
1426 (POINT *)&rect,
1427 &rect);
1428 rect_minify_inclusive(&rect);
1429 }
1430 u_box_origin_2d(0, 0, &src->dirty_rect[z]);
1431 m -= l;
1432 }
1433 } else
1434 if (dstb->base.type == D3DRTYPE_VOLUMETEXTURE) {
1435 struct NineVolumeTexture9 *dst = NineVolumeTexture9(dstb);
1436 struct NineVolumeTexture9 *src = NineVolumeTexture9(srcb);
1437
1438 if (src->dirty_box.width == 0)
1439 return D3D_OK;
1440 for (l = 0; l <= last_dst_level; ++l, ++m)
1441 NineVolume9_CopyMemToDefault(dst->volumes[l],
1442 src->volumes[m], 0, 0, 0, NULL);
1443 u_box_3d(0, 0, 0, 0, 0, 0, &src->dirty_box);
1444 } else{
1445 assert(!"invalid texture type");
1446 }
1447
1448 if (dstb->base.usage & D3DUSAGE_AUTOGENMIPMAP) {
1449 dstb->dirty_mip = TRUE;
1450 NineBaseTexture9_GenerateMipSubLevels(dstb);
1451 }
1452
1453 return D3D_OK;
1454 }
1455
1456 HRESULT NINE_WINAPI
1457 NineDevice9_GetRenderTargetData( struct NineDevice9 *This,
1458 IDirect3DSurface9 *pRenderTarget,
1459 IDirect3DSurface9 *pDestSurface )
1460 {
1461 struct NineSurface9 *dst = NineSurface9(pDestSurface);
1462 struct NineSurface9 *src = NineSurface9(pRenderTarget);
1463
1464 DBG("This=%p pRenderTarget=%p pDestSurface=%p\n",
1465 This, pRenderTarget, pDestSurface);
1466
1467 user_assert(pRenderTarget && pDestSurface, D3DERR_INVALIDCALL);
1468
1469 user_assert(dst->desc.Pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1470 user_assert(src->desc.Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1471
1472 user_assert(dst->desc.MultiSampleType < 2, D3DERR_INVALIDCALL);
1473 user_assert(src->desc.MultiSampleType < 2, D3DERR_INVALIDCALL);
1474
1475 user_assert(src->desc.Width == dst->desc.Width, D3DERR_INVALIDCALL);
1476 user_assert(src->desc.Height == dst->desc.Height, D3DERR_INVALIDCALL);
1477
1478 user_assert(src->desc.Format != D3DFMT_NULL, D3DERR_INVALIDCALL);
1479
1480 NineSurface9_CopyDefaultToMem(dst, src);
1481
1482 return D3D_OK;
1483 }
1484
1485 HRESULT NINE_WINAPI
1486 NineDevice9_GetFrontBufferData( struct NineDevice9 *This,
1487 UINT iSwapChain,
1488 IDirect3DSurface9 *pDestSurface )
1489 {
1490 DBG("This=%p iSwapChain=%u pDestSurface=%p\n", This,
1491 iSwapChain, pDestSurface);
1492
1493 user_assert(pDestSurface != NULL, D3DERR_INVALIDCALL);
1494 user_assert(iSwapChain < This->nswapchains, D3DERR_INVALIDCALL);
1495
1496 return NineSwapChain9_GetFrontBufferData(This->swapchains[iSwapChain],
1497 pDestSurface);
1498 }
1499
1500 HRESULT NINE_WINAPI
1501 NineDevice9_StretchRect( struct NineDevice9 *This,
1502 IDirect3DSurface9 *pSourceSurface,
1503 const RECT *pSourceRect,
1504 IDirect3DSurface9 *pDestSurface,
1505 const RECT *pDestRect,
1506 D3DTEXTUREFILTERTYPE Filter )
1507 {
1508 struct pipe_screen *screen = This->screen;
1509 struct pipe_context *pipe = This->pipe;
1510 struct NineSurface9 *dst = NineSurface9(pDestSurface);
1511 struct NineSurface9 *src = NineSurface9(pSourceSurface);
1512 struct pipe_resource *dst_res = NineSurface9_GetResource(dst);
1513 struct pipe_resource *src_res = NineSurface9_GetResource(src);
1514 boolean zs;
1515 struct pipe_blit_info blit;
1516 boolean scaled, clamped, ms, flip_x = FALSE, flip_y = FALSE;
1517
1518 DBG("This=%p pSourceSurface=%p pSourceRect=%p pDestSurface=%p "
1519 "pDestRect=%p Filter=%u\n",
1520 This, pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter);
1521 if (pSourceRect)
1522 DBG("pSourceRect=(%u,%u)-(%u,%u)\n",
1523 pSourceRect->left, pSourceRect->top,
1524 pSourceRect->right, pSourceRect->bottom);
1525 if (pDestRect)
1526 DBG("pDestRect=(%u,%u)-(%u,%u)\n", pDestRect->left, pDestRect->top,
1527 pDestRect->right, pDestRect->bottom);
1528
1529 user_assert(dst->base.pool == D3DPOOL_DEFAULT &&
1530 src->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1531 zs = util_format_is_depth_or_stencil(dst_res->format);
1532 user_assert(!zs || !This->in_scene, D3DERR_INVALIDCALL);
1533 user_assert(!zs || !pSourceRect ||
1534 (pSourceRect->left == 0 &&
1535 pSourceRect->top == 0 &&
1536 pSourceRect->right == src->desc.Width &&
1537 pSourceRect->bottom == src->desc.Height), D3DERR_INVALIDCALL);
1538 user_assert(!zs || !pDestRect ||
1539 (pDestRect->left == 0 &&
1540 pDestRect->top == 0 &&
1541 pDestRect->right == dst->desc.Width &&
1542 pDestRect->bottom == dst->desc.Height), D3DERR_INVALIDCALL);
1543 user_assert(!zs ||
1544 (dst->desc.Width == src->desc.Width &&
1545 dst->desc.Height == src->desc.Height), D3DERR_INVALIDCALL);
1546 user_assert(zs || !util_format_is_depth_or_stencil(src_res->format),
1547 D3DERR_INVALIDCALL);
1548 user_assert(!zs || dst->desc.Format == src->desc.Format,
1549 D3DERR_INVALIDCALL);
1550 user_assert(screen->is_format_supported(screen, src_res->format,
1551 src_res->target,
1552 src_res->nr_samples,
1553 PIPE_BIND_SAMPLER_VIEW),
1554 D3DERR_INVALIDCALL);
1555
1556 /* We might want to permit these, but wine thinks we shouldn't. */
1557 user_assert(!pDestRect ||
1558 (pDestRect->left <= pDestRect->right &&
1559 pDestRect->top <= pDestRect->bottom), D3DERR_INVALIDCALL);
1560 user_assert(!pSourceRect ||
1561 (pSourceRect->left <= pSourceRect->right &&
1562 pSourceRect->top <= pSourceRect->bottom), D3DERR_INVALIDCALL);
1563
1564 memset(&blit, 0, sizeof(blit));
1565 blit.dst.resource = dst_res;
1566 blit.dst.level = dst->level;
1567 blit.dst.box.z = dst->layer;
1568 blit.dst.box.depth = 1;
1569 blit.dst.format = dst_res->format;
1570 if (pDestRect) {
1571 flip_x = pDestRect->left > pDestRect->right;
1572 if (flip_x) {
1573 blit.dst.box.x = pDestRect->right;
1574 blit.dst.box.width = pDestRect->left - pDestRect->right;
1575 } else {
1576 blit.dst.box.x = pDestRect->left;
1577 blit.dst.box.width = pDestRect->right - pDestRect->left;
1578 }
1579 flip_y = pDestRect->top > pDestRect->bottom;
1580 if (flip_y) {
1581 blit.dst.box.y = pDestRect->bottom;
1582 blit.dst.box.height = pDestRect->top - pDestRect->bottom;
1583 } else {
1584 blit.dst.box.y = pDestRect->top;
1585 blit.dst.box.height = pDestRect->bottom - pDestRect->top;
1586 }
1587 } else {
1588 blit.dst.box.x = 0;
1589 blit.dst.box.y = 0;
1590 blit.dst.box.width = dst->desc.Width;
1591 blit.dst.box.height = dst->desc.Height;
1592 }
1593 blit.src.resource = src_res;
1594 blit.src.level = src->level;
1595 blit.src.box.z = src->layer;
1596 blit.src.box.depth = 1;
1597 blit.src.format = src_res->format;
1598 if (pSourceRect) {
1599 if (flip_x ^ (pSourceRect->left > pSourceRect->right)) {
1600 blit.src.box.x = pSourceRect->right;
1601 blit.src.box.width = pSourceRect->left - pSourceRect->right;
1602 } else {
1603 blit.src.box.x = pSourceRect->left;
1604 blit.src.box.width = pSourceRect->right - pSourceRect->left;
1605 }
1606 if (flip_y ^ (pSourceRect->top > pSourceRect->bottom)) {
1607 blit.src.box.y = pSourceRect->bottom;
1608 blit.src.box.height = pSourceRect->top - pSourceRect->bottom;
1609 } else {
1610 blit.src.box.y = pSourceRect->top;
1611 blit.src.box.height = pSourceRect->bottom - pSourceRect->top;
1612 }
1613 } else {
1614 blit.src.box.x = flip_x ? src->desc.Width : 0;
1615 blit.src.box.y = flip_y ? src->desc.Height : 0;
1616 blit.src.box.width = flip_x ? -src->desc.Width : src->desc.Width;
1617 blit.src.box.height = flip_y ? -src->desc.Height : src->desc.Height;
1618 }
1619 blit.mask = zs ? PIPE_MASK_ZS : PIPE_MASK_RGBA;
1620 blit.filter = Filter == D3DTEXF_LINEAR ?
1621 PIPE_TEX_FILTER_LINEAR : PIPE_TEX_FILTER_NEAREST;
1622 blit.scissor_enable = FALSE;
1623 blit.alpha_blend = FALSE;
1624
1625 /* If both of a src and dst dimension are negative, flip them. */
1626 if (blit.dst.box.width < 0 && blit.src.box.width < 0) {
1627 blit.dst.box.width = -blit.dst.box.width;
1628 blit.src.box.width = -blit.src.box.width;
1629 }
1630 if (blit.dst.box.height < 0 && blit.src.box.height < 0) {
1631 blit.dst.box.height = -blit.dst.box.height;
1632 blit.src.box.height = -blit.src.box.height;
1633 }
1634 scaled =
1635 blit.dst.box.width != blit.src.box.width ||
1636 blit.dst.box.height != blit.src.box.height;
1637
1638 user_assert(!scaled || dst != src, D3DERR_INVALIDCALL);
1639 user_assert(!scaled ||
1640 !NineSurface9_IsOffscreenPlain(dst), D3DERR_INVALIDCALL);
1641 user_assert(!NineSurface9_IsOffscreenPlain(dst) ||
1642 NineSurface9_IsOffscreenPlain(src), D3DERR_INVALIDCALL);
1643 user_assert(NineSurface9_IsOffscreenPlain(dst) ||
1644 dst->desc.Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL),
1645 D3DERR_INVALIDCALL);
1646 user_assert(!scaled ||
1647 (!util_format_is_compressed(dst->base.info.format) &&
1648 !util_format_is_compressed(src->base.info.format)),
1649 D3DERR_INVALIDCALL);
1650
1651 user_warn(src == dst &&
1652 u_box_test_intersection_2d(&blit.src.box, &blit.dst.box));
1653
1654 /* Check for clipping/clamping: */
1655 {
1656 struct pipe_box box;
1657 int xy;
1658
1659 xy = u_box_clip_2d(&box, &blit.dst.box,
1660 dst->desc.Width, dst->desc.Height);
1661 if (xy < 0)
1662 return D3D_OK;
1663 if (xy == 0)
1664 xy = u_box_clip_2d(&box, &blit.src.box,
1665 src->desc.Width, src->desc.Height);
1666 clamped = !!xy;
1667 }
1668
1669 ms = (dst->desc.MultiSampleType != src->desc.MultiSampleType) ||
1670 (dst->desc.MultiSampleQuality != src->desc.MultiSampleQuality);
1671
1672 if (clamped || scaled || (blit.dst.format != blit.src.format) || ms) {
1673 DBG("using pipe->blit()\n");
1674 /* TODO: software scaling */
1675 user_assert(screen->is_format_supported(screen, dst_res->format,
1676 dst_res->target,
1677 dst_res->nr_samples,
1678 zs ? PIPE_BIND_DEPTH_STENCIL :
1679 PIPE_BIND_RENDER_TARGET),
1680 D3DERR_INVALIDCALL);
1681
1682 pipe->blit(pipe, &blit);
1683 } else {
1684 assert(blit.dst.box.x >= 0 && blit.dst.box.y >= 0 &&
1685 blit.src.box.x >= 0 && blit.src.box.y >= 0 &&
1686 blit.dst.box.x + blit.dst.box.width <= dst->desc.Width &&
1687 blit.src.box.x + blit.src.box.width <= src->desc.Width &&
1688 blit.dst.box.y + blit.dst.box.height <= dst->desc.Height &&
1689 blit.src.box.y + blit.src.box.height <= src->desc.Height);
1690 /* Or drivers might crash ... */
1691 DBG("Using resource_copy_region.\n");
1692 pipe->resource_copy_region(pipe,
1693 blit.dst.resource, blit.dst.level,
1694 blit.dst.box.x, blit.dst.box.y, blit.dst.box.z,
1695 blit.src.resource, blit.src.level,
1696 &blit.src.box);
1697 }
1698
1699 /* Communicate the container it needs to update sublevels - if apply */
1700 NineSurface9_MarkContainerDirty(dst);
1701
1702 return D3D_OK;
1703 }
1704
1705 HRESULT NINE_WINAPI
1706 NineDevice9_ColorFill( struct NineDevice9 *This,
1707 IDirect3DSurface9 *pSurface,
1708 const RECT *pRect,
1709 D3DCOLOR color )
1710 {
1711 struct pipe_context *pipe = This->pipe;
1712 struct NineSurface9 *surf = NineSurface9(pSurface);
1713 struct pipe_surface *psurf;
1714 unsigned x, y, w, h;
1715 union pipe_color_union rgba;
1716 boolean fallback;
1717
1718 DBG("This=%p pSurface=%p pRect=%p color=%08x\n", This,
1719 pSurface, pRect, color);
1720 if (pRect)
1721 DBG("pRect=(%u,%u)-(%u,%u)\n", pRect->left, pRect->top,
1722 pRect->right, pRect->bottom);
1723
1724 user_assert(surf->base.pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
1725
1726 user_assert((surf->base.usage & D3DUSAGE_RENDERTARGET) ||
1727 NineSurface9_IsOffscreenPlain(surf), D3DERR_INVALIDCALL);
1728
1729 user_assert(surf->desc.Format != D3DFMT_NULL, D3D_OK);
1730
1731 if (pRect) {
1732 x = pRect->left;
1733 y = pRect->top;
1734 w = pRect->right - pRect->left;
1735 h = pRect->bottom - pRect->top;
1736 /* Wine tests: */
1737 if (compressed_format(surf->desc.Format)) {
1738 const unsigned bw = util_format_get_blockwidth(surf->base.info.format);
1739 const unsigned bh = util_format_get_blockheight(surf->base.info.format);
1740
1741 user_assert(!(x % bw) && !(y % bh) && !(w % bw) && !(h % bh),
1742 D3DERR_INVALIDCALL);
1743 }
1744 } else{
1745 x = 0;
1746 y = 0;
1747 w = surf->desc.Width;
1748 h = surf->desc.Height;
1749 }
1750 d3dcolor_to_pipe_color_union(&rgba, color);
1751
1752 fallback = !(surf->base.info.bind & PIPE_BIND_RENDER_TARGET);
1753
1754 if (!fallback) {
1755 psurf = NineSurface9_GetSurface(surf, 0);
1756 if (!psurf)
1757 fallback = TRUE;
1758 }
1759
1760 if (!fallback) {
1761 pipe->clear_render_target(pipe, psurf, &rgba, x, y, w, h, false);
1762 } else {
1763 D3DLOCKED_RECT lock;
1764 union util_color uc;
1765 HRESULT hr;
1766 /* XXX: lock pRect and fix util_fill_rect */
1767 hr = NineSurface9_LockRect(surf, &lock, NULL, 0);
1768 if (FAILED(hr))
1769 return hr;
1770 util_pack_color_ub(color >> 16, color >> 8, color >> 0, color >> 24,
1771 surf->base.info.format, &uc);
1772 util_fill_rect(lock.pBits, surf->base.info.format,lock.Pitch,
1773 x, y, w, h, &uc);
1774 NineSurface9_UnlockRect(surf);
1775 }
1776
1777 return D3D_OK;
1778 }
1779
1780 HRESULT NINE_WINAPI
1781 NineDevice9_CreateOffscreenPlainSurface( struct NineDevice9 *This,
1782 UINT Width,
1783 UINT Height,
1784 D3DFORMAT Format,
1785 D3DPOOL Pool,
1786 IDirect3DSurface9 **ppSurface,
1787 HANDLE *pSharedHandle )
1788 {
1789 HRESULT hr;
1790
1791 DBG("This=%p Width=%u Height=%u Format=%s(0x%x) Pool=%u "
1792 "ppSurface=%p pSharedHandle=%p\n", This,
1793 Width, Height, d3dformat_to_string(Format), Format, Pool,
1794 ppSurface, pSharedHandle);
1795
1796 *ppSurface = NULL;
1797 user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT
1798 || Pool == D3DPOOL_SYSTEMMEM, D3DERR_INVALIDCALL);
1799 user_assert(Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
1800
1801 /* Can be used with StretchRect and ColorFill. It's also always lockable.
1802 */
1803 hr = create_zs_or_rt_surface(This, 2, Pool, Width, Height,
1804 Format,
1805 D3DMULTISAMPLE_NONE, 0,
1806 TRUE,
1807 ppSurface, pSharedHandle);
1808 if (FAILED(hr))
1809 DBG("Failed to create surface.\n");
1810 return hr;
1811 }
1812
1813 HRESULT NINE_WINAPI
1814 NineDevice9_SetRenderTarget( struct NineDevice9 *This,
1815 DWORD RenderTargetIndex,
1816 IDirect3DSurface9 *pRenderTarget )
1817 {
1818 struct NineSurface9 *rt = NineSurface9(pRenderTarget);
1819 const unsigned i = RenderTargetIndex;
1820
1821 DBG("This=%p RenderTargetIndex=%u pRenderTarget=%p\n", This,
1822 RenderTargetIndex, pRenderTarget);
1823
1824 user_assert(i < This->caps.NumSimultaneousRTs, D3DERR_INVALIDCALL);
1825 user_assert(i != 0 || pRenderTarget, D3DERR_INVALIDCALL);
1826 user_assert(!pRenderTarget ||
1827 rt->desc.Usage & D3DUSAGE_RENDERTARGET, D3DERR_INVALIDCALL);
1828
1829 if (i == 0) {
1830 This->state.viewport.X = 0;
1831 This->state.viewport.Y = 0;
1832 This->state.viewport.Width = rt->desc.Width;
1833 This->state.viewport.Height = rt->desc.Height;
1834 This->state.viewport.MinZ = 0.0f;
1835 This->state.viewport.MaxZ = 1.0f;
1836
1837 This->state.scissor.minx = 0;
1838 This->state.scissor.miny = 0;
1839 This->state.scissor.maxx = rt->desc.Width;
1840 This->state.scissor.maxy = rt->desc.Height;
1841
1842 This->state.changed.group |= NINE_STATE_VIEWPORT | NINE_STATE_SCISSOR | NINE_STATE_MULTISAMPLE;
1843
1844 if (This->state.rt[0] &&
1845 (This->state.rt[0]->desc.MultiSampleType <= D3DMULTISAMPLE_NONMASKABLE) !=
1846 (rt->desc.MultiSampleType <= D3DMULTISAMPLE_NONMASKABLE))
1847 This->state.changed.group |= NINE_STATE_SAMPLE_MASK;
1848 }
1849
1850 if (This->state.rt[i] != NineSurface9(pRenderTarget)) {
1851 nine_bind(&This->state.rt[i], pRenderTarget);
1852 This->state.changed.group |= NINE_STATE_FB;
1853 }
1854 return D3D_OK;
1855 }
1856
1857 HRESULT NINE_WINAPI
1858 NineDevice9_GetRenderTarget( struct NineDevice9 *This,
1859 DWORD RenderTargetIndex,
1860 IDirect3DSurface9 **ppRenderTarget )
1861 {
1862 const unsigned i = RenderTargetIndex;
1863
1864 user_assert(i < This->caps.NumSimultaneousRTs, D3DERR_INVALIDCALL);
1865 user_assert(ppRenderTarget, D3DERR_INVALIDCALL);
1866
1867 *ppRenderTarget = (IDirect3DSurface9 *)This->state.rt[i];
1868 if (!This->state.rt[i])
1869 return D3DERR_NOTFOUND;
1870
1871 NineUnknown_AddRef(NineUnknown(This->state.rt[i]));
1872 return D3D_OK;
1873 }
1874
1875 HRESULT NINE_WINAPI
1876 NineDevice9_SetDepthStencilSurface( struct NineDevice9 *This,
1877 IDirect3DSurface9 *pNewZStencil )
1878 {
1879 DBG("This=%p pNewZStencil=%p\n", This, pNewZStencil);
1880
1881 if (This->state.ds != NineSurface9(pNewZStencil)) {
1882 nine_bind(&This->state.ds, pNewZStencil);
1883 This->state.changed.group |= NINE_STATE_FB;
1884 }
1885 return D3D_OK;
1886 }
1887
1888 HRESULT NINE_WINAPI
1889 NineDevice9_GetDepthStencilSurface( struct NineDevice9 *This,
1890 IDirect3DSurface9 **ppZStencilSurface )
1891 {
1892 user_assert(ppZStencilSurface, D3DERR_INVALIDCALL);
1893
1894 *ppZStencilSurface = (IDirect3DSurface9 *)This->state.ds;
1895 if (!This->state.ds)
1896 return D3DERR_NOTFOUND;
1897
1898 NineUnknown_AddRef(NineUnknown(This->state.ds));
1899 return D3D_OK;
1900 }
1901
1902 HRESULT NINE_WINAPI
1903 NineDevice9_BeginScene( struct NineDevice9 *This )
1904 {
1905 DBG("This=%p\n", This);
1906 user_assert(!This->in_scene, D3DERR_INVALIDCALL);
1907 This->in_scene = TRUE;
1908 /* Do we want to do anything else here ? */
1909 return D3D_OK;
1910 }
1911
1912 HRESULT NINE_WINAPI
1913 NineDevice9_EndScene( struct NineDevice9 *This )
1914 {
1915 DBG("This=%p\n", This);
1916 user_assert(This->in_scene, D3DERR_INVALIDCALL);
1917 This->in_scene = FALSE;
1918 return D3D_OK;
1919 }
1920
1921 HRESULT NINE_WINAPI
1922 NineDevice9_Clear( struct NineDevice9 *This,
1923 DWORD Count,
1924 const D3DRECT *pRects,
1925 DWORD Flags,
1926 D3DCOLOR Color,
1927 float Z,
1928 DWORD Stencil )
1929 {
1930 struct NineSurface9 *zsbuf_surf = This->state.ds;
1931
1932 DBG("This=%p Count=%u pRects=%p Flags=%x Color=%08x Z=%f Stencil=%x\n",
1933 This, Count, pRects, Flags, Color, Z, Stencil);
1934
1935 user_assert(This->state.ds || !(Flags & NINED3DCLEAR_DEPTHSTENCIL),
1936 D3DERR_INVALIDCALL);
1937 user_assert(!(Flags & D3DCLEAR_STENCIL) ||
1938 (zsbuf_surf &&
1939 util_format_is_depth_and_stencil(zsbuf_surf->base.info.format)),
1940 D3DERR_INVALIDCALL);
1941 #ifdef NINE_STRICT
1942 user_assert((Count && pRects) || (!Count && !pRects), D3DERR_INVALIDCALL);
1943 #else
1944 user_warn((pRects && !Count) || (!pRects && Count));
1945 if (pRects && !Count)
1946 return D3D_OK;
1947 if (!pRects)
1948 Count = 0;
1949 #endif
1950
1951 nine_context_clear_fb(This, Count, pRects, Flags, Color, Z, Stencil);
1952 return D3D_OK;
1953 }
1954
1955 HRESULT NINE_WINAPI
1956 NineDevice9_SetTransform( struct NineDevice9 *This,
1957 D3DTRANSFORMSTATETYPE State,
1958 const D3DMATRIX *pMatrix )
1959 {
1960 struct nine_state *state = This->update;
1961 D3DMATRIX *M = nine_state_access_transform(state, State, TRUE);
1962
1963 DBG("This=%p State=%d pMatrix=%p\n", This, State, pMatrix);
1964
1965 user_assert(M, D3DERR_INVALIDCALL);
1966
1967 *M = *pMatrix;
1968 state->ff.changed.transform[State / 32] |= 1 << (State % 32);
1969 state->changed.group |= NINE_STATE_FF;
1970
1971 return D3D_OK;
1972 }
1973
1974 HRESULT NINE_WINAPI
1975 NineDevice9_GetTransform( struct NineDevice9 *This,
1976 D3DTRANSFORMSTATETYPE State,
1977 D3DMATRIX *pMatrix )
1978 {
1979 D3DMATRIX *M = nine_state_access_transform(&This->state, State, FALSE);
1980 user_assert(M, D3DERR_INVALIDCALL);
1981 *pMatrix = *M;
1982 return D3D_OK;
1983 }
1984
1985 HRESULT NINE_WINAPI
1986 NineDevice9_MultiplyTransform( struct NineDevice9 *This,
1987 D3DTRANSFORMSTATETYPE State,
1988 const D3DMATRIX *pMatrix )
1989 {
1990 struct nine_state *state = This->update;
1991 D3DMATRIX T;
1992 D3DMATRIX *M = nine_state_access_transform(state, State, TRUE);
1993
1994 DBG("This=%p State=%d pMatrix=%p\n", This, State, pMatrix);
1995
1996 user_assert(M, D3DERR_INVALIDCALL);
1997
1998 nine_d3d_matrix_matrix_mul(&T, pMatrix, M);
1999 return NineDevice9_SetTransform(This, State, &T);
2000 }
2001
2002 HRESULT NINE_WINAPI
2003 NineDevice9_SetViewport( struct NineDevice9 *This,
2004 const D3DVIEWPORT9 *pViewport )
2005 {
2006 struct nine_state *state = This->update;
2007
2008 DBG("X=%u Y=%u W=%u H=%u MinZ=%f MaxZ=%f\n",
2009 pViewport->X, pViewport->Y, pViewport->Width, pViewport->Height,
2010 pViewport->MinZ, pViewport->MaxZ);
2011
2012 state->viewport = *pViewport;
2013 state->changed.group |= NINE_STATE_VIEWPORT;
2014
2015 return D3D_OK;
2016 }
2017
2018 HRESULT NINE_WINAPI
2019 NineDevice9_GetViewport( struct NineDevice9 *This,
2020 D3DVIEWPORT9 *pViewport )
2021 {
2022 *pViewport = This->state.viewport;
2023 return D3D_OK;
2024 }
2025
2026 HRESULT NINE_WINAPI
2027 NineDevice9_SetMaterial( struct NineDevice9 *This,
2028 const D3DMATERIAL9 *pMaterial )
2029 {
2030 struct nine_state *state = This->update;
2031
2032 DBG("This=%p pMaterial=%p\n", This, pMaterial);
2033 if (pMaterial)
2034 nine_dump_D3DMATERIAL9(DBG_FF, pMaterial);
2035
2036 user_assert(pMaterial, E_POINTER);
2037
2038 state->ff.material = *pMaterial;
2039 state->changed.group |= NINE_STATE_FF_MATERIAL;
2040
2041 return D3D_OK;
2042 }
2043
2044 HRESULT NINE_WINAPI
2045 NineDevice9_GetMaterial( struct NineDevice9 *This,
2046 D3DMATERIAL9 *pMaterial )
2047 {
2048 user_assert(pMaterial, E_POINTER);
2049 *pMaterial = This->state.ff.material;
2050 return D3D_OK;
2051 }
2052
2053 HRESULT NINE_WINAPI
2054 NineDevice9_SetLight( struct NineDevice9 *This,
2055 DWORD Index,
2056 const D3DLIGHT9 *pLight )
2057 {
2058 struct nine_state *state = This->update;
2059
2060 DBG("This=%p Index=%u pLight=%p\n", This, Index, pLight);
2061 if (pLight)
2062 nine_dump_D3DLIGHT9(DBG_FF, pLight);
2063
2064 user_assert(pLight, D3DERR_INVALIDCALL);
2065 user_assert(pLight->Type < NINED3DLIGHT_INVALID, D3DERR_INVALIDCALL);
2066
2067 user_assert(Index < NINE_MAX_LIGHTS, D3DERR_INVALIDCALL); /* sanity */
2068
2069 if (Index >= state->ff.num_lights) {
2070 unsigned n = state->ff.num_lights;
2071 unsigned N = Index + 1;
2072
2073 state->ff.light = REALLOC(state->ff.light, n * sizeof(D3DLIGHT9),
2074 N * sizeof(D3DLIGHT9));
2075 if (!state->ff.light)
2076 return E_OUTOFMEMORY;
2077 state->ff.num_lights = N;
2078
2079 for (; n < Index; ++n) {
2080 memset(&state->ff.light[n], 0, sizeof(D3DLIGHT9));
2081 state->ff.light[n].Type = (D3DLIGHTTYPE)NINED3DLIGHT_INVALID;
2082 }
2083 }
2084 state->ff.light[Index] = *pLight;
2085
2086 if (pLight->Type == D3DLIGHT_SPOT && pLight->Theta >= pLight->Phi) {
2087 DBG("Warning: clamping D3DLIGHT9.Theta\n");
2088 state->ff.light[Index].Theta = state->ff.light[Index].Phi;
2089 }
2090 if (pLight->Type != D3DLIGHT_DIRECTIONAL &&
2091 pLight->Attenuation0 == 0.0f &&
2092 pLight->Attenuation1 == 0.0f &&
2093 pLight->Attenuation2 == 0.0f) {
2094 DBG("Warning: all D3DLIGHT9.Attenuation[i] are 0\n");
2095 }
2096
2097 state->changed.group |= NINE_STATE_FF_LIGHTING;
2098
2099 return D3D_OK;
2100 }
2101
2102 HRESULT NINE_WINAPI
2103 NineDevice9_GetLight( struct NineDevice9 *This,
2104 DWORD Index,
2105 D3DLIGHT9 *pLight )
2106 {
2107 const struct nine_state *state = &This->state;
2108
2109 user_assert(pLight, D3DERR_INVALIDCALL);
2110 user_assert(Index < state->ff.num_lights, D3DERR_INVALIDCALL);
2111 user_assert(state->ff.light[Index].Type < NINED3DLIGHT_INVALID,
2112 D3DERR_INVALIDCALL);
2113
2114 *pLight = state->ff.light[Index];
2115
2116 return D3D_OK;
2117 }
2118
2119 HRESULT NINE_WINAPI
2120 NineDevice9_LightEnable( struct NineDevice9 *This,
2121 DWORD Index,
2122 BOOL Enable )
2123 {
2124 struct nine_state *state = This->update;
2125 unsigned i;
2126
2127 DBG("This=%p Index=%u Enable=%i\n", This, Index, Enable);
2128
2129 if (Index >= state->ff.num_lights ||
2130 state->ff.light[Index].Type == NINED3DLIGHT_INVALID) {
2131 /* This should create a default light. */
2132 D3DLIGHT9 light;
2133 memset(&light, 0, sizeof(light));
2134 light.Type = D3DLIGHT_DIRECTIONAL;
2135 light.Diffuse.r = 1.0f;
2136 light.Diffuse.g = 1.0f;
2137 light.Diffuse.b = 1.0f;
2138 light.Direction.z = 1.0f;
2139 NineDevice9_SetLight(This, Index, &light);
2140 }
2141 user_assert(Index < state->ff.num_lights, D3DERR_INVALIDCALL);
2142
2143 for (i = 0; i < state->ff.num_lights_active; ++i) {
2144 if (state->ff.active_light[i] == Index)
2145 break;
2146 }
2147
2148 if (Enable) {
2149 if (i < state->ff.num_lights_active)
2150 return D3D_OK;
2151 /* XXX wine thinks this should still succeed:
2152 */
2153 user_assert(i < NINE_MAX_LIGHTS_ACTIVE, D3DERR_INVALIDCALL);
2154
2155 state->ff.active_light[i] = Index;
2156 state->ff.num_lights_active++;
2157 } else {
2158 if (i == state->ff.num_lights_active)
2159 return D3D_OK;
2160 --state->ff.num_lights_active;
2161 for (; i < state->ff.num_lights_active; ++i)
2162 state->ff.active_light[i] = state->ff.active_light[i + 1];
2163 }
2164 state->changed.group |= NINE_STATE_FF_LIGHTING;
2165
2166 return D3D_OK;
2167 }
2168
2169 HRESULT NINE_WINAPI
2170 NineDevice9_GetLightEnable( struct NineDevice9 *This,
2171 DWORD Index,
2172 BOOL *pEnable )
2173 {
2174 const struct nine_state *state = &This->state;
2175 unsigned i;
2176
2177 user_assert(Index < state->ff.num_lights, D3DERR_INVALIDCALL);
2178 user_assert(state->ff.light[Index].Type < NINED3DLIGHT_INVALID,
2179 D3DERR_INVALIDCALL);
2180
2181 for (i = 0; i < state->ff.num_lights_active; ++i)
2182 if (state->ff.active_light[i] == Index)
2183 break;
2184
2185 *pEnable = i != state->ff.num_lights_active ? 128 : 0; // Taken from wine
2186
2187 return D3D_OK;
2188 }
2189
2190 HRESULT NINE_WINAPI
2191 NineDevice9_SetClipPlane( struct NineDevice9 *This,
2192 DWORD Index,
2193 const float *pPlane )
2194 {
2195 struct nine_state *state = This->update;
2196
2197 user_assert(pPlane, D3DERR_INVALIDCALL);
2198
2199 DBG("This=%p Index=%u pPlane=%f %f %f %f\n", This, Index,
2200 pPlane[0], pPlane[1],
2201 pPlane[2], pPlane[3]);
2202
2203 user_assert(Index < PIPE_MAX_CLIP_PLANES, D3DERR_INVALIDCALL);
2204
2205 memcpy(&state->clip.ucp[Index][0], pPlane, sizeof(state->clip.ucp[0]));
2206 state->changed.ucp |= 1 << Index;
2207
2208 return D3D_OK;
2209 }
2210
2211 HRESULT NINE_WINAPI
2212 NineDevice9_GetClipPlane( struct NineDevice9 *This,
2213 DWORD Index,
2214 float *pPlane )
2215 {
2216 const struct nine_state *state = &This->state;
2217
2218 user_assert(Index < PIPE_MAX_CLIP_PLANES, D3DERR_INVALIDCALL);
2219
2220 memcpy(pPlane, &state->clip.ucp[Index][0], sizeof(state->clip.ucp[0]));
2221 return D3D_OK;
2222 }
2223
2224 #define RESZ_CODE 0x7fa05000
2225
2226 static HRESULT
2227 NineDevice9_ResolveZ( struct NineDevice9 *This )
2228 {
2229 struct nine_state *state = &This->state;
2230 const struct util_format_description *desc;
2231 struct NineSurface9 *source = state->ds;
2232 struct NineBaseTexture9 *destination = state->texture[0];
2233 struct pipe_resource *src, *dst;
2234 struct pipe_blit_info blit;
2235
2236 DBG("RESZ resolve\n");
2237
2238 user_assert(source && destination &&
2239 destination->base.type == D3DRTYPE_TEXTURE, D3DERR_INVALIDCALL);
2240
2241 src = source->base.resource;
2242 dst = destination->base.resource;
2243
2244 user_assert(src && dst, D3DERR_INVALIDCALL);
2245
2246 /* check dst is depth format. we know already for src */
2247 desc = util_format_description(dst->format);
2248 user_assert(desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS, D3DERR_INVALIDCALL);
2249
2250 memset(&blit, 0, sizeof(blit));
2251 blit.src.resource = src;
2252 blit.src.level = 0;
2253 blit.src.format = src->format;
2254 blit.src.box.z = 0;
2255 blit.src.box.depth = 1;
2256 blit.src.box.x = 0;
2257 blit.src.box.y = 0;
2258 blit.src.box.width = src->width0;
2259 blit.src.box.height = src->height0;
2260
2261 blit.dst.resource = dst;
2262 blit.dst.level = 0;
2263 blit.dst.format = dst->format;
2264 blit.dst.box.z = 0;
2265 blit.dst.box.depth = 1;
2266 blit.dst.box.x = 0;
2267 blit.dst.box.y = 0;
2268 blit.dst.box.width = dst->width0;
2269 blit.dst.box.height = dst->height0;
2270
2271 blit.mask = PIPE_MASK_ZS;
2272 blit.filter = PIPE_TEX_FILTER_NEAREST;
2273 blit.scissor_enable = FALSE;
2274
2275 This->pipe->blit(This->pipe, &blit);
2276 return D3D_OK;
2277 }
2278
2279 #define ALPHA_TO_COVERAGE_ENABLE MAKEFOURCC('A', '2', 'M', '1')
2280 #define ALPHA_TO_COVERAGE_DISABLE MAKEFOURCC('A', '2', 'M', '0')
2281
2282 HRESULT NINE_WINAPI
2283 NineDevice9_SetRenderState( struct NineDevice9 *This,
2284 D3DRENDERSTATETYPE State,
2285 DWORD Value )
2286 {
2287 struct nine_state *state = This->update;
2288
2289 DBG("This=%p State=%u(%s) Value=%08x\n", This,
2290 State, nine_d3drs_to_string(State), Value);
2291
2292 user_assert(State < D3DRS_COUNT, D3DERR_INVALIDCALL);
2293
2294 if (state->rs_advertised[State] == Value && likely(!This->is_recording))
2295 return D3D_OK;
2296
2297 state->rs_advertised[State] = Value;
2298
2299 /* Amd hacks (equivalent to GL extensions) */
2300 if (unlikely(State == D3DRS_POINTSIZE)) {
2301 if (Value == RESZ_CODE)
2302 return NineDevice9_ResolveZ(This);
2303
2304 if (Value == ALPHA_TO_COVERAGE_ENABLE ||
2305 Value == ALPHA_TO_COVERAGE_DISABLE) {
2306 state->rs[NINED3DRS_ALPHACOVERAGE] = (Value == ALPHA_TO_COVERAGE_ENABLE);
2307 state->changed.group |= NINE_STATE_BLEND;
2308 return D3D_OK;
2309 }
2310 }
2311
2312 /* NV hack */
2313 if (unlikely(State == D3DRS_ADAPTIVETESS_Y)) {
2314 if (Value == D3DFMT_ATOC || (Value == D3DFMT_UNKNOWN && state->rs[NINED3DRS_ALPHACOVERAGE])) {
2315 state->rs[NINED3DRS_ALPHACOVERAGE] = (Value == D3DFMT_ATOC) ? 3 : 0;
2316 state->rs[NINED3DRS_ALPHACOVERAGE] &= state->rs[D3DRS_ALPHATESTENABLE] ? 3 : 2;
2317 state->changed.group |= NINE_STATE_BLEND;
2318 return D3D_OK;
2319 }
2320 }
2321 if (unlikely(State == D3DRS_ALPHATESTENABLE && (state->rs[NINED3DRS_ALPHACOVERAGE] & 2))) {
2322 DWORD alphacoverage_prev = state->rs[NINED3DRS_ALPHACOVERAGE];
2323 state->rs[NINED3DRS_ALPHACOVERAGE] = (Value ? 3 : 2);
2324 if (state->rs[NINED3DRS_ALPHACOVERAGE] != alphacoverage_prev)
2325 state->changed.group |= NINE_STATE_BLEND;
2326 }
2327
2328 state->rs[State] = nine_fix_render_state_value(State, Value);
2329 state->changed.rs[State / 32] |= 1 << (State % 32);
2330 state->changed.group |= nine_render_state_group[State];
2331
2332 return D3D_OK;
2333 }
2334
2335 HRESULT NINE_WINAPI
2336 NineDevice9_GetRenderState( struct NineDevice9 *This,
2337 D3DRENDERSTATETYPE State,
2338 DWORD *pValue )
2339 {
2340 user_assert(State < D3DRS_COUNT, D3DERR_INVALIDCALL);
2341
2342 *pValue = This->state.rs_advertised[State];
2343 return D3D_OK;
2344 }
2345
2346 HRESULT NINE_WINAPI
2347 NineDevice9_CreateStateBlock( struct NineDevice9 *This,
2348 D3DSTATEBLOCKTYPE Type,
2349 IDirect3DStateBlock9 **ppSB )
2350 {
2351 struct NineStateBlock9 *nsb;
2352 struct nine_state *dst;
2353 HRESULT hr;
2354 enum nine_stateblock_type type;
2355 unsigned s;
2356
2357 DBG("This=%p Type=%u ppSB=%p\n", This, Type, ppSB);
2358
2359 user_assert(Type == D3DSBT_ALL ||
2360 Type == D3DSBT_VERTEXSTATE ||
2361 Type == D3DSBT_PIXELSTATE, D3DERR_INVALIDCALL);
2362
2363 switch (Type) {
2364 case D3DSBT_VERTEXSTATE: type = NINESBT_VERTEXSTATE; break;
2365 case D3DSBT_PIXELSTATE: type = NINESBT_PIXELSTATE; break;
2366 default:
2367 type = NINESBT_ALL;
2368 break;
2369 }
2370
2371 hr = NineStateBlock9_new(This, &nsb, type);
2372 if (FAILED(hr))
2373 return hr;
2374 *ppSB = (IDirect3DStateBlock9 *)nsb;
2375 dst = &nsb->state;
2376
2377 dst->changed.group =
2378 NINE_STATE_TEXTURE |
2379 NINE_STATE_SAMPLER;
2380
2381 if (Type == D3DSBT_ALL || Type == D3DSBT_VERTEXSTATE) {
2382 dst->changed.group |=
2383 NINE_STATE_FF_LIGHTING |
2384 NINE_STATE_VS | NINE_STATE_VS_CONST |
2385 NINE_STATE_VDECL;
2386 /* TODO: texture/sampler state */
2387 memcpy(dst->changed.rs,
2388 nine_render_states_vertex, sizeof(dst->changed.rs));
2389 nine_ranges_insert(&dst->changed.vs_const_f, 0, This->may_swvp ? NINE_MAX_CONST_F_SWVP : This->max_vs_const_f,
2390 &This->range_pool);
2391 nine_ranges_insert(&dst->changed.vs_const_i, 0, This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I,
2392 &This->range_pool);
2393 nine_ranges_insert(&dst->changed.vs_const_b, 0, This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B,
2394 &This->range_pool);
2395 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2396 dst->changed.sampler[s] |= 1 << D3DSAMP_DMAPOFFSET;
2397 if (This->state.ff.num_lights) {
2398 dst->ff.num_lights = This->state.ff.num_lights;
2399 /* zero'd -> light type won't be NINED3DLIGHT_INVALID, so
2400 * all currently existing lights will be captured
2401 */
2402 dst->ff.light = CALLOC(This->state.ff.num_lights,
2403 sizeof(D3DLIGHT9));
2404 if (!dst->ff.light) {
2405 nine_bind(ppSB, NULL);
2406 return E_OUTOFMEMORY;
2407 }
2408 }
2409 }
2410 if (Type == D3DSBT_ALL || Type == D3DSBT_PIXELSTATE) {
2411 dst->changed.group |=
2412 NINE_STATE_PS | NINE_STATE_PS_CONST | NINE_STATE_BLEND |
2413 NINE_STATE_FF_OTHER | NINE_STATE_FF_PSSTAGES | NINE_STATE_PS_CONST |
2414 NINE_STATE_FB | NINE_STATE_DSA | NINE_STATE_MULTISAMPLE |
2415 NINE_STATE_RASTERIZER | NINE_STATE_STENCIL_REF;
2416 memcpy(dst->changed.rs,
2417 nine_render_states_pixel, sizeof(dst->changed.rs));
2418 nine_ranges_insert(&dst->changed.ps_const_f, 0, This->max_ps_const_f,
2419 &This->range_pool);
2420 dst->changed.ps_const_i = 0xffff;
2421 dst->changed.ps_const_b = 0xffff;
2422 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2423 dst->changed.sampler[s] |= 0x1ffe;
2424 for (s = 0; s < NINE_MAX_TEXTURE_STAGES; ++s) {
2425 dst->ff.changed.tex_stage[s][0] |= 0xffffffff;
2426 dst->ff.changed.tex_stage[s][1] |= 0xffffffff;
2427 }
2428 }
2429 if (Type == D3DSBT_ALL) {
2430 dst->changed.group |=
2431 NINE_STATE_VIEWPORT |
2432 NINE_STATE_SCISSOR |
2433 NINE_STATE_RASTERIZER |
2434 NINE_STATE_BLEND |
2435 NINE_STATE_DSA |
2436 NINE_STATE_IDXBUF |
2437 NINE_STATE_MATERIAL |
2438 NINE_STATE_BLEND_COLOR |
2439 NINE_STATE_SAMPLE_MASK;
2440 memset(dst->changed.rs, ~0, (D3DRS_COUNT / 32) * sizeof(uint32_t));
2441 dst->changed.rs[D3DRS_LAST / 32] |= (1 << (D3DRS_COUNT % 32)) - 1;
2442 dst->changed.vtxbuf = (1ULL << This->caps.MaxStreams) - 1;
2443 dst->changed.stream_freq = dst->changed.vtxbuf;
2444 dst->changed.ucp = (1 << PIPE_MAX_CLIP_PLANES) - 1;
2445 dst->changed.texture = (1 << NINE_MAX_SAMPLERS) - 1;
2446 }
2447 NineStateBlock9_Capture(NineStateBlock9(*ppSB));
2448
2449 /* TODO: fixed function state */
2450
2451 return D3D_OK;
2452 }
2453
2454 HRESULT NINE_WINAPI
2455 NineDevice9_BeginStateBlock( struct NineDevice9 *This )
2456 {
2457 HRESULT hr;
2458
2459 DBG("This=%p\n", This);
2460
2461 user_assert(!This->record, D3DERR_INVALIDCALL);
2462
2463 hr = NineStateBlock9_new(This, &This->record, NINESBT_CUSTOM);
2464 if (FAILED(hr))
2465 return hr;
2466 NineUnknown_ConvertRefToBind(NineUnknown(This->record));
2467
2468 This->update = &This->record->state;
2469 This->is_recording = TRUE;
2470
2471 return D3D_OK;
2472 }
2473
2474 HRESULT NINE_WINAPI
2475 NineDevice9_EndStateBlock( struct NineDevice9 *This,
2476 IDirect3DStateBlock9 **ppSB )
2477 {
2478 DBG("This=%p ppSB=%p\n", This, ppSB);
2479
2480 user_assert(This->record, D3DERR_INVALIDCALL);
2481
2482 This->update = &This->state;
2483 This->is_recording = FALSE;
2484
2485 NineUnknown_AddRef(NineUnknown(This->record));
2486 *ppSB = (IDirect3DStateBlock9 *)This->record;
2487 NineUnknown_Unbind(NineUnknown(This->record));
2488 This->record = NULL;
2489
2490 return D3D_OK;
2491 }
2492
2493 HRESULT NINE_WINAPI
2494 NineDevice9_SetClipStatus( struct NineDevice9 *This,
2495 const D3DCLIPSTATUS9 *pClipStatus )
2496 {
2497 STUB(D3DERR_INVALIDCALL);
2498 }
2499
2500 HRESULT NINE_WINAPI
2501 NineDevice9_GetClipStatus( struct NineDevice9 *This,
2502 D3DCLIPSTATUS9 *pClipStatus )
2503 {
2504 STUB(D3DERR_INVALIDCALL);
2505 }
2506
2507 HRESULT NINE_WINAPI
2508 NineDevice9_GetTexture( struct NineDevice9 *This,
2509 DWORD Stage,
2510 IDirect3DBaseTexture9 **ppTexture )
2511 {
2512 user_assert(Stage < This->caps.MaxSimultaneousTextures ||
2513 Stage == D3DDMAPSAMPLER ||
2514 (Stage >= D3DVERTEXTEXTURESAMPLER0 &&
2515 Stage <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2516 user_assert(ppTexture, D3DERR_INVALIDCALL);
2517
2518 if (Stage >= D3DDMAPSAMPLER)
2519 Stage = Stage - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2520
2521 *ppTexture = (IDirect3DBaseTexture9 *)This->state.texture[Stage];
2522
2523 if (This->state.texture[Stage])
2524 NineUnknown_AddRef(NineUnknown(This->state.texture[Stage]));
2525 return D3D_OK;
2526 }
2527
2528 HRESULT NINE_WINAPI
2529 NineDevice9_SetTexture( struct NineDevice9 *This,
2530 DWORD Stage,
2531 IDirect3DBaseTexture9 *pTexture )
2532 {
2533 struct nine_state *state = This->update;
2534 struct NineBaseTexture9 *tex = NineBaseTexture9(pTexture);
2535
2536 DBG("This=%p Stage=%u pTexture=%p\n", This, Stage, pTexture);
2537
2538 user_assert(Stage < This->caps.MaxSimultaneousTextures ||
2539 Stage == D3DDMAPSAMPLER ||
2540 (Stage >= D3DVERTEXTEXTURESAMPLER0 &&
2541 Stage <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2542 user_assert(!tex || (tex->base.pool != D3DPOOL_SCRATCH &&
2543 tex->base.pool != D3DPOOL_SYSTEMMEM), D3DERR_INVALIDCALL);
2544
2545 if (Stage >= D3DDMAPSAMPLER)
2546 Stage = Stage - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2547
2548 if (!This->is_recording) {
2549 struct NineBaseTexture9 *old = state->texture[Stage];
2550 if (old == tex)
2551 return D3D_OK;
2552
2553 state->samplers_shadow &= ~(1 << Stage);
2554 if (tex) {
2555 state->samplers_shadow |= tex->shadow << Stage;
2556
2557 if ((tex->managed.dirty | tex->dirty_mip) && LIST_IS_EMPTY(&tex->list))
2558 list_add(&tex->list, &This->update_textures);
2559
2560 tex->bind_count++;
2561 }
2562 if (old)
2563 old->bind_count--;
2564 }
2565 nine_bind(&state->texture[Stage], pTexture);
2566
2567 state->changed.texture |= 1 << Stage;
2568 state->changed.group |= NINE_STATE_TEXTURE;
2569
2570 return D3D_OK;
2571 }
2572
2573 HRESULT NINE_WINAPI
2574 NineDevice9_GetTextureStageState( struct NineDevice9 *This,
2575 DWORD Stage,
2576 D3DTEXTURESTAGESTATETYPE Type,
2577 DWORD *pValue )
2578 {
2579 const struct nine_state *state = &This->state;
2580
2581 user_assert(Stage < ARRAY_SIZE(state->ff.tex_stage), D3DERR_INVALIDCALL);
2582 user_assert(Type < ARRAY_SIZE(state->ff.tex_stage[0]), D3DERR_INVALIDCALL);
2583
2584 *pValue = state->ff.tex_stage[Stage][Type];
2585
2586 return D3D_OK;
2587 }
2588
2589 HRESULT NINE_WINAPI
2590 NineDevice9_SetTextureStageState( struct NineDevice9 *This,
2591 DWORD Stage,
2592 D3DTEXTURESTAGESTATETYPE Type,
2593 DWORD Value )
2594 {
2595 struct nine_state *state = This->update;
2596 struct nine_context *context = &This->context;
2597 int bumpmap_index = -1;
2598
2599 DBG("Stage=%u Type=%u Value=%08x\n", Stage, Type, Value);
2600 nine_dump_D3DTSS_value(DBG_FF, Type, Value);
2601
2602 user_assert(Stage < ARRAY_SIZE(state->ff.tex_stage), D3DERR_INVALIDCALL);
2603 user_assert(Type < ARRAY_SIZE(state->ff.tex_stage[0]), D3DERR_INVALIDCALL);
2604
2605 state->ff.tex_stage[Stage][Type] = Value;
2606 switch (Type) {
2607 case D3DTSS_BUMPENVMAT00:
2608 bumpmap_index = 4 * Stage;
2609 break;
2610 case D3DTSS_BUMPENVMAT01:
2611 bumpmap_index = 4 * Stage + 1;
2612 break;
2613 case D3DTSS_BUMPENVMAT10:
2614 bumpmap_index = 4 * Stage + 2;
2615 break;
2616 case D3DTSS_BUMPENVMAT11:
2617 bumpmap_index = 4 * Stage + 3;
2618 break;
2619 case D3DTSS_BUMPENVLSCALE:
2620 bumpmap_index = 4 * 8 + 2 * Stage;
2621 break;
2622 case D3DTSS_BUMPENVLOFFSET:
2623 bumpmap_index = 4 * 8 + 2 * Stage + 1;
2624 break;
2625 case D3DTSS_TEXTURETRANSFORMFLAGS:
2626 state->changed.group |= NINE_STATE_PS1X_SHADER;
2627 break;
2628 default:
2629 break;
2630 }
2631
2632 if (bumpmap_index >= 0 && !This->is_recording) {
2633 context->bumpmap_vars[bumpmap_index] = Value;
2634 state->changed.group |= NINE_STATE_PS_CONST;
2635 }
2636
2637 state->changed.group |= NINE_STATE_FF_PSSTAGES;
2638 state->ff.changed.tex_stage[Stage][Type / 32] |= 1 << (Type % 32);
2639
2640 return D3D_OK;
2641 }
2642
2643 HRESULT NINE_WINAPI
2644 NineDevice9_GetSamplerState( struct NineDevice9 *This,
2645 DWORD Sampler,
2646 D3DSAMPLERSTATETYPE Type,
2647 DWORD *pValue )
2648 {
2649 user_assert(Sampler < This->caps.MaxSimultaneousTextures ||
2650 Sampler == D3DDMAPSAMPLER ||
2651 (Sampler >= D3DVERTEXTEXTURESAMPLER0 &&
2652 Sampler <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2653
2654 if (Sampler >= D3DDMAPSAMPLER)
2655 Sampler = Sampler - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2656
2657 *pValue = This->state.samp_advertised[Sampler][Type];
2658 return D3D_OK;
2659 }
2660
2661 HRESULT NINE_WINAPI
2662 NineDevice9_SetSamplerState( struct NineDevice9 *This,
2663 DWORD Sampler,
2664 D3DSAMPLERSTATETYPE Type,
2665 DWORD Value )
2666 {
2667 struct nine_state *state = This->update;
2668
2669 DBG("This=%p Sampler=%u Type=%s Value=%08x\n", This,
2670 Sampler, nine_D3DSAMP_to_str(Type), Value);
2671
2672 user_assert(Sampler < This->caps.MaxSimultaneousTextures ||
2673 Sampler == D3DDMAPSAMPLER ||
2674 (Sampler >= D3DVERTEXTEXTURESAMPLER0 &&
2675 Sampler <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2676
2677 if (Sampler >= D3DDMAPSAMPLER)
2678 Sampler = Sampler - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2679
2680 if (state->samp_advertised[Sampler][Type] != Value || unlikely(This->is_recording)) {
2681 /* Contrary to render states, old value is kept if new value is wrong (except intel + Value == 0) */
2682 if (likely(nine_check_sampler_state_value(Type, Value)))
2683 state->samp[Sampler][Type] = Value;
2684 state->samp_advertised[Sampler][Type] = Value;
2685 state->changed.group |= NINE_STATE_SAMPLER;
2686 state->changed.sampler[Sampler] |= 1 << Type;
2687 }
2688
2689 return D3D_OK;
2690 }
2691
2692 HRESULT NINE_WINAPI
2693 NineDevice9_ValidateDevice( struct NineDevice9 *This,
2694 DWORD *pNumPasses )
2695 {
2696 const struct nine_state *state = &This->state;
2697 unsigned i;
2698 unsigned w = 0, h = 0;
2699
2700 DBG("This=%p pNumPasses=%p\n", This, pNumPasses);
2701
2702 for (i = 0; i < ARRAY_SIZE(state->samp); ++i) {
2703 if (state->samp[i][D3DSAMP_MINFILTER] == D3DTEXF_NONE ||
2704 state->samp[i][D3DSAMP_MAGFILTER] == D3DTEXF_NONE)
2705 return D3DERR_UNSUPPORTEDTEXTUREFILTER;
2706 }
2707
2708 for (i = 0; i < This->caps.NumSimultaneousRTs; ++i) {
2709 if (!state->rt[i])
2710 continue;
2711 if (w == 0) {
2712 w = state->rt[i]->desc.Width;
2713 h = state->rt[i]->desc.Height;
2714 } else
2715 if (state->rt[i]->desc.Width != w || state->rt[i]->desc.Height != h) {
2716 return D3DERR_CONFLICTINGRENDERSTATE;
2717 }
2718 }
2719 if (state->ds &&
2720 (state->rs[D3DRS_ZENABLE] || state->rs[D3DRS_STENCILENABLE])) {
2721 if (w != 0 &&
2722 (state->ds->desc.Width != w || state->ds->desc.Height != h))
2723 return D3DERR_CONFLICTINGRENDERSTATE;
2724 }
2725
2726 if (pNumPasses)
2727 *pNumPasses = 1;
2728
2729 return D3D_OK;
2730 }
2731
2732 HRESULT NINE_WINAPI
2733 NineDevice9_SetPaletteEntries( struct NineDevice9 *This,
2734 UINT PaletteNumber,
2735 const PALETTEENTRY *pEntries )
2736 {
2737 STUB(D3D_OK); /* like wine */
2738 }
2739
2740 HRESULT NINE_WINAPI
2741 NineDevice9_GetPaletteEntries( struct NineDevice9 *This,
2742 UINT PaletteNumber,
2743 PALETTEENTRY *pEntries )
2744 {
2745 STUB(D3DERR_INVALIDCALL);
2746 }
2747
2748 HRESULT NINE_WINAPI
2749 NineDevice9_SetCurrentTexturePalette( struct NineDevice9 *This,
2750 UINT PaletteNumber )
2751 {
2752 STUB(D3D_OK); /* like wine */
2753 }
2754
2755 HRESULT NINE_WINAPI
2756 NineDevice9_GetCurrentTexturePalette( struct NineDevice9 *This,
2757 UINT *PaletteNumber )
2758 {
2759 STUB(D3DERR_INVALIDCALL);
2760 }
2761
2762 HRESULT NINE_WINAPI
2763 NineDevice9_SetScissorRect( struct NineDevice9 *This,
2764 const RECT *pRect )
2765 {
2766 struct nine_state *state = This->update;
2767
2768 DBG("x=(%u..%u) y=(%u..%u)\n",
2769 pRect->left, pRect->top, pRect->right, pRect->bottom);
2770
2771 state->scissor.minx = pRect->left;
2772 state->scissor.miny = pRect->top;
2773 state->scissor.maxx = pRect->right;
2774 state->scissor.maxy = pRect->bottom;
2775
2776 state->changed.group |= NINE_STATE_SCISSOR;
2777
2778 return D3D_OK;
2779 }
2780
2781 HRESULT NINE_WINAPI
2782 NineDevice9_GetScissorRect( struct NineDevice9 *This,
2783 RECT *pRect )
2784 {
2785 pRect->left = This->state.scissor.minx;
2786 pRect->top = This->state.scissor.miny;
2787 pRect->right = This->state.scissor.maxx;
2788 pRect->bottom = This->state.scissor.maxy;
2789
2790 return D3D_OK;
2791 }
2792
2793 HRESULT NINE_WINAPI
2794 NineDevice9_SetSoftwareVertexProcessing( struct NineDevice9 *This,
2795 BOOL bSoftware )
2796 {
2797 if (This->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING) {
2798 This->swvp = bSoftware;
2799 This->state.changed.group |= NINE_STATE_SWVP;
2800 return D3D_OK;
2801 } else
2802 return D3DERR_INVALIDCALL; /* msdn. TODO: check in practice */
2803 }
2804
2805 BOOL NINE_WINAPI
2806 NineDevice9_GetSoftwareVertexProcessing( struct NineDevice9 *This )
2807 {
2808 return This->swvp;
2809 }
2810
2811 HRESULT NINE_WINAPI
2812 NineDevice9_SetNPatchMode( struct NineDevice9 *This,
2813 float nSegments )
2814 {
2815 return D3D_OK; /* Nothing to do because we don't advertise NPatch support */
2816 }
2817
2818 float NINE_WINAPI
2819 NineDevice9_GetNPatchMode( struct NineDevice9 *This )
2820 {
2821 STUB(0);
2822 }
2823
2824 static inline void
2825 init_draw_info(struct pipe_draw_info *info,
2826 struct NineDevice9 *dev, D3DPRIMITIVETYPE type, UINT count)
2827 {
2828 info->mode = d3dprimitivetype_to_pipe_prim(type);
2829 info->count = prim_count_to_vertex_count(type, count);
2830 info->start_instance = 0;
2831 info->instance_count = 1;
2832 if (dev->state.stream_instancedata_mask & dev->state.stream_usage_mask)
2833 info->instance_count = MAX2(dev->state.stream_freq[0] & 0x7FFFFF, 1);
2834 info->primitive_restart = FALSE;
2835 info->restart_index = 0;
2836 info->count_from_stream_output = NULL;
2837 info->indirect = NULL;
2838 info->indirect_params = NULL;
2839 }
2840
2841 HRESULT NINE_WINAPI
2842 NineDevice9_DrawPrimitive( struct NineDevice9 *This,
2843 D3DPRIMITIVETYPE PrimitiveType,
2844 UINT StartVertex,
2845 UINT PrimitiveCount )
2846 {
2847 struct pipe_draw_info info;
2848
2849 DBG("iface %p, PrimitiveType %u, StartVertex %u, PrimitiveCount %u\n",
2850 This, PrimitiveType, StartVertex, PrimitiveCount);
2851
2852 nine_update_state(This);
2853
2854 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
2855 info.indexed = FALSE;
2856 info.start = StartVertex;
2857 info.index_bias = 0;
2858 info.min_index = info.start;
2859 info.max_index = info.count - 1;
2860
2861 This->pipe->draw_vbo(This->pipe, &info);
2862
2863 return D3D_OK;
2864 }
2865
2866 HRESULT NINE_WINAPI
2867 NineDevice9_DrawIndexedPrimitive( struct NineDevice9 *This,
2868 D3DPRIMITIVETYPE PrimitiveType,
2869 INT BaseVertexIndex,
2870 UINT MinVertexIndex,
2871 UINT NumVertices,
2872 UINT StartIndex,
2873 UINT PrimitiveCount )
2874 {
2875 struct pipe_draw_info info;
2876
2877 DBG("iface %p, PrimitiveType %u, BaseVertexIndex %u, MinVertexIndex %u "
2878 "NumVertices %u, StartIndex %u, PrimitiveCount %u\n",
2879 This, PrimitiveType, BaseVertexIndex, MinVertexIndex, NumVertices,
2880 StartIndex, PrimitiveCount);
2881
2882 user_assert(This->state.idxbuf, D3DERR_INVALIDCALL);
2883 user_assert(This->state.vdecl, D3DERR_INVALIDCALL);
2884
2885 nine_update_state(This);
2886
2887 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
2888 info.indexed = TRUE;
2889 info.start = StartIndex;
2890 info.index_bias = BaseVertexIndex;
2891 /* These don't include index bias: */
2892 info.min_index = MinVertexIndex;
2893 info.max_index = MinVertexIndex + NumVertices - 1;
2894
2895 This->pipe->draw_vbo(This->pipe, &info);
2896
2897 return D3D_OK;
2898 }
2899
2900 HRESULT NINE_WINAPI
2901 NineDevice9_DrawPrimitiveUP( struct NineDevice9 *This,
2902 D3DPRIMITIVETYPE PrimitiveType,
2903 UINT PrimitiveCount,
2904 const void *pVertexStreamZeroData,
2905 UINT VertexStreamZeroStride )
2906 {
2907 struct pipe_vertex_buffer vtxbuf;
2908 struct pipe_draw_info info;
2909
2910 DBG("iface %p, PrimitiveType %u, PrimitiveCount %u, data %p, stride %u\n",
2911 This, PrimitiveType, PrimitiveCount,
2912 pVertexStreamZeroData, VertexStreamZeroStride);
2913
2914 user_assert(pVertexStreamZeroData && VertexStreamZeroStride,
2915 D3DERR_INVALIDCALL);
2916 user_assert(PrimitiveCount, D3D_OK);
2917
2918 nine_update_state(This);
2919
2920 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
2921 info.indexed = FALSE;
2922 info.start = 0;
2923 info.index_bias = 0;
2924 info.min_index = 0;
2925 info.max_index = info.count - 1;
2926
2927 vtxbuf.stride = VertexStreamZeroStride;
2928 vtxbuf.buffer_offset = 0;
2929 vtxbuf.buffer = NULL;
2930 vtxbuf.user_buffer = pVertexStreamZeroData;
2931
2932 if (!This->driver_caps.user_vbufs) {
2933 u_upload_data(This->vertex_uploader,
2934 0,
2935 (info.max_index + 1) * VertexStreamZeroStride, /* XXX */
2936 4,
2937 vtxbuf.user_buffer,
2938 &vtxbuf.buffer_offset,
2939 &vtxbuf.buffer);
2940 u_upload_unmap(This->vertex_uploader);
2941 vtxbuf.user_buffer = NULL;
2942 }
2943
2944 This->pipe->set_vertex_buffers(This->pipe, 0, 1, &vtxbuf);
2945
2946 This->pipe->draw_vbo(This->pipe, &info);
2947
2948 NineDevice9_PauseRecording(This);
2949 NineDevice9_SetStreamSource(This, 0, NULL, 0, 0);
2950 NineDevice9_ResumeRecording(This);
2951
2952 pipe_resource_reference(&vtxbuf.buffer, NULL);
2953
2954 return D3D_OK;
2955 }
2956
2957 HRESULT NINE_WINAPI
2958 NineDevice9_DrawIndexedPrimitiveUP( struct NineDevice9 *This,
2959 D3DPRIMITIVETYPE PrimitiveType,
2960 UINT MinVertexIndex,
2961 UINT NumVertices,
2962 UINT PrimitiveCount,
2963 const void *pIndexData,
2964 D3DFORMAT IndexDataFormat,
2965 const void *pVertexStreamZeroData,
2966 UINT VertexStreamZeroStride )
2967 {
2968 struct pipe_draw_info info;
2969 struct pipe_vertex_buffer vbuf;
2970 struct pipe_index_buffer ibuf;
2971
2972 DBG("iface %p, PrimitiveType %u, MinVertexIndex %u, NumVertices %u "
2973 "PrimitiveCount %u, pIndexData %p, IndexDataFormat %u "
2974 "pVertexStreamZeroData %p, VertexStreamZeroStride %u\n",
2975 This, PrimitiveType, MinVertexIndex, NumVertices, PrimitiveCount,
2976 pIndexData, IndexDataFormat,
2977 pVertexStreamZeroData, VertexStreamZeroStride);
2978
2979 user_assert(pIndexData && pVertexStreamZeroData, D3DERR_INVALIDCALL);
2980 user_assert(VertexStreamZeroStride, D3DERR_INVALIDCALL);
2981 user_assert(IndexDataFormat == D3DFMT_INDEX16 ||
2982 IndexDataFormat == D3DFMT_INDEX32, D3DERR_INVALIDCALL);
2983 user_assert(PrimitiveCount, D3D_OK);
2984
2985 nine_update_state(This);
2986
2987 init_draw_info(&info, This, PrimitiveType, PrimitiveCount);
2988 info.indexed = TRUE;
2989 info.start = 0;
2990 info.index_bias = 0;
2991 info.min_index = MinVertexIndex;
2992 info.max_index = MinVertexIndex + NumVertices - 1;
2993
2994 vbuf.stride = VertexStreamZeroStride;
2995 vbuf.buffer_offset = 0;
2996 vbuf.buffer = NULL;
2997 vbuf.user_buffer = pVertexStreamZeroData;
2998
2999 ibuf.index_size = (IndexDataFormat == D3DFMT_INDEX16) ? 2 : 4;
3000 ibuf.offset = 0;
3001 ibuf.buffer = NULL;
3002 ibuf.user_buffer = pIndexData;
3003
3004 if (!This->driver_caps.user_vbufs) {
3005 const unsigned base = info.min_index * VertexStreamZeroStride;
3006 u_upload_data(This->vertex_uploader,
3007 base,
3008 (info.max_index -
3009 info.min_index + 1) * VertexStreamZeroStride, /* XXX */
3010 4,
3011 (const uint8_t *)vbuf.user_buffer + base,
3012 &vbuf.buffer_offset,
3013 &vbuf.buffer);
3014 u_upload_unmap(This->vertex_uploader);
3015 /* Won't be used: */
3016 vbuf.buffer_offset -= base;
3017 vbuf.user_buffer = NULL;
3018 }
3019 if (!This->driver_caps.user_ibufs) {
3020 u_upload_data(This->index_uploader,
3021 0,
3022 info.count * ibuf.index_size,
3023 4,
3024 ibuf.user_buffer,
3025 &ibuf.offset,
3026 &ibuf.buffer);
3027 u_upload_unmap(This->index_uploader);
3028 ibuf.user_buffer = NULL;
3029 }
3030
3031 This->pipe->set_vertex_buffers(This->pipe, 0, 1, &vbuf);
3032 This->pipe->set_index_buffer(This->pipe, &ibuf);
3033
3034 This->pipe->draw_vbo(This->pipe, &info);
3035
3036 pipe_resource_reference(&vbuf.buffer, NULL);
3037 pipe_resource_reference(&ibuf.buffer, NULL);
3038
3039 NineDevice9_PauseRecording(This);
3040 NineDevice9_SetIndices(This, NULL);
3041 NineDevice9_SetStreamSource(This, 0, NULL, 0, 0);
3042 NineDevice9_ResumeRecording(This);
3043
3044 return D3D_OK;
3045 }
3046
3047 HRESULT NINE_WINAPI
3048 NineDevice9_ProcessVertices( struct NineDevice9 *This,
3049 UINT SrcStartIndex,
3050 UINT DestIndex,
3051 UINT VertexCount,
3052 IDirect3DVertexBuffer9 *pDestBuffer,
3053 IDirect3DVertexDeclaration9 *pVertexDecl,
3054 DWORD Flags )
3055 {
3056 struct pipe_screen *screen_sw = This->screen_sw;
3057 struct pipe_context *pipe_sw = This->pipe_sw;
3058 struct NineVertexDeclaration9 *vdecl = NineVertexDeclaration9(pVertexDecl);
3059 struct NineVertexBuffer9 *dst = NineVertexBuffer9(pDestBuffer);
3060 struct NineVertexShader9 *vs;
3061 struct pipe_resource *resource;
3062 struct pipe_transfer *transfer = NULL;
3063 struct pipe_stream_output_info so;
3064 struct pipe_stream_output_target *target;
3065 struct pipe_draw_info draw;
3066 struct pipe_box box;
3067 unsigned offsets[1] = {0};
3068 HRESULT hr;
3069 unsigned buffer_size;
3070 void *map;
3071
3072 DBG("This=%p SrcStartIndex=%u DestIndex=%u VertexCount=%u "
3073 "pDestBuffer=%p pVertexDecl=%p Flags=%d\n",
3074 This, SrcStartIndex, DestIndex, VertexCount, pDestBuffer,
3075 pVertexDecl, Flags);
3076
3077 if (!screen_sw->get_param(screen_sw, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS)) {
3078 DBG("ProcessVertices not supported\n");
3079 return D3DERR_INVALIDCALL;
3080 }
3081
3082
3083 vs = This->state.programmable_vs ? This->state.vs : This->ff.vs;
3084 /* Note: version is 0 for ff */
3085 user_assert(vdecl || (vs->byte_code.version < 0x30 && dst->desc.FVF),
3086 D3DERR_INVALIDCALL);
3087 if (!vdecl) {
3088 DWORD FVF = dst->desc.FVF;
3089 vdecl = util_hash_table_get(This->ff.ht_fvf, &FVF);
3090 if (!vdecl) {
3091 hr = NineVertexDeclaration9_new_from_fvf(This, FVF, &vdecl);
3092 if (FAILED(hr))
3093 return hr;
3094 vdecl->fvf = FVF;
3095 util_hash_table_set(This->ff.ht_fvf, &vdecl->fvf, vdecl);
3096 NineUnknown_ConvertRefToBind(NineUnknown(vdecl));
3097 }
3098 }
3099
3100 /* Flags: Can be 0 or D3DPV_DONOTCOPYDATA, and/or lock flags
3101 * D3DPV_DONOTCOPYDATA -> Has effect only for ff. In particular
3102 * if not set, everything from src will be used, and dst
3103 * must match exactly the ff vs outputs.
3104 * TODO: Handle all the checks, etc for ff */
3105 user_assert(vdecl->position_t || This->state.programmable_vs,
3106 D3DERR_INVALIDCALL);
3107
3108 /* TODO: Support vs < 3 and ff */
3109 user_assert(vs->byte_code.version == 0x30,
3110 D3DERR_INVALIDCALL);
3111 /* TODO: Not hardcode the constant buffers for swvp */
3112 user_assert(This->may_swvp,
3113 D3DERR_INVALIDCALL);
3114
3115 nine_state_prepare_draw_sw(This, vdecl, SrcStartIndex, VertexCount, &so);
3116
3117 buffer_size = VertexCount * so.stride[0] * 4;
3118 {
3119 struct pipe_resource templ;
3120
3121 memset(&templ, 0, sizeof(templ));
3122 templ.target = PIPE_BUFFER;
3123 templ.format = PIPE_FORMAT_R8_UNORM;
3124 templ.width0 = buffer_size;
3125 templ.flags = 0;
3126 templ.bind = PIPE_BIND_STREAM_OUTPUT;
3127 templ.usage = PIPE_USAGE_STREAM;
3128 templ.height0 = templ.depth0 = templ.array_size = 1;
3129 templ.last_level = templ.nr_samples = 0;
3130
3131 resource = screen_sw->resource_create(screen_sw, &templ);
3132 if (!resource)
3133 return E_OUTOFMEMORY;
3134 }
3135 target = pipe_sw->create_stream_output_target(pipe_sw, resource,
3136 0, buffer_size);
3137 if (!target) {
3138 pipe_resource_reference(&resource, NULL);
3139 return D3DERR_DRIVERINTERNALERROR;
3140 }
3141
3142 init_draw_info(&draw, This, D3DPT_POINTLIST, VertexCount);
3143 draw.instance_count = 1;
3144 draw.indexed = FALSE;
3145 draw.start = 0;
3146 draw.index_bias = 0;
3147 draw.min_index = 0;
3148 draw.max_index = VertexCount - 1;
3149
3150
3151 pipe_sw->set_stream_output_targets(pipe_sw, 1, &target, offsets);
3152
3153 pipe_sw->draw_vbo(pipe_sw, &draw);
3154
3155 pipe_sw->set_stream_output_targets(pipe_sw, 0, NULL, 0);
3156 pipe_sw->stream_output_target_destroy(pipe_sw, target);
3157
3158 u_box_1d(0, VertexCount * so.stride[0] * 4, &box);
3159 map = pipe_sw->transfer_map(pipe_sw, resource, 0, PIPE_TRANSFER_READ, &box,
3160 &transfer);
3161 if (!map) {
3162 hr = D3DERR_DRIVERINTERNALERROR;
3163 goto out;
3164 }
3165
3166 hr = NineVertexDeclaration9_ConvertStreamOutput(vdecl,
3167 dst, DestIndex, VertexCount,
3168 map, &so);
3169 if (transfer)
3170 pipe_sw->transfer_unmap(pipe_sw, transfer);
3171
3172 out:
3173 nine_state_after_draw_sw(This);
3174 pipe_resource_reference(&resource, NULL);
3175 return hr;
3176 }
3177
3178 HRESULT NINE_WINAPI
3179 NineDevice9_CreateVertexDeclaration( struct NineDevice9 *This,
3180 const D3DVERTEXELEMENT9 *pVertexElements,
3181 IDirect3DVertexDeclaration9 **ppDecl )
3182 {
3183 struct NineVertexDeclaration9 *vdecl;
3184
3185 DBG("This=%p pVertexElements=%p ppDecl=%p\n",
3186 This, pVertexElements, ppDecl);
3187
3188 HRESULT hr = NineVertexDeclaration9_new(This, pVertexElements, &vdecl);
3189 if (SUCCEEDED(hr))
3190 *ppDecl = (IDirect3DVertexDeclaration9 *)vdecl;
3191
3192 return hr;
3193 }
3194
3195 HRESULT NINE_WINAPI
3196 NineDevice9_SetVertexDeclaration( struct NineDevice9 *This,
3197 IDirect3DVertexDeclaration9 *pDecl )
3198 {
3199 struct nine_state *state = This->update;
3200 struct nine_context *context = &This->context;
3201 BOOL was_programmable_vs = This->state.programmable_vs;
3202
3203 DBG("This=%p pDecl=%p\n", This, pDecl);
3204
3205 if (likely(!This->is_recording) && state->vdecl == NineVertexDeclaration9(pDecl))
3206 return D3D_OK;
3207
3208 nine_bind(&state->vdecl, pDecl);
3209
3210 This->state.programmable_vs = This->state.vs && !(This->state.vdecl && This->state.vdecl->position_t);
3211 if (likely(!This->is_recording) && was_programmable_vs != This->state.programmable_vs) {
3212 context->commit |= NINE_STATE_COMMIT_CONST_VS;
3213 state->changed.group |= NINE_STATE_VS;
3214 }
3215
3216 state->changed.group |= NINE_STATE_VDECL;
3217
3218 return D3D_OK;
3219 }
3220
3221 HRESULT NINE_WINAPI
3222 NineDevice9_GetVertexDeclaration( struct NineDevice9 *This,
3223 IDirect3DVertexDeclaration9 **ppDecl )
3224 {
3225 user_assert(ppDecl, D3DERR_INVALIDCALL);
3226
3227 *ppDecl = (IDirect3DVertexDeclaration9 *)This->state.vdecl;
3228 if (*ppDecl)
3229 NineUnknown_AddRef(NineUnknown(*ppDecl));
3230 return D3D_OK;
3231 }
3232
3233 HRESULT NINE_WINAPI
3234 NineDevice9_SetFVF( struct NineDevice9 *This,
3235 DWORD FVF )
3236 {
3237 struct NineVertexDeclaration9 *vdecl;
3238 HRESULT hr;
3239
3240 DBG("FVF = %08x\n", FVF);
3241 if (!FVF)
3242 return D3D_OK; /* like wine */
3243
3244 vdecl = util_hash_table_get(This->ff.ht_fvf, &FVF);
3245 if (!vdecl) {
3246 hr = NineVertexDeclaration9_new_from_fvf(This, FVF, &vdecl);
3247 if (FAILED(hr))
3248 return hr;
3249 vdecl->fvf = FVF;
3250 util_hash_table_set(This->ff.ht_fvf, &vdecl->fvf, vdecl);
3251 NineUnknown_ConvertRefToBind(NineUnknown(vdecl));
3252 }
3253 return NineDevice9_SetVertexDeclaration(
3254 This, (IDirect3DVertexDeclaration9 *)vdecl);
3255 }
3256
3257 HRESULT NINE_WINAPI
3258 NineDevice9_GetFVF( struct NineDevice9 *This,
3259 DWORD *pFVF )
3260 {
3261 *pFVF = This->state.vdecl ? This->state.vdecl->fvf : 0;
3262 return D3D_OK;
3263 }
3264
3265 HRESULT NINE_WINAPI
3266 NineDevice9_CreateVertexShader( struct NineDevice9 *This,
3267 const DWORD *pFunction,
3268 IDirect3DVertexShader9 **ppShader )
3269 {
3270 struct NineVertexShader9 *vs;
3271 HRESULT hr;
3272
3273 DBG("This=%p pFunction=%p ppShader=%p\n", This, pFunction, ppShader);
3274
3275 hr = NineVertexShader9_new(This, &vs, pFunction, NULL);
3276 if (FAILED(hr))
3277 return hr;
3278 *ppShader = (IDirect3DVertexShader9 *)vs;
3279 return D3D_OK;
3280 }
3281
3282 HRESULT NINE_WINAPI
3283 NineDevice9_SetVertexShader( struct NineDevice9 *This,
3284 IDirect3DVertexShader9 *pShader )
3285 {
3286 struct nine_state *state = This->update;
3287 struct nine_context *context = &This->context;
3288 BOOL was_programmable_vs = This->state.programmable_vs;
3289
3290 DBG("This=%p pShader=%p\n", This, pShader);
3291
3292 if (!This->is_recording && state->vs == (struct NineVertexShader9*)pShader)
3293 return D3D_OK;
3294
3295 nine_bind(&state->vs, pShader);
3296
3297 This->state.programmable_vs = This->state.vs && !(This->state.vdecl && This->state.vdecl->position_t);
3298
3299 /* ff -> non-ff: commit back non-ff constants */
3300 if (!was_programmable_vs && This->state.programmable_vs)
3301 context->commit |= NINE_STATE_COMMIT_CONST_VS;
3302
3303 state->changed.group |= NINE_STATE_VS;
3304
3305 return D3D_OK;
3306 }
3307
3308 HRESULT NINE_WINAPI
3309 NineDevice9_GetVertexShader( struct NineDevice9 *This,
3310 IDirect3DVertexShader9 **ppShader )
3311 {
3312 user_assert(ppShader, D3DERR_INVALIDCALL);
3313 nine_reference_set(ppShader, This->state.vs);
3314 return D3D_OK;
3315 }
3316
3317 HRESULT NINE_WINAPI
3318 NineDevice9_SetVertexShaderConstantF( struct NineDevice9 *This,
3319 UINT StartRegister,
3320 const float *pConstantData,
3321 UINT Vector4fCount )
3322 {
3323 struct nine_state *state = This->update;
3324 float *vs_const_f = This->may_swvp ? state->vs_const_f_swvp : state->vs_const_f;
3325
3326 DBG("This=%p StartRegister=%u pConstantData=%p Vector4fCount=%u\n",
3327 This, StartRegister, pConstantData, Vector4fCount);
3328
3329 user_assert(StartRegister < This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3330 user_assert(StartRegister + Vector4fCount <= This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3331
3332 if (!Vector4fCount)
3333 return D3D_OK;
3334 user_assert(pConstantData, D3DERR_INVALIDCALL);
3335
3336 if (!This->is_recording) {
3337 if (!memcmp(&vs_const_f[StartRegister * 4], pConstantData,
3338 Vector4fCount * 4 * sizeof(state->vs_const_f[0])))
3339 return D3D_OK;
3340 }
3341
3342 memcpy(&vs_const_f[StartRegister * 4],
3343 pConstantData,
3344 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3345
3346 nine_ranges_insert(&state->changed.vs_const_f,
3347 StartRegister, StartRegister + Vector4fCount,
3348 &This->range_pool);
3349
3350 if (This->may_swvp) {
3351 Vector4fCount = MIN2(StartRegister + Vector4fCount, NINE_MAX_CONST_F) - StartRegister;
3352 if (StartRegister < NINE_MAX_CONST_F)
3353 memcpy(&state->vs_const_f[StartRegister * 4],
3354 pConstantData,
3355 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3356 }
3357
3358 state->changed.group |= NINE_STATE_VS_CONST;
3359
3360 return D3D_OK;
3361 }
3362
3363 HRESULT NINE_WINAPI
3364 NineDevice9_GetVertexShaderConstantF( struct NineDevice9 *This,
3365 UINT StartRegister,
3366 float *pConstantData,
3367 UINT Vector4fCount )
3368 {
3369 const struct nine_state *state = &This->state;
3370 float *vs_const_f = This->may_swvp ? state->vs_const_f_swvp : state->vs_const_f;
3371
3372 user_assert(StartRegister < This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3373 user_assert(StartRegister + Vector4fCount <= This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3374 user_assert(pConstantData, D3DERR_INVALIDCALL);
3375
3376 memcpy(pConstantData,
3377 &vs_const_f[StartRegister * 4],
3378 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3379
3380 return D3D_OK;
3381 }
3382
3383 HRESULT NINE_WINAPI
3384 NineDevice9_SetVertexShaderConstantI( struct NineDevice9 *This,
3385 UINT StartRegister,
3386 const int *pConstantData,
3387 UINT Vector4iCount )
3388 {
3389 struct nine_state *state = This->update;
3390 int i;
3391
3392 DBG("This=%p StartRegister=%u pConstantData=%p Vector4iCount=%u\n",
3393 This, StartRegister, pConstantData, Vector4iCount);
3394
3395 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3396 D3DERR_INVALIDCALL);
3397 user_assert(StartRegister + Vector4iCount <= (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3398 D3DERR_INVALIDCALL);
3399 user_assert(pConstantData, D3DERR_INVALIDCALL);
3400
3401 if (This->driver_caps.vs_integer) {
3402 if (!This->is_recording) {
3403 if (!memcmp(&state->vs_const_i[4 * StartRegister], pConstantData,
3404 Vector4iCount * sizeof(int[4])))
3405 return D3D_OK;
3406 }
3407 memcpy(&state->vs_const_i[4 * StartRegister],
3408 pConstantData,
3409 Vector4iCount * sizeof(int[4]));
3410 } else {
3411 for (i = 0; i < Vector4iCount; i++) {
3412 state->vs_const_i[4 * (StartRegister + i)] = fui((float)(pConstantData[4 * i]));
3413 state->vs_const_i[4 * (StartRegister + i) + 1] = fui((float)(pConstantData[4 * i + 1]));
3414 state->vs_const_i[4 * (StartRegister + i) + 2] = fui((float)(pConstantData[4 * i + 2]));
3415 state->vs_const_i[4 * (StartRegister + i) + 3] = fui((float)(pConstantData[4 * i + 3]));
3416 }
3417 }
3418
3419 nine_ranges_insert(&state->changed.vs_const_i,
3420 StartRegister, StartRegister + Vector4iCount,
3421 &This->range_pool);
3422 state->changed.group |= NINE_STATE_VS_CONST;
3423
3424 return D3D_OK;
3425 }
3426
3427 HRESULT NINE_WINAPI
3428 NineDevice9_GetVertexShaderConstantI( struct NineDevice9 *This,
3429 UINT StartRegister,
3430 int *pConstantData,
3431 UINT Vector4iCount )
3432 {
3433 const struct nine_state *state = &This->state;
3434 int i;
3435
3436 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3437 D3DERR_INVALIDCALL);
3438 user_assert(StartRegister + Vector4iCount <= (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3439 D3DERR_INVALIDCALL);
3440 user_assert(pConstantData, D3DERR_INVALIDCALL);
3441
3442 if (This->driver_caps.vs_integer) {
3443 memcpy(pConstantData,
3444 &state->vs_const_i[4 * StartRegister],
3445 Vector4iCount * sizeof(int[4]));
3446 } else {
3447 for (i = 0; i < Vector4iCount; i++) {
3448 pConstantData[4 * i] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i)]);
3449 pConstantData[4 * i + 1] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 1]);
3450 pConstantData[4 * i + 2] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 2]);
3451 pConstantData[4 * i + 3] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 3]);
3452 }
3453 }
3454
3455 return D3D_OK;
3456 }
3457
3458 HRESULT NINE_WINAPI
3459 NineDevice9_SetVertexShaderConstantB( struct NineDevice9 *This,
3460 UINT StartRegister,
3461 const BOOL *pConstantData,
3462 UINT BoolCount )
3463 {
3464 struct nine_state *state = This->update;
3465 int i;
3466 uint32_t bool_true = This->driver_caps.vs_integer ? 0xFFFFFFFF : fui(1.0f);
3467
3468 DBG("This=%p StartRegister=%u pConstantData=%p BoolCount=%u\n",
3469 This, StartRegister, pConstantData, BoolCount);
3470
3471 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3472 D3DERR_INVALIDCALL);
3473 user_assert(StartRegister + BoolCount <= (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3474 D3DERR_INVALIDCALL);
3475 user_assert(pConstantData, D3DERR_INVALIDCALL);
3476
3477 if (!This->is_recording) {
3478 bool noChange = true;
3479 for (i = 0; i < BoolCount; i++) {
3480 if (!!state->vs_const_b[StartRegister + i] != !!pConstantData[i])
3481 noChange = false;
3482 }
3483 if (noChange)
3484 return D3D_OK;
3485 }
3486
3487 for (i = 0; i < BoolCount; i++)
3488 state->vs_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
3489
3490 nine_ranges_insert(&state->changed.vs_const_b,
3491 StartRegister, StartRegister + BoolCount,
3492 &This->range_pool);
3493 state->changed.group |= NINE_STATE_VS_CONST;
3494
3495 return D3D_OK;
3496 }
3497
3498 HRESULT NINE_WINAPI
3499 NineDevice9_GetVertexShaderConstantB( struct NineDevice9 *This,
3500 UINT StartRegister,
3501 BOOL *pConstantData,
3502 UINT BoolCount )
3503 {
3504 const struct nine_state *state = &This->state;
3505 int i;
3506
3507 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3508 D3DERR_INVALIDCALL);
3509 user_assert(StartRegister + BoolCount <= (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3510 D3DERR_INVALIDCALL);
3511 user_assert(pConstantData, D3DERR_INVALIDCALL);
3512
3513 for (i = 0; i < BoolCount; i++)
3514 pConstantData[i] = state->vs_const_b[StartRegister + i] != 0 ? TRUE : FALSE;
3515
3516 return D3D_OK;
3517 }
3518
3519 HRESULT NINE_WINAPI
3520 NineDevice9_SetStreamSource( struct NineDevice9 *This,
3521 UINT StreamNumber,
3522 IDirect3DVertexBuffer9 *pStreamData,
3523 UINT OffsetInBytes,
3524 UINT Stride )
3525 {
3526 struct nine_state *state = This->update;
3527 struct NineVertexBuffer9 *pVBuf9 = NineVertexBuffer9(pStreamData);
3528 const unsigned i = StreamNumber;
3529
3530 DBG("This=%p StreamNumber=%u pStreamData=%p OffsetInBytes=%u Stride=%u\n",
3531 This, StreamNumber, pStreamData, OffsetInBytes, Stride);
3532
3533 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3534 user_assert(Stride <= This->caps.MaxStreamStride, D3DERR_INVALIDCALL);
3535
3536 if (likely(!This->is_recording)) {
3537 if (state->stream[i] == NineVertexBuffer9(pStreamData) &&
3538 state->vtxbuf[i].stride == Stride &&
3539 state->vtxbuf[i].buffer_offset == OffsetInBytes)
3540 return D3D_OK;
3541 }
3542 nine_bind(&state->stream[i], pStreamData);
3543
3544 state->changed.vtxbuf |= 1 << StreamNumber;
3545
3546 if (pStreamData) {
3547 state->vtxbuf[i].stride = Stride;
3548 state->vtxbuf[i].buffer_offset = OffsetInBytes;
3549 }
3550 pipe_resource_reference(&state->vtxbuf[i].buffer,
3551 pStreamData ? NineVertexBuffer9_GetResource(pVBuf9) : NULL);
3552
3553 return D3D_OK;
3554 }
3555
3556 HRESULT NINE_WINAPI
3557 NineDevice9_GetStreamSource( struct NineDevice9 *This,
3558 UINT StreamNumber,
3559 IDirect3DVertexBuffer9 **ppStreamData,
3560 UINT *pOffsetInBytes,
3561 UINT *pStride )
3562 {
3563 const struct nine_state *state = &This->state;
3564 const unsigned i = StreamNumber;
3565
3566 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3567 user_assert(ppStreamData, D3DERR_INVALIDCALL);
3568
3569 nine_reference_set(ppStreamData, state->stream[i]);
3570 *pStride = state->vtxbuf[i].stride;
3571 *pOffsetInBytes = state->vtxbuf[i].buffer_offset;
3572
3573 return D3D_OK;
3574 }
3575
3576 HRESULT NINE_WINAPI
3577 NineDevice9_SetStreamSourceFreq( struct NineDevice9 *This,
3578 UINT StreamNumber,
3579 UINT Setting )
3580 {
3581 struct nine_state *state = This->update;
3582 /* const UINT freq = Setting & 0x7FFFFF; */
3583
3584 DBG("This=%p StreamNumber=%u FrequencyParameter=0x%x\n", This,
3585 StreamNumber, Setting);
3586
3587 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3588 user_assert(StreamNumber != 0 || !(Setting & D3DSTREAMSOURCE_INSTANCEDATA),
3589 D3DERR_INVALIDCALL);
3590 user_assert(!((Setting & D3DSTREAMSOURCE_INSTANCEDATA) &&
3591 (Setting & D3DSTREAMSOURCE_INDEXEDDATA)), D3DERR_INVALIDCALL);
3592 user_assert(Setting, D3DERR_INVALIDCALL);
3593
3594 if (likely(!This->is_recording) && state->stream_freq[StreamNumber] == Setting)
3595 return D3D_OK;
3596
3597 state->stream_freq[StreamNumber] = Setting;
3598
3599 if (Setting & D3DSTREAMSOURCE_INSTANCEDATA)
3600 state->stream_instancedata_mask |= 1 << StreamNumber;
3601 else
3602 state->stream_instancedata_mask &= ~(1 << StreamNumber);
3603
3604 state->changed.stream_freq |= 1 << StreamNumber; /* Used for stateblocks */
3605 if (StreamNumber != 0)
3606 state->changed.group |= NINE_STATE_STREAMFREQ;
3607 return D3D_OK;
3608 }
3609
3610 HRESULT NINE_WINAPI
3611 NineDevice9_GetStreamSourceFreq( struct NineDevice9 *This,
3612 UINT StreamNumber,
3613 UINT *pSetting )
3614 {
3615 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3616 *pSetting = This->state.stream_freq[StreamNumber];
3617 return D3D_OK;
3618 }
3619
3620 HRESULT NINE_WINAPI
3621 NineDevice9_SetIndices( struct NineDevice9 *This,
3622 IDirect3DIndexBuffer9 *pIndexData )
3623 {
3624 struct nine_state *state = This->update;
3625
3626 DBG("This=%p pIndexData=%p\n", This, pIndexData);
3627
3628 if (likely(!This->is_recording))
3629 if (state->idxbuf == NineIndexBuffer9(pIndexData))
3630 return D3D_OK;
3631 nine_bind(&state->idxbuf, pIndexData);
3632
3633 state->changed.group |= NINE_STATE_IDXBUF;
3634
3635 return D3D_OK;
3636 }
3637
3638 /* XXX: wine/d3d9 doesn't have pBaseVertexIndex, and it doesn't make sense
3639 * here because it's an argument passed to the Draw calls.
3640 */
3641 HRESULT NINE_WINAPI
3642 NineDevice9_GetIndices( struct NineDevice9 *This,
3643 IDirect3DIndexBuffer9 **ppIndexData)
3644 {
3645 user_assert(ppIndexData, D3DERR_INVALIDCALL);
3646 nine_reference_set(ppIndexData, This->state.idxbuf);
3647 return D3D_OK;
3648 }
3649
3650 HRESULT NINE_WINAPI
3651 NineDevice9_CreatePixelShader( struct NineDevice9 *This,
3652 const DWORD *pFunction,
3653 IDirect3DPixelShader9 **ppShader )
3654 {
3655 struct NinePixelShader9 *ps;
3656 HRESULT hr;
3657
3658 DBG("This=%p pFunction=%p ppShader=%p\n", This, pFunction, ppShader);
3659
3660 hr = NinePixelShader9_new(This, &ps, pFunction, NULL);
3661 if (FAILED(hr))
3662 return hr;
3663 *ppShader = (IDirect3DPixelShader9 *)ps;
3664 return D3D_OK;
3665 }
3666
3667 HRESULT NINE_WINAPI
3668 NineDevice9_SetPixelShader( struct NineDevice9 *This,
3669 IDirect3DPixelShader9 *pShader )
3670 {
3671 struct nine_state *state = This->update;
3672 struct nine_context *context = &This->context;
3673 unsigned old_mask = state->ps ? state->ps->rt_mask : 1;
3674 unsigned mask;
3675
3676 DBG("This=%p pShader=%p\n", This, pShader);
3677
3678 if (!This->is_recording && state->ps == (struct NinePixelShader9*)pShader)
3679 return D3D_OK;
3680
3681 /* ff -> non-ff: commit back non-ff constants */
3682 if (!state->ps && pShader)
3683 context->commit |= NINE_STATE_COMMIT_CONST_PS;
3684
3685 nine_bind(&state->ps, pShader);
3686
3687 state->changed.group |= NINE_STATE_PS;
3688
3689 mask = state->ps ? state->ps->rt_mask : 1;
3690 /* We need to update cbufs if the pixel shader would
3691 * write to different render targets */
3692 if (mask != old_mask)
3693 state->changed.group |= NINE_STATE_FB;
3694
3695 return D3D_OK;
3696 }
3697
3698 HRESULT NINE_WINAPI
3699 NineDevice9_GetPixelShader( struct NineDevice9 *This,
3700 IDirect3DPixelShader9 **ppShader )
3701 {
3702 user_assert(ppShader, D3DERR_INVALIDCALL);
3703 nine_reference_set(ppShader, This->state.ps);
3704 return D3D_OK;
3705 }
3706
3707 HRESULT NINE_WINAPI
3708 NineDevice9_SetPixelShaderConstantF( struct NineDevice9 *This,
3709 UINT StartRegister,
3710 const float *pConstantData,
3711 UINT Vector4fCount )
3712 {
3713 struct nine_state *state = This->update;
3714
3715 DBG("This=%p StartRegister=%u pConstantData=%p Vector4fCount=%u\n",
3716 This, StartRegister, pConstantData, Vector4fCount);
3717
3718 user_assert(StartRegister < NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3719 user_assert(StartRegister + Vector4fCount <= NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3720
3721 if (!Vector4fCount)
3722 return D3D_OK;
3723 user_assert(pConstantData, D3DERR_INVALIDCALL);
3724
3725 if (!This->is_recording) {
3726 if (!memcmp(&state->ps_const_f[StartRegister * 4], pConstantData,
3727 Vector4fCount * 4 * sizeof(state->ps_const_f[0])))
3728 return D3D_OK;
3729 }
3730
3731 memcpy(&state->ps_const_f[StartRegister * 4],
3732 pConstantData,
3733 Vector4fCount * 4 * sizeof(state->ps_const_f[0]));
3734
3735 nine_ranges_insert(&state->changed.ps_const_f,
3736 StartRegister, StartRegister + Vector4fCount,
3737 &This->range_pool);
3738
3739 state->changed.group |= NINE_STATE_PS_CONST;
3740
3741 return D3D_OK;
3742 }
3743
3744 HRESULT NINE_WINAPI
3745 NineDevice9_GetPixelShaderConstantF( struct NineDevice9 *This,
3746 UINT StartRegister,
3747 float *pConstantData,
3748 UINT Vector4fCount )
3749 {
3750 const struct nine_state *state = &This->state;
3751
3752 user_assert(StartRegister < NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3753 user_assert(StartRegister + Vector4fCount <= NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3754 user_assert(pConstantData, D3DERR_INVALIDCALL);
3755
3756 memcpy(pConstantData,
3757 &state->ps_const_f[StartRegister * 4],
3758 Vector4fCount * 4 * sizeof(state->ps_const_f[0]));
3759
3760 return D3D_OK;
3761 }
3762
3763 HRESULT NINE_WINAPI
3764 NineDevice9_SetPixelShaderConstantI( struct NineDevice9 *This,
3765 UINT StartRegister,
3766 const int *pConstantData,
3767 UINT Vector4iCount )
3768 {
3769 struct nine_state *state = This->update;
3770 int i;
3771
3772 DBG("This=%p StartRegister=%u pConstantData=%p Vector4iCount=%u\n",
3773 This, StartRegister, pConstantData, Vector4iCount);
3774
3775 user_assert(StartRegister < NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3776 user_assert(StartRegister + Vector4iCount <= NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3777 user_assert(pConstantData, D3DERR_INVALIDCALL);
3778
3779 if (This->driver_caps.ps_integer) {
3780 if (!This->is_recording) {
3781 if (!memcmp(&state->ps_const_i[StartRegister][0], pConstantData,
3782 Vector4iCount * sizeof(state->ps_const_i[0])))
3783 return D3D_OK;
3784 }
3785 memcpy(&state->ps_const_i[StartRegister][0],
3786 pConstantData,
3787 Vector4iCount * sizeof(state->ps_const_i[0]));
3788 } else {
3789 for (i = 0; i < Vector4iCount; i++) {
3790 state->ps_const_i[StartRegister+i][0] = fui((float)(pConstantData[4*i]));
3791 state->ps_const_i[StartRegister+i][1] = fui((float)(pConstantData[4*i+1]));
3792 state->ps_const_i[StartRegister+i][2] = fui((float)(pConstantData[4*i+2]));
3793 state->ps_const_i[StartRegister+i][3] = fui((float)(pConstantData[4*i+3]));
3794 }
3795 }
3796 state->changed.ps_const_i |= ((1 << Vector4iCount) - 1) << StartRegister;
3797 state->changed.group |= NINE_STATE_PS_CONST;
3798
3799 return D3D_OK;
3800 }
3801
3802 HRESULT NINE_WINAPI
3803 NineDevice9_GetPixelShaderConstantI( struct NineDevice9 *This,
3804 UINT StartRegister,
3805 int *pConstantData,
3806 UINT Vector4iCount )
3807 {
3808 const struct nine_state *state = &This->state;
3809 int i;
3810
3811 user_assert(StartRegister < NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3812 user_assert(StartRegister + Vector4iCount <= NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3813 user_assert(pConstantData, D3DERR_INVALIDCALL);
3814
3815 if (This->driver_caps.ps_integer) {
3816 memcpy(pConstantData,
3817 &state->ps_const_i[StartRegister][0],
3818 Vector4iCount * sizeof(state->ps_const_i[0]));
3819 } else {
3820 for (i = 0; i < Vector4iCount; i++) {
3821 pConstantData[4*i] = (int32_t) uif(state->ps_const_i[StartRegister+i][0]);
3822 pConstantData[4*i+1] = (int32_t) uif(state->ps_const_i[StartRegister+i][1]);
3823 pConstantData[4*i+2] = (int32_t) uif(state->ps_const_i[StartRegister+i][2]);
3824 pConstantData[4*i+3] = (int32_t) uif(state->ps_const_i[StartRegister+i][3]);
3825 }
3826 }
3827
3828 return D3D_OK;
3829 }
3830
3831 HRESULT NINE_WINAPI
3832 NineDevice9_SetPixelShaderConstantB( struct NineDevice9 *This,
3833 UINT StartRegister,
3834 const BOOL *pConstantData,
3835 UINT BoolCount )
3836 {
3837 struct nine_state *state = This->update;
3838 int i;
3839 uint32_t bool_true = This->driver_caps.ps_integer ? 0xFFFFFFFF : fui(1.0f);
3840
3841 DBG("This=%p StartRegister=%u pConstantData=%p BoolCount=%u\n",
3842 This, StartRegister, pConstantData, BoolCount);
3843
3844 user_assert(StartRegister < NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3845 user_assert(StartRegister + BoolCount <= NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3846 user_assert(pConstantData, D3DERR_INVALIDCALL);
3847
3848 if (!This->is_recording) {
3849 bool noChange = true;
3850 for (i = 0; i < BoolCount; i++) {
3851 if (!!state->ps_const_b[StartRegister + i] != !!pConstantData[i])
3852 noChange = false;
3853 }
3854 if (noChange)
3855 return D3D_OK;
3856 }
3857
3858 for (i = 0; i < BoolCount; i++)
3859 state->ps_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
3860
3861 state->changed.ps_const_b |= ((1 << BoolCount) - 1) << StartRegister;
3862 state->changed.group |= NINE_STATE_PS_CONST;
3863
3864 return D3D_OK;
3865 }
3866
3867 HRESULT NINE_WINAPI
3868 NineDevice9_GetPixelShaderConstantB( struct NineDevice9 *This,
3869 UINT StartRegister,
3870 BOOL *pConstantData,
3871 UINT BoolCount )
3872 {
3873 const struct nine_state *state = &This->state;
3874 int i;
3875
3876 user_assert(StartRegister < NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3877 user_assert(StartRegister + BoolCount <= NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3878 user_assert(pConstantData, D3DERR_INVALIDCALL);
3879
3880 for (i = 0; i < BoolCount; i++)
3881 pConstantData[i] = state->ps_const_b[StartRegister + i] ? TRUE : FALSE;
3882
3883 return D3D_OK;
3884 }
3885
3886 HRESULT NINE_WINAPI
3887 NineDevice9_DrawRectPatch( struct NineDevice9 *This,
3888 UINT Handle,
3889 const float *pNumSegs,
3890 const D3DRECTPATCH_INFO *pRectPatchInfo )
3891 {
3892 STUB(D3DERR_INVALIDCALL);
3893 }
3894
3895 HRESULT NINE_WINAPI
3896 NineDevice9_DrawTriPatch( struct NineDevice9 *This,
3897 UINT Handle,
3898 const float *pNumSegs,
3899 const D3DTRIPATCH_INFO *pTriPatchInfo )
3900 {
3901 STUB(D3DERR_INVALIDCALL);
3902 }
3903
3904 HRESULT NINE_WINAPI
3905 NineDevice9_DeletePatch( struct NineDevice9 *This,
3906 UINT Handle )
3907 {
3908 STUB(D3DERR_INVALIDCALL);
3909 }
3910
3911 HRESULT NINE_WINAPI
3912 NineDevice9_CreateQuery( struct NineDevice9 *This,
3913 D3DQUERYTYPE Type,
3914 IDirect3DQuery9 **ppQuery )
3915 {
3916 struct NineQuery9 *query;
3917 HRESULT hr;
3918
3919 DBG("This=%p Type=%d ppQuery=%p\n", This, Type, ppQuery);
3920
3921 hr = nine_is_query_supported(This->screen, Type);
3922 if (!ppQuery || hr != D3D_OK)
3923 return hr;
3924
3925 hr = NineQuery9_new(This, &query, Type);
3926 if (FAILED(hr))
3927 return hr;
3928 *ppQuery = (IDirect3DQuery9 *)query;
3929 return D3D_OK;
3930 }
3931
3932 IDirect3DDevice9Vtbl NineDevice9_vtable = {
3933 (void *)NineUnknown_QueryInterface,
3934 (void *)NineUnknown_AddRef,
3935 (void *)NineUnknown_Release,
3936 (void *)NineDevice9_TestCooperativeLevel,
3937 (void *)NineDevice9_GetAvailableTextureMem,
3938 (void *)NineDevice9_EvictManagedResources,
3939 (void *)NineDevice9_GetDirect3D,
3940 (void *)NineDevice9_GetDeviceCaps,
3941 (void *)NineDevice9_GetDisplayMode,
3942 (void *)NineDevice9_GetCreationParameters,
3943 (void *)NineDevice9_SetCursorProperties,
3944 (void *)NineDevice9_SetCursorPosition,
3945 (void *)NineDevice9_ShowCursor,
3946 (void *)NineDevice9_CreateAdditionalSwapChain,
3947 (void *)NineDevice9_GetSwapChain,
3948 (void *)NineDevice9_GetNumberOfSwapChains,
3949 (void *)NineDevice9_Reset,
3950 (void *)NineDevice9_Present,
3951 (void *)NineDevice9_GetBackBuffer,
3952 (void *)NineDevice9_GetRasterStatus,
3953 (void *)NineDevice9_SetDialogBoxMode,
3954 (void *)NineDevice9_SetGammaRamp,
3955 (void *)NineDevice9_GetGammaRamp,
3956 (void *)NineDevice9_CreateTexture,
3957 (void *)NineDevice9_CreateVolumeTexture,
3958 (void *)NineDevice9_CreateCubeTexture,
3959 (void *)NineDevice9_CreateVertexBuffer,
3960 (void *)NineDevice9_CreateIndexBuffer,
3961 (void *)NineDevice9_CreateRenderTarget,
3962 (void *)NineDevice9_CreateDepthStencilSurface,
3963 (void *)NineDevice9_UpdateSurface,
3964 (void *)NineDevice9_UpdateTexture,
3965 (void *)NineDevice9_GetRenderTargetData,
3966 (void *)NineDevice9_GetFrontBufferData,
3967 (void *)NineDevice9_StretchRect,
3968 (void *)NineDevice9_ColorFill,
3969 (void *)NineDevice9_CreateOffscreenPlainSurface,
3970 (void *)NineDevice9_SetRenderTarget,
3971 (void *)NineDevice9_GetRenderTarget,
3972 (void *)NineDevice9_SetDepthStencilSurface,
3973 (void *)NineDevice9_GetDepthStencilSurface,
3974 (void *)NineDevice9_BeginScene,
3975 (void *)NineDevice9_EndScene,
3976 (void *)NineDevice9_Clear,
3977 (void *)NineDevice9_SetTransform,
3978 (void *)NineDevice9_GetTransform,
3979 (void *)NineDevice9_MultiplyTransform,
3980 (void *)NineDevice9_SetViewport,
3981 (void *)NineDevice9_GetViewport,
3982 (void *)NineDevice9_SetMaterial,
3983 (void *)NineDevice9_GetMaterial,
3984 (void *)NineDevice9_SetLight,
3985 (void *)NineDevice9_GetLight,
3986 (void *)NineDevice9_LightEnable,
3987 (void *)NineDevice9_GetLightEnable,
3988 (void *)NineDevice9_SetClipPlane,
3989 (void *)NineDevice9_GetClipPlane,
3990 (void *)NineDevice9_SetRenderState,
3991 (void *)NineDevice9_GetRenderState,
3992 (void *)NineDevice9_CreateStateBlock,
3993 (void *)NineDevice9_BeginStateBlock,
3994 (void *)NineDevice9_EndStateBlock,
3995 (void *)NineDevice9_SetClipStatus,
3996 (void *)NineDevice9_GetClipStatus,
3997 (void *)NineDevice9_GetTexture,
3998 (void *)NineDevice9_SetTexture,
3999 (void *)NineDevice9_GetTextureStageState,
4000 (void *)NineDevice9_SetTextureStageState,
4001 (void *)NineDevice9_GetSamplerState,
4002 (void *)NineDevice9_SetSamplerState,
4003 (void *)NineDevice9_ValidateDevice,
4004 (void *)NineDevice9_SetPaletteEntries,
4005 (void *)NineDevice9_GetPaletteEntries,
4006 (void *)NineDevice9_SetCurrentTexturePalette,
4007 (void *)NineDevice9_GetCurrentTexturePalette,
4008 (void *)NineDevice9_SetScissorRect,
4009 (void *)NineDevice9_GetScissorRect,
4010 (void *)NineDevice9_SetSoftwareVertexProcessing,
4011 (void *)NineDevice9_GetSoftwareVertexProcessing,
4012 (void *)NineDevice9_SetNPatchMode,
4013 (void *)NineDevice9_GetNPatchMode,
4014 (void *)NineDevice9_DrawPrimitive,
4015 (void *)NineDevice9_DrawIndexedPrimitive,
4016 (void *)NineDevice9_DrawPrimitiveUP,
4017 (void *)NineDevice9_DrawIndexedPrimitiveUP,
4018 (void *)NineDevice9_ProcessVertices,
4019 (void *)NineDevice9_CreateVertexDeclaration,
4020 (void *)NineDevice9_SetVertexDeclaration,
4021 (void *)NineDevice9_GetVertexDeclaration,
4022 (void *)NineDevice9_SetFVF,
4023 (void *)NineDevice9_GetFVF,
4024 (void *)NineDevice9_CreateVertexShader,
4025 (void *)NineDevice9_SetVertexShader,
4026 (void *)NineDevice9_GetVertexShader,
4027 (void *)NineDevice9_SetVertexShaderConstantF,
4028 (void *)NineDevice9_GetVertexShaderConstantF,
4029 (void *)NineDevice9_SetVertexShaderConstantI,
4030 (void *)NineDevice9_GetVertexShaderConstantI,
4031 (void *)NineDevice9_SetVertexShaderConstantB,
4032 (void *)NineDevice9_GetVertexShaderConstantB,
4033 (void *)NineDevice9_SetStreamSource,
4034 (void *)NineDevice9_GetStreamSource,
4035 (void *)NineDevice9_SetStreamSourceFreq,
4036 (void *)NineDevice9_GetStreamSourceFreq,
4037 (void *)NineDevice9_SetIndices,
4038 (void *)NineDevice9_GetIndices,
4039 (void *)NineDevice9_CreatePixelShader,
4040 (void *)NineDevice9_SetPixelShader,
4041 (void *)NineDevice9_GetPixelShader,
4042 (void *)NineDevice9_SetPixelShaderConstantF,
4043 (void *)NineDevice9_GetPixelShaderConstantF,
4044 (void *)NineDevice9_SetPixelShaderConstantI,
4045 (void *)NineDevice9_GetPixelShaderConstantI,
4046 (void *)NineDevice9_SetPixelShaderConstantB,
4047 (void *)NineDevice9_GetPixelShaderConstantB,
4048 (void *)NineDevice9_DrawRectPatch,
4049 (void *)NineDevice9_DrawTriPatch,
4050 (void *)NineDevice9_DeletePatch,
4051 (void *)NineDevice9_CreateQuery
4052 };
4053
4054 static const GUID *NineDevice9_IIDs[] = {
4055 &IID_IDirect3DDevice9,
4056 &IID_IUnknown,
4057 NULL
4058 };
4059
4060 HRESULT
4061 NineDevice9_new( struct pipe_screen *pScreen,
4062 D3DDEVICE_CREATION_PARAMETERS *pCreationParameters,
4063 D3DCAPS9 *pCaps,
4064 D3DPRESENT_PARAMETERS *pPresentationParameters,
4065 IDirect3D9 *pD3D9,
4066 ID3DPresentGroup *pPresentationGroup,
4067 struct d3dadapter9_context *pCTX,
4068 boolean ex,
4069 D3DDISPLAYMODEEX *pFullscreenDisplayMode,
4070 struct NineDevice9 **ppOut,
4071 int minorVersionNum )
4072 {
4073 BOOL lock;
4074 lock = !!(pCreationParameters->BehaviorFlags & D3DCREATE_MULTITHREADED);
4075
4076 NINE_NEW(Device9, ppOut, lock, /* args */
4077 pScreen, pCreationParameters, pCaps,
4078 pPresentationParameters, pD3D9, pPresentationGroup, pCTX,
4079 ex, pFullscreenDisplayMode, minorVersionNum );
4080 }