blob: 446cbb741716290c52e2d3b16c15b6b362a93684 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
2// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +000010#include "libGLESv2/BinaryStream.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000011#include "libGLESv2/ProgramBinary.h"
12
13#include "common/debug.h"
apatrick@chromium.org90080e32012-07-09 22:15:33 +000014#include "common/version.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/Shader.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000018
19#include <string>
20
daniel@transgaming.com88853c52012-12-20 20:56:40 +000021#undef near
22#undef far
23
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000024namespace gl
25{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000026std::string str(int i)
27{
28 char buffer[20];
29 snprintf(buffer, sizeof(buffer), "%d", i);
30 return buffer;
31}
32
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000033UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index)
daniel@transgaming.comab1c1462012-12-20 21:08:30 +000034 : name(Uniform::undecorate(_name)), element(element), index(index)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000035{
36}
37
daniel@transgaming.come87ca002012-07-24 18:30:43 +000038unsigned int ProgramBinary::mCurrentSerial = 1;
39
daniel@transgaming.com77fbf972012-11-28 21:02:55 +000040ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000041{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000042 mPixelExecutable = NULL;
43 mVertexExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000044
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000045 mValidated = false;
46
47 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
48 {
49 mSemanticIndex[index] = -1;
50 }
51
52 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
53 {
54 mSamplersPS[index].active = false;
55 }
56
57 for (int index = 0; index < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; index++)
58 {
59 mSamplersVS[index].active = false;
60 }
61
62 mUsedVertexSamplerRange = 0;
63 mUsedPixelSamplerRange = 0;
64
daniel@transgaming.com593ebc42012-12-20 20:56:46 +000065 mDxDepthRangeRegisterVS = -1;
66 mDxDepthRangeRegisterPS = -1;
67 mDxDepthFrontRegister = -1;
68 mDxCoordRegister = -1;
69 mDxHalfPixelSizeRegister = -1;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000070}
71
72ProgramBinary::~ProgramBinary()
73{
daniel@transgaming.com95892412012-11-28 20:59:09 +000074 delete mPixelExecutable;
75 delete mVertexExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000076
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000077 while (!mUniforms.empty())
78 {
79 delete mUniforms.back();
80 mUniforms.pop_back();
81 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000082}
83
daniel@transgaming.come87ca002012-07-24 18:30:43 +000084unsigned int ProgramBinary::getSerial() const
85{
86 return mSerial;
87}
88
89unsigned int ProgramBinary::issueSerial()
90{
91 return mCurrentSerial++;
92}
93
daniel@transgaming.com95892412012-11-28 20:59:09 +000094rx::ShaderExecutable *ProgramBinary::getPixelExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000095{
96 return mPixelExecutable;
97}
98
daniel@transgaming.com95892412012-11-28 20:59:09 +000099rx::ShaderExecutable *ProgramBinary::getVertexExecutable()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000100{
101 return mVertexExecutable;
102}
103
104GLuint ProgramBinary::getAttributeLocation(const char *name)
105{
106 if (name)
107 {
108 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
109 {
110 if (mLinkedAttribute[index].name == std::string(name))
111 {
112 return index;
113 }
114 }
115 }
116
117 return -1;
118}
119
120int ProgramBinary::getSemanticIndex(int attributeIndex)
121{
122 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
123
124 return mSemanticIndex[attributeIndex];
125}
126
127// Returns one more than the highest sampler index used.
128GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
129{
130 switch (type)
131 {
132 case SAMPLER_PIXEL:
133 return mUsedPixelSamplerRange;
134 case SAMPLER_VERTEX:
135 return mUsedVertexSamplerRange;
136 default:
137 UNREACHABLE();
138 return 0;
139 }
140}
141
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000142bool ProgramBinary::usesPointSize() const
143{
144 return mUsesPointSize;
145}
146
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000147// Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler
148// index (0-15 for the pixel shader and 0-3 for the vertex shader).
149GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex)
150{
151 GLint logicalTextureUnit = -1;
152
153 switch (type)
154 {
155 case SAMPLER_PIXEL:
156 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
157
158 if (mSamplersPS[samplerIndex].active)
159 {
160 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit;
161 }
162 break;
163 case SAMPLER_VERTEX:
164 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
165
166 if (mSamplersVS[samplerIndex].active)
167 {
168 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit;
169 }
170 break;
171 default: UNREACHABLE();
172 }
173
174 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)getContext()->getMaximumCombinedTextureImageUnits())
175 {
176 return logicalTextureUnit;
177 }
178
179 return -1;
180}
181
182// Returns the texture type for a given Direct3D 9 sampler type and
183// index (0-15 for the pixel shader and 0-3 for the vertex shader).
184TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
185{
186 switch (type)
187 {
188 case SAMPLER_PIXEL:
189 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0]));
190 ASSERT(mSamplersPS[samplerIndex].active);
191 return mSamplersPS[samplerIndex].textureType;
192 case SAMPLER_VERTEX:
193 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0]));
194 ASSERT(mSamplersVS[samplerIndex].active);
195 return mSamplersVS[samplerIndex].textureType;
196 default: UNREACHABLE();
197 }
198
199 return TEXTURE_2D;
200}
201
202GLint ProgramBinary::getUniformLocation(std::string name)
203{
204 unsigned int subscript = 0;
205
206 // Strip any trailing array operator and retrieve the subscript
207 size_t open = name.find_last_of('[');
208 size_t close = name.find_last_of(']');
209 if (open != std::string::npos && close == name.length() - 1)
210 {
211 subscript = atoi(name.substr(open + 1).c_str());
212 name.erase(open);
213 }
214
215 unsigned int numUniforms = mUniformIndex.size();
216 for (unsigned int location = 0; location < numUniforms; location++)
217 {
218 if (mUniformIndex[location].name == name &&
219 mUniformIndex[location].element == subscript)
220 {
221 return location;
222 }
223 }
224
225 return -1;
226}
227
228bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
229{
230 if (location < 0 || location >= (int)mUniformIndex.size())
231 {
232 return false;
233 }
234
235 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
236 targetUniform->dirty = true;
237
238 if (targetUniform->type == GL_FLOAT)
239 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000240 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000241
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000242 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000243 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
244
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000245 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000246
247 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
248
249 for (int i = 0; i < count; i++)
250 {
251 target[0] = v[0];
252 target[1] = 0;
253 target[2] = 0;
254 target[3] = 0;
255 target += 4;
256 v += 1;
257 }
258 }
259 else if (targetUniform->type == GL_BOOL)
260 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000261 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000262
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000263 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000264 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
265
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000266 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000267 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
268
269 for (int i = 0; i < count; ++i)
270 {
271 if (v[i] == 0.0f)
272 {
273 boolParams[i] = GL_FALSE;
274 }
275 else
276 {
277 boolParams[i] = GL_TRUE;
278 }
279 }
280 }
281 else
282 {
283 return false;
284 }
285
286 return true;
287}
288
289bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
290{
291 if (location < 0 || location >= (int)mUniformIndex.size())
292 {
293 return false;
294 }
295
296 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
297 targetUniform->dirty = true;
298
299 if (targetUniform->type == GL_FLOAT_VEC2)
300 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000301 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000302
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000303 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000304 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
305
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000306 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000307
308 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
309
310 for (int i = 0; i < count; i++)
311 {
312 target[0] = v[0];
313 target[1] = v[1];
314 target[2] = 0;
315 target[3] = 0;
316 target += 4;
317 v += 2;
318 }
319 }
320 else if (targetUniform->type == GL_BOOL_VEC2)
321 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000322 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000323
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000324 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000325 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
326
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000327 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000328
329 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
330
331 for (int i = 0; i < count * 2; ++i)
332 {
333 if (v[i] == 0.0f)
334 {
335 boolParams[i] = GL_FALSE;
336 }
337 else
338 {
339 boolParams[i] = GL_TRUE;
340 }
341 }
342 }
343 else
344 {
345 return false;
346 }
347
348 return true;
349}
350
351bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
352{
353 if (location < 0 || location >= (int)mUniformIndex.size())
354 {
355 return false;
356 }
357
358 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
359 targetUniform->dirty = true;
360
361 if (targetUniform->type == GL_FLOAT_VEC3)
362 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000363 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000364
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000365 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000366 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
367
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000368 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000369
370 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4;
371
372 for (int i = 0; i < count; i++)
373 {
374 target[0] = v[0];
375 target[1] = v[1];
376 target[2] = v[2];
377 target[3] = 0;
378 target += 4;
379 v += 3;
380 }
381 }
382 else if (targetUniform->type == GL_BOOL_VEC3)
383 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000384 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000385
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000386 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000387 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
388
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000389 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000390 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
391
392 for (int i = 0; i < count * 3; ++i)
393 {
394 if (v[i] == 0.0f)
395 {
396 boolParams[i] = GL_FALSE;
397 }
398 else
399 {
400 boolParams[i] = GL_TRUE;
401 }
402 }
403 }
404 else
405 {
406 return false;
407 }
408
409 return true;
410}
411
412bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
413{
414 if (location < 0 || location >= (int)mUniformIndex.size())
415 {
416 return false;
417 }
418
419 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
420 targetUniform->dirty = true;
421
422 if (targetUniform->type == GL_FLOAT_VEC4)
423 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000424 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000425
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000426 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000427 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
428
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000429 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000430
431 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 4,
432 v, 4 * sizeof(GLfloat) * count);
433 }
434 else if (targetUniform->type == GL_BOOL_VEC4)
435 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000436 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000437
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000438 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000439 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
440
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000441 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000442 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
443
444 for (int i = 0; i < count * 4; ++i)
445 {
446 if (v[i] == 0.0f)
447 {
448 boolParams[i] = GL_FALSE;
449 }
450 else
451 {
452 boolParams[i] = GL_TRUE;
453 }
454 }
455 }
456 else
457 {
458 return false;
459 }
460
461 return true;
462}
463
464template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight>
465void transposeMatrix(T *target, const GLfloat *value)
466{
467 int copyWidth = std::min(targetWidth, srcWidth);
468 int copyHeight = std::min(targetHeight, srcHeight);
469
470 for (int x = 0; x < copyWidth; x++)
471 {
472 for (int y = 0; y < copyHeight; y++)
473 {
474 target[x * targetWidth + y] = (T)value[y * srcWidth + x];
475 }
476 }
477 // clear unfilled right side
478 for (int y = 0; y < copyHeight; y++)
479 {
480 for (int x = srcWidth; x < targetWidth; x++)
481 {
482 target[y * targetWidth + x] = (T)0;
483 }
484 }
485 // clear unfilled bottom.
486 for (int y = srcHeight; y < targetHeight; y++)
487 {
488 for (int x = 0; x < targetWidth; x++)
489 {
490 target[y * targetWidth + x] = (T)0;
491 }
492 }
493}
494
495bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
496{
497 if (location < 0 || location >= (int)mUniformIndex.size())
498 {
499 return false;
500 }
501
502 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
503 targetUniform->dirty = true;
504
505 if (targetUniform->type != GL_FLOAT_MAT2)
506 {
507 return false;
508 }
509
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000510 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000511
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000512 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000513 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
514
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000515 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000516
517 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8;
518 for (int i = 0; i < count; i++)
519 {
520 transposeMatrix<GLfloat,4,2,2,2>(target, value);
521 target += 8;
522 value += 4;
523 }
524
525 return true;
526}
527
528bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
529{
530 if (location < 0 || location >= (int)mUniformIndex.size())
531 {
532 return false;
533 }
534
535 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
536 targetUniform->dirty = true;
537
538 if (targetUniform->type != GL_FLOAT_MAT3)
539 {
540 return false;
541 }
542
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000543 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000544
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000545 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000546 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
547
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000548 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000549
550 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12;
551 for (int i = 0; i < count; i++)
552 {
553 transposeMatrix<GLfloat,4,3,3,3>(target, value);
554 target += 12;
555 value += 9;
556 }
557
558 return true;
559}
560
561
562bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
563{
564 if (location < 0 || location >= (int)mUniformIndex.size())
565 {
566 return false;
567 }
568
569 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
570 targetUniform->dirty = true;
571
572 if (targetUniform->type != GL_FLOAT_MAT4)
573 {
574 return false;
575 }
576
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000577 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000578
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000579 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000580 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
581
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000582 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000583
584 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16);
585 for (int i = 0; i < count; i++)
586 {
587 transposeMatrix<GLfloat,4,4,4,4>(target, value);
588 target += 16;
589 value += 16;
590 }
591
592 return true;
593}
594
595bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v)
596{
597 if (location < 0 || location >= (int)mUniformIndex.size())
598 {
599 return false;
600 }
601
602 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
603 targetUniform->dirty = true;
604
605 if (targetUniform->type == GL_INT ||
606 targetUniform->type == GL_SAMPLER_2D ||
607 targetUniform->type == GL_SAMPLER_CUBE)
608 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000609 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000610
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000611 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000612 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
613
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000614 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000615
616 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint),
617 v, sizeof(GLint) * count);
618 }
619 else if (targetUniform->type == GL_BOOL)
620 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000621 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000622
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000623 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000624 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
625
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000626 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000627 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element;
628
629 for (int i = 0; i < count; ++i)
630 {
631 if (v[i] == 0)
632 {
633 boolParams[i] = GL_FALSE;
634 }
635 else
636 {
637 boolParams[i] = GL_TRUE;
638 }
639 }
640 }
641 else
642 {
643 return false;
644 }
645
646 return true;
647}
648
649bool ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v)
650{
651 if (location < 0 || location >= (int)mUniformIndex.size())
652 {
653 return false;
654 }
655
656 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
657 targetUniform->dirty = true;
658
659 if (targetUniform->type == GL_INT_VEC2)
660 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000661 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000662
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000663 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000664 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
665
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000666 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000667
668 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 2,
669 v, 2 * sizeof(GLint) * count);
670 }
671 else if (targetUniform->type == GL_BOOL_VEC2)
672 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000673 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000674
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000675 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000676 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
677
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000678 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000679 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 2;
680
681 for (int i = 0; i < count * 2; ++i)
682 {
683 if (v[i] == 0)
684 {
685 boolParams[i] = GL_FALSE;
686 }
687 else
688 {
689 boolParams[i] = GL_TRUE;
690 }
691 }
692 }
693 else
694 {
695 return false;
696 }
697
698 return true;
699}
700
701bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v)
702{
703 if (location < 0 || location >= (int)mUniformIndex.size())
704 {
705 return false;
706 }
707
708 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
709 targetUniform->dirty = true;
710
711 if (targetUniform->type == GL_INT_VEC3)
712 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000713 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000714
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000715 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000716 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
717
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000718 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000719
720 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 3,
721 v, 3 * sizeof(GLint) * count);
722 }
723 else if (targetUniform->type == GL_BOOL_VEC3)
724 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000725 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000726
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000727 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000728 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
729
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000730 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000731 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 3;
732
733 for (int i = 0; i < count * 3; ++i)
734 {
735 if (v[i] == 0)
736 {
737 boolParams[i] = GL_FALSE;
738 }
739 else
740 {
741 boolParams[i] = GL_TRUE;
742 }
743 }
744 }
745 else
746 {
747 return false;
748 }
749
750 return true;
751}
752
753bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v)
754{
755 if (location < 0 || location >= (int)mUniformIndex.size())
756 {
757 return false;
758 }
759
760 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
761 targetUniform->dirty = true;
762
763 if (targetUniform->type == GL_INT_VEC4)
764 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000765 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000766
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000767 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000768 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
769
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000770 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000771
772 memcpy(targetUniform->data + mUniformIndex[location].element * sizeof(GLint) * 4,
773 v, 4 * sizeof(GLint) * count);
774 }
775 else if (targetUniform->type == GL_BOOL_VEC4)
776 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000777 int elementCount = targetUniform->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000778
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000779 if (elementCount == 1 && count > 1)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000780 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION
781
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000782 count = std::min(elementCount - (int)mUniformIndex[location].element, count);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000783 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * 4;
784
785 for (int i = 0; i < count * 4; ++i)
786 {
787 if (v[i] == 0)
788 {
789 boolParams[i] = GL_FALSE;
790 }
791 else
792 {
793 boolParams[i] = GL_TRUE;
794 }
795 }
796 }
797 else
798 {
799 return false;
800 }
801
802 return true;
803}
804
805bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params)
806{
807 if (location < 0 || location >= (int)mUniformIndex.size())
808 {
809 return false;
810 }
811
812 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
813
814 // sized queries -- ensure the provided buffer is large enough
815 if (bufSize)
816 {
817 int requiredBytes = UniformExternalSize(targetUniform->type);
818 if (*bufSize < requiredBytes)
819 {
820 return false;
821 }
822 }
823
824 switch (targetUniform->type)
825 {
826 case GL_FLOAT_MAT2:
827 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
828 break;
829 case GL_FLOAT_MAT3:
830 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
831 break;
832 case GL_FLOAT_MAT4:
833 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
834 break;
835 default:
836 {
837 unsigned int count = UniformExternalComponentCount(targetUniform->type);
838 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
839
840 switch (UniformComponentType(targetUniform->type))
841 {
842 case GL_BOOL:
843 {
844 GLboolean *boolParams = (GLboolean*)targetUniform->data + mUniformIndex[location].element * internalCount;
845
846 for (unsigned int i = 0; i < count; ++i)
847 {
848 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f;
849 }
850 }
851 break;
852 case GL_FLOAT:
853 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLfloat),
854 count * sizeof(GLfloat));
855 break;
856 case GL_INT:
857 {
858 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * internalCount;
859
860 for (unsigned int i = 0; i < count; ++i)
861 {
862 params[i] = (float)intParams[i];
863 }
864 }
865 break;
866 default: UNREACHABLE();
867 }
868 }
869 }
870
871 return true;
872}
873
874bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params)
875{
876 if (location < 0 || location >= (int)mUniformIndex.size())
877 {
878 return false;
879 }
880
881 Uniform *targetUniform = mUniforms[mUniformIndex[location].index];
882
883 // sized queries -- ensure the provided buffer is large enough
884 if (bufSize)
885 {
886 int requiredBytes = UniformExternalSize(targetUniform->type);
887 if (*bufSize < requiredBytes)
888 {
889 return false;
890 }
891 }
892
893 switch (targetUniform->type)
894 {
895 case GL_FLOAT_MAT2:
896 {
897 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8);
898 }
899 break;
900 case GL_FLOAT_MAT3:
901 {
902 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12);
903 }
904 break;
905 case GL_FLOAT_MAT4:
906 {
907 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16);
908 }
909 break;
910 default:
911 {
912 unsigned int count = UniformExternalComponentCount(targetUniform->type);
913 unsigned int internalCount = UniformInternalComponentCount(targetUniform->type);
914
915 switch (UniformComponentType(targetUniform->type))
916 {
917 case GL_BOOL:
918 {
919 GLboolean *boolParams = targetUniform->data + mUniformIndex[location].element * internalCount;
920
921 for (unsigned int i = 0; i < count; ++i)
922 {
923 params[i] = (GLint)boolParams[i];
924 }
925 }
926 break;
927 case GL_FLOAT:
928 {
929 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * internalCount;
930
931 for (unsigned int i = 0; i < count; ++i)
932 {
933 params[i] = (GLint)floatParams[i];
934 }
935 }
936 break;
937 case GL_INT:
938 memcpy(params, targetUniform->data + mUniformIndex[location].element * internalCount * sizeof(GLint),
939 count * sizeof(GLint));
940 break;
941 default: UNREACHABLE();
942 }
943 }
944 }
945
946 return true;
947}
948
949void ProgramBinary::dirtyAllUniforms()
950{
951 unsigned int numUniforms = mUniforms.size();
952 for (unsigned int index = 0; index < numUniforms; index++)
953 {
954 mUniforms[index]->dirty = true;
955 }
956}
957
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000958// Applies all the uniforms set for this program object to the renderer
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000959void ProgramBinary::applyUniforms()
960{
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000961 // Retrieve sampler uniform values
962 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub)
963 {
964 Uniform *targetUniform = *ub;
965
966 if (targetUniform->dirty)
967 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000968 if (targetUniform->type == GL_SAMPLER_2D ||
969 targetUniform->type == GL_SAMPLER_CUBE)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000970 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +0000971 int count = targetUniform->elementCount();
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000972 GLint *v = (GLint*)targetUniform->data;
973
974 if (targetUniform->ps.registerCount)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000975 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000976 ASSERT(targetUniform->ps.registerIndex >= 0);
977 unsigned int firstIndex = targetUniform->ps.registerIndex;
978
979 for (int i = 0; i < count; i++)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000980 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000981 unsigned int samplerIndex = firstIndex + i;
982
983 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000984 {
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000985 ASSERT(mSamplersPS[samplerIndex].active);
986 mSamplersPS[samplerIndex].logicalTextureUnit = v[i];
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000987 }
988 }
989 }
daniel@transgaming.comf9561862012-12-20 21:12:07 +0000990
991 if (targetUniform->vs.registerCount)
992 {
993 ASSERT(targetUniform->vs.registerIndex >= 0);
994 unsigned int firstIndex = targetUniform->vs.registerIndex;
995
996 for (int i = 0; i < count; i++)
997 {
998 unsigned int samplerIndex = firstIndex + i;
999
1000 if (samplerIndex < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF)
1001 {
1002 ASSERT(mSamplersVS[samplerIndex].active);
1003 mSamplersVS[samplerIndex].logicalTextureUnit = v[i];
1004 }
1005 }
1006 }
daniel@transgaming.comb6e55102012-12-20 21:08:14 +00001007 }
1008 }
1009 }
1010
daniel@transgaming.comab1c1462012-12-20 21:08:30 +00001011 mRenderer->applyUniforms(&mUniforms);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001012}
1013
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001014// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
1015// Returns the number of used varying registers, or -1 if unsuccesful
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001016int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001017{
1018 Context *context = getContext();
1019 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1020
1021 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1022 {
1023 int n = VariableRowCount(varying->type) * varying->size;
1024 int m = VariableColumnCount(varying->type);
1025 bool success = false;
1026
1027 if (m == 2 || m == 3 || m == 4)
1028 {
1029 for (int r = 0; r <= maxVaryingVectors - n && !success; r++)
1030 {
1031 bool available = true;
1032
1033 for (int y = 0; y < n && available; y++)
1034 {
1035 for (int x = 0; x < m && available; x++)
1036 {
1037 if (packing[r + y][x])
1038 {
1039 available = false;
1040 }
1041 }
1042 }
1043
1044 if (available)
1045 {
1046 varying->reg = r;
1047 varying->col = 0;
1048
1049 for (int y = 0; y < n; y++)
1050 {
1051 for (int x = 0; x < m; x++)
1052 {
1053 packing[r + y][x] = &*varying;
1054 }
1055 }
1056
1057 success = true;
1058 }
1059 }
1060
1061 if (!success && m == 2)
1062 {
1063 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--)
1064 {
1065 bool available = true;
1066
1067 for (int y = 0; y < n && available; y++)
1068 {
1069 for (int x = 2; x < 4 && available; x++)
1070 {
1071 if (packing[r + y][x])
1072 {
1073 available = false;
1074 }
1075 }
1076 }
1077
1078 if (available)
1079 {
1080 varying->reg = r;
1081 varying->col = 2;
1082
1083 for (int y = 0; y < n; y++)
1084 {
1085 for (int x = 2; x < 4; x++)
1086 {
1087 packing[r + y][x] = &*varying;
1088 }
1089 }
1090
1091 success = true;
1092 }
1093 }
1094 }
1095 }
1096 else if (m == 1)
1097 {
1098 int space[4] = {0};
1099
1100 for (int y = 0; y < maxVaryingVectors; y++)
1101 {
1102 for (int x = 0; x < 4; x++)
1103 {
1104 space[x] += packing[y][x] ? 0 : 1;
1105 }
1106 }
1107
1108 int column = 0;
1109
1110 for (int x = 0; x < 4; x++)
1111 {
1112 if (space[x] >= n && space[x] < space[column])
1113 {
1114 column = x;
1115 }
1116 }
1117
1118 if (space[column] >= n)
1119 {
1120 for (int r = 0; r < maxVaryingVectors; r++)
1121 {
1122 if (!packing[r][column])
1123 {
1124 varying->reg = r;
1125
1126 for (int y = r; y < r + n; y++)
1127 {
1128 packing[y][column] = &*varying;
1129 }
1130
1131 break;
1132 }
1133 }
1134
1135 varying->col = column;
1136
1137 success = true;
1138 }
1139 }
1140 else UNREACHABLE();
1141
1142 if (!success)
1143 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001144 infoLog.append("Could not pack varying %s", varying->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001145
1146 return -1;
1147 }
1148 }
1149
1150 // Return the number of used registers
1151 int registers = 0;
1152
1153 for (int r = 0; r < maxVaryingVectors; r++)
1154 {
1155 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
1156 {
1157 registers++;
1158 }
1159 }
1160
1161 return registers;
1162}
1163
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001164bool ProgramBinary::linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001165{
1166 if (pixelHLSL.empty() || vertexHLSL.empty())
1167 {
1168 return false;
1169 }
1170
1171 // Reset the varying register assignments
1172 for (VaryingList::iterator fragVar = fragmentShader->mVaryings.begin(); fragVar != fragmentShader->mVaryings.end(); fragVar++)
1173 {
1174 fragVar->reg = -1;
1175 fragVar->col = -1;
1176 }
1177
1178 for (VaryingList::iterator vtxVar = vertexShader->mVaryings.begin(); vtxVar != vertexShader->mVaryings.end(); vtxVar++)
1179 {
1180 vtxVar->reg = -1;
1181 vtxVar->col = -1;
1182 }
1183
1184 // Map the varyings to the register file
1185 const Varying *packing[MAX_VARYING_VECTORS_SM3][4] = {NULL};
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001186 int registers = packVaryings(infoLog, packing, fragmentShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001187
1188 if (registers < 0)
1189 {
1190 return false;
1191 }
1192
1193 // Write the HLSL input/output declarations
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001194 const bool sm3 = (mRenderer->getMajorShaderModel() >= 3);
1195 const bool sm4 = (mRenderer->getMajorShaderModel() >= 4);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001196 Context *context = getContext();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001197 const int maxVaryingVectors = context->getMaximumVaryingVectors();
1198
1199 if (registers == maxVaryingVectors && fragmentShader->mUsesFragCoord)
1200 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001201 infoLog.append("No varying registers left to support gl_FragCoord");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001202
1203 return false;
1204 }
1205
1206 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++)
1207 {
1208 bool matched = false;
1209
1210 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++)
1211 {
1212 if (output->name == input->name)
1213 {
1214 if (output->type != input->type || output->size != input->size)
1215 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001216 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 +00001217
1218 return false;
1219 }
1220
1221 output->reg = input->reg;
1222 output->col = input->col;
1223
1224 matched = true;
1225 break;
1226 }
1227 }
1228
1229 if (!matched)
1230 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001231 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001232
1233 return false;
1234 }
1235 }
1236
daniel@transgaming.com087e5782012-09-17 21:28:47 +00001237 mUsesPointSize = vertexShader->mUsesPointSize;
1238 std::string varyingSemantic = (mUsesPointSize && sm3) ? "COLOR" : "TEXCOORD";
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001239 std::string targetSemantic = sm4 ? "SV_Target" : "COLOR";
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001240 std::string positionSemantic = sm4 ? "SV_POSITION" : "POSITION";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001241
1242 vertexHLSL += "struct VS_INPUT\n"
1243 "{\n";
1244
1245 int semanticIndex = 0;
1246 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1247 {
1248 switch (attribute->type)
1249 {
1250 case GL_FLOAT: vertexHLSL += " float "; break;
1251 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break;
1252 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break;
1253 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break;
1254 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break;
1255 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break;
1256 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break;
1257 default: UNREACHABLE();
1258 }
1259
1260 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n";
1261
1262 semanticIndex += VariableRowCount(attribute->type);
1263 }
1264
1265 vertexHLSL += "};\n"
1266 "\n"
1267 "struct VS_OUTPUT\n"
1268 "{\n"
daniel@transgaming.com617048e2012-11-28 21:05:22 +00001269 " float4 gl_Position : " + positionSemantic + ";\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001270
1271 for (int r = 0; r < registers; r++)
1272 {
1273 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1));
1274
1275 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n";
1276 }
1277
1278 if (fragmentShader->mUsesFragCoord)
1279 {
1280 vertexHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1281 }
1282
1283 if (vertexShader->mUsesPointSize && sm3)
1284 {
1285 vertexHLSL += " float gl_PointSize : PSIZE;\n";
1286 }
1287
1288 vertexHLSL += "};\n"
1289 "\n"
1290 "VS_OUTPUT main(VS_INPUT input)\n"
1291 "{\n";
1292
1293 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1294 {
1295 vertexHLSL += " " + decorateAttribute(attribute->name) + " = ";
1296
1297 if (VariableRowCount(attribute->type) > 1) // Matrix
1298 {
1299 vertexHLSL += "transpose";
1300 }
1301
1302 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n";
1303 }
1304
1305 vertexHLSL += "\n"
1306 " gl_main();\n"
1307 "\n"
1308 " VS_OUTPUT output;\n"
1309 " output.gl_Position.x = gl_Position.x - dx_HalfPixelSize.x * gl_Position.w;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001310 " output.gl_Position.y = -(gl_Position.y + dx_HalfPixelSize.y * gl_Position.w);\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001311 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
1312 " output.gl_Position.w = gl_Position.w;\n";
1313
1314 if (vertexShader->mUsesPointSize && sm3)
1315 {
daniel@transgaming.com13be3e42012-07-04 19:16:24 +00001316 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001317 }
1318
1319 if (fragmentShader->mUsesFragCoord)
1320 {
1321 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
1322 }
1323
1324 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++)
1325 {
1326 if (varying->reg >= 0)
1327 {
1328 for (int i = 0; i < varying->size; i++)
1329 {
1330 int rows = VariableRowCount(varying->type);
1331
1332 for (int j = 0; j < rows; j++)
1333 {
1334 int r = varying->reg + i * rows + j;
1335 vertexHLSL += " output.v" + str(r);
1336
1337 bool sharedRegister = false; // Register used by multiple varyings
1338
1339 for (int x = 0; x < 4; x++)
1340 {
1341 if (packing[r][x] && packing[r][x] != packing[r][0])
1342 {
1343 sharedRegister = true;
1344 break;
1345 }
1346 }
1347
1348 if(sharedRegister)
1349 {
1350 vertexHLSL += ".";
1351
1352 for (int x = 0; x < 4; x++)
1353 {
1354 if (packing[r][x] == &*varying)
1355 {
1356 switch(x)
1357 {
1358 case 0: vertexHLSL += "x"; break;
1359 case 1: vertexHLSL += "y"; break;
1360 case 2: vertexHLSL += "z"; break;
1361 case 3: vertexHLSL += "w"; break;
1362 }
1363 }
1364 }
1365 }
1366
1367 vertexHLSL += " = " + varying->name;
1368
1369 if (varying->array)
1370 {
1371 vertexHLSL += "[" + str(i) + "]";
1372 }
1373
1374 if (rows > 1)
1375 {
1376 vertexHLSL += "[" + str(j) + "]";
1377 }
1378
1379 vertexHLSL += ";\n";
1380 }
1381 }
1382 }
1383 }
1384
1385 vertexHLSL += "\n"
1386 " return output;\n"
1387 "}\n";
1388
1389 pixelHLSL += "struct PS_INPUT\n"
1390 "{\n";
1391
1392 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1393 {
1394 if (varying->reg >= 0)
1395 {
1396 for (int i = 0; i < varying->size; i++)
1397 {
1398 int rows = VariableRowCount(varying->type);
1399 for (int j = 0; j < rows; j++)
1400 {
1401 std::string n = str(varying->reg + i * rows + j);
1402 pixelHLSL += " float4 v" + n + " : " + varyingSemantic + n + ";\n";
1403 }
1404 }
1405 }
1406 else UNREACHABLE();
1407 }
1408
1409 if (fragmentShader->mUsesFragCoord)
1410 {
1411 pixelHLSL += " float4 gl_FragCoord : " + varyingSemantic + str(registers) + ";\n";
1412 if (sm3) {
1413 pixelHLSL += " float2 dx_VPos : VPOS;\n";
1414 }
1415 }
1416
1417 if (fragmentShader->mUsesPointCoord && sm3)
1418 {
1419 pixelHLSL += " float2 gl_PointCoord : TEXCOORD0;\n";
1420 }
1421
1422 if (fragmentShader->mUsesFrontFacing)
1423 {
1424 pixelHLSL += " float vFace : VFACE;\n";
1425 }
1426
1427 pixelHLSL += "};\n"
1428 "\n"
1429 "struct PS_OUTPUT\n"
1430 "{\n"
daniel@transgaming.comc5693152012-11-28 21:03:02 +00001431 " float4 gl_Color[1] : " + targetSemantic + ";\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001432 "};\n"
1433 "\n"
1434 "PS_OUTPUT main(PS_INPUT input)\n"
1435 "{\n";
1436
1437 if (fragmentShader->mUsesFragCoord)
1438 {
1439 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
1440
1441 if (sm3)
1442 {
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001443 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001444 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001445 }
1446 else
1447 {
1448 // dx_Coord contains the viewport width/2, height/2, center.x and center.y. See Context::applyRenderTarget()
1449 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_Coord.x + dx_Coord.z;\n"
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001450 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_Coord.y + dx_Coord.w;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001451 }
1452
daniel@transgaming.com12985182012-12-20 20:56:31 +00001453 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001454 " gl_FragCoord.w = rhw;\n";
1455 }
1456
1457 if (fragmentShader->mUsesPointCoord && sm3)
1458 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001459 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1460 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001461 }
1462
1463 if (fragmentShader->mUsesFrontFacing)
1464 {
daniel@transgaming.com12985182012-12-20 20:56:31 +00001465 pixelHLSL += " gl_FrontFacing = (input.vFace * dx_DepthFront.z >= 0.0);\n";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001466 }
1467
1468 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++)
1469 {
1470 if (varying->reg >= 0)
1471 {
1472 for (int i = 0; i < varying->size; i++)
1473 {
1474 int rows = VariableRowCount(varying->type);
1475 for (int j = 0; j < rows; j++)
1476 {
1477 std::string n = str(varying->reg + i * rows + j);
1478 pixelHLSL += " " + varying->name;
1479
1480 if (varying->array)
1481 {
1482 pixelHLSL += "[" + str(i) + "]";
1483 }
1484
1485 if (rows > 1)
1486 {
1487 pixelHLSL += "[" + str(j) + "]";
1488 }
1489
daniel@transgaming.comf5a2ae52012-12-20 20:52:03 +00001490 switch (VariableColumnCount(varying->type))
1491 {
1492 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1493 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1494 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1495 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1496 default: UNREACHABLE();
1497 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001498 }
1499 }
1500 }
1501 else UNREACHABLE();
1502 }
1503
1504 pixelHLSL += "\n"
1505 " gl_main();\n"
1506 "\n"
1507 " PS_OUTPUT output;\n"
1508 " output.gl_Color[0] = gl_Color[0];\n"
1509 "\n"
1510 " return output;\n"
1511 "}\n";
1512
1513 return true;
1514}
1515
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001516bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length)
1517{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001518 BinaryInputStream stream(binary, length);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001519
1520 int format = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001521 stream.read(&format);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001522 if (format != GL_PROGRAM_BINARY_ANGLE)
1523 {
1524 infoLog.append("Invalid program binary format.");
1525 return false;
1526 }
1527
1528 int version = 0;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001529 stream.read(&version);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001530 if (version != VERSION_DWORD)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001531 {
1532 infoLog.append("Invalid program binary version.");
1533 return false;
1534 }
1535
1536 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1537 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001538 stream.read(&mLinkedAttribute[i].type);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001539 std::string name;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001540 stream.read(&name);
1541 mLinkedAttribute[i].name = name;
1542 stream.read(&mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001543 }
1544
1545 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1546 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001547 stream.read(&mSamplersPS[i].active);
1548 stream.read(&mSamplersPS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001549
1550 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001551 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001552 mSamplersPS[i].textureType = (TextureType) textureType;
1553 }
1554
1555 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1556 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001557 stream.read(&mSamplersVS[i].active);
1558 stream.read(&mSamplersVS[i].logicalTextureUnit);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001559
1560 int textureType;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001561 stream.read(&textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001562 mSamplersVS[i].textureType = (TextureType) textureType;
1563 }
1564
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001565 stream.read(&mUsedVertexSamplerRange);
1566 stream.read(&mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001567
1568 unsigned int size;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001569 stream.read(&size);
1570 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001571 {
1572 infoLog.append("Invalid program binary.");
1573 return false;
1574 }
1575
1576 mUniforms.resize(size);
1577 for (unsigned int i = 0; i < size; ++i)
1578 {
1579 GLenum type;
1580 std::string _name;
1581 unsigned int arraySize;
1582
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001583 stream.read(&type);
1584 stream.read(&_name);
1585 stream.read(&arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001586
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001587 mUniforms[i] = new Uniform(type, _name, arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001588
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001589 stream.read(&mUniforms[i]->ps.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001590 stream.read(&mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001591
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001592 stream.read(&mUniforms[i]->vs.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001593 stream.read(&mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001594 }
1595
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001596 stream.read(&size);
1597 if (stream.error())
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001598 {
1599 infoLog.append("Invalid program binary.");
1600 return false;
1601 }
1602
1603 mUniformIndex.resize(size);
1604 for (unsigned int i = 0; i < size; ++i)
1605 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001606 stream.read(&mUniformIndex[i].name);
1607 stream.read(&mUniformIndex[i].element);
1608 stream.read(&mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001609 }
1610
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001611 stream.read(&mDxDepthRangeRegisterVS);
1612 stream.read(&mDxDepthRangeRegisterPS);
1613 stream.read(&mDxDepthFrontRegister);
1614 stream.read(&mDxCoordRegister);
1615 stream.read(&mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001616
1617 unsigned int pixelShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001618 stream.read(&pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001619
1620 unsigned int vertexShaderSize;
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001621 stream.read(&vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001622
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001623 const char *ptr = (const char*) binary + stream.offset();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001624
daniel@transgaming.com36038542012-11-28 20:59:26 +00001625 const GUID *binaryIdentifier = (const GUID *) ptr;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001626 ptr += sizeof(GUID);
1627
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001628 GUID identifier = mRenderer->getAdapterIdentifier();
1629 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001630 {
1631 infoLog.append("Invalid program binary.");
1632 return false;
1633 }
1634
1635 const char *pixelShaderFunction = ptr;
1636 ptr += pixelShaderSize;
1637
1638 const char *vertexShaderFunction = ptr;
1639 ptr += vertexShaderSize;
1640
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001641 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction),
1642 pixelShaderSize, GL_FRAGMENT_SHADER, NULL);
1643 if (!mPixelExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001644 {
1645 infoLog.append("Could not create pixel shader.");
1646 return false;
1647 }
1648
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001649 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction),
1650 vertexShaderSize, GL_VERTEX_SHADER, NULL);
1651 if (!mVertexExecutable)
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001652 {
1653 infoLog.append("Could not create vertex shader.");
daniel@transgaming.com95892412012-11-28 20:59:09 +00001654 delete mPixelExecutable;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001655 mPixelExecutable = NULL;
1656 return false;
1657 }
1658
1659 return true;
1660}
1661
1662bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length)
1663{
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001664 BinaryOutputStream stream;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001665
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001666 stream.write(GL_PROGRAM_BINARY_ANGLE);
daniel@transgaming.com8226f4c2012-12-20 21:08:42 +00001667 stream.write(VERSION_DWORD);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001668
1669 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
1670 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001671 stream.write(mLinkedAttribute[i].type);
1672 stream.write(mLinkedAttribute[i].name);
1673 stream.write(mSemanticIndex[i]);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001674 }
1675
1676 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i)
1677 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001678 stream.write(mSamplersPS[i].active);
1679 stream.write(mSamplersPS[i].logicalTextureUnit);
1680 stream.write((int) mSamplersPS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001681 }
1682
1683 for (unsigned int i = 0; i < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; ++i)
1684 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001685 stream.write(mSamplersVS[i].active);
1686 stream.write(mSamplersVS[i].logicalTextureUnit);
1687 stream.write((int) mSamplersVS[i].textureType);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001688 }
1689
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001690 stream.write(mUsedVertexSamplerRange);
1691 stream.write(mUsedPixelSamplerRange);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001692
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001693 stream.write(mUniforms.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001694 for (unsigned int i = 0; i < mUniforms.size(); ++i)
1695 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001696 stream.write(mUniforms[i]->type);
1697 stream.write(mUniforms[i]->_name);
1698 stream.write(mUniforms[i]->arraySize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001699
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001700 stream.write(mUniforms[i]->ps.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001701 stream.write(mUniforms[i]->ps.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001702
daniel@transgaming.comf9561862012-12-20 21:12:07 +00001703 stream.write(mUniforms[i]->vs.registerIndex);
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001704 stream.write(mUniforms[i]->vs.registerCount);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001705 }
1706
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001707 stream.write(mUniformIndex.size());
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001708 for (unsigned int i = 0; i < mUniformIndex.size(); ++i)
1709 {
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001710 stream.write(mUniformIndex[i].name);
1711 stream.write(mUniformIndex[i].element);
1712 stream.write(mUniformIndex[i].index);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001713 }
1714
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00001715 stream.write(mDxDepthRangeRegisterVS);
1716 stream.write(mDxDepthRangeRegisterPS);
1717 stream.write(mDxDepthFrontRegister);
1718 stream.write(mDxCoordRegister);
1719 stream.write(mDxHalfPixelSizeRegister);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001720
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001721 UINT pixelShaderSize = mPixelExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001722 stream.write(pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001723
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001724 UINT vertexShaderSize = mVertexExecutable->getLength();
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001725 stream.write(vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001726
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001727 GUID identifier = mRenderer->getAdapterIdentifier();
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001728
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001729 GLsizei streamLength = stream.length();
1730 const void *streamData = stream.data();
1731
1732 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001733 if (totalLength > bufSize)
1734 {
1735 if (length)
1736 {
1737 *length = 0;
1738 }
1739
1740 return false;
1741 }
1742
1743 if (binary)
1744 {
1745 char *ptr = (char*) binary;
1746
apatrick@chromium.org6f1796f2012-07-12 01:40:11 +00001747 memcpy(ptr, streamData, streamLength);
1748 ptr += streamLength;
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001749
daniel@transgaming.com4ca789e2012-10-31 18:46:40 +00001750 memcpy(ptr, &identifier, sizeof(GUID));
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001751 ptr += sizeof(GUID);
1752
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001753 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001754 ptr += pixelShaderSize;
1755
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00001756 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize);
apatrick@chromium.org90080e32012-07-09 22:15:33 +00001757 ptr += vertexShaderSize;
1758
1759 ASSERT(ptr - totalLength == binary);
1760 }
1761
1762 if (length)
1763 {
1764 *length = totalLength;
1765 }
1766
1767 return true;
1768}
1769
1770GLint ProgramBinary::getLength()
1771{
1772 GLint length;
1773 if (save(NULL, INT_MAX, &length))
1774 {
1775 return length;
1776 }
1777 else
1778 {
1779 return 0;
1780 }
1781}
1782
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001783bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001784{
1785 if (!fragmentShader || !fragmentShader->isCompiled())
1786 {
1787 return false;
1788 }
1789
1790 if (!vertexShader || !vertexShader->isCompiled())
1791 {
1792 return false;
1793 }
1794
1795 std::string pixelHLSL = fragmentShader->getHLSL();
1796 std::string vertexHLSL = vertexShader->getHLSL();
1797
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001798 if (!linkVaryings(infoLog, pixelHLSL, vertexHLSL, fragmentShader, vertexShader))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001799 {
1800 return false;
1801 }
1802
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001803 bool success = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001804 rx::D3DConstantTable *constantTableVS = NULL;
1805 rx::D3DConstantTable *constantTablePS = NULL;
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001806 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), GL_VERTEX_SHADER);
1807 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), GL_FRAGMENT_SHADER);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001808
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001809 if (mVertexExecutable && mPixelExecutable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001810 {
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001811 // D3D9_REPLACE
daniel@transgaming.com95892412012-11-28 20:59:09 +00001812 constantTableVS = mVertexExecutable->getConstantTable();
1813 constantTablePS = mPixelExecutable->getConstantTable();
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001814 }
1815 else
1816 {
daniel@transgaming.com95892412012-11-28 20:59:09 +00001817 infoLog.append("Failed to create D3D shaders.");
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001818 success = false;
daniel@transgaming.com95892412012-11-28 20:59:09 +00001819
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +00001820 delete mVertexExecutable;
1821 mVertexExecutable = NULL;
1822 delete mPixelExecutable;
1823 mPixelExecutable = NULL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001824 }
1825
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001826 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader))
1827 {
1828 success = false;
1829 }
1830
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001831 if (constantTableVS && constantTablePS) // D3D9_REPLACE
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001832 {
1833 if (!linkUniforms(infoLog, constantTableVS, constantTablePS))
1834 {
1835 success = false;
1836 }
1837 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001838 else
1839 {
1840 for (sh::ActiveUniforms::const_iterator uniform = vertexShader->getUniforms().begin(); uniform != vertexShader->getUniforms().end(); uniform++)
1841 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001842 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog))
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001843 {
1844 success = false;
1845 break;
1846 }
1847 }
1848
1849 for (sh::ActiveUniforms::const_iterator uniform = fragmentShader->getUniforms().begin(); uniform != fragmentShader->getUniforms().end(); uniform++)
1850 {
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00001851 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog))
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00001852 {
1853 success = false;
1854 break;
1855 }
1856 }
1857 }
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001858
daniel@transgaming.comc68fa872012-11-28 20:58:32 +00001859 Context *context = getContext();
1860 context->markDxUniformsDirty();
1861
1862 return success;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001863}
1864
1865// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001866bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001867{
1868 unsigned int usedLocations = 0;
1869
1870 // Link attributes that have a binding location
1871 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1872 {
1873 int location = attributeBindings.getAttributeBinding(attribute->name);
1874
1875 if (location != -1) // Set by glBindAttribLocation
1876 {
1877 if (!mLinkedAttribute[location].name.empty())
1878 {
1879 // Multiple active attributes bound to the same location; not an error
1880 }
1881
1882 mLinkedAttribute[location] = *attribute;
1883
1884 int rows = VariableRowCount(attribute->type);
1885
1886 if (rows + location > MAX_VERTEX_ATTRIBS)
1887 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001888 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 +00001889
1890 return false;
1891 }
1892
1893 for (int i = 0; i < rows; i++)
1894 {
1895 usedLocations |= 1 << (location + i);
1896 }
1897 }
1898 }
1899
1900 // Link attributes that don't have a binding location
1901 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++)
1902 {
1903 int location = attributeBindings.getAttributeBinding(attribute->name);
1904
1905 if (location == -1) // Not set by glBindAttribLocation
1906 {
1907 int rows = VariableRowCount(attribute->type);
1908 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
1909
1910 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
1911 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001912 infoLog.append("Too many active attributes (%s)", attribute->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001913
1914 return false; // Fail to link
1915 }
1916
1917 mLinkedAttribute[availableIndex] = *attribute;
1918 }
1919 }
1920
1921 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
1922 {
1923 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
1924 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1);
1925
1926 for (int r = 0; r < rows; r++)
1927 {
1928 mSemanticIndex[attributeIndex++] = index++;
1929 }
1930 }
1931
1932 return true;
1933}
1934
daniel@transgaming.com31240482012-11-28 21:06:41 +00001935bool ProgramBinary::linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001936{
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001937 for (unsigned int constantIndex = 0; constantIndex < psConstantTable->constants(); constantIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001938 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001939 const rx::D3DConstant *constant = psConstantTable->getConstant(constantIndex);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001940
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001941 if (!defineUniform(infoLog, GL_FRAGMENT_SHADER, constant, "", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001942 {
1943 return false;
1944 }
1945 }
1946
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001947 for (unsigned int constantIndex = 0; constantIndex < vsConstantTable->constants(); constantIndex++)
1948 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001949 const rx::D3DConstant *constant = vsConstantTable->getConstant(constantIndex);
daniel@transgaming.coma418ef12012-11-28 20:58:22 +00001950
1951 if (!defineUniform(infoLog, GL_VERTEX_SHADER, constant, "", vsConstantTable, psConstantTable))
1952 {
1953 return false;
1954 }
1955 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001956 return true;
1957}
1958
1959// Adds the description of a constant found in the binary shader to the list of uniforms
1960// Returns true if succesful (uniform not already defined)
daniel@transgaming.com31240482012-11-28 21:06:41 +00001961bool ProgramBinary::defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name,
1962 rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001963{
daniel@transgaming.com31240482012-11-28 21:06:41 +00001964 if (constant->registerSet == rx::D3DConstant::RS_SAMPLER)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001965 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001966 for (unsigned int i = 0; i < constant->registerCount; i++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001967 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00001968 const rx::D3DConstant *psConstant = psConstantTable->getConstantByName(constant->name.c_str());
1969 const rx::D3DConstant *vsConstant = vsConstantTable->getConstantByName(constant->name.c_str());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001970
1971 if (psConstant)
1972 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001973 unsigned int samplerIndex = psConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001974
1975 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
1976 {
1977 mSamplersPS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001978 mSamplersPS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001979 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
1980 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
1981 }
1982 else
1983 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001984 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001985 return false;
1986 }
1987 }
1988
1989 if (vsConstant)
1990 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00001991 unsigned int samplerIndex = vsConstant->registerIndex + i;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001992
1993 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
1994 {
1995 mSamplersVS[samplerIndex].active = true;
daniel@transgaming.com31240482012-11-28 21:06:41 +00001996 mSamplersVS[samplerIndex].textureType = (constant->type == rx::D3DConstant::PT_SAMPLERCUBE) ? TEXTURE_CUBE : TEXTURE_2D;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001997 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
1998 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
1999 }
2000 else
2001 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002002 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002003 return false;
2004 }
2005 }
2006 }
2007 }
2008
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002009 switch(constant->typeClass)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002010 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002011 case rx::D3DConstant::CLASS_STRUCT:
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002012 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002013 for (unsigned int arrayIndex = 0; arrayIndex < constant->elements; arrayIndex++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002014 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002015 for (unsigned int field = 0; field < constant->structMembers[arrayIndex].size(); field++)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002016 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002017 const rx::D3DConstant *fieldConstant = constant->structMembers[arrayIndex][field];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002018
apatrick@chromium.org85fee292012-09-06 00:43:06 +00002019 std::string structIndex = (constant->elements > 1) ? ("[" + str(arrayIndex) + "]") : "";
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002020
daniel@transgaming.com59d9ab12012-11-28 20:58:14 +00002021 if (!defineUniform(infoLog, shader, fieldConstant, name + constant->name + structIndex + ".", vsConstantTable, psConstantTable))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002022 {
2023 return false;
2024 }
2025 }
2026 }
2027
2028 return true;
2029 }
daniel@transgaming.com31240482012-11-28 21:06:41 +00002030 case rx::D3DConstant::CLASS_SCALAR:
2031 case rx::D3DConstant::CLASS_VECTOR:
2032 case rx::D3DConstant::CLASS_MATRIX_COLUMNS:
2033 case rx::D3DConstant::CLASS_OBJECT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002034 return defineUniform(shader, constant, name + constant->name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002035 default:
2036 UNREACHABLE();
2037 return false;
2038 }
2039}
2040
daniel@transgaming.com31240482012-11-28 21:06:41 +00002041bool ProgramBinary::defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002042{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002043 if (_name == "dx_DepthRange")
2044 {
2045 if (shader == GL_VERTEX_SHADER) mDxDepthRangeRegisterVS = constant->registerIndex;
2046 if (shader == GL_FRAGMENT_SHADER) mDxDepthRangeRegisterPS = constant->registerIndex;
2047 return true;
2048 }
2049
2050 if (_name == "dx_DepthFront")
2051 {
2052 mDxDepthFrontRegister = constant->registerIndex;
2053 return true;
2054 }
2055
2056 if (_name == "dx_Coord")
2057 {
2058 mDxCoordRegister = constant->registerIndex;
2059 return true;
2060 }
2061
2062 if (_name == "dx_HalfPixelSize")
2063 {
2064 mDxHalfPixelSizeRegister = constant->registerIndex;
2065 return true;
2066 }
2067
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002068 Uniform *uniform = createUniform(constant, _name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002069
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002070 if (!uniform)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002071 {
2072 return false;
2073 }
2074
2075 // Check if already defined
2076 GLint location = getUniformLocation(uniform->name);
2077 GLenum type = uniform->type;
2078
2079 if (location >= 0)
2080 {
2081 delete uniform;
2082 uniform = mUniforms[mUniformIndex[location].index];
2083 }
2084
daniel@transgaming.comf9561862012-12-20 21:12:07 +00002085 if (shader == GL_FRAGMENT_SHADER)
2086 {
2087 uniform->ps.registerIndex = constant->registerIndex;
2088 uniform->ps.registerCount = constant->registerCount;
2089 }
2090 else if (shader == GL_VERTEX_SHADER)
2091 {
2092 uniform->vs.registerIndex = constant->registerIndex;
2093 uniform->vs.registerCount = constant->registerCount;
2094 }
2095 else UNREACHABLE();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002096
2097 if (location >= 0)
2098 {
2099 return uniform->type == type;
2100 }
2101
2102 mUniforms.push_back(uniform);
2103 unsigned int uniformIndex = mUniforms.size() - 1;
2104
2105 for (unsigned int i = 0; i < uniform->arraySize; ++i)
2106 {
2107 mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
2108 }
2109
2110 return true;
2111}
2112
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002113bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002114{
daniel@transgaming.com7aa6aff2012-12-20 21:13:02 +00002115 if (constant.name == "dx_DepthRange")
2116 {
2117 if (shader == GL_VERTEX_SHADER) mDxDepthRangeRegisterVS = constant.registerIndex;
2118 if (shader == GL_FRAGMENT_SHADER) mDxDepthRangeRegisterPS = constant.registerIndex;
2119 return true;
2120 }
2121
2122 if (constant.name == "dx_DepthFront")
2123 {
2124 mDxDepthFrontRegister = constant.registerIndex;
2125 return true;
2126 }
2127
2128 if (constant.name == "dx_Coord")
2129 {
2130 mDxCoordRegister = constant.registerIndex;
2131 return true;
2132 }
2133
2134 if (constant.name == "dx_HalfPixelSize")
2135 {
2136 mDxHalfPixelSizeRegister = constant.registerIndex;
2137 return true;
2138 }
2139
daniel@transgaming.comda8d3802012-12-20 21:12:55 +00002140 if (constant.type == GL_SAMPLER_2D ||
2141 constant.type == GL_SAMPLER_CUBE)
2142 {
2143 unsigned int samplerIndex = constant.registerIndex;
2144
2145 do
2146 {
2147 if (shader == GL_VERTEX_SHADER)
2148 {
2149 if (samplerIndex < getContext()->getMaximumVertexTextureImageUnits())
2150 {
2151 mSamplersVS[samplerIndex].active = true;
2152 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2153 mSamplersVS[samplerIndex].logicalTextureUnit = 0;
2154 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange);
2155 }
2156 else
2157 {
2158 infoLog.append("Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (%d).", getContext()->getMaximumVertexTextureImageUnits());
2159 return false;
2160 }
2161 }
2162 else if (shader == GL_FRAGMENT_SHADER)
2163 {
2164 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
2165 {
2166 mSamplersPS[samplerIndex].active = true;
2167 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D;
2168 mSamplersPS[samplerIndex].logicalTextureUnit = 0;
2169 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange);
2170 }
2171 else
2172 {
2173 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS);
2174 return false;
2175 }
2176 }
2177 else UNREACHABLE();
2178
2179 samplerIndex++;
2180 }
2181 while (samplerIndex < constant.registerIndex + constant.arraySize);
2182 }
2183
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002184 Uniform *uniform = NULL;
2185 GLint location = getUniformLocation(constant.name);
2186
2187 if (location >= 0) // Previously defined, types must match
2188 {
2189 uniform = mUniforms[mUniformIndex[location].index];
2190
2191 if (uniform->type != constant.type)
2192 {
2193 return false;
2194 }
2195 }
2196 else
2197 {
2198 uniform = new Uniform(constant.type, constant.name, constant.arraySize);
2199 }
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002200
2201 if (!uniform)
2202 {
2203 return false;
2204 }
2205
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002206 if (shader == GL_FRAGMENT_SHADER)
2207 {
2208 uniform->ps.registerIndex = constant.registerIndex;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002209 uniform->ps.registerCount = uniform->registerCount();
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002210 }
2211 else if (shader == GL_VERTEX_SHADER)
2212 {
2213 uniform->vs.registerIndex = constant.registerIndex;
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002214 uniform->vs.registerCount = uniform->registerCount();
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002215 }
2216 else UNREACHABLE();
2217
2218 if (location >= 0)
2219 {
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002220 return uniform->type == constant.type;
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002221 }
2222
2223 mUniforms.push_back(uniform);
2224 unsigned int uniformIndex = mUniforms.size() - 1;
2225
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002226 for (unsigned int i = 0; i < uniform->elementCount(); i++)
daniel@transgaming.comfdc7f562012-12-20 21:12:37 +00002227 {
2228 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex));
2229 }
2230
2231 return true;
2232}
2233
daniel@transgaming.com31240482012-11-28 21:06:41 +00002234Uniform *ProgramBinary::createUniform(const rx::D3DConstant *constant, const std::string &_name)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002235{
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002236 if (constant->rows == 1) // Vectors and scalars
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002237 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002238 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002239 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002240 case rx::D3DConstant::PT_SAMPLER2D:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002241 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002242 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002243 case 1: return new Uniform(GL_SAMPLER_2D, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002244 default: UNREACHABLE();
2245 }
2246 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002247 case rx::D3DConstant::PT_SAMPLERCUBE:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002248 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002249 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002250 case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002251 default: UNREACHABLE();
2252 }
2253 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002254 case rx::D3DConstant::PT_BOOL:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002255 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002256 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002257 case 1: return new Uniform(GL_BOOL, _name, constant->elements);
2258 case 2: return new Uniform(GL_BOOL_VEC2, _name, constant->elements);
2259 case 3: return new Uniform(GL_BOOL_VEC3, _name, constant->elements);
2260 case 4: return new Uniform(GL_BOOL_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002261 default: UNREACHABLE();
2262 }
2263 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002264 case rx::D3DConstant::PT_INT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002265 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002266 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002267 case 1: return new Uniform(GL_INT, _name, constant->elements);
2268 case 2: return new Uniform(GL_INT_VEC2, _name, constant->elements);
2269 case 3: return new Uniform(GL_INT_VEC3, _name, constant->elements);
2270 case 4: return new Uniform(GL_INT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002271 default: UNREACHABLE();
2272 }
2273 break;
daniel@transgaming.com31240482012-11-28 21:06:41 +00002274 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002275 switch (constant->columns)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002276 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002277 case 1: return new Uniform(GL_FLOAT, _name, constant->elements);
2278 case 2: return new Uniform(GL_FLOAT_VEC2, _name, constant->elements);
2279 case 3: return new Uniform(GL_FLOAT_VEC3, _name, constant->elements);
2280 case 4: return new Uniform(GL_FLOAT_VEC4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002281 default: UNREACHABLE();
2282 }
2283 break;
2284 default:
2285 UNREACHABLE();
2286 }
2287 }
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002288 else if (constant->rows == constant->columns) // Square matrices
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002289 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002290 switch (constant->type)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002291 {
daniel@transgaming.com31240482012-11-28 21:06:41 +00002292 case rx::D3DConstant::PT_FLOAT:
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002293 switch (constant->rows)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002294 {
apatrick@chromium.org60dafe82012-09-05 22:26:10 +00002295 case 2: return new Uniform(GL_FLOAT_MAT2, _name, constant->elements);
2296 case 3: return new Uniform(GL_FLOAT_MAT3, _name, constant->elements);
2297 case 4: return new Uniform(GL_FLOAT_MAT4, _name, constant->elements);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002298 default: UNREACHABLE();
2299 }
2300 break;
2301 default: UNREACHABLE();
2302 }
2303 }
2304 else UNREACHABLE();
2305
2306 return 0;
2307}
2308
2309// This method needs to match OutputHLSL::decorate
2310std::string ProgramBinary::decorateAttribute(const std::string &name)
2311{
2312 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
2313 {
2314 return "_" + name;
2315 }
2316
2317 return name;
2318}
2319
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002320bool ProgramBinary::isValidated() const
2321{
2322 return mValidated;
2323}
2324
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002325void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2326{
2327 // Skip over inactive attributes
2328 unsigned int activeAttribute = 0;
2329 unsigned int attribute;
2330 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2331 {
2332 if (mLinkedAttribute[attribute].name.empty())
2333 {
2334 continue;
2335 }
2336
2337 if (activeAttribute == index)
2338 {
2339 break;
2340 }
2341
2342 activeAttribute++;
2343 }
2344
2345 if (bufsize > 0)
2346 {
2347 const char *string = mLinkedAttribute[attribute].name.c_str();
2348
2349 strncpy(name, string, bufsize);
2350 name[bufsize - 1] = '\0';
2351
2352 if (length)
2353 {
2354 *length = strlen(name);
2355 }
2356 }
2357
2358 *size = 1; // Always a single 'type' instance
2359
2360 *type = mLinkedAttribute[attribute].type;
2361}
2362
2363GLint ProgramBinary::getActiveAttributeCount()
2364{
2365 int count = 0;
2366
2367 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2368 {
2369 if (!mLinkedAttribute[attributeIndex].name.empty())
2370 {
2371 count++;
2372 }
2373 }
2374
2375 return count;
2376}
2377
2378GLint ProgramBinary::getActiveAttributeMaxLength()
2379{
2380 int maxLength = 0;
2381
2382 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
2383 {
2384 if (!mLinkedAttribute[attributeIndex].name.empty())
2385 {
2386 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
2387 }
2388 }
2389
2390 return maxLength;
2391}
2392
2393void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
2394{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002395 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount()
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002396
2397 if (bufsize > 0)
2398 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002399 std::string string = mUniforms[index]->name;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002400
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002401 if (mUniforms[index]->isArray())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002402 {
2403 string += "[0]";
2404 }
2405
2406 strncpy(name, string.c_str(), bufsize);
2407 name[bufsize - 1] = '\0';
2408
2409 if (length)
2410 {
2411 *length = strlen(name);
2412 }
2413 }
2414
daniel@transgaming.come6d12e92012-12-20 21:12:47 +00002415 *size = mUniforms[index]->elementCount();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002416
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002417 *type = mUniforms[index]->type;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002418}
2419
2420GLint ProgramBinary::getActiveUniformCount()
2421{
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002422 return mUniforms.size();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002423}
2424
2425GLint ProgramBinary::getActiveUniformMaxLength()
2426{
2427 int maxLength = 0;
2428
2429 unsigned int numUniforms = mUniforms.size();
2430 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
2431 {
daniel@transgaming.com8320a282012-12-20 21:08:08 +00002432 if (!mUniforms[uniformIndex]->name.empty())
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002433 {
2434 int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
2435 if (mUniforms[uniformIndex]->isArray())
2436 {
2437 length += 3; // Counting in "[0]".
2438 }
2439 maxLength = std::max(length, maxLength);
2440 }
2441 }
2442
2443 return maxLength;
2444}
2445
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002446void ProgramBinary::validate(InfoLog &infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002447{
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002448 applyUniforms();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002449 if (!validateSamplers(&infoLog))
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002450 {
2451 mValidated = false;
2452 }
2453 else
2454 {
2455 mValidated = true;
2456 }
2457}
2458
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002459bool ProgramBinary::validateSamplers(InfoLog *infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002460{
2461 // if any two active samplers in a program are of different types, but refer to the same
2462 // texture image unit, and this is the current program, then ValidateProgram will fail, and
2463 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
2464
2465 const unsigned int maxCombinedTextureImageUnits = getContext()->getMaximumCombinedTextureImageUnits();
2466 TextureType textureUnitType[MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF];
2467
2468 for (unsigned int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; ++i)
2469 {
2470 textureUnitType[i] = TEXTURE_UNKNOWN;
2471 }
2472
2473 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i)
2474 {
2475 if (mSamplersPS[i].active)
2476 {
2477 unsigned int unit = mSamplersPS[i].logicalTextureUnit;
2478
2479 if (unit >= maxCombinedTextureImageUnits)
2480 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002481 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002482 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002483 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002484 }
2485
2486 return false;
2487 }
2488
2489 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2490 {
2491 if (mSamplersPS[i].textureType != textureUnitType[unit])
2492 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002493 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002494 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002495 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002496 }
2497
2498 return false;
2499 }
2500 }
2501 else
2502 {
2503 textureUnitType[unit] = mSamplersPS[i].textureType;
2504 }
2505 }
2506 }
2507
2508 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i)
2509 {
2510 if (mSamplersVS[i].active)
2511 {
2512 unsigned int unit = mSamplersVS[i].logicalTextureUnit;
2513
2514 if (unit >= maxCombinedTextureImageUnits)
2515 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002516 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002517 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002518 infoLog->append("Sampler uniform (%d) exceeds MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002519 }
2520
2521 return false;
2522 }
2523
2524 if (textureUnitType[unit] != TEXTURE_UNKNOWN)
2525 {
2526 if (mSamplersVS[i].textureType != textureUnitType[unit])
2527 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002528 if (infoLog)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002529 {
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00002530 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002531 }
2532
2533 return false;
2534 }
2535 }
2536 else
2537 {
2538 textureUnitType[unit] = mSamplersVS[i].textureType;
2539 }
2540 }
2541 }
2542
2543 return true;
2544}
2545
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002546void ProgramBinary::applyDxDepthRange(float near, float far, float diff)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002547{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002548 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2549 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2550
2551 if (mDxDepthRangeRegisterVS >= 0)
2552 {
2553 float nearFarDiff[4] = {near, far, diff};
2554 device->SetVertexShaderConstantF(mDxDepthRangeRegisterVS, nearFarDiff, 1);
2555 }
2556
2557 if (mDxDepthRangeRegisterPS >= 0)
2558 {
2559 float nearFarDiff[4] = {near, far, diff};
2560 device->SetPixelShaderConstantF(mDxDepthRangeRegisterPS, nearFarDiff, 1);
2561 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002562}
2563
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002564void ProgramBinary::applyDxDepthFront(float range, float start, float frontCCW)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002565{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002566 if (mDxDepthFrontRegister >= 0)
2567 {
2568 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2569 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2570
2571 GLfloat dz[4] = {range, start, frontCCW};
2572 device->SetPixelShaderConstantF(mDxDepthFrontRegister, dz, 1);
2573 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002574}
2575
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002576void ProgramBinary::applyDxCoord(float halfWidth, float halfHeight, float x0, float y0)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002577{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002578 if (mDxCoordRegister >= 0)
2579 {
2580 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2581 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2582
2583 GLfloat whxy[4] = {halfWidth,halfHeight, x0, y0};
2584 device->SetPixelShaderConstantF(mDxCoordRegister, whxy, 1);
2585 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002586}
2587
daniel@transgaming.com88853c52012-12-20 20:56:40 +00002588void ProgramBinary::applyDxHalfPixelSize(float width, float height)
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002589{
daniel@transgaming.com593ebc42012-12-20 20:56:46 +00002590 if (mDxHalfPixelSizeRegister >= 0)
2591 {
2592 if (dynamic_cast<rx::Renderer9*>(mRenderer) == NULL) return; // UNIMPLEMENTED
2593 IDirect3DDevice9 *device = rx::Renderer9::makeRenderer9(mRenderer)->getDevice(); // D3D9_REPLACE
2594
2595 GLfloat xy[4] = {width, height};
2596 device->SetVertexShaderConstantF(mDxHalfPixelSizeRegister, xy, 1);
2597 }
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002598}
2599
apatrick@chromium.org90080e32012-07-09 22:15:33 +00002600ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D)
2601{
2602}
2603
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00002604}