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