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 "vertexbuffer9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
28 #include "pipe/p_screen.h"
29 #include "pipe/p_context.h"
30 #include "pipe/p_state.h"
31 #include "pipe/p_defines.h"
32 #include "pipe/p_format.h"
33 #include "util/u_box.h"
35 #define DBG_CHANNEL DBG_VERTEXBUFFER
38 NineVertexBuffer9_ctor( struct NineVertexBuffer9
*This
,
39 struct NineUnknownParams
*pParams
,
40 D3DVERTEXBUFFER_DESC
*pDesc
)
42 struct pipe_resource
*info
= &This
->base
.info
;
45 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This
,
46 pDesc
->Size
, pDesc
->Usage
, pDesc
->Pool
);
48 user_assert(pDesc
->Pool
!= D3DPOOL_SCRATCH
, D3DERR_INVALIDCALL
);
50 This
->maps
= MALLOC(sizeof(struct pipe_transfer
*));
56 This
->pipe
= pParams
->device
->pipe
;
58 info
->screen
= pParams
->device
->screen
;
59 info
->target
= PIPE_BUFFER
;
60 info
->format
= PIPE_FORMAT_R8_UNORM
;
61 info
->width0
= pDesc
->Size
;
64 info
->bind
= PIPE_BIND_VERTEX_BUFFER
| PIPE_BIND_TRANSFER_WRITE
;
65 if (!(pDesc
->Usage
& D3DUSAGE_WRITEONLY
))
66 info
->bind
|= PIPE_BIND_TRANSFER_READ
;
68 info
->usage
= PIPE_USAGE_DEFAULT
;
69 if (pDesc
->Usage
& D3DUSAGE_DYNAMIC
)
70 info
->usage
= PIPE_USAGE_STREAM
;
71 if (pDesc
->Pool
== D3DPOOL_SYSTEMMEM
)
72 info
->usage
= PIPE_USAGE_STAGING
;
74 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
75 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
76 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
77 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
78 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
79 /* if (pDesc->Usage & D3DUSAGE_SOFTWAREPROCESSING) { } */
80 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
88 hr
= NineResource9_ctor(&This
->base
, pParams
, NULL
, TRUE
,
89 D3DRTYPE_VERTEXBUFFER
, pDesc
->Pool
, pDesc
->Usage
);
93 pDesc
->Type
= D3DRTYPE_VERTEXBUFFER
;
94 pDesc
->Format
= D3DFMT_VERTEXDATA
;
101 NineVertexBuffer9_dtor( struct NineVertexBuffer9
*This
)
104 while (This
->nmaps
) {
105 NineVertexBuffer9_Unlock(This
);
110 NineResource9_dtor(&This
->base
);
114 NineVertexBuffer9_Lock( struct NineVertexBuffer9
*This
,
122 const unsigned usage
= d3dlock_buffer_to_pipe_transfer_usage(Flags
);
124 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
125 This
, This
->base
.resource
,
126 OffsetToLock
, SizeToLock
, Flags
);
128 user_assert(ppbData
, E_POINTER
);
129 user_assert(!(Flags
& ~(D3DLOCK_DISCARD
|
131 D3DLOCK_NO_DIRTY_UPDATE
|
134 D3DLOCK_NOOVERWRITE
)), D3DERR_INVALIDCALL
);
136 if (This
->nmaps
== This
->maxmaps
) {
137 struct pipe_transfer
**newmaps
=
138 REALLOC(This
->maps
, sizeof(struct pipe_transfer
*)*This
->maxmaps
,
139 sizeof(struct pipe_transfer
*)*(This
->maxmaps
<< 1));
141 return E_OUTOFMEMORY
;
144 This
->maps
= newmaps
;
147 if (SizeToLock
== 0) {
148 SizeToLock
= This
->desc
.Size
- OffsetToLock
;
149 user_warn(OffsetToLock
!= 0);
152 u_box_1d(OffsetToLock
, SizeToLock
, &box
);
154 data
= This
->pipe
->transfer_map(This
->pipe
, This
->base
.resource
, 0,
155 usage
, &box
, &This
->maps
[This
->nmaps
]);
157 DBG("pipe::transfer_map failed\n"
161 usage
, box
.x
, box
.width
);
162 /* not sure what to return, msdn suggests this */
163 if (Flags
& D3DLOCK_DONOTWAIT
)
164 return D3DERR_WASSTILLDRAWING
;
165 return D3DERR_INVALIDCALL
;
175 NineVertexBuffer9_Unlock( struct NineVertexBuffer9
*This
)
177 DBG("This=%p\n", This
);
179 user_assert(This
->nmaps
> 0, D3DERR_INVALIDCALL
);
180 This
->pipe
->transfer_unmap(This
->pipe
, This
->maps
[--(This
->nmaps
)]);
185 NineVertexBuffer9_GetDesc( struct NineVertexBuffer9
*This
,
186 D3DVERTEXBUFFER_DESC
*pDesc
)
188 user_assert(pDesc
, E_POINTER
);
193 IDirect3DVertexBuffer9Vtbl NineVertexBuffer9_vtable
= {
194 (void *)NineUnknown_QueryInterface
,
195 (void *)NineUnknown_AddRef
,
196 (void *)NineUnknown_Release
,
197 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
198 (void *)NineResource9_SetPrivateData
,
199 (void *)NineResource9_GetPrivateData
,
200 (void *)NineResource9_FreePrivateData
,
201 (void *)NineResource9_SetPriority
,
202 (void *)NineResource9_GetPriority
,
203 (void *)NineResource9_PreLoad
,
204 (void *)NineResource9_GetType
,
205 (void *)NineVertexBuffer9_Lock
,
206 (void *)NineVertexBuffer9_Unlock
,
207 (void *)NineVertexBuffer9_GetDesc
210 static const GUID
*NineVertexBuffer9_IIDs
[] = {
211 &IID_IDirect3DVertexBuffer9
,
212 &IID_IDirect3DResource9
,
218 NineVertexBuffer9_new( struct NineDevice9
*pDevice
,
219 D3DVERTEXBUFFER_DESC
*pDesc
,
220 struct NineVertexBuffer9
**ppOut
)
222 NINE_DEVICE_CHILD_NEW(VertexBuffer9
, ppOut
, /* args */ pDevice
, pDesc
);