1 /*
2 * Copyright 2011 Joakim Sindholt <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "device9.h"
7 #include "volumetexture9.h"
8 #include "nine_helpers.h"
9 #include "nine_pipe.h"
10
11 #define DBG_CHANNEL DBG_VOLUMETEXTURE
12
13 static HRESULT
NineVolumeTexture9_ctor(struct NineVolumeTexture9 * This,struct NineUnknownParams * pParams,UINT Width,UINT Height,UINT Depth,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,HANDLE * pSharedHandle)14 NineVolumeTexture9_ctor( struct NineVolumeTexture9 *This,
15 struct NineUnknownParams *pParams,
16 UINT Width, UINT Height, UINT Depth, UINT Levels,
17 DWORD Usage,
18 D3DFORMAT Format,
19 D3DPOOL Pool,
20 HANDLE *pSharedHandle )
21 {
22 struct pipe_resource *info = &This->base.base.info;
23 struct pipe_screen *screen = pParams->device->screen;
24 enum pipe_format pf;
25 unsigned l;
26 D3DVOLUME_DESC voldesc;
27 HRESULT hr;
28
29 DBG("This=%p pParams=%p Width=%u Height=%u Depth=%u Levels=%u "
30 "Usage=%d Format=%d Pool=%d pSharedHandle=%p\n",
31 This, pParams, Width, Height, Depth, Levels,
32 Usage, Format, Pool, pSharedHandle);
33
34 user_assert(Width && Height && Depth, D3DERR_INVALIDCALL);
35
36 /* user_assert(!pSharedHandle || Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL); */
37 user_assert(!pSharedHandle, D3DERR_INVALIDCALL); /* TODO */
38
39 /* An IDirect3DVolume9 cannot be bound as a render target can it ? */
40 user_assert(!(Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)),
41 D3DERR_INVALIDCALL);
42 user_assert(!(Usage & D3DUSAGE_AUTOGENMIPMAP), D3DERR_INVALIDCALL);
43
44 pf = d3d9_to_pipe_format_checked(screen, Format, PIPE_TEXTURE_3D, 0,
45 PIPE_BIND_SAMPLER_VIEW, false,
46 Pool == D3DPOOL_SCRATCH);
47
48 if (pf == PIPE_FORMAT_NONE)
49 return D3DERR_INVALIDCALL;
50
51 /* We support ATI1 and ATI2 hacks only for 2D and Cube textures */
52 if (Format == D3DFMT_ATI1 || Format == D3DFMT_ATI2)
53 return D3DERR_INVALIDCALL;
54
55 if (compressed_format(Format)) {
56 const unsigned w = util_format_get_blockwidth(pf);
57 const unsigned h = util_format_get_blockheight(pf);
58 /* Compressed formats are not compressed on depth component */
59 user_assert(!(Width % w) && !(Height % h), D3DERR_INVALIDCALL);
60 }
61
62 info->screen = pParams->device->screen;
63 info->target = PIPE_TEXTURE_3D;
64 info->format = pf;
65 info->width0 = Width;
66 info->height0 = Height;
67 info->depth0 = Depth;
68 if (Levels)
69 info->last_level = Levels - 1;
70 else
71 info->last_level = util_logbase2(MAX2(MAX2(Width, Height), Depth));
72 info->array_size = 1;
73 info->nr_samples = 0;
74 info->nr_storage_samples = 0;
75 info->bind = PIPE_BIND_SAMPLER_VIEW;
76 info->usage = PIPE_USAGE_DEFAULT;
77 info->flags = 0;
78
79 if (Usage & D3DUSAGE_DYNAMIC) {
80 info->usage = PIPE_USAGE_DYNAMIC;
81 }
82 if (Usage & D3DUSAGE_SOFTWAREPROCESSING)
83 DBG("Application asked for Software Vertex Processing, "
84 "but this is unimplemented\n");
85
86 This->base.pstype = 3;
87
88 hr = NineBaseTexture9_ctor(&This->base, pParams, NULL,
89 D3DRTYPE_VOLUMETEXTURE, Format, Pool, Usage);
90 if (FAILED(hr))
91 return hr;
92
93 This->volumes = CALLOC(This->base.level_count, sizeof(*This->volumes));
94 if (!This->volumes)
95 return E_OUTOFMEMORY;
96
97 voldesc.Format = Format;
98 voldesc.Type = D3DRTYPE_VOLUME;
99 voldesc.Usage = Usage;
100 voldesc.Pool = Pool;
101 for (l = 0; l < This->base.level_count; ++l) {
102 voldesc.Width = u_minify(Width, l);
103 voldesc.Height = u_minify(Height, l);
104 voldesc.Depth = u_minify(Depth, l);
105
106 hr = NineVolume9_new(This->base.base.base.device, NineUnknown(This),
107 This->base.base.resource, l,
108 &voldesc, &This->volumes[l]);
109 if (FAILED(hr))
110 return hr;
111 }
112
113 /* Textures start initially dirty */
114 NineVolumeTexture9_AddDirtyBox(This, NULL);
115
116 return D3D_OK;
117 }
118
119 static void
NineVolumeTexture9_dtor(struct NineVolumeTexture9 * This)120 NineVolumeTexture9_dtor( struct NineVolumeTexture9 *This )
121 {
122 unsigned l;
123
124 DBG("This=%p\n", This);
125
126 if (This->volumes) {
127 for (l = 0; l < This->base.level_count; ++l)
128 if (This->volumes[l])
129 NineUnknown_Destroy(&This->volumes[l]->base);
130 FREE(This->volumes);
131 }
132
133 NineBaseTexture9_dtor(&This->base);
134 }
135
136 HRESULT NINE_WINAPI
NineVolumeTexture9_GetLevelDesc(struct NineVolumeTexture9 * This,UINT Level,D3DVOLUME_DESC * pDesc)137 NineVolumeTexture9_GetLevelDesc( struct NineVolumeTexture9 *This,
138 UINT Level,
139 D3DVOLUME_DESC *pDesc )
140 {
141 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
142
143 *pDesc = This->volumes[Level]->desc;
144
145 return D3D_OK;
146 }
147
148 HRESULT NINE_WINAPI
NineVolumeTexture9_GetVolumeLevel(struct NineVolumeTexture9 * This,UINT Level,IDirect3DVolume9 ** ppVolumeLevel)149 NineVolumeTexture9_GetVolumeLevel( struct NineVolumeTexture9 *This,
150 UINT Level,
151 IDirect3DVolume9 **ppVolumeLevel )
152 {
153 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
154
155 NineUnknown_AddRef(NineUnknown(This->volumes[Level]));
156 *ppVolumeLevel = (IDirect3DVolume9 *)This->volumes[Level];
157
158 return D3D_OK;
159 }
160
161 HRESULT NINE_WINAPI
NineVolumeTexture9_LockBox(struct NineVolumeTexture9 * This,UINT Level,D3DLOCKED_BOX * pLockedVolume,const D3DBOX * pBox,DWORD Flags)162 NineVolumeTexture9_LockBox( struct NineVolumeTexture9 *This,
163 UINT Level,
164 D3DLOCKED_BOX *pLockedVolume,
165 const D3DBOX *pBox,
166 DWORD Flags )
167 {
168 DBG("This=%p Level=%u pLockedVolume=%p pBox=%p Flags=%d\n",
169 This, Level, pLockedVolume, pBox, Flags);
170
171 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
172
173 return NineVolume9_LockBox(This->volumes[Level], pLockedVolume, pBox,
174 Flags);
175 }
176
177 HRESULT NINE_WINAPI
NineVolumeTexture9_UnlockBox(struct NineVolumeTexture9 * This,UINT Level)178 NineVolumeTexture9_UnlockBox( struct NineVolumeTexture9 *This,
179 UINT Level )
180 {
181 DBG("This=%p Level=%u\n", This, Level);
182
183 user_assert(Level < This->base.level_count, D3DERR_INVALIDCALL);
184
185 return NineVolume9_UnlockBox(This->volumes[Level]);
186 }
187
188 HRESULT NINE_WINAPI
NineVolumeTexture9_AddDirtyBox(struct NineVolumeTexture9 * This,const D3DBOX * pDirtyBox)189 NineVolumeTexture9_AddDirtyBox( struct NineVolumeTexture9 *This,
190 const D3DBOX *pDirtyBox )
191 {
192 DBG("This=%p pDirtybox=%p\n", This, pDirtyBox);
193
194 if (This->base.base.pool == D3DPOOL_DEFAULT) {
195 return D3D_OK;
196 }
197
198 if (This->base.base.pool == D3DPOOL_MANAGED) {
199 This->base.managed.dirty = true;
200 BASETEX_REGISTER_UPDATE(&This->base);
201 }
202
203 if (!pDirtyBox) {
204 This->dirty_box.x = 0;
205 This->dirty_box.y = 0;
206 This->dirty_box.z = 0;
207 This->dirty_box.width = This->base.base.info.width0;
208 This->dirty_box.height = This->base.base.info.height0;
209 This->dirty_box.depth = This->base.base.info.depth0;
210 } else {
211 if (This->dirty_box.width == 0) {
212 d3dbox_to_pipe_box(&This->dirty_box, pDirtyBox);
213 } else {
214 struct pipe_box box;
215 d3dbox_to_pipe_box(&box, pDirtyBox);
216 u_box_union_3d(&This->dirty_box, &This->dirty_box, &box);
217 }
218 This->dirty_box.x = MAX2(This->dirty_box.x, 0);
219 This->dirty_box.y = MAX2(This->dirty_box.y, 0);
220 This->dirty_box.z = MAX2(This->dirty_box.z, 0);
221 This->dirty_box.width = MIN2(This->dirty_box.width,
222 This->base.base.info.width0 - This->dirty_box.x);
223 This->dirty_box.height = MIN2(This->dirty_box.height,
224 This->base.base.info.height0 - This->dirty_box.y);
225 This->dirty_box.depth = MIN2(This->dirty_box.depth,
226 This->base.base.info.depth0 - This->dirty_box.z);
227 }
228 return D3D_OK;
229 }
230
231 IDirect3DVolumeTexture9Vtbl NineVolumeTexture9_vtable = {
232 (void *)NineUnknown_QueryInterface,
233 (void *)NineUnknown_AddRef,
234 (void *)NineUnknown_Release,
235 (void *)NineUnknown_GetDevice, /* actually part of Resource9 iface */
236 (void *)NineUnknown_SetPrivateData,
237 (void *)NineUnknown_GetPrivateData,
238 (void *)NineUnknown_FreePrivateData,
239 (void *)NineResource9_SetPriority,
240 (void *)NineResource9_GetPriority,
241 (void *)NineBaseTexture9_PreLoad,
242 (void *)NineResource9_GetType,
243 (void *)NineBaseTexture9_SetLOD,
244 (void *)NineBaseTexture9_GetLOD,
245 (void *)NineBaseTexture9_GetLevelCount,
246 (void *)NineBaseTexture9_SetAutoGenFilterType,
247 (void *)NineBaseTexture9_GetAutoGenFilterType,
248 (void *)NineBaseTexture9_GenerateMipSubLevels,
249 (void *)NineVolumeTexture9_GetLevelDesc,
250 (void *)NineVolumeTexture9_GetVolumeLevel,
251 (void *)NineVolumeTexture9_LockBox,
252 (void *)NineVolumeTexture9_UnlockBox,
253 (void *)NineVolumeTexture9_AddDirtyBox
254 };
255
256 static const GUID *NineVolumeTexture9_IIDs[] = {
257 &IID_IDirect3DVolumeTexture9,
258 &IID_IDirect3DBaseTexture9,
259 &IID_IDirect3DResource9,
260 &IID_IUnknown,
261 NULL
262 };
263
264 HRESULT
NineVolumeTexture9_new(struct NineDevice9 * pDevice,UINT Width,UINT Height,UINT Depth,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,struct NineVolumeTexture9 ** ppOut,HANDLE * pSharedHandle)265 NineVolumeTexture9_new( struct NineDevice9 *pDevice,
266 UINT Width, UINT Height, UINT Depth, UINT Levels,
267 DWORD Usage,
268 D3DFORMAT Format,
269 D3DPOOL Pool,
270 struct NineVolumeTexture9 **ppOut,
271 HANDLE *pSharedHandle )
272 {
273 NINE_DEVICE_CHILD_NEW(VolumeTexture9, ppOut, pDevice,
274 Width, Height, Depth, Levels,
275 Usage, Format, Pool, pSharedHandle);
276 }
277
278