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