daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 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 | // Shader.cpp: Implements the gl::Shader class and its derived classes |
| 8 | // VertexShader and FragmentShader. Implements GL shader objects and related |
| 9 | // functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 3.8 page 84. |
| 10 | |
| 11 | #include "Shader.h" |
| 12 | |
| 13 | #include "main.h" |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 14 | #include "GLSLANG/Shaderlang.h" |
| 15 | #include "compiler/OutputHLSL.h" |
| 16 | #include "common/debug.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
| 18 | namespace gl |
| 19 | { |
| 20 | void *Shader::mFragmentCompiler = NULL; |
| 21 | void *Shader::mVertexCompiler = NULL; |
| 22 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame^] | 23 | Shader::Shader(GLuint handle) : mHandle(handle) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | { |
| 25 | mSource = NULL; |
| 26 | mHlsl = NULL; |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 27 | mInfoLog = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 28 | |
| 29 | // Perform a one-time initialization of the shader compiler (or after being destructed by releaseCompiler) |
| 30 | if (!mFragmentCompiler) |
| 31 | { |
| 32 | int result = ShInitialize(); |
| 33 | |
| 34 | if (result) |
| 35 | { |
| 36 | mFragmentCompiler = ShConstructCompiler(EShLangFragment, EDebugOpObjectCode); |
| 37 | mVertexCompiler = ShConstructCompiler(EShLangVertex, EDebugOpObjectCode); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | mAttachCount = 0; |
| 42 | mDeleteStatus = false; |
| 43 | } |
| 44 | |
| 45 | Shader::~Shader() |
| 46 | { |
| 47 | delete[] mSource; |
| 48 | delete[] mHlsl; |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 49 | delete[] mInfoLog; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame^] | 52 | GLuint Shader::getHandle() const |
| 53 | { |
| 54 | return mHandle; |
| 55 | } |
| 56 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 57 | void Shader::setSource(GLsizei count, const char **string, const GLint *length) |
| 58 | { |
| 59 | delete[] mSource; |
| 60 | int totalLength = 0; |
| 61 | |
| 62 | for (int i = 0; i < count; i++) |
| 63 | { |
| 64 | if (length && length[i] >= 0) |
| 65 | { |
| 66 | totalLength += length[i]; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | totalLength += (int)strlen(string[i]); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | mSource = new char[totalLength + 1]; |
| 75 | char *code = mSource; |
| 76 | |
| 77 | for (int i = 0; i < count; i++) |
| 78 | { |
| 79 | int stringLength; |
| 80 | |
| 81 | if (length && length[i] >= 0) |
| 82 | { |
| 83 | stringLength = length[i]; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | stringLength = (int)strlen(string[i]); |
| 88 | } |
| 89 | |
| 90 | strncpy(code, string[i], stringLength); |
| 91 | code += stringLength; |
| 92 | } |
| 93 | |
| 94 | mSource[totalLength] = '\0'; |
| 95 | } |
| 96 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 97 | int Shader::getInfoLogLength() const |
| 98 | { |
| 99 | if (!mInfoLog) |
| 100 | { |
| 101 | return 0; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | return strlen(mInfoLog) + 1; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) |
| 110 | { |
| 111 | int index = 0; |
| 112 | |
| 113 | if (mInfoLog) |
| 114 | { |
| 115 | while (index < bufSize - 1 && index < (int)strlen(mInfoLog)) |
| 116 | { |
| 117 | infoLog[index] = mInfoLog[index]; |
| 118 | index++; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (bufSize) |
| 123 | { |
| 124 | infoLog[index] = '\0'; |
| 125 | } |
| 126 | |
| 127 | if (length) |
| 128 | { |
| 129 | *length = index; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | int Shader::getSourceLength() const |
| 134 | { |
| 135 | if (!mSource) |
| 136 | { |
| 137 | return 0; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | return strlen(mSource) + 1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void Shader::getSource(GLsizei bufSize, GLsizei *length, char *source) |
| 146 | { |
| 147 | int index = 0; |
| 148 | |
| 149 | if (mSource) |
| 150 | { |
| 151 | while (index < bufSize - 1 && index < (int)strlen(mInfoLog)) |
| 152 | { |
| 153 | source[index] = mSource[index]; |
| 154 | index++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (bufSize) |
| 159 | { |
| 160 | source[index] = '\0'; |
| 161 | } |
| 162 | |
| 163 | if (length) |
| 164 | { |
| 165 | *length = index; |
| 166 | } |
| 167 | } |
| 168 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 169 | bool Shader::isCompiled() |
| 170 | { |
| 171 | return mHlsl != NULL; |
| 172 | } |
| 173 | |
| 174 | const char *Shader::linkHLSL() |
| 175 | { |
| 176 | return mHlsl; |
| 177 | } |
| 178 | |
| 179 | void Shader::attach() |
| 180 | { |
| 181 | mAttachCount++; |
| 182 | } |
| 183 | |
| 184 | void Shader::detach() |
| 185 | { |
| 186 | mAttachCount--; |
| 187 | } |
| 188 | |
| 189 | bool Shader::isAttached() const |
| 190 | { |
| 191 | return mAttachCount > 0; |
| 192 | } |
| 193 | |
| 194 | bool Shader::isDeletable() const |
| 195 | { |
| 196 | return mDeleteStatus == true && mAttachCount == 0; |
| 197 | } |
| 198 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 199 | bool Shader::isFlaggedForDeletion() const |
| 200 | { |
| 201 | return mDeleteStatus; |
| 202 | } |
| 203 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 204 | void Shader::flagForDeletion() |
| 205 | { |
| 206 | mDeleteStatus = true; |
| 207 | } |
| 208 | |
| 209 | void Shader::releaseCompiler() |
| 210 | { |
| 211 | ShDestruct(mFragmentCompiler); |
| 212 | ShDestruct(mVertexCompiler); |
| 213 | |
| 214 | mFragmentCompiler = NULL; |
| 215 | mVertexCompiler = NULL; |
| 216 | } |
| 217 | |
| 218 | void Shader::compileToHLSL(void *compiler) |
| 219 | { |
| 220 | if (isCompiled() || !mSource) |
| 221 | { |
| 222 | return; |
| 223 | } |
| 224 | |
daniel@transgaming.com | 0599dc6 | 2010-03-21 04:31:36 +0000 | [diff] [blame] | 225 | TRACE("\n%s", mSource); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 226 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 227 | delete[] mInfoLog; |
| 228 | mInfoLog = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 229 | |
| 230 | TBuiltInResource resources; |
| 231 | |
| 232 | resources.maxVertexAttribs = MAX_VERTEX_ATTRIBS; |
| 233 | resources.maxVertexUniformVectors = MAX_VERTEX_UNIFORM_VECTORS; |
| 234 | resources.maxVaryingVectors = MAX_VARYING_VECTORS; |
| 235 | resources.maxVertexTextureImageUnits = MAX_VERTEX_TEXTURE_IMAGE_UNITS; |
| 236 | resources.maxCombinedTextureImageUnits = MAX_COMBINED_TEXTURE_IMAGE_UNITS; |
| 237 | resources.maxTextureImageUnits = MAX_TEXTURE_IMAGE_UNITS; |
| 238 | resources.maxFragmentUniformVectors = MAX_FRAGMENT_UNIFORM_VECTORS; |
| 239 | resources.maxDrawBuffers = MAX_DRAW_BUFFERS; |
| 240 | |
| 241 | int result = ShCompile(compiler, &mSource, 1, EShOptNone, &resources, EDebugOpObjectCode); |
| 242 | const char *obj = ShGetObjectCode(compiler); |
| 243 | const char *info = ShGetInfoLog(compiler); |
| 244 | |
| 245 | if (result) |
| 246 | { |
| 247 | mHlsl = new char[strlen(obj) + 1]; |
| 248 | strcpy(mHlsl, obj); |
| 249 | |
daniel@transgaming.com | 0599dc6 | 2010-03-21 04:31:36 +0000 | [diff] [blame] | 250 | TRACE("\n%s", mHlsl); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | { |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 254 | mInfoLog = new char[strlen(info) + 1]; |
| 255 | strcpy(mInfoLog, info); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 256 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 257 | TRACE("\n%s", mInfoLog); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | InputMapping::InputMapping() |
| 262 | { |
| 263 | mAttribute = NULL; |
| 264 | mSemanticIndex = -1; |
| 265 | } |
| 266 | |
| 267 | InputMapping::InputMapping(const char *attribute, int semanticIndex) |
| 268 | { |
| 269 | mAttribute = new char[strlen(attribute) + 1]; |
| 270 | strcpy(mAttribute, attribute); |
| 271 | mSemanticIndex = semanticIndex; |
| 272 | } |
| 273 | |
| 274 | InputMapping &InputMapping::operator=(const InputMapping &inputMapping) |
| 275 | { |
| 276 | delete[] mAttribute; |
| 277 | |
| 278 | mAttribute = new char[strlen(inputMapping.mAttribute) + 1]; |
| 279 | strcpy(mAttribute, inputMapping.mAttribute); |
| 280 | mSemanticIndex = inputMapping.mSemanticIndex; |
| 281 | |
| 282 | return *this; |
| 283 | } |
| 284 | |
| 285 | InputMapping::~InputMapping() |
| 286 | { |
| 287 | delete[] mAttribute; |
| 288 | } |
| 289 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame^] | 290 | VertexShader::VertexShader(GLuint handle) : Shader(handle) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 291 | { |
| 292 | } |
| 293 | |
| 294 | VertexShader::~VertexShader() |
| 295 | { |
| 296 | } |
| 297 | |
| 298 | GLenum VertexShader::getType() |
| 299 | { |
| 300 | return GL_VERTEX_SHADER; |
| 301 | } |
| 302 | |
| 303 | void VertexShader::compile() |
| 304 | { |
| 305 | compileToHLSL(mVertexCompiler); |
| 306 | parseAttributes(); |
| 307 | } |
| 308 | |
| 309 | const char *VertexShader::linkHLSL(const char *pixelHLSL) |
| 310 | { |
| 311 | if (mHlsl && pixelHLSL) |
| 312 | { |
| 313 | const char *input = strstr(pixelHLSL, "struct PS_INPUT"); |
| 314 | char *output = strstr(mHlsl, "struct VS_OUTPUT"); |
| 315 | |
| 316 | while (*input != '}' && output) |
| 317 | { |
| 318 | char varyingName[100]; |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 319 | unsigned int semanticIndex; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 320 | int matches = sscanf(input, "%s : TEXCOORD%d;", varyingName, &semanticIndex); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 321 | |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 322 | if (matches == 2 && semanticIndex != sh::HLSL_FRAG_COORD_SEMANTIC) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 323 | { |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 324 | ASSERT(semanticIndex < MAX_VARYING_VECTORS); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 325 | char *varying = strstr(output, varyingName); |
daniel@transgaming.com | c7d8a93 | 2010-03-16 06:16:45 +0000 | [diff] [blame] | 326 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 327 | if (varying) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 328 | { |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 329 | ASSERT(semanticIndex <= 9); // Single character |
| 330 | varying = strstr(varying, " : TEXCOORD0;"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 331 | varying[11] = '0' + semanticIndex; |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | input = strstr(input, ";"); |
| 339 | } |
| 340 | |
| 341 | input++; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return mHlsl; |
| 346 | } |
| 347 | |
| 348 | const char *VertexShader::getAttributeName(unsigned int attributeIndex) |
| 349 | { |
| 350 | if (attributeIndex < MAX_VERTEX_ATTRIBS) |
| 351 | { |
| 352 | return mInputMapping[attributeIndex].mAttribute; |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | bool VertexShader::isActiveAttribute(const char *attributeName) |
| 359 | { |
| 360 | return getInputMapping(attributeName) != -1; |
| 361 | } |
| 362 | |
| 363 | int VertexShader::getInputMapping(const char *attributeName) |
| 364 | { |
| 365 | if (attributeName) |
| 366 | { |
| 367 | for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++) |
| 368 | { |
| 369 | if (mInputMapping[index].mAttribute && strcmp(mInputMapping[index].mAttribute, attributeName) == 0) |
| 370 | { |
| 371 | return mInputMapping[index].mSemanticIndex; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | void VertexShader::parseAttributes() |
| 380 | { |
| 381 | if (mHlsl) |
| 382 | { |
| 383 | const char *input = strstr(mHlsl, "struct VS_INPUT"); |
| 384 | |
| 385 | for (int attributeIndex = 0; *input != '}'; input++) |
| 386 | { |
| 387 | char attributeName[100]; |
| 388 | int semanticIndex; |
| 389 | |
| 390 | int matches = sscanf(input, "%s : TEXCOORD%d;", attributeName, &semanticIndex); |
| 391 | |
| 392 | if (matches == 2) |
| 393 | { |
| 394 | ASSERT(attributeIndex < MAX_VERTEX_ATTRIBS); |
| 395 | mInputMapping[attributeIndex] = InputMapping(attributeName, semanticIndex); |
| 396 | attributeIndex++; |
| 397 | |
| 398 | input = strstr(input, ";"); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame^] | 404 | FragmentShader::FragmentShader(GLuint handle) : Shader(handle) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 405 | { |
| 406 | } |
| 407 | |
| 408 | FragmentShader::~FragmentShader() |
| 409 | { |
| 410 | } |
| 411 | |
| 412 | GLenum FragmentShader::getType() |
| 413 | { |
| 414 | return GL_FRAGMENT_SHADER; |
| 415 | } |
| 416 | |
| 417 | void FragmentShader::compile() |
| 418 | { |
| 419 | compileToHLSL(mFragmentCompiler); |
| 420 | } |
| 421 | } |