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 "indexbuffer9.h"
24 #include "device9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27 #include "nine_dump.h"
28 
29 #include "pipe/p_screen.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_state.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_format.h"
34 #include "util/u_box.h"
35 
36 #define DBG_CHANNEL DBG_INDEXBUFFER
37 
38 HRESULT
39 NineIndexBuffer9_ctor( struct NineIndexBuffer9 *This,
40                        struct NineUnknownParams *pParams,
41                        D3DINDEXBUFFER_DESC *pDesc )
42 {
43     HRESULT hr;
44     DBG("This=%p pParams=%p pDesc=%p Usage=%s\n",
45          This, pParams, pDesc, nine_D3DUSAGE_to_str(pDesc->Usage));
46 
47     hr = NineBuffer9_ctor(&This->base, pParams, D3DRTYPE_INDEXBUFFER,
48                           pDesc->Usage, pDesc->Size, pDesc->Pool);
49     if (FAILED(hr))
50         return hr;
51 
52     This->buffer.buffer = NULL;
53     This->buffer.offset = 0;
54 
55     switch (pDesc->Format) {
56     case D3DFMT_INDEX16: This->buffer.index_size = 2; break;
57     case D3DFMT_INDEX32: This->buffer.index_size = 4; break;
58     default:
59         user_assert(!"Invalid index format.", D3DERR_INVALIDCALL);
60         break;
61     }
62     This->buffer.user_buffer = NULL;
63 
64     pDesc->Type = D3DRTYPE_INDEXBUFFER;
65     This->desc = *pDesc;
66 
67     return D3D_OK;
68 }
69 
70 void
71 NineIndexBuffer9_dtor( struct NineIndexBuffer9 *This )
72 {
73     NineBuffer9_dtor(&This->base);
74 }
75 
76 const struct pipe_index_buffer *
77 NineIndexBuffer9_GetBuffer( struct NineIndexBuffer9 *This )
78 {
79     /* The resource may change */
80     This->buffer.buffer = NineBuffer9_GetResource(&This->base, &This->buffer.offset);
81     return &This->buffer;
82 }
83 
84 HRESULT NINE_WINAPI
85 NineIndexBuffer9_Lock( struct NineIndexBuffer9 *This,
86                        UINT OffsetToLock,
87                        UINT SizeToLock,
88                        void **ppbData,
89                        DWORD Flags )
90 {
91     return NineBuffer9_Lock(&This->base, OffsetToLock, SizeToLock, ppbData, Flags);
92 }
93 
94 HRESULT NINE_WINAPI
95 NineIndexBuffer9_Unlock( struct NineIndexBuffer9 *This )
96 {
97     return NineBuffer9_Unlock(&This->base);
98 }
99 
100 HRESULT NINE_WINAPI
101 NineIndexBuffer9_GetDesc( struct NineIndexBuffer9 *This,
102                           D3DINDEXBUFFER_DESC *pDesc )
103 {
104     user_assert(pDesc, E_POINTER);
105     *pDesc = This->desc;
106     return D3D_OK;
107 }
108 
109 IDirect3DIndexBuffer9Vtbl NineIndexBuffer9_vtable = {
110     (void *)NineUnknown_QueryInterface,
111     (void *)NineUnknown_AddRef,
112     (void *)NineUnknown_Release,
113     (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
114     (void *)NineUnknown_SetPrivateData,
115     (void *)NineUnknown_GetPrivateData,
116     (void *)NineUnknown_FreePrivateData,
117     (void *)NineResource9_SetPriority,
118     (void *)NineResource9_GetPriority,
119     (void *)NineResource9_PreLoad,
120     (void *)NineResource9_GetType,
121     (void *)NineIndexBuffer9_Lock,
122     (void *)NineIndexBuffer9_Unlock,
123     (void *)NineIndexBuffer9_GetDesc
124 };
125 
126 static const GUID *NineIndexBuffer9_IIDs[] = {
127     &IID_IDirect3DIndexBuffer9,
128     &IID_IDirect3DResource9,
129     &IID_IUnknown,
130     NULL
131 };
132 
133 HRESULT
134 NineIndexBuffer9_new( struct NineDevice9 *pDevice,
135                       D3DINDEXBUFFER_DESC *pDesc,
136                       struct NineIndexBuffer9 **ppOut )
137 {
138     NINE_DEVICE_CHILD_NEW(IndexBuffer9, ppOut, /* args */ pDevice, pDesc);
139 }
140