• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef D3D8_Direct3DDevice8_hpp
16 #define D3D8_Direct3DDevice8_hpp
17 
18 #include "Unknown.hpp"
19 
20 #include "Direct3D8.hpp"
21 #include "Direct3DStateBlock8.hpp"
22 #include "Direct3DVertexDeclaration8.hpp"
23 #include "Direct3DSwapChain8.hpp"
24 
25 #include "Stream.hpp"
26 
27 #include <d3d8.h>
28 #include <vector>
29 #include <list>
30 #include <map>
31 
32 namespace sw
33 {
34 	class Renderer;
35 	class Context;
36 }
37 
38 namespace D3D8
39 {
40 	class Direct3DPixelShader8;
41 	class Direct3DVertexShader8;
42 	class Direct3DSurface8;
43 	class Direct3DVertexBuffer8;
44 	class Direct3DIndexBuffer8;
45 
46 	class Direct3DDevice8 : public IDirect3DDevice8, protected Unknown
47 	{
48 	public:
49 		Direct3DDevice8(const HINSTANCE instance, Direct3D8 *d3d8, unsigned int adapter, D3DDEVTYPE deviceType, HWND focusWindow, unsigned long behaviourFlags, D3DPRESENT_PARAMETERS *presentParameters);
50 
51 		~Direct3DDevice8() override;
52 
53 		// IUnknown methods
54 		long __stdcall QueryInterface(const IID &iid, void **object) override;
55 		unsigned long __stdcall AddRef() override;
56 		unsigned long __stdcall Release() override;
57 
58 		// IDirect3DDevice8 methods
59 		long __stdcall ApplyStateBlock(unsigned long token) override;
60 		long __stdcall BeginScene() override;
61 		long __stdcall BeginStateBlock() override;
62 		long __stdcall CaptureStateBlock(unsigned long token) override;
63 		long __stdcall Clear(unsigned long count, const D3DRECT *rects, unsigned long flags, unsigned long color, float z, unsigned long stencil) override;
64 		long __stdcall CopyRects(IDirect3DSurface8 *sourceSurface, const RECT *sourceRectsArray, unsigned int rects, IDirect3DSurface8 *destinationSurface, const POINT *destPointsArray) override;
65 		long __stdcall CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *presentParameters, IDirect3DSwapChain8 **swapChain) override;
66 		long __stdcall CreateCubeTexture(unsigned int edgeLength, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **cubeTexture) override;
67 		long __stdcall CreateDepthStencilSurface(unsigned int width, unsigned int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multiSample, IDirect3DSurface8 **surface) override;
68 		long __stdcall CreateImageSurface(unsigned int width, unsigned int height, D3DFORMAT format, IDirect3DSurface8 **surface) override;
69 		long __stdcall CreateIndexBuffer(unsigned int length, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **indexBuffer) override;
70 		long __stdcall CreatePixelShader(const unsigned long *function, unsigned long *handle) override;
71 		long __stdcall CreateRenderTarget(unsigned int width, unsigned int height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multiSample, int lockable, IDirect3DSurface8 **surface) override;
72 		long __stdcall CreateStateBlock(D3DSTATEBLOCKTYPE type, unsigned long *token) override;
73 		long __stdcall CreateTexture(unsigned int width, unsigned int height, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DTexture8 **texture) override;
74 		long __stdcall CreateVertexBuffer(unsigned int length, unsigned long usage, unsigned long FVF, D3DPOOL, IDirect3DVertexBuffer8 **vertexBuffer) override;
75 		long __stdcall CreateVertexShader(const unsigned long *declaration, const unsigned long *function, unsigned long *handle, unsigned long usage) override;
76 		long __stdcall CreateVolumeTexture(unsigned int width, unsigned int height, unsigned int depth, unsigned int levels, unsigned long usage, D3DFORMAT format, D3DPOOL pool, IDirect3DVolumeTexture8 **volumeTexture) override;
77 		long __stdcall DeletePatch(unsigned int handle) override;
78 		long __stdcall DeletePixelShader(unsigned long handle) override;
79 		long __stdcall DeleteStateBlock(unsigned long token) override;
80 		long __stdcall DeleteVertexShader(unsigned long handle) override;
81 		long __stdcall DrawIndexedPrimitive(D3DPRIMITIVETYPE type, unsigned int minIndex, unsigned int numVertices, unsigned int startIndex, unsigned int primitiveCount) override;
82 		long __stdcall DrawIndexedPrimitiveUP(D3DPRIMITIVETYPE type, unsigned int minVertexIndex, unsigned int numVertexIndices, unsigned int PrimitiveCount, const void *indexData, D3DFORMAT indexDataFormat, const void *vertexStreamZeroData, unsigned int VertexStreamZeroStride) override;
83 		long __stdcall DrawPrimitive(D3DPRIMITIVETYPE primitiveType, unsigned int startVertex, unsigned int primiveCount) override;
84 		long __stdcall DrawPrimitiveUP(D3DPRIMITIVETYPE primitiveType, unsigned int primitiveCount, const void *vertexStreamZeroData, unsigned int vertexStreamZeroStride) override;
85 		long __stdcall DrawRectPatch(unsigned int handle, const float *numSegs, const D3DRECTPATCH_INFO *rectPatchInfo) override;
86 		long __stdcall DrawTriPatch(unsigned int handle, const float *numSegs, const D3DTRIPATCH_INFO *triPatchInfo) override;
87 		long __stdcall EndScene() override;
88 		long __stdcall EndStateBlock(unsigned long *token) override;
89 		unsigned int __stdcall GetAvailableTextureMem() override;
90 		long __stdcall GetBackBuffer(unsigned int index, D3DBACKBUFFER_TYPE type, IDirect3DSurface8 **backBuffer) override;
91 		long __stdcall GetClipPlane(unsigned long index, float *plane) override;
92 		long __stdcall GetClipStatus(D3DCLIPSTATUS8 *clipStatus) override;
93 		long __stdcall GetCreationParameters(D3DDEVICE_CREATION_PARAMETERS *parameters) override;
94 		long __stdcall GetCurrentTexturePalette(unsigned int *paletteNumber) override;
95 		long __stdcall GetDepthStencilSurface(IDirect3DSurface8 **depthStencilSurface) override;
96 		long __stdcall GetDeviceCaps(D3DCAPS8 *caps) override;
97 		long __stdcall GetDirect3D(IDirect3D8 **D3D) override;
98 		long __stdcall GetDisplayMode(D3DDISPLAYMODE *mode) override;
99 		long __stdcall GetFrontBuffer(IDirect3DSurface8 *destSurface) override;
100 		void __stdcall GetGammaRamp(D3DGAMMARAMP *ramp) override;
101 		long __stdcall GetIndices(IDirect3DIndexBuffer8 **indexData, unsigned int *baseVertexIndex) override;
102 		long __stdcall GetInfo(unsigned long devInfoID, void *devInfoStruct, unsigned long devInfoStructSize) override;
103 		long __stdcall GetLight(unsigned long index, D3DLIGHT8 *p) override;
104 		long __stdcall GetLightEnable(unsigned long index , int *enable) override;
105 		long __stdcall GetMaterial(D3DMATERIAL8 *material) override;
106 		long __stdcall GetPaletteEntries(unsigned int paletteNumber, PALETTEENTRY *entries) override;
107 		long __stdcall GetPixelShader(unsigned long *handle) override;
108 		long __stdcall GetPixelShaderFunction(unsigned long handle, void *data, unsigned long *sizeOfData) override;
109 		long __stdcall GetPixelShaderConstant(unsigned long startRegister, void *constantData, unsigned long constantCount) override;
110 		long __stdcall GetRasterStatus(D3DRASTER_STATUS *rasterStatus) override;
111 		long __stdcall GetRenderState(D3DRENDERSTATETYPE State, unsigned long *value) override;
112 		long __stdcall GetRenderTarget(IDirect3DSurface8 **renderTarget) override;
113 		long __stdcall GetStreamSource(unsigned int streamNumber, IDirect3DVertexBuffer8 **streamData, unsigned int *stride) override;
114 		long __stdcall GetTexture(unsigned long stage, IDirect3DBaseTexture8 **texture) override;
115 		long __stdcall GetTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long *value) override;
116 		long __stdcall GetTransform(D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix) override;
117 		long __stdcall GetVertexShader(unsigned long *handle) override;
118 		long __stdcall GetVertexShaderConstant(unsigned long startRegister, void *constantData, unsigned long constantCount) override;
119 		long __stdcall GetVertexShaderDeclaration(unsigned long handle, void *data, unsigned long *size) override;
120 		long __stdcall GetVertexShaderFunction(unsigned long handle, void *data, unsigned long *size) override;
121 		long __stdcall GetViewport(D3DVIEWPORT8 *viewport) override;
122 		long __stdcall LightEnable(unsigned long index, int enable) override;
123 		long __stdcall MultiplyTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix) override;
124 		long __stdcall Present(const RECT *sourceRect, const RECT *destRect, HWND destWindowOverride, const RGNDATA *dirtyRegion) override;
125 		long __stdcall ProcessVertices(unsigned int srcStartIndex, unsigned int destIndex, unsigned int vertexCount, IDirect3DVertexBuffer8 *destBuffer, unsigned long flags) override;
126 		long __stdcall Reset(D3DPRESENT_PARAMETERS *presentParameters) override;
127 		long __stdcall ResourceManagerDiscardBytes(unsigned long bytes) override;
128 		long __stdcall SetClipPlane(unsigned long index, const float *plane) override;
129 		long __stdcall SetClipStatus(const D3DCLIPSTATUS8 *clipStatus) override;
130 		long __stdcall SetCurrentTexturePalette(unsigned int paletteNumber) override;
131 		void __stdcall SetCursorPosition(int x, int y, unsigned long flags) override;
132 		long __stdcall SetCursorProperties(unsigned int x, unsigned int y, IDirect3DSurface8 *cursorBitmap) override;
133 		void __stdcall SetGammaRamp(unsigned long flags, const D3DGAMMARAMP *ramp) override;
134 		long __stdcall SetIndices(IDirect3DIndexBuffer8 *indexData, unsigned int baseVertexIndex) override;
135 		long __stdcall SetLight(unsigned long index, const D3DLIGHT8 *light) override;
136 		long __stdcall SetMaterial(const D3DMATERIAL8 *material) override;
137 		long __stdcall SetPaletteEntries(unsigned int paletteNumber, const PALETTEENTRY *entries) override;
138 		long __stdcall SetPixelShader(unsigned long shader) override;
139 		long __stdcall SetPixelShaderConstant(unsigned long startRegister, const void *constantData, unsigned long constantCount) override;
140 		long __stdcall SetRenderState(D3DRENDERSTATETYPE state, unsigned long value) override;
141 		long __stdcall SetRenderTarget(IDirect3DSurface8 *renderTarget, IDirect3DSurface8 *newZStencil) override;
142 		long __stdcall SetStreamSource(unsigned int streamNumber, IDirect3DVertexBuffer8 *streamData, unsigned int stride) override;
143 		long __stdcall SetTexture(unsigned long stage, IDirect3DBaseTexture8 *texture) override;
144 		long __stdcall SetTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long value) override;
145 		long __stdcall SetTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix) override;
146 		long __stdcall SetVertexShader(unsigned long handle) override;
147 		long __stdcall SetVertexShaderConstant(unsigned long startRegister, const void *constantData, unsigned long constantCount) override;
148 		long __stdcall SetViewport(const D3DVIEWPORT8 *viewport) override;
149 		int __stdcall ShowCursor(int show) override;
150 		long __stdcall TestCooperativeLevel() override;
151 		long __stdcall UpdateTexture(IDirect3DBaseTexture8 *sourceTexture, IDirect3DBaseTexture8 *destinationTexture) override;
152 		long __stdcall ValidateDevice(unsigned long *numPasses) override;
153 
154 		// Internal methods
155 		long __stdcall updateSurface(IDirect3DSurface8 *sourceSurface, const RECT *sourceRect, IDirect3DSurface8 *destinationSurface, const POINT *destPoint);
156 
157 	private:
158 		static int FVFStride(unsigned long FVF);
159 		static int typeStride(unsigned char streamType);
160 		static sw::StreamType streamType(int type);
161 		bool bindData(Direct3DIndexBuffer8 *indexBuffer, int base);
162 		void bindStreams(int base);
163 		void bindIndexBuffer(Direct3DIndexBuffer8 *indexBuffer);
164 		void bindLights();
165 		bool bindViewport();
166 		void bindTextures();
167 		void bindCursor();
168 
169 		long updateVolume(IDirect3DVolume8 *sourceVolume, IDirect3DVolume8 *destinationVolume);
170 		void configureFPU();
171 
172 		// Creation parameters
173 		const HINSTANCE instance;
174 		Direct3D8 *d3d8;
175 		const unsigned int adapter;
176 		const D3DDEVTYPE deviceType;
177 		const HWND focusWindow;
178 		const unsigned long behaviourFlags;
179 		const D3DPRESENT_PARAMETERS presentParameters;
180 
181 		HWND windowHandle;
182 
183 		D3DVIEWPORT8 viewport;
184 		D3DMATRIX matrix[512];
185 		Direct3DBaseTexture8 *texture[8];
186 		D3DMATERIAL8 material;
187 		float plane[6][4];
188 		D3DCLIPSTATUS8 clipStatus;
189 
190 		struct Light : D3DLIGHT8
191 		{
operator =D3D8::Direct3DDevice8::Light192 			Light &operator=(const D3DLIGHT8 &light)
193 			{
194 				Type = light.Type;
195 				Diffuse = light.Diffuse;
196 				Specular = light.Specular;
197 				Ambient = light.Ambient;
198 				Position = light.Position;
199 				Direction = light.Direction;
200 				Range = light.Range;
201 				Falloff = light.Falloff;
202 				Attenuation0 = light.Attenuation0;
203 				Attenuation1 = light.Attenuation1;
204 				Attenuation2 = light.Attenuation2;
205 				Theta = light.Theta;
206 				Phi = light.Phi;
207 
208 				return *this;
209 			}
210 
211 			bool enable;
212 		};
213 
214 		struct Lights : std::map<int, Light>
215 		{
existsD3D8::Direct3DDevice8::Lights216 			bool exists(int index)
217 			{
218 				return find(index) != end();
219 			}
220 		};
221 
222 		Lights light;
223 		bool lightsDirty;
224 
225 		Direct3DVertexBuffer8 *dataStream[16];
226 		int streamStride[16];
227 		Direct3DIndexBuffer8 *indexData;
228 		unsigned int baseVertexIndex;
229 
230 		unsigned long FVF;
231 
232 		std::vector<Direct3DSwapChain8*> swapChain;
233 
234 		Direct3DSurface8 *renderTarget;
235 		Direct3DSurface8 *depthStencil;
236 
237 		bool recordState;
238 		std::vector<Direct3DStateBlock8*> stateRecorder;
239 
240 		unsigned long renderState[D3DRS_NORMALORDER + 1];
241 		unsigned long textureStageState[8][D3DTSS_RESULTARG + 1];
242 		bool init;   // TODO: Deprecate when all state changes implemented
243 
244 		std::vector<Direct3DPixelShader8*> pixelShader;
245 		std::vector<Direct3DVertexShader8*> vertexShader;
246 		unsigned long pixelShaderHandle;
247 		unsigned long vertexShaderHandle;
248 		const unsigned long *declaration;
249 
250 		float pixelShaderConstant[8][4];
251 		float vertexShaderConstant[256][4];
252 
253 		struct Palette
254 		{
255 			PALETTEENTRY entry[256];
256 		};
257 
258 		unsigned int currentPalette;
259 		std::map<int, Palette> palette;
260 
261 		sw::Context *context;
262 		sw::Renderer *renderer;
263 
264 		sw::Surface *cursor;
265 		bool showCursor;
266 		HCURSOR nullCursor;
267 		HCURSOR win32Cursor;
268 	};
269 }
270 
271 #endif   // D3D8_Direct3DDevice8_hpp
272