blob: a70bac8236cff7264ee46668184155deee6b726e [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
shannonwoods@chromium.orgc2ed9912013-05-30 00:05:33 +0000250GLuint ProgramBinary::getUniformIndex(std::string name)
251{
252 unsigned int subscript = GL_INVALID_INDEX;
253
254 // Strip any trailing array operator and retrieve the subscript
255 size_t open = name.find_last_of('[');
256 size_t close = name.find_last_of(']');
257 if (open != std::string::npos && close == name.length() - 1)
258 {
259 subscript = atoi(name.substr(open + 1).c_str());
260 name.erase(open);
261 }
262
263 // The app is not allowed to specify array indices other than 0 for arrays of basic types
264 if (subscript != 0 && subscript != GL_INVALID_INDEX)
265 {
266 return GL_INVALID_INDEX;
267 }
268
269 unsigned int numUniforms = mUniforms.size();
270 for (unsigned int index = 0; index < numUniforms; index++)
271 {
272 if (mUniforms[index]->name == name)
273 {
274 if (mUniforms[index]->isArray() || subscript == GL_INVALID_INDEX)
275 {
276 return index;
277 }
278 }
279 }
280
281 return GL_INVALID_INDEX;
282}
283
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000284template <typename T>
285bool ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000286{
287 if (location < 0 || location >= (int)mUniformIndex.size())
288 {
289 return false;
290 }
291
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000292 const int components = UniformComponentCount(targetUniformType);
293 const GLenum targetBoolType = UniformBoolVectorType(targetUniformType);
294
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000295 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
296 targetUniform->dirty = true;
297
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000298 int elementCount = targetUniform->elementCount();
299
300 if (elementCount == 1 && count > 1)
301 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
302
303 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
304
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000305 if (targetUniform->type == targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000306 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000307 T *target = (T*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000308
309 for (int i = 0; i < count; i++)
310 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000311 for (int c = 0; c < components; c++)
312 {
313 target[c] = v[c];
314 }
315 for (int c = components; c < 4; c++)
316 {
317 target[c] = 0;
318 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000319 target += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000320 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000321 }
322 }
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000323 else if (targetUniform->type == targetBoolType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000324 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000325 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000326
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000327 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000328 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000329 for (int c = 0; c < components; c++)
330 {
331 boolParams[c] = (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
332 }
333 for (int c = components; c < 4; c++)
334 {
335 boolParams[c] = GL_FALSE;
336 }
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000337 boolParams += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000338 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000339 }
340 }
341 else
342 {
343 return false;
344 }
345
346 return true;
347}
348
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000349bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
350{
351 return setUniform(location, count, v, GL_FLOAT);
352}
353
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000354bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
355{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000356 return setUniform(location, count, v, GL_FLOAT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000357}
358
359bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
360{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000361 return setUniform(location, count, v, GL_FLOAT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000362}
363
364bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
365{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000366 return setUniform(location, count, v, GL_FLOAT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000367}
368
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000369template<typename T>
370void transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000371{
372 int copyWidth = std::min(targetWidth, srcWidth);
373 int copyHeight = std::min(targetHeight, srcHeight);
374
375 for (int x = 0; x < copyWidth; x++)
376 {
377 for (int y = 0; y < copyHeight; y++)
378 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000379 target[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000380 }
381 }
382 // clear unfilled right side
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000383 for (int y = 0; y < copyWidth; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000384 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000385 for (int x = copyHeight; x < targetWidth; x++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000386 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000387 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000388 }
389 }
390 // clear unfilled bottom.
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000391 for (int y = copyWidth; y < targetHeight; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000392 {
393 for (int x = 0; x < targetWidth; x++)
394 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000395 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000396 }
397 }
398}
399
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000400template<typename T>
401void expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
402{
403 int copyWidth = std::min(targetWidth, srcWidth);
404 int copyHeight = std::min(targetHeight, srcHeight);
405
406 for (int y = 0; y < copyHeight; y++)
407 {
408 for (int x = 0; x < copyWidth; x++)
409 {
410 target[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
411 }
412 }
413 // clear unfilled right side
414 for (int y = 0; y < copyHeight; y++)
415 {
416 for (int x = copyWidth; x < targetWidth; x++)
417 {
418 target[y * targetWidth + x] = static_cast<T>(0);
419 }
420 }
421 // clear unfilled bottom.
422 for (int y = copyHeight; y < targetHeight; y++)
423 {
424 for (int x = 0; x < targetWidth; x++)
425 {
426 target[y * targetWidth + x] = static_cast<T>(0);
427 }
428 }
429}
430
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000431template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000432bool ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000433{
434 if (location < 0 || location >= (int)mUniformIndex.size())
435 {
436 return false;
437 }
438
439 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
440 targetUniform->dirty = true;
441
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000442 if (targetUniform->type != targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000443 {
444 return false;
445 }
446
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000447 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000448
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000449 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000450 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
451
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000452 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000453 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4 * rows);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000454
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000455 for (int i = 0; i < count; i++)
456 {
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000457 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
458 if (transpose == GL_FALSE)
459 {
460 transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols);
461 }
462 else
463 {
464 expandMatrix<GLfloat>(target, value, 4, rows, cols, rows);
465 }
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000466 target += 4 * rows;
467 value += cols * rows;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000468 }
469
470 return true;
471}
472
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000473bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000474{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000475 return setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000476}
477
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000478bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000479{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000480 return setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000481}
482
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000483bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000484{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000485 return setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000486}
487
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000488bool ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000489{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000490 return setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000491}
492
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000493bool ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000494{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000495 return setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000496}
497
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000498bool ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000499{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000500 return setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000501}
502
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000503bool ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000504{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000505 return setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000506}
507
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000508bool ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000509{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000510 return setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000511}
512
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000513bool ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000514{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000515 return setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000516}
517
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000518bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
519{
520 if (location < 0 || location >= (int)mUniformIndex.size())
521 {
522 return false;
523 }
524
525 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
526 targetUniform->dirty = true;
527
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000528 int elementCount = targetUniform->elementCount();
529
530 if (elementCount == 1 && count > 1)
531 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
532
533 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
534
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000535 if (targetUniform->type == GL_INT ||
536 targetUniform->type == GL_SAMPLER_2D ||
537 targetUniform->type == GL_SAMPLER_CUBE)
538 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000539 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000540
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000541 for (int i = 0; i < count; i++)
542 {
543 target[0] = v[0];
544 target[1] = 0;
545 target[2] = 0;
546 target[3] = 0;
547 target += 4;
548 v += 1;
549 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000550 }
551 else if (targetUniform->type == GL_BOOL)
552 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000553 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000554
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000555 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000556 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000557 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
558 boolParams[1] = GL_FALSE;
559 boolParams[2] = GL_FALSE;
560 boolParams[3] = GL_FALSE;
561 boolParams += 4;
562 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000563 }
564 }
565 else
566 {
567 return false;
568 }
569
570 return true;
571}
572
573bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
574{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000575 return setUniform(location, count, v, GL_INT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000576}
577
578bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
579{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000580 return setUniform(location, count, v, GL_INT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000581}
582
583bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
584{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000585 return setUniform(location, count, v, GL_INT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000586}
587
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +0000588bool ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
589{
590 return setUniform(location, count, v, GL_UNSIGNED_INT);
591}
592
593bool ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
594{
595 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC2);
596}
597
598bool ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
599{
600 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC3);
601}
602
603bool ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
604{
605 return setUniform(location, count, v, GL_UNSIGNED_INT_VEC4);
606}
607
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000608template <typename T>
609bool ProgramBinary::getUniformv(GLint location, GLsizei *bufSize, T *params, GLenum uniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000610{
611 if (location < 0 || location >= (int)mUniformIndex.size())
612 {
613 return false;
614 }
615
616 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
617
618 // sized queries -- ensure the provided buffer is large enough
619 if (bufSize)
620 {
621 int requiredBytes = UniformExternalSize(targetUniform->type);
622 if (*bufSize < requiredBytes)
623 {
624 return false;
625 }
626 }
627
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000628 if (IsMatrixType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000629 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000630 const int rows = VariableRowCount(targetUniform->type);
631 const int cols = VariableColumnCount(targetUniform->type);
632 transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, cols, rows, 4, rows);
633 }
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000634 else if (uniformType == UniformComponentType(targetUniform->type))
635 {
636 unsigned int size = UniformComponentCount(targetUniform->type);
637 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T),
638 size * sizeof(T));
639 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000640 else
641 {
642 unsigned int size = UniformComponentCount(targetUniform->type);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000643 switch (UniformComponentType(targetUniform->type))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000644 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000645 case GL_BOOL:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000646 {
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000647 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000648
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000649 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000650 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000651 params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000652 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000653 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000654 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000655
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000656 case GL_FLOAT:
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000657 {
658 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
659
660 for (unsigned int i = 0; i < size; i++)
661 {
662 params[i] = static_cast<T>(floatParams[i]);
663 }
664 }
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000665 break;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000666
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000667 case GL_INT:
668 {
669 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
670
671 for (unsigned int i = 0; i < size; i++)
672 {
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000673 params[i] = static_cast<T>(intParams[i]);
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000674 }
675 }
676 break;
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000677
678 case GL_UNSIGNED_INT:
679 {
680 GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4;
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000681
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000682 for (unsigned int i = 0; i < size; i++)
683 {
684 params[i] = static_cast<T>(uintParams[i]);
685 }
686 }
687 break;
688
shannon.woods%transgaming.com@gtempaccount.comf4895612013-04-13 03:40:56 +0000689 default: UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000690 }
691 }
692
693 return true;
694}
695
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000696bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
697{
698 return getUniformv(location, bufSize, params, GL_FLOAT);
699}
700
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000701bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
702{
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000703 return getUniformv(location, bufSize, params, GL_INT);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000704}
705
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000706bool ProgramBinary::getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params)
707{
708 return getUniformv(location, bufSize, params, GL_UNSIGNED_INT);
709}
710
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000711void ProgramBinary::dirtyAllUniforms()
712{
713 unsigned int numUniforms = mUniforms.size();
714 for (unsigned int index = 0; index < numUniforms; index++)
715 {
716 mUniforms[index]->dirty = true;
717 }
718}
719
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000720// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000721void ProgramBinary::applyUniforms()
722{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000723 // Retrieve sampler uniform values
724 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
725 {
726 Uniform *targetUniform = *ub;
727
728 if (targetUniform->dirty)
729 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000730 if (targetUniform->type == GL_SAMPLER_2D ||
731 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000732 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000733 int count = targetUniform->elementCount();
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000734 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000735
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000736 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000737 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000738 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000739
740 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000741 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000742 unsigned int samplerIndex = firstIndex + i;
743
744 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000745 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000746 ASSERT(mSamplersPS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000747 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000748 }
749 }
750 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000751
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000752 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000753 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000754 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000755
756 for (int i = 0; i < count; i++)
757 {
758 unsigned int samplerIndex = firstIndex + i;
759
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000760 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000761 {
762 ASSERT(mSamplersVS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000763 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000764 }
765 }
766 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000767 }
768 }
769 }
770
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +0000771 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000772}
773
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000774// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
775// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000776int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000777{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000778 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000779
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000780 fragmentShader->resetVaryingsRegisterAssignment();
781
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000782 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
783 {
784 int n = VariableRowCount(varying->type) * varying->size;
785 int m = VariableColumnCount(varying->type);
786 bool success = false;
787
788 if (m == 2 || m == 3 || m == 4)
789 {
790 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
791 {
792 bool available = true;
793
794 for (int y = 0; y < n && available; y++)
795 {
796 for (int x = 0; x < m && 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 = 0;
809
810 for (int y = 0; y < n; y++)
811 {
812 for (int x = 0; x < m; x++)
813 {
814 packing[r + y][x] = &*varying;
815 }
816 }
817
818 success = true;
819 }
820 }
821
822 if (!success && m == 2)
823 {
824 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
825 {
826 bool available = true;
827
828 for (int y = 0; y < n && available; y++)
829 {
830 for (int x = 2; x < 4 && available; x++)
831 {
832 if (packing[r + y][x])
833 {
834 available = false;
835 }
836 }
837 }
838
839 if (available)
840 {
841 varying->reg = r;
842 varying->col = 2;
843
844 for (int y = 0; y < n; y++)
845 {
846 for (int x = 2; x < 4; x++)
847 {
848 packing[r + y][x] = &*varying;
849 }
850 }
851
852 success = true;
853 }
854 }
855 }
856 }
857 else if (m == 1)
858 {
859 int space[4] = {0};
860
861 for (int y = 0; y < maxVaryingVectors; y++)
862 {
863 for (int x = 0; x < 4; x++)
864 {
865 space[x] += packing[y][x] ? 0 : 1;
866 }
867 }
868
869 int column = 0;
870
871 for (int x = 0; x < 4; x++)
872 {
873 if (space[x] >= n && space[x] < space[column])
874 {
875 column = x;
876 }
877 }
878
879 if (space[column] >= n)
880 {
881 for (int r = 0; r < maxVaryingVectors; r++)
882 {
883 if (!packing[r][column])
884 {
885 varying->reg = r;
886
887 for (int y = r; y < r + n; y++)
888 {
889 packing[y][column] = &*varying;
890 }
891
892 break;
893 }
894 }
895
896 varying->col = column;
897
898 success = true;
899 }
900 }
901 else UNREACHABLE();
902
903 if (!success)
904 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000905 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000906
907 return -1;
908 }
909 }
910
911 // Return the number of used registers
912 int registers = 0;
913
914 for (int r = 0; r < maxVaryingVectors; r++)
915 {
916 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
917 {
918 registers++;
919 }
920 }
921
922 return registers;
923}
924
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000925bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
926 std::string& pixelHLSL, std::string& vertexHLSL,
927 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000928{
929 if (pixelHLSL.empty() || vertexHLSL.empty())
930 {
931 return false;
932 }
933
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000934 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
935 bool usesFragColor = fragmentShader->mUsesFragColor;
936 bool usesFragData = fragmentShader->mUsesFragData;
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +0000937 if (usesFragColor && usesFragData)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000938 {
939 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
940 return false;
941 }
942
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000943 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000944 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000945 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000946
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000947 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
948
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +0000949 // Two cases when writing to gl_FragColor and using ESSL 1.0:
950 // - with a 3.0 context, the output color is copied to channel 0
951 // - with a 2.0 context, the output color is broadcast to all channels
952 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
953 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
954
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000955 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000956 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000957 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000958
959 return false;
960 }
961
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000962 vertexShader->resetVaryingsRegisterAssignment();
963
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000964 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
965 {
966 bool matched = false;
967
968 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
969 {
970 if (output->name == input->name)
971 {
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000972 if (output->type != input->type || output->size != input->size || output->interpolation != input->interpolation)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000973 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000974 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 +0000975
976 return false;
977 }
978
979 output->reg = input->reg;
980 output->col = input->col;
981
982 matched = true;
983 break;
984 }
985 }
986
987 if (!matched)
988 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000989 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000990
991 return false;
992 }
993 }
994
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000995 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000996 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000997 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000998 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
999
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001000 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
1001
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001002 // special varyings that use reserved registers
1003 int reservedRegisterIndex = registers;
1004 std::string fragCoordSemantic;
1005 std::string pointCoordSemantic;
1006
1007 if (fragmentShader->mUsesFragCoord)
1008 {
1009 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1010 }
1011
1012 if (fragmentShader->mUsesPointCoord)
1013 {
1014 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1015 // In DX11 we compute this in the GS.
1016 if (shaderModel == 3)
1017 {
1018 pointCoordSemantic = "TEXCOORD0";
1019 }
1020 else if (shaderModel >= 4)
1021 {
1022 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1023 }
1024 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001025
1026 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001027 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001028
1029 int semanticIndex = 0;
1030 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1031 {
1032 switch (attribute->type)
1033 {
1034 case GL_FLOAT: vertexHLSL += " float "; break;
1035 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1036 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1037 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1038 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1039 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1040 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1041 default: UNREACHABLE();
1042 }
1043
1044 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1045
1046 semanticIndex += VariableRowCount(attribute->type);
1047 }
1048
1049 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001050 "\n"
1051 "struct VS_OUTPUT\n"
1052 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001053
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001054 if (shaderModel < 4)
1055 {
1056 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1057 }
1058
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001059 vertexHLSL += varyingHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001060
1061 if (fragmentShader->mUsesFragCoord)
1062 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001063 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001064 }
1065
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001066 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001067 {
1068 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1069 }
1070
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001071 if (shaderModel >= 4)
1072 {
1073 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1074 }
1075
1076 vertexHLSL += "};\n"
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001077 "\n"
1078 "VS_OUTPUT main(VS_INPUT input)\n"
1079 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001080
1081 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1082 {
1083 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1084
1085 if (VariableRowCount(attribute->type) > 1) // Matrix
1086 {
1087 vertexHLSL += "transpose";
1088 }
1089
1090 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1091 }
1092
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001093 if (shaderModel >= 4)
1094 {
1095 vertexHLSL += "\n"
1096 " gl_main();\n"
1097 "\n"
1098 " VS_OUTPUT output;\n"
1099 " output.gl_Position.x = gl_Position.x;\n"
1100 " output.gl_Position.y = -gl_Position.y;\n"
1101 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1102 " output.gl_Position.w = gl_Position.w;\n";
1103 }
1104 else
1105 {
1106 vertexHLSL += "\n"
1107 " gl_main();\n"
1108 "\n"
1109 " VS_OUTPUT output;\n"
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001110 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1111 " 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 +00001112 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1113 " output.gl_Position.w = gl_Position.w;\n";
1114 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001115
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001116 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001117 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001118 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001119 }
1120
1121 if (fragmentShader->mUsesFragCoord)
1122 {
1123 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1124 }
1125
1126 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1127 {
1128 if (varying->reg >= 0)
1129 {
1130 for (int i = 0; i < varying->size; i++)
1131 {
1132 int rows = VariableRowCount(varying->type);
1133
1134 for (int j = 0; j < rows; j++)
1135 {
1136 int r = varying->reg + i * rows + j;
1137 vertexHLSL += " output.v" + str(r);
1138
1139 bool sharedRegister = false; // Register used by multiple varyings
1140
1141 for (int x = 0; x < 4; x++)
1142 {
1143 if (packing[r][x] && packing[r][x] != packing[r][0])
1144 {
1145 sharedRegister = true;
1146 break;
1147 }
1148 }
1149
1150 if(sharedRegister)
1151 {
1152 vertexHLSL += ".";
1153
1154 for (int x = 0; x < 4; x++)
1155 {
1156 if (packing[r][x] == &*varying)
1157 {
1158 switch(x)
1159 {
1160 case 0: vertexHLSL += "x"; break;
1161 case 1: vertexHLSL += "y"; break;
1162 case 2: vertexHLSL += "z"; break;
1163 case 3: vertexHLSL += "w"; break;
1164 }
1165 }
1166 }
1167 }
1168
1169 vertexHLSL += " = " + varying->name;
1170
1171 if (varying->array)
1172 {
1173 vertexHLSL += "[" + str(i) + "]";
1174 }
1175
1176 if (rows > 1)
1177 {
1178 vertexHLSL += "[" + str(j) + "]";
1179 }
1180
1181 vertexHLSL += ";\n";
1182 }
1183 }
1184 }
1185 }
1186
1187 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001188 " return output;\n"
1189 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001190
1191 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001192 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001193
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001194 pixelHLSL += varyingHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001195
1196 if (fragmentShader->mUsesFragCoord)
1197 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001198 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001199 }
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001200
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001201 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1202 {
1203 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1204 }
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001205
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001206 // Must consume the PSIZE element if the geometry shader is not active
1207 // We won't know if we use a GS until we draw
1208 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1209 {
1210 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1211 }
1212
1213 if (fragmentShader->mUsesFragCoord)
1214 {
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001215 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001216 {
1217 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1218 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001219 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001220 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001221 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1222 }
1223 }
1224
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001225 pixelHLSL += "};\n"
1226 "\n"
1227 "struct PS_OUTPUT\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001228 "{\n";
1229
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001230 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001231 {
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001232 pixelHLSL += " float4 gl_Color" + str(renderTargetIndex) + " : " + targetSemantic + str(renderTargetIndex) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001233 }
1234
1235 pixelHLSL += "};\n"
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001236 "\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001237
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001238 if (fragmentShader->mUsesFrontFacing)
1239 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001240 if (shaderModel >= 4)
1241 {
1242 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1243 "{\n";
1244 }
1245 else
1246 {
1247 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1248 "{\n";
1249 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001250 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001251 else
1252 {
1253 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1254 "{\n";
1255 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001256
1257 if (fragmentShader->mUsesFragCoord)
1258 {
1259 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1260
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001261 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001262 {
1263 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1264 " gl_FragCoord.y = input.dx_VPos.y;\n";
1265 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001266 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001267 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001268 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001269 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001270 }
1271 else
1272 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001273 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1274 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1275 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001276 }
1277
daniel@transgaming.com12985182012-12-20 20:56:31 +00001278 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001279 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001280 }
1281
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001282 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001283 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001284 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1285 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001286 }
1287
1288 if (fragmentShader->mUsesFrontFacing)
1289 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001290 if (shaderModel <= 3)
1291 {
1292 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1293 }
1294 else
1295 {
1296 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1297 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001298 }
1299
1300 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1301 {
1302 if (varying->reg >= 0)
1303 {
1304 for (int i = 0; i < varying->size; i++)
1305 {
1306 int rows = VariableRowCount(varying->type);
1307 for (int j = 0; j < rows; j++)
1308 {
1309 std::string n = str(varying->reg + i * rows + j);
1310 pixelHLSL += " " + varying->name;
1311
1312 if (varying->array)
1313 {
1314 pixelHLSL += "[" + str(i) + "]";
1315 }
1316
1317 if (rows > 1)
1318 {
1319 pixelHLSL += "[" + str(j) + "]";
1320 }
1321
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001322 switch (VariableColumnCount(varying->type))
1323 {
1324 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1325 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1326 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1327 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1328 default: UNREACHABLE();
1329 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001330 }
1331 }
1332 }
1333 else UNREACHABLE();
1334 }
1335
1336 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001337 " gl_main();\n"
1338 "\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001339 " PS_OUTPUT output;\n";
1340
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001341 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001342 {
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001343 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001344
shannon.woods%transgaming.com@gtempaccount.come3fe5dad2013-04-13 03:42:07 +00001345 pixelHLSL += " output.gl_Color" + str(renderTargetIndex) + " = gl_Color[" + str(sourceColorIndex) + "];\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001346 }
1347
1348 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001349 " return output;\n"
1350 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001351
1352 return true;
1353}
1354
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001355std::string ProgramBinary::generateVaryingHLSL(FragmentShader *fragmentShader, const std::string &varyingSemantic) const
1356{
1357 std::string varyingHLSL;
1358
1359 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1360 {
1361 if (varying->reg >= 0)
1362 {
1363 for (int i = 0; i < varying->size; i++)
1364 {
1365 int rows = VariableRowCount(varying->type);
1366 for (int j = 0; j < rows; j++)
1367 {
1368 switch (varying->interpolation)
1369 {
1370 case Smooth: varyingHLSL += " "; break;
1371 case Flat: varyingHLSL += " nointerpolation "; break;
1372 case Centroid: varyingHLSL += " centroid "; break;
1373 default: UNREACHABLE();
1374 }
1375
1376 std::string n = str(varying->reg + i * rows + j);
1377 varyingHLSL += "float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
1378 }
1379 }
1380 }
1381 else UNREACHABLE();
1382 }
1383
1384 return varyingHLSL;
1385}
1386
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001387bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1388{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001389 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001390
1391 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001392 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001393 if (format != GL_PROGRAM_BINARY_ANGLE)
1394 {
1395 infoLog.append("Invalid program binary format.");
1396 return false;
1397 }
1398
1399 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001400 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001401 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001402 {
1403 infoLog.append("Invalid program binary version.");
1404 return false;
1405 }
1406
1407 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1408 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001409 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001410 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001411 stream.read(&name);
1412 mLinkedAttribute[i].name = name;
1413 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001414 }
1415
1416 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1417 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001418 stream.read(&mSamplersPS[i].active);
1419 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001420
1421 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001422 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001423 mSamplersPS[i].textureType = (TextureType) textureType;
1424 }
1425
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001426 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001427 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001428 stream.read(&mSamplersVS[i].active);
1429 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001430
1431 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001432 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001433 mSamplersVS[i].textureType = (TextureType) textureType;
1434 }
1435
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001436 stream.read(&mUsedVertexSamplerRange);
1437 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001438 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001439
shannon.woods@transgaming.com45886d62013-02-28 23:19:20 +00001440 size_t size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001441 stream.read(&size);
1442 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001443 {
1444 infoLog.append("Invalid program binary.");
1445 return false;
1446 }
1447
1448 mUniforms.resize(size);
1449 for (unsigned int i = 0; i < size; ++i)
1450 {
1451 GLenum type;
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001452 GLenum precision;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001453 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001454 unsigned int arraySize;
1455
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001456 stream.read(&type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001457 stream.read(&precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001458 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001459 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001460
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001461 mUniforms[i] = new Uniform(type, precision, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001462
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001463 stream.read(&mUniforms[i]->psRegisterIndex);
1464 stream.read(&mUniforms[i]->vsRegisterIndex);
1465 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001466 }
1467
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001468 stream.read(&size);
1469 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001470 {
1471 infoLog.append("Invalid program binary.");
1472 return false;
1473 }
1474
1475 mUniformIndex.resize(size);
1476 for (unsigned int i = 0; i < size; ++i)
1477 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001478 stream.read(&mUniformIndex[i].name);
1479 stream.read(&mUniformIndex[i].element);
1480 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001481 }
1482
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001483 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001484 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001485
1486 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001487 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001488
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001489 unsigned int geometryShaderSize;
1490 stream.read(&geometryShaderSize);
1491
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001492 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001493
daniel@transgaming.com36038542012-11-28 20:59:26 +00001494 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001495 ptr += sizeof(GUID);
1496
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001497 GUID identifier = mRenderer->getAdapterIdentifier();
1498 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001499 {
1500 infoLog.append("Invalid program binary.");
1501 return false;
1502 }
1503
1504 const char *pixelShaderFunction = ptr;
1505 ptr += pixelShaderSize;
1506
1507 const char *vertexShaderFunction = ptr;
1508 ptr += vertexShaderSize;
1509
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001510 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1511 ptr += geometryShaderSize;
1512
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001513 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001514 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001515 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001516 {
1517 infoLog.append("Could not create pixel shader.");
1518 return false;
1519 }
1520
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001521 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001522 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001523 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001524 {
1525 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001526 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001527 mPixelExecutable = NULL;
1528 return false;
1529 }
1530
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001531 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1532 {
1533 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1534 geometryShaderSize, rx::SHADER_GEOMETRY);
1535 if (!mGeometryExecutable)
1536 {
1537 infoLog.append("Could not create geometry shader.");
1538 delete mPixelExecutable;
1539 mPixelExecutable = NULL;
1540 delete mVertexExecutable;
1541 mVertexExecutable = NULL;
1542 return false;
1543 }
1544 }
1545 else
1546 {
1547 mGeometryExecutable = NULL;
1548 }
1549
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001550 return true;
1551}
1552
1553bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1554{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001555 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001556
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001557 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001558 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001559
1560 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1561 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001562 stream.write(mLinkedAttribute[i].type);
1563 stream.write(mLinkedAttribute[i].name);
1564 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001565 }
1566
1567 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1568 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001569 stream.write(mSamplersPS[i].active);
1570 stream.write(mSamplersPS[i].logicalTextureUnit);
1571 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001572 }
1573
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001574 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001575 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001576 stream.write(mSamplersVS[i].active);
1577 stream.write(mSamplersVS[i].logicalTextureUnit);
1578 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001579 }
1580
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001581 stream.write(mUsedVertexSamplerRange);
1582 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001583 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001584
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001585 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1587 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001588 stream.write(mUniforms[i]->type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001589 stream.write(mUniforms[i]->precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001590 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001591 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001592
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001593 stream.write(mUniforms[i]->psRegisterIndex);
1594 stream.write(mUniforms[i]->vsRegisterIndex);
1595 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001596 }
1597
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001598 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001599 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1600 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001601 stream.write(mUniformIndex[i].name);
1602 stream.write(mUniformIndex[i].element);
1603 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001604 }
1605
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001606 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001607 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001608
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001609 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001610 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001611
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001612 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1613 stream.write(geometryShaderSize);
1614
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001615 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001616
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001617 GLsizei streamLength = stream.length();
1618 const void *streamData = stream.data();
1619
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001620 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001621 if (totalLength > bufSize)
1622 {
1623 if (length)
1624 {
1625 *length = 0;
1626 }
1627
1628 return false;
1629 }
1630
1631 if (binary)
1632 {
1633 char *ptr = (char*) binary;
1634
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001635 memcpy(ptr, streamData, streamLength);
1636 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001637
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001638 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001639 ptr += sizeof(GUID);
1640
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001641 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001642 ptr += pixelShaderSize;
1643
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001644 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001645 ptr += vertexShaderSize;
1646
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001647 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1648 {
1649 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1650 ptr += geometryShaderSize;
1651 }
1652
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001653 ASSERT(ptr - totalLength == binary);
1654 }
1655
1656 if (length)
1657 {
1658 *length = totalLength;
1659 }
1660
1661 return true;
1662}
1663
1664GLint ProgramBinary::getLength()
1665{
1666 GLint length;
1667 if (save(NULL, INT_MAX, &length))
1668 {
1669 return length;
1670 }
1671 else
1672 {
1673 return 0;
1674 }
1675}
1676
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001677bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001678{
1679 if (!fragmentShader || !fragmentShader->isCompiled())
1680 {
1681 return false;
1682 }
1683
1684 if (!vertexShader || !vertexShader->isCompiled())
1685 {
1686 return false;
1687 }
1688
1689 std::string pixelHLSL = fragmentShader->getHLSL();
1690 std::string vertexHLSL = vertexShader->getHLSL();
1691
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001692 // Map the varyings to the register file
1693 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1694 int registers = packVaryings(infoLog, packing, fragmentShader);
1695
1696 if (registers < 0)
1697 {
1698 return false;
1699 }
1700
1701 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001702 {
1703 return false;
1704 }
1705
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001706 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001707 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1708 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001709
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001710 if (usesGeometryShader())
1711 {
1712 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1713 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY);
1714 }
1715
1716 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001717 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001718 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001719 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001720
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001721 delete mVertexExecutable;
1722 mVertexExecutable = NULL;
1723 delete mPixelExecutable;
1724 mPixelExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001725 delete mGeometryExecutable;
1726 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001727 }
1728
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001729 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1730 {
1731 success = false;
1732 }
1733
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001734 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001735 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001736 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001737 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001738
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001739 // special case for gl_DepthRange, the only built-in uniform (also a struct)
1740 if (vertexShader->mUsesDepthRange || fragmentShader->mUsesDepthRange)
1741 {
1742 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0));
1743 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0));
1744 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0));
1745 }
1746
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001747 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001748}
1749
1750// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001751bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001752{
1753 unsigned int usedLocations = 0;
1754
1755 // Link attributes that have a binding location
1756 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1757 {
1758 int location = attributeBindings.getAttributeBinding(attribute->name);
1759
1760 if (location != -1) // Set by glBindAttribLocation
1761 {
1762 if (!mLinkedAttribute[location].name.empty())
1763 {
1764 // Multiple active attributes bound to the same location; not an error
1765 }
1766
1767 mLinkedAttribute[location] = *attribute;
1768
1769 int rows = VariableRowCount(attribute->type);
1770
1771 if (rows + location > MAX_VERTEX_ATTRIBS)
1772 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001773 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 +00001774
1775 return false;
1776 }
1777
1778 for (int i = 0; i < rows; i++)
1779 {
1780 usedLocations |= 1 << (location + i);
1781 }
1782 }
1783 }
1784
1785 // Link attributes that don't have a binding location
1786 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1787 {
1788 int location = attributeBindings.getAttributeBinding(attribute->name);
1789
1790 if (location == -1) // Not set by glBindAttribLocation
1791 {
1792 int rows = VariableRowCount(attribute->type);
1793 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1794
1795 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1796 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001797 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001798
1799 return false; // Fail to link
1800 }
1801
1802 mLinkedAttribute[availableIndex] = *attribute;
1803 }
1804 }
1805
1806 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1807 {
1808 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1809 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1810
1811 for (int r = 0; r < rows; r++)
1812 {
1813 mSemanticIndex[attributeIndex++] = index++;
1814 }
1815 }
1816
1817 return true;
1818}
1819
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001820bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001821{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001822 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001823 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001824 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001825 {
1826 return false;
1827 }
1828 }
1829
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001830 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001831 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001832 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001833 {
1834 return false;
1835 }
1836 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001837
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001838 return true;
1839}
1840
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001841bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001842{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001843 if (constant.type == GL_SAMPLER_2D ||
1844 constant.type == GL_SAMPLER_CUBE)
1845 {
1846 unsigned int samplerIndex = constant.registerIndex;
1847
1848 do
1849 {
1850 if (shader == GL_VERTEX_SHADER)
1851 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001852 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001853 {
1854 mSamplersVS[samplerIndex].active = true;
1855 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1856 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1857 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1858 }
1859 else
1860 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001861 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001862 return false;
1863 }
1864 }
1865 else if (shader == GL_FRAGMENT_SHADER)
1866 {
1867 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1868 {
1869 mSamplersPS[samplerIndex].active = true;
1870 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1871 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1872 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1873 }
1874 else
1875 {
1876 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1877 return false;
1878 }
1879 }
1880 else UNREACHABLE();
1881
1882 samplerIndex++;
1883 }
1884 while (samplerIndex < constant.registerIndex + constant.arraySize);
1885 }
1886
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001887 Uniform *uniform = NULL;
1888 GLint location = getUniformLocation(constant.name);
1889
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001890 if (location >= 0) // Previously defined, type and precision must match
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001891 {
1892 uniform = mUniforms[mUniformIndex[location].index];
1893
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00001894 if (uniform->type != constant.type)
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001895 {
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00001896 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
1897 return false;
1898 }
1899
1900 if (uniform->precision != constant.precision)
1901 {
1902 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 +00001903 return false;
1904 }
1905 }
1906 else
1907 {
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001908 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize);
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001909 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001910
1911 if (!uniform)
1912 {
1913 return false;
1914 }
1915
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001916 if (shader == GL_FRAGMENT_SHADER)
1917 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001918 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001919 }
1920 else if (shader == GL_VERTEX_SHADER)
1921 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001922 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001923 }
1924 else UNREACHABLE();
1925
1926 if (location >= 0)
1927 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001928 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001929 }
1930
1931 mUniforms.push_back(uniform);
1932 unsigned int uniformIndex = mUniforms.size() - 1;
1933
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001934 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001935 {
1936 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
1937 }
1938
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001939 if (shader == GL_VERTEX_SHADER)
1940 {
1941 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
1942 {
1943 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
1944 return false;
1945 }
1946 }
1947 else if (shader == GL_FRAGMENT_SHADER)
1948 {
1949 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
1950 {
1951 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
1952 return false;
1953 }
1954 }
1955 else UNREACHABLE();
1956
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001957 return true;
1958}
1959
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001960std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
1961{
1962 // for now we only handle point sprite emulation
1963 ASSERT(usesPointSpriteEmulation());
1964 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
1965}
1966
1967std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
1968{
1969 ASSERT(registers >= 0);
1970 ASSERT(vertexShader->mUsesPointSize);
1971 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1972
1973 std::string geomHLSL;
1974
1975 std::string varyingSemantic = "TEXCOORD";
1976
1977 std::string fragCoordSemantic;
1978 std::string pointCoordSemantic;
1979
1980 int reservedRegisterIndex = registers;
1981
1982 if (fragmentShader->mUsesFragCoord)
1983 {
1984 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1985 }
1986
1987 if (fragmentShader->mUsesPointCoord)
1988 {
1989 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1990 }
1991
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001992 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1993 "\n"
1994 "struct GS_INPUT\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001995 "{\n";
1996
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001997 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001998
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00001999 geomHLSL += varyingHLSL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002000
2001 if (fragmentShader->mUsesFragCoord)
2002 {
2003 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2004 }
2005
2006 geomHLSL += " float gl_PointSize : PSIZE;\n"
2007 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002008 "};\n"
2009 "\n"
2010 "struct GS_OUTPUT\n"
2011 "{\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002012
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00002013 geomHLSL += varyingHLSL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002014
2015 if (fragmentShader->mUsesFragCoord)
2016 {
2017 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2018 }
2019
2020 if (fragmentShader->mUsesPointCoord)
2021 {
2022 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
2023 }
2024
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002025 geomHLSL += " float gl_PointSize : PSIZE;\n"
2026 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002027 "};\n"
2028 "\n"
2029 "static float2 pointSpriteCorners[] = \n"
2030 "{\n"
2031 " float2( 0.5f, -0.5f),\n"
2032 " float2( 0.5f, 0.5f),\n"
2033 " float2(-0.5f, -0.5f),\n"
2034 " float2(-0.5f, 0.5f)\n"
2035 "};\n"
2036 "\n"
2037 "static float2 pointSpriteTexcoords[] = \n"
2038 "{\n"
2039 " float2(1.0f, 1.0f),\n"
2040 " float2(1.0f, 0.0f),\n"
2041 " float2(0.0f, 1.0f),\n"
2042 " float2(0.0f, 0.0f)\n"
2043 "};\n"
2044 "\n"
2045 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2046 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2047 "\n"
2048 "[maxvertexcount(4)]\n"
2049 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2050 "{\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002051 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2052 " output.gl_PointSize = input[0].gl_PointSize;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002053
2054 for (int r = 0; r < registers; r++)
2055 {
2056 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2057 }
2058
2059 if (fragmentShader->mUsesFragCoord)
2060 {
2061 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2062 }
2063
2064 geomHLSL += " \n"
2065 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2066 " float4 gl_Position = input[0].gl_Position;\n"
shannon.woods@transgaming.com771ca2a2013-02-28 23:14:52 +00002067 " 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 +00002068
2069 for (int corner = 0; corner < 4; corner++)
2070 {
2071 geomHLSL += " \n"
2072 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2073
2074 if (fragmentShader->mUsesPointCoord)
2075 {
2076 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2077 }
2078
2079 geomHLSL += " outStream.Append(output);\n";
2080 }
2081
2082 geomHLSL += " \n"
2083 " outStream.RestartStrip();\n"
2084 "}\n";
2085
2086 return geomHLSL;
2087}
2088
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002089// This method needs to match OutputHLSL::decorate
2090std::string ProgramBinary::decorateAttribute(const std::string &name)
2091{
2092 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2093 {
2094 return "_" + name;
2095 }
2096
2097 return name;
2098}
2099
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002100bool ProgramBinary::isValidated() const
2101{
2102 return mValidated;
2103}
2104
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002105void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002106{
2107 // Skip over inactive attributes
2108 unsigned int activeAttribute = 0;
2109 unsigned int attribute;
2110 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2111 {
2112 if (mLinkedAttribute[attribute].name.empty())
2113 {
2114 continue;
2115 }
2116
2117 if (activeAttribute == index)
2118 {
2119 break;
2120 }
2121
2122 activeAttribute++;
2123 }
2124
2125 if (bufsize > 0)
2126 {
2127 const char *string = mLinkedAttribute[attribute].name.c_str();
2128
2129 strncpy(name, string, bufsize);
2130 name[bufsize - 1] = '\0';
2131
2132 if (length)
2133 {
2134 *length = strlen(name);
2135 }
2136 }
2137
2138 *size = 1; // Always a single 'type' instance
2139
2140 *type = mLinkedAttribute[attribute].type;
2141}
2142
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002143GLint ProgramBinary::getActiveAttributeCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002144{
2145 int count = 0;
2146
2147 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2148 {
2149 if (!mLinkedAttribute[attributeIndex].name.empty())
2150 {
2151 count++;
2152 }
2153 }
2154
2155 return count;
2156}
2157
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002158GLint ProgramBinary::getActiveAttributeMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002159{
2160 int maxLength = 0;
2161
2162 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2163 {
2164 if (!mLinkedAttribute[attributeIndex].name.empty())
2165 {
2166 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2167 }
2168 }
2169
2170 return maxLength;
2171}
2172
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002173void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002174{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002175 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002176
2177 if (bufsize > 0)
2178 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002179 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002180
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002181 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002182 {
2183 string += "[0]";
2184 }
2185
2186 strncpy(name, string.c_str(), bufsize);
2187 name[bufsize - 1] = '\0';
2188
2189 if (length)
2190 {
2191 *length = strlen(name);
2192 }
2193 }
2194
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002195 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002196
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002197 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002198}
2199
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002200GLint ProgramBinary::getActiveUniformCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002201{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002202 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002203}
2204
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002205GLint ProgramBinary::getActiveUniformMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002206{
2207 int maxLength = 0;
2208
2209 unsigned int numUniforms = mUniforms.size();
2210 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2211 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002212 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002213 {
2214 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2215 if (mUniforms[uniformIndex]->isArray())
2216 {
2217 length += 3; // Counting in "[0]".
2218 }
2219 maxLength = std::max(length, maxLength);
2220 }
2221 }
2222
2223 return maxLength;
2224}
2225
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002226void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002227{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002228 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002229 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002230 {
2231 mValidated = false;
2232 }
2233 else
2234 {
2235 mValidated = true;
2236 }
2237}
2238
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002239bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002240{
2241 // if any two active samplers in a program are of different types, but refer to the same
2242 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2243 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2244
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002245 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002246 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002247
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002248 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002249 {
2250 textureUnitType[i] = TEXTURE_UNKNOWN;
2251 }
2252
2253 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2254 {
2255 if (mSamplersPS[i].active)
2256 {
2257 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2258
2259 if (unit >= maxCombinedTextureImageUnits)
2260 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002261 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002262 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002263 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002264 }
2265
2266 return false;
2267 }
2268
2269 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2270 {
2271 if (mSamplersPS[i].textureType != textureUnitType[unit])
2272 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002273 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002274 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002275 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002276 }
2277
2278 return false;
2279 }
2280 }
2281 else
2282 {
2283 textureUnitType[unit] = mSamplersPS[i].textureType;
2284 }
2285 }
2286 }
2287
2288 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2289 {
2290 if (mSamplersVS[i].active)
2291 {
2292 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2293
2294 if (unit >= maxCombinedTextureImageUnits)
2295 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002296 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002297 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002298 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002299 }
2300
2301 return false;
2302 }
2303
2304 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2305 {
2306 if (mSamplersVS[i].textureType != textureUnitType[unit])
2307 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002308 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002309 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002310 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002311 }
2312
2313 return false;
2314 }
2315 }
2316 else
2317 {
2318 textureUnitType[unit] = mSamplersVS[i].textureType;
2319 }
2320 }
2321 }
2322
2323 return true;
2324}
2325
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002326ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2327{
2328}
2329
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002330struct AttributeSorter
2331{
2332 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2333 : originalIndices(semanticIndices)
2334 {
2335 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2336 {
2337 indices[i] = i;
2338 }
2339
2340 std::sort(&indices[0], &indices[MAX_VERTEX_ATTRIBS], *this);
2341 }
2342
2343 bool operator()(int a, int b)
2344 {
2345 return originalIndices[a] == -1 ? false : originalIndices[a] < originalIndices[b];
2346 }
2347
2348 int indices[MAX_VERTEX_ATTRIBS];
2349 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2350};
2351
2352void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2353{
2354 AttributeSorter sorter(mSemanticIndex);
2355
2356 int oldIndices[MAX_VERTEX_ATTRIBS];
2357 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2358
2359 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2360 {
2361 oldIndices[i] = mSemanticIndex[i];
2362 oldTranslatedAttributes[i] = attributes[i];
2363 }
2364
2365 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2366 {
2367 int oldIndex = sorter.indices[i];
2368 sortedSemanticIndices[i] = oldIndices[oldIndex];
2369 attributes[i] = oldTranslatedAttributes[oldIndex];
2370 }
2371}
2372
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002373}