source: OGRE/trunk/ogrenew/RenderSystems/Direct3D9/src/OgreD3D9GpuProgram.cpp @ 657

Revision 657, 6.9 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreD3D9GpuProgram.h"
26#include "OgreMatrix4.h"
27#include "OgreException.h"
28#include "OgreLogManager.h"
29#include "OgreD3D9Mappings.h"
30#include "OgreResourceGroupManager.h"
31
32namespace Ogre {
33
34    //-----------------------------------------------------------------------------
35    D3D9GpuProgram::D3D9GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
36        const String& group, bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev)
37        : GpuProgram(creator, name, handle, group, isManual, loader),
38        mpDevice(pDev), mpExternalMicrocode(NULL)
39    {
40        if (createParamDictionary("D3D9GpuProgram"))
41        {
42            setupBaseParamDictionary();
43        }
44    }
45        //-----------------------------------------------------------------------------
46    void D3D9GpuProgram::loadImpl(void)
47    {
48        if (mpExternalMicrocode)
49        {
50            loadFromMicrocode(mpExternalMicrocode);
51        }
52        else
53        {
54            // Normal load-from-source approach
55            if (mLoadFromFile)
56            {
57                // find & load source code
58                DataStreamPtr stream =
59                    ResourceGroupManager::getSingleton().openResource(mFilename, mGroup);
60                mSource = stream->getAsString();
61            }
62
63            // Call polymorphic load
64            loadFromSource();
65        }
66
67    }
68        //-----------------------------------------------------------------------------
69    void D3D9GpuProgram::loadFromSource(void)
70    {
71        // Create the shader
72        // Assemble source into microcode
73        LPD3DXBUFFER microcode;
74        LPD3DXBUFFER errors;
75        HRESULT hr = D3DXAssembleShader(
76            mSource.c_str(),
77            static_cast<UINT>(mSource.length()),
78            NULL,               // no #define support
79            NULL,               // no #include support
80            0,                  // standard compile options
81            &microcode,
82            &errors);
83
84        if (FAILED(hr))
85        {
86            String message = "Cannot assemble D3D9 shader " + mName + " Errors:\n" +
87                static_cast<const char*>(errors->GetBufferPointer());
88            errors->Release();
89            OGRE_EXCEPT(hr, message,
90                "D3D9GpuProgram::loadFromSource");
91           
92        }
93
94        loadFromMicrocode(microcode);
95
96        SAFE_RELEASE(microcode);
97        SAFE_RELEASE(errors);
98    }
99        //-----------------------------------------------------------------------------
100    D3D9GpuVertexProgram::D3D9GpuVertexProgram(ResourceManager* creator,
101        const String& name, ResourceHandle handle, const String& group,
102        bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev)
103        : D3D9GpuProgram(creator, name, handle, group, isManual, loader, pDev)
104        , mpVertexShader(NULL)
105    {
106        mType = GPT_VERTEX_PROGRAM;
107    }
108        //-----------------------------------------------------------------------------
109        D3D9GpuVertexProgram::~D3D9GpuVertexProgram()
110        {
111                // have to call this here reather than in Resource destructor
112                // since calling virtual methods in base destructors causes crash
113                unload();
114        }
115        //-----------------------------------------------------------------------------
116    void D3D9GpuVertexProgram::loadFromMicrocode(LPD3DXBUFFER microcode)
117    {
118                if (isSupported())
119                {
120                        // Create the shader
121                        HRESULT hr = mpDevice->CreateVertexShader(
122                                static_cast<DWORD*>(microcode->GetBufferPointer()),
123                                &mpVertexShader);
124
125                        if (FAILED(hr))
126                        {
127                                OGRE_EXCEPT(hr, "Cannot create D3D9 vertex shader " + mName + " from microcode.",
128                                        "D3D9GpuVertexProgram::loadFromMicrocode");
129                   
130                        }
131                }
132                else
133                {
134                        LogManager::getSingleton().logMessage(
135                                "Unsupported D3D9 vertex shader '" + mName + "' was not loaded.");
136                }
137    }
138        //-----------------------------------------------------------------------------
139    void D3D9GpuVertexProgram::unloadImpl(void)
140    {
141        SAFE_RELEASE(mpVertexShader);
142    }
143        //-----------------------------------------------------------------------------
144        //-----------------------------------------------------------------------------
145    D3D9GpuFragmentProgram::D3D9GpuFragmentProgram(ResourceManager* creator,
146        const String& name, ResourceHandle handle, const String& group,
147        bool isManual, ManualResourceLoader* loader, LPDIRECT3DDEVICE9 pDev)
148        : D3D9GpuProgram(creator, name, handle, group, isManual, loader, pDev)
149        , mpPixelShader(NULL)
150    {
151        mType = GPT_FRAGMENT_PROGRAM;
152    }
153        //-----------------------------------------------------------------------------
154        D3D9GpuFragmentProgram::~D3D9GpuFragmentProgram()
155        {
156                // have to call this here reather than in Resource destructor
157                // since calling virtual methods in base destructors causes crash
158                unload();
159        }
160        //-----------------------------------------------------------------------------
161    void D3D9GpuFragmentProgram::loadFromMicrocode(LPD3DXBUFFER microcode)
162    {
163                if (isSupported())
164                {
165                        // Create the shader
166                        HRESULT hr = mpDevice->CreatePixelShader(
167                                static_cast<DWORD*>(microcode->GetBufferPointer()),
168                                &mpPixelShader);
169
170                        if (FAILED(hr))
171                        {
172                                OGRE_EXCEPT(hr, "Cannot create D3D9 pixel shader " + mName + " from microcode.",
173                                        "D3D9GpuFragmentProgram::loadFromMicrocode");
174                   
175                        }
176                }
177                else
178                {
179                        LogManager::getSingleton().logMessage(
180                                "Unsupported D3D9 pixel shader '" + mName + "' was not loaded.");
181                }
182    }
183        //-----------------------------------------------------------------------------
184    void D3D9GpuFragmentProgram::unloadImpl(void)
185    {
186        SAFE_RELEASE(mpPixelShader);
187    }
188        //-----------------------------------------------------------------------------
189
190}
191
Note: See TracBrowser for help on using the repository browser.