summaryrefslogtreecommitdiff
path: root/indra/newview/lldrawpoolmaterials.cpp
blob: 5eb3cb9629bf6c8a945cb258fec76dde179c8f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/** 
 * @file lldrawpool.cpp
 * @brief LLDrawPoolMaterials class implementation
 * @author Jonathan "Geenz" Goodman
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2013, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "lldrawpoolmaterials.h"
#include "llviewershadermgr.h"
#include "pipeline.h"

S32 diffuse_channel = -1;

LLDrawPoolMaterials::LLDrawPoolMaterials()
:  LLRenderPass(LLDrawPool::POOL_MATERIALS)
{
	
}

void LLDrawPoolMaterials::prerender()
{
	mVertexShaderLevel = LLViewerShaderMgr::instance()->getVertexShaderLevel(LLViewerShaderMgr::SHADER_OBJECT); 
}

void LLDrawPoolMaterials::beginDeferredPass(S32 pass)
{
	LLFastTimer t(FTM_RENDER_MATERIALS);
}

void LLDrawPoolMaterials::endDeferredPass(S32 pass)
{
	LLFastTimer t(FTM_RENDER_MATERIALS);
	LLRenderPass::endRenderPass(pass);
}

void LLDrawPoolMaterials::renderDeferred(S32 pass)
{
	U32 type = LLRenderPass::PASS_MATERIALS;
	LLCullResult::drawinfo_iterator begin = gPipeline.beginRenderMap(type);
	LLCullResult::drawinfo_iterator end = gPipeline.endRenderMap(type);
	
	for (LLCullResult::drawinfo_iterator i = begin; i != end; ++i)
	{
		LLDrawInfo& params = **i;
		
		switch (params.mDiffuseAlphaMode)
		{
			case 0:
				LL_INFOS("Asdf") << "Renderererererrerererrrr!!!~!!!~!~" << LL_ENDL;
				mShader = &gDeferredMaterialShinyNormal;
				break;
			case 1: // Alpha blending not supported in the opaque draw pool.
				return;
			case 2:
				mShader = &gDeferredMaterialShinyNormalAlphaTest;
				break;
			case 3:
				mShader = &gDeferredMaterialShinyNormalEmissive;
				break;
		};
		
		mShader->bind();
		
		mShader->uniform4f(LLShaderMgr::SPECULAR_COLOR, params.mSpecColor.mV[0], params.mSpecColor.mV[1], params.mSpecColor.mV[2], params.mSpecColor.mV[3]);
		mShader->uniform1f(LLShaderMgr::ENVIRONMENT_INTENSITY, params.mEnvIntensity);
		
		params.mNormalMap->addTextureStats(params.mVSize);
		bindNormalMap(params.mNormalMap);
		
		params.mSpecularMap->addTextureStats(params.mVSize);
		bindSpecularMap(params.mNormalMap);
		
		diffuse_channel = mShader->enableTexture(LLShaderMgr::DIFFUSE_MAP);
		pushBatch(params, VERTEX_DATA_MASK, TRUE);
		mShader->disableTexture(LLShaderMgr::DIFFUSE_MAP);
		mShader->unbind();
	}
}

void LLDrawPoolMaterials::bindSpecularMap(LLViewerTexture* tex)
{
	mShader->bindTexture(LLShaderMgr::SPECULAR_MAP, tex);
}

void LLDrawPoolMaterials::bindNormalMap(LLViewerTexture* tex)
{
	mShader->bindTexture(LLShaderMgr::BUMP_MAP, tex);
}

void LLDrawPoolMaterials::beginRenderPass(S32 pass)
{
	LLFastTimer t(FTM_RENDER_MATERIALS);
	
	// Materials isn't supported under forward rendering.
	// Use the simple shaders to handle it instead.
	// This is basically replicated from LLDrawPoolSimple.
	
	if (LLPipeline::sUnderWaterRender)
	{
		mShader = &gObjectSimpleWaterProgram;
	}
	else
	{
		mShader = &gObjectSimpleProgram;
	}
	
	if (mVertexShaderLevel > 0)
	{
		mShader->bind();
	}
	else
	{
		// don't use shaders!
		if (gGLManager.mHasShaderObjects)
		{
			LLGLSLShader::bindNoShader();
		}
	}
}

void LLDrawPoolMaterials::endRenderPass(S32 pass)
{
	LLFastTimer t(FTM_RENDER_MATERIALS);
	stop_glerror();
	LLRenderPass::endRenderPass(pass);
	stop_glerror();
	if (mVertexShaderLevel > 0)
	{
		mShader->unbind();
	}
}

void LLDrawPoolMaterials::render(S32 pass)
{
	LLGLDisable blend(GL_BLEND);
	
	{ //render simple
		LLFastTimer t(FTM_RENDER_MATERIALS);
		gPipeline.enableLightsDynamic();
		
		if (mVertexShaderLevel > 0)
		{
			U32 mask = getVertexDataMask() | LLVertexBuffer::MAP_TEXTURE_INDEX;
			
			pushBatches(LLRenderPass::PASS_MATERIALS, mask, TRUE, TRUE);
		}
		else
		{
			LLGLDisable alpha_test(GL_ALPHA_TEST);
			renderTexture(LLRenderPass::PASS_MATERIALS, getVertexDataMask());
		}
	}
}


void LLDrawPoolMaterials::pushBatch(LLDrawInfo& params, U32 mask, BOOL texture, BOOL batch_textures)
{
	applyModelMatrix(params);
	
	bool tex_setup = false;
	
	if (batch_textures && params.mTextureList.size() > 1)
	{
		for (U32 i = 0; i < params.mTextureList.size(); ++i)
		{
			if (params.mTextureList[i].notNull())
			{
				gGL.getTexUnit(i)->bind(params.mTextureList[i], TRUE);
			}
		}
	}
	else
	{ //not batching textures or batch has only 1 texture -- might need a texture matrix
		if (params.mTextureMatrix)
		{
			//if (mShiny)
			{
				gGL.getTexUnit(0)->activate();
				gGL.matrixMode(LLRender::MM_TEXTURE);
			}
			
			gGL.loadMatrix((GLfloat*) params.mTextureMatrix->mMatrix);
			gPipeline.mTextureMatrixOps++;
			
			tex_setup = true;
		}
		
		if (mVertexShaderLevel > 1 && texture)
		{
			if (params.mTexture.notNull())
			{
				gGL.getTexUnit(diffuse_channel)->bind(params.mTexture);
				params.mTexture->addTextureStats(params.mVSize);
			}
			else
			{
				gGL.getTexUnit(diffuse_channel)->unbind(LLTexUnit::TT_TEXTURE);
			}
		}
	}
	
	if (params.mGroup)
	{
		params.mGroup->rebuildMesh();
	}
	params.mVertexBuffer->setBuffer(mask);
	params.mVertexBuffer->drawRange(params.mDrawMode, params.mStart, params.mEnd, params.mCount, params.mOffset);
	gPipeline.addTrianglesDrawn(params.mCount, params.mDrawMode);
	if (tex_setup)
	{
		gGL.getTexUnit(0)->activate();
		gGL.loadIdentity();
		gGL.matrixMode(LLRender::MM_MODELVIEW);
	}
}