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