|
1 #include "precompiled.h" |
|
2 // |
|
3 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
|
4 // Use of this source code is governed by a BSD-style license that can be |
|
5 // found in the LICENSE file. |
|
6 // |
|
7 |
|
8 // Program.cpp: Implements the gl::Program class. Implements GL program objects |
|
9 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. |
|
10 |
|
11 #include "libGLESv2/BinaryStream.h" |
|
12 #include "libGLESv2/ProgramBinary.h" |
|
13 #include "libGLESv2/renderer/ShaderExecutable.h" |
|
14 |
|
15 #include "common/debug.h" |
|
16 #include "common/version.h" |
|
17 #include "utilities.h" |
|
18 |
|
19 #include "libGLESv2/main.h" |
|
20 #include "libGLESv2/Shader.h" |
|
21 #include "libGLESv2/Program.h" |
|
22 #include "libGLESv2/renderer/Renderer.h" |
|
23 #include "libGLESv2/renderer/VertexDataManager.h" |
|
24 |
|
25 #include <algorithm> |
|
26 |
|
27 #undef near |
|
28 #undef far |
|
29 |
|
30 namespace gl |
|
31 { |
|
32 std::string str(int i) |
|
33 { |
|
34 char buffer[20]; |
|
35 snprintf(buffer, sizeof(buffer), "%d", i); |
|
36 return buffer; |
|
37 } |
|
38 |
|
39 UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index) |
|
40 : name(name), element(element), index(index) |
|
41 { |
|
42 } |
|
43 |
|
44 unsigned int ProgramBinary::mCurrentSerial = 1; |
|
45 |
|
46 ProgramBinary::ProgramBinary(rx::Renderer *renderer) : mRenderer(renderer), RefCountObject(0), mSerial(issueSerial()) |
|
47 { |
|
48 mPixelExecutable = NULL; |
|
49 mVertexExecutable = NULL; |
|
50 mGeometryExecutable = NULL; |
|
51 |
|
52 mValidated = false; |
|
53 |
|
54 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
|
55 { |
|
56 mSemanticIndex[index] = -1; |
|
57 } |
|
58 |
|
59 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++) |
|
60 { |
|
61 mSamplersPS[index].active = false; |
|
62 } |
|
63 |
|
64 for (int index = 0; index < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; index++) |
|
65 { |
|
66 mSamplersVS[index].active = false; |
|
67 } |
|
68 |
|
69 mUsedVertexSamplerRange = 0; |
|
70 mUsedPixelSamplerRange = 0; |
|
71 mUsesPointSize = false; |
|
72 } |
|
73 |
|
74 ProgramBinary::~ProgramBinary() |
|
75 { |
|
76 delete mPixelExecutable; |
|
77 mPixelExecutable = NULL; |
|
78 |
|
79 delete mVertexExecutable; |
|
80 mVertexExecutable = NULL; |
|
81 |
|
82 delete mGeometryExecutable; |
|
83 mGeometryExecutable = NULL; |
|
84 |
|
85 while (!mUniforms.empty()) |
|
86 { |
|
87 delete mUniforms.back(); |
|
88 mUniforms.pop_back(); |
|
89 } |
|
90 } |
|
91 |
|
92 unsigned int ProgramBinary::getSerial() const |
|
93 { |
|
94 return mSerial; |
|
95 } |
|
96 |
|
97 unsigned int ProgramBinary::issueSerial() |
|
98 { |
|
99 return mCurrentSerial++; |
|
100 } |
|
101 |
|
102 rx::ShaderExecutable *ProgramBinary::getPixelExecutable() |
|
103 { |
|
104 return mPixelExecutable; |
|
105 } |
|
106 |
|
107 rx::ShaderExecutable *ProgramBinary::getVertexExecutable() |
|
108 { |
|
109 return mVertexExecutable; |
|
110 } |
|
111 |
|
112 rx::ShaderExecutable *ProgramBinary::getGeometryExecutable() |
|
113 { |
|
114 return mGeometryExecutable; |
|
115 } |
|
116 |
|
117 GLuint ProgramBinary::getAttributeLocation(const char *name) |
|
118 { |
|
119 if (name) |
|
120 { |
|
121 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
|
122 { |
|
123 if (mLinkedAttribute[index].name == std::string(name)) |
|
124 { |
|
125 return index; |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 return -1; |
|
131 } |
|
132 |
|
133 int ProgramBinary::getSemanticIndex(int attributeIndex) |
|
134 { |
|
135 ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS); |
|
136 |
|
137 return mSemanticIndex[attributeIndex]; |
|
138 } |
|
139 |
|
140 // Returns one more than the highest sampler index used. |
|
141 GLint ProgramBinary::getUsedSamplerRange(SamplerType type) |
|
142 { |
|
143 switch (type) |
|
144 { |
|
145 case SAMPLER_PIXEL: |
|
146 return mUsedPixelSamplerRange; |
|
147 case SAMPLER_VERTEX: |
|
148 return mUsedVertexSamplerRange; |
|
149 default: |
|
150 UNREACHABLE(); |
|
151 return 0; |
|
152 } |
|
153 } |
|
154 |
|
155 bool ProgramBinary::usesPointSize() const |
|
156 { |
|
157 return mUsesPointSize; |
|
158 } |
|
159 |
|
160 bool ProgramBinary::usesPointSpriteEmulation() const |
|
161 { |
|
162 return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4; |
|
163 } |
|
164 |
|
165 bool ProgramBinary::usesGeometryShader() const |
|
166 { |
|
167 return usesPointSpriteEmulation(); |
|
168 } |
|
169 |
|
170 // Returns the index of the texture image unit (0-19) corresponding to a Direct3D 9 sampler |
|
171 // index (0-15 for the pixel shader and 0-3 for the vertex shader). |
|
172 GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex) |
|
173 { |
|
174 GLint logicalTextureUnit = -1; |
|
175 |
|
176 switch (type) |
|
177 { |
|
178 case SAMPLER_PIXEL: |
|
179 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0])); |
|
180 |
|
181 if (mSamplersPS[samplerIndex].active) |
|
182 { |
|
183 logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit; |
|
184 } |
|
185 break; |
|
186 case SAMPLER_VERTEX: |
|
187 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0])); |
|
188 |
|
189 if (mSamplersVS[samplerIndex].active) |
|
190 { |
|
191 logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit; |
|
192 } |
|
193 break; |
|
194 default: UNREACHABLE(); |
|
195 } |
|
196 |
|
197 if (logicalTextureUnit >= 0 && logicalTextureUnit < (GLint)mRenderer->getMaxCombinedTextureImageUnits()) |
|
198 { |
|
199 return logicalTextureUnit; |
|
200 } |
|
201 |
|
202 return -1; |
|
203 } |
|
204 |
|
205 // Returns the texture type for a given Direct3D 9 sampler type and |
|
206 // index (0-15 for the pixel shader and 0-3 for the vertex shader). |
|
207 TextureType ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex) |
|
208 { |
|
209 switch (type) |
|
210 { |
|
211 case SAMPLER_PIXEL: |
|
212 ASSERT(samplerIndex < sizeof(mSamplersPS)/sizeof(mSamplersPS[0])); |
|
213 ASSERT(mSamplersPS[samplerIndex].active); |
|
214 return mSamplersPS[samplerIndex].textureType; |
|
215 case SAMPLER_VERTEX: |
|
216 ASSERT(samplerIndex < sizeof(mSamplersVS)/sizeof(mSamplersVS[0])); |
|
217 ASSERT(mSamplersVS[samplerIndex].active); |
|
218 return mSamplersVS[samplerIndex].textureType; |
|
219 default: UNREACHABLE(); |
|
220 } |
|
221 |
|
222 return TEXTURE_2D; |
|
223 } |
|
224 |
|
225 GLint ProgramBinary::getUniformLocation(std::string name) |
|
226 { |
|
227 unsigned int subscript = 0; |
|
228 |
|
229 // Strip any trailing array operator and retrieve the subscript |
|
230 size_t open = name.find_last_of('['); |
|
231 size_t close = name.find_last_of(']'); |
|
232 if (open != std::string::npos && close == name.length() - 1) |
|
233 { |
|
234 subscript = atoi(name.substr(open + 1).c_str()); |
|
235 name.erase(open); |
|
236 } |
|
237 |
|
238 unsigned int numUniforms = mUniformIndex.size(); |
|
239 for (unsigned int location = 0; location < numUniforms; location++) |
|
240 { |
|
241 if (mUniformIndex[location].name == name && |
|
242 mUniformIndex[location].element == subscript) |
|
243 { |
|
244 return location; |
|
245 } |
|
246 } |
|
247 |
|
248 return -1; |
|
249 } |
|
250 |
|
251 bool ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) |
|
252 { |
|
253 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
254 { |
|
255 return false; |
|
256 } |
|
257 |
|
258 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
259 targetUniform->dirty = true; |
|
260 |
|
261 int elementCount = targetUniform->elementCount(); |
|
262 |
|
263 if (elementCount == 1 && count > 1) |
|
264 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
265 |
|
266 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
267 |
|
268 if (targetUniform->type == GL_FLOAT) |
|
269 { |
|
270 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
|
271 |
|
272 for (int i = 0; i < count; i++) |
|
273 { |
|
274 target[0] = v[0]; |
|
275 target[1] = 0; |
|
276 target[2] = 0; |
|
277 target[3] = 0; |
|
278 target += 4; |
|
279 v += 1; |
|
280 } |
|
281 } |
|
282 else if (targetUniform->type == GL_BOOL) |
|
283 { |
|
284 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
285 |
|
286 for (int i = 0; i < count; i++) |
|
287 { |
|
288 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
289 boolParams[1] = GL_FALSE; |
|
290 boolParams[2] = GL_FALSE; |
|
291 boolParams[3] = GL_FALSE; |
|
292 boolParams += 4; |
|
293 v += 1; |
|
294 } |
|
295 } |
|
296 else |
|
297 { |
|
298 return false; |
|
299 } |
|
300 |
|
301 return true; |
|
302 } |
|
303 |
|
304 bool ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
|
305 { |
|
306 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
307 { |
|
308 return false; |
|
309 } |
|
310 |
|
311 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
312 targetUniform->dirty = true; |
|
313 |
|
314 int elementCount = targetUniform->elementCount(); |
|
315 |
|
316 if (elementCount == 1 && count > 1) |
|
317 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
318 |
|
319 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
320 |
|
321 if (targetUniform->type == GL_FLOAT_VEC2) |
|
322 { |
|
323 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
|
324 |
|
325 for (int i = 0; i < count; i++) |
|
326 { |
|
327 target[0] = v[0]; |
|
328 target[1] = v[1]; |
|
329 target[2] = 0; |
|
330 target[3] = 0; |
|
331 target += 4; |
|
332 v += 2; |
|
333 } |
|
334 } |
|
335 else if (targetUniform->type == GL_BOOL_VEC2) |
|
336 { |
|
337 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
338 |
|
339 for (int i = 0; i < count; i++) |
|
340 { |
|
341 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
342 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
343 boolParams[2] = GL_FALSE; |
|
344 boolParams[3] = GL_FALSE; |
|
345 boolParams += 4; |
|
346 v += 2; |
|
347 } |
|
348 } |
|
349 else |
|
350 { |
|
351 return false; |
|
352 } |
|
353 |
|
354 return true; |
|
355 } |
|
356 |
|
357 bool ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
|
358 { |
|
359 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
360 { |
|
361 return false; |
|
362 } |
|
363 |
|
364 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
365 targetUniform->dirty = true; |
|
366 |
|
367 int elementCount = targetUniform->elementCount(); |
|
368 |
|
369 if (elementCount == 1 && count > 1) |
|
370 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
371 |
|
372 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
373 |
|
374 if (targetUniform->type == GL_FLOAT_VEC3) |
|
375 { |
|
376 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
|
377 |
|
378 for (int i = 0; i < count; i++) |
|
379 { |
|
380 target[0] = v[0]; |
|
381 target[1] = v[1]; |
|
382 target[2] = v[2]; |
|
383 target[3] = 0; |
|
384 target += 4; |
|
385 v += 3; |
|
386 } |
|
387 } |
|
388 else if (targetUniform->type == GL_BOOL_VEC3) |
|
389 { |
|
390 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
391 |
|
392 for (int i = 0; i < count; i++) |
|
393 { |
|
394 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
395 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
396 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
397 boolParams[3] = GL_FALSE; |
|
398 boolParams += 4; |
|
399 v += 3; |
|
400 } |
|
401 } |
|
402 else |
|
403 { |
|
404 return false; |
|
405 } |
|
406 |
|
407 return true; |
|
408 } |
|
409 |
|
410 bool ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
|
411 { |
|
412 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
413 { |
|
414 return false; |
|
415 } |
|
416 |
|
417 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
418 targetUniform->dirty = true; |
|
419 |
|
420 int elementCount = targetUniform->elementCount(); |
|
421 |
|
422 if (elementCount == 1 && count > 1) |
|
423 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
424 |
|
425 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
426 |
|
427 if (targetUniform->type == GL_FLOAT_VEC4) |
|
428 { |
|
429 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
|
430 |
|
431 for (int i = 0; i < count; i++) |
|
432 { |
|
433 target[0] = v[0]; |
|
434 target[1] = v[1]; |
|
435 target[2] = v[2]; |
|
436 target[3] = v[3]; |
|
437 target += 4; |
|
438 v += 4; |
|
439 } |
|
440 } |
|
441 else if (targetUniform->type == GL_BOOL_VEC4) |
|
442 { |
|
443 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
444 |
|
445 for (int i = 0; i < count; i++) |
|
446 { |
|
447 boolParams[0] = (v[0] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
448 boolParams[1] = (v[1] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
449 boolParams[2] = (v[2] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
450 boolParams[3] = (v[3] == 0.0f) ? GL_FALSE : GL_TRUE; |
|
451 boolParams += 4; |
|
452 v += 4; |
|
453 } |
|
454 } |
|
455 else |
|
456 { |
|
457 return false; |
|
458 } |
|
459 |
|
460 return true; |
|
461 } |
|
462 |
|
463 template<typename T, int targetWidth, int targetHeight, int srcWidth, int srcHeight> |
|
464 void transposeMatrix(T *target, const GLfloat *value) |
|
465 { |
|
466 int copyWidth = std::min(targetWidth, srcWidth); |
|
467 int copyHeight = std::min(targetHeight, srcHeight); |
|
468 |
|
469 for (int x = 0; x < copyWidth; x++) |
|
470 { |
|
471 for (int y = 0; y < copyHeight; y++) |
|
472 { |
|
473 target[x * targetWidth + y] = (T)value[y * srcWidth + x]; |
|
474 } |
|
475 } |
|
476 // clear unfilled right side |
|
477 for (int y = 0; y < copyHeight; y++) |
|
478 { |
|
479 for (int x = srcWidth; x < targetWidth; x++) |
|
480 { |
|
481 target[y * targetWidth + x] = (T)0; |
|
482 } |
|
483 } |
|
484 // clear unfilled bottom. |
|
485 for (int y = srcHeight; y < targetHeight; y++) |
|
486 { |
|
487 for (int x = 0; x < targetWidth; x++) |
|
488 { |
|
489 target[y * targetWidth + x] = (T)0; |
|
490 } |
|
491 } |
|
492 } |
|
493 |
|
494 bool ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value) |
|
495 { |
|
496 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
497 { |
|
498 return false; |
|
499 } |
|
500 |
|
501 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
502 targetUniform->dirty = true; |
|
503 |
|
504 if (targetUniform->type != GL_FLOAT_MAT2) |
|
505 { |
|
506 return false; |
|
507 } |
|
508 |
|
509 int elementCount = targetUniform->elementCount(); |
|
510 |
|
511 if (elementCount == 1 && count > 1) |
|
512 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
513 |
|
514 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
515 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8; |
|
516 |
|
517 for (int i = 0; i < count; i++) |
|
518 { |
|
519 transposeMatrix<GLfloat,4,2,2,2>(target, value); |
|
520 target += 8; |
|
521 value += 4; |
|
522 } |
|
523 |
|
524 return true; |
|
525 } |
|
526 |
|
527 bool ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value) |
|
528 { |
|
529 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
530 { |
|
531 return false; |
|
532 } |
|
533 |
|
534 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
535 targetUniform->dirty = true; |
|
536 |
|
537 if (targetUniform->type != GL_FLOAT_MAT3) |
|
538 { |
|
539 return false; |
|
540 } |
|
541 |
|
542 int elementCount = targetUniform->elementCount(); |
|
543 |
|
544 if (elementCount == 1 && count > 1) |
|
545 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
546 |
|
547 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
548 GLfloat *target = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12; |
|
549 |
|
550 for (int i = 0; i < count; i++) |
|
551 { |
|
552 transposeMatrix<GLfloat,4,3,3,3>(target, value); |
|
553 target += 12; |
|
554 value += 9; |
|
555 } |
|
556 |
|
557 return true; |
|
558 } |
|
559 |
|
560 |
|
561 bool ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value) |
|
562 { |
|
563 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
564 { |
|
565 return false; |
|
566 } |
|
567 |
|
568 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
569 targetUniform->dirty = true; |
|
570 |
|
571 if (targetUniform->type != GL_FLOAT_MAT4) |
|
572 { |
|
573 return false; |
|
574 } |
|
575 |
|
576 int elementCount = targetUniform->elementCount(); |
|
577 |
|
578 if (elementCount == 1 && count > 1) |
|
579 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
580 |
|
581 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
582 GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * 16); |
|
583 |
|
584 for (int i = 0; i < count; i++) |
|
585 { |
|
586 transposeMatrix<GLfloat,4,4,4,4>(target, value); |
|
587 target += 16; |
|
588 value += 16; |
|
589 } |
|
590 |
|
591 return true; |
|
592 } |
|
593 |
|
594 bool ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
|
595 { |
|
596 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
597 { |
|
598 return false; |
|
599 } |
|
600 |
|
601 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
602 targetUniform->dirty = true; |
|
603 |
|
604 int elementCount = targetUniform->elementCount(); |
|
605 |
|
606 if (elementCount == 1 && count > 1) |
|
607 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
608 |
|
609 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
610 |
|
611 if (targetUniform->type == GL_INT || |
|
612 targetUniform->type == GL_SAMPLER_2D || |
|
613 targetUniform->type == GL_SAMPLER_CUBE) |
|
614 { |
|
615 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
616 |
|
617 for (int i = 0; i < count; i++) |
|
618 { |
|
619 target[0] = v[0]; |
|
620 target[1] = 0; |
|
621 target[2] = 0; |
|
622 target[3] = 0; |
|
623 target += 4; |
|
624 v += 1; |
|
625 } |
|
626 } |
|
627 else if (targetUniform->type == GL_BOOL) |
|
628 { |
|
629 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
630 |
|
631 for (int i = 0; i < count; i++) |
|
632 { |
|
633 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE; |
|
634 boolParams[1] = GL_FALSE; |
|
635 boolParams[2] = GL_FALSE; |
|
636 boolParams[3] = GL_FALSE; |
|
637 boolParams += 4; |
|
638 v += 1; |
|
639 } |
|
640 } |
|
641 else |
|
642 { |
|
643 return false; |
|
644 } |
|
645 |
|
646 return true; |
|
647 } |
|
648 |
|
649 bool 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 int elementCount = targetUniform->elementCount(); |
|
660 |
|
661 if (elementCount == 1 && count > 1) |
|
662 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
663 |
|
664 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
665 |
|
666 if (targetUniform->type == GL_INT_VEC2) |
|
667 { |
|
668 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
669 |
|
670 for (int i = 0; i < count; i++) |
|
671 { |
|
672 target[0] = v[0]; |
|
673 target[1] = v[1]; |
|
674 target[2] = 0; |
|
675 target[3] = 0; |
|
676 target += 4; |
|
677 v += 2; |
|
678 } |
|
679 } |
|
680 else if (targetUniform->type == GL_BOOL_VEC2) |
|
681 { |
|
682 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
683 |
|
684 for (int i = 0; i < count; i++) |
|
685 { |
|
686 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE; |
|
687 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE; |
|
688 boolParams[2] = GL_FALSE; |
|
689 boolParams[3] = GL_FALSE; |
|
690 boolParams += 4; |
|
691 v += 2; |
|
692 } |
|
693 } |
|
694 else |
|
695 { |
|
696 return false; |
|
697 } |
|
698 |
|
699 return true; |
|
700 } |
|
701 |
|
702 bool ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
|
703 { |
|
704 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
705 { |
|
706 return false; |
|
707 } |
|
708 |
|
709 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
710 targetUniform->dirty = true; |
|
711 |
|
712 int elementCount = targetUniform->elementCount(); |
|
713 |
|
714 if (elementCount == 1 && count > 1) |
|
715 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
716 |
|
717 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
718 |
|
719 if (targetUniform->type == GL_INT_VEC3) |
|
720 { |
|
721 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
722 |
|
723 for (int i = 0; i < count; i++) |
|
724 { |
|
725 target[0] = v[0]; |
|
726 target[1] = v[1]; |
|
727 target[2] = v[2]; |
|
728 target[3] = 0; |
|
729 target += 4; |
|
730 v += 3; |
|
731 } |
|
732 } |
|
733 else if (targetUniform->type == GL_BOOL_VEC3) |
|
734 { |
|
735 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
736 |
|
737 for (int i = 0; i < count; i++) |
|
738 { |
|
739 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE; |
|
740 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE; |
|
741 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE; |
|
742 boolParams[3] = GL_FALSE; |
|
743 boolParams += 4; |
|
744 v += 3; |
|
745 } |
|
746 } |
|
747 else |
|
748 { |
|
749 return false; |
|
750 } |
|
751 |
|
752 return true; |
|
753 } |
|
754 |
|
755 bool ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
|
756 { |
|
757 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
758 { |
|
759 return false; |
|
760 } |
|
761 |
|
762 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
763 targetUniform->dirty = true; |
|
764 |
|
765 int elementCount = targetUniform->elementCount(); |
|
766 |
|
767 if (elementCount == 1 && count > 1) |
|
768 return false; // attempting to write an array to a non-array uniform is an INVALID_OPERATION |
|
769 |
|
770 count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
|
771 |
|
772 if (targetUniform->type == GL_INT_VEC4) |
|
773 { |
|
774 GLint *target = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
775 |
|
776 for (int i = 0; i < count; i++) |
|
777 { |
|
778 target[0] = v[0]; |
|
779 target[1] = v[1]; |
|
780 target[2] = v[2]; |
|
781 target[3] = v[3]; |
|
782 target += 4; |
|
783 v += 4; |
|
784 } |
|
785 } |
|
786 else if (targetUniform->type == GL_BOOL_VEC4) |
|
787 { |
|
788 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
789 |
|
790 for (int i = 0; i < count; i++) |
|
791 { |
|
792 boolParams[0] = (v[0] == 0) ? GL_FALSE : GL_TRUE; |
|
793 boolParams[1] = (v[1] == 0) ? GL_FALSE : GL_TRUE; |
|
794 boolParams[2] = (v[2] == 0) ? GL_FALSE : GL_TRUE; |
|
795 boolParams[3] = (v[3] == 0) ? GL_FALSE : GL_TRUE; |
|
796 boolParams += 4; |
|
797 v += 4; |
|
798 } |
|
799 } |
|
800 else |
|
801 { |
|
802 return false; |
|
803 } |
|
804 |
|
805 return true; |
|
806 } |
|
807 |
|
808 bool ProgramBinary::getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params) |
|
809 { |
|
810 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
811 { |
|
812 return false; |
|
813 } |
|
814 |
|
815 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
816 |
|
817 // sized queries -- ensure the provided buffer is large enough |
|
818 if (bufSize) |
|
819 { |
|
820 int requiredBytes = UniformExternalSize(targetUniform->type); |
|
821 if (*bufSize < requiredBytes) |
|
822 { |
|
823 return false; |
|
824 } |
|
825 } |
|
826 |
|
827 switch (targetUniform->type) |
|
828 { |
|
829 case GL_FLOAT_MAT2: |
|
830 transposeMatrix<GLfloat,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8); |
|
831 break; |
|
832 case GL_FLOAT_MAT3: |
|
833 transposeMatrix<GLfloat,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12); |
|
834 break; |
|
835 case GL_FLOAT_MAT4: |
|
836 transposeMatrix<GLfloat,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16); |
|
837 break; |
|
838 default: |
|
839 { |
|
840 unsigned int size = UniformComponentCount(targetUniform->type); |
|
841 |
|
842 switch (UniformComponentType(targetUniform->type)) |
|
843 { |
|
844 case GL_BOOL: |
|
845 { |
|
846 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
847 |
|
848 for (unsigned int i = 0; i < size; i++) |
|
849 { |
|
850 params[i] = (boolParams[i] == GL_FALSE) ? 0.0f : 1.0f; |
|
851 } |
|
852 } |
|
853 break; |
|
854 case GL_FLOAT: |
|
855 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLfloat), |
|
856 size * sizeof(GLfloat)); |
|
857 break; |
|
858 case GL_INT: |
|
859 { |
|
860 GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
861 |
|
862 for (unsigned int i = 0; i < size; i++) |
|
863 { |
|
864 params[i] = (float)intParams[i]; |
|
865 } |
|
866 } |
|
867 break; |
|
868 default: UNREACHABLE(); |
|
869 } |
|
870 } |
|
871 } |
|
872 |
|
873 return true; |
|
874 } |
|
875 |
|
876 bool ProgramBinary::getUniformiv(GLint location, GLsizei *bufSize, GLint *params) |
|
877 { |
|
878 if (location < 0 || location >= (int)mUniformIndex.size()) |
|
879 { |
|
880 return false; |
|
881 } |
|
882 |
|
883 Uniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
|
884 |
|
885 // sized queries -- ensure the provided buffer is large enough |
|
886 if (bufSize) |
|
887 { |
|
888 int requiredBytes = UniformExternalSize(targetUniform->type); |
|
889 if (*bufSize < requiredBytes) |
|
890 { |
|
891 return false; |
|
892 } |
|
893 } |
|
894 |
|
895 switch (targetUniform->type) |
|
896 { |
|
897 case GL_FLOAT_MAT2: |
|
898 transposeMatrix<GLint,2,2,4,2>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 8); |
|
899 break; |
|
900 case GL_FLOAT_MAT3: |
|
901 transposeMatrix<GLint,3,3,4,3>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 12); |
|
902 break; |
|
903 case GL_FLOAT_MAT4: |
|
904 transposeMatrix<GLint,4,4,4,4>(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 16); |
|
905 break; |
|
906 default: |
|
907 { |
|
908 unsigned int size = VariableColumnCount(targetUniform->type); |
|
909 |
|
910 switch (UniformComponentType(targetUniform->type)) |
|
911 { |
|
912 case GL_BOOL: |
|
913 { |
|
914 GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
|
915 |
|
916 for (unsigned int i = 0; i < size; i++) |
|
917 { |
|
918 params[i] = boolParams[i]; |
|
919 } |
|
920 } |
|
921 break; |
|
922 case GL_FLOAT: |
|
923 { |
|
924 GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
|
925 |
|
926 for (unsigned int i = 0; i < size; i++) |
|
927 { |
|
928 params[i] = (GLint)floatParams[i]; |
|
929 } |
|
930 } |
|
931 break; |
|
932 case GL_INT: |
|
933 memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(GLint), |
|
934 size * sizeof(GLint)); |
|
935 break; |
|
936 default: UNREACHABLE(); |
|
937 } |
|
938 } |
|
939 } |
|
940 |
|
941 return true; |
|
942 } |
|
943 |
|
944 void ProgramBinary::dirtyAllUniforms() |
|
945 { |
|
946 unsigned int numUniforms = mUniforms.size(); |
|
947 for (unsigned int index = 0; index < numUniforms; index++) |
|
948 { |
|
949 mUniforms[index]->dirty = true; |
|
950 } |
|
951 } |
|
952 |
|
953 // Applies all the uniforms set for this program object to the renderer |
|
954 void ProgramBinary::applyUniforms() |
|
955 { |
|
956 // Retrieve sampler uniform values |
|
957 for (std::vector<Uniform*>::iterator ub = mUniforms.begin(), ue = mUniforms.end(); ub != ue; ++ub) |
|
958 { |
|
959 Uniform *targetUniform = *ub; |
|
960 |
|
961 if (targetUniform->dirty) |
|
962 { |
|
963 if (targetUniform->type == GL_SAMPLER_2D || |
|
964 targetUniform->type == GL_SAMPLER_CUBE) |
|
965 { |
|
966 int count = targetUniform->elementCount(); |
|
967 GLint (*v)[4] = (GLint(*)[4])targetUniform->data; |
|
968 |
|
969 if (targetUniform->psRegisterIndex >= 0) |
|
970 { |
|
971 unsigned int firstIndex = targetUniform->psRegisterIndex; |
|
972 |
|
973 for (int i = 0; i < count; i++) |
|
974 { |
|
975 unsigned int samplerIndex = firstIndex + i; |
|
976 |
|
977 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS) |
|
978 { |
|
979 ASSERT(mSamplersPS[samplerIndex].active); |
|
980 mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0]; |
|
981 } |
|
982 } |
|
983 } |
|
984 |
|
985 if (targetUniform->vsRegisterIndex >= 0) |
|
986 { |
|
987 unsigned int firstIndex = targetUniform->vsRegisterIndex; |
|
988 |
|
989 for (int i = 0; i < count; i++) |
|
990 { |
|
991 unsigned int samplerIndex = firstIndex + i; |
|
992 |
|
993 if (samplerIndex < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS) |
|
994 { |
|
995 ASSERT(mSamplersVS[samplerIndex].active); |
|
996 mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0]; |
|
997 } |
|
998 } |
|
999 } |
|
1000 } |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 mRenderer->applyUniforms(this, &mUniforms); |
|
1005 } |
|
1006 |
|
1007 // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111 |
|
1008 // Returns the number of used varying registers, or -1 if unsuccesful |
|
1009 int ProgramBinary::packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader) |
|
1010 { |
|
1011 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors(); |
|
1012 |
|
1013 fragmentShader->resetVaryingsRegisterAssignment(); |
|
1014 |
|
1015 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++) |
|
1016 { |
|
1017 int n = VariableRowCount(varying->type) * varying->size; |
|
1018 int m = VariableColumnCount(varying->type); |
|
1019 bool success = false; |
|
1020 |
|
1021 if (m == 2 || m == 3 || m == 4) |
|
1022 { |
|
1023 for (int r = 0; r <= maxVaryingVectors - n && !success; r++) |
|
1024 { |
|
1025 bool available = true; |
|
1026 |
|
1027 for (int y = 0; y < n && available; y++) |
|
1028 { |
|
1029 for (int x = 0; x < m && available; x++) |
|
1030 { |
|
1031 if (packing[r + y][x]) |
|
1032 { |
|
1033 available = false; |
|
1034 } |
|
1035 } |
|
1036 } |
|
1037 |
|
1038 if (available) |
|
1039 { |
|
1040 varying->reg = r; |
|
1041 varying->col = 0; |
|
1042 |
|
1043 for (int y = 0; y < n; y++) |
|
1044 { |
|
1045 for (int x = 0; x < m; x++) |
|
1046 { |
|
1047 packing[r + y][x] = &*varying; |
|
1048 } |
|
1049 } |
|
1050 |
|
1051 success = true; |
|
1052 } |
|
1053 } |
|
1054 |
|
1055 if (!success && m == 2) |
|
1056 { |
|
1057 for (int r = maxVaryingVectors - n; r >= 0 && !success; r--) |
|
1058 { |
|
1059 bool available = true; |
|
1060 |
|
1061 for (int y = 0; y < n && available; y++) |
|
1062 { |
|
1063 for (int x = 2; x < 4 && available; x++) |
|
1064 { |
|
1065 if (packing[r + y][x]) |
|
1066 { |
|
1067 available = false; |
|
1068 } |
|
1069 } |
|
1070 } |
|
1071 |
|
1072 if (available) |
|
1073 { |
|
1074 varying->reg = r; |
|
1075 varying->col = 2; |
|
1076 |
|
1077 for (int y = 0; y < n; y++) |
|
1078 { |
|
1079 for (int x = 2; x < 4; x++) |
|
1080 { |
|
1081 packing[r + y][x] = &*varying; |
|
1082 } |
|
1083 } |
|
1084 |
|
1085 success = true; |
|
1086 } |
|
1087 } |
|
1088 } |
|
1089 } |
|
1090 else if (m == 1) |
|
1091 { |
|
1092 int space[4] = {0}; |
|
1093 |
|
1094 for (int y = 0; y < maxVaryingVectors; y++) |
|
1095 { |
|
1096 for (int x = 0; x < 4; x++) |
|
1097 { |
|
1098 space[x] += packing[y][x] ? 0 : 1; |
|
1099 } |
|
1100 } |
|
1101 |
|
1102 int column = 0; |
|
1103 |
|
1104 for (int x = 0; x < 4; x++) |
|
1105 { |
|
1106 if (space[x] >= n && space[x] < space[column]) |
|
1107 { |
|
1108 column = x; |
|
1109 } |
|
1110 } |
|
1111 |
|
1112 if (space[column] >= n) |
|
1113 { |
|
1114 for (int r = 0; r < maxVaryingVectors; r++) |
|
1115 { |
|
1116 if (!packing[r][column]) |
|
1117 { |
|
1118 varying->reg = r; |
|
1119 |
|
1120 for (int y = r; y < r + n; y++) |
|
1121 { |
|
1122 packing[y][column] = &*varying; |
|
1123 } |
|
1124 |
|
1125 break; |
|
1126 } |
|
1127 } |
|
1128 |
|
1129 varying->col = column; |
|
1130 |
|
1131 success = true; |
|
1132 } |
|
1133 } |
|
1134 else UNREACHABLE(); |
|
1135 |
|
1136 if (!success) |
|
1137 { |
|
1138 infoLog.append("Could not pack varying %s", varying->name.c_str()); |
|
1139 |
|
1140 return -1; |
|
1141 } |
|
1142 } |
|
1143 |
|
1144 // Return the number of used registers |
|
1145 int registers = 0; |
|
1146 |
|
1147 for (int r = 0; r < maxVaryingVectors; r++) |
|
1148 { |
|
1149 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3]) |
|
1150 { |
|
1151 registers++; |
|
1152 } |
|
1153 } |
|
1154 |
|
1155 return registers; |
|
1156 } |
|
1157 |
|
1158 bool ProgramBinary::linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4], |
|
1159 std::string& pixelHLSL, std::string& vertexHLSL, |
|
1160 FragmentShader *fragmentShader, VertexShader *vertexShader) |
|
1161 { |
|
1162 if (pixelHLSL.empty() || vertexHLSL.empty()) |
|
1163 { |
|
1164 return false; |
|
1165 } |
|
1166 |
|
1167 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets; |
|
1168 bool usesFragColor = fragmentShader->mUsesFragColor; |
|
1169 bool usesFragData = fragmentShader->mUsesFragData; |
|
1170 if (usesFragColor && usesFragData) |
|
1171 { |
|
1172 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader."); |
|
1173 return false; |
|
1174 } |
|
1175 |
|
1176 // Write the HLSL input/output declarations |
|
1177 const int shaderModel = mRenderer->getMajorShaderModel(); |
|
1178 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors(); |
|
1179 |
|
1180 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0); |
|
1181 |
|
1182 // The output color is broadcast to all enabled draw buffers when writing to gl_FragColor |
|
1183 const bool broadcast = fragmentShader->mUsesFragColor; |
|
1184 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1); |
|
1185 |
|
1186 if (registersNeeded > maxVaryingVectors) |
|
1187 { |
|
1188 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord"); |
|
1189 |
|
1190 return false; |
|
1191 } |
|
1192 |
|
1193 vertexShader->resetVaryingsRegisterAssignment(); |
|
1194 |
|
1195 for (VaryingList::iterator input = fragmentShader->mVaryings.begin(); input != fragmentShader->mVaryings.end(); input++) |
|
1196 { |
|
1197 bool matched = false; |
|
1198 |
|
1199 for (VaryingList::iterator output = vertexShader->mVaryings.begin(); output != vertexShader->mVaryings.end(); output++) |
|
1200 { |
|
1201 if (output->name == input->name) |
|
1202 { |
|
1203 if (output->type != input->type || output->size != input->size) |
|
1204 { |
|
1205 infoLog.append("Type of vertex varying %s does not match that of the fragment varying", output->name.c_str()); |
|
1206 |
|
1207 return false; |
|
1208 } |
|
1209 |
|
1210 output->reg = input->reg; |
|
1211 output->col = input->col; |
|
1212 |
|
1213 matched = true; |
|
1214 break; |
|
1215 } |
|
1216 } |
|
1217 |
|
1218 if (!matched) |
|
1219 { |
|
1220 infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str()); |
|
1221 |
|
1222 return false; |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 mUsesPointSize = vertexShader->mUsesPointSize; |
|
1227 std::string varyingSemantic = (mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD"; |
|
1228 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR"; |
|
1229 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION"; |
|
1230 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH"; |
|
1231 |
|
1232 // special varyings that use reserved registers |
|
1233 int reservedRegisterIndex = registers; |
|
1234 std::string fragCoordSemantic; |
|
1235 std::string pointCoordSemantic; |
|
1236 |
|
1237 if (fragmentShader->mUsesFragCoord) |
|
1238 { |
|
1239 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++); |
|
1240 } |
|
1241 |
|
1242 if (fragmentShader->mUsesPointCoord) |
|
1243 { |
|
1244 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords. |
|
1245 // In DX11 we compute this in the GS. |
|
1246 if (shaderModel == 3) |
|
1247 { |
|
1248 pointCoordSemantic = "TEXCOORD0"; |
|
1249 } |
|
1250 else if (shaderModel >= 4) |
|
1251 { |
|
1252 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++); |
|
1253 } |
|
1254 } |
|
1255 |
|
1256 vertexHLSL += "struct VS_INPUT\n" |
|
1257 "{\n"; |
|
1258 |
|
1259 int semanticIndex = 0; |
|
1260 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
|
1261 { |
|
1262 switch (attribute->type) |
|
1263 { |
|
1264 case GL_FLOAT: vertexHLSL += " float "; break; |
|
1265 case GL_FLOAT_VEC2: vertexHLSL += " float2 "; break; |
|
1266 case GL_FLOAT_VEC3: vertexHLSL += " float3 "; break; |
|
1267 case GL_FLOAT_VEC4: vertexHLSL += " float4 "; break; |
|
1268 case GL_FLOAT_MAT2: vertexHLSL += " float2x2 "; break; |
|
1269 case GL_FLOAT_MAT3: vertexHLSL += " float3x3 "; break; |
|
1270 case GL_FLOAT_MAT4: vertexHLSL += " float4x4 "; break; |
|
1271 default: UNREACHABLE(); |
|
1272 } |
|
1273 |
|
1274 vertexHLSL += decorateAttribute(attribute->name) + " : TEXCOORD" + str(semanticIndex) + ";\n"; |
|
1275 |
|
1276 semanticIndex += VariableRowCount(attribute->type); |
|
1277 } |
|
1278 |
|
1279 vertexHLSL += "};\n" |
|
1280 "\n" |
|
1281 "struct VS_OUTPUT\n" |
|
1282 "{\n"; |
|
1283 |
|
1284 if (shaderModel < 4) |
|
1285 { |
|
1286 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n"; |
|
1287 } |
|
1288 |
|
1289 for (int r = 0; r < registers; r++) |
|
1290 { |
|
1291 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1)); |
|
1292 |
|
1293 vertexHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n"; |
|
1294 } |
|
1295 |
|
1296 if (fragmentShader->mUsesFragCoord) |
|
1297 { |
|
1298 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n"; |
|
1299 } |
|
1300 |
|
1301 if (vertexShader->mUsesPointSize && shaderModel >= 3) |
|
1302 { |
|
1303 vertexHLSL += " float gl_PointSize : PSIZE;\n"; |
|
1304 } |
|
1305 |
|
1306 if (shaderModel >= 4) |
|
1307 { |
|
1308 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n"; |
|
1309 } |
|
1310 |
|
1311 vertexHLSL += "};\n" |
|
1312 "\n" |
|
1313 "VS_OUTPUT main(VS_INPUT input)\n" |
|
1314 "{\n"; |
|
1315 |
|
1316 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
|
1317 { |
|
1318 vertexHLSL += " " + decorateAttribute(attribute->name) + " = "; |
|
1319 |
|
1320 if (VariableRowCount(attribute->type) > 1) // Matrix |
|
1321 { |
|
1322 vertexHLSL += "transpose"; |
|
1323 } |
|
1324 |
|
1325 vertexHLSL += "(input." + decorateAttribute(attribute->name) + ");\n"; |
|
1326 } |
|
1327 |
|
1328 if (shaderModel >= 4) |
|
1329 { |
|
1330 vertexHLSL += "\n" |
|
1331 " gl_main();\n" |
|
1332 "\n" |
|
1333 " VS_OUTPUT output;\n" |
|
1334 " output.gl_Position.x = gl_Position.x;\n" |
|
1335 " output.gl_Position.y = -gl_Position.y;\n" |
|
1336 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
|
1337 " output.gl_Position.w = gl_Position.w;\n"; |
|
1338 } |
|
1339 else |
|
1340 { |
|
1341 vertexHLSL += "\n" |
|
1342 " gl_main();\n" |
|
1343 "\n" |
|
1344 " VS_OUTPUT output;\n" |
|
1345 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n" |
|
1346 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n" |
|
1347 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
|
1348 " output.gl_Position.w = gl_Position.w;\n"; |
|
1349 } |
|
1350 |
|
1351 if (vertexShader->mUsesPointSize && shaderModel >= 3) |
|
1352 { |
|
1353 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n"; |
|
1354 } |
|
1355 |
|
1356 if (fragmentShader->mUsesFragCoord) |
|
1357 { |
|
1358 vertexHLSL += " output.gl_FragCoord = gl_Position;\n"; |
|
1359 } |
|
1360 |
|
1361 for (VaryingList::iterator varying = vertexShader->mVaryings.begin(); varying != vertexShader->mVaryings.end(); varying++) |
|
1362 { |
|
1363 if (varying->reg >= 0) |
|
1364 { |
|
1365 for (int i = 0; i < varying->size; i++) |
|
1366 { |
|
1367 int rows = VariableRowCount(varying->type); |
|
1368 |
|
1369 for (int j = 0; j < rows; j++) |
|
1370 { |
|
1371 int r = varying->reg + i * rows + j; |
|
1372 vertexHLSL += " output.v" + str(r); |
|
1373 |
|
1374 bool sharedRegister = false; // Register used by multiple varyings |
|
1375 |
|
1376 for (int x = 0; x < 4; x++) |
|
1377 { |
|
1378 if (packing[r][x] && packing[r][x] != packing[r][0]) |
|
1379 { |
|
1380 sharedRegister = true; |
|
1381 break; |
|
1382 } |
|
1383 } |
|
1384 |
|
1385 if(sharedRegister) |
|
1386 { |
|
1387 vertexHLSL += "."; |
|
1388 |
|
1389 for (int x = 0; x < 4; x++) |
|
1390 { |
|
1391 if (packing[r][x] == &*varying) |
|
1392 { |
|
1393 switch(x) |
|
1394 { |
|
1395 case 0: vertexHLSL += "x"; break; |
|
1396 case 1: vertexHLSL += "y"; break; |
|
1397 case 2: vertexHLSL += "z"; break; |
|
1398 case 3: vertexHLSL += "w"; break; |
|
1399 } |
|
1400 } |
|
1401 } |
|
1402 } |
|
1403 |
|
1404 vertexHLSL += " = " + varying->name; |
|
1405 |
|
1406 if (varying->array) |
|
1407 { |
|
1408 vertexHLSL += "[" + str(i) + "]"; |
|
1409 } |
|
1410 |
|
1411 if (rows > 1) |
|
1412 { |
|
1413 vertexHLSL += "[" + str(j) + "]"; |
|
1414 } |
|
1415 |
|
1416 vertexHLSL += ";\n"; |
|
1417 } |
|
1418 } |
|
1419 } |
|
1420 } |
|
1421 |
|
1422 vertexHLSL += "\n" |
|
1423 " return output;\n" |
|
1424 "}\n"; |
|
1425 |
|
1426 pixelHLSL += "struct PS_INPUT\n" |
|
1427 "{\n"; |
|
1428 |
|
1429 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++) |
|
1430 { |
|
1431 if (varying->reg >= 0) |
|
1432 { |
|
1433 for (int i = 0; i < varying->size; i++) |
|
1434 { |
|
1435 int rows = VariableRowCount(varying->type); |
|
1436 for (int j = 0; j < rows; j++) |
|
1437 { |
|
1438 std::string n = str(varying->reg + i * rows + j); |
|
1439 pixelHLSL += " float" + str(VariableColumnCount(varying->type)) + " v" + n + " : " + varyingSemantic + n + ";\n"; |
|
1440 } |
|
1441 } |
|
1442 } |
|
1443 else UNREACHABLE(); |
|
1444 } |
|
1445 |
|
1446 if (fragmentShader->mUsesFragCoord) |
|
1447 { |
|
1448 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n"; |
|
1449 } |
|
1450 |
|
1451 if (fragmentShader->mUsesPointCoord && shaderModel >= 3) |
|
1452 { |
|
1453 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n"; |
|
1454 } |
|
1455 |
|
1456 // Must consume the PSIZE element if the geometry shader is not active |
|
1457 // We won't know if we use a GS until we draw |
|
1458 if (vertexShader->mUsesPointSize && shaderModel >= 4) |
|
1459 { |
|
1460 pixelHLSL += " float gl_PointSize : PSIZE;\n"; |
|
1461 } |
|
1462 |
|
1463 if (fragmentShader->mUsesFragCoord) |
|
1464 { |
|
1465 if (shaderModel >= 4) |
|
1466 { |
|
1467 pixelHLSL += " float4 dx_VPos : SV_Position;\n"; |
|
1468 } |
|
1469 else if (shaderModel >= 3) |
|
1470 { |
|
1471 pixelHLSL += " float2 dx_VPos : VPOS;\n"; |
|
1472 } |
|
1473 } |
|
1474 |
|
1475 pixelHLSL += "};\n" |
|
1476 "\n" |
|
1477 "struct PS_OUTPUT\n" |
|
1478 "{\n"; |
|
1479 |
|
1480 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++) |
|
1481 { |
|
1482 pixelHLSL += " float4 gl_Color" + str(renderTargetIndex) + " : " + targetSemantic + str(renderTargetIndex) + ";\n"; |
|
1483 } |
|
1484 |
|
1485 if (fragmentShader->mUsesFragDepth) |
|
1486 { |
|
1487 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n"; |
|
1488 } |
|
1489 |
|
1490 pixelHLSL += "};\n" |
|
1491 "\n"; |
|
1492 |
|
1493 if (fragmentShader->mUsesFrontFacing) |
|
1494 { |
|
1495 if (shaderModel >= 4) |
|
1496 { |
|
1497 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n" |
|
1498 "{\n"; |
|
1499 } |
|
1500 else |
|
1501 { |
|
1502 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n" |
|
1503 "{\n"; |
|
1504 } |
|
1505 } |
|
1506 else |
|
1507 { |
|
1508 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n" |
|
1509 "{\n"; |
|
1510 } |
|
1511 |
|
1512 if (fragmentShader->mUsesFragCoord) |
|
1513 { |
|
1514 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n"; |
|
1515 |
|
1516 if (shaderModel >= 4) |
|
1517 { |
|
1518 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n" |
|
1519 " gl_FragCoord.y = input.dx_VPos.y;\n"; |
|
1520 } |
|
1521 else if (shaderModel >= 3) |
|
1522 { |
|
1523 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n" |
|
1524 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n"; |
|
1525 } |
|
1526 else |
|
1527 { |
|
1528 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport() |
|
1529 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n" |
|
1530 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n"; |
|
1531 } |
|
1532 |
|
1533 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n" |
|
1534 " gl_FragCoord.w = rhw;\n"; |
|
1535 } |
|
1536 |
|
1537 if (fragmentShader->mUsesPointCoord && shaderModel >= 3) |
|
1538 { |
|
1539 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n"; |
|
1540 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n"; |
|
1541 } |
|
1542 |
|
1543 if (fragmentShader->mUsesFrontFacing) |
|
1544 { |
|
1545 if (shaderModel <= 3) |
|
1546 { |
|
1547 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n"; |
|
1548 } |
|
1549 else |
|
1550 { |
|
1551 pixelHLSL += " gl_FrontFacing = isFrontFace;\n"; |
|
1552 } |
|
1553 } |
|
1554 |
|
1555 for (VaryingList::iterator varying = fragmentShader->mVaryings.begin(); varying != fragmentShader->mVaryings.end(); varying++) |
|
1556 { |
|
1557 if (varying->reg >= 0) |
|
1558 { |
|
1559 for (int i = 0; i < varying->size; i++) |
|
1560 { |
|
1561 int rows = VariableRowCount(varying->type); |
|
1562 for (int j = 0; j < rows; j++) |
|
1563 { |
|
1564 std::string n = str(varying->reg + i * rows + j); |
|
1565 pixelHLSL += " " + varying->name; |
|
1566 |
|
1567 if (varying->array) |
|
1568 { |
|
1569 pixelHLSL += "[" + str(i) + "]"; |
|
1570 } |
|
1571 |
|
1572 if (rows > 1) |
|
1573 { |
|
1574 pixelHLSL += "[" + str(j) + "]"; |
|
1575 } |
|
1576 |
|
1577 switch (VariableColumnCount(varying->type)) |
|
1578 { |
|
1579 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break; |
|
1580 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break; |
|
1581 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break; |
|
1582 case 4: pixelHLSL += " = input.v" + n + ";\n"; break; |
|
1583 default: UNREACHABLE(); |
|
1584 } |
|
1585 } |
|
1586 } |
|
1587 } |
|
1588 else UNREACHABLE(); |
|
1589 } |
|
1590 |
|
1591 pixelHLSL += "\n" |
|
1592 " gl_main();\n" |
|
1593 "\n" |
|
1594 " PS_OUTPUT output;\n"; |
|
1595 |
|
1596 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++) |
|
1597 { |
|
1598 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex; |
|
1599 |
|
1600 pixelHLSL += " output.gl_Color" + str(renderTargetIndex) + " = gl_Color[" + str(sourceColorIndex) + "];\n"; |
|
1601 } |
|
1602 |
|
1603 if (fragmentShader->mUsesFragDepth) |
|
1604 { |
|
1605 pixelHLSL += " output.gl_Depth = gl_Depth;\n"; |
|
1606 } |
|
1607 |
|
1608 pixelHLSL += "\n" |
|
1609 " return output;\n" |
|
1610 "}\n"; |
|
1611 |
|
1612 return true; |
|
1613 } |
|
1614 |
|
1615 bool ProgramBinary::load(InfoLog &infoLog, const void *binary, GLsizei length) |
|
1616 { |
|
1617 BinaryInputStream stream(binary, length); |
|
1618 |
|
1619 int format = 0; |
|
1620 stream.read(&format); |
|
1621 if (format != GL_PROGRAM_BINARY_ANGLE) |
|
1622 { |
|
1623 infoLog.append("Invalid program binary format."); |
|
1624 return false; |
|
1625 } |
|
1626 |
|
1627 int version = 0; |
|
1628 stream.read(&version); |
|
1629 if (version != VERSION_DWORD) |
|
1630 { |
|
1631 infoLog.append("Invalid program binary version."); |
|
1632 return false; |
|
1633 } |
|
1634 |
|
1635 int compileFlags = 0; |
|
1636 stream.read(&compileFlags); |
|
1637 if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL) |
|
1638 { |
|
1639 infoLog.append("Mismatched compilation flags."); |
|
1640 return false; |
|
1641 } |
|
1642 |
|
1643 for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i) |
|
1644 { |
|
1645 stream.read(&mLinkedAttribute[i].type); |
|
1646 std::string name; |
|
1647 stream.read(&name); |
|
1648 mLinkedAttribute[i].name = name; |
|
1649 stream.read(&mSemanticIndex[i]); |
|
1650 } |
|
1651 |
|
1652 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i) |
|
1653 { |
|
1654 stream.read(&mSamplersPS[i].active); |
|
1655 stream.read(&mSamplersPS[i].logicalTextureUnit); |
|
1656 |
|
1657 int textureType; |
|
1658 stream.read(&textureType); |
|
1659 mSamplersPS[i].textureType = (TextureType) textureType; |
|
1660 } |
|
1661 |
|
1662 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i) |
|
1663 { |
|
1664 stream.read(&mSamplersVS[i].active); |
|
1665 stream.read(&mSamplersVS[i].logicalTextureUnit); |
|
1666 |
|
1667 int textureType; |
|
1668 stream.read(&textureType); |
|
1669 mSamplersVS[i].textureType = (TextureType) textureType; |
|
1670 } |
|
1671 |
|
1672 stream.read(&mUsedVertexSamplerRange); |
|
1673 stream.read(&mUsedPixelSamplerRange); |
|
1674 stream.read(&mUsesPointSize); |
|
1675 |
|
1676 size_t size; |
|
1677 stream.read(&size); |
|
1678 if (stream.error()) |
|
1679 { |
|
1680 infoLog.append("Invalid program binary."); |
|
1681 return false; |
|
1682 } |
|
1683 |
|
1684 mUniforms.resize(size); |
|
1685 for (unsigned int i = 0; i < size; ++i) |
|
1686 { |
|
1687 GLenum type; |
|
1688 GLenum precision; |
|
1689 std::string name; |
|
1690 unsigned int arraySize; |
|
1691 |
|
1692 stream.read(&type); |
|
1693 stream.read(&precision); |
|
1694 stream.read(&name); |
|
1695 stream.read(&arraySize); |
|
1696 |
|
1697 mUniforms[i] = new Uniform(type, precision, name, arraySize); |
|
1698 |
|
1699 stream.read(&mUniforms[i]->psRegisterIndex); |
|
1700 stream.read(&mUniforms[i]->vsRegisterIndex); |
|
1701 stream.read(&mUniforms[i]->registerCount); |
|
1702 } |
|
1703 |
|
1704 stream.read(&size); |
|
1705 if (stream.error()) |
|
1706 { |
|
1707 infoLog.append("Invalid program binary."); |
|
1708 return false; |
|
1709 } |
|
1710 |
|
1711 mUniformIndex.resize(size); |
|
1712 for (unsigned int i = 0; i < size; ++i) |
|
1713 { |
|
1714 stream.read(&mUniformIndex[i].name); |
|
1715 stream.read(&mUniformIndex[i].element); |
|
1716 stream.read(&mUniformIndex[i].index); |
|
1717 } |
|
1718 |
|
1719 unsigned int pixelShaderSize; |
|
1720 stream.read(&pixelShaderSize); |
|
1721 |
|
1722 unsigned int vertexShaderSize; |
|
1723 stream.read(&vertexShaderSize); |
|
1724 |
|
1725 unsigned int geometryShaderSize; |
|
1726 stream.read(&geometryShaderSize); |
|
1727 |
|
1728 const char *ptr = (const char*) binary + stream.offset(); |
|
1729 |
|
1730 const GUID *binaryIdentifier = (const GUID *) ptr; |
|
1731 ptr += sizeof(GUID); |
|
1732 |
|
1733 GUID identifier = mRenderer->getAdapterIdentifier(); |
|
1734 if (memcmp(&identifier, binaryIdentifier, sizeof(GUID)) != 0) |
|
1735 { |
|
1736 infoLog.append("Invalid program binary."); |
|
1737 return false; |
|
1738 } |
|
1739 |
|
1740 const char *pixelShaderFunction = ptr; |
|
1741 ptr += pixelShaderSize; |
|
1742 |
|
1743 const char *vertexShaderFunction = ptr; |
|
1744 ptr += vertexShaderSize; |
|
1745 |
|
1746 const char *geometryShaderFunction = geometryShaderSize > 0 ? ptr : NULL; |
|
1747 ptr += geometryShaderSize; |
|
1748 |
|
1749 mPixelExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(pixelShaderFunction), |
|
1750 pixelShaderSize, rx::SHADER_PIXEL); |
|
1751 if (!mPixelExecutable) |
|
1752 { |
|
1753 infoLog.append("Could not create pixel shader."); |
|
1754 return false; |
|
1755 } |
|
1756 |
|
1757 mVertexExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(vertexShaderFunction), |
|
1758 vertexShaderSize, rx::SHADER_VERTEX); |
|
1759 if (!mVertexExecutable) |
|
1760 { |
|
1761 infoLog.append("Could not create vertex shader."); |
|
1762 delete mPixelExecutable; |
|
1763 mPixelExecutable = NULL; |
|
1764 return false; |
|
1765 } |
|
1766 |
|
1767 if (geometryShaderFunction != NULL && geometryShaderSize > 0) |
|
1768 { |
|
1769 mGeometryExecutable = mRenderer->loadExecutable(reinterpret_cast<const DWORD*>(geometryShaderFunction), |
|
1770 geometryShaderSize, rx::SHADER_GEOMETRY); |
|
1771 if (!mGeometryExecutable) |
|
1772 { |
|
1773 infoLog.append("Could not create geometry shader."); |
|
1774 delete mPixelExecutable; |
|
1775 mPixelExecutable = NULL; |
|
1776 delete mVertexExecutable; |
|
1777 mVertexExecutable = NULL; |
|
1778 return false; |
|
1779 } |
|
1780 } |
|
1781 else |
|
1782 { |
|
1783 mGeometryExecutable = NULL; |
|
1784 } |
|
1785 |
|
1786 return true; |
|
1787 } |
|
1788 |
|
1789 bool ProgramBinary::save(void* binary, GLsizei bufSize, GLsizei *length) |
|
1790 { |
|
1791 BinaryOutputStream stream; |
|
1792 |
|
1793 stream.write(GL_PROGRAM_BINARY_ANGLE); |
|
1794 stream.write(VERSION_DWORD); |
|
1795 stream.write(ANGLE_COMPILE_OPTIMIZATION_LEVEL); |
|
1796 |
|
1797 for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i) |
|
1798 { |
|
1799 stream.write(mLinkedAttribute[i].type); |
|
1800 stream.write(mLinkedAttribute[i].name); |
|
1801 stream.write(mSemanticIndex[i]); |
|
1802 } |
|
1803 |
|
1804 for (unsigned int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; ++i) |
|
1805 { |
|
1806 stream.write(mSamplersPS[i].active); |
|
1807 stream.write(mSamplersPS[i].logicalTextureUnit); |
|
1808 stream.write((int) mSamplersPS[i].textureType); |
|
1809 } |
|
1810 |
|
1811 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; ++i) |
|
1812 { |
|
1813 stream.write(mSamplersVS[i].active); |
|
1814 stream.write(mSamplersVS[i].logicalTextureUnit); |
|
1815 stream.write((int) mSamplersVS[i].textureType); |
|
1816 } |
|
1817 |
|
1818 stream.write(mUsedVertexSamplerRange); |
|
1819 stream.write(mUsedPixelSamplerRange); |
|
1820 stream.write(mUsesPointSize); |
|
1821 |
|
1822 stream.write(mUniforms.size()); |
|
1823 for (unsigned int i = 0; i < mUniforms.size(); ++i) |
|
1824 { |
|
1825 stream.write(mUniforms[i]->type); |
|
1826 stream.write(mUniforms[i]->precision); |
|
1827 stream.write(mUniforms[i]->name); |
|
1828 stream.write(mUniforms[i]->arraySize); |
|
1829 |
|
1830 stream.write(mUniforms[i]->psRegisterIndex); |
|
1831 stream.write(mUniforms[i]->vsRegisterIndex); |
|
1832 stream.write(mUniforms[i]->registerCount); |
|
1833 } |
|
1834 |
|
1835 stream.write(mUniformIndex.size()); |
|
1836 for (unsigned int i = 0; i < mUniformIndex.size(); ++i) |
|
1837 { |
|
1838 stream.write(mUniformIndex[i].name); |
|
1839 stream.write(mUniformIndex[i].element); |
|
1840 stream.write(mUniformIndex[i].index); |
|
1841 } |
|
1842 |
|
1843 UINT pixelShaderSize = mPixelExecutable->getLength(); |
|
1844 stream.write(pixelShaderSize); |
|
1845 |
|
1846 UINT vertexShaderSize = mVertexExecutable->getLength(); |
|
1847 stream.write(vertexShaderSize); |
|
1848 |
|
1849 UINT geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0; |
|
1850 stream.write(geometryShaderSize); |
|
1851 |
|
1852 GUID identifier = mRenderer->getAdapterIdentifier(); |
|
1853 |
|
1854 GLsizei streamLength = stream.length(); |
|
1855 const void *streamData = stream.data(); |
|
1856 |
|
1857 GLsizei totalLength = streamLength + sizeof(GUID) + pixelShaderSize + vertexShaderSize + geometryShaderSize; |
|
1858 if (totalLength > bufSize) |
|
1859 { |
|
1860 if (length) |
|
1861 { |
|
1862 *length = 0; |
|
1863 } |
|
1864 |
|
1865 return false; |
|
1866 } |
|
1867 |
|
1868 if (binary) |
|
1869 { |
|
1870 char *ptr = (char*) binary; |
|
1871 |
|
1872 memcpy(ptr, streamData, streamLength); |
|
1873 ptr += streamLength; |
|
1874 |
|
1875 memcpy(ptr, &identifier, sizeof(GUID)); |
|
1876 ptr += sizeof(GUID); |
|
1877 |
|
1878 memcpy(ptr, mPixelExecutable->getFunction(), pixelShaderSize); |
|
1879 ptr += pixelShaderSize; |
|
1880 |
|
1881 memcpy(ptr, mVertexExecutable->getFunction(), vertexShaderSize); |
|
1882 ptr += vertexShaderSize; |
|
1883 |
|
1884 if (mGeometryExecutable != NULL && geometryShaderSize > 0) |
|
1885 { |
|
1886 memcpy(ptr, mGeometryExecutable->getFunction(), geometryShaderSize); |
|
1887 ptr += geometryShaderSize; |
|
1888 } |
|
1889 |
|
1890 ASSERT(ptr - totalLength == binary); |
|
1891 } |
|
1892 |
|
1893 if (length) |
|
1894 { |
|
1895 *length = totalLength; |
|
1896 } |
|
1897 |
|
1898 return true; |
|
1899 } |
|
1900 |
|
1901 GLint ProgramBinary::getLength() |
|
1902 { |
|
1903 GLint length; |
|
1904 if (save(NULL, INT_MAX, &length)) |
|
1905 { |
|
1906 return length; |
|
1907 } |
|
1908 else |
|
1909 { |
|
1910 return 0; |
|
1911 } |
|
1912 } |
|
1913 |
|
1914 bool ProgramBinary::link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader) |
|
1915 { |
|
1916 if (!fragmentShader || !fragmentShader->isCompiled()) |
|
1917 { |
|
1918 return false; |
|
1919 } |
|
1920 |
|
1921 if (!vertexShader || !vertexShader->isCompiled()) |
|
1922 { |
|
1923 return false; |
|
1924 } |
|
1925 |
|
1926 std::string pixelHLSL = fragmentShader->getHLSL(); |
|
1927 std::string vertexHLSL = vertexShader->getHLSL(); |
|
1928 |
|
1929 // Map the varyings to the register file |
|
1930 const Varying *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL}; |
|
1931 int registers = packVaryings(infoLog, packing, fragmentShader); |
|
1932 |
|
1933 if (registers < 0) |
|
1934 { |
|
1935 return false; |
|
1936 } |
|
1937 |
|
1938 if (!linkVaryings(infoLog, registers, packing, pixelHLSL, vertexHLSL, fragmentShader, vertexShader)) |
|
1939 { |
|
1940 return false; |
|
1941 } |
|
1942 |
|
1943 bool success = true; |
|
1944 |
|
1945 if (!linkAttributes(infoLog, attributeBindings, fragmentShader, vertexShader)) |
|
1946 { |
|
1947 success = false; |
|
1948 } |
|
1949 |
|
1950 if (!linkUniforms(infoLog, vertexShader->getUniforms(), fragmentShader->getUniforms())) |
|
1951 { |
|
1952 success = false; |
|
1953 } |
|
1954 |
|
1955 // special case for gl_DepthRange, the only built-in uniform (also a struct) |
|
1956 if (vertexShader->mUsesDepthRange || fragmentShader->mUsesDepthRange) |
|
1957 { |
|
1958 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0)); |
|
1959 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0)); |
|
1960 mUniforms.push_back(new Uniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0)); |
|
1961 } |
|
1962 |
|
1963 if (success) |
|
1964 { |
|
1965 mVertexExecutable = mRenderer->compileToExecutable(infoLog, vertexHLSL.c_str(), rx::SHADER_VERTEX); |
|
1966 mPixelExecutable = mRenderer->compileToExecutable(infoLog, pixelHLSL.c_str(), rx::SHADER_PIXEL); |
|
1967 |
|
1968 if (usesGeometryShader()) |
|
1969 { |
|
1970 std::string geometryHLSL = generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader); |
|
1971 mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY); |
|
1972 } |
|
1973 |
|
1974 if (!mVertexExecutable || !mPixelExecutable || (usesGeometryShader() && !mGeometryExecutable)) |
|
1975 { |
|
1976 infoLog.append("Failed to create D3D shaders."); |
|
1977 success = false; |
|
1978 |
|
1979 delete mVertexExecutable; |
|
1980 mVertexExecutable = NULL; |
|
1981 delete mPixelExecutable; |
|
1982 mPixelExecutable = NULL; |
|
1983 delete mGeometryExecutable; |
|
1984 mGeometryExecutable = NULL; |
|
1985 } |
|
1986 } |
|
1987 |
|
1988 return success; |
|
1989 } |
|
1990 |
|
1991 // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices |
|
1992 bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader) |
|
1993 { |
|
1994 unsigned int usedLocations = 0; |
|
1995 |
|
1996 // Link attributes that have a binding location |
|
1997 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
|
1998 { |
|
1999 int location = attributeBindings.getAttributeBinding(attribute->name); |
|
2000 |
|
2001 if (location != -1) // Set by glBindAttribLocation |
|
2002 { |
|
2003 if (!mLinkedAttribute[location].name.empty()) |
|
2004 { |
|
2005 // Multiple active attributes bound to the same location; not an error |
|
2006 } |
|
2007 |
|
2008 mLinkedAttribute[location] = *attribute; |
|
2009 |
|
2010 int rows = VariableRowCount(attribute->type); |
|
2011 |
|
2012 if (rows + location > MAX_VERTEX_ATTRIBS) |
|
2013 { |
|
2014 infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute->name.c_str(), location); |
|
2015 |
|
2016 return false; |
|
2017 } |
|
2018 |
|
2019 for (int i = 0; i < rows; i++) |
|
2020 { |
|
2021 usedLocations |= 1 << (location + i); |
|
2022 } |
|
2023 } |
|
2024 } |
|
2025 |
|
2026 // Link attributes that don't have a binding location |
|
2027 for (AttributeArray::iterator attribute = vertexShader->mAttributes.begin(); attribute != vertexShader->mAttributes.end(); attribute++) |
|
2028 { |
|
2029 int location = attributeBindings.getAttributeBinding(attribute->name); |
|
2030 |
|
2031 if (location == -1) // Not set by glBindAttribLocation |
|
2032 { |
|
2033 int rows = VariableRowCount(attribute->type); |
|
2034 int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS); |
|
2035 |
|
2036 if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS) |
|
2037 { |
|
2038 infoLog.append("Too many active attributes (%s)", attribute->name.c_str()); |
|
2039 |
|
2040 return false; // Fail to link |
|
2041 } |
|
2042 |
|
2043 mLinkedAttribute[availableIndex] = *attribute; |
|
2044 } |
|
2045 } |
|
2046 |
|
2047 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; ) |
|
2048 { |
|
2049 int index = vertexShader->getSemanticIndex(mLinkedAttribute[attributeIndex].name); |
|
2050 int rows = std::max(VariableRowCount(mLinkedAttribute[attributeIndex].type), 1); |
|
2051 |
|
2052 for (int r = 0; r < rows; r++) |
|
2053 { |
|
2054 mSemanticIndex[attributeIndex++] = index++; |
|
2055 } |
|
2056 } |
|
2057 |
|
2058 return true; |
|
2059 } |
|
2060 |
|
2061 bool ProgramBinary::linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms) |
|
2062 { |
|
2063 for (sh::ActiveUniforms::const_iterator uniform = vertexUniforms.begin(); uniform != vertexUniforms.end(); uniform++) |
|
2064 { |
|
2065 if (!defineUniform(GL_VERTEX_SHADER, *uniform, infoLog)) |
|
2066 { |
|
2067 return false; |
|
2068 } |
|
2069 } |
|
2070 |
|
2071 for (sh::ActiveUniforms::const_iterator uniform = fragmentUniforms.begin(); uniform != fragmentUniforms.end(); uniform++) |
|
2072 { |
|
2073 if (!defineUniform(GL_FRAGMENT_SHADER, *uniform, infoLog)) |
|
2074 { |
|
2075 return false; |
|
2076 } |
|
2077 } |
|
2078 |
|
2079 return true; |
|
2080 } |
|
2081 |
|
2082 bool ProgramBinary::defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog) |
|
2083 { |
|
2084 if (constant.type == GL_SAMPLER_2D || |
|
2085 constant.type == GL_SAMPLER_CUBE) |
|
2086 { |
|
2087 unsigned int samplerIndex = constant.registerIndex; |
|
2088 |
|
2089 do |
|
2090 { |
|
2091 if (shader == GL_VERTEX_SHADER) |
|
2092 { |
|
2093 if (samplerIndex < mRenderer->getMaxVertexTextureImageUnits()) |
|
2094 { |
|
2095 mSamplersVS[samplerIndex].active = true; |
|
2096 mSamplersVS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D; |
|
2097 mSamplersVS[samplerIndex].logicalTextureUnit = 0; |
|
2098 mUsedVertexSamplerRange = std::max(samplerIndex + 1, mUsedVertexSamplerRange); |
|
2099 } |
|
2100 else |
|
2101 { |
|
2102 infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", mRenderer->getMaxVertexTextureImageUnits()); |
|
2103 return false; |
|
2104 } |
|
2105 } |
|
2106 else if (shader == GL_FRAGMENT_SHADER) |
|
2107 { |
|
2108 if (samplerIndex < MAX_TEXTURE_IMAGE_UNITS) |
|
2109 { |
|
2110 mSamplersPS[samplerIndex].active = true; |
|
2111 mSamplersPS[samplerIndex].textureType = (constant.type == GL_SAMPLER_CUBE) ? TEXTURE_CUBE : TEXTURE_2D; |
|
2112 mSamplersPS[samplerIndex].logicalTextureUnit = 0; |
|
2113 mUsedPixelSamplerRange = std::max(samplerIndex + 1, mUsedPixelSamplerRange); |
|
2114 } |
|
2115 else |
|
2116 { |
|
2117 infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", MAX_TEXTURE_IMAGE_UNITS); |
|
2118 return false; |
|
2119 } |
|
2120 } |
|
2121 else UNREACHABLE(); |
|
2122 |
|
2123 samplerIndex++; |
|
2124 } |
|
2125 while (samplerIndex < constant.registerIndex + constant.arraySize); |
|
2126 } |
|
2127 |
|
2128 Uniform *uniform = NULL; |
|
2129 GLint location = getUniformLocation(constant.name); |
|
2130 |
|
2131 if (location >= 0) // Previously defined, type and precision must match |
|
2132 { |
|
2133 uniform = mUniforms[mUniformIndex[location].index]; |
|
2134 |
|
2135 if (uniform->type != constant.type) |
|
2136 { |
|
2137 infoLog.append("Types for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str()); |
|
2138 return false; |
|
2139 } |
|
2140 |
|
2141 if (uniform->precision != constant.precision) |
|
2142 { |
|
2143 infoLog.append("Precisions for uniform %s do not match between the vertex and fragment shader", uniform->name.c_str()); |
|
2144 return false; |
|
2145 } |
|
2146 } |
|
2147 else |
|
2148 { |
|
2149 uniform = new Uniform(constant.type, constant.precision, constant.name, constant.arraySize); |
|
2150 } |
|
2151 |
|
2152 if (!uniform) |
|
2153 { |
|
2154 return false; |
|
2155 } |
|
2156 |
|
2157 if (shader == GL_FRAGMENT_SHADER) |
|
2158 { |
|
2159 uniform->psRegisterIndex = constant.registerIndex; |
|
2160 } |
|
2161 else if (shader == GL_VERTEX_SHADER) |
|
2162 { |
|
2163 uniform->vsRegisterIndex = constant.registerIndex; |
|
2164 } |
|
2165 else UNREACHABLE(); |
|
2166 |
|
2167 if (location >= 0) |
|
2168 { |
|
2169 return uniform->type == constant.type; |
|
2170 } |
|
2171 |
|
2172 mUniforms.push_back(uniform); |
|
2173 unsigned int uniformIndex = mUniforms.size() - 1; |
|
2174 |
|
2175 for (unsigned int i = 0; i < uniform->elementCount(); i++) |
|
2176 { |
|
2177 mUniformIndex.push_back(UniformLocation(constant.name, i, uniformIndex)); |
|
2178 } |
|
2179 |
|
2180 if (shader == GL_VERTEX_SHADER) |
|
2181 { |
|
2182 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedVertexUniformVectors() + mRenderer->getMaxVertexUniformVectors()) |
|
2183 { |
|
2184 infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", mRenderer->getMaxVertexUniformVectors()); |
|
2185 return false; |
|
2186 } |
|
2187 } |
|
2188 else if (shader == GL_FRAGMENT_SHADER) |
|
2189 { |
|
2190 if (constant.registerIndex + uniform->registerCount > mRenderer->getReservedFragmentUniformVectors() + mRenderer->getMaxFragmentUniformVectors()) |
|
2191 { |
|
2192 infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", mRenderer->getMaxFragmentUniformVectors()); |
|
2193 return false; |
|
2194 } |
|
2195 } |
|
2196 else UNREACHABLE(); |
|
2197 |
|
2198 return true; |
|
2199 } |
|
2200 |
|
2201 std::string ProgramBinary::generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const |
|
2202 { |
|
2203 // for now we only handle point sprite emulation |
|
2204 ASSERT(usesPointSpriteEmulation()); |
|
2205 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader); |
|
2206 } |
|
2207 |
|
2208 std::string ProgramBinary::generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const |
|
2209 { |
|
2210 ASSERT(registers >= 0); |
|
2211 ASSERT(vertexShader->mUsesPointSize); |
|
2212 ASSERT(mRenderer->getMajorShaderModel() >= 4); |
|
2213 |
|
2214 std::string geomHLSL; |
|
2215 |
|
2216 std::string varyingSemantic = "TEXCOORD"; |
|
2217 |
|
2218 std::string fragCoordSemantic; |
|
2219 std::string pointCoordSemantic; |
|
2220 |
|
2221 int reservedRegisterIndex = registers; |
|
2222 |
|
2223 if (fragmentShader->mUsesFragCoord) |
|
2224 { |
|
2225 fragCoordSemantic = varyingSemantic + str(reservedRegisterIndex++); |
|
2226 } |
|
2227 |
|
2228 if (fragmentShader->mUsesPointCoord) |
|
2229 { |
|
2230 pointCoordSemantic = varyingSemantic + str(reservedRegisterIndex++); |
|
2231 } |
|
2232 |
|
2233 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n" |
|
2234 "\n" |
|
2235 "struct GS_INPUT\n" |
|
2236 "{\n"; |
|
2237 |
|
2238 for (int r = 0; r < registers; r++) |
|
2239 { |
|
2240 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1)); |
|
2241 |
|
2242 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n"; |
|
2243 } |
|
2244 |
|
2245 if (fragmentShader->mUsesFragCoord) |
|
2246 { |
|
2247 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n"; |
|
2248 } |
|
2249 |
|
2250 geomHLSL += " float gl_PointSize : PSIZE;\n" |
|
2251 " float4 gl_Position : SV_Position;\n" |
|
2252 "};\n" |
|
2253 "\n" |
|
2254 "struct GS_OUTPUT\n" |
|
2255 "{\n"; |
|
2256 |
|
2257 for (int r = 0; r < registers; r++) |
|
2258 { |
|
2259 int registerSize = packing[r][3] ? 4 : (packing[r][2] ? 3 : (packing[r][1] ? 2 : 1)); |
|
2260 |
|
2261 geomHLSL += " float" + str(registerSize) + " v" + str(r) + " : " + varyingSemantic + str(r) + ";\n"; |
|
2262 } |
|
2263 |
|
2264 if (fragmentShader->mUsesFragCoord) |
|
2265 { |
|
2266 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n"; |
|
2267 } |
|
2268 |
|
2269 if (fragmentShader->mUsesPointCoord) |
|
2270 { |
|
2271 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n"; |
|
2272 } |
|
2273 |
|
2274 geomHLSL += " float gl_PointSize : PSIZE;\n" |
|
2275 " float4 gl_Position : SV_Position;\n" |
|
2276 "};\n" |
|
2277 "\n" |
|
2278 "static float2 pointSpriteCorners[] = \n" |
|
2279 "{\n" |
|
2280 " float2( 0.5f, -0.5f),\n" |
|
2281 " float2( 0.5f, 0.5f),\n" |
|
2282 " float2(-0.5f, -0.5f),\n" |
|
2283 " float2(-0.5f, 0.5f)\n" |
|
2284 "};\n" |
|
2285 "\n" |
|
2286 "static float2 pointSpriteTexcoords[] = \n" |
|
2287 "{\n" |
|
2288 " float2(1.0f, 1.0f),\n" |
|
2289 " float2(1.0f, 0.0f),\n" |
|
2290 " float2(0.0f, 1.0f),\n" |
|
2291 " float2(0.0f, 0.0f)\n" |
|
2292 "};\n" |
|
2293 "\n" |
|
2294 "static float minPointSize = " + str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n" |
|
2295 "static float maxPointSize = " + str(mRenderer->getMaxPointSize()) + ".0f;\n" |
|
2296 "\n" |
|
2297 "[maxvertexcount(4)]\n" |
|
2298 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n" |
|
2299 "{\n" |
|
2300 " GS_OUTPUT output = (GS_OUTPUT)0;\n" |
|
2301 " output.gl_PointSize = input[0].gl_PointSize;\n"; |
|
2302 |
|
2303 for (int r = 0; r < registers; r++) |
|
2304 { |
|
2305 geomHLSL += " output.v" + str(r) + " = input[0].v" + str(r) + ";\n"; |
|
2306 } |
|
2307 |
|
2308 if (fragmentShader->mUsesFragCoord) |
|
2309 { |
|
2310 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n"; |
|
2311 } |
|
2312 |
|
2313 geomHLSL += " \n" |
|
2314 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n" |
|
2315 " float4 gl_Position = input[0].gl_Position;\n" |
|
2316 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n"; |
|
2317 |
|
2318 for (int corner = 0; corner < 4; corner++) |
|
2319 { |
|
2320 geomHLSL += " \n" |
|
2321 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n"; |
|
2322 |
|
2323 if (fragmentShader->mUsesPointCoord) |
|
2324 { |
|
2325 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + str(corner) + "];\n"; |
|
2326 } |
|
2327 |
|
2328 geomHLSL += " outStream.Append(output);\n"; |
|
2329 } |
|
2330 |
|
2331 geomHLSL += " \n" |
|
2332 " outStream.RestartStrip();\n" |
|
2333 "}\n"; |
|
2334 |
|
2335 return geomHLSL; |
|
2336 } |
|
2337 |
|
2338 // This method needs to match OutputHLSL::decorate |
|
2339 std::string ProgramBinary::decorateAttribute(const std::string &name) |
|
2340 { |
|
2341 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0) |
|
2342 { |
|
2343 return "_" + name; |
|
2344 } |
|
2345 |
|
2346 return name; |
|
2347 } |
|
2348 |
|
2349 bool ProgramBinary::isValidated() const |
|
2350 { |
|
2351 return mValidated; |
|
2352 } |
|
2353 |
|
2354 void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const |
|
2355 { |
|
2356 // Skip over inactive attributes |
|
2357 unsigned int activeAttribute = 0; |
|
2358 unsigned int attribute; |
|
2359 for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) |
|
2360 { |
|
2361 if (mLinkedAttribute[attribute].name.empty()) |
|
2362 { |
|
2363 continue; |
|
2364 } |
|
2365 |
|
2366 if (activeAttribute == index) |
|
2367 { |
|
2368 break; |
|
2369 } |
|
2370 |
|
2371 activeAttribute++; |
|
2372 } |
|
2373 |
|
2374 if (bufsize > 0) |
|
2375 { |
|
2376 const char *string = mLinkedAttribute[attribute].name.c_str(); |
|
2377 |
|
2378 strncpy(name, string, bufsize); |
|
2379 name[bufsize - 1] = '\0'; |
|
2380 |
|
2381 if (length) |
|
2382 { |
|
2383 *length = strlen(name); |
|
2384 } |
|
2385 } |
|
2386 |
|
2387 *size = 1; // Always a single 'type' instance |
|
2388 |
|
2389 *type = mLinkedAttribute[attribute].type; |
|
2390 } |
|
2391 |
|
2392 GLint ProgramBinary::getActiveAttributeCount() const |
|
2393 { |
|
2394 int count = 0; |
|
2395 |
|
2396 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
|
2397 { |
|
2398 if (!mLinkedAttribute[attributeIndex].name.empty()) |
|
2399 { |
|
2400 count++; |
|
2401 } |
|
2402 } |
|
2403 |
|
2404 return count; |
|
2405 } |
|
2406 |
|
2407 GLint ProgramBinary::getActiveAttributeMaxLength() const |
|
2408 { |
|
2409 int maxLength = 0; |
|
2410 |
|
2411 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
|
2412 { |
|
2413 if (!mLinkedAttribute[attributeIndex].name.empty()) |
|
2414 { |
|
2415 maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength); |
|
2416 } |
|
2417 } |
|
2418 |
|
2419 return maxLength; |
|
2420 } |
|
2421 |
|
2422 void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const |
|
2423 { |
|
2424 ASSERT(index < mUniforms.size()); // index must be smaller than getActiveUniformCount() |
|
2425 |
|
2426 if (bufsize > 0) |
|
2427 { |
|
2428 std::string string = mUniforms[index]->name; |
|
2429 |
|
2430 if (mUniforms[index]->isArray()) |
|
2431 { |
|
2432 string += "[0]"; |
|
2433 } |
|
2434 |
|
2435 strncpy(name, string.c_str(), bufsize); |
|
2436 name[bufsize - 1] = '\0'; |
|
2437 |
|
2438 if (length) |
|
2439 { |
|
2440 *length = strlen(name); |
|
2441 } |
|
2442 } |
|
2443 |
|
2444 *size = mUniforms[index]->elementCount(); |
|
2445 |
|
2446 *type = mUniforms[index]->type; |
|
2447 } |
|
2448 |
|
2449 GLint ProgramBinary::getActiveUniformCount() const |
|
2450 { |
|
2451 return mUniforms.size(); |
|
2452 } |
|
2453 |
|
2454 GLint ProgramBinary::getActiveUniformMaxLength() const |
|
2455 { |
|
2456 int maxLength = 0; |
|
2457 |
|
2458 unsigned int numUniforms = mUniforms.size(); |
|
2459 for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) |
|
2460 { |
|
2461 if (!mUniforms[uniformIndex]->name.empty()) |
|
2462 { |
|
2463 int length = (int)(mUniforms[uniformIndex]->name.length() + 1); |
|
2464 if (mUniforms[uniformIndex]->isArray()) |
|
2465 { |
|
2466 length += 3; // Counting in "[0]". |
|
2467 } |
|
2468 maxLength = std::max(length, maxLength); |
|
2469 } |
|
2470 } |
|
2471 |
|
2472 return maxLength; |
|
2473 } |
|
2474 |
|
2475 void ProgramBinary::validate(InfoLog &infoLog) |
|
2476 { |
|
2477 applyUniforms(); |
|
2478 if (!validateSamplers(&infoLog)) |
|
2479 { |
|
2480 mValidated = false; |
|
2481 } |
|
2482 else |
|
2483 { |
|
2484 mValidated = true; |
|
2485 } |
|
2486 } |
|
2487 |
|
2488 bool ProgramBinary::validateSamplers(InfoLog *infoLog) |
|
2489 { |
|
2490 // if any two active samplers in a program are of different types, but refer to the same |
|
2491 // texture image unit, and this is the current program, then ValidateProgram will fail, and |
|
2492 // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
|
2493 |
|
2494 const unsigned int maxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits(); |
|
2495 TextureType textureUnitType[IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS]; |
|
2496 |
|
2497 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i) |
|
2498 { |
|
2499 textureUnitType[i] = TEXTURE_UNKNOWN; |
|
2500 } |
|
2501 |
|
2502 for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i) |
|
2503 { |
|
2504 if (mSamplersPS[i].active) |
|
2505 { |
|
2506 unsigned int unit = mSamplersPS[i].logicalTextureUnit; |
|
2507 |
|
2508 if (unit >= maxCombinedTextureImageUnits) |
|
2509 { |
|
2510 if (infoLog) |
|
2511 { |
|
2512 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits); |
|
2513 } |
|
2514 |
|
2515 return false; |
|
2516 } |
|
2517 |
|
2518 if (textureUnitType[unit] != TEXTURE_UNKNOWN) |
|
2519 { |
|
2520 if (mSamplersPS[i].textureType != textureUnitType[unit]) |
|
2521 { |
|
2522 if (infoLog) |
|
2523 { |
|
2524 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit); |
|
2525 } |
|
2526 |
|
2527 return false; |
|
2528 } |
|
2529 } |
|
2530 else |
|
2531 { |
|
2532 textureUnitType[unit] = mSamplersPS[i].textureType; |
|
2533 } |
|
2534 } |
|
2535 } |
|
2536 |
|
2537 for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i) |
|
2538 { |
|
2539 if (mSamplersVS[i].active) |
|
2540 { |
|
2541 unsigned int unit = mSamplersVS[i].logicalTextureUnit; |
|
2542 |
|
2543 if (unit >= maxCombinedTextureImageUnits) |
|
2544 { |
|
2545 if (infoLog) |
|
2546 { |
|
2547 infoLog->append("Sampler uniform (%d) exceeds IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, maxCombinedTextureImageUnits); |
|
2548 } |
|
2549 |
|
2550 return false; |
|
2551 } |
|
2552 |
|
2553 if (textureUnitType[unit] != TEXTURE_UNKNOWN) |
|
2554 { |
|
2555 if (mSamplersVS[i].textureType != textureUnitType[unit]) |
|
2556 { |
|
2557 if (infoLog) |
|
2558 { |
|
2559 infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit); |
|
2560 } |
|
2561 |
|
2562 return false; |
|
2563 } |
|
2564 } |
|
2565 else |
|
2566 { |
|
2567 textureUnitType[unit] = mSamplersVS[i].textureType; |
|
2568 } |
|
2569 } |
|
2570 } |
|
2571 |
|
2572 return true; |
|
2573 } |
|
2574 |
|
2575 ProgramBinary::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(TEXTURE_2D) |
|
2576 { |
|
2577 } |
|
2578 |
|
2579 struct AttributeSorter |
|
2580 { |
|
2581 AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS]) |
|
2582 : originalIndices(semanticIndices) |
|
2583 { |
|
2584 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
|
2585 { |
|
2586 indices[i] = i; |
|
2587 } |
|
2588 |
|
2589 std::sort(&indices[0], &indices[MAX_VERTEX_ATTRIBS], *this); |
|
2590 } |
|
2591 |
|
2592 bool operator()(int a, int b) |
|
2593 { |
|
2594 return originalIndices[a] == -1 ? false : originalIndices[a] < originalIndices[b]; |
|
2595 } |
|
2596 |
|
2597 int indices[MAX_VERTEX_ATTRIBS]; |
|
2598 const int (&originalIndices)[MAX_VERTEX_ATTRIBS]; |
|
2599 }; |
|
2600 |
|
2601 void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const |
|
2602 { |
|
2603 AttributeSorter sorter(mSemanticIndex); |
|
2604 |
|
2605 int oldIndices[MAX_VERTEX_ATTRIBS]; |
|
2606 rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS]; |
|
2607 |
|
2608 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
|
2609 { |
|
2610 oldIndices[i] = mSemanticIndex[i]; |
|
2611 oldTranslatedAttributes[i] = attributes[i]; |
|
2612 } |
|
2613 |
|
2614 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
|
2615 { |
|
2616 int oldIndex = sorter.indices[i]; |
|
2617 sortedSemanticIndices[i] = oldIndices[oldIndex]; |
|
2618 attributes[i] = oldTranslatedAttributes[oldIndex]; |
|
2619 } |
|
2620 } |
|
2621 |
|
2622 } |