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 D3D9_Direct3DStateBlock9_hpp 16 #define D3D9_Direct3DStateBlock9_hpp 17 18 #include "Direct3DDevice9.hpp" 19 #include "Unknown.hpp" 20 21 #include <vector> 22 23 #include <d3d9.h> 24 25 namespace D3D9 26 { 27 class Direct3DDevice9; 28 class Direct3DVertexDeclaration9; 29 class Direct3DIndexBuffer9; 30 class Direct3DVertexBuffer9; 31 class Direct3DBaseTexture9; 32 class Direct3DPixelShader9; 33 class Direct3DVertexShader9; 34 35 class Direct3DStateBlock9 : public IDirect3DStateBlock9, public Unknown 36 { 37 public: 38 Direct3DStateBlock9(Direct3DDevice9 *device, D3DSTATEBLOCKTYPE type); 39 40 ~Direct3DStateBlock9() override; 41 42 // IUnknown methods 43 long __stdcall QueryInterface(const IID &iid, void **object) override; 44 unsigned long __stdcall AddRef() override; 45 unsigned long __stdcall Release() override; 46 47 // IDirect3DStateBlock9 methods 48 long __stdcall Apply() override; 49 long __stdcall Capture() override; 50 long __stdcall GetDevice(IDirect3DDevice9 **device) override; 51 52 // Internal methods 53 void lightEnable(unsigned long index, int enable); 54 void setClipPlane(unsigned long index, const float *plane); 55 void setCurrentTexturePalette(unsigned int paletteNumber); 56 void setFVF(unsigned long FVF); 57 void setIndices(Direct3DIndexBuffer9 *indexData); 58 void setLight(unsigned long index, const D3DLIGHT9 *light); 59 void setMaterial(const D3DMATERIAL9 *material); 60 void setNPatchMode(float segments); 61 void setPixelShader(Direct3DPixelShader9 *shader); 62 void setPixelShaderConstantB(unsigned int startRegister, const int *constantData, unsigned int count); 63 void setPixelShaderConstantF(unsigned int startRegister, const float *constantData, unsigned int count); 64 void setPixelShaderConstantI(unsigned int startRegister, const int *constantData, unsigned int count); 65 void setRenderState(D3DRENDERSTATETYPE state, unsigned long value); 66 void setSamplerState(unsigned long index, D3DSAMPLERSTATETYPE state, unsigned long value); 67 void setScissorRect(const RECT *rect); 68 void setStreamSource(unsigned int stream, Direct3DVertexBuffer9 *data, unsigned int offset, unsigned int stride); 69 void setStreamSourceFreq(unsigned int streamNumber, unsigned int divider); 70 void setTexture(unsigned long index, Direct3DBaseTexture9 *texture); 71 void setTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type, unsigned long value); 72 void setTransform(D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix); 73 void setViewport(const D3DVIEWPORT9 *viewport); 74 void setVertexDeclaration(Direct3DVertexDeclaration9 *declaration); 75 void setVertexShader(Direct3DVertexShader9 *shader); 76 void setVertexShaderConstantB(unsigned int startRegister, const int *constantData, unsigned int count); 77 void setVertexShaderConstantF(unsigned int startRegister, const float *constantData, unsigned int count); 78 void setVertexShaderConstantI(unsigned int startRegister, const int *constantData, unsigned int count); 79 80 private: 81 // Individual states 82 void captureRenderState(D3DRENDERSTATETYPE state); 83 void captureSamplerState(unsigned long index, D3DSAMPLERSTATETYPE state); 84 void captureTextureStageState(unsigned long stage, D3DTEXTURESTAGESTATETYPE type); 85 void captureTransform(D3DTRANSFORMSTATETYPE state); 86 87 // Pixel states 88 void capturePixelRenderStates(); 89 void capturePixelTextureStates(); 90 void capturePixelSamplerStates(); 91 void capturePixelShaderStates(); 92 93 // Vertex states 94 void captureVertexRenderStates(); 95 void captureVertexSamplerStates(); 96 void captureVertexTextureStates(); 97 void captureNPatchMode(); 98 void captureLightStates(); 99 void captureVertexShaderStates(); 100 void captureStreamSourceFrequencies(); 101 void captureVertexDeclaration(); 102 void captureFVF(); 103 104 // All (remaining) states 105 void captureTextures(); 106 void captureTexturePalette(); 107 void captureVertexStreams(); 108 void captureIndexBuffer(); 109 void captureViewport(); 110 void captureScissorRectangle(); 111 void captureTransforms(); 112 void captureTextureTransforms(); 113 void captureClippingPlanes(); 114 void captureMaterial(); 115 116 // Creation parameters 117 Direct3DDevice9 *const device; 118 const D3DSTATEBLOCKTYPE type; 119 120 // State data 121 bool vertexDeclarationCaptured; 122 Direct3DVertexDeclaration9 *vertexDeclaration; 123 124 bool fvfCaptured; 125 unsigned long FVF; 126 127 bool indexBufferCaptured; 128 Direct3DIndexBuffer9 *indexBuffer; 129 130 bool renderStateCaptured[D3DRS_BLENDOPALPHA + 1]; 131 unsigned long renderState[D3DRS_BLENDOPALPHA + 1]; 132 133 bool nPatchModeCaptured; 134 float nPatchMode; 135 136 bool textureStageStateCaptured[8][D3DTSS_CONSTANT + 1]; 137 unsigned long textureStageState[8][D3DTSS_CONSTANT + 1]; 138 139 bool samplerStateCaptured[16 + 4][D3DSAMP_DMAPOFFSET + 1]; 140 unsigned long samplerState[16 + 4][D3DSAMP_DMAPOFFSET + 1]; 141 142 bool streamSourceCaptured[MAX_VERTEX_INPUTS]; 143 struct StreamSource 144 { 145 Direct3DVertexBuffer9 *vertexBuffer; 146 unsigned int offset; 147 unsigned int stride; 148 }; 149 StreamSource streamSource[MAX_VERTEX_INPUTS]; 150 151 bool streamSourceFrequencyCaptured[MAX_VERTEX_INPUTS]; 152 unsigned int streamSourceFrequency[MAX_VERTEX_INPUTS]; 153 154 bool textureCaptured[16 + 4]; 155 Direct3DBaseTexture9 *texture[16 + 4]; 156 157 bool transformCaptured[512]; 158 D3DMATRIX transform[512]; 159 160 bool materialCaptured; 161 D3DMATERIAL9 material; 162 163 bool lightCaptured[8]; // FIXME: Unlimited index 164 D3DLIGHT9 light[8]; 165 166 bool lightEnableCaptured[8]; // FIXME: Unlimited index 167 int lightEnableState[8]; 168 169 bool pixelShaderCaptured; 170 Direct3DPixelShader9 *pixelShader; 171 172 bool vertexShaderCaptured; 173 Direct3DVertexShader9 *vertexShader; 174 175 bool viewportCaptured; 176 D3DVIEWPORT9 viewport; 177 178 float pixelShaderConstantF[MAX_PIXEL_SHADER_CONST][4]; 179 int pixelShaderConstantI[16][4]; 180 int pixelShaderConstantB[16]; 181 182 float vertexShaderConstantF[MAX_VERTEX_SHADER_CONST][4]; 183 int vertexShaderConstantI[16][4]; 184 int vertexShaderConstantB[16]; 185 186 bool clipPlaneCaptured[6]; 187 float clipPlane[6][4]; 188 189 bool scissorRectCaptured; 190 RECT scissorRect; 191 192 bool paletteNumberCaptured; 193 unsigned int paletteNumber; 194 195 void clear(); 196 }; 197 } 198 199 #endif // D3D9_Direct3DStateBlock9_hpp 200