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 DBG("Application asked for Software Vertex Processing, "
81 "but this is unimplemented\n");
82 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
90 hr
= NineResource9_ctor(&This
->base
, pParams
, NULL
, TRUE
,
91 D3DRTYPE_VERTEXBUFFER
, pDesc
->Pool
, pDesc
->Usage
);
95 pDesc
->Type
= D3DRTYPE_VERTEXBUFFER
;
96 pDesc
->Format
= D3DFMT_VERTEXDATA
;
103 NineVertexBuffer9_dtor( struct NineVertexBuffer9
*This
)
106 while (This
->nmaps
) {
107 NineVertexBuffer9_Unlock(This
);
112 NineResource9_dtor(&This
->base
);
116 NineVertexBuffer9_Lock( struct NineVertexBuffer9
*This
,
124 const unsigned usage
= d3dlock_buffer_to_pipe_transfer_usage(Flags
);
126 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
127 This
, This
->base
.resource
,
128 OffsetToLock
, SizeToLock
, Flags
);
130 user_assert(ppbData
, E_POINTER
);
131 user_assert(!(Flags
& ~(D3DLOCK_DISCARD
|
133 D3DLOCK_NO_DIRTY_UPDATE
|
136 D3DLOCK_NOOVERWRITE
)), D3DERR_INVALIDCALL
);
138 if (This
->nmaps
== This
->maxmaps
) {
139 struct pipe_transfer
**newmaps
=
140 REALLOC(This
->maps
, sizeof(struct pipe_transfer
*)*This
->maxmaps
,
141 sizeof(struct pipe_transfer
*)*(This
->maxmaps
<< 1));
143 return E_OUTOFMEMORY
;
146 This
->maps
= newmaps
;
149 if (SizeToLock
== 0) {
150 SizeToLock
= This
->desc
.Size
- OffsetToLock
;
151 user_warn(OffsetToLock
!= 0);
154 u_box_1d(OffsetToLock
, SizeToLock
, &box
);
156 data
= This
->pipe
->transfer_map(This
->pipe
, This
->base
.resource
, 0,
157 usage
, &box
, &This
->maps
[This
->nmaps
]);
159 DBG("pipe::transfer_map failed\n"
163 usage
, box
.x
, box
.width
);
164 /* not sure what to return, msdn suggests this */
165 if (Flags
& D3DLOCK_DONOTWAIT
)
166 return D3DERR_WASSTILLDRAWING
;
167 return D3DERR_INVALIDCALL
;
177 NineVertexBuffer9_Unlock( struct NineVertexBuffer9
*This
)
179 DBG("This=%p\n", This
);
181 user_assert(This
->nmaps
> 0, D3DERR_INVALIDCALL
);
182 This
->pipe
->transfer_unmap(This
->pipe
, This
->maps
[--(This
->nmaps
)]);
187 NineVertexBuffer9_GetDesc( struct NineVertexBuffer9
*This
,
188 D3DVERTEXBUFFER_DESC
*pDesc
)
190 user_assert(pDesc
, E_POINTER
);
195 IDirect3DVertexBuffer9Vtbl NineVertexBuffer9_vtable
= {
196 (void *)NineUnknown_QueryInterface
,
197 (void *)NineUnknown_AddRef
,
198 (void *)NineUnknown_Release
,
199 (void *)NineUnknown_GetDevice
, /* actually part of Resource9 iface */
200 (void *)NineResource9_SetPrivateData
,
201 (void *)NineResource9_GetPrivateData
,
202 (void *)NineResource9_FreePrivateData
,
203 (void *)NineResource9_SetPriority
,
204 (void *)NineResource9_GetPriority
,
205 (void *)NineResource9_PreLoad
,
206 (void *)NineResource9_GetType
,
207 (void *)NineVertexBuffer9_Lock
,
208 (void *)NineVertexBuffer9_Unlock
,
209 (void *)NineVertexBuffer9_GetDesc
212 static const GUID
*NineVertexBuffer9_IIDs
[] = {
213 &IID_IDirect3DVertexBuffer9
,
214 &IID_IDirect3DResource9
,
220 NineVertexBuffer9_new( struct NineDevice9
*pDevice
,
221 D3DVERTEXBUFFER_DESC
*pDesc
,
222 struct NineVertexBuffer9
**ppOut
)
224 NINE_DEVICE_CHILD_NEW(VertexBuffer9
, ppOut
, /* args */ pDevice
, pDesc
);