blob: 7cdd898d8e811fd6372e5fddad6610cdd97bd718 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 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
10#include "Program.h"
11
12#include "main.h"
13#include "Shader.h"
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
16namespace gl
17{
daniel@transgaming.com0361b922010-03-28 19:36:15 +000018Uniform::Uniform(GLenum type, const std::string &name, unsigned int bytes) : type(type), name(name), bytes(bytes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019{
20 this->data = new unsigned char[bytes];
21 memset(this->data, 0, bytes);
22}
23
24Uniform::~Uniform()
25{
26 delete[] data;
27}
28
29Program::Program()
30{
31 mFragmentShader = NULL;
32 mVertexShader = NULL;
33
34 mPixelExecutable = NULL;
35 mVertexExecutable = NULL;
36 mConstantTablePS = NULL;
37 mConstantTableVS = NULL;
38
39 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
40 {
41 mAttributeName[index] = NULL;
42 }
43
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +000044 mPixelHLSL = NULL;
45 mVertexHLSL = NULL;
daniel@transgaming.comcba50572010-03-28 19:36:09 +000046 mInfoLog = NULL;
47
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 unlink();
49
50 mDeleteStatus = false;
51}
52
53Program::~Program()
54{
55 unlink(true);
56}
57
58bool Program::attachShader(Shader *shader)
59{
60 if (shader->getType() == GL_VERTEX_SHADER)
61 {
62 if (mVertexShader)
63 {
64 return false;
65 }
66
67 mVertexShader = (VertexShader*)shader;
68 mVertexShader->attach();
69 }
70 else if (shader->getType() == GL_FRAGMENT_SHADER)
71 {
72 if (mFragmentShader)
73 {
74 return false;
75 }
76
77 mFragmentShader = (FragmentShader*)shader;
78 mFragmentShader->attach();
79 }
80 else UNREACHABLE();
81
82 return true;
83}
84
85bool Program::detachShader(Shader *shader)
86{
87 if (shader->getType() == GL_VERTEX_SHADER)
88 {
89 if (mVertexShader != shader)
90 {
91 return false;
92 }
93
94 mVertexShader->detach();
95 mVertexShader = NULL;
96 }
97 else if (shader->getType() == GL_FRAGMENT_SHADER)
98 {
99 if (mFragmentShader != shader)
100 {
101 return false;
102 }
103
104 mFragmentShader->detach();
105 mFragmentShader = NULL;
106 }
107 else UNREACHABLE();
108
109 unlink();
110
111 return true;
112}
113
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000114int Program::getAttachedShadersCount() const
115{
116 return (mVertexShader ? 1 : 0) + (mFragmentShader ? 1 : 0);
117}
118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119IDirect3DPixelShader9 *Program::getPixelShader()
120{
121 return mPixelExecutable;
122}
123
124IDirect3DVertexShader9 *Program::getVertexShader()
125{
126 return mVertexExecutable;
127}
128
129void Program::bindAttributeLocation(GLuint index, const char *name)
130{
131 if (index < MAX_VERTEX_ATTRIBS)
132 {
133 delete[] mAttributeName[index];
134 mAttributeName[index] = new char[strlen(name) + 1];
135 strcpy(mAttributeName[index], name);
136 }
137}
138
139GLuint Program::getAttributeLocation(const char *name)
140{
141 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
142 {
143 if (mAttributeName[index] && strcmp(mAttributeName[index], name) == 0)
144 {
145 return index;
146 }
147 }
148
149 return -1;
150}
151
152bool Program::isActiveAttribute(int attributeIndex)
153{
154 if (attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS)
155 {
156 return mInputMapping[attributeIndex] != -1;
157 }
158
159 return false;
160}
161
162int Program::getInputMapping(int attributeIndex)
163{
164 if (attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS)
165 {
166 return mInputMapping[attributeIndex];
167 }
168
169 return -1;
170}
171
172// Returns the index of the texture unit corresponding to a Direct3D 9 sampler
173// index referenced in the compiled HLSL shader
174GLint Program::getSamplerMapping(unsigned int samplerIndex)
175{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000176 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
177
178 if (mSamplers[samplerIndex].active)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000180 return mSamplers[samplerIndex].logicalTextureUnit;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 }
182
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000183 return -1;
184}
185
186SamplerType Program::getSamplerType(unsigned int samplerIndex)
187{
188 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
189 assert(mSamplers[samplerIndex].active);
190
191 return mSamplers[samplerIndex].type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192}
193
194GLint Program::getUniformLocation(const char *name)
195{
196 for (unsigned int location = 0; location < mUniforms.size(); location++)
197 {
198 if (mUniforms[location]->name == name)
199 {
200 return location;
201 }
202 }
203
204 return -1;
205}
206
207bool Program::setUniform1fv(GLint location, GLsizei count, const GLfloat* v)
208{
209 if (location < 0 || location >= (int)mUniforms.size())
210 {
211 return false;
212 }
213
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000214 if (mUniforms[location]->type != GL_FLOAT || mUniforms[location]->bytes < sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215 {
216 return false;
217 }
218
219 memcpy(mUniforms[location]->data, v, sizeof(GLfloat) * count);
220
221 return true;
222}
223
224bool Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
225{
226 if (location < 0 || location >= (int)mUniforms.size())
227 {
228 return false;
229 }
230
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000231 if (mUniforms[location]->type != GL_FLOAT_VEC2 || mUniforms[location]->bytes < 2 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232 {
233 return false;
234 }
235
236 memcpy(mUniforms[location]->data, v, 2 * sizeof(GLfloat) * count);
237
238 return true;
239}
240
241bool Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
242{
243 if (location < 0 || location >= (int)mUniforms.size())
244 {
245 return false;
246 }
247
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000248 if (mUniforms[location]->type != GL_FLOAT_VEC3 || mUniforms[location]->bytes < 3 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249 {
250 return false;
251 }
252
253 memcpy(mUniforms[location]->data, v, 3 * sizeof(GLfloat) * count);
254
255 return true;
256}
257
258bool Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
259{
260 if (location < 0 || location >= (int)mUniforms.size())
261 {
262 return false;
263 }
264
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000265 if (mUniforms[location]->type != GL_FLOAT_VEC4 || mUniforms[location]->bytes < 4 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266 {
267 return false;
268 }
269
270 memcpy(mUniforms[location]->data, v, 4 * sizeof(GLfloat) * count);
271
272 return true;
273}
274
275bool Program::setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
276{
277 if (location < 0 || location >= (int)mUniforms.size())
278 {
279 return false;
280 }
281
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000282 if (mUniforms[location]->type != GL_FLOAT_MAT2 || mUniforms[location]->bytes < 4 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283 {
284 return false;
285 }
286
287 memcpy(mUniforms[location]->data, value, 4 * sizeof(GLfloat) * count);
288
289 return true;
290}
291
292bool Program::setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
293{
294 if (location < 0 || location >= (int)mUniforms.size())
295 {
296 return false;
297 }
298
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000299 if (mUniforms[location]->type != GL_FLOAT_MAT3 || mUniforms[location]->bytes < 9 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 {
301 return false;
302 }
303
304 memcpy(mUniforms[location]->data, value, 9 * sizeof(GLfloat) * count);
305
306 return true;
307}
308
309bool Program::setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
310{
311 if (location < 0 || location >= (int)mUniforms.size())
312 {
313 return false;
314 }
315
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000316 if (mUniforms[location]->type != GL_FLOAT_MAT4 || mUniforms[location]->bytes < 16 * sizeof(GLfloat) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317 {
318 return false;
319 }
320
321 memcpy(mUniforms[location]->data, value, 16 * sizeof(GLfloat) * count);
322
323 return true;
324}
325
326bool Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
327{
328 if (location < 0 || location >= (int)mUniforms.size())
329 {
330 return false;
331 }
332
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000333 if (mUniforms[location]->type != GL_INT || mUniforms[location]->bytes < sizeof(GLint) * count)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334 {
335 return false;
336 }
337
338 memcpy(mUniforms[location]->data, v, sizeof(GLint) * count);
339
340 return true;
341}
342
343// Applies all the uniforms set for this program object to the Direct3D 9 device
344void Program::applyUniforms()
345{
346 for (unsigned int location = 0; location < mUniforms.size(); location++)
347 {
348 int bytes = mUniforms[location]->bytes;
349 GLfloat *f = (GLfloat*)mUniforms[location]->data;
350 GLint *i = (GLint*)mUniforms[location]->data;
351
352 switch (mUniforms[location]->type)
353 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000354 case GL_FLOAT: applyUniform1fv(location, bytes / sizeof(GLfloat), f); break;
355 case GL_FLOAT_VEC2: applyUniform2fv(location, bytes / 2 / sizeof(GLfloat), f); break;
356 case GL_FLOAT_VEC3: applyUniform3fv(location, bytes / 3 / sizeof(GLfloat), f); break;
357 case GL_FLOAT_VEC4: applyUniform4fv(location, bytes / 4 / sizeof(GLfloat), f); break;
358 case GL_FLOAT_MAT2: applyUniformMatrix2fv(location, bytes / 4 / sizeof(GLfloat), f); break;
359 case GL_FLOAT_MAT3: applyUniformMatrix3fv(location, bytes / 9 / sizeof(GLfloat), f); break;
360 case GL_FLOAT_MAT4: applyUniformMatrix4fv(location, bytes / 16 / sizeof(GLfloat), f); break;
361 case GL_INT: applyUniform1iv(location, bytes / sizeof(GLint), i); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362 default:
363 UNIMPLEMENTED(); // FIXME
364 UNREACHABLE();
365 }
366 }
367}
368
369// Compiles the HLSL code of the attached shaders into executable binaries
370ID3DXBuffer *Program::compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable)
371{
372 if (!hlsl)
373 {
374 return NULL;
375 }
376
377 ID3DXBuffer *binary = NULL;
378 ID3DXBuffer *errorMessage = NULL;
379
daniel@transgaming.comcbbca002010-04-01 13:39:32 +0000380 HRESULT result = D3DXCompileShader(hlsl, (UINT)strlen(hlsl), NULL, 0, "main", profile, 0, &binary, &errorMessage, constantTable);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
382 if (SUCCEEDED(result))
383 {
384 return binary;
385 }
386
387 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
388 {
389 return error(GL_OUT_OF_MEMORY, (ID3DXBuffer*)NULL);
390 }
391
392 if (errorMessage)
393 {
394 const char *message = (const char*)errorMessage->GetBufferPointer();
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000395
daniel@transgaming.com0599dc62010-03-21 04:31:36 +0000396 TRACE("\n%s", hlsl);
397 TRACE("\n%s", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398 }
399
400 return NULL;
401}
402
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000403void Program::parseVaryings(const char *structure, char *hlsl, VaryingArray &varyings)
404{
405 char *input = strstr(hlsl, structure);
406 input += strlen(structure);
407
408 while (input && *input != '}')
409 {
410 char varyingType[256];
411 char varyingName[256];
412 unsigned int semanticIndex;
413 int matches = sscanf(input, " %s %s : TEXCOORD%d;", varyingType, varyingName, &semanticIndex);
414
415 if (matches == 3)
416 {
417 ASSERT(semanticIndex <= 9); // Single character
418
419 varyings.push_back(Varying(varyingName, input));
420 }
421
422 input = strstr(input, ";");
423 input += 2;
424 }
425}
426
427bool Program::linkVaryings()
428{
429 if (!mPixelHLSL || !mVertexHLSL)
430 {
431 return false;
432 }
433
434 VaryingArray vertexVaryings;
435 VaryingArray pixelVaryings;
436
437 parseVaryings("struct VS_OUTPUT\n{\n", mVertexHLSL, vertexVaryings);
438 parseVaryings("struct PS_INPUT\n{\n", mPixelHLSL, pixelVaryings);
439
440 for (unsigned int out = 0; out < vertexVaryings.size(); out++)
441 {
442 unsigned int in;
443 for (in = 0; in < pixelVaryings.size(); in++)
444 {
445 if (vertexVaryings[out].name == pixelVaryings[in].name)
446 {
447 pixelVaryings[in].link = out;
448 vertexVaryings[out].link = in;
449
450 break;
451 }
452 }
453
454 if (in != pixelVaryings.size())
455 {
456 // FIXME: Verify matching type and qualifiers
457
458 char *outputSemantic = strstr(vertexVaryings[out].declaration, " : TEXCOORD");
459 char *inputSemantic = strstr(pixelVaryings[in].declaration, " : TEXCOORD");
460 outputSemantic[11] = inputSemantic[11];
461 }
462 else
463 {
464 // Comment out the declaration and output assignment
465 vertexVaryings[out].declaration[0] = '/';
466 vertexVaryings[out].declaration[1] = '/';
467
468 char outputString[256];
469 sprintf(outputString, " output.%s = ", vertexVaryings[out].name.c_str());
470 char *varyingOutput = strstr(mVertexHLSL, outputString);
471
472 varyingOutput[0] = '/';
473 varyingOutput[1] = '/';
474 }
475 }
476
477 // Verify that each pixel varying has been linked to a vertex varying
478 for (unsigned int in = 0; in < pixelVaryings.size(); in++)
479 {
480 if (pixelVaryings[in].link < 0)
481 {
482 return false;
483 }
484 }
485
486 return true;
487}
488
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
490// compiling them into binaries, determining the attribute mappings, and collecting
491// a list of uniforms
492void Program::link()
493{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494 unlink();
495
496 if (!mFragmentShader || !mFragmentShader->isCompiled())
497 {
498 return;
499 }
500
501 if (!mVertexShader || !mVertexShader->isCompiled())
502 {
503 return;
504 }
505
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000506 Context *context = getContext();
507 const char *vertexProfile = context->getVertexShaderProfile();
508 const char *pixelProfile = context->getPixelShaderProfile();
daniel@transgaming.comdebe2592010-03-24 09:44:08 +0000509
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000510 const char *ps = mFragmentShader->getHLSL();
511 const char *vs = mVertexShader->getHLSL();
512
513 mPixelHLSL = new char[strlen(ps) + 1];
514 strcpy(mPixelHLSL, ps);
515 mVertexHLSL = new char[strlen(vs) + 1];
516 strcpy(mVertexHLSL, vs);
517
518 if (!linkVaryings())
519 {
520 return;
521 }
522
523 ID3DXBuffer *vertexBinary = compileToBinary(mVertexHLSL, vertexProfile, &mConstantTableVS);
524 ID3DXBuffer *pixelBinary = compileToBinary(mPixelHLSL, pixelProfile, &mConstantTablePS);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525
526 if (vertexBinary && pixelBinary)
527 {
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000528 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529 HRESULT vertexResult = device->CreateVertexShader((DWORD*)vertexBinary->GetBufferPointer(), &mVertexExecutable);
530 HRESULT pixelResult = device->CreatePixelShader((DWORD*)pixelBinary->GetBufferPointer(), &mPixelExecutable);
531
532 if (vertexResult == D3DERR_OUTOFVIDEOMEMORY || vertexResult == E_OUTOFMEMORY || pixelResult == D3DERR_OUTOFVIDEOMEMORY || pixelResult == E_OUTOFMEMORY)
533 {
534 return error(GL_OUT_OF_MEMORY);
535 }
536
537 ASSERT(SUCCEEDED(vertexResult) && SUCCEEDED(pixelResult));
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000538
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539 vertexBinary->Release();
540 pixelBinary->Release();
541 vertexBinary = NULL;
542 pixelBinary = NULL;
543
544 if (mVertexExecutable && mPixelExecutable)
545 {
546 if (!linkAttributes())
547 {
548 return;
549 }
550
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000551 for (int i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
552 {
553 mSamplers[i].active = false;
554 }
555
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000556 if (!linkUniforms(mConstantTablePS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000558 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559 }
560
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000561 if (!linkUniforms(mConstantTableVS))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000563 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564 }
565
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000566 mLinked = true; // Success
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567 }
568 }
569}
570
571// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
572bool Program::linkAttributes()
573{
574 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
575 {
576 const char *name = mVertexShader->getAttributeName(attributeIndex);
577
578 if (name)
579 {
580 GLuint location = getAttributeLocation(name);
581
582 if (location == -1) // Not set by glBindAttribLocation
583 {
584 int availableIndex = 0;
585
586 while (availableIndex < MAX_VERTEX_ATTRIBS && mAttributeName[availableIndex] && mVertexShader->isActiveAttribute(mAttributeName[availableIndex]))
587 {
588 availableIndex++;
589 }
590
591 if (availableIndex == MAX_VERTEX_ATTRIBS)
592 {
593 return false; // Fail to link
594 }
595
596 delete[] mAttributeName[availableIndex];
597 mAttributeName[availableIndex] = new char[strlen(name) + 1]; // FIXME: Check allocation
598 strcpy(mAttributeName[availableIndex], name);
599 }
600 }
601 }
602
603 for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
604 {
605 mInputMapping[attributeIndex] = mVertexShader->getInputMapping(mAttributeName[attributeIndex]);
606 }
607
608 return true;
609}
610
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000611bool Program::linkUniforms(ID3DXConstantTable *constantTable)
612{
613 D3DXCONSTANTTABLE_DESC constantTableDescription;
614 D3DXCONSTANT_DESC constantDescription;
615 UINT descriptionCount = 1;
616
617 constantTable->GetDesc(&constantTableDescription);
618
619 for (unsigned int constantIndex = 0; constantIndex < constantTableDescription.Constants; constantIndex++)
620 {
621 D3DXHANDLE constantHandle = constantTable->GetConstant(0, constantIndex);
622 constantTable->GetConstantDesc(constantHandle, &constantDescription, &descriptionCount);
623
624 if (!defineUniform(constantHandle, constantDescription))
625 {
626 return false;
627 }
628 }
629
630 return true;
631}
632
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633// Adds the description of a constant found in the binary shader to the list of uniforms
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000634// Returns true if succesful (uniform not already defined)
635bool Program::defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name)
636{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000637 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
638 {
639 unsigned int samplerIndex = constantDescription.RegisterIndex;
640
641 assert(samplerIndex < sizeof(mSamplers)/sizeof(mSamplers[0]));
642
643 mSamplers[samplerIndex].active = true;
644 mSamplers[samplerIndex].type = (constantDescription.Type == D3DXPT_SAMPLERCUBE) ? SAMPLER_CUBE : SAMPLER_2D;
645 mSamplers[samplerIndex].logicalTextureUnit = 0;
646 }
647
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000648 switch(constantDescription.Class)
649 {
650 case D3DXPC_STRUCT:
651 {
652 for (unsigned int field = 0; field < constantDescription.StructMembers; field++)
653 {
654 D3DXHANDLE fieldHandle = mConstantTablePS->GetConstant(constantHandle, field);
655
656 D3DXCONSTANT_DESC fieldDescription;
657 UINT descriptionCount = 1;
658
659 mConstantTablePS->GetConstantDesc(fieldHandle, &fieldDescription, &descriptionCount);
660
661 if (!defineUniform(fieldHandle, fieldDescription, name + constantDescription.Name + "."))
662 {
663 return false;
664 }
665 }
666
667 return true;
668 }
669 case D3DXPC_SCALAR:
670 case D3DXPC_VECTOR:
671 case D3DXPC_MATRIX_COLUMNS:
672 case D3DXPC_OBJECT:
673 return defineUniform(constantDescription, name + constantDescription.Name);
674 default:
675 UNREACHABLE();
676 return false;
677 }
678}
679
680bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
681{
682 Uniform *uniform = createUniform(constantDescription, name);
683
684 if(!uniform)
685 {
686 return false;
687 }
688
689 // Check if already defined
690 GLint location = getUniformLocation(name.c_str());
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000691 GLenum type = uniform->type;
daniel@transgaming.comc7d8a932010-03-16 06:16:45 +0000692
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000693 if (location >= 0)
694 {
695 delete uniform;
696
697 if (mUniforms[location]->type != type)
698 {
699 return false;
700 }
701 else
702 {
703 return true;
704 }
705 }
706
707 mUniforms.push_back(uniform);
708
709 return true;
710}
711
712Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713{
714 if (constantDescription.Rows == 1) // Vectors and scalars
715 {
716 switch (constantDescription.Type)
717 {
718 case D3DXPT_SAMPLER2D:
719 case D3DXPT_SAMPLERCUBE:
720 case D3DXPT_BOOL:
721 switch (constantDescription.Columns)
722 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000723 case 1: return new Uniform(GL_INT, name, 1 * sizeof(GLint) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724 default:
725 UNIMPLEMENTED(); // FIXME
726 UNREACHABLE();
727 }
728 break;
729 case D3DXPT_FLOAT:
730 switch (constantDescription.Columns)
731 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000732 case 1: return new Uniform(GL_FLOAT, name, 1 * sizeof(GLfloat) * constantDescription.Elements);
733 case 2: return new Uniform(GL_FLOAT_VEC2, name, 2 * sizeof(GLfloat) * constantDescription.Elements);
734 case 3: return new Uniform(GL_FLOAT_VEC3, name, 3 * sizeof(GLfloat) * constantDescription.Elements);
735 case 4: return new Uniform(GL_FLOAT_VEC4, name, 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736 default: UNREACHABLE();
737 }
738 break;
739 default:
740 UNIMPLEMENTED(); // FIXME
741 UNREACHABLE();
742 }
743 }
744 else if (constantDescription.Rows == constantDescription.Columns) // Square matrices
745 {
746 switch (constantDescription.Type)
747 {
748 case D3DXPT_FLOAT:
749 switch (constantDescription.Rows)
750 {
daniel@transgaming.com0361b922010-03-28 19:36:15 +0000751 case 2: return new Uniform(GL_FLOAT_MAT2, name, 2 * 2 * sizeof(GLfloat) * constantDescription.Elements);
752 case 3: return new Uniform(GL_FLOAT_MAT3, name, 3 * 3 * sizeof(GLfloat) * constantDescription.Elements);
753 case 4: return new Uniform(GL_FLOAT_MAT4, name, 4 * 4 * sizeof(GLfloat) * constantDescription.Elements);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754 default: UNREACHABLE();
755 }
756 break;
757 default: UNREACHABLE();
758 }
759 }
760 else UNREACHABLE();
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000761
762 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763}
764
765bool Program::applyUniform1fv(GLint location, GLsizei count, const GLfloat *v)
766{
767 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
768 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
769 IDirect3DDevice9 *device = getDevice();
770
771 if (constantPS)
772 {
773 mConstantTablePS->SetFloatArray(device, constantPS, v, count);
774 }
775
776 if (constantVS)
777 {
778 mConstantTableVS->SetFloatArray(device, constantVS, v, count);
779 }
780
781 return true;
782}
783
784bool Program::applyUniform2fv(GLint location, GLsizei count, const GLfloat *v)
785{
786 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
787
788 for (int i = 0; i < count; i++)
789 {
790 vector[i] = D3DXVECTOR4(v[0], v[1], 0, 0);
791
792 v += 2;
793 }
794
795 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
796 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
797 IDirect3DDevice9 *device = getDevice();
798
799 if (constantPS)
800 {
801 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
802 }
803
804 if (constantVS)
805 {
806 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
807 }
808
809 delete[] vector;
810
811 return true;
812}
813
814bool Program::applyUniform3fv(GLint location, GLsizei count, const GLfloat *v)
815{
816 D3DXVECTOR4 *vector = new D3DXVECTOR4[count];
817
818 for (int i = 0; i < count; i++)
819 {
820 vector[i] = D3DXVECTOR4(v[0], v[1], v[2], 0);
821
822 v += 3;
823 }
824
825 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
826 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
827 IDirect3DDevice9 *device = getDevice();
828
829 if (constantPS)
830 {
831 mConstantTablePS->SetVectorArray(device, constantPS, vector, count);
832 }
833
834 if (constantVS)
835 {
836 mConstantTableVS->SetVectorArray(device, constantVS, vector, count);
837 }
838
839 delete[] vector;
840
841 return true;
842}
843
844bool Program::applyUniform4fv(GLint location, GLsizei count, const GLfloat *v)
845{
846 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
847 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
848 IDirect3DDevice9 *device = getDevice();
849
850 if (constantPS)
851 {
852 mConstantTablePS->SetVectorArray(device, constantPS, (D3DXVECTOR4*)v, count);
853 }
854
855 if (constantVS)
856 {
857 mConstantTableVS->SetVectorArray(device, constantVS, (D3DXVECTOR4*)v, count);
858 }
859
860 return true;
861}
862
863bool Program::applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value)
864{
865 D3DXMATRIX *matrix = new D3DXMATRIX[count];
866
867 for (int i = 0; i < count; i++)
868 {
869 matrix[i] = D3DXMATRIX(value[0], value[2], 0, 0,
870 value[1], value[3], 0, 0,
871 0, 0, 1, 0,
872 0, 0, 0, 1);
873
874 value += 4;
875 }
876
877 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
878 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
879 IDirect3DDevice9 *device = getDevice();
880
881 if (constantPS)
882 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000883 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884 }
885
886 if (constantVS)
887 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000888 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889 }
890
891 delete[] matrix;
892
893 return true;
894}
895
896bool Program::applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value)
897{
898 D3DXMATRIX *matrix = new D3DXMATRIX[count];
899
900 for (int i = 0; i < count; i++)
901 {
902 matrix[i] = D3DXMATRIX(value[0], value[3], value[6], 0,
903 value[1], value[4], value[7], 0,
904 value[2], value[5], value[8], 0,
905 0, 0, 0, 1);
906
907 value += 9;
908 }
909
910 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
911 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
912 IDirect3DDevice9 *device = getDevice();
913
914 if (constantPS)
915 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000916 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917 }
918
919 if (constantVS)
920 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000921 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922 }
923
924 delete[] matrix;
925
926 return true;
927}
928
929bool Program::applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value)
930{
931 D3DXMATRIX *matrix = new D3DXMATRIX[count];
932
933 for (int i = 0; i < count; i++)
934 {
935 matrix[i] = D3DXMATRIX(value[0], value[4], value[8], value[12],
936 value[1], value[5], value[9], value[13],
937 value[2], value[6], value[10], value[14],
938 value[3], value[7], value[11], value[15]);
939
940 value += 16;
941 }
942
943 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
944 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
945 IDirect3DDevice9 *device = getDevice();
946
947 if (constantPS)
948 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000949 mConstantTablePS->SetMatrixTransposeArray(device, constantPS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950 }
951
952 if (constantVS)
953 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000954 mConstantTableVS->SetMatrixTransposeArray(device, constantVS, matrix, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955 }
956
957 delete[] matrix;
958
959 return true;
960}
961
962bool Program::applyUniform1iv(GLint location, GLsizei count, const GLint *v)
963{
964 D3DXHANDLE constantPS = mConstantTablePS->GetConstantByName(0, mUniforms[location]->name.c_str());
965 D3DXHANDLE constantVS = mConstantTableVS->GetConstantByName(0, mUniforms[location]->name.c_str());
966 IDirect3DDevice9 *device = getDevice();
967
968 if (constantPS)
969 {
970 D3DXCONSTANT_DESC constantDescription;
971 UINT descriptionCount = 1;
972 HRESULT result = mConstantTablePS->GetConstantDesc(constantPS, &constantDescription, &descriptionCount);
973
daniel@transgaming.com2884b782010-03-08 21:30:48 +0000974 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975 {
976 return false;
977 }
978
979 if (constantDescription.RegisterSet == D3DXRS_SAMPLER)
980 {
981 unsigned int firstIndex = mConstantTablePS->GetSamplerIndex(constantPS);
982
983 for (unsigned int samplerIndex = firstIndex; samplerIndex < firstIndex + count; samplerIndex++)
984 {
985 GLint mappedSampler = v[0];
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000986
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987 if (mappedSampler >= 0 && mappedSampler < MAX_TEXTURE_IMAGE_UNITS)
988 {
989 if (samplerIndex >= 0 && samplerIndex < MAX_TEXTURE_IMAGE_UNITS)
990 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000991 ASSERT(mSamplers[samplerIndex].active);
992 mSamplers[samplerIndex].logicalTextureUnit = mappedSampler;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993 }
994 }
995 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000996
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997 return true;
998 }
999 }
1000
1001 if (constantPS)
1002 {
1003 mConstantTablePS->SetIntArray(device, constantPS, v, count);
1004 }
1005
1006 if (constantVS)
1007 {
1008 mConstantTableVS->SetIntArray(device, constantVS, v, count);
1009 }
1010
1011 return true;
1012}
1013
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001014void Program::appendToInfoLog(const char *info)
1015{
1016 if (!info)
1017 {
1018 return;
1019 }
1020
1021 size_t infoLength = strlen(info);
1022
1023 if (!mInfoLog)
1024 {
1025 mInfoLog = new char[infoLength + 1];
1026 strcpy(mInfoLog, info);
1027 }
1028 else
1029 {
1030 size_t logLength = strlen(mInfoLog);
1031 char *newLog = new char[logLength + infoLength + 1];
1032 strcpy(newLog, mInfoLog);
1033 strcpy(newLog + logLength, info);
1034
1035 delete[] mInfoLog;
1036 mInfoLog = newLog;
1037 }
1038}
1039
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040// Returns the program object to an unlinked state, after detaching a shader, before re-linking, or at destruction
1041void Program::unlink(bool destroy)
1042{
1043 if (destroy) // Object being destructed
1044 {
1045 if (mFragmentShader)
1046 {
1047 mFragmentShader->detach();
1048 mFragmentShader = NULL;
1049 }
1050
1051 if (mVertexShader)
1052 {
1053 mVertexShader->detach();
1054 mVertexShader = NULL;
1055 }
1056
1057 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1058 {
1059 delete[] mAttributeName[index];
1060 mAttributeName[index] = NULL;
1061 }
1062 }
1063
1064 if (mPixelExecutable)
1065 {
1066 mPixelExecutable->Release();
1067 mPixelExecutable = NULL;
1068 }
1069
1070 if (mVertexExecutable)
1071 {
1072 mVertexExecutable->Release();
1073 mVertexExecutable = NULL;
1074 }
1075
1076 if (mConstantTablePS)
1077 {
1078 mConstantTablePS->Release();
1079 mConstantTablePS = NULL;
1080 }
1081
1082 if (mConstantTableVS)
1083 {
1084 mConstantTableVS->Release();
1085 mConstantTableVS = NULL;
1086 }
1087
1088 for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
1089 {
1090 mInputMapping[index] = 0;
1091 }
1092
1093 for (int index = 0; index < MAX_TEXTURE_IMAGE_UNITS; index++)
1094 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001095 mSamplers[index].active = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096 }
1097
1098 while (!mUniforms.empty())
1099 {
1100 delete mUniforms.back();
1101 mUniforms.pop_back();
1102 }
1103
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +00001104 delete[] mPixelHLSL;
1105 mPixelHLSL = NULL;
1106
1107 delete[] mVertexHLSL;
1108 mVertexHLSL = NULL;
1109
1110 delete[] mInfoLog;
1111 mInfoLog = NULL;
1112
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113 mLinked = false;
1114}
1115
1116bool Program::isLinked()
1117{
1118 return mLinked;
1119}
1120
daniel@transgaming.comcba50572010-03-28 19:36:09 +00001121int Program::getInfoLogLength() const
1122{
1123 if (!mInfoLog)
1124 {
1125 return 0;
1126 }
1127 else
1128 {
1129 return strlen(mInfoLog) + 1;
1130 }
1131}
1132
1133void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
1134{
1135 int index = 0;
1136
1137 if (mInfoLog)
1138 {
1139 while (index < bufSize - 1 && index < (int)strlen(mInfoLog))
1140 {
1141 infoLog[index] = mInfoLog[index];
1142 index++;
1143 }
1144 }
1145
1146 if (bufSize)
1147 {
1148 infoLog[index] = '\0';
1149 }
1150
1151 if (length)
1152 {
1153 *length = index;
1154 }
1155}
1156
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001157void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
1158{
1159 int total = 0;
1160
1161 if (mVertexShader)
1162 {
1163 if (total < maxCount)
1164 {
1165 shaders[total] = mVertexShader->getHandle();
1166 }
1167
1168 total++;
1169 }
1170
1171 if (mFragmentShader)
1172 {
1173 if (total < maxCount)
1174 {
1175 shaders[total] = mFragmentShader->getHandle();
1176 }
1177
1178 total++;
1179 }
1180
1181 if (count)
1182 {
1183 *count = total;
1184 }
1185}
1186
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187void Program::flagForDeletion()
1188{
1189 mDeleteStatus = true;
1190}
1191
1192bool Program::isFlaggedForDeletion() const
1193{
1194 return mDeleteStatus;
1195}
1196}