blob: 1fba4de633c250f24d54dd09cfe72622f0eeba78 [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
250bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
251{
252 if (location < 0 || location >= (int)mUniformIndex.size())
253 {
254 return false;
255 }
256
257 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
258 targetUniform->dirty = true;
259
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000260 int elementCount = targetUniform->elementCount();
261
262 if (elementCount == 1 && count > 1)
263 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
264
265 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
266
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000267 if (targetUniform->type == GL_FLOAT)
268 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000269 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
270
271 for (int i = 0; i < count; i++)
272 {
273 target[0] = v[0];
274 target[1] = 0;
275 target[2] = 0;
276 target[3] = 0;
277 target += 4;
278 v += 1;
279 }
280 }
281 else if (targetUniform->type == GL_BOOL)
282 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000283 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000284
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000285 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000286 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000287 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
288 boolParams[1] = GL_FALSE;
289 boolParams[2] = GL_FALSE;
290 boolParams[3] = GL_FALSE;
291 boolParams += 4;
292 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000293 }
294 }
295 else
296 {
297 return false;
298 }
299
300 return true;
301}
302
303bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
304{
305 if (location < 0 || location >= (int)mUniformIndex.size())
306 {
307 return false;
308 }
309
310 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
311 targetUniform->dirty = true;
312
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000313 int elementCount = targetUniform->elementCount();
314
315 if (elementCount == 1 && count > 1)
316 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
317
318 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
319
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000320 if (targetUniform->type == GL_FLOAT_VEC2)
321 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000322 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
323
324 for (int i = 0; i < count; i++)
325 {
326 target[0] = v[0];
327 target[1] = v[1];
328 target[2] = 0;
329 target[3] = 0;
330 target += 4;
331 v += 2;
332 }
333 }
334 else if (targetUniform->type == GL_BOOL_VEC2)
335 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000336 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000337
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000338 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000339 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000340 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
341 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
342 boolParams[2] = GL_FALSE;
343 boolParams[3] = GL_FALSE;
344 boolParams += 4;
345 v += 2;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000346 }
347 }
348 else
349 {
350 return false;
351 }
352
353 return true;
354}
355
356bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
357{
358 if (location < 0 || location >= (int)mUniformIndex.size())
359 {
360 return false;
361 }
362
363 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
364 targetUniform->dirty = true;
365
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000366 int elementCount = targetUniform->elementCount();
367
368 if (elementCount == 1 && count > 1)
369 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
370
371 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
372
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000373 if (targetUniform->type == GL_FLOAT_VEC3)
374 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000375 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
376
377 for (int i = 0; i < count; i++)
378 {
379 target[0] = v[0];
380 target[1] = v[1];
381 target[2] = v[2];
382 target[3] = 0;
383 target += 4;
384 v += 3;
385 }
386 }
387 else if (targetUniform->type == GL_BOOL_VEC3)
388 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000389 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000390
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000391 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000392 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000393 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
394 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
395 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE;
396 boolParams[3] = GL_FALSE;
397 boolParams += 4;
398 v += 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000399 }
400 }
401 else
402 {
403 return false;
404 }
405
406 return true;
407}
408
409bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
410{
411 if (location < 0 || location >= (int)mUniformIndex.size())
412 {
413 return false;
414 }
415
416 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
417 targetUniform->dirty = true;
418
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000419 int elementCount = targetUniform->elementCount();
420
421 if (elementCount == 1 && count > 1)
422 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
423
424 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
425
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000426 if (targetUniform->type == GL_FLOAT_VEC4)
427 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000428 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000429
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000430 for (int i = 0; i < count; i++)
431 {
432 target[0] = v[0];
433 target[1] = v[1];
434 target[2] = v[2];
435 target[3] = v[3];
436 target += 4;
437 v += 4;
438 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000439 }
440 else if (targetUniform->type == GL_BOOL_VEC4)
441 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000442 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000443
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000444 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000445 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000446 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE;
447 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE;
448 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE;
449 boolParams[3] = (v[3] == 0.0f) ? GL_FALSE : GL_TRUE;
450 boolParams += 4;
451 v += 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000452 }
453 }
454 else
455 {
456 return false;
457 }
458
459 return true;
460}
461
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000462template<typename T>
463void transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000464{
465 int copyWidth = std::min(targetWidth, srcWidth);
466 int copyHeight = std::min(targetHeight, srcHeight);
467
468 for (int x = 0; x < copyWidth; x++)
469 {
470 for (int y = 0; y < copyHeight; y++)
471 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000472 target[x * targetWidth + y] = static_cast<T>(value[y * srcWidth + x]);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000473 }
474 }
475 // clear unfilled right side
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000476 for (int y = 0; y < copyWidth; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000477 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000478 for (int x = copyHeight; x < targetWidth; x++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000479 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000480 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000481 }
482 }
483 // clear unfilled bottom.
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000484 for (int y = copyWidth; y < targetHeight; y++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000485 {
486 for (int x = 0; x < targetWidth; x++)
487 {
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000488 target[y * targetWidth + x] = static_cast<T>(0);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000489 }
490 }
491}
492
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000493template<typename T>
494void expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight)
495{
496 int copyWidth = std::min(targetWidth, srcWidth);
497 int copyHeight = std::min(targetHeight, srcHeight);
498
499 for (int y = 0; y < copyHeight; y++)
500 {
501 for (int x = 0; x < copyWidth; x++)
502 {
503 target[y * targetWidth + x] = static_cast<T>(value[y * srcWidth + x]);
504 }
505 }
506 // clear unfilled right side
507 for (int y = 0; y < copyHeight; y++)
508 {
509 for (int x = copyWidth; x < targetWidth; x++)
510 {
511 target[y * targetWidth + x] = static_cast<T>(0);
512 }
513 }
514 // clear unfilled bottom.
515 for (int y = copyHeight; y < targetHeight; y++)
516 {
517 for (int x = 0; x < targetWidth; x++)
518 {
519 target[y * targetWidth + x] = static_cast<T>(0);
520 }
521 }
522}
523
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000524template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000525bool ProgramBinary::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000526{
527 if (location < 0 || location >= (int)mUniformIndex.size())
528 {
529 return false;
530 }
531
532 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
533 targetUniform->dirty = true;
534
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000535 if (targetUniform->type != targetUniformType)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000536 {
537 return false;
538 }
539
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000540 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000541
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000542 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000543 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
544
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000545 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000546 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4 * rows);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000547
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000548 for (int i = 0; i < count; i++)
549 {
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000550 // Internally store matrices as transposed versions to accomodate HLSL matrix indexing
551 if (transpose == GL_FALSE)
552 {
553 transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols);
554 }
555 else
556 {
557 expandMatrix<GLfloat>(target, value, 4, rows, cols, rows);
558 }
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000559 target += 4 * rows;
560 value += cols * rows;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000561 }
562
563 return true;
564}
565
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000566bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000567{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000568 return setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000569}
570
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000571bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000572{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000573 return setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000574}
575
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000576bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000577{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000578 return setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000579}
580
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000581bool ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000582{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000583 return setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000584}
585
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000586bool ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000587{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000588 return setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000589}
590
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000591bool ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000592{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000593 return setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000594}
595
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000596bool ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000597{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000598 return setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000599}
600
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000601bool ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000602{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000603 return setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000604}
605
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000606bool ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000607{
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000608 return setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3);
shannon.woods%transgaming.com@gtempaccount.comf1306162013-04-13 03:40:04 +0000609}
610
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000611bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
612{
613 if (location < 0 || location >= (int)mUniformIndex.size())
614 {
615 return false;
616 }
617
618 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
619 targetUniform->dirty = true;
620
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000621 int elementCount = targetUniform->elementCount();
622
623 if (elementCount == 1 && count > 1)
624 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
625
626 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
627
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000628 if (targetUniform->type == GL_INT ||
629 targetUniform->type == GL_SAMPLER_2D ||
630 targetUniform->type == GL_SAMPLER_CUBE)
631 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000632 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000633
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000634 for (int i = 0; i < count; i++)
635 {
636 target[0] = v[0];
637 target[1] = 0;
638 target[2] = 0;
639 target[3] = 0;
640 target += 4;
641 v += 1;
642 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000643 }
644 else if (targetUniform->type == GL_BOOL)
645 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000646 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000647
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000648 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000649 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000650 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
651 boolParams[1] = GL_FALSE;
652 boolParams[2] = GL_FALSE;
653 boolParams[3] = GL_FALSE;
654 boolParams += 4;
655 v += 1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000656 }
657 }
658 else
659 {
660 return false;
661 }
662
663 return true;
664}
665
666bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
667{
668 if (location < 0 || location >= (int)mUniformIndex.size())
669 {
670 return false;
671 }
672
673 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
674 targetUniform->dirty = true;
675
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000676 int elementCount = targetUniform->elementCount();
677
678 if (elementCount == 1 && count > 1)
679 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
680
681 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
682
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000683 if (targetUniform->type == GL_INT_VEC2)
684 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000685 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000686
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000687 for (int i = 0; i < count; i++)
688 {
689 target[0] = v[0];
690 target[1] = v[1];
691 target[2] = 0;
692 target[3] = 0;
693 target += 4;
694 v += 2;
695 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000696 }
697 else if (targetUniform->type == GL_BOOL_VEC2)
698 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000699 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000700
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000701 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000702 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000703 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
704 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
705 boolParams[2] = GL_FALSE;
706 boolParams[3] = GL_FALSE;
707 boolParams += 4;
708 v += 2;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000709 }
710 }
711 else
712 {
713 return false;
714 }
715
716 return true;
717}
718
719bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
720{
721 if (location < 0 || location >= (int)mUniformIndex.size())
722 {
723 return false;
724 }
725
726 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
727 targetUniform->dirty = true;
728
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000729 int elementCount = targetUniform->elementCount();
730
731 if (elementCount == 1 && count > 1)
732 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
733
734 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
735
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000736 if (targetUniform->type == GL_INT_VEC3)
737 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000738 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000739
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000740 for (int i = 0; i < count; i++)
741 {
742 target[0] = v[0];
743 target[1] = v[1];
744 target[2] = v[2];
745 target[3] = 0;
746 target += 4;
747 v += 3;
748 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000749 }
750 else if (targetUniform->type == GL_BOOL_VEC3)
751 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000752 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000753
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000754 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000755 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000756 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
757 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
758 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE;
759 boolParams[3] = GL_FALSE;
760 boolParams += 4;
761 v += 3;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000762 }
763 }
764 else
765 {
766 return false;
767 }
768
769 return true;
770}
771
772bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
773{
774 if (location < 0 || location >= (int)mUniformIndex.size())
775 {
776 return false;
777 }
778
779 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
780 targetUniform->dirty = true;
781
shannon.woods@transgaming.com15de0f92013-02-28 23:10:31 +0000782 int elementCount = targetUniform->elementCount();
783
784 if (elementCount == 1 && count > 1)
785 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
786
787 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
788
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000789 if (targetUniform->type == GL_INT_VEC4)
790 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000791 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000792
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000793 for (int i = 0; i < count; i++)
794 {
795 target[0] = v[0];
796 target[1] = v[1];
797 target[2] = v[2];
798 target[3] = v[3];
799 target += 4;
800 v += 4;
801 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000802 }
803 else if (targetUniform->type == GL_BOOL_VEC4)
804 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000805 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000806
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000807 for (int i = 0; i < count; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000808 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000809 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE;
810 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE;
811 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE;
812 boolParams[3] = (v[3] == 0) ? GL_FALSE : GL_TRUE;
813 boolParams += 4;
814 v += 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000815 }
816 }
817 else
818 {
819 return false;
820 }
821
822 return true;
823}
824
825bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
826{
827 if (location < 0 || location >= (int)mUniformIndex.size())
828 {
829 return false;
830 }
831
832 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
833
834 // sized queries -- ensure the provided buffer is large enough
835 if (bufSize)
836 {
837 int requiredBytes = UniformExternalSize(targetUniform->type);
838 if (*bufSize < requiredBytes)
839 {
840 return false;
841 }
842 }
843
844 switch (targetUniform->type)
845 {
846 case GL_FLOAT_MAT2:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000847 transposeMatrix<GLfloat>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8, 2, 2, 4, 2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000848 break;
849 case GL_FLOAT_MAT3:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000850 transposeMatrix<GLfloat>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12, 3, 3, 4, 3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000851 break;
852 case GL_FLOAT_MAT4:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000853 transposeMatrix<GLfloat>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16, 4, 4, 4, 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000854 break;
855 default:
856 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000857 unsigned int size = UniformComponentCount(targetUniform->type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000858
859 switch (UniformComponentType(targetUniform->type))
860 {
861 case GL_BOOL:
862 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000863 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000864
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000865 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000866 {
867 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
868 }
869 }
870 break;
871 case GL_FLOAT:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000872 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLfloat),
873 size * sizeof(GLfloat));
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000874 break;
875 case GL_INT:
876 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000877 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000878
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000879 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000880 {
881 params[i] = (float)intParams[i];
882 }
883 }
884 break;
885 default: UNREACHABLE();
886 }
887 }
888 }
889
890 return true;
891}
892
893bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
894{
895 if (location < 0 || location >= (int)mUniformIndex.size())
896 {
897 return false;
898 }
899
900 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
901
902 // sized queries -- ensure the provided buffer is large enough
903 if (bufSize)
904 {
905 int requiredBytes = UniformExternalSize(targetUniform->type);
906 if (*bufSize < requiredBytes)
907 {
908 return false;
909 }
910 }
911
912 switch (targetUniform->type)
913 {
914 case GL_FLOAT_MAT2:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000915 transposeMatrix<GLint>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8, 2, 2, 4, 2);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000916 break;
917 case GL_FLOAT_MAT3:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000918 transposeMatrix<GLint>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12, 3, 3, 4, 3);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000919 break;
920 case GL_FLOAT_MAT4:
shannon.woods%transgaming.com@gtempaccount.comcc62fac2013-04-13 03:39:52 +0000921 transposeMatrix<GLint>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16, 4, 4, 4, 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000922 break;
923 default:
924 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000925 unsigned int size = VariableColumnCount(targetUniform->type);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000926
927 switch (UniformComponentType(targetUniform->type))
928 {
929 case GL_BOOL:
930 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000931 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000932
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000933 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000934 {
shannon.woods@transgaming.comcd714ef2013-02-28 23:09:50 +0000935 params[i] = boolParams[i];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000936 }
937 }
938 break;
939 case GL_FLOAT:
940 {
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000941 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000942
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000943 for (unsigned int i = 0; i < size; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000944 {
945 params[i] = (GLint)floatParams[i];
946 }
947 }
948 break;
949 case GL_INT:
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000950 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLint),
951 size * sizeof(GLint));
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000952 break;
953 default: UNREACHABLE();
954 }
955 }
956 }
957
958 return true;
959}
960
961void ProgramBinary::dirtyAllUniforms()
962{
963 unsigned int numUniforms = mUniforms.size();
964 for (unsigned int index = 0; index < numUniforms; index++)
965 {
966 mUniforms[index]->dirty = true;
967 }
968}
969
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000970// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000971void ProgramBinary::applyUniforms()
972{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000973 // Retrieve sampler uniform values
974 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
975 {
976 Uniform *targetUniform = *ub;
977
978 if (targetUniform->dirty)
979 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000980 if (targetUniform->type == GL_SAMPLER_2D ||
981 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000982 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000983 int count = targetUniform->elementCount();
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000984 GLint (*v)[4] = (GLint(*)[4])targetUniform->data;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000985
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000986 if (targetUniform->psRegisterIndex >= 0)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000987 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +0000988 unsigned int firstIndex = targetUniform->psRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000989
990 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000991 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000992 unsigned int samplerIndex = firstIndex + i;
993
994 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000995 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000996 ASSERT(mSamplersPS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +0000997 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000998 }
999 }
1000 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001001
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001002 if (targetUniform->vsRegisterIndex >= 0)
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001003 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001004 unsigned int firstIndex = targetUniform->vsRegisterIndex;
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001005
1006 for (int i = 0; i < count; i++)
1007 {
1008 unsigned int samplerIndex = firstIndex + i;
1009
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001010 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001011 {
1012 ASSERT(mSamplersVS[samplerIndex].active);
shannon.woods@transgaming.com2494c972013-02-28 23:10:03 +00001013 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0];
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001014 }
1015 }
1016 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001017 }
1018 }
1019 }
1020
shannon.woods@transgaming.com358e88d2013-01-25 21:53:11 +00001021 mRenderer->applyUniforms(this, &mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001022}
1023
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001024// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1025// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001026int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001027{
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001028 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001029
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001030 fragmentShader->resetVaryingsRegisterAssignment();
1031
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001032 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1033 {
1034 int n = VariableRowCount(varying->type) * varying->size;
1035 int m = VariableColumnCount(varying->type);
1036 bool success = false;
1037
1038 if (m == 2 || m == 3 || m == 4)
1039 {
1040 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1041 {
1042 bool available = true;
1043
1044 for (int y = 0; y < n && available; y++)
1045 {
1046 for (int x = 0; x < m && available; x++)
1047 {
1048 if (packing[r + y][x])
1049 {
1050 available = false;
1051 }
1052 }
1053 }
1054
1055 if (available)
1056 {
1057 varying->reg = r;
1058 varying->col = 0;
1059
1060 for (int y = 0; y < n; y++)
1061 {
1062 for (int x = 0; x < m; x++)
1063 {
1064 packing[r + y][x] = &*varying;
1065 }
1066 }
1067
1068 success = true;
1069 }
1070 }
1071
1072 if (!success && m == 2)
1073 {
1074 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1075 {
1076 bool available = true;
1077
1078 for (int y = 0; y < n && available; y++)
1079 {
1080 for (int x = 2; x < 4 && available; x++)
1081 {
1082 if (packing[r + y][x])
1083 {
1084 available = false;
1085 }
1086 }
1087 }
1088
1089 if (available)
1090 {
1091 varying->reg = r;
1092 varying->col = 2;
1093
1094 for (int y = 0; y < n; y++)
1095 {
1096 for (int x = 2; x < 4; x++)
1097 {
1098 packing[r + y][x] = &*varying;
1099 }
1100 }
1101
1102 success = true;
1103 }
1104 }
1105 }
1106 }
1107 else if (m == 1)
1108 {
1109 int space[4] = {0};
1110
1111 for (int y = 0; y < maxVaryingVectors; y++)
1112 {
1113 for (int x = 0; x < 4; x++)
1114 {
1115 space[x] += packing[y][x] ? 0 : 1;
1116 }
1117 }
1118
1119 int column = 0;
1120
1121 for (int x = 0; x < 4; x++)
1122 {
1123 if (space[x] >= n && space[x] < space[column])
1124 {
1125 column = x;
1126 }
1127 }
1128
1129 if (space[column] >= n)
1130 {
1131 for (int r = 0; r < maxVaryingVectors; r++)
1132 {
1133 if (!packing[r][column])
1134 {
1135 varying->reg = r;
1136
1137 for (int y = r; y < r + n; y++)
1138 {
1139 packing[y][column] = &*varying;
1140 }
1141
1142 break;
1143 }
1144 }
1145
1146 varying->col = column;
1147
1148 success = true;
1149 }
1150 }
1151 else UNREACHABLE();
1152
1153 if (!success)
1154 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001155 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001156
1157 return -1;
1158 }
1159 }
1160
1161 // Return the number of used registers
1162 int registers = 0;
1163
1164 for (int r = 0; r < maxVaryingVectors; r++)
1165 {
1166 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1167 {
1168 registers++;
1169 }
1170 }
1171
1172 return registers;
1173}
1174
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001175bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
1176 std::string& pixelHLSL, std::string& vertexHLSL,
1177 FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001178{
1179 if (pixelHLSL.empty() || vertexHLSL.empty())
1180 {
1181 return false;
1182 }
1183
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001184 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
1185 bool usesFragColor = fragmentShader->mUsesFragColor;
1186 bool usesFragData = fragmentShader->mUsesFragData;
1187 if (usesMRT && usesFragColor && usesFragData)
1188 {
1189 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
1190 return false;
1191 }
1192
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001193 // Write the HLSL input/output declarations
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001194 const int shaderModel = mRenderer->getMajorShaderModel();
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00001195 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001196
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001197 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
1198
1199 if (registersNeeded > maxVaryingVectors)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001200 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001201 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001202
1203 return false;
1204 }
1205
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001206 vertexShader->resetVaryingsRegisterAssignment();
1207
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001208 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1209 {
1210 bool matched = false;
1211
1212 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1213 {
1214 if (output->name == input->name)
1215 {
1216 if (output->type != input->type || output->size != input->size)
1217 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001218 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 +00001219
1220 return false;
1221 }
1222
1223 output->reg = input->reg;
1224 output->col = input->col;
1225
1226 matched = true;
1227 break;
1228 }
1229 }
1230
1231 if (!matched)
1232 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001233 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001234
1235 return false;
1236 }
1237 }
1238
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001239 mUsesPointSize = vertexShader->mUsesPointSize;
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001240 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001241 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001242 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
1243
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001244 const unsigned int renderTargetCount = usesMRT ? mRenderer->getMaxRenderTargets() : 1;
1245
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001246 // special varyings that use reserved registers
1247 int reservedRegisterIndex = registers;
1248 std::string fragCoordSemantic;
1249 std::string pointCoordSemantic;
1250
1251 if (fragmentShader->mUsesFragCoord)
1252 {
1253 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1254 }
1255
1256 if (fragmentShader->mUsesPointCoord)
1257 {
1258 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
1259 // In DX11 we compute this in the GS.
1260 if (shaderModel == 3)
1261 {
1262 pointCoordSemantic = "TEXCOORD0";
1263 }
1264 else if (shaderModel >= 4)
1265 {
1266 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
1267 }
1268 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001269
1270 vertexHLSL += "struct VS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001271 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001272
1273 int semanticIndex = 0;
1274 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1275 {
1276 switch (attribute->type)
1277 {
1278 case GL_FLOAT: vertexHLSL += " float "; break;
1279 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1280 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1281 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1282 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1283 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1284 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1285 default: UNREACHABLE();
1286 }
1287
1288 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1289
1290 semanticIndex += VariableRowCount(attribute->type);
1291 }
1292
1293 vertexHLSL += "};\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001294 "\n"
1295 "struct VS_OUTPUT\n"
1296 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001297
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001298 if (shaderModel < 4)
1299 {
1300 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1301 }
1302
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001303 for (int r = 0; r < registers; r++)
1304 {
1305 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1306
1307 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1308 }
1309
1310 if (fragmentShader->mUsesFragCoord)
1311 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001312 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001313 }
1314
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001315 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001316 {
1317 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1318 }
1319
shannon.woods%transgaming.com@gtempaccount.comee8d3c82013-04-13 03:27:26 +00001320 if (shaderModel >= 4)
1321 {
1322 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
1323 }
1324
1325 vertexHLSL += "};\n"
daniel@transgaming.com9c4a6252013-01-11 04:07:18 +00001326 "\n"
1327 "VS_OUTPUT main(VS_INPUT input)\n"
1328 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001329
1330 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1331 {
1332 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1333
1334 if (VariableRowCount(attribute->type) > 1) // Matrix
1335 {
1336 vertexHLSL += "transpose";
1337 }
1338
1339 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1340 }
1341
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001342 if (shaderModel >= 4)
1343 {
1344 vertexHLSL += "\n"
1345 " gl_main();\n"
1346 "\n"
1347 " VS_OUTPUT output;\n"
1348 " output.gl_Position.x = gl_Position.x;\n"
1349 " output.gl_Position.y = -gl_Position.y;\n"
1350 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1351 " output.gl_Position.w = gl_Position.w;\n";
1352 }
1353 else
1354 {
1355 vertexHLSL += "\n"
1356 " gl_main();\n"
1357 "\n"
1358 " VS_OUTPUT output;\n"
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +00001359 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
1360 " 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 +00001361 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1362 " output.gl_Position.w = gl_Position.w;\n";
1363 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001364
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001365 if (vertexShader->mUsesPointSize && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001366 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001367 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001368 }
1369
1370 if (fragmentShader->mUsesFragCoord)
1371 {
1372 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1373 }
1374
1375 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1376 {
1377 if (varying->reg >= 0)
1378 {
1379 for (int i = 0; i < varying->size; i++)
1380 {
1381 int rows = VariableRowCount(varying->type);
1382
1383 for (int j = 0; j < rows; j++)
1384 {
1385 int r = varying->reg + i * rows + j;
1386 vertexHLSL += " output.v" + str(r);
1387
1388 bool sharedRegister = false; // Register used by multiple varyings
1389
1390 for (int x = 0; x < 4; x++)
1391 {
1392 if (packing[r][x] && packing[r][x] != packing[r][0])
1393 {
1394 sharedRegister = true;
1395 break;
1396 }
1397 }
1398
1399 if(sharedRegister)
1400 {
1401 vertexHLSL += ".";
1402
1403 for (int x = 0; x < 4; x++)
1404 {
1405 if (packing[r][x] == &*varying)
1406 {
1407 switch(x)
1408 {
1409 case 0: vertexHLSL += "x"; break;
1410 case 1: vertexHLSL += "y"; break;
1411 case 2: vertexHLSL += "z"; break;
1412 case 3: vertexHLSL += "w"; break;
1413 }
1414 }
1415 }
1416 }
1417
1418 vertexHLSL += " = " + varying->name;
1419
1420 if (varying->array)
1421 {
1422 vertexHLSL += "[" + str(i) + "]";
1423 }
1424
1425 if (rows > 1)
1426 {
1427 vertexHLSL += "[" + str(j) + "]";
1428 }
1429
1430 vertexHLSL += ";\n";
1431 }
1432 }
1433 }
1434 }
1435
1436 vertexHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001437 " return output;\n"
1438 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001439
1440 pixelHLSL += "struct PS_INPUT\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001441 "{\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001442
1443 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1444 {
1445 if (varying->reg >= 0)
1446 {
1447 for (int i = 0; i < varying->size; i++)
1448 {
1449 int rows = VariableRowCount(varying->type);
1450 for (int j = 0; j < rows; j++)
1451 {
1452 std::string n = str(varying->reg + i * rows + j);
daniel@transgaming.com00c0d152013-01-11 04:07:23 +00001453 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001454 }
1455 }
1456 }
1457 else UNREACHABLE();
1458 }
1459
1460 if (fragmentShader->mUsesFragCoord)
1461 {
shannon.woods@transgaming.come0e89872013-01-25 21:55:40 +00001462 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001463 }
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001464
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001465 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
1466 {
1467 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
1468 }
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00001469
shannon.woods@transgaming.com0693fc32013-02-28 23:18:03 +00001470 // Must consume the PSIZE element if the geometry shader is not active
1471 // We won't know if we use a GS until we draw
1472 if (vertexShader->mUsesPointSize && shaderModel >= 4)
1473 {
1474 pixelHLSL += " float gl_PointSize : PSIZE;\n";
1475 }
1476
1477 if (fragmentShader->mUsesFragCoord)
1478 {
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001479 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001480 {
1481 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
1482 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001483 else if (shaderModel >= 3)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001484 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001485 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1486 }
1487 }
1488
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001489 pixelHLSL += "};\n"
1490 "\n"
1491 "struct PS_OUTPUT\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001492 "{\n";
1493
1494 for (unsigned int i = 0; i < renderTargetCount; i++)
1495 {
1496 pixelHLSL += " float4 gl_Color" + str(i) + " : " + targetSemantic + str(i) + ";\n";
1497 }
1498
1499 pixelHLSL += "};\n"
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001500 "\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001501
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001502 if (fragmentShader->mUsesFrontFacing)
1503 {
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001504 if (shaderModel >= 4)
1505 {
1506 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
1507 "{\n";
1508 }
1509 else
1510 {
1511 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
1512 "{\n";
1513 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001514 }
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001515 else
1516 {
1517 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
1518 "{\n";
1519 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001520
1521 if (fragmentShader->mUsesFragCoord)
1522 {
1523 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1524
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001525 if (shaderModel >= 4)
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001526 {
1527 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
1528 " gl_FragCoord.y = input.dx_VPos.y;\n";
1529 }
daniel@transgaming.comb37cd2d2013-01-11 04:10:31 +00001530 else if (shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001531 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001532 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001533 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001534 }
1535 else
1536 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00001537 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
1538 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1539 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001540 }
1541
daniel@transgaming.com12985182012-12-20 20:56:31 +00001542 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
daniel@transgaming.com74471e02013-01-11 04:10:26 +00001543 " gl_FragCoord.w = rhw;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001544 }
1545
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001546 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001547 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001548 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1549 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001550 }
1551
1552 if (fragmentShader->mUsesFrontFacing)
1553 {
shannon.woods@transgaming.com41ba5e02013-01-25 21:51:20 +00001554 if (shaderModel <= 3)
1555 {
1556 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1557 }
1558 else
1559 {
1560 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1561 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001562 }
1563
1564 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1565 {
1566 if (varying->reg >= 0)
1567 {
1568 for (int i = 0; i < varying->size; i++)
1569 {
1570 int rows = VariableRowCount(varying->type);
1571 for (int j = 0; j < rows; j++)
1572 {
1573 std::string n = str(varying->reg + i * rows + j);
1574 pixelHLSL += " " + varying->name;
1575
1576 if (varying->array)
1577 {
1578 pixelHLSL += "[" + str(i) + "]";
1579 }
1580
1581 if (rows > 1)
1582 {
1583 pixelHLSL += "[" + str(j) + "]";
1584 }
1585
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001586 switch (VariableColumnCount(varying->type))
1587 {
1588 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1589 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1590 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1591 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1592 default: UNREACHABLE();
1593 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001594 }
1595 }
1596 }
1597 else UNREACHABLE();
1598 }
1599
1600 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001601 " gl_main();\n"
1602 "\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001603 " PS_OUTPUT output;\n";
1604
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +00001605 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1606 // - with a 3.0 context, the output color is copied to channel 0
1607 // - with a 2.0 context using EXT_draw_buffers, the output color is broadcast to all channels
1608 const bool broadcast = fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3;
1609
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001610 for (unsigned int i = 0; i < renderTargetCount; i++)
1611 {
shannon.woods%transgaming.com@gtempaccount.com0b05b7a2013-04-13 03:34:58 +00001612 unsigned int sourceColor = !broadcast ? i : 0;
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +00001613
1614 pixelHLSL += " output.gl_Color" + str(i) + " = gl_Color[" + str(sourceColor) + "];\n";
1615 }
1616
1617 pixelHLSL += "\n"
shannon.woods@transgaming.com5f77c552013-01-25 21:51:44 +00001618 " return output;\n"
1619 "}\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001620
1621 return true;
1622}
1623
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001624bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1625{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001626 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001627
1628 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001629 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630 if (format != GL_PROGRAM_BINARY_ANGLE)
1631 {
1632 infoLog.append("Invalid program binary format.");
1633 return false;
1634 }
1635
1636 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001637 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001638 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001639 {
1640 infoLog.append("Invalid program binary version.");
1641 return false;
1642 }
1643
1644 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1645 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001646 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001647 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001648 stream.read(&name);
1649 mLinkedAttribute[i].name = name;
1650 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001651 }
1652
1653 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1654 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001655 stream.read(&mSamplersPS[i].active);
1656 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001657
1658 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001659 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001660 mSamplersPS[i].textureType = (TextureType) textureType;
1661 }
1662
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001663 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001664 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001665 stream.read(&mSamplersVS[i].active);
1666 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001667
1668 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001669 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001670 mSamplersVS[i].textureType = (TextureType) textureType;
1671 }
1672
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001673 stream.read(&mUsedVertexSamplerRange);
1674 stream.read(&mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001675 stream.read(&mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001676
shannon.woods@transgaming.com45886d62013-02-28 23:19:20 +00001677 size_t size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001678 stream.read(&size);
1679 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001680 {
1681 infoLog.append("Invalid program binary.");
1682 return false;
1683 }
1684
1685 mUniforms.resize(size);
1686 for (unsigned int i = 0; i < size; ++i)
1687 {
1688 GLenum type;
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001689 GLenum precision;
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001690 std::string name;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001691 unsigned int arraySize;
1692
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001693 stream.read(&type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001694 stream.read(&precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001695 stream.read(&name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001696 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001697
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001698 mUniforms[i] = new Uniform(type, precision, name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001700 stream.read(&mUniforms[i]->psRegisterIndex);
1701 stream.read(&mUniforms[i]->vsRegisterIndex);
1702 stream.read(&mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001703 }
1704
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001705 stream.read(&size);
1706 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001707 {
1708 infoLog.append("Invalid program binary.");
1709 return false;
1710 }
1711
1712 mUniformIndex.resize(size);
1713 for (unsigned int i = 0; i < size; ++i)
1714 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001715 stream.read(&mUniformIndex[i].name);
1716 stream.read(&mUniformIndex[i].element);
1717 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001718 }
1719
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001720 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001721 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001722
1723 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001724 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001725
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001726 unsigned int geometryShaderSize;
1727 stream.read(&geometryShaderSize);
1728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001730
daniel@transgaming.com36038542012-11-28 20:59:26 +00001731 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001732 ptr += sizeof(GUID);
1733
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001734 GUID identifier = mRenderer->getAdapterIdentifier();
1735 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001736 {
1737 infoLog.append("Invalid program binary.");
1738 return false;
1739 }
1740
1741 const char *pixelShaderFunction = ptr;
1742 ptr += pixelShaderSize;
1743
1744 const char *vertexShaderFunction = ptr;
1745 ptr += vertexShaderSize;
1746
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001747 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL;
1748 ptr += geometryShaderSize;
1749
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001750 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001751 pixelShaderSize, rx::SHADER_PIXEL);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001752 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001753 {
1754 infoLog.append("Could not create pixel shader.");
1755 return false;
1756 }
1757
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001758 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001759 vertexShaderSize, rx::SHADER_VERTEX);
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001760 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001761 {
1762 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001763 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001764 mPixelExecutable = NULL;
1765 return false;
1766 }
1767
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001768 if (geometryShaderFunction != NULL && geometryShaderSize > 0)
1769 {
1770 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction),
1771 geometryShaderSize, rx::SHADER_GEOMETRY);
1772 if (!mGeometryExecutable)
1773 {
1774 infoLog.append("Could not create geometry shader.");
1775 delete mPixelExecutable;
1776 mPixelExecutable = NULL;
1777 delete mVertexExecutable;
1778 mVertexExecutable = NULL;
1779 return false;
1780 }
1781 }
1782 else
1783 {
1784 mGeometryExecutable = NULL;
1785 }
1786
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001787 return true;
1788}
1789
1790bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1791{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001792 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001793
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001794 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001795 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001796
1797 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1798 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001799 stream.write(mLinkedAttribute[i].type);
1800 stream.write(mLinkedAttribute[i].name);
1801 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001802 }
1803
1804 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1805 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001806 stream.write(mSamplersPS[i].active);
1807 stream.write(mSamplersPS[i].logicalTextureUnit);
1808 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001809 }
1810
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00001811 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001812 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001813 stream.write(mSamplersVS[i].active);
1814 stream.write(mSamplersVS[i].logicalTextureUnit);
1815 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001816 }
1817
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001818 stream.write(mUsedVertexSamplerRange);
1819 stream.write(mUsedPixelSamplerRange);
daniel@transgaming.com9aa6fe12012-12-20 21:13:39 +00001820 stream.write(mUsesPointSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001821
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001822 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001823 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1824 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001825 stream.write(mUniforms[i]->type);
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00001826 stream.write(mUniforms[i]->precision);
daniel@transgaming.comdb019952012-12-20 21:13:32 +00001827 stream.write(mUniforms[i]->name);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001828 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001829
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00001830 stream.write(mUniforms[i]->psRegisterIndex);
1831 stream.write(mUniforms[i]->vsRegisterIndex);
1832 stream.write(mUniforms[i]->registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001833 }
1834
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001835 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001836 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1837 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001838 stream.write(mUniformIndex[i].name);
1839 stream.write(mUniformIndex[i].element);
1840 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001841 }
1842
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001843 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001844 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001845
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001846 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001847 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001848
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001849 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0;
1850 stream.write(geometryShaderSize);
1851
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001852 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001853
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001854 GLsizei streamLength = stream.length();
1855 const void *streamData = stream.data();
1856
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001857 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001858 if (totalLength > bufSize)
1859 {
1860 if (length)
1861 {
1862 *length = 0;
1863 }
1864
1865 return false;
1866 }
1867
1868 if (binary)
1869 {
1870 char *ptr = (char*) binary;
1871
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001872 memcpy(ptr, streamData, streamLength);
1873 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001874
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001875 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001876 ptr += sizeof(GUID);
1877
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001878 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001879 ptr += pixelShaderSize;
1880
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001881 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001882 ptr += vertexShaderSize;
1883
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001884 if (mGeometryExecutable != NULL && geometryShaderSize > 0)
1885 {
1886 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize);
1887 ptr += geometryShaderSize;
1888 }
1889
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001890 ASSERT(ptr - totalLength == binary);
1891 }
1892
1893 if (length)
1894 {
1895 *length = totalLength;
1896 }
1897
1898 return true;
1899}
1900
1901GLint ProgramBinary::getLength()
1902{
1903 GLint length;
1904 if (save(NULL, INT_MAX, &length))
1905 {
1906 return length;
1907 }
1908 else
1909 {
1910 return 0;
1911 }
1912}
1913
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001914bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001915{
1916 if (!fragmentShader || !fragmentShader->isCompiled())
1917 {
1918 return false;
1919 }
1920
1921 if (!vertexShader || !vertexShader->isCompiled())
1922 {
1923 return false;
1924 }
1925
1926 std::string pixelHLSL = fragmentShader->getHLSL();
1927 std::string vertexHLSL = vertexShader->getHLSL();
1928
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +00001929 // Map the varyings to the register file
1930 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
1931 int registers = packVaryings(infoLog, packing, fragmentShader);
1932
1933 if (registers < 0)
1934 {
1935 return false;
1936 }
1937
1938 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001939 {
1940 return false;
1941 }
1942
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001943 bool success = true;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00001944 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX);
1945 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001946
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001947 if (usesGeometryShader())
1948 {
1949 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
1950 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY);
1951 }
1952
1953 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001954 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001955 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001956 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001957
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001958 delete mVertexExecutable;
1959 mVertexExecutable = NULL;
1960 delete mPixelExecutable;
1961 mPixelExecutable = NULL;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00001962 delete mGeometryExecutable;
1963 mGeometryExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001964 }
1965
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001966 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1967 {
1968 success = false;
1969 }
1970
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001971 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms()))
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001972 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00001973 success = false;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001974 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001975
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001976 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001977}
1978
1979// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001980bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001981{
1982 unsigned int usedLocations = 0;
1983
1984 // Link attributes that have a binding location
1985 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1986 {
1987 int location = attributeBindings.getAttributeBinding(attribute->name);
1988
1989 if (location != -1) // Set by glBindAttribLocation
1990 {
1991 if (!mLinkedAttribute[location].name.empty())
1992 {
1993 // Multiple active attributes bound to the same location; not an error
1994 }
1995
1996 mLinkedAttribute[location] = *attribute;
1997
1998 int rows = VariableRowCount(attribute->type);
1999
2000 if (rows + location > MAX_VERTEX_ATTRIBS)
2001 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002002 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 +00002003
2004 return false;
2005 }
2006
2007 for (int i = 0; i < rows; i++)
2008 {
2009 usedLocations |= 1 << (location + i);
2010 }
2011 }
2012 }
2013
2014 // Link attributes that don't have a binding location
2015 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
2016 {
2017 int location = attributeBindings.getAttributeBinding(attribute->name);
2018
2019 if (location == -1) // Not set by glBindAttribLocation
2020 {
2021 int rows = VariableRowCount(attribute->type);
2022 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
2023
2024 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
2025 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002026 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002027
2028 return false; // Fail to link
2029 }
2030
2031 mLinkedAttribute[availableIndex] = *attribute;
2032 }
2033 }
2034
2035 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
2036 {
2037 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
2038 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
2039
2040 for (int r = 0; r < rows; r++)
2041 {
2042 mSemanticIndex[attributeIndex++] = index++;
2043 }
2044 }
2045
2046 return true;
2047}
2048
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002049bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002050{
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002051 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002052 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002053 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002054 {
2055 return false;
2056 }
2057 }
2058
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002059 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++)
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002060 {
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002061 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00002062 {
2063 return false;
2064 }
2065 }
daniel@transgaming.com68aaf932012-12-20 21:13:16 +00002066
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002067 return true;
2068}
2069
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002070bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002071{
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002072 if (constant.type == GL_SAMPLER_2D ||
2073 constant.type == GL_SAMPLER_CUBE)
2074 {
2075 unsigned int samplerIndex = constant.registerIndex;
2076
2077 do
2078 {
2079 if (shader == GL_VERTEX_SHADER)
2080 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002081 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits())
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002082 {
2083 mSamplersVS[samplerIndex].active = true;
2084 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2085 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2086 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2087 }
2088 else
2089 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002090 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits());
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002091 return false;
2092 }
2093 }
2094 else if (shader == GL_FRAGMENT_SHADER)
2095 {
2096 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2097 {
2098 mSamplersPS[samplerIndex].active = true;
2099 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2100 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2101 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2102 }
2103 else
2104 {
2105 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
2106 return false;
2107 }
2108 }
2109 else UNREACHABLE();
2110
2111 samplerIndex++;
2112 }
2113 while (samplerIndex < constant.registerIndex + constant.arraySize);
2114 }
2115
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002116 Uniform *uniform = NULL;
2117 GLint location = getUniformLocation(constant.name);
2118
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00002119 if (location >= 0) // Previously defined, type and precision must match
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002120 {
2121 uniform = mUniforms[mUniformIndex[location].index];
2122
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00002123 if (uniform->type != constant.type)
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002124 {
shannon.woods@transgaming.coma09c70f2013-02-28 23:18:56 +00002125 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str());
2126 return false;
2127 }
2128
2129 if (uniform->precision != constant.precision)
2130 {
2131 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 +00002132 return false;
2133 }
2134 }
2135 else
2136 {
shannon.woods@transgaming.comd5a91b92013-02-28 23:17:30 +00002137 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize);
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002138 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002139
2140 if (!uniform)
2141 {
2142 return false;
2143 }
2144
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002145 if (shader == GL_FRAGMENT_SHADER)
2146 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002147 uniform->psRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002148 }
2149 else if (shader == GL_VERTEX_SHADER)
2150 {
daniel@transgaming.come76b64b2013-01-11 04:10:08 +00002151 uniform->vsRegisterIndex = constant.registerIndex;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002152 }
2153 else UNREACHABLE();
2154
2155 if (location >= 0)
2156 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002157 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002158 }
2159
2160 mUniforms.push_back(uniform);
2161 unsigned int uniformIndex = mUniforms.size() - 1;
2162
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002163 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002164 {
2165 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
2166 }
2167
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002168 if (shader == GL_VERTEX_SHADER)
2169 {
2170 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors())
2171 {
2172 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors());
2173 return false;
2174 }
2175 }
2176 else if (shader == GL_FRAGMENT_SHADER)
2177 {
2178 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors())
2179 {
2180 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors());
2181 return false;
2182 }
2183 }
2184 else UNREACHABLE();
2185
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002186 return true;
2187}
2188
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002189std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2190{
2191 // for now we only handle point sprite emulation
2192 ASSERT(usesPointSpriteEmulation());
2193 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
2194}
2195
2196std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
2197{
2198 ASSERT(registers >= 0);
2199 ASSERT(vertexShader->mUsesPointSize);
2200 ASSERT(mRenderer->getMajorShaderModel() >= 4);
2201
2202 std::string geomHLSL;
2203
2204 std::string varyingSemantic = "TEXCOORD";
2205
2206 std::string fragCoordSemantic;
2207 std::string pointCoordSemantic;
2208
2209 int reservedRegisterIndex = registers;
2210
2211 if (fragmentShader->mUsesFragCoord)
2212 {
2213 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2214 }
2215
2216 if (fragmentShader->mUsesPointCoord)
2217 {
2218 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++);
2219 }
2220
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +00002221 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
2222 "\n"
2223 "struct GS_INPUT\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002224 "{\n";
2225
2226 for (int r = 0; r < registers; r++)
2227 {
2228 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2229
2230 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2231 }
2232
2233 if (fragmentShader->mUsesFragCoord)
2234 {
2235 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2236 }
2237
2238 geomHLSL += " float gl_PointSize : PSIZE;\n"
2239 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002240 "};\n"
2241 "\n"
2242 "struct GS_OUTPUT\n"
2243 "{\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002244
2245 for (int r = 0; r < registers; r++)
2246 {
2247 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
2248
2249 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
2250 }
2251
2252 if (fragmentShader->mUsesFragCoord)
2253 {
2254 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
2255 }
2256
2257 if (fragmentShader->mUsesPointCoord)
2258 {
2259 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
2260 }
2261
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002262 geomHLSL += " float gl_PointSize : PSIZE;\n"
2263 " float4 gl_Position : SV_Position;\n"
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002264 "};\n"
2265 "\n"
2266 "static float2 pointSpriteCorners[] = \n"
2267 "{\n"
2268 " float2( 0.5f, -0.5f),\n"
2269 " float2( 0.5f, 0.5f),\n"
2270 " float2(-0.5f, -0.5f),\n"
2271 " float2(-0.5f, 0.5f)\n"
2272 "};\n"
2273 "\n"
2274 "static float2 pointSpriteTexcoords[] = \n"
2275 "{\n"
2276 " float2(1.0f, 1.0f),\n"
2277 " float2(1.0f, 0.0f),\n"
2278 " float2(0.0f, 1.0f),\n"
2279 " float2(0.0f, 0.0f)\n"
2280 "};\n"
2281 "\n"
2282 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
2283 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n"
2284 "\n"
2285 "[maxvertexcount(4)]\n"
2286 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
2287 "{\n"
shannon.woods@transgaming.com276337c2013-02-28 23:15:16 +00002288 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
2289 " output.gl_PointSize = input[0].gl_PointSize;\n";
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002290
2291 for (int r = 0; r < registers; r++)
2292 {
2293 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n";
2294 }
2295
2296 if (fragmentShader->mUsesFragCoord)
2297 {
2298 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
2299 }
2300
2301 geomHLSL += " \n"
2302 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
2303 " float4 gl_Position = input[0].gl_Position;\n"
shannon.woods@transgaming.com771ca2a2013-02-28 23:14:52 +00002304 " 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 +00002305
2306 for (int corner = 0; corner < 4; corner++)
2307 {
2308 geomHLSL += " \n"
2309 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
2310
2311 if (fragmentShader->mUsesPointCoord)
2312 {
2313 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n";
2314 }
2315
2316 geomHLSL += " outStream.Append(output);\n";
2317 }
2318
2319 geomHLSL += " \n"
2320 " outStream.RestartStrip();\n"
2321 "}\n";
2322
2323 return geomHLSL;
2324}
2325
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002326// This method needs to match OutputHLSL::decorate
2327std::string ProgramBinary::decorateAttribute(const std::string &name)
2328{
2329 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2330 {
2331 return "_" + name;
2332 }
2333
2334 return name;
2335}
2336
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002337bool ProgramBinary::isValidated() const
2338{
2339 return mValidated;
2340}
2341
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002342void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002343{
2344 // Skip over inactive attributes
2345 unsigned int activeAttribute = 0;
2346 unsigned int attribute;
2347 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2348 {
2349 if (mLinkedAttribute[attribute].name.empty())
2350 {
2351 continue;
2352 }
2353
2354 if (activeAttribute == index)
2355 {
2356 break;
2357 }
2358
2359 activeAttribute++;
2360 }
2361
2362 if (bufsize > 0)
2363 {
2364 const char *string = mLinkedAttribute[attribute].name.c_str();
2365
2366 strncpy(name, string, bufsize);
2367 name[bufsize - 1] = '\0';
2368
2369 if (length)
2370 {
2371 *length = strlen(name);
2372 }
2373 }
2374
2375 *size = 1; // Always a single 'type' instance
2376
2377 *type = mLinkedAttribute[attribute].type;
2378}
2379
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002380GLint ProgramBinary::getActiveAttributeCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002381{
2382 int count = 0;
2383
2384 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2385 {
2386 if (!mLinkedAttribute[attributeIndex].name.empty())
2387 {
2388 count++;
2389 }
2390 }
2391
2392 return count;
2393}
2394
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002395GLint ProgramBinary::getActiveAttributeMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002396{
2397 int maxLength = 0;
2398
2399 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2400 {
2401 if (!mLinkedAttribute[attributeIndex].name.empty())
2402 {
2403 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2404 }
2405 }
2406
2407 return maxLength;
2408}
2409
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002410void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002411{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002412 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002413
2414 if (bufsize > 0)
2415 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002416 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002417
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002418 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002419 {
2420 string += "[0]";
2421 }
2422
2423 strncpy(name, string.c_str(), bufsize);
2424 name[bufsize - 1] = '\0';
2425
2426 if (length)
2427 {
2428 *length = strlen(name);
2429 }
2430 }
2431
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002432 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002433
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002434 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002435}
2436
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002437GLint ProgramBinary::getActiveUniformCount() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002438{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002439 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002440}
2441
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +00002442GLint ProgramBinary::getActiveUniformMaxLength() const
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002443{
2444 int maxLength = 0;
2445
2446 unsigned int numUniforms = mUniforms.size();
2447 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2448 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002449 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002450 {
2451 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2452 if (mUniforms[uniformIndex]->isArray())
2453 {
2454 length += 3; // Counting in "[0]".
2455 }
2456 maxLength = std::max(length, maxLength);
2457 }
2458 }
2459
2460 return maxLength;
2461}
2462
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002463void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002464{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002465 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002466 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002467 {
2468 mValidated = false;
2469 }
2470 else
2471 {
2472 mValidated = true;
2473 }
2474}
2475
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002476bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002477{
2478 // if any two active samplers in a program are of different types, but refer to the same
2479 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2480 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2481
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002482 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits();
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002483 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002484
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002485 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002486 {
2487 textureUnitType[i] = TEXTURE_UNKNOWN;
2488 }
2489
2490 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2491 {
2492 if (mSamplersPS[i].active)
2493 {
2494 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2495
2496 if (unit >= maxCombinedTextureImageUnits)
2497 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002498 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002499 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002500 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002501 }
2502
2503 return false;
2504 }
2505
2506 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2507 {
2508 if (mSamplersPS[i].textureType != textureUnitType[unit])
2509 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002510 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002511 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002512 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002513 }
2514
2515 return false;
2516 }
2517 }
2518 else
2519 {
2520 textureUnitType[unit] = mSamplersPS[i].textureType;
2521 }
2522 }
2523 }
2524
2525 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2526 {
2527 if (mSamplersVS[i].active)
2528 {
2529 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2530
2531 if (unit >= maxCombinedTextureImageUnits)
2532 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002533 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002534 {
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002535 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002536 }
2537
2538 return false;
2539 }
2540
2541 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2542 {
2543 if (mSamplersVS[i].textureType != textureUnitType[unit])
2544 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002545 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002546 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002547 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002548 }
2549
2550 return false;
2551 }
2552 }
2553 else
2554 {
2555 textureUnitType[unit] = mSamplersVS[i].textureType;
2556 }
2557 }
2558 }
2559
2560 return true;
2561}
2562
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002563ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2564{
2565}
2566
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +00002567struct AttributeSorter
2568{
2569 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
2570 : originalIndices(semanticIndices)
2571 {
2572 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2573 {
2574 indices[i] = i;
2575 }
2576
2577 std::sort(&indices[0], &indices[MAX_VERTEX_ATTRIBS], *this);
2578 }
2579
2580 bool operator()(int a, int b)
2581 {
2582 return originalIndices[a] == -1 ? false : originalIndices[a] < originalIndices[b];
2583 }
2584
2585 int indices[MAX_VERTEX_ATTRIBS];
2586 const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
2587};
2588
2589void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
2590{
2591 AttributeSorter sorter(mSemanticIndex);
2592
2593 int oldIndices[MAX_VERTEX_ATTRIBS];
2594 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
2595
2596 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2597 {
2598 oldIndices[i] = mSemanticIndex[i];
2599 oldTranslatedAttributes[i] = attributes[i];
2600 }
2601
2602 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2603 {
2604 int oldIndex = sorter.indices[i];
2605 sortedSemanticIndices[i] = oldIndices[oldIndex];
2606 attributes[i] = oldTranslatedAttributes[oldIndex];
2607 }
2608}
2609
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002610}