1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
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
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 
15 #include "Direct3DVolumeTexture8.hpp"
16 
17 #include "Direct3DVolume8.hpp"
18 #include "Resource.hpp"
19 #include "Debug.hpp"
20 
21 #include <assert.h>
22 
23 namespace D3D8
24 {
Direct3DVolumeTexture8(Direct3DDevice8 * device,unsigned int width,unsigned int height,unsigned int depth,unsigned int levels,unsigned long usage,D3DFORMAT format,D3DPOOL pool)25 	Direct3DVolumeTexture8::Direct3DVolumeTexture8(Direct3DDevice8 *device, unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool) : Direct3DBaseTexture8(device, D3DRTYPE_VOLUMETEXTURE, levels, usage), width(width), height(height), depth(depth), format(format), pool(pool)
26 	{
27 		if(levels == 0)
28 		{
29 			this->levels = sw::log2(sw::max((int)width, (int)height, (int)depth, 1)) + 1;
30 		}
31 
32 		for(unsigned int level = 0; level < sw::MIPMAP_LEVELS; level++)
33 		{
34 			if(level < this->levels)
35 			{
36 				volumeLevel[level] = new Direct3DVolume8(device, this, width, height, depth, format, pool, true, usage);
37 				volumeLevel[level]->bind();
38 			}
39 			else
40 			{
41 				volumeLevel[level] = 0;
42 			}
43 
44 			width = sw::max(1, (int)width / 2);
45 			height = sw::max(1, (int)height / 2);
46 			depth = sw::max(1, (int)depth / 2);
47 		}
48 	}
49 
~Direct3DVolumeTexture8()50 	Direct3DVolumeTexture8::~Direct3DVolumeTexture8()
51 	{
52 		for(int level = 0; level < sw::MIPMAP_LEVELS; level++)
53 		{
54 			if(volumeLevel[level])
55 			{
56 				volumeLevel[level]->unbind();
57 				volumeLevel[level] = 0;
58 			}
59 		}
60 	}
61 
QueryInterface(const IID & iid,void ** object)62 	long Direct3DVolumeTexture8::QueryInterface(const IID &iid, void **object)
63 	{
64 		TRACE("");
65 
66 		if(iid == IID_IDirect3DVolumeTexture8 ||
67 		   iid == IID_IDirect3DBaseTexture8 ||
68 		   iid == IID_IDirect3DResource8 ||
69 		   iid == IID_IUnknown)
70 		{
71 			AddRef();
72 			*object = this;
73 
74 			return S_OK;
75 		}
76 
77 		*object = 0;
78 
79 		return NOINTERFACE(iid);
80 	}
81 
AddRef()82 	unsigned long Direct3DVolumeTexture8::AddRef()
83 	{
84 		TRACE("");
85 
86 		return Direct3DBaseTexture8::AddRef();
87 	}
88 
Release()89 	unsigned long Direct3DVolumeTexture8::Release()
90 	{
91 		TRACE("");
92 
93 		return Direct3DBaseTexture8::Release();
94 	}
95 
FreePrivateData(const GUID & guid)96 	long Direct3DVolumeTexture8::FreePrivateData(const GUID &guid)
97 	{
98 		TRACE("");
99 
100 		return Direct3DBaseTexture8::FreePrivateData(guid);
101 	}
102 
GetPrivateData(const GUID & guid,void * data,unsigned long * size)103 	long Direct3DVolumeTexture8::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
104 	{
105 		TRACE("");
106 
107 		return Direct3DBaseTexture8::GetPrivateData(guid, data, size);
108 	}
109 
PreLoad()110 	void Direct3DVolumeTexture8::PreLoad()
111 	{
112 		TRACE("");
113 
114 		Direct3DBaseTexture8::PreLoad();
115 	}
116 
SetPrivateData(const GUID & guid,const void * data,unsigned long size,unsigned long flags)117 	long Direct3DVolumeTexture8::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
118 	{
119 		TRACE("");
120 
121 		return Direct3DBaseTexture8::SetPrivateData(guid, data, size, flags);
122 	}
123 
GetDevice(IDirect3DDevice8 ** device)124 	long Direct3DVolumeTexture8::GetDevice(IDirect3DDevice8 **device)
125 	{
126 		TRACE("");
127 
128 		return Direct3DBaseTexture8::GetDevice(device);
129 	}
130 
SetPriority(unsigned long newPriority)131 	unsigned long Direct3DVolumeTexture8::SetPriority(unsigned long newPriority)
132 	{
133 		TRACE("");
134 
135 		return Direct3DBaseTexture8::SetPriority(newPriority);
136 	}
137 
GetPriority()138 	unsigned long Direct3DVolumeTexture8::GetPriority()
139 	{
140 		TRACE("");
141 
142 		return Direct3DBaseTexture8::GetPriority();
143 	}
144 
GetType()145 	D3DRESOURCETYPE Direct3DVolumeTexture8::GetType()
146 	{
147 		TRACE("");
148 
149 		return Direct3DBaseTexture8::GetType();
150 	}
151 
GetLevelCount()152 	unsigned long Direct3DVolumeTexture8::GetLevelCount()
153 	{
154 		TRACE("");
155 
156 		return Direct3DBaseTexture8::GetLevelCount();
157 	}
158 
GetLOD()159 	unsigned long Direct3DVolumeTexture8::GetLOD()
160 	{
161 		TRACE("");
162 
163 		return Direct3DBaseTexture8::GetLOD();
164 	}
165 
SetLOD(unsigned long newLOD)166 	unsigned long Direct3DVolumeTexture8::SetLOD(unsigned long newLOD)
167 	{
168 		TRACE("");
169 
170 		return Direct3DBaseTexture8::SetLOD(newLOD);
171 	}
172 
GetVolumeLevel(unsigned int level,IDirect3DVolume8 ** volume)173 	long Direct3DVolumeTexture8::GetVolumeLevel(unsigned int level, IDirect3DVolume8 **volume)
174 	{
175 		TRACE("");
176 
177 		*volume = 0;   // FIXME: Verify
178 
179 		if(level >= GetLevelCount() || !volumeLevel[level])
180 		{
181 			return INVALIDCALL();
182 		}
183 
184 		volumeLevel[level]->AddRef();
185 		*volume = volumeLevel[level];
186 
187 		return D3D_OK;
188 	}
189 
LockBox(unsigned int level,D3DLOCKED_BOX * lockedVolume,const D3DBOX * box,unsigned long flags)190 	long Direct3DVolumeTexture8::LockBox(unsigned int level, D3DLOCKED_BOX *lockedVolume, const D3DBOX *box, unsigned long flags)
191 	{
192 		TRACE("");
193 
194 		if(!lockedVolume || level >= GetLevelCount() || !volumeLevel[level])
195 		{
196 			return INVALIDCALL();
197 		}
198 
199 		return volumeLevel[level]->LockBox(lockedVolume, box, flags);
200 	}
201 
UnlockBox(unsigned int level)202 	long Direct3DVolumeTexture8::UnlockBox(unsigned int level)
203 	{
204 		TRACE("");
205 
206 		if(level >= GetLevelCount() || !volumeLevel[level])
207 		{
208 			return INVALIDCALL();
209 		}
210 
211 		return volumeLevel[level]->UnlockBox();
212 	}
213 
AddDirtyBox(const D3DBOX * dirtyBox)214 	long Direct3DVolumeTexture8::AddDirtyBox(const D3DBOX *dirtyBox)
215 	{
216 		TRACE("");
217 
218 		if(!dirtyBox)
219 		{
220 			return INVALIDCALL();
221 		}
222 
223 		UNIMPLEMENTED();
224 
225 		return D3D_OK;
226 	}
227 
GetLevelDesc(unsigned int level,D3DVOLUME_DESC * description)228 	long Direct3DVolumeTexture8::GetLevelDesc(unsigned int level, D3DVOLUME_DESC *description)
229 	{
230 		TRACE("");
231 
232 		if(!description || level >= GetLevelCount() || !volumeLevel[level])
233 		{
234 			return INVALIDCALL();
235 		}
236 
237 		volumeLevel[level]->GetDesc(description);
238 
239 		return D3D_OK;
240 	}
241 
getInternalVolumeLevel(unsigned int level)242 	Direct3DVolume8 *Direct3DVolumeTexture8::getInternalVolumeLevel(unsigned int level)
243 	{
244 		return volumeLevel[level];
245 	}
246 }
247