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