PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/visualizations/Vortex/VortexVis/Core/Shader.cpp

http://github.com/xbmc/xbmc
C++ | 289 lines | 250 code | 20 blank | 19 comment | 14 complexity | 2c5ebb11fe282dc99a01b1bab25a3558 MD5 | raw file
  1/*
  2 *  Copyright Š 2010-2013 Team XBMC
  3 *  http://xbmc.org
  4 *
  5 *  This program is free software: you can redistribute it and/or modify
  6 *  it under the terms of the GNU General Public License as published by
  7 *  the Free Software Foundation, either version 2 of the License, or
  8 *  (at your option) any later version.
  9 *
 10 *  This program is distributed in the hope that it will be useful,
 11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 *  GNU General Public License for more details.
 14 *
 15 *  You should have received a copy of the GNU General Public License
 16 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17 *
 18 */
 19
 20#include "Shader.h"
 21#include "Renderer.h"
 22
 23void Shader::ReleaseAllShaders()
 24{
 25	std::vector<Shader*>&  ShaderList = GetShaderList();
 26
 27	std::vector<Shader*>::iterator ShaderIterator;
 28
 29	for (ShaderIterator = ShaderList.begin(); 
 30		ShaderIterator != ShaderList.end();
 31		ShaderIterator++ )
 32	{
 33		if ( (*ShaderIterator)->m_pVShader )
 34			(*ShaderIterator)->m_pVShader->Release();
 35		if ( (*ShaderIterator)->m_pPShader )
 36			(*ShaderIterator)->m_pPShader->Release();
 37	}
 38}
 39
 40bool Shader::CompileAllShaders()
 41{
 42	std::vector<Shader*>&  ShaderList = GetShaderList();
 43
 44	std::vector<Shader*>::iterator ShaderIterator;
 45
 46	for (ShaderIterator = ShaderList.begin(); 
 47		ShaderIterator != ShaderList.end();
 48		ShaderIterator++ )
 49	{
 50		if ( (*ShaderIterator)->CompileShader() == false )
 51			return false;
 52	}
 53	return true;
 54}
 55
 56bool Shader::CompileShader()
 57{
 58	DWORD dwShaderFlags = D3DXSHADER_PARTIALPRECISION;
 59	LPD3DXBUFFER pCode;
 60	LPD3DXBUFFER pErrors;
 61
 62	int iSrcLen = strlen( m_pShaderSrc );
 63	if ( iSrcLen == 0 )
 64		return false;
 65
 66	HRESULT hr = ( D3DXCompileShader( m_pShaderSrc,
 67		iSrcLen,
 68		NULL,											// defines
 69		NULL,											// include
 70		"Main",											// Function name
 71		m_ShaderType == ST_PIXEL ? "ps_2_0": "vs_2_0",	// profile
 72		dwShaderFlags,									// Flags
 73		&pCode,											// Output compiled shader
 74		&pErrors,										// error messages
 75		&m_pConstantTable								// constant table
 76		) );
 77
 78	if( FAILED( hr ) )
 79	{
 80		char* errors = (char*)pErrors->GetBufferPointer();
 81		OutputDebugStringA( errors );
 82		return false;
 83	}
 84
 85	if ( m_ShaderType == ST_PIXEL )
 86	{
 87		m_pPShader = Renderer::CreatePixelShader( ( DWORD* )pCode->GetBufferPointer() );
 88	}
 89	else
 90	{
 91		m_pVShader = Renderer::CreateVertexShader( ( DWORD* )pCode->GetBufferPointer() );
 92	}
 93
 94	pCode->Release();
 95	return true;
 96}
 97
 98char DiffuseUVVertexShaderSrc[] =
 99{
100	"		struct VS_INPUT													\n"
101	"		{																\n"
102	"			float4 vPosition : POSITION;								\n"
103	"			float4 Colour : COLOR;										\n"
104	"			float2 Tex0 : TEXCOORD0;	    							\n"
105	"		};																\n"
106	"		struct VS_OUTPUT												\n"
107	"		{																\n"
108	"			float4 vPosition : POSITION;								\n"
109	"			float4 Colour : COLOR;										\n"
110	"			float2 Tex0 : TEXCOORD0;	    							\n"
111	"		};																\n"
112	"																		\n"
113	" float4x4 WVPMat;														\n"
114	" VS_OUTPUT Main( VS_INPUT input )										\n"
115	" {																		\n"
116	"	VS_OUTPUT output;													\n"
117	"	// Transform coord													\n"
118	"	output.vPosition = mul(WVPMat, float4(input.vPosition.xyz, 1.0f));	\n"
119	"	output.Colour = input.Colour;										\n"
120	"	output.Tex0 = input.Tex0;											\n"
121	"	return output;														\n"
122	" }																		\n"
123	"																		\n"
124};
125
126char DiffuseUVEnvVertexShaderSrc[] =
127{
128	"		struct VS_INPUT													\n"
129	"		{																\n"
130	"			float4 vPosition : POSITION;								\n"
131	"			float4 vNormal : NORMAL;									\n"
132	"			float4 Colour : COLOR;										\n"
133	"			float2 Tex0 : TEXCOORD0;	    							\n"
134	"		};																\n"
135	"		struct VS_OUTPUT												\n"
136	"		{																\n"
137	"			float4 vPosition : POSITION;								\n"
138	"			float4 Colour : COLOR;										\n"
139	"			float2 Tex0 : TEXCOORD0;	    							\n"
140	"		};																\n"
141	"																		\n"
142	" float4x4 WVMat;														\n"
143	" float4x4 WVPMat;														\n"
144	" VS_OUTPUT Main( VS_INPUT input )										\n"
145	" {																		\n"
146	"	VS_OUTPUT output;													\n"
147	"	// Transform coord													\n"
148	"	output.vPosition = mul(WVPMat, float4(input.vPosition.xyz, 1.0f));	\n"
149	"	output.Colour = input.Colour;										\n"
150	"	float4 NormalCamSpace = mul(WVMat, float4(input.vNormal.xyz, 0.0f));\n"
151	"	output.Tex0.xy = (NormalCamSpace.xy * 0.8f) + 0.5f;					\n"
152//	"	output.Colour = float4(NormalCamSpace.xyz, 1);						\n"
153	"	return output;														\n"
154	" }																		\n"
155	"																		\n"
156};
157
158char DiffuseUVCubeVertexShaderSrc[] =
159{
160	"		struct VS_INPUT													\n"
161	"		{																\n"
162	"			float4 vPosition : POSITION;								\n"
163	"			float4 Colour : COLOR;										\n"
164	"			float2 Tex0 : TEXCOORD0;	    							\n"
165	"		};																\n"
166	"		struct VS_OUTPUT												\n"
167	"		{																\n"
168	"			float4 vPosition : POSITION;								\n"
169	"			float4 Colour : COLOR;										\n"
170	"			float2 Tex0 : TEXCOORD0;	    							\n"
171	"		};																\n"
172	"																		\n"
173	" float4 Col;															\n"
174	" float4 vScale;														\n"
175	" float4x4 WVMat;														\n"
176	" float4x4 WVPMat;														\n"
177	" VS_OUTPUT Main( VS_INPUT input )										\n"
178	" {																		\n"
179	"	VS_OUTPUT output;													\n"
180	"	float4 vPos = float4(input.vPosition.xyz * vScale, 1);				\n"
181	"	// Transform coord													\n"
182	"	output.vPosition = mul(WVPMat, vPos);								\n"
183	"	output.Colour = Col;												\n"
184	"	output.Tex0.xy = input.Tex0;										\n"
185	"	return output;														\n"
186	" }																		\n"
187	"																		\n"
188};
189
190char DiffuseUVEnvCubeVertexShaderSrc[] =
191{
192	"		struct VS_INPUT													\n"
193	"		{																\n"
194	"			float4 vPosition : POSITION;								\n"
195	"			float4 vNormal : NORMAL;									\n"
196	"			float4 Colour : COLOR;										\n"
197	"			float2 Tex0 : TEXCOORD0;	    							\n"
198	"		};																\n"
199	"		struct VS_OUTPUT												\n"
200	"		{																\n"
201	"			float4 vPosition : POSITION;								\n"
202	"			float4 Colour : COLOR;										\n"
203	"			float2 Tex0 : TEXCOORD0;	    							\n"
204	"		};																\n"
205	"																		\n"
206	" float4 Col;															\n"
207	" float4 vScale;														\n"
208	" float4x4 WVMat;														\n"
209	" float4x4 WVPMat;														\n"
210	" VS_OUTPUT Main( VS_INPUT input )										\n"
211	" {																		\n"
212	"	VS_OUTPUT output;													\n"
213	"	float4 vPos = float4(input.vPosition.xyz * vScale, 1);				\n"
214	"	// Transform coord													\n"
215	"	output.vPosition = mul(WVPMat, vPos);								\n"
216	"	output.Colour = Col;												\n"
217	"	float4 NormalCamSpace = mul(WVMat, float4(input.vNormal.xyz, 0.0f));\n"
218	"	output.Tex0.xy = (NormalCamSpace.xy * 0.5f) + 0.5f;					\n"
219	"	return output;														\n"
220	" }																		\n"
221	"																		\n"
222};
223
224char DiffuseNormalEnvCubeVertexShaderSrc[] =
225{
226	"		struct VS_INPUT													\n"
227	"		{																\n"
228	"			float4 vPosition : POSITION;								\n"
229	"			float4 vNormal : NORMAL;									\n"
230	"		};																\n"
231	"		struct VS_OUTPUT												\n"
232	"		{																\n"
233	"			float4 vPosition : POSITION;								\n"
234	"			float4 Colour : COLOR;										\n"
235	"			float2 Tex0 : TEXCOORD0;	    							\n"
236	"		};																\n"
237	"																		\n"
238	" float4 Col;															\n"
239	" float4x4 WVMat;														\n"
240	" float4x4 WVPMat;														\n"
241	" VS_OUTPUT Main( VS_INPUT input )										\n"
242	" {																		\n"
243	"	VS_OUTPUT output;													\n"
244	"	float4 vPos = float4(input.vPosition.xyz, 1);						\n"
245	"	// Transform coord													\n"
246	"	output.vPosition = mul(WVPMat, vPos);								\n"
247	"	output.Colour = 0xffffffff;											\n"
248	"	float4 NormalCamSpace = mul(WVMat, float4(input.vNormal.xyz, 0.0f));\n"
249	"	output.Tex0.xy = (NormalCamSpace.xy * 0.2f) + 0.5f;					\n"
250	"	return output;														\n"
251	" }																		\n"
252	"																		\n"
253};
254
255char UVNormalEnvVertexShaderSrc[] =
256{
257	"		struct VS_INPUT													\n"
258	"		{																\n"
259	"			float4 vPosition : POSITION;								\n"
260	"			float4 vNormal : NORMAL;									\n"
261	"		};																\n"
262	"		struct VS_OUTPUT												\n"
263	"		{																\n"
264	"			float4 vPosition : POSITION;								\n"
265	"			float4 Colour : COLOR;										\n"
266	"			float2 Tex0 : TEXCOORD0;	    							\n"
267	"		};																\n"
268	"																		\n"
269	" float4x4 WVMat;														\n"
270	" float4x4 WVPMat;														\n"
271	" VS_OUTPUT Main( VS_INPUT input )										\n"
272	" {																		\n"
273	"	VS_OUTPUT output;													\n"
274	"	// Transform coord													\n"
275	"	output.vPosition = mul(WVPMat, float4(input.vPosition.xyz, 1.0f));	\n"
276	"	output.Colour = 0xffffffff;											\n"
277	"	float4 NormalCamSpace = mul(WVMat, float4(input.vNormal.xyz, 0.0f));\n"
278	"	output.Tex0.xy = (NormalCamSpace.xy * 0.2f) + 0.5f;					\n"
279	"	return output;														\n"
280	" }																		\n"
281	"																		\n"
282};
283
284IMPLEMENT_SHADER(DiffuseUVVertexShader, DiffuseUVVertexShaderSrc, ST_VERTEX);
285IMPLEMENT_SHADER(DiffuseUVEnvVertexShader, DiffuseUVEnvVertexShaderSrc, ST_VERTEX);
286IMPLEMENT_SHADER(DiffuseUVCubeVertexShader, DiffuseUVCubeVertexShaderSrc, ST_VERTEX);
287IMPLEMENT_SHADER(DiffuseUVEnvCubeVertexShader, DiffuseUVEnvCubeVertexShaderSrc, ST_VERTEX);
288IMPLEMENT_SHADER(DiffuseNormalEnvCubeVertexShader, DiffuseNormalEnvCubeVertexShaderSrc, ST_VERTEX);
289IMPLEMENT_SHADER(UVNormalEnvVertexShader, UVNormalEnvVertexShaderSrc, ST_VERTEX);