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