blob: 53c75f1ac49b85f56a0ef3cc584466b245ddbd30 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00003// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Program.cpp: Implements the gl::Program class. Implements GL program objects
9// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
10
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000011#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000012#include "libGLESv2/ProgramBinary.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000013#include "libGLESv2/renderer/ShaderExecutable.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000014
15#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000016#include "common/version.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000017#include "utilities.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include "libGLESv2/main.h"
20#include "libGLESv2/Shader.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000021#include "libGLESv2/Program.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000022#include "libGLESv2/renderer/Renderer.h"
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000023#include "libGLESv2/renderer/VertexDataManager.h"
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +000024#include "libGLESv2/Context.h"
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000025
daniel@transgaming.com88853c52012-12-20 20:56:40 +000026#undef near
27#undef far
28
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000029namespace gl
30{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000031std::string str(int i)
32{
33 char buffer[20];
34 snprintf(buffer, sizeof(buffer), "%d", i);
35 return buffer;
36}
37
daniel@transgaming.comdb019952012-12-20 21:13:32 +000038UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index)
39 : name(name), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000040{
41}
42
daniel@transgaming.come87ca002012-07-24 18:30:43 +000043unsigned int ProgramBinary::mCurrentSerial = 1;
44
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000045ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000046{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000047 mPixelExecutable = NULL;
48 mVertexExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000049 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000050
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000051 mValidated = false;
52
53 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
54 {
55 mSemanticIndex[index] = -1;
56 }
57
58 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
59 {
60 mSamplersPS[index].active = false;
61 }
62
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +000063 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000064 {
65 mSamplersVS[index].active = false;
66 }
67
68 mUsedVertexSamplerRange = 0;
69 mUsedPixelSamplerRange = 0;
shannon.woods@transgaming.com962d4be2013-01-25 21:55:18 +000070 mUsesPointSize = false;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000071}
72
73ProgramBinary::~ProgramBinary()
74{
daniel@transgaming.com95892412012-11-28 20:59:09 +000075 delete mPixelExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000076 mPixelExecutable = NULL;
77
daniel@transgaming.com95892412012-11-28 20:59:09 +000078 delete mVertexExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000079 mVertexExecutable = NULL;
80
81 delete mGeometryExecutable;
82 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000083
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000084 while (!mUniforms.empty())
85 {
86 delete mUniforms.back();
87 mUniforms.pop_back();
88 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000089}
90
daniel@transgaming.come87ca002012-07-24 18:30:43 +000091unsigned int ProgramBinary::getSerial() const
92{
93 return mSerial;
94}
95
96unsigned int ProgramBinary::issueSerial()
97{
98 return mCurrentSerial++;
99}
100
daniel@transgaming.com95892412012-11-28 20:59:09 +0000101rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000102{
103 return mPixelExecutable;
104}
105
daniel@transgaming.com95892412012-11-28 20:59:09 +0000106rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000107{
108 return mVertexExecutable;
109}
110
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000111rx::ShaderExecutable *ProgramBinary::getGeometryExecutable()
112{
113 return mGeometryExecutable;
114}
115
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000116GLuint ProgramBinary::getAttributeLocation(const char *name)
117{
118 if (name)
119 {
120 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
121 {
122 if (mLinkedAttribute[index].name == std::string(name))
123 {
124 return index;
125 }
126 }
127 }
128
129 return -1;
130}
131
132int ProgramBinary::getSemanticIndex(int attributeIndex)
133{
134 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
135
136 return mSemanticIndex[attributeIndex];
137}
138
139// Returns one more than the highest sampler index used.
140GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
141{
142 switch (type)
143 {
144 case SAMPLER_PIXEL:
145 return mUsedPixelSamplerRange;
146 case SAMPLER_VERTEX:
147 return mUsedVertexSamplerRange;
148 default:
149 UNREACHABLE();
150 return 0;
151 }
152}
153
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000154bool ProgramBinary::usesPointSize() const
155{
156 return mUsesPointSize;
157}
158
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000159bool ProgramBinary::usesPointSpriteEmulation() const
160{
161 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4;
162}
163
164bool ProgramBinary::usesGeometryShader() const
165{
166 return usesPointSpriteEmulation();
167}
168
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000169// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
170// index (0-15 for the pixel shader and 0-3 for the vertex shader).
171GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
172{
173 GLint logicalTextureUnit = -1;
174
175 switch (type)
176 {
177 case SAMPLER_PIXEL:
178 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
179
180 if (mSamplersPS[samplerIndex].active)
181 {
182 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
183 }
184 break;
185 case SAMPLER_VERTEX:
186 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
187
188 if (mSamplersVS[samplerIndex].active)
189 {
190 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
191 }
192 break;
193 default: UNREACHABLE();
194 }
195
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000196 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000197 {
198 return logicalTextureUnit;
199 }
200
201 return -1;
202}
203
204// Returns the texture type for a given Direct3D 9 sampler type and
205// index (0-15 for the pixel shader and 0-3 for the vertex shader).
206TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
207{
208 switch (type)
209 {
210 case SAMPLER_PIXEL:
211 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
212 ASSERT(mSamplersPS[samplerIndex].active);
213 return mSamplersPS[samplerIndex].textureType;
214 case SAMPLER_VERTEX:
215 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
216 ASSERT(mSamplersVS[samplerIndex].active);
217 return mSamplersVS[samplerIndex].textureType;
218 default: UNREACHABLE();
219 }
220
221 return TEXTURE_2D;
222}
223
224GLint ProgramBinary::getUniformLocation(std::string name)
225{
226 unsigned int subscript = 0;
227
228 // Strip any trailing array operator and retrieve the subscript
229 size_t open = name.find_last_of('[');
230 size_t close = name.find_last_of(']');
231 if (open != std::string::npos && close == name.length() - 1)
232 {
233 subscript = atoi(name.substr(open + 1).c_str());
234 name.erase(open);
235 }
236
237 unsigned int numUniforms = mUniformIndex.size();
238 for (unsigned int location = 0; location < numUniforms; location++)
239 {
240 if (mUniformIndex[location].name == name &&
241 mUniformIndex[location].element == subscript)
242 {
243 return location;
244 }
245 }
246
247 return -1;
248}
249
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000250template <typename T>
251bool ProgramBinary::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000252{
253 if (location < 0 || location >= (int)mUniformIndex.size())
254 {
255 return false;
256 }
257
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000258 const int components = UniformComponentCount(targetUniformType);
259 const GLenum targetBoolType = UniformBoolVectorType(targetUniformType);
260
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000261 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
262 targetUniform->dirty = true;
263
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000264 int elementCount = targetUniform->elementCount();
265
266 if (elementCount == 1 && count > 1)
267 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
268
269 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
270
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000271 if (targetUniform->type == targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000272 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000273 T *target = (T*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000274
275 for (int i = 0; i < count; i++)
276 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000277 for (int c = 0; c < components; c++)
278 {
279 target[c] = v[c];
280 }
281 for (int c = components; c < 4; c++)
282 {
283 target[c] = 0;
284 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000285 target += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000286 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000287 }
288 }
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000289 else if (targetUniform->type == targetBoolType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000290 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000291 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000292
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000293 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000294 {
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000295 for (int c = 0; c < components; c++)
296 {
297 boolParams[c] = (v[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
298 }
299 for (int c = components; c < 4; c++)
300 {
301 boolParams[c] = GL_FALSE;
302 }
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000303 boolParams += 4;
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000304 v += components;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000305 }
306 }
307 else
308 {
309 return false;
310 }
311
312 return true;
313}
314
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000315bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
316{
317 return setUniform(location, count, v, GL_FLOAT);
318}
319
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000320bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
321{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000322 return setUniform(location, count, v, GL_FLOAT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000323}
324
325bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
326{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000327 return setUniform(location, count, v, GL_FLOAT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000328}
329
330bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
331{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000332 return setUniform(location, count, v, GL_FLOAT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000333}
334
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000335template<typename T>
336void transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000337{
338 int copyWidth = std::min(targetWidth, srcWidth);
339 int copyHeight = std::min(targetHeight, srcHeight);
340
341 for (int x = 0; x < copyWidth; x++)
342 {
343 for (int y = 0; y < copyHeight; y++)
344 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000345 target[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000346 }
347 }
348 // clear unfilled right side
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000349 for (int y = 0; y < copyWidth; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000350 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000351 for (int x = copyHeight; x < targetWidth; x++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000352 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000353 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000354 }
355 }
356 // clear unfilled bottom.
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000357 for (int y = copyWidth; y < targetHeight; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000358 {
359 for (int x = 0; x < targetWidth; x++)
360 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000361 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000362 }
363 }
364}
365
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000366template<typename T>
367void expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
368{
369 int copyWidth = std::min(targetWidth, srcWidth);
370 int copyHeight = std::min(targetHeight, srcHeight);
371
372 for (int y = 0; y < copyHeight; y++)
373 {
374 for (int x = 0; x < copyWidth; x++)
375 {
376 target[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
377 }
378 }
379 // clear unfilled right side
380 for (int y = 0; y < copyHeight; y++)
381 {
382 for (int x = copyWidth; x < targetWidth; x++)
383 {
384 target[y * targetWidth + x] = static_cast<T>(0);
385 }
386 }
387 // clear unfilled bottom.
388 for (int y = copyHeight; y < targetHeight; y++)
389 {
390 for (int x = 0; x < targetWidth; x++)
391 {
392 target[y * targetWidth + x] = static_cast<T>(0);
393 }
394 }
395}
396
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000397template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000398bool ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000399{
400 if (location < 0 || location >= (int)mUniformIndex.size())
401 {
402 return false;
403 }
404
405 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
406 targetUniform->dirty = true;
407
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000408 if (targetUniform->type != targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000409 {
410 return false;
411 }
412
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000413 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000414
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000415 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000416 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
417
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000418 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000419 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4 * rows);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000420
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000421 for (int i = 0; i < count; i++)
422 {
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000423 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
424 if (transpose == GL_FALSE)
425 {
426 transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols);
427 }
428 else
429 {
430 expandMatrix<GLfloat>(target, value, 4, rows, cols, rows);
431 }
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000432 target += 4 * rows;
433 value += cols * rows;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000434 }
435
436 return true;
437}
438
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000439bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000440{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000441 return setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000442}
443
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000444bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000445{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000446 return setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000447}
448
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000449bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000450{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000451 return setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000452}
453
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000454bool ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000455{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000456 return setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000457}
458
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000459bool ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000460{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000461 return setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000462}
463
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000464bool ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000465{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000466 return setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000467}
468
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000469bool ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000470{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000471 return setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000472}
473
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000474bool ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000475{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000476 return setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000477}
478
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000479bool ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000480{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000481 return setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000482}
483
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000484bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
485{
486 if (location < 0 || location >= (int)mUniformIndex.size())
487 {
488 return false;
489 }
490
491 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
492 targetUniform->dirty = true;
493
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000494 int elementCount = targetUniform->elementCount();
495
496 if (elementCount == 1 && count > 1)
497 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
498
499 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
500
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000501 if (targetUniform->type == GL_INT ||
502 targetUniform->type == GL_SAMPLER_2D ||
503 targetUniform->type == GL_SAMPLER_CUBE)
504 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000505 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000506
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000507 for (int i = 0; i < count; i++)
508 {
509 target[0] = v[0];
510 target[1] = 0;
511 target[2] = 0;
512 target[3] = 0;
513 target += 4;
514 v += 1;
515 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000516 }
517 else if (targetUniform->type == GL_BOOL)
518 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000519 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000520
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000521 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000522 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000523 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
524 boolParams[1] = GL_FALSE;
525 boolParams[2] = GL_FALSE;
526 boolParams[3] = GL_FALSE;
527 boolParams += 4;
528 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000529 }
530 }
531 else
532 {
533 return false;
534 }
535
536 return true;
537}
538
539bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
540{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000541 return setUniform(location, count, v, GL_INT_VEC2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000542}
543
544bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
545{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000546 return setUniform(location, count, v, GL_INT_VEC3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000547}
548
549bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
550{
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000551 return setUniform(location, count, v, GL_INT_VEC4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000552}
553
554bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
555{
556 if (location < 0 || location >= (int)mUniformIndex.size())
557 {
558 return false;
559 }
560
561 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
562
563 // sized queries -- ensure the provided buffer is large enough
564 if (bufSize)
565 {
566 int requiredBytes = UniformExternalSize(targetUniform->type);
567 if (*bufSize < requiredBytes)
568 {
569 return false;
570 }
571 }
572
573 switch (targetUniform->type)
574 {
575 case GL_FLOAT_MAT2:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000576 transposeMatrix<GLfloat>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8, 2, 2, 4, 2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000577 break;
578 case GL_FLOAT_MAT3:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000579 transposeMatrix<GLfloat>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12, 3, 3, 4, 3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000580 break;
581 case GL_FLOAT_MAT4:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000582 transposeMatrix<GLfloat>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16, 4, 4, 4, 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000583 break;
584 default:
585 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000586 unsigned int size = UniformComponentCount(targetUniform->type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000587
588 switch (UniformComponentType(targetUniform->type))
589 {
590 case GL_BOOL:
591 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000592 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000593
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000594 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000595 {
596 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
597 }
598 }
599 break;
600 case GL_FLOAT:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000601 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLfloat),
602 size * sizeof(GLfloat));
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000603 break;
604 case GL_INT:
605 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000606 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000607
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000608 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000609 {
610 params[i] = (float)intParams[i];
611 }
612 }
613 break;
614 default: UNREACHABLE();
615 }
616 }
617 }
618
619 return true;
620}
621
622bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
623{
624 if (location < 0 || location >= (int)mUniformIndex.size())
625 {
626 return false;
627 }
628
629 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
630
631 // sized queries -- ensure the provided buffer is large enough
632 if (bufSize)
633 {
634 int requiredBytes = UniformExternalSize(targetUniform->type);
635 if (*bufSize < requiredBytes)
636 {
637 return false;
638 }
639 }
640
641 switch (targetUniform->type)
642 {
643 case GL_FLOAT_MAT2:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000644 transposeMatrix<GLint>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8, 2, 2, 4, 2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000645 break;
646 case GL_FLOAT_MAT3:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000647 transposeMatrix<GLint>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12, 3, 3, 4, 3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000648 break;
649 case GL_FLOAT_MAT4:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000650 transposeMatrix<GLint>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16, 4, 4, 4, 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000651 break;
652 default:
653 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000654 unsigned int size = VariableColumnCount(targetUniform->type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000655
656 switch (UniformComponentType(targetUniform->type))
657 {
658 case GL_BOOL:
659 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000660 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000661
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000662 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000663 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000664 params[i] = boolParams[i];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000665 }
666 }
667 break;
668 case GL_FLOAT:
669 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000670 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000671
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000672 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000673 {
674 params[i] = (GLint)floatParams[i];
675 }
676 }
677 break;
678 case GL_INT:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000679 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLint),
680 size * sizeof(GLint));
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000681 break;
682 default: UNREACHABLE();
683 }
684 }
685 }
686
687 return true;
688}
689
690void ProgramBinary::dirtyAllUniforms()
691{
692 unsigned int numUniforms = mUniforms.size();
693 for (unsigned int index = 0; index < numUniforms; index++)
694 {
695 mUniforms[index]->dirty = true;
696 }
697}
698
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000699// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000700void ProgramBinary::applyUniforms()
701{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000702 // Retrieve sampler uniform values
703 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
704 {
705 Uniform *targetUniform = *ub;
706
707 if (targetUniform->dirty)
708 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000709 if (targetUniform->type == GL_SAMPLER_2D ||
710 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000711 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000712 int count = targetUniform->elementCount();
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000713 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000714
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000715 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000716 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000717 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000718
719 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000720 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000721 unsigned int samplerIndex = firstIndex + i;
722
723 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000724 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000725 ASSERT(mSamplersPS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000726 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000727 }
728 }
729 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000730
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000731 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000732 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000733 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000734
735 for (int i = 0; i < count; i++)
736 {
737 unsigned int samplerIndex = firstIndex + i;
738
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000739 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000740 {
741 ASSERT(mSamplersVS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000742 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000743 }
744 }
745 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000746 }
747 }
748 }
749
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +0000750 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000751}
752
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000753// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
754// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000755int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000756{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000757 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000758
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000759 fragmentShader->resetVaryingsRegisterAssignment();
760
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000761 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
762 {
763 int n = VariableRowCount(varying->type) * varying->size;
764 int m = VariableColumnCount(varying->type);
765 bool success = false;
766
767 if (m == 2 || m == 3 || m == 4)
768 {
769 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
770 {
771 bool available = true;
772
773 for (int y = 0; y < n && available; y++)
774 {
775 for (int x = 0; x < m && available; x++)
776 {
777 if (packing[r + y][x])
778 {
779 available = false;
780 }
781 }
782 }
783
784 if (available)
785 {
786 varying->reg = r;
787 varying->col = 0;
788
789 for (int y = 0; y < n; y++)
790 {
791 for (int x = 0; x < m; x++)
792 {
793 packing[r + y][x] = &*varying;
794 }
795 }
796
797 success = true;
798 }
799 }
800
801 if (!success && m == 2)
802 {
803 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
804 {
805 bool available = true;
806
807 for (int y = 0; y < n && available; y++)
808 {
809 for (int x = 2; x < 4 && available; x++)
810 {
811 if (packing[r + y][x])
812 {
813 available = false;
814 }
815 }
816 }
817
818 if (available)
819 {
820 varying->reg = r;
821 varying->col = 2;
822
823 for (int y = 0; y < n; y++)
824 {
825 for (int x = 2; x < 4; x++)
826 {
827 packing[r + y][x] = &*varying;
828 }
829 }
830
831 success = true;
832 }
833 }
834 }
835 }
836 else if (m == 1)
837 {
838 int space[4] = {0};
839
840 for (int y = 0; y < maxVaryingVectors; y++)
841 {
842 for (int x = 0; x < 4; x++)
843 {
844 space[x] += packing[y][x] ? 0 : 1;
845 }
846 }
847
848 int column = 0;
849
850 for (int x = 0; x < 4; x++)
851 {
852 if (space[x] >= n && space[x] < space[column])
853 {
854 column = x;
855 }
856 }
857
858 if (space[column] >= n)
859 {
860 for (int r = 0; r < maxVaryingVectors; r++)
861 {
862 if (!packing[r][column])
863 {
864 varying->reg = r;
865
866 for (int y = r; y < r + n; y++)
867 {
868 packing[y][column] = &*varying;
869 }
870
871 break;
872 }
873 }
874
875 varying->col = column;
876
877 success = true;
878 }
879 }
880 else UNREACHABLE();
881
882 if (!success)
883 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000884 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000885
886 return -1;
887 }
888 }
889
890 // Return the number of used registers
891 int registers = 0;
892
893 for (int r = 0; r < maxVaryingVectors; r++)
894 {
895 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
896 {
897 registers++;
898 }
899 }
900
901 return registers;
902}
903
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000904bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
905 std::string& pixelHLSL, std::string& vertexHLSL,
906 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000907{
908 if (pixelHLSL.empty() || vertexHLSL.empty())
909 {
910 return false;
911 }
912
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000913 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
914 bool usesFragColor = fragmentShader->mUsesFragColor;
915 bool usesFragData = fragmentShader->mUsesFragData;
916 if (usesMRT && usesFragColor && usesFragData)
917 {
918 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
919 return false;
920 }
921
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000922 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000923 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +0000924 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000925
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000926 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
927
928 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000929 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000930 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000931
932 return false;
933 }
934
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000935 vertexShader->resetVaryingsRegisterAssignment();
936
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000937 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
938 {
939 bool matched = false;
940
941 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
942 {
943 if (output->name == input->name)
944 {
945 if (output->type != input->type || output->size != input->size)
946 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000947 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 +0000948
949 return false;
950 }
951
952 output->reg = input->reg;
953 output->col = input->col;
954
955 matched = true;
956 break;
957 }
958 }
959
960 if (!matched)
961 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000962 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000963
964 return false;
965 }
966 }
967
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000968 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000969 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +0000970 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000971 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
972
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000973 const unsigned int renderTargetCount = usesMRT ? mRenderer->getMaxRenderTargets() : 1;
974
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +0000975 // special varyings that use reserved registers
976 int reservedRegisterIndex = registers;
977 std::string fragCoordSemantic;
978 std::string pointCoordSemantic;
979
980 if (fragmentShader->mUsesFragCoord)
981 {
982 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
983 }
984
985 if (fragmentShader->mUsesPointCoord)
986 {
987 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
988 // In DX11 we compute this in the GS.
989 if (shaderModel == 3)
990 {
991 pointCoordSemantic = "TEXCOORD0";
992 }
993 else if (shaderModel >= 4)
994 {
995 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
996 }
997 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000998
999 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001000 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001001
1002 int semanticIndex = 0;
1003 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1004 {
1005 switch (attribute->type)
1006 {
1007 case GL_FLOAT: vertexHLSL += " float "; break;
1008 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1009 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1010 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1011 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1012 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1013 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1014 default: UNREACHABLE();
1015 }
1016
1017 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1018
1019 semanticIndex += VariableRowCount(attribute->type);
1020 }
1021
1022 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001023 "\n"
1024 "struct VS_OUTPUT\n"
1025 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001026
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001027 if (shaderModel < 4)
1028 {
1029 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1030 }
1031
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001032 for (int r = 0; r < registers; r++)
1033 {
1034 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1035
1036 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1037 }
1038
1039 if (fragmentShader->mUsesFragCoord)
1040 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001041 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001042 }
1043
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001044 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001045 {
1046 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1047 }
1048
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001049 if (shaderModel >= 4)
1050 {
1051 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1052 }
1053
1054 vertexHLSL += "};\n"
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001055 "\n"
1056 "VS_OUTPUT main(VS_INPUT input)\n"
1057 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001058
1059 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1060 {
1061 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1062
1063 if (VariableRowCount(attribute->type) > 1) // Matrix
1064 {
1065 vertexHLSL += "transpose";
1066 }
1067
1068 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1069 }
1070
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001071 if (shaderModel >= 4)
1072 {
1073 vertexHLSL += "\n"
1074 " gl_main();\n"
1075 "\n"
1076 " VS_OUTPUT output;\n"
1077 " output.gl_Position.x = gl_Position.x;\n"
1078 " output.gl_Position.y = -gl_Position.y;\n"
1079 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1080 " output.gl_Position.w = gl_Position.w;\n";
1081 }
1082 else
1083 {
1084 vertexHLSL += "\n"
1085 " gl_main();\n"
1086 "\n"
1087 " VS_OUTPUT output;\n"
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001088 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1089 " 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 +00001090 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1091 " output.gl_Position.w = gl_Position.w;\n";
1092 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001093
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001094 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001095 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001096 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001097 }
1098
1099 if (fragmentShader->mUsesFragCoord)
1100 {
1101 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1102 }
1103
1104 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1105 {
1106 if (varying->reg >= 0)
1107 {
1108 for (int i = 0; i < varying->size; i++)
1109 {
1110 int rows = VariableRowCount(varying->type);
1111
1112 for (int j = 0; j < rows; j++)
1113 {
1114 int r = varying->reg + i * rows + j;
1115 vertexHLSL += " output.v" + str(r);
1116
1117 bool sharedRegister = false; // Register used by multiple varyings
1118
1119 for (int x = 0; x < 4; x++)
1120 {
1121 if (packing[r][x] && packing[r][x] != packing[r][0])
1122 {
1123 sharedRegister = true;
1124 break;
1125 }
1126 }
1127
1128 if(sharedRegister)
1129 {
1130 vertexHLSL += ".";
1131
1132 for (int x = 0; x < 4; x++)
1133 {
1134 if (packing[r][x] == &*varying)
1135 {
1136 switch(x)
1137 {
1138 case 0: vertexHLSL += "x"; break;
1139 case 1: vertexHLSL += "y"; break;
1140 case 2: vertexHLSL += "z"; break;
1141 case 3: vertexHLSL += "w"; break;
1142 }
1143 }
1144 }
1145 }
1146
1147 vertexHLSL += " = " + varying->name;
1148
1149 if (varying->array)
1150 {
1151 vertexHLSL += "[" + str(i) + "]";
1152 }
1153
1154 if (rows > 1)
1155 {
1156 vertexHLSL += "[" + str(j) + "]";
1157 }
1158
1159 vertexHLSL += ";\n";
1160 }
1161 }
1162 }
1163 }
1164
1165 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001166 " return output;\n"
1167 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001168
1169 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001170 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001171
1172 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1173 {
1174 if (varying->reg >= 0)
1175 {
1176 for (int i = 0; i < varying->size; i++)
1177 {
1178 int rows = VariableRowCount(varying->type);
1179 for (int j = 0; j < rows; j++)
1180 {
1181 std::string n = str(varying->reg + i * rows + j);
daniel@transgaming.com00c0d152013-01-11 04:07:23 +00001182 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001183 }
1184 }
1185 }
1186 else UNREACHABLE();
1187 }
1188
1189 if (fragmentShader->mUsesFragCoord)
1190 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001191 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001192 }
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001193
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001194 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1195 {
1196 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1197 }
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001198
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001199 // Must consume the PSIZE element if the geometry shader is not active
1200 // We won't know if we use a GS until we draw
1201 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1202 {
1203 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1204 }
1205
1206 if (fragmentShader->mUsesFragCoord)
1207 {
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001208 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001209 {
1210 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1211 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001212 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001213 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001214 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1215 }
1216 }
1217
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001218 pixelHLSL += "};\n"
1219 "\n"
1220 "struct PS_OUTPUT\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001221 "{\n";
1222
1223 for (unsigned int i = 0; i < renderTargetCount; i++)
1224 {
1225 pixelHLSL += " float4 gl_Color" + str(i) + " : " + targetSemantic + str(i) + ";\n";
1226 }
1227
1228 pixelHLSL += "};\n"
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001229 "\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001230
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001231 if (fragmentShader->mUsesFrontFacing)
1232 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001233 if (shaderModel >= 4)
1234 {
1235 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1236 "{\n";
1237 }
1238 else
1239 {
1240 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1241 "{\n";
1242 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001243 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001244 else
1245 {
1246 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1247 "{\n";
1248 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001249
1250 if (fragmentShader->mUsesFragCoord)
1251 {
1252 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1253
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001254 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001255 {
1256 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1257 " gl_FragCoord.y = input.dx_VPos.y;\n";
1258 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001259 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001260 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001261 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001262 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001263 }
1264 else
1265 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001266 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1267 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1268 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001269 }
1270
daniel@transgaming.com12985182012-12-20 20:56:31 +00001271 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001272 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001273 }
1274
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001275 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001276 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001277 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1278 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001279 }
1280
1281 if (fragmentShader->mUsesFrontFacing)
1282 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001283 if (shaderModel <= 3)
1284 {
1285 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1286 }
1287 else
1288 {
1289 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1290 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001291 }
1292
1293 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1294 {
1295 if (varying->reg >= 0)
1296 {
1297 for (int i = 0; i < varying->size; i++)
1298 {
1299 int rows = VariableRowCount(varying->type);
1300 for (int j = 0; j < rows; j++)
1301 {
1302 std::string n = str(varying->reg + i * rows + j);
1303 pixelHLSL += " " + varying->name;
1304
1305 if (varying->array)
1306 {
1307 pixelHLSL += "[" + str(i) + "]";
1308 }
1309
1310 if (rows > 1)
1311 {
1312 pixelHLSL += "[" + str(j) + "]";
1313 }
1314
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001315 switch (VariableColumnCount(varying->type))
1316 {
1317 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1318 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1319 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1320 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1321 default: UNREACHABLE();
1322 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001323 }
1324 }
1325 }
1326 else UNREACHABLE();
1327 }
1328
1329 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001330 " gl_main();\n"
1331 "\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001332 " PS_OUTPUT output;\n";
1333
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +00001334 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1335 // - with a 3.0 context, the output color is copied to channel 0
1336 // - with a 2.0 context using EXT_draw_buffers, the output color is broadcast to all channels
1337 const bool broadcast = fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3;
1338
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001339 for (unsigned int i = 0; i < renderTargetCount; i++)
1340 {
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +00001341 unsigned int sourceColor = !broadcast ? i : 0;
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001342
1343 pixelHLSL += " output.gl_Color" + str(i) + " = gl_Color[" + str(sourceColor) + "];\n";
1344 }
1345
1346 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001347 " return output;\n"
1348 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001349
1350 return true;
1351}
1352
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001353bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1354{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001355 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001356
1357 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001358 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001359 if (format != GL_PROGRAM_BINARY_ANGLE)
1360 {
1361 infoLog.append("Invalid program binary format.");
1362 return false;
1363 }
1364
1365 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001366 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001367 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001368 {
1369 infoLog.append("Invalid program binary version.");
1370 return false;
1371 }
1372
1373 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1374 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001375 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001376 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001377 stream.read(&name);
1378 mLinkedAttribute[i].name = name;
1379 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001380 }
1381
1382 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1383 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001384 stream.read(&mSamplersPS[i].active);
1385 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001386
1387 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001388 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001389 mSamplersPS[i].textureType = (TextureType) textureType;
1390 }
1391
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001392 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001393 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001394 stream.read(&mSamplersVS[i].active);
1395 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001396
1397 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001398 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001399 mSamplersVS[i].textureType = (TextureType) textureType;
1400 }
1401
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001402 stream.read(&mUsedVertexSamplerRange);
1403 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001404 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001405
shannon.woods@transgaming.com45886d62013-02-28 23:19:20 +00001406 size_t size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001407 stream.read(&size);
1408 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001409 {
1410 infoLog.append("Invalid program binary.");
1411 return false;
1412 }
1413
1414 mUniforms.resize(size);
1415 for (unsigned int i = 0; i < size; ++i)
1416 {
1417 GLenum type;
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001418 GLenum precision;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001419 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001420 unsigned int arraySize;
1421
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001422 stream.read(&type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001423 stream.read(&precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001424 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001425 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001426
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001427 mUniforms[i] = new Uniform(type, precision, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001428
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001429 stream.read(&mUniforms[i]->psRegisterIndex);
1430 stream.read(&mUniforms[i]->vsRegisterIndex);
1431 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001432 }
1433
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001434 stream.read(&size);
1435 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001436 {
1437 infoLog.append("Invalid program binary.");
1438 return false;
1439 }
1440
1441 mUniformIndex.resize(size);
1442 for (unsigned int i = 0; i < size; ++i)
1443 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001444 stream.read(&mUniformIndex[i].name);
1445 stream.read(&mUniformIndex[i].element);
1446 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001447 }
1448
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001449 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001450 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001451
1452 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001453 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001454
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001455 unsigned int geometryShaderSize;
1456 stream.read(&geometryShaderSize);
1457
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001458 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001459
daniel@transgaming.com36038542012-11-28 20:59:26 +00001460 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001461 ptr += sizeof(GUID);
1462
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001463 GUID identifier = mRenderer->getAdapterIdentifier();
1464 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001465 {
1466 infoLog.append("Invalid program binary.");
1467 return false;
1468 }
1469
1470 const char *pixelShaderFunction = ptr;
1471 ptr += pixelShaderSize;
1472
1473 const char *vertexShaderFunction = ptr;
1474 ptr += vertexShaderSize;
1475
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001476 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1477 ptr += geometryShaderSize;
1478
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001479 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001480 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001481 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001482 {
1483 infoLog.append("Could not create pixel shader.");
1484 return false;
1485 }
1486
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001487 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001488 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001489 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001490 {
1491 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001492 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001493 mPixelExecutable = NULL;
1494 return false;
1495 }
1496
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001497 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1498 {
1499 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1500 geometryShaderSize, rx::SHADER_GEOMETRY);
1501 if (!mGeometryExecutable)
1502 {
1503 infoLog.append("Could not create geometry shader.");
1504 delete mPixelExecutable;
1505 mPixelExecutable = NULL;
1506 delete mVertexExecutable;
1507 mVertexExecutable = NULL;
1508 return false;
1509 }
1510 }
1511 else
1512 {
1513 mGeometryExecutable = NULL;
1514 }
1515
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001516 return true;
1517}
1518
1519bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1520{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001521 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001522
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001523 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001524 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001525
1526 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1527 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001528 stream.write(mLinkedAttribute[i].type);
1529 stream.write(mLinkedAttribute[i].name);
1530 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001531 }
1532
1533 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1534 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001535 stream.write(mSamplersPS[i].active);
1536 stream.write(mSamplersPS[i].logicalTextureUnit);
1537 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001538 }
1539
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001540 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001541 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001542 stream.write(mSamplersVS[i].active);
1543 stream.write(mSamplersVS[i].logicalTextureUnit);
1544 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001545 }
1546
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001547 stream.write(mUsedVertexSamplerRange);
1548 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001549 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001550
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1553 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001554 stream.write(mUniforms[i]->type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001555 stream.write(mUniforms[i]->precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001556 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001557 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001558
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001559 stream.write(mUniforms[i]->psRegisterIndex);
1560 stream.write(mUniforms[i]->vsRegisterIndex);
1561 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001562 }
1563
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001564 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001565 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1566 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001567 stream.write(mUniformIndex[i].name);
1568 stream.write(mUniformIndex[i].element);
1569 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001570 }
1571
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001572 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001573 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001574
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001575 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001576 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001577
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001578 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1579 stream.write(geometryShaderSize);
1580
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001581 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001582
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001583 GLsizei streamLength = stream.length();
1584 const void *streamData = stream.data();
1585
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001586 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001587 if (totalLength > bufSize)
1588 {
1589 if (length)
1590 {
1591 *length = 0;
1592 }
1593
1594 return false;
1595 }
1596
1597 if (binary)
1598 {
1599 char *ptr = (char*) binary;
1600
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001601 memcpy(ptr, streamData, streamLength);
1602 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001603
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001604 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001605 ptr += sizeof(GUID);
1606
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001607 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001608 ptr += pixelShaderSize;
1609
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001610 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001611 ptr += vertexShaderSize;
1612
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001613 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1614 {
1615 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1616 ptr += geometryShaderSize;
1617 }
1618
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619 ASSERT(ptr - totalLength == binary);
1620 }
1621
1622 if (length)
1623 {
1624 *length = totalLength;
1625 }
1626
1627 return true;
1628}
1629
1630GLint ProgramBinary::getLength()
1631{
1632 GLint length;
1633 if (save(NULL, INT_MAX, &length))
1634 {
1635 return length;
1636 }
1637 else
1638 {
1639 return 0;
1640 }
1641}
1642
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001643bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001644{
1645 if (!fragmentShader || !fragmentShader->isCompiled())
1646 {
1647 return false;
1648 }
1649
1650 if (!vertexShader || !vertexShader->isCompiled())
1651 {
1652 return false;
1653 }
1654
1655 std::string pixelHLSL = fragmentShader->getHLSL();
1656 std::string vertexHLSL = vertexShader->getHLSL();
1657
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001658 // Map the varyings to the register file
1659 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1660 int registers = packVaryings(infoLog, packing, fragmentShader);
1661
1662 if (registers < 0)
1663 {
1664 return false;
1665 }
1666
1667 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001668 {
1669 return false;
1670 }
1671
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001672 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001673 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1674 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001675
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001676 if (usesGeometryShader())
1677 {
1678 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1679 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY);
1680 }
1681
1682 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001683 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001684 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001685 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001686
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001687 delete mVertexExecutable;
1688 mVertexExecutable = NULL;
1689 delete mPixelExecutable;
1690 mPixelExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001691 delete mGeometryExecutable;
1692 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001693 }
1694
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001695 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1696 {
1697 success = false;
1698 }
1699
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001700 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001701 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001702 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001703 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001704
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001705 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001706}
1707
1708// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001709bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001710{
1711 unsigned int usedLocations = 0;
1712
1713 // Link attributes that have a binding location
1714 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1715 {
1716 int location = attributeBindings.getAttributeBinding(attribute->name);
1717
1718 if (location != -1) // Set by glBindAttribLocation
1719 {
1720 if (!mLinkedAttribute[location].name.empty())
1721 {
1722 // Multiple active attributes bound to the same location; not an error
1723 }
1724
1725 mLinkedAttribute[location] = *attribute;
1726
1727 int rows = VariableRowCount(attribute->type);
1728
1729 if (rows + location > MAX_VERTEX_ATTRIBS)
1730 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001731 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 +00001732
1733 return false;
1734 }
1735
1736 for (int i = 0; i < rows; i++)
1737 {
1738 usedLocations |= 1 << (location + i);
1739 }
1740 }
1741 }
1742
1743 // Link attributes that don't have a binding location
1744 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1745 {
1746 int location = attributeBindings.getAttributeBinding(attribute->name);
1747
1748 if (location == -1) // Not set by glBindAttribLocation
1749 {
1750 int rows = VariableRowCount(attribute->type);
1751 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1752
1753 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1754 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001755 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001756
1757 return false; // Fail to link
1758 }
1759
1760 mLinkedAttribute[availableIndex] = *attribute;
1761 }
1762 }
1763
1764 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1765 {
1766 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1767 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1768
1769 for (int r = 0; r < rows; r++)
1770 {
1771 mSemanticIndex[attributeIndex++] = index++;
1772 }
1773 }
1774
1775 return true;
1776}
1777
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001778bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001779{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001780 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001781 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001782 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001783 {
1784 return false;
1785 }
1786 }
1787
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001788 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001789 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001790 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001791 {
1792 return false;
1793 }
1794 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001795
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001796 return true;
1797}
1798
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001799bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001800{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001801 if (constant.type == GL_SAMPLER_2D ||
1802 constant.type == GL_SAMPLER_CUBE)
1803 {
1804 unsigned int samplerIndex = constant.registerIndex;
1805
1806 do
1807 {
1808 if (shader == GL_VERTEX_SHADER)
1809 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001810 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001811 {
1812 mSamplersVS[samplerIndex].active = true;
1813 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1814 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1815 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1816 }
1817 else
1818 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001819 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001820 return false;
1821 }
1822 }
1823 else if (shader == GL_FRAGMENT_SHADER)
1824 {
1825 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1826 {
1827 mSamplersPS[samplerIndex].active = true;
1828 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
1829 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1830 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1831 }
1832 else
1833 {
1834 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
1835 return false;
1836 }
1837 }
1838 else UNREACHABLE();
1839
1840 samplerIndex++;
1841 }
1842 while (samplerIndex < constant.registerIndex + constant.arraySize);
1843 }
1844
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001845 Uniform *uniform = NULL;
1846 GLint location = getUniformLocation(constant.name);
1847
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001848 if (location >= 0) // Previously defined, type and precision must match
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001849 {
1850 uniform = mUniforms[mUniformIndex[location].index];
1851
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00001852 if (uniform->type != constant.type)
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001853 {
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00001854 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
1855 return false;
1856 }
1857
1858 if (uniform->precision != constant.precision)
1859 {
1860 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 +00001861 return false;
1862 }
1863 }
1864 else
1865 {
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001866 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize);
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001867 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001868
1869 if (!uniform)
1870 {
1871 return false;
1872 }
1873
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001874 if (shader == GL_FRAGMENT_SHADER)
1875 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001876 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001877 }
1878 else if (shader == GL_VERTEX_SHADER)
1879 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001880 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001881 }
1882 else UNREACHABLE();
1883
1884 if (location >= 0)
1885 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001886 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001887 }
1888
1889 mUniforms.push_back(uniform);
1890 unsigned int uniformIndex = mUniforms.size() - 1;
1891
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00001892 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001893 {
1894 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
1895 }
1896
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00001897 if (shader == GL_VERTEX_SHADER)
1898 {
1899 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
1900 {
1901 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
1902 return false;
1903 }
1904 }
1905 else if (shader == GL_FRAGMENT_SHADER)
1906 {
1907 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
1908 {
1909 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
1910 return false;
1911 }
1912 }
1913 else UNREACHABLE();
1914
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001915 return true;
1916}
1917
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001918std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
1919{
1920 // for now we only handle point sprite emulation
1921 ASSERT(usesPointSpriteEmulation());
1922 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
1923}
1924
1925std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
1926{
1927 ASSERT(registers >= 0);
1928 ASSERT(vertexShader->mUsesPointSize);
1929 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1930
1931 std::string geomHLSL;
1932
1933 std::string varyingSemantic = "TEXCOORD";
1934
1935 std::string fragCoordSemantic;
1936 std::string pointCoordSemantic;
1937
1938 int reservedRegisterIndex = registers;
1939
1940 if (fragmentShader->mUsesFragCoord)
1941 {
1942 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1943 }
1944
1945 if (fragmentShader->mUsesPointCoord)
1946 {
1947 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1948 }
1949
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001950 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1951 "\n"
1952 "struct GS_INPUT\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001953 "{\n";
1954
1955 for (int r = 0; r < registers; r++)
1956 {
1957 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1958
1959 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1960 }
1961
1962 if (fragmentShader->mUsesFragCoord)
1963 {
1964 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1965 }
1966
1967 geomHLSL += " float gl_PointSize : PSIZE;\n"
1968 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001969 "};\n"
1970 "\n"
1971 "struct GS_OUTPUT\n"
1972 "{\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001973
1974 for (int r = 0; r < registers; r++)
1975 {
1976 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1977
1978 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1979 }
1980
1981 if (fragmentShader->mUsesFragCoord)
1982 {
1983 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
1984 }
1985
1986 if (fragmentShader->mUsesPointCoord)
1987 {
1988 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1989 }
1990
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001991 geomHLSL += " float gl_PointSize : PSIZE;\n"
1992 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001993 "};\n"
1994 "\n"
1995 "static float2 pointSpriteCorners[] = \n"
1996 "{\n"
1997 " float2( 0.5f, -0.5f),\n"
1998 " float2( 0.5f, 0.5f),\n"
1999 " float2(-0.5f, -0.5f),\n"
2000 " float2(-0.5f, 0.5f)\n"
2001 "};\n"
2002 "\n"
2003 "static float2 pointSpriteTexcoords[] = \n"
2004 "{\n"
2005 " float2(1.0f, 1.0f),\n"
2006 " float2(1.0f, 0.0f),\n"
2007 " float2(0.0f, 1.0f),\n"
2008 " float2(0.0f, 0.0f)\n"
2009 "};\n"
2010 "\n"
2011 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2012 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2013 "\n"
2014 "[maxvertexcount(4)]\n"
2015 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2016 "{\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002017 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2018 " output.gl_PointSize = input[0].gl_PointSize;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002019
2020 for (int r = 0; r < registers; r++)
2021 {
2022 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2023 }
2024
2025 if (fragmentShader->mUsesFragCoord)
2026 {
2027 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2028 }
2029
2030 geomHLSL += " \n"
2031 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2032 " float4 gl_Position = input[0].gl_Position;\n"
shannon.woods@transgaming.com771ca2a2013-02-28 23:14:52 +00002033 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002034
2035 for (int corner = 0; corner < 4; corner++)
2036 {
2037 geomHLSL += " \n"
2038 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2039
2040 if (fragmentShader->mUsesPointCoord)
2041 {
2042 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2043 }
2044
2045 geomHLSL += " outStream.Append(output);\n";
2046 }
2047
2048 geomHLSL += " \n"
2049 " outStream.RestartStrip();\n"
2050 "}\n";
2051
2052 return geomHLSL;
2053}
2054
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002055// This method needs to match OutputHLSL::decorate
2056std::string ProgramBinary::decorateAttribute(const std::string &name)
2057{
2058 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2059 {
2060 return "_" + name;
2061 }
2062
2063 return name;
2064}
2065
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002066bool ProgramBinary::isValidated() const
2067{
2068 return mValidated;
2069}
2070
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002071void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002072{
2073 // Skip over inactive attributes
2074 unsigned int activeAttribute = 0;
2075 unsigned int attribute;
2076 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2077 {
2078 if (mLinkedAttribute[attribute].name.empty())
2079 {
2080 continue;
2081 }
2082
2083 if (activeAttribute == index)
2084 {
2085 break;
2086 }
2087
2088 activeAttribute++;
2089 }
2090
2091 if (bufsize > 0)
2092 {
2093 const char *string = mLinkedAttribute[attribute].name.c_str();
2094
2095 strncpy(name, string, bufsize);
2096 name[bufsize - 1] = '\0';
2097
2098 if (length)
2099 {
2100 *length = strlen(name);
2101 }
2102 }
2103
2104 *size = 1; // Always a single 'type' instance
2105
2106 *type = mLinkedAttribute[attribute].type;
2107}
2108
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002109GLint ProgramBinary::getActiveAttributeCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002110{
2111 int count = 0;
2112
2113 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2114 {
2115 if (!mLinkedAttribute[attributeIndex].name.empty())
2116 {
2117 count++;
2118 }
2119 }
2120
2121 return count;
2122}
2123
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002124GLint ProgramBinary::getActiveAttributeMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002125{
2126 int maxLength = 0;
2127
2128 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2129 {
2130 if (!mLinkedAttribute[attributeIndex].name.empty())
2131 {
2132 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2133 }
2134 }
2135
2136 return maxLength;
2137}
2138
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002139void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002140{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002141 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002142
2143 if (bufsize > 0)
2144 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002145 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002146
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002147 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002148 {
2149 string += "[0]";
2150 }
2151
2152 strncpy(name, string.c_str(), bufsize);
2153 name[bufsize - 1] = '\0';
2154
2155 if (length)
2156 {
2157 *length = strlen(name);
2158 }
2159 }
2160
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002161 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002162
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002163 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002164}
2165
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002166GLint ProgramBinary::getActiveUniformCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002167{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002168 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002169}
2170
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002171GLint ProgramBinary::getActiveUniformMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002172{
2173 int maxLength = 0;
2174
2175 unsigned int numUniforms = mUniforms.size();
2176 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2177 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002178 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002179 {
2180 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2181 if (mUniforms[uniformIndex]->isArray())
2182 {
2183 length += 3; // Counting in "[0]".
2184 }
2185 maxLength = std::max(length, maxLength);
2186 }
2187 }
2188
2189 return maxLength;
2190}
2191
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002192void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002193{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002194 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002195 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002196 {
2197 mValidated = false;
2198 }
2199 else
2200 {
2201 mValidated = true;
2202 }
2203}
2204
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002205bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002206{
2207 // if any two active samplers in a program are of different types, but refer to the same
2208 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2209 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2210
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002211 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002212 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002213
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002214 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002215 {
2216 textureUnitType[i] = TEXTURE_UNKNOWN;
2217 }
2218
2219 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2220 {
2221 if (mSamplersPS[i].active)
2222 {
2223 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2224
2225 if (unit >= maxCombinedTextureImageUnits)
2226 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002227 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002228 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002229 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002230 }
2231
2232 return false;
2233 }
2234
2235 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2236 {
2237 if (mSamplersPS[i].textureType != textureUnitType[unit])
2238 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002239 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002240 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002241 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002242 }
2243
2244 return false;
2245 }
2246 }
2247 else
2248 {
2249 textureUnitType[unit] = mSamplersPS[i].textureType;
2250 }
2251 }
2252 }
2253
2254 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2255 {
2256 if (mSamplersVS[i].active)
2257 {
2258 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2259
2260 if (unit >= maxCombinedTextureImageUnits)
2261 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002262 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002263 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002264 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002265 }
2266
2267 return false;
2268 }
2269
2270 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2271 {
2272 if (mSamplersVS[i].textureType != textureUnitType[unit])
2273 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002274 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002275 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002276 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002277 }
2278
2279 return false;
2280 }
2281 }
2282 else
2283 {
2284 textureUnitType[unit] = mSamplersVS[i].textureType;
2285 }
2286 }
2287 }
2288
2289 return true;
2290}
2291
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002292ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2293{
2294}
2295
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002296struct AttributeSorter
2297{
2298 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2299 : originalIndices(semanticIndices)
2300 {
2301 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2302 {
2303 indices[i] = i;
2304 }
2305
2306 std::sort(&indices[0], &indices[MAX_VERTEX_ATTRIBS], *this);
2307 }
2308
2309 bool operator()(int a, int b)
2310 {
2311 return originalIndices[a] == -1 ? false : originalIndices[a] < originalIndices[b];
2312 }
2313
2314 int indices[MAX_VERTEX_ATTRIBS];
2315 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2316};
2317
2318void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2319{
2320 AttributeSorter sorter(mSemanticIndex);
2321
2322 int oldIndices[MAX_VERTEX_ATTRIBS];
2323 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2324
2325 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2326 {
2327 oldIndices[i] = mSemanticIndex[i];
2328 oldTranslatedAttributes[i] = attributes[i];
2329 }
2330
2331 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2332 {
2333 int oldIndex = sorter.indices[i];
2334 sortedSemanticIndices[i] = oldIndices[oldIndex];
2335 attributes[i] = oldTranslatedAttributes[oldIndex];
2336 }
2337}
2338
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002339}