source: OGRE/trunk/ogre_dependencies/Dependencies/include/Cg/cgProgramManager.h @ 692

Revision 692, 10.9 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2 *
3 * Copyright (c) 2002, NVIDIA Corporation.
4 *
5 * 
6 *
7 * NVIDIA Corporation("NVIDIA") supplies this software to you in consideration
8 * of your agreement to the following terms, and your use, installation,
9 * modification or redistribution of this NVIDIA software constitutes
10 * acceptance of these terms.  If you do not agree with these terms, please do
11 * not use, install, modify or redistribute this NVIDIA software.
12 *
13 * 
14 *
15 * In consideration of your agreement to abide by the following terms, and
16 * subject to these terms, NVIDIA grants you a personal, non-exclusive license,
17 * under NVIDIA’s copyrights in this original NVIDIA software (the "NVIDIA
18 * Software"), to use, reproduce, modify and redistribute the NVIDIA
19 * Software, with or without modifications, in source and/or binary forms;
20 * provided that if you redistribute the NVIDIA Software, you must retain the
21 * copyright notice of NVIDIA, this notice and the following text and
22 * disclaimers in all such redistributions of the NVIDIA Software. Neither the
23 * name, trademarks, service marks nor logos of NVIDIA Corporation may be used
24 * to endorse or promote products derived from the NVIDIA Software without
25 * specific prior written permission from NVIDIA.  Except as expressly stated
26 * in this notice, no other rights or licenses express or implied, are granted
27 * by NVIDIA herein, including but not limited to any patent rights that may be
28 * infringed by your derivative works or by other works in which the NVIDIA
29 * Software may be incorporated. No hardware is licensed hereunder.
30 *
31 * 
32 *
33 * THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
35 * WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR ITS USE AND OPERATION
37 * EITHER ALONE OR IN COMBINATION WITH OTHER PRODUCTS.
38 *
39 * 
40 *
41 * IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL,
42 * EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, LOST
43 * PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 * PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY OUT OF THE USE,
45 * REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE NVIDIA SOFTWARE,
46 * HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING
47 * NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF NVIDIA HAS BEEN ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 *
50 */
51
52/*********************************************************************NVMH2****
53File:  cgProgramManager.h
54
55Copyright (C) 1999, 2000 NVIDIA Corporation
56This file is provided without support, instruction, or implied warranty of any
57kind.  NVIDIA makes no guarantee of its fitness for a particular purpose and is
58not liable under any circumstances for any damages or loss whatsoever arising
59from the use or inability to use this file or items derived from it.
60
61Comments:
62
63cgProgramManager - a class for loading various pixel & vertex shaders
64
65
66******************************************************************************/
67
68
69#ifndef __NV_PROGRAM_MANAGER_H
70#define __NV_PROGRAM_MANAGER_H
71
72#if WIN32
73
74#pragma warning (disable:4786) // identifier was truncated to '255' characters in the browser information
75
76#include <windows.h>
77#include <d3d8.h>
78#include <d3dx8.h>
79
80#include <Cg/cgTemplates.h>
81#include <Cg/cg.h>
82
83
84
85// to allow multiple streams and user specifed vertex type
86// D3DVSDT_FLOAT1      0x00    // 1D float expanded to (value, 0., 0., 1.)
87// D3DVSDT_FLOAT2      0x01    // 2D float expanded to (value, value, 0., 1.)
88// D3DVSDT_FLOAT3      0x02    // 3D float expanded to (value, value, value, 1.)
89// D3DVSDT_FLOAT4      0x03    // 4D float
90// D3DVSDT_D3DCOLOR    0x04    // 4D packed unsigned bytes mapped to 0. to 1. range
91//                             // Input is in D3DCOLOR format (ARGB) expanded to (R, G, B, A)
92// D3DVSDT_UBYTE4      0x05    // 4D unsigned byte
93// D3DVSDT_SHORT2      0x06    // 2D signed short expanded to (value, value, 0., 1.)
94// D3DVSDT_SHORT4      0x07    // 4D signed short
95
96
97// was cgVertexAttribute
98typedef struct cgVertexDefinition
99{
100    int D3DVSDT_type;       // one of D3DVSDT_*
101    char * name;    // name of the entry
102    int stream;     // stream to put the vertex in
103} cgVertexDefinition;
104
105       
106#define CGVERTEXDEFINITIONEND {-1, 0, 0}
107
108// internal cgDirect3D data structure
109typedef struct cgiVertexAttribute
110{
111    char name[64];
112    int D3DVSDT_type;
113    int bind_location;
114    cgBindIter * BindIter;
115} cgiVertexAttribute;
116
117
118
119typedef cg_vector<cgiVertexAttribute> cgiVertexAttributeList;
120typedef cg_list<cgBindIter *> cgBindIterList;
121
122
123class cgProgramManager;
124class cgContextContainer;
125
126class cgProgramContainer
127{
128
129public:
130
131    // get a text definition of what the runtime expects for the vertex
132    const char * GetVertexDeclaration();
133    /*
134        Example: returns string containing
135        "struct stream0
136        {
137            D3DXVECTOR3 position;
138            D3DXVECTOR2 tex0;
139        };"
140
141    */
142
143
144
145   
146    // convert D3DXMATRIX class to type specified in cg program
147    // even if your cg program uses 3x3, you can use this to pass a matrix to it
148    // upper left hand part of D3DXMATRIX is used to fill the cg matrix
149    HRESULT SetShaderConstantD3DXMATRIX(cgBindIter * iter, const D3DXMATRIX *, int nArrayElements = 0, int ArrayOffset = 0);
150
151
152
153    // All other data formats use this interface
154    // The D3D runtime records the size of your data and puts that in the constant fields
155    HRESULT SetShaderConstant(cgBindIter * iter, void * data, int nArrayElements = 0, int ArrayOffset = 0);
156    // for arrays:
157    //   if nArrayElements is specified, write this number of elements rather than the whole array
158    //   if ArrayOffset is specified, start writing at this array element
159    // otherwise nArrayElements and ArrayOffset are not specified
160
161
162    // returns texture position 0-8 for the iterator
163    // -1 is failure
164    int GetTexturePosition(cgBindIter * BindIter);
165    int GetTexturePosition(const char * name);
166
167    // set the texture via name (needs to search bindings by name, so this is the slowest
168    // not recommended
169    HRESULT SetTexture(const char * name, LPDIRECT3DBASETEXTURE8 pTexture);
170
171    // does a GetTexturePosition, then settexture(n, nTexture)
172    HRESULT SetTexture(cgBindIter * BindIter, LPDIRECT3DBASETEXTURE8 pTexture);
173
174    // does a GetTexturePosition, then SetTextureStageState(n, nTexture, Value)
175    HRESULT SetTextureStageState(cgBindIter * BindIter, D3DTEXTURESTAGESTATETYPE Type,DWORD Value);
176
177    // Direct3D hardcodes wrap state, D3DRS_WRAP1, D3DWRAPCOORD_0 | D3DWRAPCOORD_1
178    //   WrapCoords : 0, D3DWRAPCOORD_0, D3DWRAPCOORD_1 or'd together
179    HRESULT SetTextureWrapMode(cgBindIter * BindIter, DWORD WrapCoords);
180
181
182    // activate this shader (SetVertexShader or SetPixelShader)
183    HRESULT SetShaderActive();
184
185
186    // locate a binding within a program by parameter name
187    // it is recommended to do this once and use the cgBindIter to reference data
188    cgBindIter *GetVertexBindByName(const char *vertex_attr_name);
189    cgBindIter *GetTextureBindByName(const char *texture_name);
190    cgBindIter *GetParameterBindByName(const char *parameter_name);
191   
192    void FreeBindIter(cgBindIter *);
193
194
195
196
197    // Get the type for this bind iterator
198    cgValueType GetBindValueType(const cgBindIter *BindIter,  int *array_size)
199    {
200        return cgGetBindValueType(BindIter,  array_size);
201    }
202
203
204    // locate and bindinf iterator by full name
205    //  e.g. appdata.texcoord0
206    cgBindIter *GetBindByFullName(const char *parameter_name);
207
208    // from the iterator, get the name of the program
209    const char *GetProgramName();
210   
211    const char *GetProgramObjectCode();
212
213    cgProfileType GetProgramProfile() { return m_ProfileType;}
214
215    // get the shader handle passed to Direct3D
216    DWORD GetShaderHandle() {return m_dwShaderHandle;} 
217
218private:
219
220    cgBindIter *AllocBind();
221    void SaveBind(cgBindIter * BindIter);  // for deleting
222
223    cgBindIterList m_BindIterList;
224
225    void GetParameterConstantPosition(cgBindIter * iter, int * pos, int *width, int * num4vectors, int *nArrayElements);
226    void GetParameterConstantPosition(const char * name, int * pos, int *width, int * num4vectors, int *nArrayElements);
227
228
229    HRESULT CreateProgramShader();
230    HRESULT CreateVertexShader();
231    HRESULT CreatePixelShader();
232   
233    // take asm code, assembles it and create a handle
234        HRESULT CreateShaderHandleFromFile(
235        const cg_string & strFilePath,
236        const cg_string & strSourceFile,
237                const DWORD * pDeclaration,     
238        DWORD Usage);
239   
240    HRESULT CreateShaderHandleFromMemory(
241        const char * pShaderText,
242        const DWORD * pDeclaration,     
243        DWORD Usage);
244
245
246    HRESULT Free();
247
248   
249    // cgDX8VertexProfile or cgDX8PixelProfile (cg_profiles.h)
250        // These types are defined with macros:
251        //      CG_PROFILE_MACRO(DX8Vertex,dx8vs,"dx8vs")
252        //      CG_PROFILE_MACRO(DX8Pixel,dx8ps,"dx8ps")
253
254
255    friend  cgProgramManager;
256    friend  cgContextContainer;
257    DWORD                   m_dwShaderHandle;           // DWORD handle returned by DX8 runtime
258        cgProfileType   m_ProfileType;          // profile for this program
259    HRESULT SetVertexDefinition(cgVertexDefinition * streams);
260
261
262    cgProgramContainer(LPDIRECT3DDEVICE8 pDev, cgProgramIter *m_ProgramIter,
263                                                cgProfileType ProfileType);
264    ~cgProgramContainer();
265
266
267    HRESULT         LoadShaderConstants();
268    cg_string               m_ProgramSourceFile;        // text file name
269    cg_string               m_ProgramObjectFile;                // compiled shader object file (.vso, .pso)
270
271    // for declaration of vertex positions
272    cgiVertexAttributeList m_VertexAttributes[MAX_STREAMS];
273
274    cgProgramIter *m_ProgramIter;
275       
276 
277        LPDIRECT3DDEVICE8 m_pd3dDevice;
278
279};
280
281
282
283
284class cgProgramManager
285{
286private:
287        cg_vector < cgProgramContainer * >      m_ProgramContainers;                    // m_Shaders[0] is always = 0
288        cg_vector < DWORD >                     m_ProgramIndices;       // indirection to m_Shaders
289                                                                                        // Programs hold an index to this array for
290                                                                                        // their shaders.  The array int at that index
291                                                                                        // holds the index into the m_Shaders array.
292
293public:
294
295
296        cgProgramManager(LPDIRECT3DDEVICE8 pDev);
297        ~cgProgramManager();
298
299        HRESULT Free();
300
301
302    cgProgramContainer * CreateProgramContainer(LPDIRECT3DDEVICE8 pDev,
303        cgProgramIter * ProgramIter,
304        DWORD Usage,
305        cgProfileType ProfileType,
306        DWORD * outIndex );
307
308
309
310        DWORD   GetShaderHandle( DWORD Index );
311        DWORD   GetShaderHandle( cgProfileType ProfileType );
312
313    //
314    cgProgramContainer * GetProgramIterator(DWORD index);
315
316
317
318private:
319    // for creating programs
320    LPDIRECT3DDEVICE8 m_pd3dDevice;
321
322};
323
324#endif // WIN32
325
326#endif                 
Note: See TracBrowser for help on using the repository browser.