st/nine: Refactor how user constbufs sizes are calculated
[mesa.git] / src / gallium / state_trackers / nine / vertexbuffer9.c
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 "vertexbuffer9.h"
24 #include "device9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27
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"
34
35 #define DBG_CHANNEL DBG_VERTEXBUFFER
36
37 HRESULT
38 NineVertexBuffer9_ctor( struct NineVertexBuffer9 *This,
39 struct NineUnknownParams *pParams,
40 D3DVERTEXBUFFER_DESC *pDesc )
41 {
42 struct pipe_resource *info = &This->base.info;
43 HRESULT hr;
44
45 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This,
46 pDesc->Size, pDesc->Usage, pDesc->Pool);
47
48 user_assert(pDesc->Pool != D3DPOOL_SCRATCH, D3DERR_INVALIDCALL);
49
50 This->maps = MALLOC(sizeof(struct pipe_transfer *));
51 if (!This->maps)
52 return E_OUTOFMEMORY;
53 This->nmaps = 0;
54 This->maxmaps = 1;
55
56 This->pipe = pParams->device->pipe;
57
58 info->screen = pParams->device->screen;
59 info->target = PIPE_BUFFER;
60 info->format = PIPE_FORMAT_R8_UNORM;
61 info->width0 = pDesc->Size;
62 info->flags = 0;
63
64 info->bind = PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_TRANSFER_WRITE;
65 if (!(pDesc->Usage & D3DUSAGE_WRITEONLY))
66 info->bind |= PIPE_BIND_TRANSFER_READ;
67
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;
73
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) { } */
81
82 info->height0 = 1;
83 info->depth0 = 1;
84 info->array_size = 1;
85 info->last_level = 0;
86 info->nr_samples = 0;
87
88 hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE,
89 D3DRTYPE_VERTEXBUFFER, pDesc->Pool, pDesc->Usage);
90 if (FAILED(hr))
91 return hr;
92
93 pDesc->Type = D3DRTYPE_VERTEXBUFFER;
94 pDesc->Format = D3DFMT_VERTEXDATA;
95 This->desc = *pDesc;
96
97 return D3D_OK;
98 }
99
100 void
101 NineVertexBuffer9_dtor( struct NineVertexBuffer9 *This )
102 {
103 if (This->maps) {
104 while (This->nmaps) {
105 NineVertexBuffer9_Unlock(This);
106 }
107 FREE(This->maps);
108 }
109
110 NineResource9_dtor(&This->base);
111 }
112
113 HRESULT WINAPI
114 NineVertexBuffer9_Lock( struct NineVertexBuffer9 *This,
115 UINT OffsetToLock,
116 UINT SizeToLock,
117 void **ppbData,
118 DWORD Flags )
119 {
120 struct pipe_box box;
121 void *data;
122 const unsigned usage = d3dlock_buffer_to_pipe_transfer_usage(Flags);
123
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);
127
128 user_assert(ppbData, E_POINTER);
129 user_assert(!(Flags & ~(D3DLOCK_DISCARD |
130 D3DLOCK_DONOTWAIT |
131 D3DLOCK_NO_DIRTY_UPDATE |
132 D3DLOCK_NOSYSLOCK |
133 D3DLOCK_READONLY |
134 D3DLOCK_NOOVERWRITE)), D3DERR_INVALIDCALL);
135
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));
140 if (newmaps == NULL)
141 return E_OUTOFMEMORY;
142
143 This->maxmaps <<= 1;
144 This->maps = newmaps;
145 }
146
147 if (SizeToLock == 0) {
148 SizeToLock = This->desc.Size - OffsetToLock;
149 user_warn(OffsetToLock != 0);
150 }
151
152 u_box_1d(OffsetToLock, SizeToLock, &box);
153
154 data = This->pipe->transfer_map(This->pipe, This->base.resource, 0,
155 usage, &box, &This->maps[This->nmaps]);
156 if (!data) {
157 DBG("pipe::transfer_map failed\n"
158 " usage = %x\n"
159 " box.x = %u\n"
160 " box.width = %u\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;
166 }
167
168 This->nmaps++;
169 *ppbData = data;
170
171 return D3D_OK;
172 }
173
174 HRESULT WINAPI
175 NineVertexBuffer9_Unlock( struct NineVertexBuffer9 *This )
176 {
177 DBG("This=%p\n", This);
178
179 user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
180 This->pipe->transfer_unmap(This->pipe, This->maps[--(This->nmaps)]);
181 return D3D_OK;
182 }
183
184 HRESULT WINAPI
185 NineVertexBuffer9_GetDesc( struct NineVertexBuffer9 *This,
186 D3DVERTEXBUFFER_DESC *pDesc )
187 {
188 user_assert(pDesc, E_POINTER);
189 *pDesc = This->desc;
190 return D3D_OK;
191 }
192
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
208 };
209
210 static const GUID *NineVertexBuffer9_IIDs[] = {
211 &IID_IDirect3DVertexBuffer9,
212 &IID_IDirect3DResource9,
213 &IID_IUnknown,
214 NULL
215 };
216
217 HRESULT
218 NineVertexBuffer9_new( struct NineDevice9 *pDevice,
219 D3DVERTEXBUFFER_DESC *pDesc,
220 struct NineVertexBuffer9 **ppOut )
221 {
222 NINE_DEVICE_CHILD_NEW(VertexBuffer9, ppOut, /* args */ pDevice, pDesc);
223 }