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 "Direct3DBaseTexture9.hpp"
16
17#include "Direct3DDevice9.hpp"
18#include "Resource.hpp"
19#include "Debug.hpp"
20
21#include <assert.h>
22
23namespace D3D9
24{
25	Direct3DBaseTexture9::Direct3DBaseTexture9(Direct3DDevice9 *device, D3DRESOURCETYPE type, D3DFORMAT format, D3DPOOL pool, unsigned long levels, unsigned long usage) : Direct3DResource9(device, type, pool, 0), format(format), levels(levels), usage(usage)
26	{
27		filterType = D3DTEXF_LINEAR;
28		LOD = 0;
29
30		resource = new sw::Resource(0);
31	}
32
33	Direct3DBaseTexture9::~Direct3DBaseTexture9()
34	{
35		resource->destruct();
36	}
37
38	long Direct3DBaseTexture9::QueryInterface(const IID &iid, void **object)
39	{
40		CriticalSection cs(device);
41
42		TRACE("");
43
44		if(iid == IID_IDirect3DBaseTexture9 ||
45		   iid == IID_IDirect3DResource9 ||
46		   iid == IID_IUnknown)
47		{
48			AddRef();
49			*object = this;
50
51			return S_OK;
52		}
53
54		*object = 0;
55
56		return NOINTERFACE(iid);
57	}
58
59	unsigned long Direct3DBaseTexture9::AddRef()
60	{
61		TRACE("");
62
63		return Direct3DResource9::AddRef();
64	}
65
66	unsigned long Direct3DBaseTexture9::Release()
67	{
68		TRACE("");
69
70		return Direct3DResource9::Release();
71	}
72
73	long Direct3DBaseTexture9::FreePrivateData(const GUID &guid)
74	{
75		CriticalSection cs(device);
76
77		TRACE("");
78
79		return Direct3DResource9::FreePrivateData(guid);
80	}
81
82	long Direct3DBaseTexture9::GetPrivateData(const GUID &guid, void *data, unsigned long *size)
83	{
84		CriticalSection cs(device);
85
86		TRACE("");
87
88		return Direct3DResource9::GetPrivateData(guid, data, size);
89	}
90
91	void Direct3DBaseTexture9::PreLoad()
92	{
93		CriticalSection cs(device);
94
95		TRACE("");
96
97		Direct3DResource9::PreLoad();
98	}
99
100	long Direct3DBaseTexture9::SetPrivateData(const GUID &guid, const void *data, unsigned long size, unsigned long flags)
101	{
102		CriticalSection cs(device);
103
104		TRACE("");
105
106		return Direct3DResource9::SetPrivateData(guid, data, size, flags);
107	}
108
109	long Direct3DBaseTexture9::GetDevice(IDirect3DDevice9 **device)
110	{
111		CriticalSection cs(this->device);
112
113		TRACE("");
114
115		return Direct3DResource9::GetDevice(device);
116	}
117
118	unsigned long Direct3DBaseTexture9::SetPriority(unsigned long newPriority)
119	{
120		CriticalSection cs(device);
121
122		TRACE("");
123
124		return Direct3DResource9::SetPriority(newPriority);
125	}
126
127	unsigned long Direct3DBaseTexture9::GetPriority()
128	{
129		CriticalSection cs(device);
130
131		TRACE("");
132
133		return Direct3DResource9::GetPriority();
134	}
135
136	D3DRESOURCETYPE Direct3DBaseTexture9::GetType()
137	{
138		CriticalSection cs(device);
139
140		TRACE("");
141
142		return Direct3DResource9::GetType();
143	}
144
145	D3DTEXTUREFILTERTYPE Direct3DBaseTexture9::GetAutoGenFilterType()
146	{
147		CriticalSection cs(device);
148
149		TRACE("");
150
151		if(usage & D3DUSAGE_AUTOGENMIPMAP)
152		{
153			return filterType;
154		}
155		else
156		{
157			return D3DTEXF_NONE;
158		}
159	}
160
161	unsigned long Direct3DBaseTexture9::GetLevelCount()
162	{
163		CriticalSection cs(device);
164
165		TRACE("");
166
167		if(usage & D3DUSAGE_AUTOGENMIPMAP)
168		{
169			return 1;
170		}
171
172		return levels;
173	}
174
175	unsigned long Direct3DBaseTexture9::GetLOD()
176	{
177		CriticalSection cs(device);
178
179		TRACE("");
180
181		if(pool & D3DPOOL_MANAGED)
182		{
183			return LOD;
184		}
185		else
186		{
187			return 0;
188		}
189	}
190
191	long Direct3DBaseTexture9::SetAutoGenFilterType(D3DTEXTUREFILTERTYPE filterType)
192	{
193		CriticalSection cs(device);
194
195		TRACE("");
196
197		if(usage & D3DUSAGE_AUTOGENMIPMAP)
198		{
199			this->filterType = filterType;   // FIXME: Check if valid
200			// FIXME: Dirty the mipmap chain
201
202			return D3D_OK;
203		}
204		else
205		{
206			return D3DTEXF_NONE;
207		}
208	}
209
210	unsigned long Direct3DBaseTexture9::SetLOD(unsigned long newLOD)
211	{
212		CriticalSection cs(device);
213
214		TRACE("");
215
216		unsigned long oldLOD = LOD;
217		LOD = newLOD < levels ? newLOD : levels - 1;
218
219		if(pool & D3DPOOL_MANAGED)
220		{
221			return oldLOD;
222		}
223		else
224		{
225			return 0;
226		}
227	}
228
229	void Direct3DBaseTexture9::GenerateMipSubLevels()
230	{
231		CriticalSection cs(device);
232
233		TRACE("");
234	}
235
236	sw::Resource *Direct3DBaseTexture9::getResource() const
237	{
238		return resource;
239	}
240
241	unsigned long Direct3DBaseTexture9::getInternalLevelCount() const
242	{
243		return levels;
244	}
245
246	unsigned long Direct3DBaseTexture9::getUsage() const
247	{
248		return usage;
249	}
250
251	D3DFORMAT Direct3DBaseTexture9::getFormat() const
252	{
253		return format;
254	}
255}
256