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