st/nine: Fix present_buffers allocation
[mesa.git] / src / gallium / state_trackers / nine / indexbuffer9.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 "indexbuffer9.h"
24 #include "device9.h"
25 #include "nine_helpers.h"
26 #include "nine_pipe.h"
27 #include "nine_dump.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
37
38 HRESULT
39 NineIndexBuffer9_ctor( struct NineIndexBuffer9 *This,
40 struct NineUnknownParams *pParams,
41 D3DINDEXBUFFER_DESC *pDesc )
42 {
43 struct pipe_resource *info = &This->base.info;
44 HRESULT hr;
45 DBG("This=%p pParams=%p pDesc=%p Usage=%s\n",
46 This, pParams, pDesc, nine_D3DUSAGE_to_str(pDesc->Usage));
47
48 This->pipe = pParams->device->pipe;
49
50 info->screen = pParams->device->screen;
51 info->target = PIPE_BUFFER;
52 info->format = PIPE_FORMAT_R8_UNORM;
53 info->width0 = pDesc->Size;
54 info->flags = 0;
55
56 info->bind = PIPE_BIND_INDEX_BUFFER | PIPE_BIND_TRANSFER_WRITE;
57 if (!(pDesc->Usage & D3DUSAGE_WRITEONLY))
58 info->bind |= PIPE_BIND_TRANSFER_READ;
59
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;
65
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) { } */
72
73 info->height0 = 1;
74 info->depth0 = 1;
75 info->array_size = 1;
76 info->last_level = 0;
77 info->nr_samples = 0;
78
79 hr = NineResource9_ctor(&This->base, pParams, NULL, TRUE, D3DRTYPE_INDEXBUFFER,
80 pDesc->Pool, pDesc->Usage);
81 if (FAILED(hr))
82 return hr;
83
84 This->buffer.buffer = This->base.resource;
85 This->buffer.offset = 0;
86 This->map_count = 0;
87
88 switch (pDesc->Format) {
89 case D3DFMT_INDEX16: This->buffer.index_size = 2; break;
90 case D3DFMT_INDEX32: This->buffer.index_size = 4; break;
91 default:
92 user_assert(!"Invalid index format.", D3DERR_INVALIDCALL);
93 break;
94 }
95 This->buffer.user_buffer = NULL;
96
97 pDesc->Type = D3DRTYPE_INDEXBUFFER;
98 This->desc = *pDesc;
99
100 return D3D_OK;
101 }
102
103 void
104 NineIndexBuffer9_dtor( struct NineIndexBuffer9 *This )
105 {
106 if (This->transfer) { NineIndexBuffer9_Unlock(This); }
107
108 NineResource9_dtor(&This->base);
109 }
110
111 const struct pipe_index_buffer *
112 NineIndexBuffer9_GetBuffer( struct NineIndexBuffer9 *This )
113 {
114 return &This->buffer;
115 }
116
117 HRESULT WINAPI
118 NineIndexBuffer9_Lock( struct NineIndexBuffer9 *This,
119 UINT OffsetToLock,
120 UINT SizeToLock,
121 void **ppbData,
122 DWORD Flags )
123 {
124 struct pipe_box box;
125 void *data;
126 UINT count;
127 const unsigned usage = d3dlock_buffer_to_pipe_transfer_usage(Flags);
128
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);
132
133 count = ++This->map_count;
134
135 if (SizeToLock == 0) {
136 SizeToLock = This->desc.Size - OffsetToLock;
137 user_warn(OffsetToLock != 0);
138 }
139
140 u_box_1d(OffsetToLock, SizeToLock, &box);
141
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);
146 }
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"
151 " usage = %u\n"
152 " box.x = %u\n"
153 " box.width = %u\n",
154 usage, box.x, box.width);
155 }
156 *ppbData = data;
157 DBG("Returning memory at %p at address %p\n", *ppbData, ppbData);
158
159 return D3D_OK;
160 }
161
162 HRESULT WINAPI
163 NineIndexBuffer9_Unlock( struct NineIndexBuffer9 *This )
164 {
165 DBG("This=%p\n", This);
166 if (!This->map_count) {
167 DBG("Unmap called without a previous map call.\n");
168 return D3D_OK;
169 }
170 if (--This->map_count) {
171 DBG("Ignoring unmap.\n");
172 return D3D_OK;
173 }
174 This->pipe->transfer_unmap(This->pipe, This->transfer);
175 This->transfer = NULL;
176 return D3D_OK;
177 }
178
179 HRESULT WINAPI
180 NineIndexBuffer9_GetDesc( struct NineIndexBuffer9 *This,
181 D3DINDEXBUFFER_DESC *pDesc )
182 {
183 user_assert(pDesc, E_POINTER);
184 *pDesc = This->desc;
185 return D3D_OK;
186 }
187
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
203 };
204
205 static const GUID *NineIndexBuffer9_IIDs[] = {
206 &IID_IDirect3DIndexBuffer9,
207 &IID_IDirect3DResource9,
208 &IID_IUnknown,
209 NULL
210 };
211
212 HRESULT
213 NineIndexBuffer9_new( struct NineDevice9 *pDevice,
214 D3DINDEXBUFFER_DESC *pDesc,
215 struct NineIndexBuffer9 **ppOut )
216 {
217 NINE_DEVICE_CHILD_NEW(IndexBuffer9, ppOut, /* args */ pDevice, pDesc);
218 }