1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "Direct3DVolumeTexture9.hpp"
17 #include "Direct3DVolume9.hpp"
18 #include "Direct3DDevice9.hpp"
19 #include "Resource.hpp"
26 Direct3DVolumeTexture9::Direct3DVolumeTexture9(Direct3DDevice9 *device, unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool) : Direct3DBaseTexture9(device, D3DRTYPE_VOLUMETEXTURE, format, pool, levels, usage), width(width), height(height), depth(depth)
30 this->levels = sw::log2(sw::max((int)width, (int)height, (int)depth, 1)) + 1;
33 for(unsigned int level = 0; level < sw::MIPMAP_LEVELS; level++)
35 if(level < this->levels)
37 volumeLevel[level] = new Direct3DVolume9(device, this, width, height, depth, format, pool, usage);
38 volumeLevel[level]->bind();
42 volumeLevel[level] = 0;
45 width = sw::max(1, (int)width / 2);
46 height = sw::max(1, (int)height / 2);
47 depth = sw::max(1, (int)depth / 2);
51 Direct3DVolumeTexture9::~Direct3DVolumeTexture9()
53 resource->lock(sw::DESTRUCT);
55 for(int level = 0; level < sw::MIPMAP_LEVELS; level++)
57 if(volumeLevel[level])
59 volumeLevel[level]->unbind();
60 volumeLevel[level] = 0;
67 long Direct3DVolumeTexture9::QueryInterface(const IID &iid, void **object)
69 CriticalSection cs(device);
73 if(iid == IID_IDirect3DVolumeTexture9 ||
74 iid == IID_IDirect3DBaseTexture9 ||
75 iid == IID_IDirect3DResource9 ||
86 return NOINTERFACE(iid);
89 unsigned long Direct3DVolumeTexture9::AddRef()
93 return Direct3DBaseTexture9::AddRef();
96 unsigned long Direct3DVolumeTexture9::Release()
100 return Direct3DBaseTexture9::Release();
103 long Direct3DVolumeTexture9::FreePrivateData(const GUID &guid)
105 CriticalSection cs(device);
109 return Direct3DBaseTexture9::FreePrivateData(guid);
112 long Direct3DVolumeTexture9::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
114 CriticalSection cs(device);
118 return Direct3DBaseTexture9::GetPrivateData(guid, data, size);
121 void Direct3DVolumeTexture9::PreLoad()
123 CriticalSection cs(device);
127 Direct3DBaseTexture9::PreLoad();
130 long Direct3DVolumeTexture9::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
132 CriticalSection cs(device);
136 return Direct3DBaseTexture9::SetPrivateData(guid, data, size, flags);
139 long Direct3DVolumeTexture9::GetDevice(IDirect3DDevice9 **device)
141 CriticalSection(this->device);
145 return Direct3DBaseTexture9::GetDevice(device);
148 unsigned long Direct3DVolumeTexture9::SetPriority(unsigned long newPriority)
150 CriticalSection cs(device);
154 return Direct3DBaseTexture9::SetPriority(newPriority);
157 unsigned long Direct3DVolumeTexture9::GetPriority()
159 CriticalSection cs(device);
163 return Direct3DBaseTexture9::GetPriority();
166 D3DRESOURCETYPE Direct3DVolumeTexture9::GetType()
168 CriticalSection cs(device);
172 return Direct3DBaseTexture9::GetType();
175 void Direct3DVolumeTexture9::GenerateMipSubLevels()
177 CriticalSection cs(device);
181 if(!(usage & D3DUSAGE_AUTOGENMIPMAP) || !volumeLevel[0]->hasDirtyMipmaps())
186 resource->lock(sw::PUBLIC);
188 for(unsigned int i = 0; i < levels - 1; i++)
190 Direct3DVolume9 *source = volumeLevel[i];
191 Direct3DVolume9 *dest = volumeLevel[i + 1];
193 source->lockInternal(0, 0, 0, sw::LOCK_READONLY, sw::PUBLIC);
194 dest->lockInternal(0, 0, 0, sw::LOCK_DISCARD, sw::PUBLIC);
196 int sWidth = source->getWidth();
197 int sHeight = source->getHeight();
198 int sDepth = source->getDepth();
200 int dWidth = dest->getWidth();
201 int dHeight = dest->getHeight();
202 int dDepth = dest->getDepth();
204 D3DTEXTUREFILTERTYPE filter = GetAutoGenFilterType();
206 float w = (float)sWidth / (float)dWidth;
207 float h = (float)sHeight / (float)dHeight;
208 float d = (float)sDepth / (float)dDepth;
212 for(int k = 0; k < dDepth; k++)
216 for(int j = 0; j < dHeight; j++)
220 for(int i = 0; i < dWidth; i++)
222 dest->copyInternal(source, i, j, k, x, y, z, filter > D3DTEXF_POINT);
233 source->unlockInternal();
234 dest->unlockInternal();
237 volumeLevel[0]->cleanMipmaps();
242 D3DTEXTUREFILTERTYPE Direct3DVolumeTexture9::GetAutoGenFilterType()
244 CriticalSection cs(device);
248 return Direct3DBaseTexture9::GetAutoGenFilterType();
251 unsigned long Direct3DVolumeTexture9::GetLevelCount()
253 CriticalSection cs(device);
257 return Direct3DBaseTexture9::GetLevelCount();
260 unsigned long Direct3DVolumeTexture9::GetLOD()
262 CriticalSection cs(device);
266 return Direct3DBaseTexture9::GetLOD();
269 long Direct3DVolumeTexture9::SetAutoGenFilterType(D3DTEXTUREFILTERTYPE filterType)
271 CriticalSection cs(device);
275 return Direct3DBaseTexture9::SetAutoGenFilterType(filterType);
278 unsigned long Direct3DVolumeTexture9::SetLOD(unsigned long newLOD)
280 CriticalSection cs(device);
284 return Direct3DBaseTexture9::SetLOD(newLOD);
287 long Direct3DVolumeTexture9::GetVolumeLevel(unsigned int level, IDirect3DVolume9 **volume)
289 CriticalSection cs(device);
295 if(level >= GetLevelCount() || !volumeLevel[level])
297 return INVALIDCALL();
300 volumeLevel[level]->AddRef();
301 *volume = volumeLevel[level];
306 long Direct3DVolumeTexture9::LockBox(unsigned int level, D3DLOCKED_BOX *lockedVolume, const D3DBOX *box, unsigned long flags)
308 CriticalSection cs(device);
312 if(!lockedVolume || level >= GetLevelCount() || !volumeLevel[level])
314 return INVALIDCALL();
317 return volumeLevel[level]->LockBox(lockedVolume, box, flags);
320 long Direct3DVolumeTexture9::UnlockBox(unsigned int level)
322 CriticalSection cs(device);
326 if(level >= GetLevelCount() || !volumeLevel[level])
328 return INVALIDCALL();
331 return volumeLevel[level]->UnlockBox();
334 long Direct3DVolumeTexture9::AddDirtyBox(const D3DBOX *dirtyBox)
336 CriticalSection cs(device);
345 long Direct3DVolumeTexture9::GetLevelDesc(unsigned int level, D3DVOLUME_DESC *description)
347 CriticalSection cs(device);
351 if(!description || level >= GetLevelCount() || !volumeLevel[level])
353 return INVALIDCALL();
356 return volumeLevel[level]->GetDesc(description);
359 Direct3DVolume9 *Direct3DVolumeTexture9::getInternalVolumeLevel(unsigned int level)
361 return volumeLevel[level];