st/nine: Fix buffer locking flags handling
[mesa.git] / src / gallium / state_trackers / nine / buffer9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 * Copyright 2015 Patrick Rudolph <siro@das-labor.org>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "buffer9.h"
25 #include "device9.h"
26 #include "nine_helpers.h"
27 #include "nine_pipe.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|DBG_VERTEXBUFFER)
37
38 HRESULT
39 NineBuffer9_ctor( struct NineBuffer9 *This,
40 struct NineUnknownParams *pParams,
41 D3DRESOURCETYPE Type,
42 DWORD Usage,
43 UINT Size,
44 D3DPOOL Pool )
45 {
46 struct pipe_resource *info = &This->base.info;
47 HRESULT hr;
48
49 DBG("This=%p Size=0x%x Usage=%x Pool=%u\n", This, Size, Usage, Pool);
50
51 user_assert(Pool != D3DPOOL_SCRATCH, D3DERR_INVALIDCALL);
52
53 This->maps = MALLOC(sizeof(struct pipe_transfer *));
54 if (!This->maps)
55 return E_OUTOFMEMORY;
56 This->nmaps = 0;
57 This->maxmaps = 1;
58 This->size = Size;
59
60 This->pipe = pParams->device->pipe;
61
62 info->screen = pParams->device->screen;
63 info->target = PIPE_BUFFER;
64 info->format = PIPE_FORMAT_R8_UNORM;
65 info->width0 = Size;
66 info->flags = 0;
67
68 info->bind = PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_TRANSFER_WRITE;
69 if (!(Usage & D3DUSAGE_WRITEONLY))
70 info->bind |= PIPE_BIND_TRANSFER_READ;
71
72 info->usage = PIPE_USAGE_DEFAULT;
73 if (Usage & D3DUSAGE_DYNAMIC)
74 info->usage = PIPE_USAGE_STREAM;
75 else if (Pool == D3DPOOL_SYSTEMMEM)
76 info->usage = PIPE_USAGE_STAGING;
77
78 /* if (pDesc->Usage & D3DUSAGE_DONOTCLIP) { } */
79 /* if (pDesc->Usage & D3DUSAGE_NONSECURE) { } */
80 /* if (pDesc->Usage & D3DUSAGE_NPATCHES) { } */
81 /* if (pDesc->Usage & D3DUSAGE_POINTS) { } */
82 /* if (pDesc->Usage & D3DUSAGE_RTPATCHES) { } */
83 if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
84 DBG("Application asked for Software Vertex Processing, "
85 "but this is unimplemented\n");
86 /* if (pDesc->Usage & D3DUSAGE_TEXTAPI) { } */
87
88 info->height0 = 1;
89 info->depth0 = 1;
90 info->array_size = 1;
91 info->last_level = 0;
92 info->nr_samples = 0;
93
94 hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE,
95 Type, Pool, Usage);
96
97 if (FAILED(hr))
98 return hr;
99
100 if (Pool == D3DPOOL_MANAGED) {
101 This->managed.data = align_malloc(
102 nine_format_get_level_alloc_size(This->base.info.format,
103 Size, 1, 0), 32);
104 if (!This->managed.data)
105 return E_OUTOFMEMORY;
106 memset(This->managed.data, 0, Size);
107 This->managed.dirty = TRUE;
108 u_box_1d(0, Size, &This->managed.dirty_box);
109 list_inithead(&This->managed.list);
110 list_inithead(&This->managed.list2);
111 list_add(&This->managed.list, &pParams->device->update_buffers);
112 list_add(&This->managed.list2, &pParams->device->managed_buffers);
113 }
114
115 return D3D_OK;
116 }
117
118 void
119 NineBuffer9_dtor( struct NineBuffer9 *This )
120 {
121 DBG("This=%p\n", This);
122
123 if (This->maps) {
124 while (This->nmaps) {
125 NineBuffer9_Unlock(This);
126 }
127 FREE(This->maps);
128 }
129
130 if (This->base.pool == D3DPOOL_MANAGED) {
131 if (This->managed.data)
132 align_free(This->managed.data);
133 if (This->managed.list.prev != NULL && This->managed.list.next != NULL)
134 list_del(&This->managed.list);
135 if (This->managed.list2.prev != NULL && This->managed.list2.next != NULL)
136 list_del(&This->managed.list2);
137 }
138
139 NineResource9_dtor(&This->base);
140 }
141
142 struct pipe_resource *
143 NineBuffer9_GetResource( struct NineBuffer9 *This )
144 {
145 return NineResource9_GetResource(&This->base);
146 }
147
148 HRESULT NINE_WINAPI
149 NineBuffer9_Lock( struct NineBuffer9 *This,
150 UINT OffsetToLock,
151 UINT SizeToLock,
152 void **ppbData,
153 DWORD Flags )
154 {
155 struct pipe_box box;
156 void *data;
157 unsigned usage;
158
159 DBG("This=%p(pipe=%p) OffsetToLock=0x%x, SizeToLock=0x%x, Flags=0x%x\n",
160 This, This->base.resource,
161 OffsetToLock, SizeToLock, Flags);
162
163 user_assert(ppbData, E_POINTER);
164 user_assert(!(Flags & ~(D3DLOCK_DISCARD |
165 D3DLOCK_DONOTWAIT |
166 D3DLOCK_NO_DIRTY_UPDATE |
167 D3DLOCK_NOSYSLOCK |
168 D3DLOCK_READONLY |
169 D3DLOCK_NOOVERWRITE)), D3DERR_INVALIDCALL);
170
171 if (SizeToLock == 0) {
172 SizeToLock = This->size - OffsetToLock;
173 user_warn(OffsetToLock != 0);
174 }
175
176 u_box_1d(OffsetToLock, SizeToLock, &box);
177
178 if (This->base.pool == D3DPOOL_MANAGED) {
179 /* READONLY doesn't dirty the buffer */
180 if (!(Flags & D3DLOCK_READONLY)) {
181 if (!This->managed.dirty) {
182 assert(LIST_IS_EMPTY(&This->managed.list));
183 This->managed.dirty = TRUE;
184 This->managed.dirty_box = box;
185 } else {
186 u_box_union_2d(&This->managed.dirty_box, &This->managed.dirty_box, &box);
187 /* Do not upload while we are locking, we'll add it back later */
188 if (!LIST_IS_EMPTY(&This->managed.list))
189 list_delinit(&This->managed.list);
190 }
191 }
192 *ppbData = (char *)This->managed.data + OffsetToLock;
193 DBG("returning pointer %p\n", *ppbData);
194 This->nmaps++;
195 return D3D_OK;
196 }
197
198 /* Driver ddi doc: READONLY is never passed to the device. So it can only
199 * have effect on things handled by the driver (MANAGED pool for example).
200 * Msdn doc: DISCARD and NOOVERWRITE are only for DYNAMIC.
201 * ATI doc: You can use DISCARD and NOOVERWRITE without DYNAMIC.
202 * Msdn doc: D3DLOCK_DONOTWAIT is not among the valid flags for buffers.
203 * Our tests: On win 7 nvidia, D3DLOCK_DONOTWAIT does return
204 * D3DERR_WASSTILLDRAWING if the resource is in use, except for DYNAMIC.
205 * Our tests: some apps do use both DISCARD and NOOVERWRITE at the same
206 * time. On windows it seems to return different pointer, thus indicating
207 * DISCARD is taken into account. */
208
209 if (Flags & D3DLOCK_DISCARD)
210 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
211 else if (Flags & D3DLOCK_NOOVERWRITE)
212 usage = PIPE_TRANSFER_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED;
213 else
214 usage = PIPE_TRANSFER_READ_WRITE;
215 if (Flags & D3DLOCK_DONOTWAIT && !(This->base.usage & D3DUSAGE_DYNAMIC))
216 usage |= PIPE_TRANSFER_DONTBLOCK;
217
218 if (This->nmaps == This->maxmaps) {
219 struct pipe_transfer **newmaps =
220 REALLOC(This->maps, sizeof(struct pipe_transfer *)*This->maxmaps,
221 sizeof(struct pipe_transfer *)*(This->maxmaps << 1));
222 if (newmaps == NULL)
223 return E_OUTOFMEMORY;
224
225 This->maxmaps <<= 1;
226 This->maps = newmaps;
227 }
228
229 data = This->pipe->transfer_map(This->pipe, This->base.resource, 0,
230 usage, &box, &This->maps[This->nmaps]);
231
232 if (!data) {
233 DBG("pipe::transfer_map failed\n"
234 " usage = %x\n"
235 " box.x = %u\n"
236 " box.width = %u\n",
237 usage, box.x, box.width);
238
239 if (Flags & D3DLOCK_DONOTWAIT)
240 return D3DERR_WASSTILLDRAWING;
241 return D3DERR_INVALIDCALL;
242 }
243
244 DBG("returning pointer %p\n", data);
245 This->nmaps++;
246 *ppbData = data;
247
248 return D3D_OK;
249 }
250
251 HRESULT NINE_WINAPI
252 NineBuffer9_Unlock( struct NineBuffer9 *This )
253 {
254 DBG("This=%p\n", This);
255
256 user_assert(This->nmaps > 0, D3DERR_INVALIDCALL);
257 if (This->base.pool != D3DPOOL_MANAGED)
258 This->pipe->transfer_unmap(This->pipe, This->maps[--(This->nmaps)]);
259 else {
260 This->nmaps--;
261 /* TODO: Fix this to upload at the first draw call needing the data,
262 * instead of at the next draw call */
263 if (!This->nmaps && This->managed.dirty && LIST_IS_EMPTY(&This->managed.list))
264 list_add(&This->managed.list, &This->base.base.device->update_buffers);
265 }
266 return D3D_OK;
267 }
268
269 void
270 NineBuffer9_SetDirty( struct NineBuffer9 *This )
271 {
272 assert(This->base.pool == D3DPOOL_MANAGED);
273
274 if (!This->managed.dirty) {
275 assert(LIST_IS_EMPTY(&This->managed.list));
276 list_add(&This->managed.list, &This->base.base.device->update_buffers);
277 This->managed.dirty = TRUE;
278 }
279 u_box_1d(0, This->size, &This->managed.dirty_box);
280 }