2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
23 #include "indexbuffer9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27 #include "nine_dump.h"
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"
36 #define DBG_CHANNEL DBG_INDEXBUFFER
39 NineIndexBuffer9_ctor( struct NineIndexBuffer9
*This
,
40 struct NineUnknownParams
*pParams
,
41 D3DINDEXBUFFER_DESC
*pDesc
)
43 struct pipe_resource
*info
= &This
->base
.info
;
45 DBG("This=%p pParams=%p pDesc=%p Usage=%s\n",
46 This
, pParams
, pDesc
, nine_D3DUSAGE_to_str(pDesc
->Usage
));
48 This
->pipe
= pParams
->device
->pipe
;
50 info
->screen
= pParams
->device
->screen
;
51 info
->target
= PIPE_BUFFER
;
52 info
->format
= PIPE_FORMAT_R8_UNORM
;
53 info
->width0
= pDesc
->Size
;
56 info
->bind
= PIPE_BIND_INDEX_BUFFER
| PIPE_BIND_TRANSFER_WRITE
;
57 if (!(pDesc
->Usage
& D3DUSAGE_WRITEONLY
))
58 info
->bind
|= PIPE_BIND_TRANSFER_READ
;
60 info
->usage
= PIPE_USAGE_DEFAULT
;
61 if (pDesc
->Usage
& D3DUSAGE_DYNAMIC
)
62 info
->usage
= PIPE_USAGE_STREAM
;
63 if (pDesc
->Pool
== D3DPOOL_SYSTEMMEM
)
64 info
->usage
= PIPE_USAGE_STAGING
;
66 /* if (This->desc.Usage & D3DUSAGE_DONOTCLIP) { } */
67 /* if (This->desc.Usage & D3DUSAGE_NONSECURE) { } */
68 /* if (This->desc.Usage & D3DUSAGE_NPATCHES) { } */
69 /* if (This->desc.Usage & D3DUSAGE_POINTS) { } */
70 /* if (This->desc.Usage & D3DUSAGE_RTPATCHES) { } */
71 /* if (This->desc.Usage & D3DUSAGE_SOFTWAREPROCESSING) { } */
79 hr
= NineResource9_ctor(&This
->base
, pParams
, NULL
, TRUE
, D3DRTYPE_INDEXBUFFER
,
80 pDesc
->Pool
, pDesc
->Usage
);
84 This
->buffer
.buffer
= This
->base
.resource
;
85 This
->buffer
.offset
= 0;
88 switch (pDesc
->Format
) {
89 case D3DFMT_INDEX16
: This
->buffer
.index_size
= 2; break;
90 case D3DFMT_INDEX32
: This
->buffer
.index_size
= 4; break;
92 user_assert(!"Invalid index format.", D3DERR_INVALIDCALL
);
95 This
->buffer
.user_buffer
= NULL
;
97 pDesc
->Type
= D3DRTYPE_INDEXBUFFER
;
104 NineIndexBuffer9_dtor( struct NineIndexBuffer9
*This
)
106 if (This
->transfer
) { NineIndexBuffer9_Unlock(This
); }
108 NineResource9_dtor(&This
->base
);
111 const struct pipe_index_buffer
*
112 NineIndexBuffer9_GetBuffer( struct NineIndexBuffer9
*This
)
114 return &This
->buffer
;
118 NineIndexBuffer9_Lock( struct NineIndexBuffer9
*This
,
127 const unsigned usage
= d3dlock_buffer_to_pipe_transfer_usage(Flags
);
129 DBG("This=%p OffsetToLock=%u SizeToLock=%u ppbData=%p Flags=%i "
130 "transfer=%p map_count=%u\n", This
, OffsetToLock
,
131 SizeToLock
, ppbData
, Flags
, This
->transfer
, This
->map_count
);
133 count
= ++This
->map_count
;
135 if (SizeToLock
== 0) {
136 SizeToLock
= This
->desc
.Size
- OffsetToLock
;
137 user_warn(OffsetToLock
!= 0);
140 u_box_1d(OffsetToLock
, SizeToLock
, &box
);
142 if (unlikely(count
!= 1)) {
143 DBG("Lock has been called on already locked buffer."
144 "Unmapping before mapping again.");
145 This
->pipe
->transfer_unmap(This
->pipe
, This
->transfer
);
147 data
= This
->pipe
->transfer_map(This
->pipe
, This
->base
.resource
, 0,
148 usage
, &box
, &This
->transfer
);
149 if (!This
->transfer
) {
150 DBG("pipe::transfer_map failed\n"
154 usage
, box
.x
, box
.width
);
157 DBG("Returning memory at %p at address %p\n", *ppbData
, ppbData
);
163 NineIndexBuffer9_Unlock( struct NineIndexBuffer9
*This
)
165 DBG("This=%p\n", This
);
166 if (!This
->map_count
) {
167 DBG("Unmap called without a previous map call.\n");
170 if (--This
->map_count
) {
171 DBG("Ignoring unmap.\n");
174 This
->pipe
->transfer_unmap(This
->pipe
, This
->transfer
);
175 This
->transfer
= NULL
;
180 NineIndexBuffer9_GetDesc( struct NineIndexBuffer9
*This
,
181 D3DINDEXBUFFER_DESC
*pDesc
)
183 user_assert(pDesc
, E_POINTER
);
188 IDirect3DIndexBuffer9Vtbl NineIndexBuffer9_vtable
= {
189 (void *)NineUnknown_QueryInterface
,
190 (void *)NineUnknown_AddRef
,
191 (void *)NineUnknown_Release
,
192 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
193 (void *)NineResource9_SetPrivateData
,
194 (void *)NineResource9_GetPrivateData
,
195 (void *)NineResource9_FreePrivateData
,
196 (void *)NineResource9_SetPriority
,
197 (void *)NineResource9_GetPriority
,
198 (void *)NineResource9_PreLoad
,
199 (void *)NineResource9_GetType
,
200 (void *)NineIndexBuffer9_Lock
,
201 (void *)NineIndexBuffer9_Unlock
,
202 (void *)NineIndexBuffer9_GetDesc
205 static const GUID
*NineIndexBuffer9_IIDs
[] = {
206 &IID_IDirect3DIndexBuffer9
,
207 &IID_IDirect3DResource9
,
213 NineIndexBuffer9_new( struct NineDevice9
*pDevice
,
214 D3DINDEXBUFFER_DESC
*pDesc
,
215 struct NineIndexBuffer9
**ppOut
)
217 NINE_DEVICE_CHILD_NEW(IndexBuffer9
, ppOut
, /* args */ pDevice
, pDesc
);