home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / direct3d / mfcpixelshader / pixelshader.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  9.2 KB  |  286 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DApp.h
  3. //
  4. // Desc: Header file for a D3DIM app that uses MFC
  5. //
  6. //
  7. // Copyright (c) 1997-1999 Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #ifndef D3D_MFC_APP_H
  10. #define D3D_MFC_APP_H
  11.  
  12. #if _MSC_VER >= 1000
  13. #pragma once
  14. #endif
  15. #ifndef __AFXWIN_H__
  16. #error include 'stdafx.h' before including this file
  17. #endif
  18.  
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Name: class CAppDoc
  25. // Desc: Overridden CDocument class needed for the CFormView
  26. //-----------------------------------------------------------------------------
  27. class CAppDoc : public CDocument
  28. {
  29. protected:
  30.     DECLARE_DYNCREATE(CAppDoc)
  31. };
  32.  
  33.  
  34.  
  35.  
  36. //-----------------------------------------------------------------------------
  37. // Name: class CAppFrameWnd
  38. // Desc: CFrameWnd-based class needed to override the CFormView's window style
  39. //-----------------------------------------------------------------------------
  40. class CAppFrameWnd : public CFrameWnd
  41. {
  42. protected:
  43.     DECLARE_DYNCREATE(CAppFrameWnd)
  44. public:
  45.     virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
  46. };
  47.  
  48.  
  49.  
  50.  
  51. //-----------------------------------------------------------------------------
  52. // Name: class CApp
  53. // Desc: Main MFCapplication class derived from CWinApp.
  54. //-----------------------------------------------------------------------------
  55. class CApp : public CWinApp
  56. {
  57. public:
  58.     //{{AFX_VIRTUAL(CD3DApp)
  59.     virtual BOOL InitInstance();
  60.     virtual BOOL OnIdle( LONG );
  61.     //}}AFX_VIRTUAL
  62.  
  63.     //{{AFX_MSG(CApp)
  64.     //}}AFX_MSG
  65.     DECLARE_MESSAGE_MAP()
  66. };
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: class CAppForm
  73. // Desc: CFormView-based class which allows the UI to be created with a form
  74. //       (dialog) resource. This class manages all the controls on the form.
  75. //-----------------------------------------------------------------------------
  76. class CAppForm : public CFormView, public CD3DApplication
  77. {
  78.     HWND                    m_hwndRenderWindow;
  79.     HWND                    m_hwndRenderFullScreen;
  80.     LPDIRECT3DTEXTURE8      m_pTexture0;
  81.     LPDIRECT3DTEXTURE8      m_pTexture1;
  82.     LPDIRECT3DVERTEXBUFFER8 m_pQuadVB;
  83.     DWORD                   m_hPixelShader;
  84.     LPD3DXBUFFER            m_pD3DXBufShader;
  85.  
  86.     VOID UpdateUIForInstruction( INT iInstruction );
  87.     VOID    UpdateUIForDeviceCapabilites();
  88.     VOID    DisablePixelShaderEditingControls();
  89.     VOID    GeneratePixelShaderOpcodes();
  90.     HRESULT SetPixelShader( TCHAR* strOpcodes );
  91.     HRESULT ConfirmDevice( D3DCAPS8*,DWORD,D3DFORMAT );
  92.     HRESULT OneTimeSceneInit();
  93.     HRESULT InitDeviceObjects();
  94.     HRESULT RestoreDeviceObjects();
  95.     HRESULT FrameMove();
  96.     HRESULT Render();
  97.     HRESULT InvalidateDeviceObjects();
  98.     HRESULT DeleteDeviceObjects();
  99.     HRESULT FinalCleanup();
  100.     virtual HRESULT AdjustWindowForChange();
  101.  
  102. protected:
  103.     DECLARE_DYNCREATE(CAppForm)
  104.  
  105.              CAppForm();
  106.     virtual  ~CAppForm();
  107. public:
  108.     BOOL IsReady() { return m_bReady; }
  109.     TCHAR* PstrFrameStats() { return m_strFrameStats; }
  110.     VOID RenderScene() { Render3DEnvironment(); }
  111.     HRESULT CheckForLostFullscreen();
  112.  
  113.     //{{AFX_DATA(CAppForm)
  114.     enum { IDD = IDD_FORMVIEW };
  115.     //}}AFX_DATA
  116.  
  117.     //{{AFX_VIRTUAL(CAppForm)
  118.     virtual void OnInitialUpdate();
  119.     //}}AFX_VIRTUAL
  120.  
  121.     //{{AFX_MSG(CAppForm)
  122.     afx_msg void OnToggleFullScreen();
  123.     afx_msg void OnChangeDevice();
  124.     afx_msg void OnHScroll( UINT, UINT, CScrollBar* );
  125.     afx_msg void OnPresets();
  126.     afx_msg void OnOpenPixelShaderFile();
  127.     afx_msg void OnPixelShaderEdit();
  128.     afx_msg void OnChangeInstructions();
  129.     //}}AFX_MSG
  130.     DECLARE_MESSAGE_MAP()
  131.  
  132.  
  133.  
  134. };
  135.  
  136.  
  137.  
  138.  
  139. //-----------------------------------------------------------------------------
  140. // Pixel shader instructions
  141. //-----------------------------------------------------------------------------
  142. struct PIXELSHADER_OPCODE
  143. {
  144.     DWORD  opcode;
  145.     DWORD  dwNumArgs;
  146.     BOOL   bInstructionSupportsScaling;
  147.     BOOL   bInstructionSupportsBias;
  148.     BOOL   bInputsSupportModifiers;
  149.     BOOL   bUsesTextureRegisters;
  150.     TCHAR* optext;
  151.     TCHAR* description;
  152. };
  153.  
  154.  
  155. struct PIXELSHADER_PARAMETER
  156. {
  157.     DWORD  opcode;
  158.     TCHAR* optext;
  159.     TCHAR* description;
  160. };
  161.  
  162. #define D3DSIO_PS  0x00010000 // for specifying PixelShader version
  163.  
  164. // The pixel shader instructions
  165. PIXELSHADER_OPCODE g_PixelShaderInstructions[] =
  166. { //    opcode          numArgs Scaling Bias InputMods Tex  opText         Description
  167.     {   D3DSIO_NOP,          0, FALSE, FALSE, FALSE, FALSE, "nop",         "No operation" },
  168.     {   D3DSIO_PS,           2, FALSE, FALSE, FALSE, FALSE, "ps",          "Pixel Shader Version" },
  169.     {   D3DSIO_DEF,          5, FALSE, FALSE, FALSE, FALSE, "def",         "Define Constant" },
  170.     {   D3DSIO_MOV,          2,  TRUE,  TRUE,  TRUE, FALSE, "mov",         "Move" },
  171.     {   D3DSIO_ADD,          3,  TRUE,  TRUE,  TRUE, FALSE, "add",         "Add" },
  172.     {   D3DSIO_SUB,          3,  TRUE,  TRUE,  TRUE, FALSE, "sub",         "Subtract" },
  173.     {   D3DSIO_MAD,          4,  TRUE,  TRUE,  TRUE, FALSE, "mad",         "Multiply and add" },
  174.     {   D3DSIO_MUL,          3,  TRUE,  TRUE,  TRUE, FALSE, "mul",         "Multiply" },
  175.     {   D3DSIO_DP3,          3,  TRUE,  TRUE,  TRUE, FALSE, "dp3",         "DotProduct3" },
  176.     {   D3DSIO_LRP,          4,  TRUE,  TRUE,  TRUE, FALSE, "lrp",         "Interpolate (blend)" },
  177.     {   D3DSIO_CND,          4,  TRUE,  TRUE,  TRUE, FALSE, "cnd",         "Conditional" },
  178.     {   D3DSIO_TEXCOORD,     1, FALSE, FALSE, FALSE,  TRUE, "texcoord",    "Texture coord ???" },
  179.     {   D3DSIO_TEXKILL,      1, FALSE, FALSE, FALSE,  TRUE, "texkill",     "Texture kill ???" },
  180.     {   D3DSIO_TEX,          1, FALSE, FALSE, FALSE,  TRUE, "tex",         "Texture" },
  181.     {   D3DSIO_TEXBEM,       2, FALSE, FALSE, FALSE,  TRUE, "texbem",      "Bump env map" },
  182.     {   D3DSIO_TEXBEML,      2, FALSE, FALSE, FALSE,  TRUE, "texbeml",     "Bump env map w/luminance" },
  183.     {   D3DSIO_TEXREG2AR,    2, FALSE, FALSE, FALSE,  TRUE, "texreg2ar",   "???" },
  184.     {   D3DSIO_TEXREG2GB,    2, FALSE, FALSE, FALSE,  TRUE, "texreg2gb",   "???" },
  185.     {   D3DSIO_TEXM3x2PAD,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x2pad",  "3x2 ???" },
  186.     {   D3DSIO_TEXM3x2TEX,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x2tex",  "3x2 ???" },
  187.     {   D3DSIO_TEXM3x3PAD,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x3pad",  "3x3 ???" },
  188.     {   D3DSIO_TEXM3x3TEX,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x3tex",  "3x3 ???" },
  189.     {   D3DSIO_TEXM3x3DIFF,  2, FALSE, FALSE, FALSE,  TRUE, "texm3x3diff", "3x3 ???" },
  190.     {   D3DSIO_TEXM3x3SPEC,  3, FALSE, FALSE, FALSE,  TRUE, "texm3x3spec", "3x3 ???" },
  191.     {   D3DSIO_TEXM3x3VSPEC, 2, FALSE, FALSE, FALSE,  TRUE, "texm3x3vspec","3x3 ???" },
  192.     {   D3DSIO_END,          0, FALSE, FALSE, FALSE, FALSE,  NULL,                NULL },
  193. };
  194.  
  195.  
  196.  
  197.  
  198. // The source parameters for the pixel shader instructions
  199. PIXELSHADER_PARAMETER g_PixelShaderParameters[] =
  200. {
  201.     {   D3DS_SR0,  "r0", "Register 0" },
  202.     {   D3DS_SR1,  "r1", "Register 1" },
  203.     {   D3DS_SR2,  "r2", "Register 2" },
  204.     {   D3DS_SR3,  "r3", "Register 3" },
  205.     {   D3DS_SR4,  "r4", "Register 4" },
  206.     {   D3DS_SR5,  "r5", "Register 5" },
  207.     {   D3DS_SR6,  "r6", "Register 6" },
  208.     {   D3DS_SR7,  "r7", "Register 7" },
  209.     {   D3DS_T0,   "t0", "Texture 0" },
  210.     {   D3DS_T1,   "t1", "Texture 1" },
  211.     {   D3DS_T2,   "t2", "Texture 2" },
  212.     {   D3DS_T3,   "t3", "Texture 3" },
  213.     {   D3DS_T4,   "t4", "Texture 4" },
  214.     {   D3DS_T5,   "t5", "Texture 5" },
  215.     {   D3DS_T6,   "t6", "Texture 6" },
  216.     {   D3DS_T7,   "t7", "Texture 7" },
  217.     {   D3DS_C0,   "c0", "Constant 0" },
  218.     {   D3DS_C1,   "c1", "Constant 1" },
  219.     {   D3DS_C2,   "c2", "Constant 2" },
  220.     {   D3DS_C3,   "c3", "Constant 3" },
  221.     {   D3DS_C4,   "c4", "Constant 4" },
  222.     {   D3DS_C5,   "c5", "Constant 5" },
  223.     {   D3DS_C6,   "c6", "Constant 6" },
  224.     {   D3DS_C7,   "c7", "Constant 7" },
  225.     {   D3DS_V0,   "v0", "Vertex color 0" },
  226.     {   D3DS_V1,   "v1", "Vertex color 1" },
  227.     {   D3DS_V1,   NULL, NULL },
  228. };
  229.  
  230.  
  231.  
  232. // Define some preset pixel shaders
  233. TCHAR* g_strPixelShaderPresets[10] =
  234. {
  235.     {
  236.         "// Diffuse only\r\n"
  237.         "ps.1.0\r\n"
  238.         "mov r0, v0"
  239.     },
  240.     {
  241.         "// Decal\r\n"
  242.         "ps.1.0\r\n"
  243.         "tex t0\r\n"
  244.         "mov r0, t0"
  245.     },
  246.     {
  247.         "// Modulate texture with diffuse (color0)\r\n"
  248.         "ps.1.0\r\n"
  249.         "tex t0\r\n"
  250.         "mul r0, v0, t0"
  251.     },
  252.     {
  253.         "// Blend between tex0 and tex1\r\n"
  254.         "ps.1.0\r\n"
  255.         "tex t0\r\n"
  256.         "tex t1\r\n"
  257.         "mov r1, t1\r\n"
  258.         "lrp r0, v0, t0, r1"
  259.     },
  260.     {
  261.         "// Scale tex0 by color0 and add to tex1\r\n"
  262.         "ps.1.0\r\n"
  263.         "tex t0\r\n"
  264.         "tex t1\r\n"
  265.         "mov r1, t1\r\n"
  266.         "mad r0, v0, t0, r1"
  267.     },
  268.     {
  269.         "// Add tex0, tex1, color0, and color1\r\n"
  270.         "ps.1.0\r\n"
  271.         "tex t0\r\n"
  272.         "tex t1\r\n"
  273.         "add r0, t0, v1\r\n"
  274.         "add r0, r0, t1\r\n"
  275.         "add r0, r0, v0"
  276.     },
  277. };
  278.  
  279.  
  280.  
  281.  
  282.  
  283. #endif
  284.  
  285.  
  286.