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