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