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