9923c894dd28951d859e19b34d417c889d3c14dc
[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 nine_context_set_render_state(This, D3DRS_ZENABLE, TRUE);
110 This->state.rs_advertised[D3DRS_ZENABLE] = TRUE;
111 }
112 if (This->state.rs_advertised[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 HRESULT NINE_WINAPI
2225 NineDevice9_SetRenderState( struct NineDevice9 *This,
2226 D3DRENDERSTATETYPE State,
2227 DWORD Value )
2228 {
2229 struct nine_state *state = This->update;
2230
2231 DBG("This=%p State=%u(%s) Value=%08x\n", This,
2232 State, nine_d3drs_to_string(State), Value);
2233
2234 user_assert(State < D3DRS_COUNT, D3DERR_INVALIDCALL);
2235
2236 if (unlikely(This->is_recording)) {
2237 state->rs_advertised[State] = Value;
2238 /* only need to record changed render states for stateblocks */
2239 state->changed.rs[State / 32] |= 1 << (State % 32);
2240 state->changed.group |= nine_render_state_group[State];
2241 return D3D_OK;
2242 }
2243
2244 if (state->rs_advertised[State] == Value)
2245 return D3D_OK;
2246
2247 state->rs_advertised[State] = Value;
2248 nine_context_set_render_state(This, State, Value);
2249
2250 return D3D_OK;
2251 }
2252
2253 HRESULT NINE_WINAPI
2254 NineDevice9_GetRenderState( struct NineDevice9 *This,
2255 D3DRENDERSTATETYPE State,
2256 DWORD *pValue )
2257 {
2258 user_assert(State < D3DRS_COUNT, D3DERR_INVALIDCALL);
2259
2260 *pValue = This->state.rs_advertised[State];
2261 return D3D_OK;
2262 }
2263
2264 HRESULT NINE_WINAPI
2265 NineDevice9_CreateStateBlock( struct NineDevice9 *This,
2266 D3DSTATEBLOCKTYPE Type,
2267 IDirect3DStateBlock9 **ppSB )
2268 {
2269 struct NineStateBlock9 *nsb;
2270 struct nine_state *dst;
2271 HRESULT hr;
2272 enum nine_stateblock_type type;
2273 unsigned s;
2274
2275 DBG("This=%p Type=%u ppSB=%p\n", This, Type, ppSB);
2276
2277 user_assert(Type == D3DSBT_ALL ||
2278 Type == D3DSBT_VERTEXSTATE ||
2279 Type == D3DSBT_PIXELSTATE, D3DERR_INVALIDCALL);
2280
2281 switch (Type) {
2282 case D3DSBT_VERTEXSTATE: type = NINESBT_VERTEXSTATE; break;
2283 case D3DSBT_PIXELSTATE: type = NINESBT_PIXELSTATE; break;
2284 default:
2285 type = NINESBT_ALL;
2286 break;
2287 }
2288
2289 hr = NineStateBlock9_new(This, &nsb, type);
2290 if (FAILED(hr))
2291 return hr;
2292 *ppSB = (IDirect3DStateBlock9 *)nsb;
2293 dst = &nsb->state;
2294
2295 dst->changed.group =
2296 NINE_STATE_TEXTURE |
2297 NINE_STATE_SAMPLER;
2298
2299 if (Type == D3DSBT_ALL || Type == D3DSBT_VERTEXSTATE) {
2300 dst->changed.group |=
2301 NINE_STATE_FF_LIGHTING |
2302 NINE_STATE_VS | NINE_STATE_VS_CONST |
2303 NINE_STATE_VDECL;
2304 /* TODO: texture/sampler state */
2305 memcpy(dst->changed.rs,
2306 nine_render_states_vertex, sizeof(dst->changed.rs));
2307 nine_ranges_insert(&dst->changed.vs_const_f, 0, This->may_swvp ? NINE_MAX_CONST_F_SWVP : This->max_vs_const_f,
2308 &This->range_pool);
2309 nine_ranges_insert(&dst->changed.vs_const_i, 0, This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I,
2310 &This->range_pool);
2311 nine_ranges_insert(&dst->changed.vs_const_b, 0, This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B,
2312 &This->range_pool);
2313 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2314 dst->changed.sampler[s] |= 1 << D3DSAMP_DMAPOFFSET;
2315 if (This->state.ff.num_lights) {
2316 dst->ff.num_lights = This->state.ff.num_lights;
2317 /* zero'd -> light type won't be NINED3DLIGHT_INVALID, so
2318 * all currently existing lights will be captured
2319 */
2320 dst->ff.light = CALLOC(This->state.ff.num_lights,
2321 sizeof(D3DLIGHT9));
2322 if (!dst->ff.light) {
2323 nine_bind(ppSB, NULL);
2324 return E_OUTOFMEMORY;
2325 }
2326 }
2327 }
2328 if (Type == D3DSBT_ALL || Type == D3DSBT_PIXELSTATE) {
2329 dst->changed.group |=
2330 NINE_STATE_PS | NINE_STATE_PS_CONST | NINE_STATE_BLEND |
2331 NINE_STATE_FF_OTHER | NINE_STATE_FF_PSSTAGES | NINE_STATE_PS_CONST |
2332 NINE_STATE_FB | NINE_STATE_DSA | NINE_STATE_MULTISAMPLE |
2333 NINE_STATE_RASTERIZER | NINE_STATE_STENCIL_REF;
2334 memcpy(dst->changed.rs,
2335 nine_render_states_pixel, sizeof(dst->changed.rs));
2336 nine_ranges_insert(&dst->changed.ps_const_f, 0, This->max_ps_const_f,
2337 &This->range_pool);
2338 dst->changed.ps_const_i = 0xffff;
2339 dst->changed.ps_const_b = 0xffff;
2340 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
2341 dst->changed.sampler[s] |= 0x1ffe;
2342 for (s = 0; s < NINE_MAX_TEXTURE_STAGES; ++s) {
2343 dst->ff.changed.tex_stage[s][0] |= 0xffffffff;
2344 dst->ff.changed.tex_stage[s][1] |= 0xffffffff;
2345 }
2346 }
2347 if (Type == D3DSBT_ALL) {
2348 dst->changed.group |=
2349 NINE_STATE_VIEWPORT |
2350 NINE_STATE_SCISSOR |
2351 NINE_STATE_RASTERIZER |
2352 NINE_STATE_BLEND |
2353 NINE_STATE_DSA |
2354 NINE_STATE_IDXBUF |
2355 NINE_STATE_MATERIAL |
2356 NINE_STATE_BLEND_COLOR |
2357 NINE_STATE_SAMPLE_MASK;
2358 memset(dst->changed.rs, ~0, (D3DRS_COUNT / 32) * sizeof(uint32_t));
2359 dst->changed.rs[D3DRS_LAST / 32] |= (1 << (D3DRS_COUNT % 32)) - 1;
2360 dst->changed.vtxbuf = (1ULL << This->caps.MaxStreams) - 1;
2361 dst->changed.stream_freq = dst->changed.vtxbuf;
2362 dst->changed.ucp = (1 << PIPE_MAX_CLIP_PLANES) - 1;
2363 dst->changed.texture = (1 << NINE_MAX_SAMPLERS) - 1;
2364 }
2365 NineStateBlock9_Capture(NineStateBlock9(*ppSB));
2366
2367 /* TODO: fixed function state */
2368
2369 return D3D_OK;
2370 }
2371
2372 HRESULT NINE_WINAPI
2373 NineDevice9_BeginStateBlock( struct NineDevice9 *This )
2374 {
2375 HRESULT hr;
2376
2377 DBG("This=%p\n", This);
2378
2379 user_assert(!This->record, D3DERR_INVALIDCALL);
2380
2381 hr = NineStateBlock9_new(This, &This->record, NINESBT_CUSTOM);
2382 if (FAILED(hr))
2383 return hr;
2384 NineUnknown_ConvertRefToBind(NineUnknown(This->record));
2385
2386 This->update = &This->record->state;
2387 This->is_recording = TRUE;
2388
2389 return D3D_OK;
2390 }
2391
2392 HRESULT NINE_WINAPI
2393 NineDevice9_EndStateBlock( struct NineDevice9 *This,
2394 IDirect3DStateBlock9 **ppSB )
2395 {
2396 DBG("This=%p ppSB=%p\n", This, ppSB);
2397
2398 user_assert(This->record, D3DERR_INVALIDCALL);
2399
2400 This->update = &This->state;
2401 This->is_recording = FALSE;
2402
2403 NineUnknown_AddRef(NineUnknown(This->record));
2404 *ppSB = (IDirect3DStateBlock9 *)This->record;
2405 NineUnknown_Unbind(NineUnknown(This->record));
2406 This->record = NULL;
2407
2408 return D3D_OK;
2409 }
2410
2411 HRESULT NINE_WINAPI
2412 NineDevice9_SetClipStatus( struct NineDevice9 *This,
2413 const D3DCLIPSTATUS9 *pClipStatus )
2414 {
2415 STUB(D3DERR_INVALIDCALL);
2416 }
2417
2418 HRESULT NINE_WINAPI
2419 NineDevice9_GetClipStatus( struct NineDevice9 *This,
2420 D3DCLIPSTATUS9 *pClipStatus )
2421 {
2422 STUB(D3DERR_INVALIDCALL);
2423 }
2424
2425 HRESULT NINE_WINAPI
2426 NineDevice9_GetTexture( struct NineDevice9 *This,
2427 DWORD Stage,
2428 IDirect3DBaseTexture9 **ppTexture )
2429 {
2430 user_assert(Stage < This->caps.MaxSimultaneousTextures ||
2431 Stage == D3DDMAPSAMPLER ||
2432 (Stage >= D3DVERTEXTEXTURESAMPLER0 &&
2433 Stage <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2434 user_assert(ppTexture, D3DERR_INVALIDCALL);
2435
2436 if (Stage >= D3DDMAPSAMPLER)
2437 Stage = Stage - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2438
2439 *ppTexture = (IDirect3DBaseTexture9 *)This->state.texture[Stage];
2440
2441 if (This->state.texture[Stage])
2442 NineUnknown_AddRef(NineUnknown(This->state.texture[Stage]));
2443 return D3D_OK;
2444 }
2445
2446 HRESULT NINE_WINAPI
2447 NineDevice9_SetTexture( struct NineDevice9 *This,
2448 DWORD Stage,
2449 IDirect3DBaseTexture9 *pTexture )
2450 {
2451 struct nine_state *state = This->update;
2452 struct NineBaseTexture9 *tex = NineBaseTexture9(pTexture);
2453 struct NineBaseTexture9 *old;
2454
2455 DBG("This=%p Stage=%u pTexture=%p\n", This, Stage, pTexture);
2456
2457 user_assert(Stage < This->caps.MaxSimultaneousTextures ||
2458 Stage == D3DDMAPSAMPLER ||
2459 (Stage >= D3DVERTEXTEXTURESAMPLER0 &&
2460 Stage <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2461 user_assert(!tex || (tex->base.pool != D3DPOOL_SCRATCH &&
2462 tex->base.pool != D3DPOOL_SYSTEMMEM), D3DERR_INVALIDCALL);
2463
2464 if (Stage >= D3DDMAPSAMPLER)
2465 Stage = Stage - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2466
2467 if (This->is_recording) {
2468 state->changed.texture |= 1 << Stage;
2469 state->changed.group |= NINE_STATE_TEXTURE;
2470 nine_bind(&state->texture[Stage], pTexture);
2471 return D3D_OK;
2472 }
2473
2474 old = state->texture[Stage];
2475 if (old == tex)
2476 return D3D_OK;
2477
2478 if (tex) {
2479 if ((tex->managed.dirty | tex->dirty_mip) && LIST_IS_EMPTY(&tex->list))
2480 list_add(&tex->list, &This->update_textures);
2481
2482 tex->bind_count++;
2483 }
2484 if (old)
2485 old->bind_count--;
2486
2487 nine_context_set_texture(This, Stage, tex);
2488
2489 nine_bind(&state->texture[Stage], pTexture);
2490
2491 return D3D_OK;
2492 }
2493
2494 HRESULT NINE_WINAPI
2495 NineDevice9_GetTextureStageState( struct NineDevice9 *This,
2496 DWORD Stage,
2497 D3DTEXTURESTAGESTATETYPE Type,
2498 DWORD *pValue )
2499 {
2500 const struct nine_state *state = &This->state;
2501
2502 user_assert(Stage < ARRAY_SIZE(state->ff.tex_stage), D3DERR_INVALIDCALL);
2503 user_assert(Type < ARRAY_SIZE(state->ff.tex_stage[0]), D3DERR_INVALIDCALL);
2504
2505 *pValue = state->ff.tex_stage[Stage][Type];
2506
2507 return D3D_OK;
2508 }
2509
2510 HRESULT NINE_WINAPI
2511 NineDevice9_SetTextureStageState( struct NineDevice9 *This,
2512 DWORD Stage,
2513 D3DTEXTURESTAGESTATETYPE Type,
2514 DWORD Value )
2515 {
2516 struct nine_state *state = This->update;
2517 struct nine_context *context = &This->context;
2518 int bumpmap_index = -1;
2519
2520 DBG("Stage=%u Type=%u Value=%08x\n", Stage, Type, Value);
2521 nine_dump_D3DTSS_value(DBG_FF, Type, Value);
2522
2523 user_assert(Stage < ARRAY_SIZE(state->ff.tex_stage), D3DERR_INVALIDCALL);
2524 user_assert(Type < ARRAY_SIZE(state->ff.tex_stage[0]), D3DERR_INVALIDCALL);
2525
2526 state->ff.tex_stage[Stage][Type] = Value;
2527 switch (Type) {
2528 case D3DTSS_BUMPENVMAT00:
2529 bumpmap_index = 4 * Stage;
2530 break;
2531 case D3DTSS_BUMPENVMAT01:
2532 bumpmap_index = 4 * Stage + 1;
2533 break;
2534 case D3DTSS_BUMPENVMAT10:
2535 bumpmap_index = 4 * Stage + 2;
2536 break;
2537 case D3DTSS_BUMPENVMAT11:
2538 bumpmap_index = 4 * Stage + 3;
2539 break;
2540 case D3DTSS_BUMPENVLSCALE:
2541 bumpmap_index = 4 * 8 + 2 * Stage;
2542 break;
2543 case D3DTSS_BUMPENVLOFFSET:
2544 bumpmap_index = 4 * 8 + 2 * Stage + 1;
2545 break;
2546 case D3DTSS_TEXTURETRANSFORMFLAGS:
2547 state->changed.group |= NINE_STATE_PS1X_SHADER;
2548 break;
2549 default:
2550 break;
2551 }
2552
2553 if (bumpmap_index >= 0 && !This->is_recording) {
2554 context->bumpmap_vars[bumpmap_index] = Value;
2555 state->changed.group |= NINE_STATE_PS_CONST;
2556 }
2557
2558 state->changed.group |= NINE_STATE_FF_PSSTAGES;
2559 state->ff.changed.tex_stage[Stage][Type / 32] |= 1 << (Type % 32);
2560
2561 return D3D_OK;
2562 }
2563
2564 HRESULT NINE_WINAPI
2565 NineDevice9_GetSamplerState( struct NineDevice9 *This,
2566 DWORD Sampler,
2567 D3DSAMPLERSTATETYPE Type,
2568 DWORD *pValue )
2569 {
2570 user_assert(Sampler < This->caps.MaxSimultaneousTextures ||
2571 Sampler == D3DDMAPSAMPLER ||
2572 (Sampler >= D3DVERTEXTEXTURESAMPLER0 &&
2573 Sampler <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2574
2575 if (Sampler >= D3DDMAPSAMPLER)
2576 Sampler = Sampler - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2577
2578 *pValue = This->state.samp_advertised[Sampler][Type];
2579 return D3D_OK;
2580 }
2581
2582 HRESULT NINE_WINAPI
2583 NineDevice9_SetSamplerState( struct NineDevice9 *This,
2584 DWORD Sampler,
2585 D3DSAMPLERSTATETYPE Type,
2586 DWORD Value )
2587 {
2588 struct nine_state *state = This->update;
2589
2590 DBG("This=%p Sampler=%u Type=%s Value=%08x\n", This,
2591 Sampler, nine_D3DSAMP_to_str(Type), Value);
2592
2593 user_assert(Sampler < This->caps.MaxSimultaneousTextures ||
2594 Sampler == D3DDMAPSAMPLER ||
2595 (Sampler >= D3DVERTEXTEXTURESAMPLER0 &&
2596 Sampler <= D3DVERTEXTEXTURESAMPLER3), D3DERR_INVALIDCALL);
2597
2598 if (Sampler >= D3DDMAPSAMPLER)
2599 Sampler = Sampler - D3DDMAPSAMPLER + NINE_MAX_SAMPLERS_PS;
2600
2601 if (state->samp_advertised[Sampler][Type] != Value || unlikely(This->is_recording)) {
2602 /* Contrary to render states, old value is kept if new value is wrong (except intel + Value == 0) */
2603 if (likely(nine_check_sampler_state_value(Type, Value)))
2604 state->samp[Sampler][Type] = Value;
2605 state->samp_advertised[Sampler][Type] = Value;
2606 state->changed.group |= NINE_STATE_SAMPLER;
2607 state->changed.sampler[Sampler] |= 1 << Type;
2608 }
2609
2610 return D3D_OK;
2611 }
2612
2613 HRESULT NINE_WINAPI
2614 NineDevice9_ValidateDevice( struct NineDevice9 *This,
2615 DWORD *pNumPasses )
2616 {
2617 const struct nine_state *state = &This->state;
2618 unsigned i;
2619 unsigned w = 0, h = 0;
2620
2621 DBG("This=%p pNumPasses=%p\n", This, pNumPasses);
2622
2623 for (i = 0; i < ARRAY_SIZE(state->samp); ++i) {
2624 if (state->samp[i][D3DSAMP_MINFILTER] == D3DTEXF_NONE ||
2625 state->samp[i][D3DSAMP_MAGFILTER] == D3DTEXF_NONE)
2626 return D3DERR_UNSUPPORTEDTEXTUREFILTER;
2627 }
2628
2629 for (i = 0; i < This->caps.NumSimultaneousRTs; ++i) {
2630 if (!state->rt[i])
2631 continue;
2632 if (w == 0) {
2633 w = state->rt[i]->desc.Width;
2634 h = state->rt[i]->desc.Height;
2635 } else
2636 if (state->rt[i]->desc.Width != w || state->rt[i]->desc.Height != h) {
2637 return D3DERR_CONFLICTINGRENDERSTATE;
2638 }
2639 }
2640 if (state->ds &&
2641 (state->rs_advertised[D3DRS_ZENABLE] || state->rs_advertised[D3DRS_STENCILENABLE])) {
2642 if (w != 0 &&
2643 (state->ds->desc.Width != w || state->ds->desc.Height != h))
2644 return D3DERR_CONFLICTINGRENDERSTATE;
2645 }
2646
2647 if (pNumPasses)
2648 *pNumPasses = 1;
2649
2650 return D3D_OK;
2651 }
2652
2653 HRESULT NINE_WINAPI
2654 NineDevice9_SetPaletteEntries( struct NineDevice9 *This,
2655 UINT PaletteNumber,
2656 const PALETTEENTRY *pEntries )
2657 {
2658 STUB(D3D_OK); /* like wine */
2659 }
2660
2661 HRESULT NINE_WINAPI
2662 NineDevice9_GetPaletteEntries( struct NineDevice9 *This,
2663 UINT PaletteNumber,
2664 PALETTEENTRY *pEntries )
2665 {
2666 STUB(D3DERR_INVALIDCALL);
2667 }
2668
2669 HRESULT NINE_WINAPI
2670 NineDevice9_SetCurrentTexturePalette( struct NineDevice9 *This,
2671 UINT PaletteNumber )
2672 {
2673 STUB(D3D_OK); /* like wine */
2674 }
2675
2676 HRESULT NINE_WINAPI
2677 NineDevice9_GetCurrentTexturePalette( struct NineDevice9 *This,
2678 UINT *PaletteNumber )
2679 {
2680 STUB(D3DERR_INVALIDCALL);
2681 }
2682
2683 HRESULT NINE_WINAPI
2684 NineDevice9_SetScissorRect( struct NineDevice9 *This,
2685 const RECT *pRect )
2686 {
2687 struct nine_state *state = This->update;
2688
2689 DBG("x=(%u..%u) y=(%u..%u)\n",
2690 pRect->left, pRect->top, pRect->right, pRect->bottom);
2691
2692 state->scissor.minx = pRect->left;
2693 state->scissor.miny = pRect->top;
2694 state->scissor.maxx = pRect->right;
2695 state->scissor.maxy = pRect->bottom;
2696
2697 state->changed.group |= NINE_STATE_SCISSOR;
2698
2699 return D3D_OK;
2700 }
2701
2702 HRESULT NINE_WINAPI
2703 NineDevice9_GetScissorRect( struct NineDevice9 *This,
2704 RECT *pRect )
2705 {
2706 pRect->left = This->state.scissor.minx;
2707 pRect->top = This->state.scissor.miny;
2708 pRect->right = This->state.scissor.maxx;
2709 pRect->bottom = This->state.scissor.maxy;
2710
2711 return D3D_OK;
2712 }
2713
2714 HRESULT NINE_WINAPI
2715 NineDevice9_SetSoftwareVertexProcessing( struct NineDevice9 *This,
2716 BOOL bSoftware )
2717 {
2718 if (This->params.BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING) {
2719 This->swvp = bSoftware;
2720 This->state.changed.group |= NINE_STATE_SWVP;
2721 return D3D_OK;
2722 } else
2723 return D3DERR_INVALIDCALL; /* msdn. TODO: check in practice */
2724 }
2725
2726 BOOL NINE_WINAPI
2727 NineDevice9_GetSoftwareVertexProcessing( struct NineDevice9 *This )
2728 {
2729 return This->swvp;
2730 }
2731
2732 HRESULT NINE_WINAPI
2733 NineDevice9_SetNPatchMode( struct NineDevice9 *This,
2734 float nSegments )
2735 {
2736 return D3D_OK; /* Nothing to do because we don't advertise NPatch support */
2737 }
2738
2739 float NINE_WINAPI
2740 NineDevice9_GetNPatchMode( struct NineDevice9 *This )
2741 {
2742 STUB(0);
2743 }
2744
2745 HRESULT NINE_WINAPI
2746 NineDevice9_DrawPrimitive( struct NineDevice9 *This,
2747 D3DPRIMITIVETYPE PrimitiveType,
2748 UINT StartVertex,
2749 UINT PrimitiveCount )
2750 {
2751 DBG("iface %p, PrimitiveType %u, StartVertex %u, PrimitiveCount %u\n",
2752 This, PrimitiveType, StartVertex, PrimitiveCount);
2753
2754 nine_context_draw_primitive(This, PrimitiveType, StartVertex, PrimitiveCount);
2755
2756 return D3D_OK;
2757 }
2758
2759 HRESULT NINE_WINAPI
2760 NineDevice9_DrawIndexedPrimitive( struct NineDevice9 *This,
2761 D3DPRIMITIVETYPE PrimitiveType,
2762 INT BaseVertexIndex,
2763 UINT MinVertexIndex,
2764 UINT NumVertices,
2765 UINT StartIndex,
2766 UINT PrimitiveCount )
2767 {
2768 DBG("iface %p, PrimitiveType %u, BaseVertexIndex %u, MinVertexIndex %u "
2769 "NumVertices %u, StartIndex %u, PrimitiveCount %u\n",
2770 This, PrimitiveType, BaseVertexIndex, MinVertexIndex, NumVertices,
2771 StartIndex, PrimitiveCount);
2772
2773 user_assert(This->state.idxbuf, D3DERR_INVALIDCALL);
2774 user_assert(This->state.vdecl, D3DERR_INVALIDCALL);
2775
2776 nine_context_draw_indexed_primitive(This, PrimitiveType, BaseVertexIndex,
2777 MinVertexIndex, NumVertices, StartIndex,
2778 PrimitiveCount);
2779
2780 return D3D_OK;
2781 }
2782
2783 HRESULT NINE_WINAPI
2784 NineDevice9_DrawPrimitiveUP( struct NineDevice9 *This,
2785 D3DPRIMITIVETYPE PrimitiveType,
2786 UINT PrimitiveCount,
2787 const void *pVertexStreamZeroData,
2788 UINT VertexStreamZeroStride )
2789 {
2790 struct pipe_vertex_buffer vtxbuf;
2791
2792 DBG("iface %p, PrimitiveType %u, PrimitiveCount %u, data %p, stride %u\n",
2793 This, PrimitiveType, PrimitiveCount,
2794 pVertexStreamZeroData, VertexStreamZeroStride);
2795
2796 user_assert(pVertexStreamZeroData && VertexStreamZeroStride,
2797 D3DERR_INVALIDCALL);
2798 user_assert(PrimitiveCount, D3D_OK);
2799
2800 vtxbuf.stride = VertexStreamZeroStride;
2801 vtxbuf.buffer_offset = 0;
2802 vtxbuf.buffer = NULL;
2803 vtxbuf.user_buffer = pVertexStreamZeroData;
2804
2805 if (!This->driver_caps.user_vbufs) {
2806 u_upload_data(This->vertex_uploader,
2807 0,
2808 (prim_count_to_vertex_count(PrimitiveType, PrimitiveCount)) * VertexStreamZeroStride, /* XXX */
2809 4,
2810 vtxbuf.user_buffer,
2811 &vtxbuf.buffer_offset,
2812 &vtxbuf.buffer);
2813 u_upload_unmap(This->vertex_uploader);
2814 vtxbuf.user_buffer = NULL;
2815 }
2816
2817 nine_context_draw_primitive_from_vtxbuf(This, PrimitiveType, PrimitiveCount, &vtxbuf);
2818
2819 pipe_resource_reference(&vtxbuf.buffer, NULL);
2820
2821 NineDevice9_PauseRecording(This);
2822 NineDevice9_SetStreamSource(This, 0, NULL, 0, 0);
2823 NineDevice9_ResumeRecording(This);
2824
2825 return D3D_OK;
2826 }
2827
2828 HRESULT NINE_WINAPI
2829 NineDevice9_DrawIndexedPrimitiveUP( struct NineDevice9 *This,
2830 D3DPRIMITIVETYPE PrimitiveType,
2831 UINT MinVertexIndex,
2832 UINT NumVertices,
2833 UINT PrimitiveCount,
2834 const void *pIndexData,
2835 D3DFORMAT IndexDataFormat,
2836 const void *pVertexStreamZeroData,
2837 UINT VertexStreamZeroStride )
2838 {
2839 struct pipe_vertex_buffer vbuf;
2840 struct pipe_index_buffer ibuf;
2841
2842 DBG("iface %p, PrimitiveType %u, MinVertexIndex %u, NumVertices %u "
2843 "PrimitiveCount %u, pIndexData %p, IndexDataFormat %u "
2844 "pVertexStreamZeroData %p, VertexStreamZeroStride %u\n",
2845 This, PrimitiveType, MinVertexIndex, NumVertices, PrimitiveCount,
2846 pIndexData, IndexDataFormat,
2847 pVertexStreamZeroData, VertexStreamZeroStride);
2848
2849 user_assert(pIndexData && pVertexStreamZeroData, D3DERR_INVALIDCALL);
2850 user_assert(VertexStreamZeroStride, D3DERR_INVALIDCALL);
2851 user_assert(IndexDataFormat == D3DFMT_INDEX16 ||
2852 IndexDataFormat == D3DFMT_INDEX32, D3DERR_INVALIDCALL);
2853 user_assert(PrimitiveCount, D3D_OK);
2854
2855 vbuf.stride = VertexStreamZeroStride;
2856 vbuf.buffer_offset = 0;
2857 vbuf.buffer = NULL;
2858 vbuf.user_buffer = pVertexStreamZeroData;
2859
2860 ibuf.index_size = (IndexDataFormat == D3DFMT_INDEX16) ? 2 : 4;
2861 ibuf.offset = 0;
2862 ibuf.buffer = NULL;
2863 ibuf.user_buffer = pIndexData;
2864
2865 if (!This->driver_caps.user_vbufs) {
2866 const unsigned base = MinVertexIndex * VertexStreamZeroStride;
2867 u_upload_data(This->vertex_uploader,
2868 base,
2869 NumVertices * VertexStreamZeroStride, /* XXX */
2870 4,
2871 (const uint8_t *)vbuf.user_buffer + base,
2872 &vbuf.buffer_offset,
2873 &vbuf.buffer);
2874 u_upload_unmap(This->vertex_uploader);
2875 /* Won't be used: */
2876 vbuf.buffer_offset -= base;
2877 vbuf.user_buffer = NULL;
2878 }
2879 if (!This->driver_caps.user_ibufs) {
2880 u_upload_data(This->index_uploader,
2881 0,
2882 (prim_count_to_vertex_count(PrimitiveType, PrimitiveCount)) * ibuf.index_size,
2883 4,
2884 ibuf.user_buffer,
2885 &ibuf.offset,
2886 &ibuf.buffer);
2887 u_upload_unmap(This->index_uploader);
2888 ibuf.user_buffer = NULL;
2889 }
2890
2891 nine_context_draw_indexed_primitive_from_vtxbuf_idxbuf(This, PrimitiveType,
2892 MinVertexIndex,
2893 NumVertices,
2894 PrimitiveCount,
2895 &vbuf,
2896 &ibuf);
2897
2898 pipe_resource_reference(&vbuf.buffer, NULL);
2899 pipe_resource_reference(&ibuf.buffer, NULL);
2900
2901 NineDevice9_PauseRecording(This);
2902 NineDevice9_SetIndices(This, NULL);
2903 NineDevice9_SetStreamSource(This, 0, NULL, 0, 0);
2904 NineDevice9_ResumeRecording(This);
2905
2906 return D3D_OK;
2907 }
2908
2909 HRESULT NINE_WINAPI
2910 NineDevice9_ProcessVertices( struct NineDevice9 *This,
2911 UINT SrcStartIndex,
2912 UINT DestIndex,
2913 UINT VertexCount,
2914 IDirect3DVertexBuffer9 *pDestBuffer,
2915 IDirect3DVertexDeclaration9 *pVertexDecl,
2916 DWORD Flags )
2917 {
2918 struct pipe_screen *screen_sw = This->screen_sw;
2919 struct pipe_context *pipe_sw = This->pipe_sw;
2920 struct NineVertexDeclaration9 *vdecl = NineVertexDeclaration9(pVertexDecl);
2921 struct NineVertexBuffer9 *dst = NineVertexBuffer9(pDestBuffer);
2922 struct NineVertexShader9 *vs;
2923 struct pipe_resource *resource;
2924 struct pipe_transfer *transfer = NULL;
2925 struct pipe_stream_output_info so;
2926 struct pipe_stream_output_target *target;
2927 struct pipe_draw_info draw;
2928 struct pipe_box box;
2929 unsigned offsets[1] = {0};
2930 HRESULT hr;
2931 unsigned buffer_size;
2932 void *map;
2933
2934 DBG("This=%p SrcStartIndex=%u DestIndex=%u VertexCount=%u "
2935 "pDestBuffer=%p pVertexDecl=%p Flags=%d\n",
2936 This, SrcStartIndex, DestIndex, VertexCount, pDestBuffer,
2937 pVertexDecl, Flags);
2938
2939 if (!screen_sw->get_param(screen_sw, PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS)) {
2940 DBG("ProcessVertices not supported\n");
2941 return D3DERR_INVALIDCALL;
2942 }
2943
2944
2945 vs = This->state.programmable_vs ? This->state.vs : This->ff.vs;
2946 /* Note: version is 0 for ff */
2947 user_assert(vdecl || (vs->byte_code.version < 0x30 && dst->desc.FVF),
2948 D3DERR_INVALIDCALL);
2949 if (!vdecl) {
2950 DWORD FVF = dst->desc.FVF;
2951 vdecl = util_hash_table_get(This->ff.ht_fvf, &FVF);
2952 if (!vdecl) {
2953 hr = NineVertexDeclaration9_new_from_fvf(This, FVF, &vdecl);
2954 if (FAILED(hr))
2955 return hr;
2956 vdecl->fvf = FVF;
2957 util_hash_table_set(This->ff.ht_fvf, &vdecl->fvf, vdecl);
2958 NineUnknown_ConvertRefToBind(NineUnknown(vdecl));
2959 }
2960 }
2961
2962 /* Flags: Can be 0 or D3DPV_DONOTCOPYDATA, and/or lock flags
2963 * D3DPV_DONOTCOPYDATA -> Has effect only for ff. In particular
2964 * if not set, everything from src will be used, and dst
2965 * must match exactly the ff vs outputs.
2966 * TODO: Handle all the checks, etc for ff */
2967 user_assert(vdecl->position_t || This->state.programmable_vs,
2968 D3DERR_INVALIDCALL);
2969
2970 /* TODO: Support vs < 3 and ff */
2971 user_assert(vs->byte_code.version == 0x30,
2972 D3DERR_INVALIDCALL);
2973 /* TODO: Not hardcode the constant buffers for swvp */
2974 user_assert(This->may_swvp,
2975 D3DERR_INVALIDCALL);
2976
2977 nine_state_prepare_draw_sw(This, vdecl, SrcStartIndex, VertexCount, &so);
2978
2979 buffer_size = VertexCount * so.stride[0] * 4;
2980 {
2981 struct pipe_resource templ;
2982
2983 memset(&templ, 0, sizeof(templ));
2984 templ.target = PIPE_BUFFER;
2985 templ.format = PIPE_FORMAT_R8_UNORM;
2986 templ.width0 = buffer_size;
2987 templ.flags = 0;
2988 templ.bind = PIPE_BIND_STREAM_OUTPUT;
2989 templ.usage = PIPE_USAGE_STREAM;
2990 templ.height0 = templ.depth0 = templ.array_size = 1;
2991 templ.last_level = templ.nr_samples = 0;
2992
2993 resource = screen_sw->resource_create(screen_sw, &templ);
2994 if (!resource)
2995 return E_OUTOFMEMORY;
2996 }
2997 target = pipe_sw->create_stream_output_target(pipe_sw, resource,
2998 0, buffer_size);
2999 if (!target) {
3000 pipe_resource_reference(&resource, NULL);
3001 return D3DERR_DRIVERINTERNALERROR;
3002 }
3003
3004 draw.mode = PIPE_PRIM_POINTS;
3005 draw.count = VertexCount;
3006 draw.start_instance = 0;
3007 draw.primitive_restart = FALSE;
3008 draw.restart_index = 0;
3009 draw.count_from_stream_output = NULL;
3010 draw.indirect = NULL;
3011 draw.indirect_params = NULL;
3012 draw.instance_count = 1;
3013 draw.indexed = FALSE;
3014 draw.start = 0;
3015 draw.index_bias = 0;
3016 draw.min_index = 0;
3017 draw.max_index = VertexCount - 1;
3018
3019
3020 pipe_sw->set_stream_output_targets(pipe_sw, 1, &target, offsets);
3021
3022 pipe_sw->draw_vbo(pipe_sw, &draw);
3023
3024 pipe_sw->set_stream_output_targets(pipe_sw, 0, NULL, 0);
3025 pipe_sw->stream_output_target_destroy(pipe_sw, target);
3026
3027 u_box_1d(0, VertexCount * so.stride[0] * 4, &box);
3028 map = pipe_sw->transfer_map(pipe_sw, resource, 0, PIPE_TRANSFER_READ, &box,
3029 &transfer);
3030 if (!map) {
3031 hr = D3DERR_DRIVERINTERNALERROR;
3032 goto out;
3033 }
3034
3035 hr = NineVertexDeclaration9_ConvertStreamOutput(vdecl,
3036 dst, DestIndex, VertexCount,
3037 map, &so);
3038 if (transfer)
3039 pipe_sw->transfer_unmap(pipe_sw, transfer);
3040
3041 out:
3042 nine_state_after_draw_sw(This);
3043 pipe_resource_reference(&resource, NULL);
3044 return hr;
3045 }
3046
3047 HRESULT NINE_WINAPI
3048 NineDevice9_CreateVertexDeclaration( struct NineDevice9 *This,
3049 const D3DVERTEXELEMENT9 *pVertexElements,
3050 IDirect3DVertexDeclaration9 **ppDecl )
3051 {
3052 struct NineVertexDeclaration9 *vdecl;
3053
3054 DBG("This=%p pVertexElements=%p ppDecl=%p\n",
3055 This, pVertexElements, ppDecl);
3056
3057 HRESULT hr = NineVertexDeclaration9_new(This, pVertexElements, &vdecl);
3058 if (SUCCEEDED(hr))
3059 *ppDecl = (IDirect3DVertexDeclaration9 *)vdecl;
3060
3061 return hr;
3062 }
3063
3064 HRESULT NINE_WINAPI
3065 NineDevice9_SetVertexDeclaration( struct NineDevice9 *This,
3066 IDirect3DVertexDeclaration9 *pDecl )
3067 {
3068 struct nine_state *state = This->update;
3069 struct nine_context *context = &This->context;
3070 BOOL was_programmable_vs = This->state.programmable_vs;
3071
3072 DBG("This=%p pDecl=%p\n", This, pDecl);
3073
3074 if (likely(!This->is_recording) && state->vdecl == NineVertexDeclaration9(pDecl))
3075 return D3D_OK;
3076
3077 nine_bind(&state->vdecl, pDecl);
3078
3079 This->state.programmable_vs = This->state.vs && !(This->state.vdecl && This->state.vdecl->position_t);
3080 if (likely(!This->is_recording) && was_programmable_vs != This->state.programmable_vs) {
3081 context->commit |= NINE_STATE_COMMIT_CONST_VS;
3082 state->changed.group |= NINE_STATE_VS;
3083 }
3084
3085 state->changed.group |= NINE_STATE_VDECL;
3086
3087 return D3D_OK;
3088 }
3089
3090 HRESULT NINE_WINAPI
3091 NineDevice9_GetVertexDeclaration( struct NineDevice9 *This,
3092 IDirect3DVertexDeclaration9 **ppDecl )
3093 {
3094 user_assert(ppDecl, D3DERR_INVALIDCALL);
3095
3096 *ppDecl = (IDirect3DVertexDeclaration9 *)This->state.vdecl;
3097 if (*ppDecl)
3098 NineUnknown_AddRef(NineUnknown(*ppDecl));
3099 return D3D_OK;
3100 }
3101
3102 HRESULT NINE_WINAPI
3103 NineDevice9_SetFVF( struct NineDevice9 *This,
3104 DWORD FVF )
3105 {
3106 struct NineVertexDeclaration9 *vdecl;
3107 HRESULT hr;
3108
3109 DBG("FVF = %08x\n", FVF);
3110 if (!FVF)
3111 return D3D_OK; /* like wine */
3112
3113 vdecl = util_hash_table_get(This->ff.ht_fvf, &FVF);
3114 if (!vdecl) {
3115 hr = NineVertexDeclaration9_new_from_fvf(This, FVF, &vdecl);
3116 if (FAILED(hr))
3117 return hr;
3118 vdecl->fvf = FVF;
3119 util_hash_table_set(This->ff.ht_fvf, &vdecl->fvf, vdecl);
3120 NineUnknown_ConvertRefToBind(NineUnknown(vdecl));
3121 }
3122 return NineDevice9_SetVertexDeclaration(
3123 This, (IDirect3DVertexDeclaration9 *)vdecl);
3124 }
3125
3126 HRESULT NINE_WINAPI
3127 NineDevice9_GetFVF( struct NineDevice9 *This,
3128 DWORD *pFVF )
3129 {
3130 *pFVF = This->state.vdecl ? This->state.vdecl->fvf : 0;
3131 return D3D_OK;
3132 }
3133
3134 HRESULT NINE_WINAPI
3135 NineDevice9_CreateVertexShader( struct NineDevice9 *This,
3136 const DWORD *pFunction,
3137 IDirect3DVertexShader9 **ppShader )
3138 {
3139 struct NineVertexShader9 *vs;
3140 HRESULT hr;
3141
3142 DBG("This=%p pFunction=%p ppShader=%p\n", This, pFunction, ppShader);
3143
3144 hr = NineVertexShader9_new(This, &vs, pFunction, NULL);
3145 if (FAILED(hr))
3146 return hr;
3147 *ppShader = (IDirect3DVertexShader9 *)vs;
3148 return D3D_OK;
3149 }
3150
3151 HRESULT NINE_WINAPI
3152 NineDevice9_SetVertexShader( struct NineDevice9 *This,
3153 IDirect3DVertexShader9 *pShader )
3154 {
3155 struct nine_state *state = This->update;
3156 struct nine_context *context = &This->context;
3157 BOOL was_programmable_vs = This->state.programmable_vs;
3158
3159 DBG("This=%p pShader=%p\n", This, pShader);
3160
3161 if (!This->is_recording && state->vs == (struct NineVertexShader9*)pShader)
3162 return D3D_OK;
3163
3164 nine_bind(&state->vs, pShader);
3165
3166 This->state.programmable_vs = This->state.vs && !(This->state.vdecl && This->state.vdecl->position_t);
3167
3168 /* ff -> non-ff: commit back non-ff constants */
3169 if (!was_programmable_vs && This->state.programmable_vs)
3170 context->commit |= NINE_STATE_COMMIT_CONST_VS;
3171
3172 state->changed.group |= NINE_STATE_VS;
3173
3174 return D3D_OK;
3175 }
3176
3177 HRESULT NINE_WINAPI
3178 NineDevice9_GetVertexShader( struct NineDevice9 *This,
3179 IDirect3DVertexShader9 **ppShader )
3180 {
3181 user_assert(ppShader, D3DERR_INVALIDCALL);
3182 nine_reference_set(ppShader, This->state.vs);
3183 return D3D_OK;
3184 }
3185
3186 HRESULT NINE_WINAPI
3187 NineDevice9_SetVertexShaderConstantF( struct NineDevice9 *This,
3188 UINT StartRegister,
3189 const float *pConstantData,
3190 UINT Vector4fCount )
3191 {
3192 struct nine_state *state = This->update;
3193 float *vs_const_f = This->may_swvp ? state->vs_const_f_swvp : state->vs_const_f;
3194
3195 DBG("This=%p StartRegister=%u pConstantData=%p Vector4fCount=%u\n",
3196 This, StartRegister, pConstantData, Vector4fCount);
3197
3198 user_assert(StartRegister < This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3199 user_assert(StartRegister + Vector4fCount <= This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3200
3201 if (!Vector4fCount)
3202 return D3D_OK;
3203 user_assert(pConstantData, D3DERR_INVALIDCALL);
3204
3205 if (!This->is_recording) {
3206 if (!memcmp(&vs_const_f[StartRegister * 4], pConstantData,
3207 Vector4fCount * 4 * sizeof(state->vs_const_f[0])))
3208 return D3D_OK;
3209 }
3210
3211 memcpy(&vs_const_f[StartRegister * 4],
3212 pConstantData,
3213 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3214
3215 nine_ranges_insert(&state->changed.vs_const_f,
3216 StartRegister, StartRegister + Vector4fCount,
3217 &This->range_pool);
3218
3219 if (This->may_swvp) {
3220 Vector4fCount = MIN2(StartRegister + Vector4fCount, NINE_MAX_CONST_F) - StartRegister;
3221 if (StartRegister < NINE_MAX_CONST_F)
3222 memcpy(&state->vs_const_f[StartRegister * 4],
3223 pConstantData,
3224 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3225 }
3226
3227 state->changed.group |= NINE_STATE_VS_CONST;
3228
3229 return D3D_OK;
3230 }
3231
3232 HRESULT NINE_WINAPI
3233 NineDevice9_GetVertexShaderConstantF( struct NineDevice9 *This,
3234 UINT StartRegister,
3235 float *pConstantData,
3236 UINT Vector4fCount )
3237 {
3238 const struct nine_state *state = &This->state;
3239 float *vs_const_f = This->may_swvp ? state->vs_const_f_swvp : state->vs_const_f;
3240
3241 user_assert(StartRegister < This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3242 user_assert(StartRegister + Vector4fCount <= This->caps.MaxVertexShaderConst, D3DERR_INVALIDCALL);
3243 user_assert(pConstantData, D3DERR_INVALIDCALL);
3244
3245 memcpy(pConstantData,
3246 &vs_const_f[StartRegister * 4],
3247 Vector4fCount * 4 * sizeof(state->vs_const_f[0]));
3248
3249 return D3D_OK;
3250 }
3251
3252 HRESULT NINE_WINAPI
3253 NineDevice9_SetVertexShaderConstantI( struct NineDevice9 *This,
3254 UINT StartRegister,
3255 const int *pConstantData,
3256 UINT Vector4iCount )
3257 {
3258 struct nine_state *state = This->update;
3259 int i;
3260
3261 DBG("This=%p StartRegister=%u pConstantData=%p Vector4iCount=%u\n",
3262 This, StartRegister, pConstantData, Vector4iCount);
3263
3264 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3265 D3DERR_INVALIDCALL);
3266 user_assert(StartRegister + Vector4iCount <= (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3267 D3DERR_INVALIDCALL);
3268 user_assert(pConstantData, D3DERR_INVALIDCALL);
3269
3270 if (This->driver_caps.vs_integer) {
3271 if (!This->is_recording) {
3272 if (!memcmp(&state->vs_const_i[4 * StartRegister], pConstantData,
3273 Vector4iCount * sizeof(int[4])))
3274 return D3D_OK;
3275 }
3276 memcpy(&state->vs_const_i[4 * StartRegister],
3277 pConstantData,
3278 Vector4iCount * sizeof(int[4]));
3279 } else {
3280 for (i = 0; i < Vector4iCount; i++) {
3281 state->vs_const_i[4 * (StartRegister + i)] = fui((float)(pConstantData[4 * i]));
3282 state->vs_const_i[4 * (StartRegister + i) + 1] = fui((float)(pConstantData[4 * i + 1]));
3283 state->vs_const_i[4 * (StartRegister + i) + 2] = fui((float)(pConstantData[4 * i + 2]));
3284 state->vs_const_i[4 * (StartRegister + i) + 3] = fui((float)(pConstantData[4 * i + 3]));
3285 }
3286 }
3287
3288 nine_ranges_insert(&state->changed.vs_const_i,
3289 StartRegister, StartRegister + Vector4iCount,
3290 &This->range_pool);
3291 state->changed.group |= NINE_STATE_VS_CONST;
3292
3293 return D3D_OK;
3294 }
3295
3296 HRESULT NINE_WINAPI
3297 NineDevice9_GetVertexShaderConstantI( struct NineDevice9 *This,
3298 UINT StartRegister,
3299 int *pConstantData,
3300 UINT Vector4iCount )
3301 {
3302 const struct nine_state *state = &This->state;
3303 int i;
3304
3305 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3306 D3DERR_INVALIDCALL);
3307 user_assert(StartRegister + Vector4iCount <= (This->may_swvp ? NINE_MAX_CONST_I_SWVP : NINE_MAX_CONST_I),
3308 D3DERR_INVALIDCALL);
3309 user_assert(pConstantData, D3DERR_INVALIDCALL);
3310
3311 if (This->driver_caps.vs_integer) {
3312 memcpy(pConstantData,
3313 &state->vs_const_i[4 * StartRegister],
3314 Vector4iCount * sizeof(int[4]));
3315 } else {
3316 for (i = 0; i < Vector4iCount; i++) {
3317 pConstantData[4 * i] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i)]);
3318 pConstantData[4 * i + 1] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 1]);
3319 pConstantData[4 * i + 2] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 2]);
3320 pConstantData[4 * i + 3] = (int32_t) uif(state->vs_const_i[4 * (StartRegister + i) + 3]);
3321 }
3322 }
3323
3324 return D3D_OK;
3325 }
3326
3327 HRESULT NINE_WINAPI
3328 NineDevice9_SetVertexShaderConstantB( struct NineDevice9 *This,
3329 UINT StartRegister,
3330 const BOOL *pConstantData,
3331 UINT BoolCount )
3332 {
3333 struct nine_state *state = This->update;
3334 int i;
3335 uint32_t bool_true = This->driver_caps.vs_integer ? 0xFFFFFFFF : fui(1.0f);
3336
3337 DBG("This=%p StartRegister=%u pConstantData=%p BoolCount=%u\n",
3338 This, StartRegister, pConstantData, BoolCount);
3339
3340 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3341 D3DERR_INVALIDCALL);
3342 user_assert(StartRegister + BoolCount <= (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3343 D3DERR_INVALIDCALL);
3344 user_assert(pConstantData, D3DERR_INVALIDCALL);
3345
3346 if (!This->is_recording) {
3347 bool noChange = true;
3348 for (i = 0; i < BoolCount; i++) {
3349 if (!!state->vs_const_b[StartRegister + i] != !!pConstantData[i])
3350 noChange = false;
3351 }
3352 if (noChange)
3353 return D3D_OK;
3354 }
3355
3356 for (i = 0; i < BoolCount; i++)
3357 state->vs_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
3358
3359 nine_ranges_insert(&state->changed.vs_const_b,
3360 StartRegister, StartRegister + BoolCount,
3361 &This->range_pool);
3362 state->changed.group |= NINE_STATE_VS_CONST;
3363
3364 return D3D_OK;
3365 }
3366
3367 HRESULT NINE_WINAPI
3368 NineDevice9_GetVertexShaderConstantB( struct NineDevice9 *This,
3369 UINT StartRegister,
3370 BOOL *pConstantData,
3371 UINT BoolCount )
3372 {
3373 const struct nine_state *state = &This->state;
3374 int i;
3375
3376 user_assert(StartRegister < (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3377 D3DERR_INVALIDCALL);
3378 user_assert(StartRegister + BoolCount <= (This->may_swvp ? NINE_MAX_CONST_B_SWVP : NINE_MAX_CONST_B),
3379 D3DERR_INVALIDCALL);
3380 user_assert(pConstantData, D3DERR_INVALIDCALL);
3381
3382 for (i = 0; i < BoolCount; i++)
3383 pConstantData[i] = state->vs_const_b[StartRegister + i] != 0 ? TRUE : FALSE;
3384
3385 return D3D_OK;
3386 }
3387
3388 HRESULT NINE_WINAPI
3389 NineDevice9_SetStreamSource( struct NineDevice9 *This,
3390 UINT StreamNumber,
3391 IDirect3DVertexBuffer9 *pStreamData,
3392 UINT OffsetInBytes,
3393 UINT Stride )
3394 {
3395 struct nine_state *state = This->update;
3396 struct NineVertexBuffer9 *pVBuf9 = NineVertexBuffer9(pStreamData);
3397 const unsigned i = StreamNumber;
3398
3399 DBG("This=%p StreamNumber=%u pStreamData=%p OffsetInBytes=%u Stride=%u\n",
3400 This, StreamNumber, pStreamData, OffsetInBytes, Stride);
3401
3402 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3403 user_assert(Stride <= This->caps.MaxStreamStride, D3DERR_INVALIDCALL);
3404
3405 if (likely(!This->is_recording)) {
3406 if (state->stream[i] == NineVertexBuffer9(pStreamData) &&
3407 state->vtxbuf[i].stride == Stride &&
3408 state->vtxbuf[i].buffer_offset == OffsetInBytes)
3409 return D3D_OK;
3410 }
3411 nine_bind(&state->stream[i], pStreamData);
3412
3413 state->changed.vtxbuf |= 1 << StreamNumber;
3414
3415 if (pStreamData) {
3416 state->vtxbuf[i].stride = Stride;
3417 state->vtxbuf[i].buffer_offset = OffsetInBytes;
3418 }
3419 pipe_resource_reference(&state->vtxbuf[i].buffer,
3420 pStreamData ? NineVertexBuffer9_GetResource(pVBuf9) : NULL);
3421
3422 return D3D_OK;
3423 }
3424
3425 HRESULT NINE_WINAPI
3426 NineDevice9_GetStreamSource( struct NineDevice9 *This,
3427 UINT StreamNumber,
3428 IDirect3DVertexBuffer9 **ppStreamData,
3429 UINT *pOffsetInBytes,
3430 UINT *pStride )
3431 {
3432 const struct nine_state *state = &This->state;
3433 const unsigned i = StreamNumber;
3434
3435 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3436 user_assert(ppStreamData, D3DERR_INVALIDCALL);
3437
3438 nine_reference_set(ppStreamData, state->stream[i]);
3439 *pStride = state->vtxbuf[i].stride;
3440 *pOffsetInBytes = state->vtxbuf[i].buffer_offset;
3441
3442 return D3D_OK;
3443 }
3444
3445 HRESULT NINE_WINAPI
3446 NineDevice9_SetStreamSourceFreq( struct NineDevice9 *This,
3447 UINT StreamNumber,
3448 UINT Setting )
3449 {
3450 struct nine_state *state = This->update;
3451 /* const UINT freq = Setting & 0x7FFFFF; */
3452
3453 DBG("This=%p StreamNumber=%u FrequencyParameter=0x%x\n", This,
3454 StreamNumber, Setting);
3455
3456 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3457 user_assert(StreamNumber != 0 || !(Setting & D3DSTREAMSOURCE_INSTANCEDATA),
3458 D3DERR_INVALIDCALL);
3459 user_assert(!((Setting & D3DSTREAMSOURCE_INSTANCEDATA) &&
3460 (Setting & D3DSTREAMSOURCE_INDEXEDDATA)), D3DERR_INVALIDCALL);
3461 user_assert(Setting, D3DERR_INVALIDCALL);
3462
3463 if (likely(!This->is_recording) && state->stream_freq[StreamNumber] == Setting)
3464 return D3D_OK;
3465
3466 state->stream_freq[StreamNumber] = Setting;
3467
3468 if (Setting & D3DSTREAMSOURCE_INSTANCEDATA)
3469 state->stream_instancedata_mask |= 1 << StreamNumber;
3470 else
3471 state->stream_instancedata_mask &= ~(1 << StreamNumber);
3472
3473 state->changed.stream_freq |= 1 << StreamNumber; /* Used for stateblocks */
3474 if (StreamNumber != 0)
3475 state->changed.group |= NINE_STATE_STREAMFREQ;
3476 return D3D_OK;
3477 }
3478
3479 HRESULT NINE_WINAPI
3480 NineDevice9_GetStreamSourceFreq( struct NineDevice9 *This,
3481 UINT StreamNumber,
3482 UINT *pSetting )
3483 {
3484 user_assert(StreamNumber < This->caps.MaxStreams, D3DERR_INVALIDCALL);
3485 *pSetting = This->state.stream_freq[StreamNumber];
3486 return D3D_OK;
3487 }
3488
3489 HRESULT NINE_WINAPI
3490 NineDevice9_SetIndices( struct NineDevice9 *This,
3491 IDirect3DIndexBuffer9 *pIndexData )
3492 {
3493 struct nine_state *state = This->update;
3494
3495 DBG("This=%p pIndexData=%p\n", This, pIndexData);
3496
3497 if (likely(!This->is_recording))
3498 if (state->idxbuf == NineIndexBuffer9(pIndexData))
3499 return D3D_OK;
3500 nine_bind(&state->idxbuf, pIndexData);
3501
3502 state->changed.group |= NINE_STATE_IDXBUF;
3503
3504 return D3D_OK;
3505 }
3506
3507 /* XXX: wine/d3d9 doesn't have pBaseVertexIndex, and it doesn't make sense
3508 * here because it's an argument passed to the Draw calls.
3509 */
3510 HRESULT NINE_WINAPI
3511 NineDevice9_GetIndices( struct NineDevice9 *This,
3512 IDirect3DIndexBuffer9 **ppIndexData)
3513 {
3514 user_assert(ppIndexData, D3DERR_INVALIDCALL);
3515 nine_reference_set(ppIndexData, This->state.idxbuf);
3516 return D3D_OK;
3517 }
3518
3519 HRESULT NINE_WINAPI
3520 NineDevice9_CreatePixelShader( struct NineDevice9 *This,
3521 const DWORD *pFunction,
3522 IDirect3DPixelShader9 **ppShader )
3523 {
3524 struct NinePixelShader9 *ps;
3525 HRESULT hr;
3526
3527 DBG("This=%p pFunction=%p ppShader=%p\n", This, pFunction, ppShader);
3528
3529 hr = NinePixelShader9_new(This, &ps, pFunction, NULL);
3530 if (FAILED(hr))
3531 return hr;
3532 *ppShader = (IDirect3DPixelShader9 *)ps;
3533 return D3D_OK;
3534 }
3535
3536 HRESULT NINE_WINAPI
3537 NineDevice9_SetPixelShader( struct NineDevice9 *This,
3538 IDirect3DPixelShader9 *pShader )
3539 {
3540 struct nine_state *state = This->update;
3541 struct nine_context *context = &This->context;
3542 unsigned old_mask = state->ps ? state->ps->rt_mask : 1;
3543 unsigned mask;
3544
3545 DBG("This=%p pShader=%p\n", This, pShader);
3546
3547 if (!This->is_recording && state->ps == (struct NinePixelShader9*)pShader)
3548 return D3D_OK;
3549
3550 /* ff -> non-ff: commit back non-ff constants */
3551 if (!state->ps && pShader)
3552 context->commit |= NINE_STATE_COMMIT_CONST_PS;
3553
3554 nine_bind(&state->ps, pShader);
3555
3556 state->changed.group |= NINE_STATE_PS;
3557
3558 mask = state->ps ? state->ps->rt_mask : 1;
3559 /* We need to update cbufs if the pixel shader would
3560 * write to different render targets */
3561 if (mask != old_mask)
3562 state->changed.group |= NINE_STATE_FB;
3563
3564 return D3D_OK;
3565 }
3566
3567 HRESULT NINE_WINAPI
3568 NineDevice9_GetPixelShader( struct NineDevice9 *This,
3569 IDirect3DPixelShader9 **ppShader )
3570 {
3571 user_assert(ppShader, D3DERR_INVALIDCALL);
3572 nine_reference_set(ppShader, This->state.ps);
3573 return D3D_OK;
3574 }
3575
3576 HRESULT NINE_WINAPI
3577 NineDevice9_SetPixelShaderConstantF( struct NineDevice9 *This,
3578 UINT StartRegister,
3579 const float *pConstantData,
3580 UINT Vector4fCount )
3581 {
3582 struct nine_state *state = This->update;
3583
3584 DBG("This=%p StartRegister=%u pConstantData=%p Vector4fCount=%u\n",
3585 This, StartRegister, pConstantData, Vector4fCount);
3586
3587 user_assert(StartRegister < NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3588 user_assert(StartRegister + Vector4fCount <= NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3589
3590 if (!Vector4fCount)
3591 return D3D_OK;
3592 user_assert(pConstantData, D3DERR_INVALIDCALL);
3593
3594 if (!This->is_recording) {
3595 if (!memcmp(&state->ps_const_f[StartRegister * 4], pConstantData,
3596 Vector4fCount * 4 * sizeof(state->ps_const_f[0])))
3597 return D3D_OK;
3598 }
3599
3600 memcpy(&state->ps_const_f[StartRegister * 4],
3601 pConstantData,
3602 Vector4fCount * 4 * sizeof(state->ps_const_f[0]));
3603
3604 nine_ranges_insert(&state->changed.ps_const_f,
3605 StartRegister, StartRegister + Vector4fCount,
3606 &This->range_pool);
3607
3608 state->changed.group |= NINE_STATE_PS_CONST;
3609
3610 return D3D_OK;
3611 }
3612
3613 HRESULT NINE_WINAPI
3614 NineDevice9_GetPixelShaderConstantF( struct NineDevice9 *This,
3615 UINT StartRegister,
3616 float *pConstantData,
3617 UINT Vector4fCount )
3618 {
3619 const struct nine_state *state = &This->state;
3620
3621 user_assert(StartRegister < NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3622 user_assert(StartRegister + Vector4fCount <= NINE_MAX_CONST_F_PS3, D3DERR_INVALIDCALL);
3623 user_assert(pConstantData, D3DERR_INVALIDCALL);
3624
3625 memcpy(pConstantData,
3626 &state->ps_const_f[StartRegister * 4],
3627 Vector4fCount * 4 * sizeof(state->ps_const_f[0]));
3628
3629 return D3D_OK;
3630 }
3631
3632 HRESULT NINE_WINAPI
3633 NineDevice9_SetPixelShaderConstantI( struct NineDevice9 *This,
3634 UINT StartRegister,
3635 const int *pConstantData,
3636 UINT Vector4iCount )
3637 {
3638 struct nine_state *state = This->update;
3639 int i;
3640
3641 DBG("This=%p StartRegister=%u pConstantData=%p Vector4iCount=%u\n",
3642 This, StartRegister, pConstantData, Vector4iCount);
3643
3644 user_assert(StartRegister < NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3645 user_assert(StartRegister + Vector4iCount <= NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3646 user_assert(pConstantData, D3DERR_INVALIDCALL);
3647
3648 if (This->driver_caps.ps_integer) {
3649 if (!This->is_recording) {
3650 if (!memcmp(&state->ps_const_i[StartRegister][0], pConstantData,
3651 Vector4iCount * sizeof(state->ps_const_i[0])))
3652 return D3D_OK;
3653 }
3654 memcpy(&state->ps_const_i[StartRegister][0],
3655 pConstantData,
3656 Vector4iCount * sizeof(state->ps_const_i[0]));
3657 } else {
3658 for (i = 0; i < Vector4iCount; i++) {
3659 state->ps_const_i[StartRegister+i][0] = fui((float)(pConstantData[4*i]));
3660 state->ps_const_i[StartRegister+i][1] = fui((float)(pConstantData[4*i+1]));
3661 state->ps_const_i[StartRegister+i][2] = fui((float)(pConstantData[4*i+2]));
3662 state->ps_const_i[StartRegister+i][3] = fui((float)(pConstantData[4*i+3]));
3663 }
3664 }
3665 state->changed.ps_const_i |= ((1 << Vector4iCount) - 1) << StartRegister;
3666 state->changed.group |= NINE_STATE_PS_CONST;
3667
3668 return D3D_OK;
3669 }
3670
3671 HRESULT NINE_WINAPI
3672 NineDevice9_GetPixelShaderConstantI( struct NineDevice9 *This,
3673 UINT StartRegister,
3674 int *pConstantData,
3675 UINT Vector4iCount )
3676 {
3677 const struct nine_state *state = &This->state;
3678 int i;
3679
3680 user_assert(StartRegister < NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3681 user_assert(StartRegister + Vector4iCount <= NINE_MAX_CONST_I, D3DERR_INVALIDCALL);
3682 user_assert(pConstantData, D3DERR_INVALIDCALL);
3683
3684 if (This->driver_caps.ps_integer) {
3685 memcpy(pConstantData,
3686 &state->ps_const_i[StartRegister][0],
3687 Vector4iCount * sizeof(state->ps_const_i[0]));
3688 } else {
3689 for (i = 0; i < Vector4iCount; i++) {
3690 pConstantData[4*i] = (int32_t) uif(state->ps_const_i[StartRegister+i][0]);
3691 pConstantData[4*i+1] = (int32_t) uif(state->ps_const_i[StartRegister+i][1]);
3692 pConstantData[4*i+2] = (int32_t) uif(state->ps_const_i[StartRegister+i][2]);
3693 pConstantData[4*i+3] = (int32_t) uif(state->ps_const_i[StartRegister+i][3]);
3694 }
3695 }
3696
3697 return D3D_OK;
3698 }
3699
3700 HRESULT NINE_WINAPI
3701 NineDevice9_SetPixelShaderConstantB( struct NineDevice9 *This,
3702 UINT StartRegister,
3703 const BOOL *pConstantData,
3704 UINT BoolCount )
3705 {
3706 struct nine_state *state = This->update;
3707 int i;
3708 uint32_t bool_true = This->driver_caps.ps_integer ? 0xFFFFFFFF : fui(1.0f);
3709
3710 DBG("This=%p StartRegister=%u pConstantData=%p BoolCount=%u\n",
3711 This, StartRegister, pConstantData, BoolCount);
3712
3713 user_assert(StartRegister < NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3714 user_assert(StartRegister + BoolCount <= NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3715 user_assert(pConstantData, D3DERR_INVALIDCALL);
3716
3717 if (!This->is_recording) {
3718 bool noChange = true;
3719 for (i = 0; i < BoolCount; i++) {
3720 if (!!state->ps_const_b[StartRegister + i] != !!pConstantData[i])
3721 noChange = false;
3722 }
3723 if (noChange)
3724 return D3D_OK;
3725 }
3726
3727 for (i = 0; i < BoolCount; i++)
3728 state->ps_const_b[StartRegister + i] = pConstantData[i] ? bool_true : 0;
3729
3730 state->changed.ps_const_b |= ((1 << BoolCount) - 1) << StartRegister;
3731 state->changed.group |= NINE_STATE_PS_CONST;
3732
3733 return D3D_OK;
3734 }
3735
3736 HRESULT NINE_WINAPI
3737 NineDevice9_GetPixelShaderConstantB( struct NineDevice9 *This,
3738 UINT StartRegister,
3739 BOOL *pConstantData,
3740 UINT BoolCount )
3741 {
3742 const struct nine_state *state = &This->state;
3743 int i;
3744
3745 user_assert(StartRegister < NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3746 user_assert(StartRegister + BoolCount <= NINE_MAX_CONST_B, D3DERR_INVALIDCALL);
3747 user_assert(pConstantData, D3DERR_INVALIDCALL);
3748
3749 for (i = 0; i < BoolCount; i++)
3750 pConstantData[i] = state->ps_const_b[StartRegister + i] ? TRUE : FALSE;
3751
3752 return D3D_OK;
3753 }
3754
3755 HRESULT NINE_WINAPI
3756 NineDevice9_DrawRectPatch( struct NineDevice9 *This,
3757 UINT Handle,
3758 const float *pNumSegs,
3759 const D3DRECTPATCH_INFO *pRectPatchInfo )
3760 {
3761 STUB(D3DERR_INVALIDCALL);
3762 }
3763
3764 HRESULT NINE_WINAPI
3765 NineDevice9_DrawTriPatch( struct NineDevice9 *This,
3766 UINT Handle,
3767 const float *pNumSegs,
3768 const D3DTRIPATCH_INFO *pTriPatchInfo )
3769 {
3770 STUB(D3DERR_INVALIDCALL);
3771 }
3772
3773 HRESULT NINE_WINAPI
3774 NineDevice9_DeletePatch( struct NineDevice9 *This,
3775 UINT Handle )
3776 {
3777 STUB(D3DERR_INVALIDCALL);
3778 }
3779
3780 HRESULT NINE_WINAPI
3781 NineDevice9_CreateQuery( struct NineDevice9 *This,
3782 D3DQUERYTYPE Type,
3783 IDirect3DQuery9 **ppQuery )
3784 {
3785 struct NineQuery9 *query;
3786 HRESULT hr;
3787
3788 DBG("This=%p Type=%d ppQuery=%p\n", This, Type, ppQuery);
3789
3790 hr = nine_is_query_supported(This->screen, Type);
3791 if (!ppQuery || hr != D3D_OK)
3792 return hr;
3793
3794 hr = NineQuery9_new(This, &query, Type);
3795 if (FAILED(hr))
3796 return hr;
3797 *ppQuery = (IDirect3DQuery9 *)query;
3798 return D3D_OK;
3799 }
3800
3801 IDirect3DDevice9Vtbl NineDevice9_vtable = {
3802 (void *)NineUnknown_QueryInterface,
3803 (void *)NineUnknown_AddRef,
3804 (void *)NineUnknown_Release,
3805 (void *)NineDevice9_TestCooperativeLevel,
3806 (void *)NineDevice9_GetAvailableTextureMem,
3807 (void *)NineDevice9_EvictManagedResources,
3808 (void *)NineDevice9_GetDirect3D,
3809 (void *)NineDevice9_GetDeviceCaps,
3810 (void *)NineDevice9_GetDisplayMode,
3811 (void *)NineDevice9_GetCreationParameters,
3812 (void *)NineDevice9_SetCursorProperties,
3813 (void *)NineDevice9_SetCursorPosition,
3814 (void *)NineDevice9_ShowCursor,
3815 (void *)NineDevice9_CreateAdditionalSwapChain,
3816 (void *)NineDevice9_GetSwapChain,
3817 (void *)NineDevice9_GetNumberOfSwapChains,
3818 (void *)NineDevice9_Reset,
3819 (void *)NineDevice9_Present,
3820 (void *)NineDevice9_GetBackBuffer,
3821 (void *)NineDevice9_GetRasterStatus,
3822 (void *)NineDevice9_SetDialogBoxMode,
3823 (void *)NineDevice9_SetGammaRamp,
3824 (void *)NineDevice9_GetGammaRamp,
3825 (void *)NineDevice9_CreateTexture,
3826 (void *)NineDevice9_CreateVolumeTexture,
3827 (void *)NineDevice9_CreateCubeTexture,
3828 (void *)NineDevice9_CreateVertexBuffer,
3829 (void *)NineDevice9_CreateIndexBuffer,
3830 (void *)NineDevice9_CreateRenderTarget,
3831 (void *)NineDevice9_CreateDepthStencilSurface,
3832 (void *)NineDevice9_UpdateSurface,
3833 (void *)NineDevice9_UpdateTexture,
3834 (void *)NineDevice9_GetRenderTargetData,
3835 (void *)NineDevice9_GetFrontBufferData,
3836 (void *)NineDevice9_StretchRect,
3837 (void *)NineDevice9_ColorFill,
3838 (void *)NineDevice9_CreateOffscreenPlainSurface,
3839 (void *)NineDevice9_SetRenderTarget,
3840 (void *)NineDevice9_GetRenderTarget,
3841 (void *)NineDevice9_SetDepthStencilSurface,
3842 (void *)NineDevice9_GetDepthStencilSurface,
3843 (void *)NineDevice9_BeginScene,
3844 (void *)NineDevice9_EndScene,
3845 (void *)NineDevice9_Clear,
3846 (void *)NineDevice9_SetTransform,
3847 (void *)NineDevice9_GetTransform,
3848 (void *)NineDevice9_MultiplyTransform,
3849 (void *)NineDevice9_SetViewport,
3850 (void *)NineDevice9_GetViewport,
3851 (void *)NineDevice9_SetMaterial,
3852 (void *)NineDevice9_GetMaterial,
3853 (void *)NineDevice9_SetLight,
3854 (void *)NineDevice9_GetLight,
3855 (void *)NineDevice9_LightEnable,
3856 (void *)NineDevice9_GetLightEnable,
3857 (void *)NineDevice9_SetClipPlane,
3858 (void *)NineDevice9_GetClipPlane,
3859 (void *)NineDevice9_SetRenderState,
3860 (void *)NineDevice9_GetRenderState,
3861 (void *)NineDevice9_CreateStateBlock,
3862 (void *)NineDevice9_BeginStateBlock,
3863 (void *)NineDevice9_EndStateBlock,
3864 (void *)NineDevice9_SetClipStatus,
3865 (void *)NineDevice9_GetClipStatus,
3866 (void *)NineDevice9_GetTexture,
3867 (void *)NineDevice9_SetTexture,
3868 (void *)NineDevice9_GetTextureStageState,
3869 (void *)NineDevice9_SetTextureStageState,
3870 (void *)NineDevice9_GetSamplerState,
3871 (void *)NineDevice9_SetSamplerState,
3872 (void *)NineDevice9_ValidateDevice,
3873 (void *)NineDevice9_SetPaletteEntries,
3874 (void *)NineDevice9_GetPaletteEntries,
3875 (void *)NineDevice9_SetCurrentTexturePalette,
3876 (void *)NineDevice9_GetCurrentTexturePalette,
3877 (void *)NineDevice9_SetScissorRect,
3878 (void *)NineDevice9_GetScissorRect,
3879 (void *)NineDevice9_SetSoftwareVertexProcessing,
3880 (void *)NineDevice9_GetSoftwareVertexProcessing,
3881 (void *)NineDevice9_SetNPatchMode,
3882 (void *)NineDevice9_GetNPatchMode,
3883 (void *)NineDevice9_DrawPrimitive,
3884 (void *)NineDevice9_DrawIndexedPrimitive,
3885 (void *)NineDevice9_DrawPrimitiveUP,
3886 (void *)NineDevice9_DrawIndexedPrimitiveUP,
3887 (void *)NineDevice9_ProcessVertices,
3888 (void *)NineDevice9_CreateVertexDeclaration,
3889 (void *)NineDevice9_SetVertexDeclaration,
3890 (void *)NineDevice9_GetVertexDeclaration,
3891 (void *)NineDevice9_SetFVF,
3892 (void *)NineDevice9_GetFVF,
3893 (void *)NineDevice9_CreateVertexShader,
3894 (void *)NineDevice9_SetVertexShader,
3895 (void *)NineDevice9_GetVertexShader,
3896 (void *)NineDevice9_SetVertexShaderConstantF,
3897 (void *)NineDevice9_GetVertexShaderConstantF,
3898 (void *)NineDevice9_SetVertexShaderConstantI,
3899 (void *)NineDevice9_GetVertexShaderConstantI,
3900 (void *)NineDevice9_SetVertexShaderConstantB,
3901 (void *)NineDevice9_GetVertexShaderConstantB,
3902 (void *)NineDevice9_SetStreamSource,
3903 (void *)NineDevice9_GetStreamSource,
3904 (void *)NineDevice9_SetStreamSourceFreq,
3905 (void *)NineDevice9_GetStreamSourceFreq,
3906 (void *)NineDevice9_SetIndices,
3907 (void *)NineDevice9_GetIndices,
3908 (void *)NineDevice9_CreatePixelShader,
3909 (void *)NineDevice9_SetPixelShader,
3910 (void *)NineDevice9_GetPixelShader,
3911 (void *)NineDevice9_SetPixelShaderConstantF,
3912 (void *)NineDevice9_GetPixelShaderConstantF,
3913 (void *)NineDevice9_SetPixelShaderConstantI,
3914 (void *)NineDevice9_GetPixelShaderConstantI,
3915 (void *)NineDevice9_SetPixelShaderConstantB,
3916 (void *)NineDevice9_GetPixelShaderConstantB,
3917 (void *)NineDevice9_DrawRectPatch,
3918 (void *)NineDevice9_DrawTriPatch,
3919 (void *)NineDevice9_DeletePatch,
3920 (void *)NineDevice9_CreateQuery
3921 };
3922
3923 static const GUID *NineDevice9_IIDs[] = {
3924 &IID_IDirect3DDevice9,
3925 &IID_IUnknown,
3926 NULL
3927 };
3928
3929 HRESULT
3930 NineDevice9_new( struct pipe_screen *pScreen,
3931 D3DDEVICE_CREATION_PARAMETERS *pCreationParameters,
3932 D3DCAPS9 *pCaps,
3933 D3DPRESENT_PARAMETERS *pPresentationParameters,
3934 IDirect3D9 *pD3D9,
3935 ID3DPresentGroup *pPresentationGroup,
3936 struct d3dadapter9_context *pCTX,
3937 boolean ex,
3938 D3DDISPLAYMODEEX *pFullscreenDisplayMode,
3939 struct NineDevice9 **ppOut,
3940 int minorVersionNum )
3941 {
3942 BOOL lock;
3943 lock = !!(pCreationParameters->BehaviorFlags & D3DCREATE_MULTITHREADED);
3944
3945 NINE_NEW(Device9, ppOut, lock, /* args */
3946 pScreen, pCreationParameters, pCaps,
3947 pPresentationParameters, pD3D9, pPresentationGroup, pCTX,
3948 ex, pFullscreenDisplayMode, minorVersionNum );
3949 }