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