• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "adapter9.h"
24 #include "device9ex.h"
25 #include "nine_helpers.h"
26 #include "nine_defines.h"
27 #include "nine_pipe.h"
28 #include "nine_dump.h"
29 #include "util/u_math.h"
30 #include "util/format/u_format.h"
31 #include "util/u_dump.h"
32 
33 #include "pipe/p_screen.h"
34 
35 #define DBG_CHANNEL DBG_ADAPTER
36 
37 static bool
has_sm3(struct pipe_screen * hal)38 has_sm3(struct pipe_screen *hal)
39 {
40     return hal->get_param(hal, PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD) &&
41            hal->get_param(hal, PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES);
42 }
43 
44 HRESULT
NineAdapter9_ctor(struct NineAdapter9 * This,struct NineUnknownParams * pParams,struct d3dadapter9_context * pCTX)45 NineAdapter9_ctor( struct NineAdapter9 *This,
46                    struct NineUnknownParams *pParams,
47                    struct d3dadapter9_context *pCTX )
48 {
49     struct pipe_screen *hal = pCTX->hal;
50     HRESULT hr = NineUnknown_ctor(&This->base, pParams);
51     if (FAILED(hr)) { return hr; }
52 
53     DBG("This=%p pParams=%p pCTX=%p\n", This, pParams, pCTX);
54     nine_dump_D3DADAPTER_IDENTIFIER9(DBG_CHANNEL, &pCTX->identifier);
55 
56     This->ctx = pCTX;
57     if (!hal->get_param(hal, PIPE_CAP_CLIP_HALFZ)) {
58         ERR("Driver doesn't support d3d9 coordinates\n");
59         return D3DERR_DRIVERINTERNALERROR;
60     }
61     if (This->ctx->ref &&
62         !This->ctx->ref->get_param(This->ctx->ref, PIPE_CAP_CLIP_HALFZ)) {
63         ERR("Warning: Sotware rendering driver doesn't support d3d9 coordinates\n");
64     }
65     /* Old cards had tricks to bypass some restrictions to implement
66      * everything and fit tight the requirements: number of constants,
67      * number of temp registers, special behaviours, etc. Since we don't
68      * have access to all this, we need a bit more than what dx9 required.
69      * For example we have to use more than 32 temp registers to emulate
70      * behaviours, while some dx9 hw don't have more. As for sm2 hardware,
71      * we could support vs2 / ps2 for them but it needs some more care, and
72      * as these are very old, we choose to drop support for them */
73 
74     /* checks minimum requirements, most are vs3/ps3 strict requirements */
75     if (!has_sm3(hal) ||
76         hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
77                               PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE) < 256 * sizeof(float[4]) ||
78         hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
79                               PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE) < 244 * sizeof(float[4]) ||
80         hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
81                               PIPE_SHADER_CAP_MAX_TEMPS) < 32 ||
82         hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
83                               PIPE_SHADER_CAP_MAX_TEMPS) < 32 ||
84         hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
85                               PIPE_SHADER_CAP_MAX_INPUTS) < 16 ||
86         hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
87                               PIPE_SHADER_CAP_MAX_INPUTS) < 10 ||
88         hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
89                               PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS) < 16) {
90         ERR("Your card is not supported by Gallium Nine. Minimum requirement "
91             "is >= r500, >= nv50, >= i965\n");
92         return D3DERR_DRIVERINTERNALERROR;
93     }
94     /* for r500 */
95     if (hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
96                               PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE) < 276 * sizeof(float[4]) || /* we put bool and int constants with float constants */
97         hal->get_shader_param(hal, PIPE_SHADER_VERTEX,
98                               PIPE_SHADER_CAP_MAX_TEMPS) < 40 || /* we use some more temp registers */
99         hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
100                               PIPE_SHADER_CAP_MAX_TEMPS) < 40 ||
101         hal->get_shader_param(hal, PIPE_SHADER_FRAGMENT,
102                               PIPE_SHADER_CAP_MAX_INPUTS) < 20) /* we don't pack inputs as much as we could */
103         ERR("Your card is at the limit of Gallium Nine requirements. Some games "
104             "may run into issues because requirements are too tight\n");
105     return D3D_OK;
106 }
107 
108 void
NineAdapter9_dtor(struct NineAdapter9 * This)109 NineAdapter9_dtor( struct NineAdapter9 *This )
110 {
111     struct d3dadapter9_context *ctx = This->ctx;
112 
113     DBG("This=%p\n", This);
114 
115     NineUnknown_dtor(&This->base);
116 
117     /* special case, call backend-specific dtor AFTER destroying this object
118      * completely. */
119     if (ctx) {
120         if (ctx->destroy) { ctx->destroy(ctx); }
121     }
122 }
123 
124 static HRESULT
NineAdapter9_GetScreen(struct NineAdapter9 * This,D3DDEVTYPE DevType,struct pipe_screen ** ppScreen)125 NineAdapter9_GetScreen( struct NineAdapter9 *This,
126                         D3DDEVTYPE DevType,
127                         struct pipe_screen **ppScreen )
128 {
129     const char *force_sw = getenv("D3D_ALWAYS_SOFTWARE");
130     switch (DevType) {
131         case D3DDEVTYPE_HAL:
132             if (force_sw && !strcmp(force_sw, "1") && This->ctx->ref) {
133                 *ppScreen = This->ctx->ref;
134                 break;
135             }
136             *ppScreen = This->ctx->hal;
137             break;
138 
139         case D3DDEVTYPE_REF:
140         case D3DDEVTYPE_NULLREF:
141         case D3DDEVTYPE_SW:
142             if (force_sw && !strcmp(force_sw, "0")) {
143                 *ppScreen = This->ctx->hal;
144                 break;
145             }
146             *ppScreen = This->ctx->ref;
147             break;
148 
149         default:
150             user_assert(!"Invalid device type", D3DERR_INVALIDCALL);
151     }
152 
153     if (!*ppScreen) { return D3DERR_NOTAVAILABLE; }
154 
155     return D3D_OK;
156 }
157 
158 HRESULT NINE_WINAPI
NineAdapter9_GetAdapterIdentifier(struct NineAdapter9 * This,DWORD Flags,D3DADAPTER_IDENTIFIER9 * pIdentifier)159 NineAdapter9_GetAdapterIdentifier( struct NineAdapter9 *This,
160                                    DWORD Flags,
161                                    D3DADAPTER_IDENTIFIER9 *pIdentifier )
162 {
163     DBG("This=%p Flags=%x pIdentifier=%p\n", This, Flags, pIdentifier);
164 
165     /* regarding flags, MSDN has this to say:
166      *  Flags sets the WHQLLevel member of D3DADAPTER_IDENTIFIER9. Flags can be
167      *  set to either 0 or D3DENUM_WHQL_LEVEL. If D3DENUM_WHQL_LEVEL is
168      *  specified, this call can connect to the Internet to download new
169      *  Microsoft Windows Hardware Quality Labs (WHQL) certificates.
170      * so let's just ignore it. */
171     *pIdentifier = This->ctx->identifier;
172     return D3D_OK;
173 }
174 
175 static inline boolean
backbuffer_format(D3DFORMAT dfmt,D3DFORMAT bfmt,boolean win)176 backbuffer_format( D3DFORMAT dfmt,
177                    D3DFORMAT bfmt,
178                    boolean win )
179 {
180     if (dfmt == D3DFMT_A2R10G10B10 && win) { return FALSE; }
181 
182     if ((dfmt == D3DFMT_A2R10G10B10 && bfmt == dfmt) ||
183         (dfmt == D3DFMT_X8R8G8B8 && (bfmt == dfmt ||
184                                      bfmt == D3DFMT_A8R8G8B8)) ||
185         (dfmt == D3DFMT_X1R5G5B5 && (bfmt == dfmt ||
186                                      bfmt == D3DFMT_A1R5G5B5)) ||
187         (dfmt == D3DFMT_R5G6B5 && bfmt == dfmt)) {
188         return TRUE;
189     }
190 
191     return FALSE;
192 }
193 
194 HRESULT NINE_WINAPI
NineAdapter9_CheckDeviceType(struct NineAdapter9 * This,D3DDEVTYPE DevType,D3DFORMAT AdapterFormat,D3DFORMAT BackBufferFormat,BOOL bWindowed)195 NineAdapter9_CheckDeviceType( struct NineAdapter9 *This,
196                               D3DDEVTYPE DevType,
197                               D3DFORMAT AdapterFormat,
198                               D3DFORMAT BackBufferFormat,
199                               BOOL bWindowed )
200 {
201     struct pipe_screen *screen;
202     enum pipe_format dfmt, bfmt;
203     HRESULT hr;
204 
205     DBG("This=%p DevType=%s AdapterFormat=%s BackBufferFormat=%s "
206         "bWindowed=%i\n", This, nine_D3DDEVTYPE_to_str(DevType),
207         d3dformat_to_string(AdapterFormat),
208         d3dformat_to_string(BackBufferFormat), bWindowed);
209 
210     user_assert(backbuffer_format(AdapterFormat, BackBufferFormat, bWindowed),
211                 D3DERR_NOTAVAILABLE);
212 
213     hr = NineAdapter9_GetScreen(This, DevType, &screen);
214     if (FAILED(hr)) { return hr; }
215 
216     /* The display format is not handled in Nine. We always present an XRGB8888
217      * buffer (and the display server will eventually do the conversion). We probably
218      * don't need to check for anything for the adapter format support, since if the
219      * display server advertise support, it will likely be able to do the conversion.
220      * We do the approximation that a format is available in the display server if
221      * the format passes with NINE_BIND_BACKBUFFER_FLAGS */
222     dfmt = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D,
223                                        1,
224                                        NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
225     bfmt = d3d9_to_pipe_format_checked(screen, BackBufferFormat, PIPE_TEXTURE_2D,
226                                        1,
227                                        NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
228     if (dfmt == PIPE_FORMAT_NONE || bfmt == PIPE_FORMAT_NONE) {
229         DBG("Unsupported Adapter/BackBufferFormat.\n");
230         return D3DERR_NOTAVAILABLE;
231     }
232 
233     return D3D_OK;
234 }
235 
236 static inline boolean
display_format(D3DFORMAT fmt,boolean win)237 display_format( D3DFORMAT fmt,
238                 boolean win )
239 {
240     /* http://msdn.microsoft.com/en-us/library/bb172558(v=VS.85).aspx#BackBuffer_or_Display_Formats */
241     static const D3DFORMAT allowed[] = {
242         D3DFMT_A2R10G10B10,
243         D3DFMT_X8R8G8B8,
244         D3DFMT_X1R5G5B5,
245         D3DFMT_R5G6B5,
246     };
247     unsigned i;
248 
249     if (fmt == D3DFMT_A2R10G10B10 && win) { return FALSE; }
250 
251     for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
252         if (fmt == allowed[i]) { return TRUE; }
253     }
254     return FALSE;
255 }
256 
257 static inline boolean
adapter_format(D3DFORMAT fmt)258 adapter_format( D3DFORMAT fmt )
259 {
260     /* Formats that are compatible to display_format (modulo alpha bits) */
261     static const D3DFORMAT allowed[] = {
262         D3DFMT_A2R10G10B10,
263         D3DFMT_X8R8G8B8,
264         D3DFMT_A8R8G8B8,
265         D3DFMT_X1R5G5B5,
266         D3DFMT_A1R5G5B5,
267         D3DFMT_R5G6B5,
268     };
269     unsigned i;
270 
271     for (i = 0; i < sizeof(allowed)/sizeof(D3DFORMAT); i++) {
272         if (fmt == allowed[i]) { return TRUE; }
273     }
274     return FALSE;
275 }
276 
277 HRESULT NINE_WINAPI
NineAdapter9_CheckDeviceFormat(struct NineAdapter9 * This,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat)278 NineAdapter9_CheckDeviceFormat( struct NineAdapter9 *This,
279                                 D3DDEVTYPE DeviceType,
280                                 D3DFORMAT AdapterFormat,
281                                 DWORD Usage,
282                                 D3DRESOURCETYPE RType,
283                                 D3DFORMAT CheckFormat )
284 {
285     struct pipe_screen *screen;
286     HRESULT hr;
287     enum pipe_format pf;
288     enum pipe_texture_target target;
289     unsigned bind = 0;
290     boolean srgb;
291 
292     /* Check adapter format. */
293 
294     DBG("This=%p DeviceType=%s AdapterFormat=%s\n", This,
295         nine_D3DDEVTYPE_to_str(DeviceType), d3dformat_to_string(AdapterFormat));
296     DBG("Usage=%x RType=%u CheckFormat=%s\n", Usage, RType,
297         d3dformat_to_string(CheckFormat));
298 
299     /* Wine tests, but suspicious. Needs more tests. */
300     user_assert(adapter_format(AdapterFormat), D3DERR_INVALIDCALL);
301     user_assert(display_format(AdapterFormat, FALSE), D3DERR_NOTAVAILABLE);
302 
303     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
304     if (FAILED(hr))
305         return hr;
306     pf = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D, 0,
307                                      PIPE_BIND_DISPLAY_TARGET |
308                                      PIPE_BIND_SHARED, FALSE, FALSE);
309     if (pf == PIPE_FORMAT_NONE) {
310         DBG("AdapterFormat %s not available.\n",
311             d3dformat_to_string(AdapterFormat));
312         return D3DERR_NOTAVAILABLE;
313     }
314 
315     /* Check actual format. */
316 
317     switch (RType) {
318     case D3DRTYPE_SURFACE:       target = PIPE_TEXTURE_2D; break;
319     case D3DRTYPE_TEXTURE:       target = PIPE_TEXTURE_2D; break;
320     case D3DRTYPE_CUBETEXTURE:   target = PIPE_TEXTURE_CUBE; break;
321     case D3DRTYPE_VOLUME:        target = PIPE_TEXTURE_3D; break;
322     case D3DRTYPE_VOLUMETEXTURE: target = PIPE_TEXTURE_3D; break;
323     case D3DRTYPE_VERTEXBUFFER:  target = PIPE_BUFFER; break;
324     case D3DRTYPE_INDEXBUFFER:   target = PIPE_BUFFER; break;
325     default:
326         user_assert(0, D3DERR_INVALIDCALL);
327     }
328 
329     bind = 0;
330     if (Usage & D3DUSAGE_RENDERTARGET) {
331         if (depth_stencil_format(CheckFormat))
332             return D3DERR_NOTAVAILABLE;
333         bind |= PIPE_BIND_RENDER_TARGET;
334     }
335     if (Usage & D3DUSAGE_DEPTHSTENCIL) {
336         if (!depth_stencil_format(CheckFormat))
337             return D3DERR_NOTAVAILABLE;
338         bind |= d3d9_get_pipe_depth_format_bindings(CheckFormat);
339     }
340 
341     if ((Usage & D3DUSAGE_QUERY_VERTEXTEXTURE) &&
342         !screen->get_shader_param(screen, PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS))
343         return D3DERR_NOTAVAILABLE;
344 
345     /* API hack because setting RT[0] to NULL is forbidden */
346     if (CheckFormat == D3DFMT_NULL && bind == PIPE_BIND_RENDER_TARGET &&
347         (RType == D3DRTYPE_SURFACE ||
348          RType == D3DRTYPE_TEXTURE))
349         return D3D_OK;
350 
351     /* RESZ hack */
352     if (CheckFormat == D3DFMT_RESZ && bind == PIPE_BIND_RENDER_TARGET &&
353         RType == D3DRTYPE_SURFACE)
354         return screen->get_param(screen, PIPE_CAP_MULTISAMPLE_Z_RESOLVE) ?
355                D3D_OK : D3DERR_NOTAVAILABLE;
356 
357     /* ATOC hack */
358     if (CheckFormat == D3DFMT_ATOC && RType == D3DRTYPE_SURFACE)
359         return D3D_OK;
360 
361     if ((Usage & D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING) &&
362         (Usage & D3DUSAGE_RENDERTARGET))
363         bind |= PIPE_BIND_BLENDABLE;
364 
365     if (Usage & D3DUSAGE_DMAP) {
366         DBG("D3DUSAGE_DMAP not available\n");
367         return D3DERR_NOTAVAILABLE; /* TODO: displacement mapping */
368     }
369 
370     switch (RType) {
371     case D3DRTYPE_TEXTURE:       bind |= PIPE_BIND_SAMPLER_VIEW; break;
372     case D3DRTYPE_CUBETEXTURE:   bind |= PIPE_BIND_SAMPLER_VIEW; break;
373     case D3DRTYPE_VOLUMETEXTURE: bind |= PIPE_BIND_SAMPLER_VIEW; break;
374     case D3DRTYPE_VERTEXBUFFER:  bind |= PIPE_BIND_VERTEX_BUFFER; break;
375     case D3DRTYPE_INDEXBUFFER:   bind |= PIPE_BIND_INDEX_BUFFER; break;
376     case D3DRTYPE_SURFACE:
377         if (!(Usage & D3DUSAGE_DEPTHSTENCIL))
378             bind |= PIPE_BIND_SAMPLER_VIEW; /* StretchRect */
379         /* Offscreen surface support: Usage = 0.
380          * In practice drivers are very restrictive on the formats supported.
381          * Basically a few common formats + YUV and compressed formats. The
382          * reason is that offscreen surface are useful only for directdraw
383          * compatibility (a WONTIMPL of nine) + format conversion (useful in
384          * particular for YUV because the format was not advertised for textures
385          * on NV chips). */
386         if (Usage == 0)
387             bind |= PIPE_BIND_RENDER_TARGET; /* A current requirement of our impl, which we should get rid of. */
388         break;
389     default:
390         break;
391     }
392 
393 
394     srgb = (Usage & (D3DUSAGE_QUERY_SRGBREAD | D3DUSAGE_QUERY_SRGBWRITE)) != 0;
395     pf = d3d9_to_pipe_format_checked(screen, CheckFormat, target,
396                                      0, bind, srgb, FALSE);
397     if (pf == PIPE_FORMAT_NONE) {
398         DBG("NOT AVAILABLE\n");
399         return D3DERR_NOTAVAILABLE;
400     }
401 
402     /* we support ATI1 and ATI2 hack only for 2D and Cube textures */
403     if (RType != D3DRTYPE_TEXTURE && RType != D3DRTYPE_CUBETEXTURE &&
404         (CheckFormat == D3DFMT_ATI1 || CheckFormat == D3DFMT_ATI2))
405         return D3DERR_NOTAVAILABLE;
406     /* if (Usage & D3DUSAGE_NONSECURE) { don't know the implications of this } */
407     /* if (Usage & D3DUSAGE_SOFTWAREPROCESSING) { we can always support this } */
408 
409     if ((Usage & D3DUSAGE_AUTOGENMIPMAP) && !(bind & PIPE_BIND_SAMPLER_VIEW))
410         return D3DOK_NOAUTOGEN;
411     return D3D_OK;
412 }
413 
414 HRESULT NINE_WINAPI
NineAdapter9_CheckDeviceMultiSampleType(struct NineAdapter9 * This,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD * pQualityLevels)415 NineAdapter9_CheckDeviceMultiSampleType( struct NineAdapter9 *This,
416                                          D3DDEVTYPE DeviceType,
417                                          D3DFORMAT SurfaceFormat,
418                                          BOOL Windowed,
419                                          D3DMULTISAMPLE_TYPE MultiSampleType,
420                                          DWORD *pQualityLevels )
421 {
422     struct pipe_screen *screen;
423     HRESULT hr;
424     enum pipe_format pf;
425     unsigned bind;
426 
427     DBG("This=%p DeviceType=%s SurfaceFormat=%s Windowed=%i MultiSampleType=%u "
428         "pQualityLevels=%p\n", This, nine_D3DDEVTYPE_to_str(DeviceType),
429         d3dformat_to_string(SurfaceFormat), Windowed, MultiSampleType,
430         pQualityLevels);
431 
432     if (pQualityLevels) {
433         /* In error cases return only 1 quality level supported */
434         *pQualityLevels = 1;
435     }
436     user_assert(MultiSampleType <= D3DMULTISAMPLE_16_SAMPLES, D3DERR_INVALIDCALL);
437 
438     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
439     if (FAILED(hr))
440         return hr;
441 
442     if (depth_stencil_format(SurfaceFormat))
443         bind = d3d9_get_pipe_depth_format_bindings(SurfaceFormat);
444     else /* render-target */
445         bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
446 
447     pf = d3d9_to_pipe_format_checked(screen, SurfaceFormat, PIPE_TEXTURE_2D,
448                                      0, PIPE_BIND_SAMPLER_VIEW, FALSE, FALSE);
449 
450     if (pf == PIPE_FORMAT_NONE && SurfaceFormat != D3DFMT_NULL) {
451         DBG("%s not available.\n", d3dformat_to_string(SurfaceFormat));
452         return D3DERR_INVALIDCALL;
453     }
454 
455     pf = d3d9_to_pipe_format_checked(screen, SurfaceFormat, PIPE_TEXTURE_2D,
456                                      MultiSampleType, bind, FALSE, FALSE);
457 
458     if (pf == PIPE_FORMAT_NONE && SurfaceFormat != D3DFMT_NULL) {
459         DBG("%s with %u samples not available.\n",
460             d3dformat_to_string(SurfaceFormat), MultiSampleType);
461         return D3DERR_NOTAVAILABLE;
462     }
463 
464     if (pQualityLevels) {
465         /* NONMASKABLE MultiSampleType might have more than one quality level,
466          * while MASKABLE MultiSampleTypes have only one level.
467          * Advertise quality levels and map each level to a sample count. */
468          (void ) d3dmultisample_type_check(screen, SurfaceFormat,
469                  &MultiSampleType, D3DMULTISAMPLE_16_SAMPLES, pQualityLevels);
470          DBG("advertising %u quality levels\n", *pQualityLevels);
471     }
472 
473     return D3D_OK;
474 }
475 
476 HRESULT NINE_WINAPI
NineAdapter9_CheckDepthStencilMatch(struct NineAdapter9 * This,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat)477 NineAdapter9_CheckDepthStencilMatch( struct NineAdapter9 *This,
478                                      D3DDEVTYPE DeviceType,
479                                      D3DFORMAT AdapterFormat,
480                                      D3DFORMAT RenderTargetFormat,
481                                      D3DFORMAT DepthStencilFormat )
482 {
483     struct pipe_screen *screen;
484     enum pipe_format dfmt, bfmt, zsfmt;
485     HRESULT hr;
486 
487     DBG("This=%p DeviceType=%s AdapterFormat=%s "
488         "RenderTargetFormat=%s DepthStencilFormat=%s\n", This,
489         nine_D3DDEVTYPE_to_str(DeviceType), d3dformat_to_string(AdapterFormat),
490         d3dformat_to_string(RenderTargetFormat),
491         d3dformat_to_string(DepthStencilFormat));
492 
493     /* TODO: does it check AdapterFormat at all ?
494      * It seems to need to pass at least for A8R8G8B8:
495      * https://github.com/iXit/Mesa-3D/issues/317 */
496     user_assert(adapter_format(AdapterFormat), D3DERR_NOTAVAILABLE);
497     user_assert(depth_stencil_format(DepthStencilFormat), D3DERR_NOTAVAILABLE);
498 
499     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
500     if (FAILED(hr)) { return hr; }
501 
502     dfmt = d3d9_to_pipe_format_checked(screen, AdapterFormat, PIPE_TEXTURE_2D, 0,
503                                        NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
504     bfmt = d3d9_to_pipe_format_checked(screen, RenderTargetFormat,
505                                        PIPE_TEXTURE_2D, 0,
506                                        NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
507     if (RenderTargetFormat == D3DFMT_NULL)
508         bfmt = dfmt;
509     zsfmt = d3d9_to_pipe_format_checked(screen, DepthStencilFormat,
510                                         PIPE_TEXTURE_2D, 0,
511                                         d3d9_get_pipe_depth_format_bindings(DepthStencilFormat),
512                                         FALSE, FALSE);
513     if (dfmt == PIPE_FORMAT_NONE ||
514         bfmt == PIPE_FORMAT_NONE ||
515         zsfmt == PIPE_FORMAT_NONE) {
516         return D3DERR_NOTAVAILABLE;
517     }
518 
519     return D3D_OK;
520 }
521 
522 HRESULT NINE_WINAPI
NineAdapter9_CheckDeviceFormatConversion(struct NineAdapter9 * This,D3DDEVTYPE DeviceType,D3DFORMAT SourceFormat,D3DFORMAT TargetFormat)523 NineAdapter9_CheckDeviceFormatConversion( struct NineAdapter9 *This,
524                                           D3DDEVTYPE DeviceType,
525                                           D3DFORMAT SourceFormat,
526                                           D3DFORMAT TargetFormat )
527 {
528     /* MSDN says this tests whether a certain backbuffer format can be used in
529      * conjunction with a certain front buffer format. It's a little confusing
530      * but some one wiser might be able to figure this one out. XXX */
531     struct pipe_screen *screen;
532     enum pipe_format dfmt, bfmt;
533     HRESULT hr;
534 
535     DBG("This=%p DeviceType=%s SourceFormat=%s TargetFormat=%s\n", This,
536         nine_D3DDEVTYPE_to_str(DeviceType),
537         d3dformat_to_string(SourceFormat), d3dformat_to_string(TargetFormat));
538 
539     user_assert(backbuffer_format(TargetFormat, SourceFormat, FALSE),
540                 D3DERR_NOTAVAILABLE);
541 
542     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
543     if (FAILED(hr)) { return hr; }
544 
545     dfmt = d3d9_to_pipe_format_checked(screen, TargetFormat, PIPE_TEXTURE_2D, 1,
546                                        NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
547     bfmt = d3d9_to_pipe_format_checked(screen, SourceFormat, PIPE_TEXTURE_2D, 1,
548                                        NINE_BIND_BACKBUFFER_FLAGS, FALSE, FALSE);
549 
550     if (dfmt == PIPE_FORMAT_NONE || bfmt == PIPE_FORMAT_NONE) {
551         DBG("%s to %s not supported.\n",
552             d3dformat_to_string(SourceFormat),
553             d3dformat_to_string(TargetFormat));
554         return D3DERR_NOTAVAILABLE;
555     }
556 
557     return D3D_OK;
558 }
559 
560 HRESULT NINE_WINAPI
NineAdapter9_GetDeviceCaps(struct NineAdapter9 * This,D3DDEVTYPE DeviceType,D3DCAPS9 * pCaps)561 NineAdapter9_GetDeviceCaps( struct NineAdapter9 *This,
562                             D3DDEVTYPE DeviceType,
563                             D3DCAPS9 *pCaps )
564 {
565     struct pipe_screen *screen;
566     HRESULT hr;
567 
568     DBG("This=%p DeviceType=%s pCaps=%p\n", This,
569         nine_D3DDEVTYPE_to_str(DeviceType), pCaps);
570 
571     user_assert(pCaps, D3DERR_INVALIDCALL);
572 
573     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
574     if (FAILED(hr)) {
575        DBG("Failed to get pipe_screen.\n");
576        return hr;
577     }
578 
579 #define D3DPIPECAP(pcap, d3dcap) \
580     (screen->get_param(screen, PIPE_CAP_##pcap) ? (d3dcap) : 0)
581 
582 #define D3DNPIPECAP(pcap, d3dcap) \
583     (screen->get_param(screen, PIPE_CAP_##pcap) ? 0 : (d3dcap))
584 
585     pCaps->DeviceType = DeviceType;
586 
587     pCaps->AdapterOrdinal = 0;
588 
589     pCaps->Caps = D3DCAPS_READ_SCANLINE;
590 
591     pCaps->Caps2 = /* D3DCAPS2_CANMANAGERESOURCE | */
592                 /* D3DCAPS2_CANSHARERESOURCE | */
593                 /* D3DCAPS2_CANCALIBRATEGAMMA | */
594                    D3DCAPS2_DYNAMICTEXTURES |
595                    D3DCAPS2_FULLSCREENGAMMA |
596                    D3DCAPS2_CANAUTOGENMIPMAP;
597 
598     /* Note: D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD just means the
599      * backbuffer can be ARGB (instead of only XRGB) when we are fullscreen
600      * and in discard mode. */
601     pCaps->Caps3 = D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD |
602                    D3DCAPS3_COPY_TO_VIDMEM |
603                    D3DCAPS3_COPY_TO_SYSTEMMEM |
604                    D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION;
605 
606     pCaps->PresentationIntervals = D3DPRESENT_INTERVAL_DEFAULT |
607                                    D3DPRESENT_INTERVAL_ONE |
608                                    D3DPRESENT_INTERVAL_TWO |
609                                    D3DPRESENT_INTERVAL_THREE |
610                                    D3DPRESENT_INTERVAL_FOUR |
611                                    D3DPRESENT_INTERVAL_IMMEDIATE;
612     pCaps->CursorCaps = D3DCURSORCAPS_COLOR /* | D3DCURSORCAPS_LOWRES*/;
613 
614     pCaps->DevCaps = D3DDEVCAPS_CANBLTSYSTONONLOCAL |
615                      D3DDEVCAPS_CANRENDERAFTERFLIP |
616                      D3DDEVCAPS_DRAWPRIMITIVES2 |
617                      D3DDEVCAPS_DRAWPRIMITIVES2EX |
618                      D3DDEVCAPS_DRAWPRIMTLVERTEX |
619                      D3DDEVCAPS_EXECUTESYSTEMMEMORY |
620                      D3DDEVCAPS_EXECUTEVIDEOMEMORY |
621                      D3DDEVCAPS_HWRASTERIZATION |
622                      D3DDEVCAPS_HWTRANSFORMANDLIGHT |
623                      /*D3DDEVCAPS_NPATCHES |*/
624                      D3DDEVCAPS_PUREDEVICE |
625                      /*D3DDEVCAPS_QUINTICRTPATCHES |*/
626                      /*D3DDEVCAPS_RTPATCHES |*/
627                      /*D3DDEVCAPS_RTPATCHHANDLEZERO |*/
628                      /*D3DDEVCAPS_SEPARATETEXTUREMEMORIES |*/
629                      D3DDEVCAPS_TEXTURENONLOCALVIDMEM |
630                      /* D3DDEVCAPS_TEXTURESYSTEMMEMORY |*/
631                      D3DDEVCAPS_TEXTUREVIDEOMEMORY |
632                      D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
633                      D3DDEVCAPS_TLVERTEXVIDEOMEMORY;
634 
635     pCaps->PrimitiveMiscCaps = D3DPMISCCAPS_MASKZ |
636                                D3DPMISCCAPS_CULLNONE | /* XXX */
637                                D3DPMISCCAPS_CULLCW |
638                                D3DPMISCCAPS_CULLCCW |
639                                D3DPMISCCAPS_COLORWRITEENABLE |
640                                D3DPMISCCAPS_CLIPPLANESCALEDPOINTS |
641                                /*D3DPMISCCAPS_CLIPTLVERTS |*/
642                                D3DPMISCCAPS_TSSARGTEMP |
643                                D3DPMISCCAPS_BLENDOP |
644                                D3DPIPECAP(INDEP_BLEND_ENABLE, D3DPMISCCAPS_INDEPENDENTWRITEMASKS) |
645                                D3DPMISCCAPS_PERSTAGECONSTANT |
646                                /*D3DPMISCCAPS_POSTBLENDSRGBCONVERT |*/ /* TODO: advertise if Ex and dx10 able card */
647                                D3DPMISCCAPS_FOGANDSPECULARALPHA | /* Note: documentation of the flag is wrong */
648                                D3DPIPECAP(BLEND_EQUATION_SEPARATE, D3DPMISCCAPS_SEPARATEALPHABLEND) |
649                                D3DPIPECAP(MIXED_COLORBUFFER_FORMATS, D3DPMISCCAPS_MRTINDEPENDENTBITDEPTHS) |
650                                D3DPMISCCAPS_MRTPOSTPIXELSHADERBLENDING |
651                                D3DPMISCCAPS_FOGVERTEXCLAMPED;
652     if (!screen->get_param(screen, PIPE_CAP_VS_WINDOW_SPACE_POSITION))
653         pCaps->PrimitiveMiscCaps |= D3DPMISCCAPS_CLIPTLVERTS;
654 
655     pCaps->RasterCaps =
656         D3DPIPECAP(ANISOTROPIC_FILTER, D3DPRASTERCAPS_ANISOTROPY) |
657         D3DPRASTERCAPS_COLORPERSPECTIVE |
658         D3DPRASTERCAPS_DITHER |
659         D3DPRASTERCAPS_DEPTHBIAS |
660         D3DPRASTERCAPS_FOGRANGE |
661         D3DPRASTERCAPS_FOGTABLE |
662         D3DPRASTERCAPS_FOGVERTEX |
663         D3DPRASTERCAPS_MIPMAPLODBIAS |
664         D3DPRASTERCAPS_MULTISAMPLE_TOGGLE |
665         D3DPRASTERCAPS_SCISSORTEST |
666         D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
667         /*D3DPRASTERCAPS_WBUFFER |*/
668         D3DPRASTERCAPS_WFOG |
669         /*D3DPRASTERCAPS_ZBUFFERLESSHSR |*/
670         D3DPRASTERCAPS_ZFOG |
671         D3DPRASTERCAPS_ZTEST;
672 
673     pCaps->ZCmpCaps = D3DPCMPCAPS_NEVER |
674                       D3DPCMPCAPS_LESS |
675                       D3DPCMPCAPS_EQUAL |
676                       D3DPCMPCAPS_LESSEQUAL |
677                       D3DPCMPCAPS_GREATER |
678                       D3DPCMPCAPS_NOTEQUAL |
679                       D3DPCMPCAPS_GREATEREQUAL |
680                       D3DPCMPCAPS_ALWAYS;
681 
682     pCaps->SrcBlendCaps = D3DPBLENDCAPS_ZERO |
683                           D3DPBLENDCAPS_ONE |
684                           D3DPBLENDCAPS_SRCCOLOR |
685                           D3DPBLENDCAPS_INVSRCCOLOR |
686                           D3DPBLENDCAPS_SRCALPHA |
687                           D3DPBLENDCAPS_INVSRCALPHA |
688                           D3DPBLENDCAPS_DESTALPHA |
689                           D3DPBLENDCAPS_INVDESTALPHA |
690                           D3DPBLENDCAPS_DESTCOLOR |
691                           D3DPBLENDCAPS_INVDESTCOLOR |
692                           D3DPBLENDCAPS_SRCALPHASAT |
693                           D3DPBLENDCAPS_BOTHSRCALPHA |
694                           D3DPBLENDCAPS_BOTHINVSRCALPHA |
695                           D3DPBLENDCAPS_BLENDFACTOR |
696                           D3DPIPECAP(MAX_DUAL_SOURCE_RENDER_TARGETS,
697                               D3DPBLENDCAPS_INVSRCCOLOR2 |
698                               D3DPBLENDCAPS_SRCCOLOR2);
699 
700     pCaps->DestBlendCaps = pCaps->SrcBlendCaps;
701 
702     pCaps->AlphaCmpCaps = D3DPCMPCAPS_NEVER |
703                           D3DPCMPCAPS_LESS |
704                           D3DPCMPCAPS_EQUAL |
705                           D3DPCMPCAPS_LESSEQUAL |
706                           D3DPCMPCAPS_GREATER |
707                           D3DPCMPCAPS_NOTEQUAL |
708                           D3DPCMPCAPS_GREATEREQUAL |
709                           D3DPCMPCAPS_ALWAYS;
710 
711     /* FLAT caps not legal for D3D9. */
712     pCaps->ShadeCaps = D3DPSHADECAPS_COLORGOURAUDRGB |
713                        D3DPSHADECAPS_SPECULARGOURAUDRGB |
714                        D3DPSHADECAPS_ALPHAGOURAUDBLEND |
715                        D3DPSHADECAPS_FOGGOURAUD;
716 
717     pCaps->TextureCaps =
718         D3DPTEXTURECAPS_ALPHA |
719         D3DPTEXTURECAPS_ALPHAPALETTE |
720         D3DPTEXTURECAPS_PERSPECTIVE |
721         D3DPTEXTURECAPS_PROJECTED |
722         D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
723         D3DPTEXTURECAPS_CUBEMAP |
724         D3DPTEXTURECAPS_VOLUMEMAP |
725         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_POW2) |
726         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_NONPOW2CONDITIONAL) |
727         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_CUBEMAP_POW2) |
728         D3DNPIPECAP(NPOT_TEXTURES, D3DPTEXTURECAPS_VOLUMEMAP_POW2) |
729         D3DPIPECAP(MAX_TEXTURE_2D_SIZE, D3DPTEXTURECAPS_MIPMAP) |
730         D3DPIPECAP(MAX_TEXTURE_3D_LEVELS, D3DPTEXTURECAPS_MIPVOLUMEMAP) |
731         D3DPIPECAP(MAX_TEXTURE_CUBE_LEVELS, D3DPTEXTURECAPS_MIPCUBEMAP);
732 
733     pCaps->TextureFilterCaps =
734         D3DPTFILTERCAPS_MINFPOINT |
735         D3DPTFILTERCAPS_MINFLINEAR |
736         D3DPIPECAP(ANISOTROPIC_FILTER, D3DPTFILTERCAPS_MINFANISOTROPIC) |
737         /*D3DPTFILTERCAPS_MINFPYRAMIDALQUAD |*/
738         /*D3DPTFILTERCAPS_MINFGAUSSIANQUAD |*/
739         D3DPTFILTERCAPS_MIPFPOINT |
740         D3DPTFILTERCAPS_MIPFLINEAR |
741         D3DPTFILTERCAPS_MAGFPOINT |
742         D3DPTFILTERCAPS_MAGFLINEAR |
743         D3DPIPECAP(ANISOTROPIC_FILTER, D3DPTFILTERCAPS_MAGFANISOTROPIC) |
744         /*D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD |*/
745         /*D3DPTFILTERCAPS_MAGFGAUSSIANQUAD*/0;
746 
747     pCaps->CubeTextureFilterCaps = pCaps->TextureFilterCaps;
748     pCaps->VolumeTextureFilterCaps = pCaps->TextureFilterCaps;
749 
750     pCaps->TextureAddressCaps =
751         D3DPTADDRESSCAPS_BORDER |
752         D3DPTADDRESSCAPS_INDEPENDENTUV |
753         D3DPTADDRESSCAPS_WRAP |
754         D3DPTADDRESSCAPS_MIRROR |
755         D3DPTADDRESSCAPS_CLAMP |
756         D3DPIPECAP(TEXTURE_MIRROR_CLAMP, D3DPTADDRESSCAPS_MIRRORONCE);
757 
758     pCaps->VolumeTextureAddressCaps = pCaps->TextureAddressCaps;
759 
760     pCaps->LineCaps =
761         D3DLINECAPS_ALPHACMP |
762         D3DLINECAPS_BLEND |
763         D3DLINECAPS_TEXTURE |
764         D3DLINECAPS_ZTEST |
765         D3DLINECAPS_FOG;
766     if (screen->get_paramf(screen, PIPE_CAPF_MAX_LINE_WIDTH_AA) > 0.0) {
767         pCaps->LineCaps |= D3DLINECAPS_ANTIALIAS;
768     }
769 
770     pCaps->MaxTextureWidth =screen->get_param(screen,
771                                               PIPE_CAP_MAX_TEXTURE_2D_SIZE);
772     pCaps->MaxTextureHeight = pCaps->MaxTextureWidth;
773     pCaps->MaxVolumeExtent =
774         1 << (screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_3D_LEVELS) - 1);
775     /* XXX values from wine */
776     pCaps->MaxTextureRepeat = 32768;
777     pCaps->MaxTextureAspectRatio = pCaps->MaxTextureWidth;
778 
779     pCaps->MaxAnisotropy =
780         (DWORD)screen->get_paramf(screen, PIPE_CAPF_MAX_TEXTURE_ANISOTROPY);
781 
782     /* Values for GeForce 9600 GT */
783     pCaps->MaxVertexW = 1e10f;
784     pCaps->GuardBandLeft = -1e9f;
785     pCaps->GuardBandTop = -1e9f;
786     pCaps->GuardBandRight = 1e9f;
787     pCaps->GuardBandBottom = 1e9f;
788     pCaps->ExtentsAdjust = 0.0f;
789 
790     pCaps->StencilCaps =
791         D3DSTENCILCAPS_KEEP |
792         D3DSTENCILCAPS_ZERO |
793         D3DSTENCILCAPS_REPLACE |
794         D3DSTENCILCAPS_INCRSAT |
795         D3DSTENCILCAPS_DECRSAT |
796         D3DSTENCILCAPS_INVERT |
797         D3DSTENCILCAPS_INCR |
798         D3DSTENCILCAPS_DECR |
799         D3DSTENCILCAPS_TWOSIDED;
800 
801     pCaps->FVFCaps =
802         8 | /* 8 textures max */
803         /*D3DFVFCAPS_DONOTSTRIPELEMENTS |*/
804         D3DFVFCAPS_PSIZE;
805 
806     pCaps->TextureOpCaps = D3DTEXOPCAPS_DISABLE |
807                            D3DTEXOPCAPS_SELECTARG1 |
808                            D3DTEXOPCAPS_SELECTARG2 |
809                            D3DTEXOPCAPS_MODULATE |
810                            D3DTEXOPCAPS_MODULATE2X |
811                            D3DTEXOPCAPS_MODULATE4X |
812                            D3DTEXOPCAPS_ADD |
813                            D3DTEXOPCAPS_ADDSIGNED |
814                            D3DTEXOPCAPS_ADDSIGNED2X |
815                            D3DTEXOPCAPS_SUBTRACT |
816                            D3DTEXOPCAPS_ADDSMOOTH |
817                            D3DTEXOPCAPS_BLENDDIFFUSEALPHA |
818                            D3DTEXOPCAPS_BLENDTEXTUREALPHA |
819                            D3DTEXOPCAPS_BLENDFACTORALPHA |
820                            D3DTEXOPCAPS_BLENDTEXTUREALPHAPM |
821                            D3DTEXOPCAPS_BLENDCURRENTALPHA |
822                            D3DTEXOPCAPS_PREMODULATE |
823                            D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR |
824                            D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
825                            D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR |
826                            D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA |
827                            D3DTEXOPCAPS_BUMPENVMAP |
828                            D3DTEXOPCAPS_BUMPENVMAPLUMINANCE |
829                            D3DTEXOPCAPS_DOTPRODUCT3 |
830                            D3DTEXOPCAPS_MULTIPLYADD |
831                            D3DTEXOPCAPS_LERP;
832 
833     pCaps->MaxTextureBlendStages = 8; /* XXX wine */
834     pCaps->MaxSimultaneousTextures = 8;
835 
836     pCaps->VertexProcessingCaps = D3DVTXPCAPS_TEXGEN |
837                                   D3DVTXPCAPS_TEXGEN_SPHEREMAP |
838                                   D3DVTXPCAPS_MATERIALSOURCE7 |
839                                   D3DVTXPCAPS_DIRECTIONALLIGHTS |
840                                   D3DVTXPCAPS_POSITIONALLIGHTS |
841                                   D3DVTXPCAPS_LOCALVIEWER |
842                                   D3DVTXPCAPS_TWEENING |
843                                   /*D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER*/0;
844 
845     pCaps->MaxActiveLights = NINE_MAX_LIGHTS_ACTIVE; /* like GL_LIGHTi */
846     pCaps->MaxUserClipPlanes = PIPE_MAX_CLIP_PLANES;
847     pCaps->MaxVertexBlendMatrices = 4; /* 1 vec4 BLENDWEIGHT/INDICES input */
848     pCaps->MaxVertexBlendMatrixIndex = 8; /* D3DTS_WORLDMATRIX(0..8) */
849 
850     pCaps->MaxPointSize = screen->get_paramf(screen, PIPE_CAPF_MAX_POINT_SIZE);
851 
852     pCaps->MaxPrimitiveCount = 0x555555; /* <- wine, really 0xFFFFFFFF; */
853     pCaps->MaxVertexIndex = 0xFFFFFF; /* <- wine, really 0xFFFFFFFF */
854     pCaps->MaxStreams =
855         _min(screen->get_shader_param(screen,
856                  PIPE_SHADER_VERTEX, PIPE_SHADER_CAP_MAX_INPUTS),
857              16);
858 
859     pCaps->MaxStreamStride = screen->get_param(screen,
860             PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE);
861 
862     pCaps->VertexShaderVersion = D3DVS_VERSION(3,0);
863 
864     /* VS 2 as well as 3.0 supports a minimum of 256 consts.
865      * Wine and d3d9 drivers for dx1x hw advertise 256. Just as them,
866      * advertise 256. Problem is with hw that can only do 256, because
867      * we need take a few slots for boolean and integer constants. For these
868      * we'll have to fail later if they use complex shaders. */
869     pCaps->MaxVertexShaderConst = NINE_MAX_CONST_F;
870 
871     pCaps->PixelShaderVersion = D3DPS_VERSION(3,0);
872     /* Value for GeForce 9600 GT */
873     pCaps->PixelShader1xMaxValue = 65504.f;
874 
875     pCaps->DevCaps2 = D3DDEVCAPS2_STREAMOFFSET |
876                       D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET |
877                       D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
878                       /*D3DDEVCAPS2_DMAPNPATCH |*/
879                       /*D3DDEVCAPS2_ADAPTIVETESSRTPATCH |*/
880                       /*D3DDEVCAPS2_ADAPTIVETESSNPATCH |*/
881                       /*D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH*/0;
882 
883     pCaps->MasterAdapterOrdinal = 0;
884     pCaps->AdapterOrdinalInGroup = 0;
885     pCaps->NumberOfAdaptersInGroup = 1;
886 
887     /* Undocumented ? */
888     pCaps->MaxNpatchTessellationLevel = 0.0f;
889     pCaps->Reserved5 = 0;
890 
891     /* XXX: use is_format_supported */
892     pCaps->DeclTypes = D3DDTCAPS_UBYTE4 |
893                        D3DDTCAPS_UBYTE4N |
894                        D3DDTCAPS_SHORT2N |
895                        D3DDTCAPS_SHORT4N |
896                        D3DDTCAPS_USHORT2N |
897                        D3DDTCAPS_USHORT4N |
898                        D3DDTCAPS_UDEC3 |
899                        D3DDTCAPS_DEC3N |
900                        D3DDTCAPS_FLOAT16_2 |
901                        D3DDTCAPS_FLOAT16_4;
902 
903     pCaps->NumSimultaneousRTs =
904         screen->get_param(screen, PIPE_CAP_MAX_RENDER_TARGETS);
905     if (pCaps->NumSimultaneousRTs > NINE_MAX_SIMULTANEOUS_RENDERTARGETS)
906         pCaps->NumSimultaneousRTs = NINE_MAX_SIMULTANEOUS_RENDERTARGETS;
907 
908     pCaps->StretchRectFilterCaps = D3DPTFILTERCAPS_MINFPOINT |
909                                    D3DPTFILTERCAPS_MINFLINEAR |
910                                    D3DPTFILTERCAPS_MAGFPOINT |
911                                    D3DPTFILTERCAPS_MAGFLINEAR;
912 
913 
914     pCaps->VS20Caps.Caps = D3DVS20CAPS_PREDICATION;
915     pCaps->VS20Caps.DynamicFlowControlDepth = /* XXX is this dynamic ? */
916         screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
917                                  PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
918     pCaps->VS20Caps.NumTemps =
919         screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
920                                  PIPE_SHADER_CAP_MAX_TEMPS);
921     pCaps->VS20Caps.StaticFlowControlDepth = /* XXX is this static ? */
922         screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
923                                  PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
924 
925     /* also check for values < 0, because get_shader_param may return unsigned */
926     if (pCaps->VS20Caps.DynamicFlowControlDepth > D3DVS20_MAX_DYNAMICFLOWCONTROLDEPTH
927         || pCaps->VS20Caps.DynamicFlowControlDepth < 0)
928         pCaps->VS20Caps.DynamicFlowControlDepth = D3DVS20_MAX_DYNAMICFLOWCONTROLDEPTH;
929     if (pCaps->VS20Caps.StaticFlowControlDepth > D3DVS20_MAX_STATICFLOWCONTROLDEPTH
930         || pCaps->VS20Caps.StaticFlowControlDepth < 0)
931         pCaps->VS20Caps.StaticFlowControlDepth = D3DVS20_MAX_STATICFLOWCONTROLDEPTH;
932     if (pCaps->VS20Caps.NumTemps > D3DVS20_MAX_NUMTEMPS)
933         pCaps->VS20Caps.NumTemps = D3DVS20_MAX_NUMTEMPS;
934     assert(pCaps->VS20Caps.DynamicFlowControlDepth >= D3DVS20_MIN_DYNAMICFLOWCONTROLDEPTH);
935     assert(pCaps->VS20Caps.StaticFlowControlDepth >= D3DVS20_MIN_STATICFLOWCONTROLDEPTH);
936     assert(pCaps->VS20Caps.NumTemps >= D3DVS20_MIN_NUMTEMPS);
937 
938 
939     pCaps->PS20Caps.Caps = D3DPS20CAPS_ARBITRARYSWIZZLE |
940                            D3DPS20CAPS_GRADIENTINSTRUCTIONS |
941                            D3DPS20CAPS_PREDICATION;
942     if (screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
943                                  PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS) ==
944         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
945                                  PIPE_SHADER_CAP_MAX_INSTRUCTIONS))
946         pCaps->PS20Caps.Caps |= D3DPS20CAPS_NOTEXINSTRUCTIONLIMIT;
947     if (screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
948                                  PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS) ==
949         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
950                                  PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS))
951         pCaps->PS20Caps.Caps |= D3DPS20CAPS_NODEPENDENTREADLIMIT;
952     pCaps->PS20Caps.DynamicFlowControlDepth = /* XXX is this dynamic ? */
953         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
954                                  PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
955     pCaps->PS20Caps.NumTemps =
956         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
957                                  PIPE_SHADER_CAP_MAX_TEMPS);
958     pCaps->PS20Caps.StaticFlowControlDepth =  /* XXX is this static ? */
959         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
960                                  PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH);
961     pCaps->PS20Caps.NumInstructionSlots =
962         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
963                                  PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
964 
965     if (pCaps->PS20Caps.DynamicFlowControlDepth > D3DPS20_MAX_DYNAMICFLOWCONTROLDEPTH
966         || pCaps->PS20Caps.DynamicFlowControlDepth < 0)
967         pCaps->PS20Caps.DynamicFlowControlDepth = D3DPS20_MAX_DYNAMICFLOWCONTROLDEPTH;
968     if (pCaps->PS20Caps.StaticFlowControlDepth > D3DPS20_MAX_STATICFLOWCONTROLDEPTH
969         || pCaps->PS20Caps.StaticFlowControlDepth < 0)
970         pCaps->PS20Caps.StaticFlowControlDepth = D3DPS20_MAX_STATICFLOWCONTROLDEPTH;
971     if (pCaps->PS20Caps.NumTemps > D3DPS20_MAX_NUMTEMPS)
972         pCaps->PS20Caps.NumTemps = D3DPS20_MAX_NUMTEMPS;
973     if (pCaps->PS20Caps.NumInstructionSlots > D3DPS20_MAX_NUMINSTRUCTIONSLOTS)
974         pCaps->PS20Caps.NumInstructionSlots = D3DPS20_MAX_NUMINSTRUCTIONSLOTS;
975     assert(pCaps->PS20Caps.DynamicFlowControlDepth >= D3DPS20_MIN_DYNAMICFLOWCONTROLDEPTH);
976     assert(pCaps->PS20Caps.StaticFlowControlDepth >= D3DPS20_MIN_STATICFLOWCONTROLDEPTH);
977     assert(pCaps->PS20Caps.NumTemps >= D3DPS20_MIN_NUMTEMPS);
978     assert(pCaps->PS20Caps.NumInstructionSlots >= D3DPS20_MIN_NUMINSTRUCTIONSLOTS);
979 
980 
981     if (screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
982                                  PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS))
983         pCaps->VertexTextureFilterCaps = pCaps->TextureFilterCaps &
984             ~(D3DPTFILTERCAPS_MIPFPOINT |
985               D3DPTFILTERCAPS_MIPFPOINT); /* XXX */
986     else
987         pCaps->VertexTextureFilterCaps = 0;
988 
989     pCaps->MaxVertexShader30InstructionSlots =
990         screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
991                                  PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
992     pCaps->MaxPixelShader30InstructionSlots =
993         screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
994                                  PIPE_SHADER_CAP_MAX_INSTRUCTIONS);
995     if (pCaps->MaxVertexShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
996         pCaps->MaxVertexShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
997     if (pCaps->MaxPixelShader30InstructionSlots > D3DMAX30SHADERINSTRUCTIONS)
998         pCaps->MaxPixelShader30InstructionSlots = D3DMAX30SHADERINSTRUCTIONS;
999     assert(pCaps->MaxVertexShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
1000     assert(pCaps->MaxPixelShader30InstructionSlots >= D3DMIN30SHADERINSTRUCTIONS);
1001 
1002     /* 65535 is required, advertise more for GPUs with >= 2048 instruction slots */
1003     pCaps->MaxVShaderInstructionsExecuted = MAX2(65535, pCaps->MaxVertexShader30InstructionSlots * 32);
1004     pCaps->MaxPShaderInstructionsExecuted = MAX2(65535, pCaps->MaxPixelShader30InstructionSlots * 32);
1005 
1006     if (debug_get_bool_option("NINE_DUMP_CAPS", FALSE))
1007         nine_dump_D3DCAPS9(DBG_CHANNEL, pCaps);
1008 
1009     return D3D_OK;
1010 }
1011 
1012 HRESULT NINE_WINAPI
NineAdapter9_CreateDevice(struct NineAdapter9 * This,UINT RealAdapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS * pPresentationParameters,IDirect3D9 * pD3D9,ID3DPresentGroup * pPresentationGroup,IDirect3DDevice9 ** ppReturnedDeviceInterface)1013 NineAdapter9_CreateDevice( struct NineAdapter9 *This,
1014                            UINT RealAdapter,
1015                            D3DDEVTYPE DeviceType,
1016                            HWND hFocusWindow,
1017                            DWORD BehaviorFlags,
1018                            D3DPRESENT_PARAMETERS *pPresentationParameters,
1019                            IDirect3D9 *pD3D9,
1020                            ID3DPresentGroup *pPresentationGroup,
1021                            IDirect3DDevice9 **ppReturnedDeviceInterface )
1022 {
1023     struct pipe_screen *screen;
1024     D3DDEVICE_CREATION_PARAMETERS params;
1025     D3DCAPS9 caps;
1026     int major, minor;
1027     HRESULT hr;
1028 
1029     DBG("This=%p RealAdapter=%u DeviceType=%s hFocusWindow=%p "
1030         "BehaviourFlags=%x " "pD3D9=%p pPresentationGroup=%p "
1031         "ppReturnedDeviceInterface=%p\n", This,
1032         RealAdapter, nine_D3DDEVTYPE_to_str(DeviceType), hFocusWindow,
1033         BehaviorFlags, pD3D9, pPresentationGroup, ppReturnedDeviceInterface);
1034 
1035     ID3DPresentGroup_GetVersion(pPresentationGroup, &major, &minor);
1036     if (major != 1) {
1037         ERR("Doesn't support the ID3DPresentGroup version %d %d. Expected 1\n",
1038             major, minor);
1039         return D3DERR_NOTAVAILABLE;
1040     }
1041 
1042     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
1043     if (FAILED(hr)) {
1044         DBG("Failed to get pipe_screen.\n");
1045         return hr;
1046     }
1047 
1048     hr = NineAdapter9_GetDeviceCaps(This, DeviceType, &caps);
1049     if (FAILED(hr)) {
1050         DBG("Failed to get device caps.\n");
1051         return hr;
1052     }
1053 
1054     params.AdapterOrdinal = RealAdapter;
1055     params.DeviceType = DeviceType;
1056     params.hFocusWindow = hFocusWindow;
1057     params.BehaviorFlags = BehaviorFlags;
1058 
1059     hr = NineDevice9_new(screen, &params, &caps, pPresentationParameters,
1060                          pD3D9, pPresentationGroup, This->ctx, FALSE, NULL,
1061                          (struct NineDevice9 **)ppReturnedDeviceInterface,
1062                          minor);
1063     if (FAILED(hr)) {
1064         DBG("Failed to create device.\n");
1065         return hr;
1066     }
1067     DBG("NineDevice9 created successfully.\n");
1068 
1069     return D3D_OK;
1070 }
1071 
1072 HRESULT NINE_WINAPI
NineAdapter9_CreateDeviceEx(struct NineAdapter9 * This,UINT RealAdapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS * pPresentationParameters,D3DDISPLAYMODEEX * pFullscreenDisplayMode,IDirect3D9Ex * pD3D9Ex,ID3DPresentGroup * pPresentationGroup,IDirect3DDevice9Ex ** ppReturnedDeviceInterface)1073 NineAdapter9_CreateDeviceEx( struct NineAdapter9 *This,
1074                              UINT RealAdapter,
1075                              D3DDEVTYPE DeviceType,
1076                              HWND hFocusWindow,
1077                              DWORD BehaviorFlags,
1078                              D3DPRESENT_PARAMETERS *pPresentationParameters,
1079                              D3DDISPLAYMODEEX *pFullscreenDisplayMode,
1080                              IDirect3D9Ex *pD3D9Ex,
1081                              ID3DPresentGroup *pPresentationGroup,
1082                              IDirect3DDevice9Ex **ppReturnedDeviceInterface )
1083 {
1084     struct pipe_screen *screen;
1085     D3DDEVICE_CREATION_PARAMETERS params;
1086     D3DCAPS9 caps;
1087     int major, minor;
1088     HRESULT hr;
1089 
1090     DBG("This=%p RealAdapter=%u DeviceType=%s hFocusWindow=%p "
1091         "BehaviourFlags=%x " "pD3D9Ex=%p pPresentationGroup=%p "
1092         "ppReturnedDeviceInterface=%p\n", This,
1093         RealAdapter, nine_D3DDEVTYPE_to_str(DeviceType), hFocusWindow,
1094         BehaviorFlags, pD3D9Ex, pPresentationGroup, ppReturnedDeviceInterface);
1095 
1096     ID3DPresentGroup_GetVersion(pPresentationGroup, &major, &minor);
1097     if (major != 1) {
1098         ERR("Doesn't support the ID3DPresentGroup version %d %d. Expected 1\n",
1099             major, minor);
1100         return D3DERR_NOTAVAILABLE;
1101     }
1102 
1103     hr = NineAdapter9_GetScreen(This, DeviceType, &screen);
1104     if (FAILED(hr)) {
1105         DBG("Failed to get pipe_screen.\n");
1106         return hr;
1107     }
1108 
1109     hr = NineAdapter9_GetDeviceCaps(This, DeviceType, &caps);
1110     if (FAILED(hr)) {
1111         DBG("Failed to get device caps.\n");
1112         return hr;
1113     }
1114 
1115     params.AdapterOrdinal = RealAdapter;
1116     params.DeviceType = DeviceType;
1117     params.hFocusWindow = hFocusWindow;
1118     params.BehaviorFlags = BehaviorFlags;
1119 
1120     hr = NineDevice9Ex_new(screen, &params, &caps, pPresentationParameters,
1121                            pFullscreenDisplayMode,
1122                            pD3D9Ex, pPresentationGroup, This->ctx,
1123                            (struct NineDevice9Ex **)ppReturnedDeviceInterface,
1124                            minor);
1125     if (FAILED(hr)) {
1126         DBG("Failed to create device.\n");
1127         return hr;
1128     }
1129     DBG("NineDevice9Ex created successfully.\n");
1130 
1131     return D3D_OK;
1132 }
1133 
1134 ID3DAdapter9Vtbl NineAdapter9_vtable = {
1135     (void *)NineUnknown_QueryInterface,
1136     (void *)NineUnknown_AddRef,
1137     (void *)NineUnknown_Release,
1138     (void *)NineAdapter9_GetAdapterIdentifier,
1139     (void *)NineAdapter9_CheckDeviceType,
1140     (void *)NineAdapter9_CheckDeviceFormat,
1141     (void *)NineAdapter9_CheckDeviceMultiSampleType,
1142     (void *)NineAdapter9_CheckDepthStencilMatch,
1143     (void *)NineAdapter9_CheckDeviceFormatConversion,
1144     (void *)NineAdapter9_GetDeviceCaps,
1145     (void *)NineAdapter9_CreateDevice,
1146     (void *)NineAdapter9_CreateDeviceEx
1147 };
1148 
1149 static const GUID *NineAdapter9_IIDs[] = {
1150     &IID_ID3D9Adapter,
1151     &IID_IUnknown,
1152     NULL
1153 };
1154 
1155 HRESULT
NineAdapter9_new(struct d3dadapter9_context * pCTX,struct NineAdapter9 ** ppOut)1156 NineAdapter9_new( struct d3dadapter9_context *pCTX,
1157                   struct NineAdapter9 **ppOut )
1158 {
1159     NINE_NEW(Adapter9, ppOut, FALSE, /* args */ pCTX);
1160 }
1161