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