blob: 803e8faad2261b7d532830a4a350d07165ba6e0e [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00003// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Program.cpp: Implements the gl::Program class. Implements GL program objects
9// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
10
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000011#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000012#include "libGLESv2/ProgramBinary.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000013#include "libGLESv2/renderer/ShaderExecutable.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000014
15#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000016#include "common/version.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000017#include "utilities.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include "libGLESv2/main.h"
20#include "libGLESv2/Shader.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000021#include "libGLESv2/Program.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000022#include "libGLESv2/renderer/Renderer.h"
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000023#include "libGLESv2/renderer/VertexDataManager.h"
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +000024#include "libGLESv2/Context.h"
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000025
daniel@transgaming.com88853c52012-12-20 20:56:40 +000026#undef near
27#undef far
28
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000029namespace gl
30{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000031std::string str(int i)
32{
33 char buffer[20];
34 snprintf(buffer, sizeof(buffer), "%d", i);
35 return buffer;
36}
37
daniel@transgaming.comdb019952012-12-20 21:13:32 +000038UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
39 : name(name), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000040{
41}
42
daniel@transgaming.come87ca002012-07-24 18:30:43 +000043unsigned int ProgramBinary::mCurrentSerial = 1;
44
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000045ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000046{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000047 mPixelExecutable = NULL;
48 mVertexExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000049 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000050
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000051 mValidated = false;
52
53 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
54 {
55 mSemanticIndex[index] = -1;
56 }
57
58 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
59 {
60 mSamplersPS[index].active = false;
61 }
62
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064 {
65 mSamplersVS[index].active = false;
66 }
67
68 mUsedVertexSamplerRange = 0;
69 mUsedPixelSamplerRange = 0;
shannon.woods@transgaming.com962d4be2013-01-25 21:55:18 +000070 mUsesPointSize = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000071}
72
73ProgramBinary::~ProgramBinary()
74{
daniel@transgaming.com95892412012-11-28 20:59:09 +000075 delete mPixelExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000076 mPixelExecutable = NULL;
77
daniel@transgaming.com95892412012-11-28 20:59:09 +000078 delete mVertexExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000079 mVertexExecutable = NULL;
80
81 delete mGeometryExecutable;
82 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000083
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000084 while (!mUniforms.empty())
85 {
86 delete mUniforms.back();
87 mUniforms.pop_back();
88 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000089}
90
daniel@transgaming.come87ca002012-07-24 18:30:43 +000091unsigned int ProgramBinary::getSerial() const
92{
93 return mSerial;
94}
95
96unsigned int ProgramBinary::issueSerial()
97{
98 return mCurrentSerial++;
99}
100
daniel@transgaming.com95892412012-11-28 20:59:09 +0000101rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000102{
103 return mPixelExecutable;
104}
105
daniel@transgaming.com95892412012-11-28 20:59:09 +0000106rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000107{
108 return mVertexExecutable;
109}
110
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000111rx::ShaderExecutable *ProgramBinary::getGeometryExecutable()
112{
113 return mGeometryExecutable;
114}
115
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000116GLuint ProgramBinary::getAttributeLocation(const char *name)
117{
118 if (name)
119 {
120 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
121 {
122 if (mLinkedAttribute[index].name == std::string(name))
123 {
124 return index;
125 }
126 }
127 }
128
129 return -1;
130}
131
132int ProgramBinary::getSemanticIndex(int attributeIndex)
133{
134 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
135
136 return mSemanticIndex[attributeIndex];
137}
138
139// Returns one more than the highest sampler index used.
140GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
141{
142 switch (type)
143 {
144 case SAMPLER_PIXEL:
145 return mUsedPixelSamplerRange;
146 case SAMPLER_VERTEX:
147 return mUsedVertexSamplerRange;
148 default:
149 UNREACHABLE();
150 return 0;
151 }
152}
153
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000154bool ProgramBinary::usesPointSize() const
155{
156 return mUsesPointSize;
157}
158
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000159bool ProgramBinary::usesPointSpriteEmulation() const
160{
161 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
162}
163
164bool ProgramBinary::usesGeometryShader() const
165{
166 return usesPointSpriteEmulation();
167}
168
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000169// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
170// index (0-15 for the pixel shader and 0-3 for the vertex shader).
171GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
172{
173 GLint logicalTextureUnit = -1;
174
175 switch (type)
176 {
177 case SAMPLER_PIXEL:
178 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
179
180 if (mSamplersPS[samplerIndex].active)
181 {
182 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
183 }
184 break;
185 case SAMPLER_VERTEX:
186 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
187
188 if (mSamplersVS[samplerIndex].active)
189 {
190 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
191 }
192 break;
193 default: UNREACHABLE();
194 }
195
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000196 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000197 {
198 return logicalTextureUnit;
199 }
200
201 return -1;
202}
203
204// Returns the texture type for a given Direct3D 9 sampler type and
205// index (0-15 for the pixel shader and 0-3 for the vertex shader).
206TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
207{
208 switch (type)
209 {
210 case SAMPLER_PIXEL:
211 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
212 ASSERT(mSamplersPS[samplerIndex].active);
213 return mSamplersPS[samplerIndex].textureType;
214 case SAMPLER_VERTEX:
215 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
216 ASSERT(mSamplersVS[samplerIndex].active);
217 return mSamplersVS[samplerIndex].textureType;
218 default: UNREACHABLE();
219 }
220
221 return TEXTURE_2D;
222}
223
224GLint ProgramBinary::getUniformLocation(std::string name)
225{
226 unsigned int subscript = 0;
227
228 // Strip any trailing array operator and retrieve the subscript
229 size_t open = name.find_last_of('[');
230 size_t close = name.find_last_of(']');
231 if (open != std::string::npos && close == name.length() - 1)
232 {
233 subscript = atoi(name.substr(open + 1).c_str());
234 name.erase(open);
235 }
236
237 unsigned int numUniforms = mUniformIndex.size();
238 for (unsigned int location = 0; location < numUniforms; location++)
239 {
240 if (mUniformIndex[location].name == name &&
241 mUniformIndex[location].element == subscript)
242 {
243 return location;
244 }
245 }
246
247 return -1;
248}
249
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000250template <typename T>
251bool ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000252{
253 if (location < 0 || location >= (int)mUniformIndex.size())
254 {
255 return false;
256 }
257
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000258 const int components = UniformComponentCount(targetUniformType);
259 const GLenum targetBoolType = UniformBoolVectorType(targetUniformType);
260
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000261 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
262 targetUniform->dirty = true;
263
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000264 int elementCount = targetUniform->elementCount();
265
266 if (elementCount == 1 && count > 1)
267 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
268
269 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
270
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000271 if (targetUniform->type == targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000272 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000273 T *target = (T*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000274
275 for (int i = 0; i < count; i++)
276 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000277 for (int c = 0; c < components; c++)
278 {
279 target[c] = v[c];
280 }
281 for (int c = components; c < 4; c++)
282 {
283 target[c] = 0;
284 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000285 target += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000286 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000287 }
288 }
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000289 else if (targetUniform->type == targetBoolType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000290 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000291 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000292
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000293 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000294 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000295 for (int c = 0; c < components; c++)
296 {
297 boolParams[c] = (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
298 }
299 for (int c = components; c < 4; c++)
300 {
301 boolParams[c] = GL_FALSE;
302 }
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000303 boolParams += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000304 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000305 }
306 }
307 else
308 {
309 return false;
310 }
311
312 return true;
313}
314
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000315bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
316{
317 return setUniform(location, count, v, GL_FLOAT);
318}
319
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000320bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
321{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000322 return setUniform(location, count, v, GL_FLOAT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000323}
324
325bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
326{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000327 return setUniform(location, count, v, GL_FLOAT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000328}
329
330bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
331{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000332 return setUniform(location, count, v, GL_FLOAT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000333}
334
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000335template<typename T>
336void transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000337{
338 int copyWidth = std::min(targetWidth, srcWidth);
339 int copyHeight = std::min(targetHeight, srcHeight);
340
341 for (int x = 0; x < copyWidth; x++)
342 {
343 for (int y = 0; y < copyHeight; y++)
344 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000345 target[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000346 }
347 }
348 // clear unfilled right side
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000349 for (int y = 0; y < copyWidth; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000350 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000351 for (int x = copyHeight; x < targetWidth; x++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000352 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000353 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000354 }
355 }
356 // clear unfilled bottom.
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000357 for (int y = copyWidth; y < targetHeight; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000358 {
359 for (int x = 0; x < targetWidth; x++)
360 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000361 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000362 }
363 }
364}
365
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000366template<typename T>
367void expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
368{
369 int copyWidth = std::min(targetWidth, srcWidth);
370 int copyHeight = std::min(targetHeight, srcHeight);
371
372 for (int y = 0; y < copyHeight; y++)
373 {
374 for (int x = 0; x < copyWidth; x++)
375 {
376 target[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
377 }
378 }
379 // clear unfilled right side
380 for (int y = 0; y < copyHeight; y++)
381 {
382 for (int x = copyWidth; x < targetWidth; x++)
383 {
384 target[y * targetWidth + x] = static_cast<T>(0);
385 }
386 }
387 // clear unfilled bottom.
388 for (int y = copyHeight; y < targetHeight; y++)
389 {
390 for (int x = 0; x < targetWidth; x++)
391 {
392 target[y * targetWidth + x] = static_cast<T>(0);
393 }
394 }
395}
396
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000397template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000398bool ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000399{
400 if (location < 0 || location >= (int)mUniformIndex.size())
401 {
402 return false;
403 }
404
405 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
406 targetUniform->dirty = true;
407
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000408 if (targetUniform->type != targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000409 {
410 return false;
411 }
412
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000413 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000414
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000415 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000416 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
417
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000418 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000419 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4 * rows);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000420
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000421 for (int i = 0; i < count; i++)
422 {
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000423 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
424 if (transpose == GL_FALSE)
425 {
426 transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols);
427 }
428 else
429 {
430 expandMatrix<GLfloat>(target, value, 4, rows, cols, rows);
431 }
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000432 target += 4 * rows;
433 value += cols * rows;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000434 }
435
436 return true;
437}
438
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000439bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000440{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000441 return setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000442}
443
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000444bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000445{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000446 return setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000447}
448
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000449bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000450{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000451 return setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000452}
453
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000454bool ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000455{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000456 return setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000457}
458
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000459bool ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000460{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000461 return setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000462}
463
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000464bool ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000465{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000466 return setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000467}
468
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000469bool ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000470{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000471 return setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000472}
473
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000474bool ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000475{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000476 return setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000477}
478
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000479bool ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000480{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000481 return setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000482}
483
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000484bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
485{
486 if (location < 0 || location >= (int)mUniformIndex.size())
487 {
488 return false;
489 }
490
491 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
492 targetUniform->dirty = true;
493
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000494 int elementCount = targetUniform->elementCount();
495
496 if (elementCount == 1 && count > 1)
497 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
498
499 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
500
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000501 if (targetUniform->type == GL_INT ||
502 targetUniform->type == GL_SAMPLER_2D ||
503 targetUniform->type == GL_SAMPLER_CUBE)
504 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000505 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000506
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000507 for (int i = 0; i < count; i++)
508 {
509 target[0] = v[0];
510 target[1] = 0;
511 target[2] = 0;
512 target[3] = 0;
513 target += 4;
514 v += 1;
515 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000516 }
517 else if (targetUniform->type == GL_BOOL)
518 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000519 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000520
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000521 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000522 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000523 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
524 boolParams[1] = GL_FALSE;
525 boolParams[2] = GL_FALSE;
526 boolParams[3] = GL_FALSE;
527 boolParams += 4;
528 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000529 }
530 }
531 else
532 {
533 return false;
534 }
535
536 return true;
537}
538
539bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
540{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000541 return setUniform(location, count, v, GL_INT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000542}
543
544bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
545{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000546 return setUniform(location, count, v, GL_INT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000547}
548
549bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
550{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000551 return setUniform(location, count, v, GL_INT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000552}
553
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000554bool ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
555{
556 return setUniform(location, count, v, GL_UNSIGNED_INT);
557}
558
559bool ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
560{
561 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
562}
563
564bool ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
565{
566 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
567}
568
569bool ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
570{
571 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
572}
573
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000574template <typename T>
575bool ProgramBinary::getUniformv(GLint location, GLsizei *bufSize, T *params, GLenum uniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000576{
577 if (location < 0 || location >= (int)mUniformIndex.size())
578 {
579 return false;
580 }
581
582 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
583
584 // sized queries -- ensure the provided buffer is large enough
585 if (bufSize)
586 {
587 int requiredBytes = UniformExternalSize(targetUniform->type);
588 if (*bufSize < requiredBytes)
589 {
590 return false;
591 }
592 }
593
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000594 if (IsMatrixType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000595 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000596 const int rows = VariableRowCount(targetUniform->type);
597 const int cols = VariableColumnCount(targetUniform->type);
598 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, cols, rows, 4, rows);
599 }
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000600 else if (uniformType == UniformComponentType(targetUniform->type))
601 {
602 unsigned int size = UniformComponentCount(targetUniform->type);
603 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
604 size * sizeof(T));
605 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000606 else
607 {
608 unsigned int size = UniformComponentCount(targetUniform->type);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000609 switch (UniformComponentType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000610 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000611 case GL_BOOL:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000612 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000613 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000614
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000615 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000616 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000617 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000618 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000619 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000620 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000621
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000622 case GL_FLOAT:
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000623 {
624 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
625
626 for (unsigned int i = 0; i < size; i++)
627 {
628 params[i] = static_cast<T>(floatParams[i]);
629 }
630 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000631 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000632
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000633 case GL_INT:
634 {
635 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
636
637 for (unsigned int i = 0; i < size; i++)
638 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000639 params[i] = static_cast<T>(intParams[i]);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000640 }
641 }
642 break;
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000643
644 case GL_UNSIGNED_INT:
645 {
646 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000647
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000648 for (unsigned int i = 0; i < size; i++)
649 {
650 params[i] = static_cast<T>(uintParams[i]);
651 }
652 }
653 break;
654
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000655 default: UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000656 }
657 }
658
659 return true;
660}
661
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000662bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
663{
664 return getUniformv(location, bufSize, params, GL_FLOAT);
665}
666
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000667bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
668{
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000669 return getUniformv(location, bufSize, params, GL_INT);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000670}
671
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000672bool ProgramBinary::getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params)
673{
674 return getUniformv(location, bufSize, params, GL_UNSIGNED_INT);
675}
676
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000677void ProgramBinary::dirtyAllUniforms()
678{
679 unsigned int numUniforms = mUniforms.size();
680 for (unsigned int index = 0; index < numUniforms; index++)
681 {
682 mUniforms[index]->dirty = true;
683 }
684}
685
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000686// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000687void ProgramBinary::applyUniforms()
688{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000689 // Retrieve sampler uniform values
690 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
691 {
692 Uniform *targetUniform = *ub;
693
694 if (targetUniform->dirty)
695 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000696 if (targetUniform->type == GL_SAMPLER_2D ||
697 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000698 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000699 int count = targetUniform->elementCount();
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000700 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000701
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000702 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000703 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000704 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000705
706 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000707 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000708 unsigned int samplerIndex = firstIndex + i;
709
710 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000711 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000712 ASSERT(mSamplersPS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000713 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000714 }
715 }
716 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000717
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000718 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000719 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000720 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000721
722 for (int i = 0; i < count; i++)
723 {
724 unsigned int samplerIndex = firstIndex + i;
725
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000726 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000727 {
728 ASSERT(mSamplersVS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000729 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000730 }
731 }
732 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000733 }
734 }
735 }
736
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +0000737 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000738}
739
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000740// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
741// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000742int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000743{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000744 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000745
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000746 fragmentShader->resetVaryingsRegisterAssignment();
747
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000748 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
749 {
750 int n = VariableRowCount(varying->type) * varying->size;
751 int m = VariableColumnCount(varying->type);
752 bool success = false;
753
754 if (m == 2 || m == 3 || m == 4)
755 {
756 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
757 {
758 bool available = true;
759
760 for (int y = 0; y < n && available; y++)
761 {
762 for (int x = 0; x < m && available; x++)
763 {
764 if (packing[r + y][x])
765 {
766 available = false;
767 }
768 }
769 }
770
771 if (available)
772 {
773 varying->reg = r;
774 varying->col = 0;
775
776 for (int y = 0; y < n; y++)
777 {
778 for (int x = 0; x < m; x++)
779 {
780 packing[r + y][x] = &*varying;
781 }
782 }
783
784 success = true;
785 }
786 }
787
788 if (!success && m == 2)
789 {
790 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
791 {
792 bool available = true;
793
794 for (int y = 0; y < n && available; y++)
795 {
796 for (int x = 2; x < 4 && available; x++)
797 {
798 if (packing[r + y][x])
799 {
800 available = false;
801 }
802 }
803 }
804
805 if (available)
806 {
807 varying->reg = r;
808 varying->col = 2;
809
810 for (int y = 0; y < n; y++)
811 {
812 for (int x = 2; x < 4; x++)
813 {
814 packing[r + y][x] = &*varying;
815 }
816 }
817
818 success = true;
819 }
820 }
821 }
822 }
823 else if (m == 1)
824 {
825 int space[4] = {0};
826
827 for (int y = 0; y < maxVaryingVectors; y++)
828 {
829 for (int x = 0; x < 4; x++)
830 {
831 space[x] += packing[y][x] ? 0 : 1;
832 }
833 }
834
835 int column = 0;
836
837 for (int x = 0; x < 4; x++)
838 {
839 if (space[x] >= n && space[x] < space[column])
840 {
841 column = x;
842 }
843 }
844
845 if (space[column] >= n)
846 {
847 for (int r = 0; r < maxVaryingVectors; r++)
848 {
849 if (!packing[r][column])
850 {
851 varying->reg = r;
852
853 for (int y = r; y < r + n; y++)
854 {
855 packing[y][column] = &*varying;
856 }
857
858 break;
859 }
860 }
861
862 varying->col = column;
863
864 success = true;
865 }
866 }
867 else UNREACHABLE();
868
869 if (!success)
870 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000871 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000872
873 return -1;
874 }
875 }
876
877 // Return the number of used registers
878 int registers = 0;
879
880 for (int r = 0; r < maxVaryingVectors; r++)
881 {
882 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
883 {
884 registers++;
885 }
886 }
887
888 return registers;
889}
890
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000891bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
892 std::string& pixelHLSL, std::string& vertexHLSL,
893 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000894{
895 if (pixelHLSL.empty() || vertexHLSL.empty())
896 {
897 return false;
898 }
899
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000900 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
901 bool usesFragColor = fragmentShader->mUsesFragColor;
902 bool usesFragData = fragmentShader->mUsesFragData;
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +0000903 if (usesFragColor && usesFragData)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000904 {
905 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
906 return false;
907 }
908
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000909 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000910 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000911 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000912
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000913 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
914
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +0000915 // Two cases when writing to gl_FragColor and using ESSL 1.0:
916 // - with a 3.0 context, the output color is copied to channel 0
917 // - with a 2.0 context, the output color is broadcast to all channels
918 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
919 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
920
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000921 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000922 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000923 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000924
925 return false;
926 }
927
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000928 vertexShader->resetVaryingsRegisterAssignment();
929
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000930 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
931 {
932 bool matched = false;
933
934 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
935 {
936 if (output->name == input->name)
937 {
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000938 if (output->type != input->type || output->size != input->size || output->interpolation != input->interpolation)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000939 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000940 infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000941
942 return false;
943 }
944
945 output->reg = input->reg;
946 output->col = input->col;
947
948 matched = true;
949 break;
950 }
951 }
952
953 if (!matched)
954 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000955 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000956
957 return false;
958 }
959 }
960
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000961 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000962 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000963 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000964 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
965
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +0000966 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
967
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000968 // special varyings that use reserved registers
969 int reservedRegisterIndex = registers;
970 std::string fragCoordSemantic;
971 std::string pointCoordSemantic;
972
973 if (fragmentShader->mUsesFragCoord)
974 {
975 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
976 }
977
978 if (fragmentShader->mUsesPointCoord)
979 {
980 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
981 // In DX11 we compute this in the GS.
982 if (shaderModel == 3)
983 {
984 pointCoordSemantic = "TEXCOORD0";
985 }
986 else if (shaderModel >= 4)
987 {
988 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
989 }
990 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000991
992 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +0000993 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000994
995 int semanticIndex = 0;
996 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
997 {
998 switch (attribute->type)
999 {
1000 case GL_FLOAT: vertexHLSL += " float "; break;
1001 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1002 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1003 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1004 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1005 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1006 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1007 default: UNREACHABLE();
1008 }
1009
1010 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1011
1012 semanticIndex += VariableRowCount(attribute->type);
1013 }
1014
1015 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001016 "\n"
1017 "struct VS_OUTPUT\n"
1018 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001019
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001020 if (shaderModel < 4)
1021 {
1022 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1023 }
1024
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001025 vertexHLSL += varyingHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001026
1027 if (fragmentShader->mUsesFragCoord)
1028 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001029 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001030 }
1031
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001032 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001033 {
1034 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1035 }
1036
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001037 if (shaderModel >= 4)
1038 {
1039 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1040 }
1041
1042 vertexHLSL += "};\n"
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001043 "\n"
1044 "VS_OUTPUT main(VS_INPUT input)\n"
1045 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001046
1047 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1048 {
1049 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1050
1051 if (VariableRowCount(attribute->type) > 1) // Matrix
1052 {
1053 vertexHLSL += "transpose";
1054 }
1055
1056 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1057 }
1058
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001059 if (shaderModel >= 4)
1060 {
1061 vertexHLSL += "\n"
1062 " gl_main();\n"
1063 "\n"
1064 " VS_OUTPUT output;\n"
1065 " output.gl_Position.x = gl_Position.x;\n"
1066 " output.gl_Position.y = -gl_Position.y;\n"
1067 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1068 " output.gl_Position.w = gl_Position.w;\n";
1069 }
1070 else
1071 {
1072 vertexHLSL += "\n"
1073 " gl_main();\n"
1074 "\n"
1075 " VS_OUTPUT output;\n"
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001076 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1077 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001078 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1079 " output.gl_Position.w = gl_Position.w;\n";
1080 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001081
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001082 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001083 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001084 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001085 }
1086
1087 if (fragmentShader->mUsesFragCoord)
1088 {
1089 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1090 }
1091
1092 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1093 {
1094 if (varying->reg >= 0)
1095 {
1096 for (int i = 0; i < varying->size; i++)
1097 {
1098 int rows = VariableRowCount(varying->type);
1099
1100 for (int j = 0; j < rows; j++)
1101 {
1102 int r = varying->reg + i * rows + j;
1103 vertexHLSL += " output.v" + str(r);
1104
1105 bool sharedRegister = false; // Register used by multiple varyings
1106
1107 for (int x = 0; x < 4; x++)
1108 {
1109 if (packing[r][x] && packing[r][x] != packing[r][0])
1110 {
1111 sharedRegister = true;
1112 break;
1113 }
1114 }
1115
1116 if(sharedRegister)
1117 {
1118 vertexHLSL += ".";
1119
1120 for (int x = 0; x < 4; x++)
1121 {
1122 if (packing[r][x] == &*varying)
1123 {
1124 switch(x)
1125 {
1126 case 0: vertexHLSL += "x"; break;
1127 case 1: vertexHLSL += "y"; break;
1128 case 2: vertexHLSL += "z"; break;
1129 case 3: vertexHLSL += "w"; break;
1130 }
1131 }
1132 }
1133 }
1134
1135 vertexHLSL += " = " + varying->name;
1136
1137 if (varying->array)
1138 {
1139 vertexHLSL += "[" + str(i) + "]";
1140 }
1141
1142 if (rows > 1)
1143 {
1144 vertexHLSL += "[" + str(j) + "]";
1145 }
1146
1147 vertexHLSL += ";\n";
1148 }
1149 }
1150 }
1151 }
1152
1153 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001154 " return output;\n"
1155 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001156
1157 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001158 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001159
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001160 pixelHLSL += varyingHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001161
1162 if (fragmentShader->mUsesFragCoord)
1163 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001164 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001165 }
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001166
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001167 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1168 {
1169 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1170 }
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001171
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001172 // Must consume the PSIZE element if the geometry shader is not active
1173 // We won't know if we use a GS until we draw
1174 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1175 {
1176 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1177 }
1178
1179 if (fragmentShader->mUsesFragCoord)
1180 {
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001181 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001182 {
1183 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1184 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001185 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001186 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001187 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1188 }
1189 }
1190
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001191 pixelHLSL += "};\n"
1192 "\n"
1193 "struct PS_OUTPUT\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001194 "{\n";
1195
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001196 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001197 {
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001198 pixelHLSL += " float4 gl_Color" + str(renderTargetIndex) + " : " + targetSemantic + str(renderTargetIndex) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001199 }
1200
1201 pixelHLSL += "};\n"
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001202 "\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001203
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001204 if (fragmentShader->mUsesFrontFacing)
1205 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001206 if (shaderModel >= 4)
1207 {
1208 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1209 "{\n";
1210 }
1211 else
1212 {
1213 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1214 "{\n";
1215 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001216 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001217 else
1218 {
1219 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1220 "{\n";
1221 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001222
1223 if (fragmentShader->mUsesFragCoord)
1224 {
1225 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1226
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001227 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001228 {
1229 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1230 " gl_FragCoord.y = input.dx_VPos.y;\n";
1231 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001232 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001233 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001234 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001235 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001236 }
1237 else
1238 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001239 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1240 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1241 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001242 }
1243
daniel@transgaming.com12985182012-12-20 20:56:31 +00001244 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001245 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001246 }
1247
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001248 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001249 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001250 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1251 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001252 }
1253
1254 if (fragmentShader->mUsesFrontFacing)
1255 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001256 if (shaderModel <= 3)
1257 {
1258 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1259 }
1260 else
1261 {
1262 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1263 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001264 }
1265
1266 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1267 {
1268 if (varying->reg >= 0)
1269 {
1270 for (int i = 0; i < varying->size; i++)
1271 {
1272 int rows = VariableRowCount(varying->type);
1273 for (int j = 0; j < rows; j++)
1274 {
1275 std::string n = str(varying->reg + i * rows + j);
1276 pixelHLSL += " " + varying->name;
1277
1278 if (varying->array)
1279 {
1280 pixelHLSL += "[" + str(i) + "]";
1281 }
1282
1283 if (rows > 1)
1284 {
1285 pixelHLSL += "[" + str(j) + "]";
1286 }
1287
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001288 switch (VariableColumnCount(varying->type))
1289 {
1290 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1291 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1292 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1293 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1294 default: UNREACHABLE();
1295 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001296 }
1297 }
1298 }
1299 else UNREACHABLE();
1300 }
1301
1302 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001303 " gl_main();\n"
1304 "\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001305 " PS_OUTPUT output;\n";
1306
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001307 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001308 {
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001309 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001310
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001311 pixelHLSL += " output.gl_Color" + str(renderTargetIndex) + " = gl_Color[" + str(sourceColorIndex) + "];\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001312 }
1313
1314 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001315 " return output;\n"
1316 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001317
1318 return true;
1319}
1320
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001321std::string ProgramBinary::generateVaryingHLSL(FragmentShader *fragmentShader, const std::string &varyingSemantic) const
1322{
1323 std::string varyingHLSL;
1324
1325 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1326 {
1327 if (varying->reg >= 0)
1328 {
1329 for (int i = 0; i < varying->size; i++)
1330 {
1331 int rows = VariableRowCount(varying->type);
1332 for (int j = 0; j < rows; j++)
1333 {
1334 switch (varying->interpolation)
1335 {
1336 case Smooth: varyingHLSL += " "; break;
1337 case Flat: varyingHLSL += " nointerpolation "; break;
1338 case Centroid: varyingHLSL += " centroid "; break;
1339 default: UNREACHABLE();
1340 }
1341
1342 std::string n = str(varying->reg + i * rows + j);
1343 varyingHLSL += "float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
1344 }
1345 }
1346 }
1347 else UNREACHABLE();
1348 }
1349
1350 return varyingHLSL;
1351}
1352
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001353bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1354{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001355 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001356
1357 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001358 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001359 if (format != GL_PROGRAM_BINARY_ANGLE)
1360 {
1361 infoLog.append("Invalid program binary format.");
1362 return false;
1363 }
1364
1365 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001366 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001367 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001368 {
1369 infoLog.append("Invalid program binary version.");
1370 return false;
1371 }
1372
1373 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1374 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001375 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001376 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001377 stream.read(&name);
1378 mLinkedAttribute[i].name = name;
1379 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001380 }
1381
1382 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1383 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001384 stream.read(&mSamplersPS[i].active);
1385 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001386
1387 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001388 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001389 mSamplersPS[i].textureType = (TextureType) textureType;
1390 }
1391
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001392 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001393 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001394 stream.read(&mSamplersVS[i].active);
1395 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001396
1397 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001398 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001399 mSamplersVS[i].textureType = (TextureType) textureType;
1400 }
1401
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001402 stream.read(&mUsedVertexSamplerRange);
1403 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001404 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001405
shannon.woods@transgaming.com45886d62013-02-28 23:19:20 +00001406 size_t size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001407 stream.read(&size);
1408 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001409 {
1410 infoLog.append("Invalid program binary.");
1411 return false;
1412 }
1413
1414 mUniforms.resize(size);
1415 for (unsigned int i = 0; i < size; ++i)
1416 {
1417 GLenum type;
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001418 GLenum precision;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001419 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001420 unsigned int arraySize;
1421
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001422 stream.read(&type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001423 stream.read(&precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001424 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001425 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001426
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001427 mUniforms[i] = new Uniform(type, precision, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001428
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001429 stream.read(&mUniforms[i]->psRegisterIndex);
1430 stream.read(&mUniforms[i]->vsRegisterIndex);
1431 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001432 }
1433
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001434 stream.read(&size);
1435 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001436 {
1437 infoLog.append("Invalid program binary.");
1438 return false;
1439 }
1440
1441 mUniformIndex.resize(size);
1442 for (unsigned int i = 0; i < size; ++i)
1443 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001444 stream.read(&mUniformIndex[i].name);
1445 stream.read(&mUniformIndex[i].element);
1446 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001447 }
1448
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001449 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001450 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001451
1452 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001453 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001454
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001455 unsigned int geometryShaderSize;
1456 stream.read(&geometryShaderSize);
1457
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001458 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001459
daniel@transgaming.com36038542012-11-28 20:59:26 +00001460 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001461 ptr += sizeof(GUID);
1462
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001463 GUID identifier = mRenderer->getAdapterIdentifier();
1464 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001465 {
1466 infoLog.append("Invalid program binary.");
1467 return false;
1468 }
1469
1470 const char *pixelShaderFunction = ptr;
1471 ptr += pixelShaderSize;
1472
1473 const char *vertexShaderFunction = ptr;
1474 ptr += vertexShaderSize;
1475
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001476 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1477 ptr += geometryShaderSize;
1478
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001479 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001480 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001481 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001482 {
1483 infoLog.append("Could not create pixel shader.");
1484 return false;
1485 }
1486
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001487 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001488 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001489 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001490 {
1491 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001492 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001493 mPixelExecutable = NULL;
1494 return false;
1495 }
1496
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001497 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1498 {
1499 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1500 geometryShaderSize, rx::SHADER_GEOMETRY);
1501 if (!mGeometryExecutable)
1502 {
1503 infoLog.append("Could not create geometry shader.");
1504 delete mPixelExecutable;
1505 mPixelExecutable = NULL;
1506 delete mVertexExecutable;
1507 mVertexExecutable = NULL;
1508 return false;
1509 }
1510 }
1511 else
1512 {
1513 mGeometryExecutable = NULL;
1514 }
1515
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001516 return true;
1517}
1518
1519bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1520{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001521 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001522
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001523 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001524 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001525
1526 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1527 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001528 stream.write(mLinkedAttribute[i].type);
1529 stream.write(mLinkedAttribute[i].name);
1530 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001531 }
1532
1533 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1534 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001535 stream.write(mSamplersPS[i].active);
1536 stream.write(mSamplersPS[i].logicalTextureUnit);
1537 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001538 }
1539
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001540 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001541 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001542 stream.write(mSamplersVS[i].active);
1543 stream.write(mSamplersVS[i].logicalTextureUnit);
1544 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001545 }
1546
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001547 stream.write(mUsedVertexSamplerRange);
1548 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001549 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001550
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1553 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001554 stream.write(mUniforms[i]->type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001555 stream.write(mUniforms[i]->precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001556 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001557 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001558
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001559 stream.write(mUniforms[i]->psRegisterIndex);
1560 stream.write(mUniforms[i]->vsRegisterIndex);
1561 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001562 }
1563
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001564 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001565 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1566 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001567 stream.write(mUniformIndex[i].name);
1568 stream.write(mUniformIndex[i].element);
1569 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001570 }
1571
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001572 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001573 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001575 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001576 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001577
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001578 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1579 stream.write(geometryShaderSize);
1580
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001581 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001582
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001583 GLsizei streamLength = stream.length();
1584 const void *streamData = stream.data();
1585
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001586 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001587 if (totalLength > bufSize)
1588 {
1589 if (length)
1590 {
1591 *length = 0;
1592 }
1593
1594 return false;
1595 }
1596
1597 if (binary)
1598 {
1599 char *ptr = (char*) binary;
1600
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001601 memcpy(ptr, streamData, streamLength);
1602 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001603
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001604 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001605 ptr += sizeof(GUID);
1606
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001607 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001608 ptr += pixelShaderSize;
1609
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001610 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001611 ptr += vertexShaderSize;
1612
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001613 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1614 {
1615 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1616 ptr += geometryShaderSize;
1617 }
1618
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619 ASSERT(ptr - totalLength == binary);
1620 }
1621
1622 if (length)
1623 {
1624 *length = totalLength;
1625 }
1626
1627 return true;
1628}
1629
1630GLint ProgramBinary::getLength()
1631{
1632 GLint length;
1633 if (save(NULL, INT_MAX, &length))
1634 {
1635 return length;
1636 }
1637 else
1638 {
1639 return 0;
1640 }
1641}
1642
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001643bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001644{
1645 if (!fragmentShader || !fragmentShader->isCompiled())
1646 {
1647 return false;
1648 }
1649
1650 if (!vertexShader || !vertexShader->isCompiled())
1651 {
1652 return false;
1653 }
1654
1655 std::string pixelHLSL = fragmentShader->getHLSL();
1656 std::string vertexHLSL = vertexShader->getHLSL();
1657
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001658 // Map the varyings to the register file
1659 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1660 int registers = packVaryings(infoLog, packing, fragmentShader);
1661
1662 if (registers < 0)
1663 {
1664 return false;
1665 }
1666
1667 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001668 {
1669 return false;
1670 }
1671
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001672 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001673 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1674 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001675
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001676 if (usesGeometryShader())
1677 {
1678 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1679 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY);
1680 }
1681
1682 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001683 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001684 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001685 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001686
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001687 delete mVertexExecutable;
1688 mVertexExecutable = NULL;
1689 delete mPixelExecutable;
1690 mPixelExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001691 delete mGeometryExecutable;
1692 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001693 }
1694
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001695 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1696 {
1697 success = false;
1698 }
1699
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001700 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001701 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001702 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001703 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001704
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001705 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1706 if (vertexShader->mUsesDepthRange || fragmentShader->mUsesDepthRange)
1707 {
1708 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0));
1709 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0));
1710 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0));
1711 }
1712
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001713 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001714}
1715
1716// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001717bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001718{
1719 unsigned int usedLocations = 0;
1720
1721 // Link attributes that have a binding location
1722 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1723 {
1724 int location = attributeBindings.getAttributeBinding(attribute->name);
1725
1726 if (location != -1) // Set by glBindAttribLocation
1727 {
1728 if (!mLinkedAttribute[location].name.empty())
1729 {
1730 // Multiple active attributes bound to the same location; not an error
1731 }
1732
1733 mLinkedAttribute[location] = *attribute;
1734
1735 int rows = VariableRowCount(attribute->type);
1736
1737 if (rows + location > MAX_VERTEX_ATTRIBS)
1738 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001739 infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001740
1741 return false;
1742 }
1743
1744 for (int i = 0; i < rows; i++)
1745 {
1746 usedLocations |= 1 << (location + i);
1747 }
1748 }
1749 }
1750
1751 // Link attributes that don't have a binding location
1752 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1753 {
1754 int location = attributeBindings.getAttributeBinding(attribute->name);
1755
1756 if (location == -1) // Not set by glBindAttribLocation
1757 {
1758 int rows = VariableRowCount(attribute->type);
1759 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1760
1761 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1762 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001763 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001764
1765 return false; // Fail to link
1766 }
1767
1768 mLinkedAttribute[availableIndex] = *attribute;
1769 }
1770 }
1771
1772 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1773 {
1774 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1775 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1776
1777 for (int r = 0; r < rows; r++)
1778 {
1779 mSemanticIndex[attributeIndex++] = index++;
1780 }
1781 }
1782
1783 return true;
1784}
1785
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001786bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001787{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001788 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001789 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001790 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001791 {
1792 return false;
1793 }
1794 }
1795
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001796 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001797 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001798 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001799 {
1800 return false;
1801 }
1802 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001803
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001804 return true;
1805}
1806
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001807bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001808{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001809 if (constant.type == GL_SAMPLER_2D ||
1810 constant.type == GL_SAMPLER_CUBE)
1811 {
1812 unsigned int samplerIndex = constant.registerIndex;
1813
1814 do
1815 {
1816 if (shader == GL_VERTEX_SHADER)
1817 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001818 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001819 {
1820 mSamplersVS[samplerIndex].active = true;
1821 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1822 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1823 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1824 }
1825 else
1826 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001827 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001828 return false;
1829 }
1830 }
1831 else if (shader == GL_FRAGMENT_SHADER)
1832 {
1833 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1834 {
1835 mSamplersPS[samplerIndex].active = true;
1836 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1837 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1838 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1839 }
1840 else
1841 {
1842 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1843 return false;
1844 }
1845 }
1846 else UNREACHABLE();
1847
1848 samplerIndex++;
1849 }
1850 while (samplerIndex < constant.registerIndex + constant.arraySize);
1851 }
1852
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001853 Uniform *uniform = NULL;
1854 GLint location = getUniformLocation(constant.name);
1855
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001856 if (location >= 0) // Previously defined, type and precision must match
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001857 {
1858 uniform = mUniforms[mUniformIndex[location].index];
1859
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00001860 if (uniform->type != constant.type)
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001861 {
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00001862 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
1863 return false;
1864 }
1865
1866 if (uniform->precision != constant.precision)
1867 {
1868 infoLog.append("Precisions for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001869 return false;
1870 }
1871 }
1872 else
1873 {
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001874 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize);
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001875 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001876
1877 if (!uniform)
1878 {
1879 return false;
1880 }
1881
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001882 if (shader == GL_FRAGMENT_SHADER)
1883 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001884 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001885 }
1886 else if (shader == GL_VERTEX_SHADER)
1887 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001888 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001889 }
1890 else UNREACHABLE();
1891
1892 if (location >= 0)
1893 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001894 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001895 }
1896
1897 mUniforms.push_back(uniform);
1898 unsigned int uniformIndex = mUniforms.size() - 1;
1899
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001900 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001901 {
1902 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
1903 }
1904
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001905 if (shader == GL_VERTEX_SHADER)
1906 {
1907 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
1908 {
1909 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
1910 return false;
1911 }
1912 }
1913 else if (shader == GL_FRAGMENT_SHADER)
1914 {
1915 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
1916 {
1917 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
1918 return false;
1919 }
1920 }
1921 else UNREACHABLE();
1922
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001923 return true;
1924}
1925
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001926std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
1927{
1928 // for now we only handle point sprite emulation
1929 ASSERT(usesPointSpriteEmulation());
1930 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
1931}
1932
1933std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
1934{
1935 ASSERT(registers >= 0);
1936 ASSERT(vertexShader->mUsesPointSize);
1937 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1938
1939 std::string geomHLSL;
1940
1941 std::string varyingSemantic = "TEXCOORD";
1942
1943 std::string fragCoordSemantic;
1944 std::string pointCoordSemantic;
1945
1946 int reservedRegisterIndex = registers;
1947
1948 if (fragmentShader->mUsesFragCoord)
1949 {
1950 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1951 }
1952
1953 if (fragmentShader->mUsesPointCoord)
1954 {
1955 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1956 }
1957
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001958 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1959 "\n"
1960 "struct GS_INPUT\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001961 "{\n";
1962
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001963 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001964
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001965 geomHLSL += varyingHLSL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001966
1967 if (fragmentShader->mUsesFragCoord)
1968 {
1969 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1970 }
1971
1972 geomHLSL += " float gl_PointSize : PSIZE;\n"
1973 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001974 "};\n"
1975 "\n"
1976 "struct GS_OUTPUT\n"
1977 "{\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001978
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001979 geomHLSL += varyingHLSL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001980
1981 if (fragmentShader->mUsesFragCoord)
1982 {
1983 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1984 }
1985
1986 if (fragmentShader->mUsesPointCoord)
1987 {
1988 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1989 }
1990
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001991 geomHLSL += " float gl_PointSize : PSIZE;\n"
1992 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001993 "};\n"
1994 "\n"
1995 "static float2 pointSpriteCorners[] = \n"
1996 "{\n"
1997 " float2( 0.5f, -0.5f),\n"
1998 " float2( 0.5f, 0.5f),\n"
1999 " float2(-0.5f, -0.5f),\n"
2000 " float2(-0.5f, 0.5f)\n"
2001 "};\n"
2002 "\n"
2003 "static float2 pointSpriteTexcoords[] = \n"
2004 "{\n"
2005 " float2(1.0f, 1.0f),\n"
2006 " float2(1.0f, 0.0f),\n"
2007 " float2(0.0f, 1.0f),\n"
2008 " float2(0.0f, 0.0f)\n"
2009 "};\n"
2010 "\n"
2011 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2012 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2013 "\n"
2014 "[maxvertexcount(4)]\n"
2015 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2016 "{\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002017 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2018 " output.gl_PointSize = input[0].gl_PointSize;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002019
2020 for (int r = 0; r < registers; r++)
2021 {
2022 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2023 }
2024
2025 if (fragmentShader->mUsesFragCoord)
2026 {
2027 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2028 }
2029
2030 geomHLSL += " \n"
2031 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2032 " float4 gl_Position = input[0].gl_Position;\n"
shannon.woods@transgaming.com771ca2a2013-02-28 23:14:52 +00002033 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002034
2035 for (int corner = 0; corner < 4; corner++)
2036 {
2037 geomHLSL += " \n"
2038 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2039
2040 if (fragmentShader->mUsesPointCoord)
2041 {
2042 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2043 }
2044
2045 geomHLSL += " outStream.Append(output);\n";
2046 }
2047
2048 geomHLSL += " \n"
2049 " outStream.RestartStrip();\n"
2050 "}\n";
2051
2052 return geomHLSL;
2053}
2054
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002055// This method needs to match OutputHLSL::decorate
2056std::string ProgramBinary::decorateAttribute(const std::string &name)
2057{
2058 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2059 {
2060 return "_" + name;
2061 }
2062
2063 return name;
2064}
2065
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002066bool ProgramBinary::isValidated() const
2067{
2068 return mValidated;
2069}
2070
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002071void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002072{
2073 // Skip over inactive attributes
2074 unsigned int activeAttribute = 0;
2075 unsigned int attribute;
2076 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2077 {
2078 if (mLinkedAttribute[attribute].name.empty())
2079 {
2080 continue;
2081 }
2082
2083 if (activeAttribute == index)
2084 {
2085 break;
2086 }
2087
2088 activeAttribute++;
2089 }
2090
2091 if (bufsize > 0)
2092 {
2093 const char *string = mLinkedAttribute[attribute].name.c_str();
2094
2095 strncpy(name, string, bufsize);
2096 name[bufsize - 1] = '\0';
2097
2098 if (length)
2099 {
2100 *length = strlen(name);
2101 }
2102 }
2103
2104 *size = 1; // Always a single 'type' instance
2105
2106 *type = mLinkedAttribute[attribute].type;
2107}
2108
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002109GLint ProgramBinary::getActiveAttributeCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002110{
2111 int count = 0;
2112
2113 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2114 {
2115 if (!mLinkedAttribute[attributeIndex].name.empty())
2116 {
2117 count++;
2118 }
2119 }
2120
2121 return count;
2122}
2123
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002124GLint ProgramBinary::getActiveAttributeMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002125{
2126 int maxLength = 0;
2127
2128 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2129 {
2130 if (!mLinkedAttribute[attributeIndex].name.empty())
2131 {
2132 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2133 }
2134 }
2135
2136 return maxLength;
2137}
2138
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002139void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002140{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002141 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002142
2143 if (bufsize > 0)
2144 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002145 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002147 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002148 {
2149 string += "[0]";
2150 }
2151
2152 strncpy(name, string.c_str(), bufsize);
2153 name[bufsize - 1] = '\0';
2154
2155 if (length)
2156 {
2157 *length = strlen(name);
2158 }
2159 }
2160
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002161 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002162
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002163 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002164}
2165
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002166GLint ProgramBinary::getActiveUniformCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002167{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002168 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002169}
2170
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002171GLint ProgramBinary::getActiveUniformMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002172{
2173 int maxLength = 0;
2174
2175 unsigned int numUniforms = mUniforms.size();
2176 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2177 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002178 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002179 {
2180 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2181 if (mUniforms[uniformIndex]->isArray())
2182 {
2183 length += 3; // Counting in "[0]".
2184 }
2185 maxLength = std::max(length, maxLength);
2186 }
2187 }
2188
2189 return maxLength;
2190}
2191
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002192void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002193{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002194 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002195 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002196 {
2197 mValidated = false;
2198 }
2199 else
2200 {
2201 mValidated = true;
2202 }
2203}
2204
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002205bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002206{
2207 // if any two active samplers in a program are of different types, but refer to the same
2208 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2209 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2210
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002211 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002212 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002213
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002214 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002215 {
2216 textureUnitType[i] = TEXTURE_UNKNOWN;
2217 }
2218
2219 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2220 {
2221 if (mSamplersPS[i].active)
2222 {
2223 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2224
2225 if (unit >= maxCombinedTextureImageUnits)
2226 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002227 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002228 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002229 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002230 }
2231
2232 return false;
2233 }
2234
2235 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2236 {
2237 if (mSamplersPS[i].textureType != textureUnitType[unit])
2238 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002239 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002240 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002241 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002242 }
2243
2244 return false;
2245 }
2246 }
2247 else
2248 {
2249 textureUnitType[unit] = mSamplersPS[i].textureType;
2250 }
2251 }
2252 }
2253
2254 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2255 {
2256 if (mSamplersVS[i].active)
2257 {
2258 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2259
2260 if (unit >= maxCombinedTextureImageUnits)
2261 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002262 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002263 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002264 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002265 }
2266
2267 return false;
2268 }
2269
2270 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2271 {
2272 if (mSamplersVS[i].textureType != textureUnitType[unit])
2273 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002274 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002275 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002276 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002277 }
2278
2279 return false;
2280 }
2281 }
2282 else
2283 {
2284 textureUnitType[unit] = mSamplersVS[i].textureType;
2285 }
2286 }
2287 }
2288
2289 return true;
2290}
2291
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002292ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2293{
2294}
2295
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002296struct AttributeSorter
2297{
2298 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2299 : originalIndices(semanticIndices)
2300 {
2301 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2302 {
2303 indices[i] = i;
2304 }
2305
2306 std::sort(&indices[0], &indices[MAX_VERTEX_ATTRIBS], *this);
2307 }
2308
2309 bool operator()(int a, int b)
2310 {
2311 return originalIndices[a] == -1 ? false : originalIndices[a] < originalIndices[b];
2312 }
2313
2314 int indices[MAX_VERTEX_ATTRIBS];
2315 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2316};
2317
2318void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2319{
2320 AttributeSorter sorter(mSemanticIndex);
2321
2322 int oldIndices[MAX_VERTEX_ATTRIBS];
2323 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2324
2325 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2326 {
2327 oldIndices[i] = mSemanticIndex[i];
2328 oldTranslatedAttributes[i] = attributes[i];
2329 }
2330
2331 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2332 {
2333 int oldIndex = sorter.indices[i];
2334 sortedSemanticIndices[i] = oldIndices[oldIndex];
2335 attributes[i] = oldTranslatedAttributes[oldIndex];
2336 }
2337}
2338
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002339}