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