Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

498 lines
14 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 2000.
  3. //
  4. // pshader.h
  5. //
  6. // Direct3D Reference Device - Pixel Shader
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _PSHADER_H
  10. #define _PSHADER_H
  11. class RefRast;
  12. //---------------------------------------------------------------------
  13. // Constants
  14. //---------------------------------------------------------------------
  15. const DWORD RD_MAX_TEXTURE_STAGES = D3DHAL_TSS_MAXSTAGES;
  16. const DWORD RD_MAX_COISSUED_INSTRUCTIONS = 2;
  17. // version 1.1 register bank sizes
  18. const DWORD RDPS_MAX_NUMTEMPREG_V255 = 6;
  19. const DWORD RDPS_MAX_NUMINPUTREG_V255 = 8;
  20. const DWORD RDPS_MAX_NUMCONSTREG_V255 = 16;
  21. const DWORD RDPS_MAX_NUMTEXTUREREG_V255 = 8;
  22. // version-independent consts for sizing arrays
  23. const DWORD RDPS_MAX_NUMTEMPREG = RDPS_MAX_NUMTEMPREG_V255;
  24. const DWORD RDPS_MAX_NUMINPUTREG = RDPS_MAX_NUMINPUTREG_V255;
  25. const DWORD RDPS_MAX_NUMCONSTREG = RDPS_MAX_NUMCONSTREG_V255;
  26. const DWORD RDPS_MAX_NUMTEXTUREREG = RDPS_MAX_NUMTEXTUREREG_V255;
  27. // sizes for internal register arrays
  28. const DWORD RDPS_MAX_NUMQUEUEDWRITEREG = RD_MAX_COISSUED_INSTRUCTIONS - 1;
  29. const DWORD RDPS_MAX_NUMPOSTMODSRCREG = 3;
  30. const DWORD RDPS_MAX_NUMSCRATCHREG = 5;
  31. // refdev-specific pixel shader 'instructions' to match legacy pixel processing
  32. #define D3DSIO_TEXBEM_LEGACY ((D3DSHADER_INSTRUCTION_OPCODE_TYPE)0xC001)
  33. #define D3DSIO_TEXBEML_LEGACY ((D3DSHADER_INSTRUCTION_OPCODE_TYPE)0xC002)
  34. //---------------------------------------------------------------------
  35. //
  36. // pshader.cpp
  37. // Structure that describes each D3DSIO_ pixelshader instruction
  38. typedef struct _PixelShaderInstruction
  39. {
  40. char Text[D3DDM_MAX_PSINSTSTRING];
  41. DWORD* pComment;
  42. DWORD CommentSize;
  43. // instruction tokens
  44. DWORD Opcode;
  45. DWORD DstParam;
  46. DWORD SrcParam[3];
  47. DWORD SrcParamCount;
  48. UINT uiTSSNum;
  49. BOOL bTexOp;
  50. BOOL bQueueWrite;
  51. BOOL bFlushQueue; // flush write - TRUE for all singly issued instructions,
  52. // and for the last in any sequence of co-issued instructions.
  53. } PixelShaderInstruction;
  54. // Enum listing refrast's pixelshader register files
  55. typedef enum _RDPS_REGISTER_TYPE
  56. {
  57. RDPSREG_UNINITIALIZED_TYPE = 0,
  58. RDPSREG_INPUT,
  59. RDPSREG_TEMP,
  60. RDPSREG_CONST,
  61. RDPSREG_TEXTURE,
  62. RDPSREG_POSTMODSRC,
  63. RDPSREG_SCRATCH,
  64. RDPSREG_QUEUEDWRITE,
  65. RDPSREG_ZERO,
  66. RDPSREG_ONE,
  67. RDPSREG_TWO,
  68. } RDPS_REGISTER_TYPE;
  69. // Type that is a pointer to an array of RGBA vectors.
  70. typedef FLOAT (*PRGBAVEC)[4];
  71. // Type used to refer to a register.
  72. class RDPSRegister
  73. {
  74. private:
  75. RDPS_REGISTER_TYPE m_RegType;
  76. UINT m_RegNum;
  77. PRGBAVEC m_pReg; // pointer to [4][4] array -> 4 pixel RGBA
  78. // this is computed when m_RegType and m_RegNum are set
  79. public:
  80. RDPSRegister() {m_pReg = NULL; m_RegType = RDPSREG_UNINITIALIZED_TYPE; m_RegNum = (UINT)-1;}
  81. void Set(RDPS_REGISTER_TYPE RegType, UINT RegNum, RefRast* pRast);
  82. inline RDPS_REGISTER_TYPE GetRegType() {return m_RegType;}
  83. inline UINT GetRegNum() {return m_RegNum;}
  84. inline PRGBAVEC GetRegPtr() {return m_pReg;}
  85. };
  86. // "RISC" opcodes which are used to implement D3DSIO_ API pixelshader instructions
  87. typedef enum _RDPS_INSTRUCTION_OPCODE_TYPE
  88. {
  89. RDPSINST_EVAL,
  90. RDPSINST_SAMPLE,
  91. RDPSINST_KILL,
  92. RDPSINST_BEM,
  93. RDPSINST_LUMINANCE,
  94. RDPSINST_DEPTH,
  95. RDPSINST_SRCMOD,
  96. RDPSINST_SWIZZLE,
  97. RDPSINST_DSTMOD,
  98. RDPSINST_MOV,
  99. RDPSINST_RCP,
  100. RDPSINST_FRC,
  101. RDPSINST_ADD,
  102. RDPSINST_SUB,
  103. RDPSINST_MUL,
  104. RDPSINST_DP3,
  105. RDPSINST_DP4,
  106. RDPSINST_MAD,
  107. RDPSINST_LRP,
  108. RDPSINST_CND,
  109. RDPSINST_CMP,
  110. RDPSINST_END,
  111. RDPSINST_TEXCOVERAGE,
  112. RDPSINST_QUADLOOPBEGIN,
  113. RDPSINST_QUADLOOPEND,
  114. RDPSINST_QUEUEWRITE,
  115. RDPSINST_FLUSHQUEUE,
  116. RDPSINST_NEXTD3DPSINST,
  117. } RDPS_INSTRUCTION_OPCODE_TYPE;
  118. // Structures defining the parameters for all the "RISC" opcodes listed above.
  119. // RDPSINST_BASE_PARAMS is the root from which the rest are inherited.
  120. typedef struct _RDPSINST_BASE_PARAMS
  121. {
  122. public:
  123. union{
  124. RDPS_INSTRUCTION_OPCODE_TYPE Inst;
  125. // Force structure alignment to pointer-size multiples.
  126. // IA64 (at least) needs this for structure packing to work.
  127. void* AlignmentDummy;
  128. };
  129. } RDPSINST_BASE_PARAMS;
  130. typedef struct _RDPSINST_EVAL_PARAMS : public RDPSINST_BASE_PARAMS
  131. {
  132. RDPSRegister DstReg;
  133. UINT uiCoordSet;
  134. BOOL bIgnoreD3DTTFF_PROJECTED;
  135. BOOL bClamp;
  136. } RDPSINST_EVAL_PARAMS;
  137. typedef struct _RDPSINST_SAMPLE_PARAMS : public RDPSINST_BASE_PARAMS
  138. {
  139. RDPSRegister DstReg;
  140. RDPSRegister CoordReg;
  141. UINT uiStage;
  142. } RDPSINST_SAMPLE_PARAMS;
  143. typedef struct _RDPSINST_BEM_PARAMS : public RDPSINST_BASE_PARAMS
  144. {
  145. RDPSRegister DstReg;
  146. RDPSRegister SrcReg0;
  147. RDPSRegister SrcReg1;
  148. BOOL bSrcReg0_Negate;
  149. BOOL bSrcReg1_Negate;
  150. BYTE WriteMask;
  151. UINT uiStage;
  152. } RDPSINST_BEM_PARAMS;
  153. typedef struct _RDPSINST_LUMINANCE_PARAMS : public RDPSINST_BASE_PARAMS
  154. {
  155. RDPSRegister DstReg;
  156. RDPSRegister SrcReg0;
  157. RDPSRegister SrcReg1;
  158. BOOL bSrcReg0_Negate;
  159. BOOL bSrcReg1_Negate;
  160. UINT uiStage;
  161. } RDPSINST_LUMINANCE_PARAMS;
  162. typedef struct _RDPSINST_DEPTH_PARAMS : public RDPSINST_BASE_PARAMS
  163. {
  164. RDPSRegister DstReg;
  165. } RDPSINST_DEPTH_PARAMS;
  166. typedef struct _RDPSINST_KILL_PARAMS : public RDPSINST_BASE_PARAMS
  167. {
  168. RDPSRegister DstReg;
  169. } RDPSINST_KILL_PARAMS;
  170. typedef struct _RDPSINST_SRCMOD_PARAMS : public RDPSINST_BASE_PARAMS
  171. {
  172. RDPSRegister DstReg;
  173. RDPSRegister SrcReg0;
  174. BYTE WriteMask;
  175. BOOL bBias;
  176. BOOL bTimes2;
  177. BOOL bComplement;
  178. FLOAT fRangeMin;
  179. FLOAT fRangeMax;
  180. } RDPSINST_SRCMOD_PARAMS;
  181. typedef struct _RDPSINST_SWIZZLE_PARAMS : public RDPSINST_BASE_PARAMS
  182. {
  183. RDPSRegister DstReg;
  184. RDPSRegister SrcReg0;
  185. BYTE WriteMask;
  186. BYTE Swizzle;
  187. } RDPSINST_SWIZZLE_PARAMS;
  188. typedef struct _RDPSINST_DSTMOD_PARAMS : public RDPSINST_BASE_PARAMS
  189. {
  190. RDPSRegister DstReg;
  191. BYTE WriteMask;
  192. FLOAT fScale;
  193. FLOAT fRangeMin;
  194. FLOAT fRangeMax;
  195. } RDPSINST_DSTMOD_PARAMS;
  196. typedef struct _RDPSINST_MOV_PARAMS : public RDPSINST_BASE_PARAMS
  197. {
  198. RDPSRegister DstReg;
  199. RDPSRegister SrcReg0;
  200. BOOL bSrcReg0_Negate;
  201. BYTE WriteMask;
  202. } RDPSINST_MOV_PARAMS;
  203. typedef struct _RDPSINST_FRC_PARAMS : public RDPSINST_BASE_PARAMS
  204. {
  205. RDPSRegister DstReg;
  206. RDPSRegister SrcReg0;
  207. BOOL bSrcReg0_Negate;
  208. BYTE WriteMask;
  209. } RDPSINST_FRC_PARAMS;
  210. typedef struct _RDPSINST_RCP_PARAMS : public RDPSINST_BASE_PARAMS
  211. {
  212. RDPSRegister DstReg;
  213. RDPSRegister SrcReg0;
  214. BOOL bSrcReg0_Negate;
  215. BYTE WriteMask;
  216. } RDPSINST_RCP_PARAMS;
  217. typedef struct _RDPSINST_ADD_PARAMS : public RDPSINST_BASE_PARAMS
  218. {
  219. RDPSRegister DstReg;
  220. RDPSRegister SrcReg0;
  221. RDPSRegister SrcReg1;
  222. BOOL bSrcReg0_Negate;
  223. BOOL bSrcReg1_Negate;
  224. BYTE WriteMask;
  225. } RDPSINST_ADD_PARAMS;
  226. typedef struct _RDPSINST_SUB_PARAMS : public RDPSINST_BASE_PARAMS
  227. {
  228. RDPSRegister DstReg;
  229. RDPSRegister SrcReg0;
  230. RDPSRegister SrcReg1;
  231. BOOL bSrcReg0_Negate;
  232. BOOL bSrcReg1_Negate;
  233. BYTE WriteMask;
  234. } RDPSINST_SUB_PARAMS;
  235. typedef struct _RDPSINST_MUL_PARAMS : public RDPSINST_BASE_PARAMS
  236. {
  237. RDPSRegister DstReg;
  238. RDPSRegister SrcReg0;
  239. RDPSRegister SrcReg1;
  240. BOOL bSrcReg0_Negate;
  241. BOOL bSrcReg1_Negate;
  242. BYTE WriteMask;
  243. } RDPSINST_MUL_PARAMS;
  244. typedef struct _RDPSINST_DP3_PARAMS : public RDPSINST_BASE_PARAMS
  245. {
  246. RDPSRegister DstReg;
  247. RDPSRegister SrcReg0;
  248. RDPSRegister SrcReg1;
  249. BOOL bSrcReg0_Negate;
  250. BOOL bSrcReg1_Negate;
  251. BYTE WriteMask;
  252. } RDPSINST_DP3_PARAMS;
  253. typedef struct _RDPSINST_DP4_PARAMS : public RDPSINST_BASE_PARAMS
  254. {
  255. RDPSRegister DstReg;
  256. RDPSRegister SrcReg0;
  257. RDPSRegister SrcReg1;
  258. BOOL bSrcReg0_Negate;
  259. BOOL bSrcReg1_Negate;
  260. BYTE WriteMask;
  261. } RDPSINST_DP4_PARAMS;
  262. typedef struct _RDPSINST_MAD_PARAMS : public RDPSINST_BASE_PARAMS
  263. {
  264. RDPSRegister DstReg;
  265. RDPSRegister SrcReg0;
  266. RDPSRegister SrcReg1;
  267. RDPSRegister SrcReg2;
  268. BOOL bSrcReg0_Negate;
  269. BOOL bSrcReg1_Negate;
  270. BOOL bSrcReg2_Negate;
  271. BYTE WriteMask;
  272. } RDPSINST_MAD_PARAMS;
  273. typedef struct _RDPSINST_LRP_PARAMS : public RDPSINST_BASE_PARAMS
  274. {
  275. RDPSRegister DstReg;
  276. RDPSRegister SrcReg0;
  277. RDPSRegister SrcReg1;
  278. RDPSRegister SrcReg2;
  279. BOOL bSrcReg0_Negate;
  280. BOOL bSrcReg1_Negate;
  281. BOOL bSrcReg2_Negate;
  282. BYTE WriteMask;
  283. } RDPSINST_LRP_PARAMS;
  284. typedef struct _RDPSINST_CND_PARAMS : public RDPSINST_BASE_PARAMS
  285. {
  286. RDPSRegister DstReg;
  287. RDPSRegister SrcReg0;
  288. RDPSRegister SrcReg1;
  289. RDPSRegister SrcReg2;
  290. BOOL bSrcReg0_Negate;
  291. BOOL bSrcReg1_Negate;
  292. BOOL bSrcReg2_Negate;
  293. BYTE WriteMask;
  294. } RDPSINST_CND_PARAMS;
  295. typedef struct _RDPSINST_CMP_PARAMS : public RDPSINST_BASE_PARAMS
  296. {
  297. RDPSRegister DstReg;
  298. RDPSRegister SrcReg0;
  299. RDPSRegister SrcReg1;
  300. RDPSRegister SrcReg2;
  301. BOOL bSrcReg0_Negate;
  302. BOOL bSrcReg1_Negate;
  303. BOOL bSrcReg2_Negate;
  304. BYTE WriteMask;
  305. } RDPSINST_CMP_PARAMS;
  306. typedef struct _RDPSINST_END_PARAMS : public RDPSINST_BASE_PARAMS
  307. {
  308. } RDPSINST_END_PARAMS;
  309. typedef struct _RDPSINST_TEXCOVERAGE_PARAMS : public RDPSINST_BASE_PARAMS
  310. {
  311. UINT uiStage;
  312. FLOAT (*pGradients)[2];
  313. FLOAT* pDUDX_0;
  314. FLOAT* pDUDX_1;
  315. FLOAT* pDUDY_0;
  316. FLOAT* pDUDY_1;
  317. FLOAT* pDVDX_0;
  318. FLOAT* pDVDX_1;
  319. FLOAT* pDVDY_0;
  320. FLOAT* pDVDY_1;
  321. FLOAT* pDWDX_0;
  322. FLOAT* pDWDX_1;
  323. FLOAT* pDWDY_0;
  324. FLOAT* pDWDY_1;
  325. } RDPSINST_TEXCOVERAGE_PARAMS;
  326. typedef struct _RDPSINST_QUADLOOPBEGIN_PARAMS : public RDPSINST_BASE_PARAMS
  327. {
  328. } RDPSINST_QUADLOOPBEGIN_PARAMS;
  329. typedef struct _RDPSINST_QUADLOOPEND_PARAMS : public RDPSINST_BASE_PARAMS
  330. {
  331. size_t JumpBackByOffset;
  332. } RDPSINST_QUADLOOPEND_PARAMS;
  333. typedef struct _RDPSINST_QUEUEWRITE_PARAMS : public RDPSINST_BASE_PARAMS
  334. {
  335. RDPSRegister DstReg;
  336. BYTE WriteMask;
  337. } RDPSINST_QUEUEWRITE_PARAMS;
  338. typedef struct _RDPSINST_FLUSHQUEUE_PARAMS : public RDPSINST_BASE_PARAMS
  339. {
  340. } RDPSINST_FLUSHQUEUE_PARAMS;
  341. typedef struct _RDPSINST_NEXTD3DPSINST_PARAMS : public RDPSINST_BASE_PARAMS
  342. {
  343. PixelShaderInstruction* pInst;
  344. } RDPSINST_NEXTD3DPSINST_PARAMS;
  345. // End of "RISC" instruction parameter definitions
  346. typedef struct _ConstDef
  347. {
  348. float f[4];
  349. UINT RegNum;
  350. } ConstDef;
  351. typedef struct _PSQueuedWriteDst
  352. {
  353. RDPSRegister DstReg;
  354. BYTE WriteMask;
  355. } PSQueuedWriteDst;
  356. #define RDPS_COMPONENTMASK_SHIFT 16
  357. #define RDPS_COMPONENTMASK_0 (D3DSP_WRITEMASK_0 >> RDPS_COMPONENTMASK_SHIFT)
  358. #define RDPS_COMPONENTMASK_1 (D3DSP_WRITEMASK_1 >> RDPS_COMPONENTMASK_SHIFT)
  359. #define RDPS_COMPONENTMASK_2 (D3DSP_WRITEMASK_2 >> RDPS_COMPONENTMASK_SHIFT)
  360. #define RDPS_COMPONENTMASK_3 (D3DSP_WRITEMASK_3 >> RDPS_COMPONENTMASK_SHIFT)
  361. #define RDPS_COMPONENTMASK_ALL (D3DSP_WRITEMASK_ALL >> RDPS_COMPONENTMASK_SHIFT)
  362. #define RDPS_NOSWIZZLE (D3DSP_NOSWIZZLE >> D3DSP_SWIZZLE_SHIFT)
  363. #define RDPS_REPLICATERED (D3DSP_REPLICATERED >> D3DSP_SWIZZLE_SHIFT)
  364. #define RDPS_REPLICATEGREEN (D3DSP_REPLICATEGREEN >> D3DSP_SWIZZLE_SHIFT)
  365. #define RDPS_REPLICATEBLUE (D3DSP_REPLICATEBLUE >> D3DSP_SWIZZLE_SHIFT)
  366. #define RDPS_REPLICATEALPHA (D3DSP_REPLICATEALPHA >> D3DSP_SWIZZLE_SHIFT)
  367. #define RDPS_SELECT_R 0
  368. #define RDPS_SELECT_G 1
  369. #define RDPS_SELECT_B 2
  370. #define RDPS_SELECT_A 3
  371. // creates BYTE swizzle description: bits xxyyzzww made of RSPS_SELECT_* for each component
  372. #define _Swizzle(x,y,z,w) ((x)|(y<<2)|(z<<4)|(w<<6))
  373. //-----------------------------------------------------------------------------
  374. //
  375. // RDPShader: Pixel Shader Class
  376. //
  377. //-----------------------------------------------------------------------------
  378. class RDPShader
  379. {
  380. public:
  381. RDPShader();
  382. ~RDPShader();
  383. HRESULT Initialize(RefDev* pRD, DWORD* pCode, DWORD ByteCodeSize, D3DCAPS8* pCaps);
  384. RefDev* m_pRD;
  385. DWORD* m_pCode; // function tokens passed to API
  386. UINT m_CodeSize; // number of DWORDs
  387. // info extracted by parsing shader
  388. UINT m_cActiveTextureStages; // number of texture stages used by this shader
  389. DWORD m_ReferencedTexCoordMask; // Which texture coordinate sets are referenced
  390. UINT m_cInst; // number of shader instructions
  391. PixelShaderInstruction* m_pInst; // processed instructions
  392. GArrayT<BYTE> m_RDPSInstBuffer; // buffer containint refrast "RISC" translated version of shader
  393. UINT m_cConstDefs; // number of D3DSIO_DEF instructions
  394. ConstDef* m_pConstDefs; // array of constant definitions
  395. };
  396. typedef RDPShader *PRDPSHADER;
  397. //-----------------------------------------------------------------------------
  398. // Struct holding the shader ptr
  399. //-----------------------------------------------------------------------------
  400. struct RDPShaderHandle
  401. {
  402. RDPShaderHandle()
  403. {
  404. m_pShader = NULL;
  405. #if DBG
  406. m_tag = 0;
  407. #endif
  408. }
  409. RDPShader* m_pShader;
  410. #if DBG
  411. // Non zero means that it has been allocated
  412. DWORD m_tag;
  413. #endif
  414. };
  415. // psutil.cpp
  416. int
  417. PixelShaderInstDisAsm(
  418. char* pStrRet, int StrSizeRet, DWORD* pShader, DWORD Flags );
  419. void
  420. RDPSDisAsm(BYTE* pRDPSInstBuffer,
  421. ConstDef* pConstDefs,
  422. UINT cConstDefs,
  423. FLOAT fMaxPixelShaderValue,
  424. DWORD dwVersion);
  425. #endif _PSHADER_H