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