Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2014 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 | // ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl. |
| 8 | |
| 9 | #include "libGLESv2/renderer/d3d/ProgramD3D.h" |
| 10 | |
| 11 | #include "common/utilities.h" |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 12 | #include "libGLESv2/Framebuffer.h" |
| 13 | #include "libGLESv2/FramebufferAttachment.h" |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 14 | #include "libGLESv2/Program.h" |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 15 | #include "libGLESv2/ProgramBinary.h" |
| 16 | #include "libGLESv2/renderer/Renderer.h" |
| 17 | #include "libGLESv2/renderer/ShaderExecutable.h" |
| 18 | #include "libGLESv2/renderer/d3d/DynamicHLSL.h" |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 19 | #include "libGLESv2/renderer/d3d/ShaderD3D.h" |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 20 | #include "libGLESv2/main.h" |
| 21 | |
| 22 | namespace rx |
| 23 | { |
| 24 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 25 | namespace |
| 26 | { |
| 27 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 28 | GLenum GetTextureType(GLenum samplerType) |
| 29 | { |
| 30 | switch (samplerType) |
| 31 | { |
| 32 | case GL_SAMPLER_2D: |
| 33 | case GL_INT_SAMPLER_2D: |
| 34 | case GL_UNSIGNED_INT_SAMPLER_2D: |
| 35 | case GL_SAMPLER_2D_SHADOW: |
| 36 | return GL_TEXTURE_2D; |
| 37 | case GL_SAMPLER_3D: |
| 38 | case GL_INT_SAMPLER_3D: |
| 39 | case GL_UNSIGNED_INT_SAMPLER_3D: |
| 40 | return GL_TEXTURE_3D; |
| 41 | case GL_SAMPLER_CUBE: |
| 42 | case GL_SAMPLER_CUBE_SHADOW: |
| 43 | return GL_TEXTURE_CUBE_MAP; |
| 44 | case GL_INT_SAMPLER_CUBE: |
| 45 | case GL_UNSIGNED_INT_SAMPLER_CUBE: |
| 46 | return GL_TEXTURE_CUBE_MAP; |
| 47 | case GL_SAMPLER_2D_ARRAY: |
| 48 | case GL_INT_SAMPLER_2D_ARRAY: |
| 49 | case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY: |
| 50 | case GL_SAMPLER_2D_ARRAY_SHADOW: |
| 51 | return GL_TEXTURE_2D_ARRAY; |
| 52 | default: UNREACHABLE(); |
| 53 | } |
| 54 | |
| 55 | return GL_TEXTURE_2D; |
| 56 | } |
| 57 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 58 | void GetDefaultInputLayoutFromShader(const std::vector<sh::Attribute> &shaderAttributes, gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS]) |
| 59 | { |
| 60 | size_t layoutIndex = 0; |
| 61 | for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++) |
| 62 | { |
| 63 | ASSERT(layoutIndex < gl::MAX_VERTEX_ATTRIBS); |
| 64 | |
| 65 | const sh::Attribute &shaderAttr = shaderAttributes[attributeIndex]; |
| 66 | |
| 67 | if (shaderAttr.type != GL_NONE) |
| 68 | { |
| 69 | GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type); |
| 70 | |
| 71 | for (size_t rowIndex = 0; static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); rowIndex++, layoutIndex++) |
| 72 | { |
| 73 | gl::VertexFormat *defaultFormat = &inputLayout[layoutIndex]; |
| 74 | |
| 75 | defaultFormat->mType = gl::VariableComponentType(transposedType); |
| 76 | defaultFormat->mNormalized = false; |
| 77 | defaultFormat->mPureInteger = (defaultFormat->mType != GL_FLOAT); // note: inputs can not be bool |
| 78 | defaultFormat->mComponents = gl::VariableColumnCount(transposedType); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars) |
| 85 | { |
| 86 | std::vector<GLenum> defaultPixelOutput(1); |
| 87 | |
| 88 | ASSERT(!shaderOutputVars.empty()); |
| 89 | defaultPixelOutput[0] = GL_COLOR_ATTACHMENT0 + shaderOutputVars[0].outputIndex; |
| 90 | |
| 91 | return defaultPixelOutput; |
| 92 | } |
| 93 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 94 | bool IsRowMajorLayout(const sh::InterfaceBlockField &var) |
| 95 | { |
| 96 | return var.isRowMajorLayout; |
| 97 | } |
| 98 | |
| 99 | bool IsRowMajorLayout(const sh::ShaderVariable &var) |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ProgramD3D::VertexExecutable::VertexExecutable(const gl::VertexFormat inputLayout[], |
| 107 | const GLenum signature[], |
| 108 | ShaderExecutable *shaderExecutable) |
| 109 | : mShaderExecutable(shaderExecutable) |
| 110 | { |
| 111 | for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 112 | { |
| 113 | mInputs[attributeIndex] = inputLayout[attributeIndex]; |
| 114 | mSignature[attributeIndex] = signature[attributeIndex]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | ProgramD3D::VertexExecutable::~VertexExecutable() |
| 119 | { |
| 120 | SafeDelete(mShaderExecutable); |
| 121 | } |
| 122 | |
| 123 | bool ProgramD3D::VertexExecutable::matchesSignature(const GLenum signature[]) const |
| 124 | { |
| 125 | for (size_t attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 126 | { |
| 127 | if (mSignature[attributeIndex] != signature[attributeIndex]) |
| 128 | { |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutable *shaderExecutable) |
| 137 | : mOutputSignature(outputSignature), |
| 138 | mShaderExecutable(shaderExecutable) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | ProgramD3D::PixelExecutable::~PixelExecutable() |
| 143 | { |
| 144 | SafeDelete(mShaderExecutable); |
| 145 | } |
| 146 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 147 | ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D) |
| 148 | { |
| 149 | } |
| 150 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 151 | ProgramD3D::ProgramD3D(Renderer *renderer) |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 152 | : ProgramImpl(), |
| 153 | mRenderer(renderer), |
| 154 | mDynamicHLSL(NULL), |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 155 | mGeometryExecutable(NULL), |
| 156 | mVertexWorkarounds(ANGLE_D3D_WORKAROUND_NONE), |
| 157 | mPixelWorkarounds(ANGLE_D3D_WORKAROUND_NONE), |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 158 | mUsesPointSize(false), |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 159 | mVertexUniformStorage(NULL), |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 160 | mFragmentUniformStorage(NULL), |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 161 | mUsedVertexSamplerRange(0), |
| 162 | mUsedPixelSamplerRange(0), |
| 163 | mDirtySamplerMapping(true), |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 164 | mShaderVersion(100) |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 165 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 166 | mDynamicHLSL = new DynamicHLSL(renderer); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | ProgramD3D::~ProgramD3D() |
| 170 | { |
| 171 | reset(); |
| 172 | SafeDelete(mDynamicHLSL); |
| 173 | } |
| 174 | |
| 175 | ProgramD3D *ProgramD3D::makeProgramD3D(ProgramImpl *impl) |
| 176 | { |
| 177 | ASSERT(HAS_DYNAMIC_TYPE(ProgramD3D*, impl)); |
| 178 | return static_cast<ProgramD3D*>(impl); |
| 179 | } |
| 180 | |
| 181 | const ProgramD3D *ProgramD3D::makeProgramD3D(const ProgramImpl *impl) |
| 182 | { |
| 183 | ASSERT(HAS_DYNAMIC_TYPE(const ProgramD3D*, impl)); |
| 184 | return static_cast<const ProgramD3D*>(impl); |
| 185 | } |
| 186 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 187 | bool ProgramD3D::usesPointSpriteEmulation() const |
| 188 | { |
| 189 | return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4; |
| 190 | } |
| 191 | |
| 192 | bool ProgramD3D::usesGeometryShader() const |
| 193 | { |
| 194 | return usesPointSpriteEmulation(); |
| 195 | } |
| 196 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 197 | GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const |
| 198 | { |
| 199 | GLint logicalTextureUnit = -1; |
| 200 | |
| 201 | switch (type) |
| 202 | { |
| 203 | case gl::SAMPLER_PIXEL: |
| 204 | ASSERT(samplerIndex < caps.maxTextureImageUnits); |
| 205 | if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active) |
| 206 | { |
| 207 | logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit; |
| 208 | } |
| 209 | break; |
| 210 | case gl::SAMPLER_VERTEX: |
| 211 | ASSERT(samplerIndex < caps.maxVertexTextureImageUnits); |
| 212 | if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active) |
| 213 | { |
| 214 | logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit; |
| 215 | } |
| 216 | break; |
| 217 | default: UNREACHABLE(); |
| 218 | } |
| 219 | |
| 220 | if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits)) |
| 221 | { |
| 222 | return logicalTextureUnit; |
| 223 | } |
| 224 | |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | // Returns the texture type for a given Direct3D 9 sampler type and |
| 229 | // index (0-15 for the pixel shader and 0-3 for the vertex shader). |
| 230 | GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const |
| 231 | { |
| 232 | switch (type) |
| 233 | { |
| 234 | case gl::SAMPLER_PIXEL: |
| 235 | ASSERT(samplerIndex < mSamplersPS.size()); |
| 236 | ASSERT(mSamplersPS[samplerIndex].active); |
| 237 | return mSamplersPS[samplerIndex].textureType; |
| 238 | case gl::SAMPLER_VERTEX: |
| 239 | ASSERT(samplerIndex < mSamplersVS.size()); |
| 240 | ASSERT(mSamplersVS[samplerIndex].active); |
| 241 | return mSamplersVS[samplerIndex].textureType; |
| 242 | default: UNREACHABLE(); |
| 243 | } |
| 244 | |
| 245 | return GL_TEXTURE_2D; |
| 246 | } |
| 247 | |
| 248 | GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const |
| 249 | { |
| 250 | switch (type) |
| 251 | { |
| 252 | case gl::SAMPLER_PIXEL: |
| 253 | return mUsedPixelSamplerRange; |
| 254 | case gl::SAMPLER_VERTEX: |
| 255 | return mUsedVertexSamplerRange; |
| 256 | default: |
| 257 | UNREACHABLE(); |
| 258 | return 0; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void ProgramD3D::updateSamplerMapping() |
| 263 | { |
| 264 | if (!mDirtySamplerMapping) |
| 265 | { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | mDirtySamplerMapping = false; |
| 270 | |
| 271 | // Retrieve sampler uniform values |
| 272 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 273 | { |
| 274 | gl::LinkedUniform *targetUniform = mUniforms[uniformIndex]; |
| 275 | |
| 276 | if (targetUniform->dirty) |
| 277 | { |
| 278 | if (gl::IsSampler(targetUniform->type)) |
| 279 | { |
| 280 | int count = targetUniform->elementCount(); |
| 281 | GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data); |
| 282 | |
| 283 | if (targetUniform->isReferencedByFragmentShader()) |
| 284 | { |
| 285 | unsigned int firstIndex = targetUniform->psRegisterIndex; |
| 286 | |
| 287 | for (int i = 0; i < count; i++) |
| 288 | { |
| 289 | unsigned int samplerIndex = firstIndex + i; |
| 290 | |
| 291 | if (samplerIndex < mSamplersPS.size()) |
| 292 | { |
| 293 | ASSERT(mSamplersPS[samplerIndex].active); |
| 294 | mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0]; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (targetUniform->isReferencedByVertexShader()) |
| 300 | { |
| 301 | unsigned int firstIndex = targetUniform->vsRegisterIndex; |
| 302 | |
| 303 | for (int i = 0; i < count; i++) |
| 304 | { |
| 305 | unsigned int samplerIndex = firstIndex + i; |
| 306 | |
| 307 | if (samplerIndex < mSamplersVS.size()) |
| 308 | { |
| 309 | ASSERT(mSamplersVS[samplerIndex].active); |
| 310 | mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0]; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps) |
| 320 | { |
| 321 | // if any two active samplers in a program are of different types, but refer to the same |
| 322 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 323 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
| 324 | updateSamplerMapping(); |
| 325 | |
| 326 | std::vector<GLenum> textureUnitTypes(caps.maxCombinedTextureImageUnits, GL_NONE); |
| 327 | |
| 328 | for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i) |
| 329 | { |
| 330 | if (mSamplersPS[i].active) |
| 331 | { |
| 332 | unsigned int unit = mSamplersPS[i].logicalTextureUnit; |
| 333 | |
| 334 | if (unit >= textureUnitTypes.size()) |
| 335 | { |
| 336 | if (infoLog) |
| 337 | { |
| 338 | infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size()); |
| 339 | } |
| 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | if (textureUnitTypes[unit] != GL_NONE) |
| 345 | { |
| 346 | if (mSamplersPS[i].textureType != textureUnitTypes[unit]) |
| 347 | { |
| 348 | if (infoLog) |
| 349 | { |
| 350 | infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit); |
| 351 | } |
| 352 | |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | textureUnitTypes[unit] = mSamplersPS[i].textureType; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i) |
| 364 | { |
| 365 | if (mSamplersVS[i].active) |
| 366 | { |
| 367 | unsigned int unit = mSamplersVS[i].logicalTextureUnit; |
| 368 | |
| 369 | if (unit >= textureUnitTypes.size()) |
| 370 | { |
| 371 | if (infoLog) |
| 372 | { |
| 373 | infoLog->append("Sampler uniform (%d) exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (%d)", unit, textureUnitTypes.size()); |
| 374 | } |
| 375 | |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | if (textureUnitTypes[unit] != GL_NONE) |
| 380 | { |
| 381 | if (mSamplersVS[i].textureType != textureUnitTypes[unit]) |
| 382 | { |
| 383 | if (infoLog) |
| 384 | { |
| 385 | infoLog->append("Samplers of conflicting types refer to the same texture image unit (%d).", unit); |
| 386 | } |
| 387 | |
| 388 | return false; |
| 389 | } |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | textureUnitTypes[unit] = mSamplersVS[i].textureType; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 401 | gl::LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 402 | { |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 403 | stream->readInt(&mShaderVersion); |
| 404 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 405 | const unsigned int psSamplerCount = stream->readInt<unsigned int>(); |
| 406 | for (unsigned int i = 0; i < psSamplerCount; ++i) |
| 407 | { |
| 408 | Sampler sampler; |
| 409 | stream->readBool(&sampler.active); |
| 410 | stream->readInt(&sampler.logicalTextureUnit); |
| 411 | stream->readInt(&sampler.textureType); |
| 412 | mSamplersPS.push_back(sampler); |
| 413 | } |
| 414 | const unsigned int vsSamplerCount = stream->readInt<unsigned int>(); |
| 415 | for (unsigned int i = 0; i < vsSamplerCount; ++i) |
| 416 | { |
| 417 | Sampler sampler; |
| 418 | stream->readBool(&sampler.active); |
| 419 | stream->readInt(&sampler.logicalTextureUnit); |
| 420 | stream->readInt(&sampler.textureType); |
| 421 | mSamplersVS.push_back(sampler); |
| 422 | } |
| 423 | |
| 424 | stream->readInt(&mUsedVertexSamplerRange); |
| 425 | stream->readInt(&mUsedPixelSamplerRange); |
| 426 | |
| 427 | const unsigned int uniformCount = stream->readInt<unsigned int>(); |
| 428 | if (stream->error()) |
| 429 | { |
| 430 | infoLog.append("Invalid program binary."); |
| 431 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 432 | } |
| 433 | |
| 434 | mUniforms.resize(uniformCount); |
| 435 | for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++) |
| 436 | { |
| 437 | GLenum type = stream->readInt<GLenum>(); |
| 438 | GLenum precision = stream->readInt<GLenum>(); |
| 439 | std::string name = stream->readString(); |
| 440 | unsigned int arraySize = stream->readInt<unsigned int>(); |
| 441 | int blockIndex = stream->readInt<int>(); |
| 442 | |
| 443 | int offset = stream->readInt<int>(); |
| 444 | int arrayStride = stream->readInt<int>(); |
| 445 | int matrixStride = stream->readInt<int>(); |
| 446 | bool isRowMajorMatrix = stream->readBool(); |
| 447 | |
| 448 | const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix); |
| 449 | |
| 450 | gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo); |
| 451 | |
| 452 | stream->readInt(&uniform->psRegisterIndex); |
| 453 | stream->readInt(&uniform->vsRegisterIndex); |
| 454 | stream->readInt(&uniform->registerCount); |
| 455 | stream->readInt(&uniform->registerElement); |
| 456 | |
| 457 | mUniforms[uniformIndex] = uniform; |
| 458 | } |
| 459 | |
| 460 | const unsigned int uniformIndexCount = stream->readInt<unsigned int>(); |
| 461 | if (stream->error()) |
| 462 | { |
| 463 | infoLog.append("Invalid program binary."); |
| 464 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 465 | } |
| 466 | |
| 467 | mUniformIndex.resize(uniformIndexCount); |
| 468 | for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++) |
| 469 | { |
| 470 | stream->readString(&mUniformIndex[uniformIndexIndex].name); |
| 471 | stream->readInt(&mUniformIndex[uniformIndexIndex].element); |
| 472 | stream->readInt(&mUniformIndex[uniformIndexIndex].index); |
| 473 | } |
| 474 | |
| 475 | unsigned int uniformBlockCount = stream->readInt<unsigned int>(); |
| 476 | if (stream->error()) |
| 477 | { |
| 478 | infoLog.append("Invalid program binary."); |
| 479 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 480 | } |
| 481 | |
| 482 | mUniformBlocks.resize(uniformBlockCount); |
| 483 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex) |
| 484 | { |
| 485 | std::string name = stream->readString(); |
| 486 | unsigned int elementIndex = stream->readInt<unsigned int>(); |
| 487 | unsigned int dataSize = stream->readInt<unsigned int>(); |
| 488 | |
| 489 | gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize); |
| 490 | |
| 491 | stream->readInt(&uniformBlock->psRegisterIndex); |
| 492 | stream->readInt(&uniformBlock->vsRegisterIndex); |
| 493 | |
| 494 | unsigned int numMembers = stream->readInt<unsigned int>(); |
| 495 | uniformBlock->memberUniformIndexes.resize(numMembers); |
| 496 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++) |
| 497 | { |
| 498 | stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]); |
| 499 | } |
| 500 | |
| 501 | mUniformBlocks[uniformBlockIndex] = uniformBlock; |
| 502 | } |
| 503 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 504 | stream->readInt(&mTransformFeedbackBufferMode); |
| 505 | const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>(); |
| 506 | mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount); |
| 507 | for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++) |
| 508 | { |
| 509 | gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex]; |
| 510 | |
| 511 | stream->readString(&varying.name); |
| 512 | stream->readInt(&varying.type); |
| 513 | stream->readInt(&varying.size); |
| 514 | stream->readString(&varying.semanticName); |
| 515 | stream->readInt(&varying.semanticIndex); |
| 516 | stream->readInt(&varying.semanticIndexCount); |
| 517 | } |
| 518 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 519 | stream->readString(&mVertexHLSL); |
| 520 | stream->readInt(&mVertexWorkarounds); |
| 521 | stream->readString(&mPixelHLSL); |
| 522 | stream->readInt(&mPixelWorkarounds); |
| 523 | stream->readBool(&mUsesFragDepth); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 524 | stream->readBool(&mUsesPointSize); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 525 | |
| 526 | const size_t pixelShaderKeySize = stream->readInt<unsigned int>(); |
| 527 | mPixelShaderKey.resize(pixelShaderKeySize); |
| 528 | for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++) |
| 529 | { |
| 530 | stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type); |
| 531 | stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name); |
| 532 | stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source); |
| 533 | stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex); |
| 534 | } |
| 535 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 536 | const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data()); |
| 537 | |
| 538 | const unsigned int vertexShaderCount = stream->readInt<unsigned int>(); |
| 539 | for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++) |
| 540 | { |
| 541 | gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS]; |
| 542 | |
| 543 | for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++) |
| 544 | { |
| 545 | gl::VertexFormat *vertexInput = &inputLayout[inputIndex]; |
| 546 | stream->readInt(&vertexInput->mType); |
| 547 | stream->readInt(&vertexInput->mNormalized); |
| 548 | stream->readInt(&vertexInput->mComponents); |
| 549 | stream->readBool(&vertexInput->mPureInteger); |
| 550 | } |
| 551 | |
| 552 | unsigned int vertexShaderSize = stream->readInt<unsigned int>(); |
| 553 | const unsigned char *vertexShaderFunction = binary + stream->offset(); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 554 | |
| 555 | ShaderExecutable *shaderExecutable = NULL; |
| 556 | gl::Error error = mRenderer->loadExecutable(vertexShaderFunction, vertexShaderSize, |
| 557 | SHADER_VERTEX, |
| 558 | mTransformFeedbackLinkedVaryings, |
| 559 | (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), |
| 560 | &shaderExecutable); |
| 561 | if (error.isError()) |
| 562 | { |
| 563 | return gl::LinkResult(false, error); |
| 564 | } |
| 565 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 566 | if (!shaderExecutable) |
| 567 | { |
| 568 | infoLog.append("Could not create vertex shader."); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 569 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // generated converted input layout |
| 573 | GLenum signature[gl::MAX_VERTEX_ATTRIBS]; |
| 574 | getInputLayoutSignature(inputLayout, signature); |
| 575 | |
| 576 | // add new binary |
| 577 | mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable)); |
| 578 | |
| 579 | stream->skip(vertexShaderSize); |
| 580 | } |
| 581 | |
| 582 | const size_t pixelShaderCount = stream->readInt<unsigned int>(); |
| 583 | for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++) |
| 584 | { |
| 585 | const size_t outputCount = stream->readInt<unsigned int>(); |
| 586 | std::vector<GLenum> outputs(outputCount); |
| 587 | for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++) |
| 588 | { |
| 589 | stream->readInt(&outputs[outputIndex]); |
| 590 | } |
| 591 | |
| 592 | const size_t pixelShaderSize = stream->readInt<unsigned int>(); |
| 593 | const unsigned char *pixelShaderFunction = binary + stream->offset(); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 594 | ShaderExecutable *shaderExecutable = NULL; |
| 595 | gl::Error error = mRenderer->loadExecutable(pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, |
| 596 | mTransformFeedbackLinkedVaryings, |
| 597 | (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), |
| 598 | &shaderExecutable); |
| 599 | if (error.isError()) |
| 600 | { |
| 601 | return gl::LinkResult(false, error); |
| 602 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 603 | |
| 604 | if (!shaderExecutable) |
| 605 | { |
| 606 | infoLog.append("Could not create pixel shader."); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 607 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // add new binary |
| 611 | mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable)); |
| 612 | |
| 613 | stream->skip(pixelShaderSize); |
| 614 | } |
| 615 | |
| 616 | unsigned int geometryShaderSize = stream->readInt<unsigned int>(); |
| 617 | |
| 618 | if (geometryShaderSize > 0) |
| 619 | { |
| 620 | const unsigned char *geometryShaderFunction = binary + stream->offset(); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 621 | gl::Error error = mRenderer->loadExecutable(geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, |
| 622 | mTransformFeedbackLinkedVaryings, |
| 623 | (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), |
| 624 | &mGeometryExecutable); |
| 625 | if (error.isError()) |
| 626 | { |
| 627 | return gl::LinkResult(false, error); |
| 628 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 629 | |
| 630 | if (!mGeometryExecutable) |
| 631 | { |
| 632 | infoLog.append("Could not create geometry shader."); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 633 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 634 | } |
| 635 | stream->skip(geometryShaderSize); |
| 636 | } |
| 637 | |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 638 | GUID binaryIdentifier = {0}; |
| 639 | stream->readBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID)); |
| 640 | |
| 641 | GUID identifier = mRenderer->getAdapterIdentifier(); |
| 642 | if (memcmp(&identifier, &binaryIdentifier, sizeof(GUID)) != 0) |
| 643 | { |
| 644 | infoLog.append("Invalid program binary."); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 645 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 648 | initializeUniformStorage(); |
| 649 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 650 | return gl::LinkResult(true, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 653 | gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 654 | { |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 655 | stream->writeInt(mShaderVersion); |
| 656 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 657 | stream->writeInt(mSamplersPS.size()); |
| 658 | for (unsigned int i = 0; i < mSamplersPS.size(); ++i) |
| 659 | { |
| 660 | stream->writeInt(mSamplersPS[i].active); |
| 661 | stream->writeInt(mSamplersPS[i].logicalTextureUnit); |
| 662 | stream->writeInt(mSamplersPS[i].textureType); |
| 663 | } |
| 664 | |
| 665 | stream->writeInt(mSamplersVS.size()); |
| 666 | for (unsigned int i = 0; i < mSamplersVS.size(); ++i) |
| 667 | { |
| 668 | stream->writeInt(mSamplersVS[i].active); |
| 669 | stream->writeInt(mSamplersVS[i].logicalTextureUnit); |
| 670 | stream->writeInt(mSamplersVS[i].textureType); |
| 671 | } |
| 672 | |
| 673 | stream->writeInt(mUsedVertexSamplerRange); |
| 674 | stream->writeInt(mUsedPixelSamplerRange); |
| 675 | |
| 676 | stream->writeInt(mUniforms.size()); |
| 677 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex) |
| 678 | { |
| 679 | const gl::LinkedUniform &uniform = *mUniforms[uniformIndex]; |
| 680 | |
| 681 | stream->writeInt(uniform.type); |
| 682 | stream->writeInt(uniform.precision); |
| 683 | stream->writeString(uniform.name); |
| 684 | stream->writeInt(uniform.arraySize); |
| 685 | stream->writeInt(uniform.blockIndex); |
| 686 | |
| 687 | stream->writeInt(uniform.blockInfo.offset); |
| 688 | stream->writeInt(uniform.blockInfo.arrayStride); |
| 689 | stream->writeInt(uniform.blockInfo.matrixStride); |
| 690 | stream->writeInt(uniform.blockInfo.isRowMajorMatrix); |
| 691 | |
| 692 | stream->writeInt(uniform.psRegisterIndex); |
| 693 | stream->writeInt(uniform.vsRegisterIndex); |
| 694 | stream->writeInt(uniform.registerCount); |
| 695 | stream->writeInt(uniform.registerElement); |
| 696 | } |
| 697 | |
| 698 | stream->writeInt(mUniformIndex.size()); |
| 699 | for (size_t i = 0; i < mUniformIndex.size(); ++i) |
| 700 | { |
| 701 | stream->writeString(mUniformIndex[i].name); |
| 702 | stream->writeInt(mUniformIndex[i].element); |
| 703 | stream->writeInt(mUniformIndex[i].index); |
| 704 | } |
| 705 | |
| 706 | stream->writeInt(mUniformBlocks.size()); |
| 707 | for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex) |
| 708 | { |
| 709 | const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex]; |
| 710 | |
| 711 | stream->writeString(uniformBlock.name); |
| 712 | stream->writeInt(uniformBlock.elementIndex); |
| 713 | stream->writeInt(uniformBlock.dataSize); |
| 714 | |
| 715 | stream->writeInt(uniformBlock.memberUniformIndexes.size()); |
| 716 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++) |
| 717 | { |
| 718 | stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]); |
| 719 | } |
| 720 | |
| 721 | stream->writeInt(uniformBlock.psRegisterIndex); |
| 722 | stream->writeInt(uniformBlock.vsRegisterIndex); |
| 723 | } |
| 724 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 725 | stream->writeInt(mTransformFeedbackBufferMode); |
| 726 | stream->writeInt(mTransformFeedbackLinkedVaryings.size()); |
| 727 | for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++) |
| 728 | { |
| 729 | const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i]; |
| 730 | |
| 731 | stream->writeString(varying.name); |
| 732 | stream->writeInt(varying.type); |
| 733 | stream->writeInt(varying.size); |
| 734 | stream->writeString(varying.semanticName); |
| 735 | stream->writeInt(varying.semanticIndex); |
| 736 | stream->writeInt(varying.semanticIndexCount); |
| 737 | } |
| 738 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 739 | stream->writeString(mVertexHLSL); |
| 740 | stream->writeInt(mVertexWorkarounds); |
| 741 | stream->writeString(mPixelHLSL); |
| 742 | stream->writeInt(mPixelWorkarounds); |
| 743 | stream->writeInt(mUsesFragDepth); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 744 | stream->writeInt(mUsesPointSize); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 745 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 746 | const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 747 | stream->writeInt(pixelShaderKey.size()); |
| 748 | for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++) |
| 749 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 750 | const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex]; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 751 | stream->writeInt(variable.type); |
| 752 | stream->writeString(variable.name); |
| 753 | stream->writeString(variable.source); |
| 754 | stream->writeInt(variable.outputIndex); |
| 755 | } |
| 756 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 757 | stream->writeInt(mVertexExecutables.size()); |
| 758 | for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++) |
| 759 | { |
| 760 | VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex]; |
| 761 | |
| 762 | for (size_t inputIndex = 0; inputIndex < gl::MAX_VERTEX_ATTRIBS; inputIndex++) |
| 763 | { |
| 764 | const gl::VertexFormat &vertexInput = vertexExecutable->inputs()[inputIndex]; |
| 765 | stream->writeInt(vertexInput.mType); |
| 766 | stream->writeInt(vertexInput.mNormalized); |
| 767 | stream->writeInt(vertexInput.mComponents); |
| 768 | stream->writeInt(vertexInput.mPureInteger); |
| 769 | } |
| 770 | |
| 771 | size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength(); |
| 772 | stream->writeInt(vertexShaderSize); |
| 773 | |
| 774 | const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction(); |
| 775 | stream->writeBytes(vertexBlob, vertexShaderSize); |
| 776 | } |
| 777 | |
| 778 | stream->writeInt(mPixelExecutables.size()); |
| 779 | for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++) |
| 780 | { |
| 781 | PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex]; |
| 782 | |
| 783 | const std::vector<GLenum> outputs = pixelExecutable->outputSignature(); |
| 784 | stream->writeInt(outputs.size()); |
| 785 | for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++) |
| 786 | { |
| 787 | stream->writeInt(outputs[outputIndex]); |
| 788 | } |
| 789 | |
| 790 | size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength(); |
| 791 | stream->writeInt(pixelShaderSize); |
| 792 | |
| 793 | const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction(); |
| 794 | stream->writeBytes(pixelBlob, pixelShaderSize); |
| 795 | } |
| 796 | |
| 797 | size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0; |
| 798 | stream->writeInt(geometryShaderSize); |
| 799 | |
| 800 | if (mGeometryExecutable != NULL && geometryShaderSize > 0) |
| 801 | { |
| 802 | const uint8_t *geometryBlob = mGeometryExecutable->getFunction(); |
| 803 | stream->writeBytes(geometryBlob, geometryShaderSize); |
| 804 | } |
| 805 | |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 806 | GUID binaryIdentifier = mRenderer->getAdapterIdentifier(); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 807 | stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(GUID)); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 808 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 809 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 812 | gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExecutable) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 813 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 814 | std::vector<GLenum> outputs; |
| 815 | |
| 816 | const gl::ColorbufferInfo &colorbuffers = fbo->getColorbuffersForRender(); |
| 817 | |
| 818 | for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment) |
| 819 | { |
| 820 | const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment]; |
| 821 | |
| 822 | if (colorbuffer) |
| 823 | { |
| 824 | outputs.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding()); |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | outputs.push_back(GL_NONE); |
| 829 | } |
| 830 | } |
| 831 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 832 | return getPixelExecutableForOutputLayout(outputs, outExecutable); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 835 | gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature, ShaderExecutable **outExectuable) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 836 | { |
| 837 | for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++) |
| 838 | { |
| 839 | if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature)) |
| 840 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 841 | *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable(); |
| 842 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 846 | std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth, |
| 847 | outputSignature); |
| 848 | |
| 849 | // Generate new pixel executable |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 850 | gl::InfoLog tempInfoLog; |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 851 | ShaderExecutable *pixelExecutable = NULL; |
| 852 | gl::Error error = mRenderer->compileToExecutable(tempInfoLog, finalPixelHLSL, SHADER_PIXEL, |
| 853 | mTransformFeedbackLinkedVaryings, |
| 854 | (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), |
| 855 | mPixelWorkarounds, &pixelExecutable); |
| 856 | if (error.isError()) |
| 857 | { |
| 858 | return error; |
| 859 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 860 | |
| 861 | if (!pixelExecutable) |
| 862 | { |
| 863 | std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3); |
| 864 | tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]); |
| 865 | ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]); |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable)); |
| 870 | } |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 871 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 872 | *outExectuable = pixelExecutable; |
| 873 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 876 | gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], ShaderExecutable **outExectuable) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 877 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 878 | GLenum signature[gl::MAX_VERTEX_ATTRIBS]; |
| 879 | getInputLayoutSignature(inputLayout, signature); |
| 880 | |
| 881 | for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++) |
| 882 | { |
| 883 | if (mVertexExecutables[executableIndex]->matchesSignature(signature)) |
| 884 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 885 | *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable(); |
| 886 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 890 | // Generate new dynamic layout with attribute conversions |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 891 | std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, mShaderAttributes); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 892 | |
| 893 | // Generate new vertex executable |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 894 | gl::InfoLog tempInfoLog; |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 895 | ShaderExecutable *vertexExecutable = NULL; |
| 896 | gl::Error error = mRenderer->compileToExecutable(tempInfoLog, finalVertexHLSL, SHADER_VERTEX, |
| 897 | mTransformFeedbackLinkedVaryings, |
| 898 | (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), |
| 899 | mVertexWorkarounds, &vertexExecutable); |
| 900 | if (error.isError()) |
| 901 | { |
| 902 | return error; |
| 903 | } |
| 904 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 905 | if (!vertexExecutable) |
| 906 | { |
| 907 | std::vector<char> tempCharBuffer(tempInfoLog.getLength()+3); |
| 908 | tempInfoLog.getLog(tempInfoLog.getLength(), NULL, &tempCharBuffer[0]); |
| 909 | ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]); |
| 910 | } |
| 911 | else |
| 912 | { |
| 913 | mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, vertexExecutable)); |
| 914 | } |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 915 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 916 | *outExectuable = vertexExecutable; |
| 917 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 920 | gl::LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader, |
| 921 | int registers) |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 922 | { |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 923 | ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation()); |
| 924 | ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation()); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 925 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 926 | gl::VertexFormat defaultInputLayout[gl::MAX_VERTEX_ATTRIBS]; |
| 927 | GetDefaultInputLayoutFromShader(vertexShader->getActiveAttributes(), defaultInputLayout); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 928 | ShaderExecutable *defaultVertexExecutable = NULL; |
| 929 | gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable); |
| 930 | if (error.isError()) |
| 931 | { |
| 932 | return gl::LinkResult(false, error); |
| 933 | } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 934 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 935 | std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey()); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 936 | ShaderExecutable *defaultPixelExecutable = NULL; |
| 937 | error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable); |
| 938 | if (error.isError()) |
| 939 | { |
| 940 | return gl::LinkResult(false, error); |
| 941 | } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 942 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 943 | if (usesGeometryShader()) |
| 944 | { |
| 945 | std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 946 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 947 | |
| 948 | error = mRenderer->compileToExecutable(infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings, |
| 949 | (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS), |
| 950 | ANGLE_D3D_WORKAROUND_NONE, &mGeometryExecutable); |
| 951 | if (error.isError()) |
| 952 | { |
| 953 | return gl::LinkResult(false, error); |
| 954 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 957 | #ifdef ANGLE_GENERATE_SHADER_DEBUG_INFO |
| 958 | if (usesGeometryShader() && mGeometryExecutable) |
| 959 | { |
| 960 | // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level |
| 961 | // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch |
| 962 | vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n"); |
| 963 | vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo()); |
| 964 | vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n"); |
| 965 | } |
| 966 | |
| 967 | if (defaultVertexExecutable) |
| 968 | { |
| 969 | vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo()); |
| 970 | } |
| 971 | |
| 972 | if (defaultPixelExecutable) |
| 973 | { |
| 974 | fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo()); |
| 975 | } |
| 976 | #endif |
| 977 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 978 | bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable)); |
| 979 | return gl::LinkResult(linkSuccess, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 982 | gl::LinkResult ProgramD3D::link(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader, |
| 983 | const std::vector<std::string> &transformFeedbackVaryings, GLenum transformFeedbackBufferMode, |
| 984 | int *registers, std::vector<gl::LinkedVarying> *linkedVaryings, |
| 985 | std::map<int, gl::VariableLocation> *outputVariables, const gl::Caps &caps) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 986 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 987 | ShaderD3D *vertexShaderD3D = ShaderD3D::makeShaderD3D(vertexShader->getImplementation()); |
| 988 | ShaderD3D *fragmentShaderD3D = ShaderD3D::makeShaderD3D(fragmentShader->getImplementation()); |
| 989 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 990 | mSamplersPS.resize(caps.maxTextureImageUnits); |
| 991 | mSamplersVS.resize(caps.maxVertexTextureImageUnits); |
| 992 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 993 | mTransformFeedbackBufferMode = transformFeedbackBufferMode; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 994 | |
| 995 | mPixelHLSL = fragmentShaderD3D->getTranslatedSource(); |
| 996 | mPixelWorkarounds = fragmentShaderD3D->getD3DWorkarounds(); |
| 997 | |
| 998 | mVertexHLSL = vertexShaderD3D->getTranslatedSource(); |
| 999 | mVertexWorkarounds = vertexShaderD3D->getD3DWorkarounds(); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1000 | mShaderVersion = vertexShaderD3D->getShaderVersion(); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | // Map the varyings to the register file |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1003 | VaryingPacking packing = { NULL }; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1004 | *registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings); |
| 1005 | |
Geoff Lang | bdee2d5 | 2014-09-17 11:02:51 -0400 | [diff] [blame] | 1006 | if (*registers < 0) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1007 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1008 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | if (!gl::ProgramBinary::linkVaryings(infoLog, fragmentShader, vertexShader)) |
| 1012 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1013 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if (!mDynamicHLSL->generateShaderLinkHLSL(infoLog, *registers, packing, mPixelHLSL, mVertexHLSL, |
| 1017 | fragmentShaderD3D, vertexShaderD3D, transformFeedbackVaryings, |
| 1018 | linkedVaryings, outputVariables, &mPixelShaderKey, &mUsesFragDepth)) |
| 1019 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1020 | return gl::LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1023 | mUsesPointSize = vertexShaderD3D->usesPointSize(); |
| 1024 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1025 | return gl::LinkResult(true, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1028 | void ProgramD3D::getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const |
| 1029 | { |
| 1030 | mDynamicHLSL->getInputLayoutSignature(inputLayout, signature); |
| 1031 | } |
| 1032 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1033 | void ProgramD3D::initializeUniformStorage() |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1034 | { |
| 1035 | // Compute total default block size |
| 1036 | unsigned int vertexRegisters = 0; |
| 1037 | unsigned int fragmentRegisters = 0; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1038 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1039 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1040 | const gl::LinkedUniform &uniform = *mUniforms[uniformIndex]; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1041 | |
| 1042 | if (!gl::IsSampler(uniform.type)) |
| 1043 | { |
| 1044 | if (uniform.isReferencedByVertexShader()) |
| 1045 | { |
| 1046 | vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount); |
| 1047 | } |
| 1048 | if (uniform.isReferencedByFragmentShader()) |
| 1049 | { |
| 1050 | fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u); |
| 1056 | mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u); |
| 1057 | } |
| 1058 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1059 | gl::Error ProgramD3D::applyUniforms() |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1060 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1061 | updateSamplerMapping(); |
| 1062 | |
| 1063 | gl::Error error = mRenderer->applyUniforms(*this, mUniforms); |
| 1064 | if (error.isError()) |
| 1065 | { |
| 1066 | return error; |
| 1067 | } |
| 1068 | |
| 1069 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 1070 | { |
| 1071 | mUniforms[uniformIndex]->dirty = false; |
| 1072 | } |
| 1073 | |
| 1074 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1077 | gl::Error ProgramD3D::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1078 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1079 | ASSERT(boundBuffers.size() == mUniformBlocks.size()); |
| 1080 | |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1081 | const gl::Buffer *vertexUniformBuffers[gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS] = {NULL}; |
| 1082 | const gl::Buffer *fragmentUniformBuffers[gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS] = {NULL}; |
| 1083 | |
| 1084 | const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers(); |
| 1085 | const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers(); |
| 1086 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1087 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1088 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1089 | gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex]; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1090 | gl::Buffer *uniformBuffer = boundBuffers[uniformBlockIndex]; |
| 1091 | |
| 1092 | ASSERT(uniformBlock && uniformBuffer); |
| 1093 | |
| 1094 | if (uniformBuffer->getSize() < uniformBlock->dataSize) |
| 1095 | { |
| 1096 | // undefined behaviour |
| 1097 | return gl::Error(GL_INVALID_OPERATION, "It is undefined behaviour to use a uniform buffer that is too small."); |
| 1098 | } |
| 1099 | |
| 1100 | // Unnecessary to apply an unreferenced standard or shared UBO |
| 1101 | if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader()) |
| 1102 | { |
| 1103 | continue; |
| 1104 | } |
| 1105 | |
| 1106 | if (uniformBlock->isReferencedByVertexShader()) |
| 1107 | { |
| 1108 | unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS; |
| 1109 | ASSERT(vertexUniformBuffers[registerIndex] == NULL); |
| 1110 | ASSERT(registerIndex < caps.maxVertexUniformBlocks); |
| 1111 | vertexUniformBuffers[registerIndex] = uniformBuffer; |
| 1112 | } |
| 1113 | |
| 1114 | if (uniformBlock->isReferencedByFragmentShader()) |
| 1115 | { |
| 1116 | unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS; |
| 1117 | ASSERT(fragmentUniformBuffers[registerIndex] == NULL); |
| 1118 | ASSERT(registerIndex < caps.maxFragmentUniformBlocks); |
| 1119 | fragmentUniformBuffers[registerIndex] = uniformBuffer; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | return mRenderer->setUniformBuffers(vertexUniformBuffers, fragmentUniformBuffers); |
| 1124 | } |
| 1125 | |
| 1126 | bool ProgramD3D::assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader, |
| 1127 | unsigned int registerIndex, const gl::Caps &caps) |
| 1128 | { |
| 1129 | if (shader == GL_VERTEX_SHADER) |
| 1130 | { |
| 1131 | uniformBlock->vsRegisterIndex = registerIndex; |
| 1132 | if (registerIndex - mRenderer->getReservedVertexUniformBuffers() >= caps.maxVertexUniformBlocks) |
| 1133 | { |
| 1134 | infoLog.append("Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS (%u)", caps.maxVertexUniformBlocks); |
| 1135 | return false; |
| 1136 | } |
| 1137 | } |
| 1138 | else if (shader == GL_FRAGMENT_SHADER) |
| 1139 | { |
| 1140 | uniformBlock->psRegisterIndex = registerIndex; |
| 1141 | if (registerIndex - mRenderer->getReservedFragmentUniformBuffers() >= caps.maxFragmentUniformBlocks) |
| 1142 | { |
| 1143 | infoLog.append("Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS (%u)", caps.maxFragmentUniformBlocks); |
| 1144 | return false; |
| 1145 | } |
| 1146 | } |
| 1147 | else UNREACHABLE(); |
| 1148 | |
| 1149 | return true; |
| 1150 | } |
| 1151 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1152 | void ProgramD3D::dirtyAllUniforms() |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1153 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1154 | unsigned int numUniforms = mUniforms.size(); |
| 1155 | for (unsigned int index = 0; index < numUniforms; index++) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1156 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1157 | mUniforms[index]->dirty = true; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1158 | } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) |
| 1162 | { |
| 1163 | setUniform(location, count, v, GL_FLOAT); |
| 1164 | } |
| 1165 | |
| 1166 | void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 1167 | { |
| 1168 | setUniform(location, count, v, GL_FLOAT_VEC2); |
| 1169 | } |
| 1170 | |
| 1171 | void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 1172 | { |
| 1173 | setUniform(location, count, v, GL_FLOAT_VEC3); |
| 1174 | } |
| 1175 | |
| 1176 | void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 1177 | { |
| 1178 | setUniform(location, count, v, GL_FLOAT_VEC4); |
| 1179 | } |
| 1180 | |
| 1181 | void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1182 | { |
| 1183 | setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2); |
| 1184 | } |
| 1185 | |
| 1186 | void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1187 | { |
| 1188 | setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3); |
| 1189 | } |
| 1190 | |
| 1191 | void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1192 | { |
| 1193 | setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4); |
| 1194 | } |
| 1195 | |
| 1196 | void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1197 | { |
| 1198 | setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3); |
| 1199 | } |
| 1200 | |
| 1201 | void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1202 | { |
| 1203 | setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2); |
| 1204 | } |
| 1205 | |
| 1206 | void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1207 | { |
| 1208 | setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4); |
| 1209 | } |
| 1210 | |
| 1211 | void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1212 | { |
| 1213 | setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2); |
| 1214 | } |
| 1215 | |
| 1216 | void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1217 | { |
| 1218 | setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4); |
| 1219 | } |
| 1220 | |
| 1221 | void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1222 | { |
| 1223 | setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3); |
| 1224 | } |
| 1225 | |
| 1226 | void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 1227 | { |
| 1228 | setUniform(location, count, v, GL_INT); |
| 1229 | } |
| 1230 | |
| 1231 | void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 1232 | { |
| 1233 | setUniform(location, count, v, GL_INT_VEC2); |
| 1234 | } |
| 1235 | |
| 1236 | void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 1237 | { |
| 1238 | setUniform(location, count, v, GL_INT_VEC3); |
| 1239 | } |
| 1240 | |
| 1241 | void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 1242 | { |
| 1243 | setUniform(location, count, v, GL_INT_VEC4); |
| 1244 | } |
| 1245 | |
| 1246 | void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 1247 | { |
| 1248 | setUniform(location, count, v, GL_UNSIGNED_INT); |
| 1249 | } |
| 1250 | |
| 1251 | void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 1252 | { |
| 1253 | setUniform(location, count, v, GL_UNSIGNED_INT_VEC2); |
| 1254 | } |
| 1255 | |
| 1256 | void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 1257 | { |
| 1258 | setUniform(location, count, v, GL_UNSIGNED_INT_VEC3); |
| 1259 | } |
| 1260 | |
| 1261 | void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 1262 | { |
| 1263 | setUniform(location, count, v, GL_UNSIGNED_INT_VEC4); |
| 1264 | } |
| 1265 | |
| 1266 | void ProgramD3D::getUniformfv(GLint location, GLfloat *params) |
| 1267 | { |
| 1268 | getUniformv(location, params, GL_FLOAT); |
| 1269 | } |
| 1270 | |
| 1271 | void ProgramD3D::getUniformiv(GLint location, GLint *params) |
| 1272 | { |
| 1273 | getUniformv(location, params, GL_INT); |
| 1274 | } |
| 1275 | |
| 1276 | void ProgramD3D::getUniformuiv(GLint location, GLuint *params) |
| 1277 | { |
| 1278 | getUniformv(location, params, GL_UNSIGNED_INT); |
| 1279 | } |
| 1280 | |
| 1281 | bool ProgramD3D::linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader, |
| 1282 | const gl::Caps &caps) |
| 1283 | { |
| 1284 | const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader.getImplementation()); |
| 1285 | const rx::ShaderD3D *fragmentShaderD3D = rx::ShaderD3D::makeShaderD3D(fragmentShader.getImplementation()); |
| 1286 | |
| 1287 | const std::vector<sh::Uniform> &vertexUniforms = vertexShader.getUniforms(); |
| 1288 | const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader.getUniforms(); |
| 1289 | |
| 1290 | // Check that uniforms defined in the vertex and fragment shaders are identical |
| 1291 | typedef std::map<std::string, const sh::Uniform*> UniformMap; |
| 1292 | UniformMap linkedUniforms; |
| 1293 | |
| 1294 | for (unsigned int vertexUniformIndex = 0; vertexUniformIndex < vertexUniforms.size(); vertexUniformIndex++) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1295 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1296 | const sh::Uniform &vertexUniform = vertexUniforms[vertexUniformIndex]; |
| 1297 | linkedUniforms[vertexUniform.name] = &vertexUniform; |
| 1298 | } |
| 1299 | |
| 1300 | for (unsigned int fragmentUniformIndex = 0; fragmentUniformIndex < fragmentUniforms.size(); fragmentUniformIndex++) |
| 1301 | { |
| 1302 | const sh::Uniform &fragmentUniform = fragmentUniforms[fragmentUniformIndex]; |
| 1303 | UniformMap::const_iterator entry = linkedUniforms.find(fragmentUniform.name); |
| 1304 | if (entry != linkedUniforms.end()) |
| 1305 | { |
| 1306 | const sh::Uniform &vertexUniform = *entry->second; |
| 1307 | const std::string &uniformName = "uniform '" + vertexUniform.name + "'"; |
| 1308 | if (!gl::ProgramBinary::linkValidateUniforms(infoLog, uniformName, vertexUniform, fragmentUniform)) |
| 1309 | { |
| 1310 | return false; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | for (unsigned int uniformIndex = 0; uniformIndex < vertexUniforms.size(); uniformIndex++) |
| 1316 | { |
| 1317 | const sh::Uniform &uniform = vertexUniforms[uniformIndex]; |
| 1318 | |
| 1319 | if (uniform.staticUse) |
| 1320 | { |
| 1321 | defineUniformBase(GL_VERTEX_SHADER, uniform, vertexShaderD3D->getUniformRegister(uniform.name)); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | for (unsigned int uniformIndex = 0; uniformIndex < fragmentUniforms.size(); uniformIndex++) |
| 1326 | { |
| 1327 | const sh::Uniform &uniform = fragmentUniforms[uniformIndex]; |
| 1328 | |
| 1329 | if (uniform.staticUse) |
| 1330 | { |
| 1331 | defineUniformBase(GL_FRAGMENT_SHADER, uniform, fragmentShaderD3D->getUniformRegister(uniform.name)); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if (!indexUniforms(infoLog, caps)) |
| 1336 | { |
| 1337 | return false; |
| 1338 | } |
| 1339 | |
| 1340 | initializeUniformStorage(); |
| 1341 | |
| 1342 | // special case for gl_DepthRange, the only built-in uniform (also a struct) |
| 1343 | if (vertexShaderD3D->usesDepthRange() || fragmentShaderD3D->usesDepthRange()) |
| 1344 | { |
| 1345 | const sh::BlockMemberInfo &defaultInfo = sh::BlockMemberInfo::getDefaultBlockInfo(); |
| 1346 | |
| 1347 | mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.near", 0, -1, defaultInfo)); |
| 1348 | mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.far", 0, -1, defaultInfo)); |
| 1349 | mUniforms.push_back(new gl::LinkedUniform(GL_FLOAT, GL_HIGH_FLOAT, "gl_DepthRange.diff", 0, -1, defaultInfo)); |
| 1350 | } |
| 1351 | |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
| 1355 | void ProgramD3D::defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister) |
| 1356 | { |
| 1357 | ShShaderOutput outputType = rx::ShaderD3D::getCompilerOutputType(shader); |
| 1358 | sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType)); |
| 1359 | encoder.skipRegisters(uniformRegister); |
| 1360 | |
| 1361 | defineUniform(shader, uniform, uniform.name, &encoder); |
| 1362 | } |
| 1363 | |
| 1364 | void ProgramD3D::defineUniform(GLenum shader, const sh::ShaderVariable &uniform, |
| 1365 | const std::string &fullName, sh::HLSLBlockEncoder *encoder) |
| 1366 | { |
| 1367 | if (uniform.isStruct()) |
| 1368 | { |
| 1369 | for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++) |
| 1370 | { |
| 1371 | const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : ""); |
| 1372 | |
| 1373 | encoder->enterAggregateType(); |
| 1374 | |
| 1375 | for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++) |
| 1376 | { |
| 1377 | const sh::ShaderVariable &field = uniform.fields[fieldIndex]; |
| 1378 | const std::string &fieldFullName = (fullName + elementString + "." + field.name); |
| 1379 | |
| 1380 | defineUniform(shader, field, fieldFullName, encoder); |
| 1381 | } |
| 1382 | |
| 1383 | encoder->exitAggregateType(); |
| 1384 | } |
| 1385 | } |
| 1386 | else // Not a struct |
| 1387 | { |
| 1388 | // Arrays are treated as aggregate types |
| 1389 | if (uniform.isArray()) |
| 1390 | { |
| 1391 | encoder->enterAggregateType(); |
| 1392 | } |
| 1393 | |
| 1394 | gl::LinkedUniform *linkedUniform = getUniformByName(fullName); |
| 1395 | |
| 1396 | if (!linkedUniform) |
| 1397 | { |
| 1398 | linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize, |
| 1399 | -1, sh::BlockMemberInfo::getDefaultBlockInfo()); |
| 1400 | ASSERT(linkedUniform); |
| 1401 | linkedUniform->registerElement = encoder->getCurrentElement(); |
| 1402 | mUniforms.push_back(linkedUniform); |
| 1403 | } |
| 1404 | |
| 1405 | ASSERT(linkedUniform->registerElement == encoder->getCurrentElement()); |
| 1406 | |
| 1407 | if (shader == GL_FRAGMENT_SHADER) |
| 1408 | { |
| 1409 | linkedUniform->psRegisterIndex = encoder->getCurrentRegister(); |
| 1410 | } |
| 1411 | else if (shader == GL_VERTEX_SHADER) |
| 1412 | { |
| 1413 | linkedUniform->vsRegisterIndex = encoder->getCurrentRegister(); |
| 1414 | } |
| 1415 | else UNREACHABLE(); |
| 1416 | |
| 1417 | // Advance the uniform offset, to track registers allocation for structs |
| 1418 | encoder->encodeType(uniform.type, uniform.arraySize, false); |
| 1419 | |
| 1420 | // Arrays are treated as aggregate types |
| 1421 | if (uniform.isArray()) |
| 1422 | { |
| 1423 | encoder->exitAggregateType(); |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | template <typename T> |
| 1429 | static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag) |
| 1430 | { |
| 1431 | ASSERT(dest != NULL); |
| 1432 | ASSERT(dirtyFlag != NULL); |
| 1433 | |
| 1434 | *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0); |
| 1435 | *dest = source; |
| 1436 | } |
| 1437 | |
| 1438 | template <typename T> |
| 1439 | void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType) |
| 1440 | { |
| 1441 | const int components = gl::VariableComponentCount(targetUniformType); |
| 1442 | const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType); |
| 1443 | |
| 1444 | gl::LinkedUniform *targetUniform = getUniformByLocation(location); |
| 1445 | |
| 1446 | int elementCount = targetUniform->elementCount(); |
| 1447 | |
| 1448 | count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
| 1449 | |
| 1450 | if (targetUniform->type == targetUniformType) |
| 1451 | { |
| 1452 | T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4; |
| 1453 | |
| 1454 | for (int i = 0; i < count; i++) |
| 1455 | { |
| 1456 | T *dest = target + (i * 4); |
| 1457 | const T *source = v + (i * components); |
| 1458 | |
| 1459 | for (int c = 0; c < components; c++) |
| 1460 | { |
| 1461 | SetIfDirty(dest + c, source[c], &targetUniform->dirty); |
| 1462 | } |
| 1463 | for (int c = components; c < 4; c++) |
| 1464 | { |
| 1465 | SetIfDirty(dest + c, T(0), &targetUniform->dirty); |
| 1466 | } |
| 1467 | } |
| 1468 | } |
| 1469 | else if (targetUniform->type == targetBoolType) |
| 1470 | { |
| 1471 | GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4; |
| 1472 | |
| 1473 | for (int i = 0; i < count; i++) |
| 1474 | { |
| 1475 | GLint *dest = boolParams + (i * 4); |
| 1476 | const T *source = v + (i * components); |
| 1477 | |
| 1478 | for (int c = 0; c < components; c++) |
| 1479 | { |
| 1480 | SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty); |
| 1481 | } |
| 1482 | for (int c = components; c < 4; c++) |
| 1483 | { |
| 1484 | SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty); |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | else if (gl::IsSampler(targetUniform->type)) |
| 1489 | { |
| 1490 | ASSERT(targetUniformType == GL_INT); |
| 1491 | |
| 1492 | GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4; |
| 1493 | |
| 1494 | bool wasDirty = targetUniform->dirty; |
| 1495 | |
| 1496 | for (int i = 0; i < count; i++) |
| 1497 | { |
| 1498 | GLint *dest = target + (i * 4); |
| 1499 | const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components); |
| 1500 | |
| 1501 | SetIfDirty(dest + 0, source[0], &targetUniform->dirty); |
| 1502 | SetIfDirty(dest + 1, 0, &targetUniform->dirty); |
| 1503 | SetIfDirty(dest + 2, 0, &targetUniform->dirty); |
| 1504 | SetIfDirty(dest + 3, 0, &targetUniform->dirty); |
| 1505 | } |
| 1506 | |
| 1507 | if (!wasDirty && targetUniform->dirty) |
| 1508 | { |
| 1509 | mDirtySamplerMapping = true; |
| 1510 | } |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1511 | } |
| 1512 | else UNREACHABLE(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1513 | } |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1514 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1515 | template<typename T> |
| 1516 | bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight) |
| 1517 | { |
| 1518 | bool dirty = false; |
| 1519 | int copyWidth = std::min(targetHeight, srcWidth); |
| 1520 | int copyHeight = std::min(targetWidth, srcHeight); |
| 1521 | |
| 1522 | for (int x = 0; x < copyWidth; x++) |
| 1523 | { |
| 1524 | for (int y = 0; y < copyHeight; y++) |
| 1525 | { |
| 1526 | SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty); |
| 1527 | } |
| 1528 | } |
| 1529 | // clear unfilled right side |
| 1530 | for (int y = 0; y < copyWidth; y++) |
| 1531 | { |
| 1532 | for (int x = copyHeight; x < targetWidth; x++) |
| 1533 | { |
| 1534 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1535 | } |
| 1536 | } |
| 1537 | // clear unfilled bottom. |
| 1538 | for (int y = copyWidth; y < targetHeight; y++) |
| 1539 | { |
| 1540 | for (int x = 0; x < targetWidth; x++) |
| 1541 | { |
| 1542 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | return dirty; |
| 1547 | } |
| 1548 | |
| 1549 | template<typename T> |
| 1550 | bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight) |
| 1551 | { |
| 1552 | bool dirty = false; |
| 1553 | int copyWidth = std::min(targetWidth, srcWidth); |
| 1554 | int copyHeight = std::min(targetHeight, srcHeight); |
| 1555 | |
| 1556 | for (int y = 0; y < copyHeight; y++) |
| 1557 | { |
| 1558 | for (int x = 0; x < copyWidth; x++) |
| 1559 | { |
| 1560 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty); |
| 1561 | } |
| 1562 | } |
| 1563 | // clear unfilled right side |
| 1564 | for (int y = 0; y < copyHeight; y++) |
| 1565 | { |
| 1566 | for (int x = copyWidth; x < targetWidth; x++) |
| 1567 | { |
| 1568 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1569 | } |
| 1570 | } |
| 1571 | // clear unfilled bottom. |
| 1572 | for (int y = copyHeight; y < targetHeight; y++) |
| 1573 | { |
| 1574 | for (int x = 0; x < targetWidth; x++) |
| 1575 | { |
| 1576 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | return dirty; |
| 1581 | } |
| 1582 | |
| 1583 | template <int cols, int rows> |
| 1584 | void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType) |
| 1585 | { |
| 1586 | gl::LinkedUniform *targetUniform = getUniformByLocation(location); |
| 1587 | |
| 1588 | int elementCount = targetUniform->elementCount(); |
| 1589 | |
| 1590 | count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
| 1591 | const unsigned int targetMatrixStride = (4 * rows); |
| 1592 | GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride); |
| 1593 | |
| 1594 | for (int i = 0; i < count; i++) |
| 1595 | { |
| 1596 | // Internally store matrices as transposed versions to accomodate HLSL matrix indexing |
| 1597 | if (transpose == GL_FALSE) |
| 1598 | { |
| 1599 | targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty; |
| 1600 | } |
| 1601 | else |
| 1602 | { |
| 1603 | targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty; |
| 1604 | } |
| 1605 | target += targetMatrixStride; |
| 1606 | value += cols * rows; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | template <typename T> |
| 1611 | void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType) |
| 1612 | { |
| 1613 | gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 1614 | |
| 1615 | if (gl::IsMatrixType(targetUniform->type)) |
| 1616 | { |
| 1617 | const int rows = gl::VariableRowCount(targetUniform->type); |
| 1618 | const int cols = gl::VariableColumnCount(targetUniform->type); |
| 1619 | transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows); |
| 1620 | } |
| 1621 | else if (uniformType == gl::VariableComponentType(targetUniform->type)) |
| 1622 | { |
| 1623 | unsigned int size = gl::VariableComponentCount(targetUniform->type); |
| 1624 | memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T), |
| 1625 | size * sizeof(T)); |
| 1626 | } |
| 1627 | else |
| 1628 | { |
| 1629 | unsigned int size = gl::VariableComponentCount(targetUniform->type); |
| 1630 | switch (gl::VariableComponentType(targetUniform->type)) |
| 1631 | { |
| 1632 | case GL_BOOL: |
| 1633 | { |
| 1634 | GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1635 | |
| 1636 | for (unsigned int i = 0; i < size; i++) |
| 1637 | { |
| 1638 | params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1); |
| 1639 | } |
| 1640 | } |
| 1641 | break; |
| 1642 | |
| 1643 | case GL_FLOAT: |
| 1644 | { |
| 1645 | GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1646 | |
| 1647 | for (unsigned int i = 0; i < size; i++) |
| 1648 | { |
| 1649 | params[i] = static_cast<T>(floatParams[i]); |
| 1650 | } |
| 1651 | } |
| 1652 | break; |
| 1653 | |
| 1654 | case GL_INT: |
| 1655 | { |
| 1656 | GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1657 | |
| 1658 | for (unsigned int i = 0; i < size; i++) |
| 1659 | { |
| 1660 | params[i] = static_cast<T>(intParams[i]); |
| 1661 | } |
| 1662 | } |
| 1663 | break; |
| 1664 | |
| 1665 | case GL_UNSIGNED_INT: |
| 1666 | { |
| 1667 | GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1668 | |
| 1669 | for (unsigned int i = 0; i < size; i++) |
| 1670 | { |
| 1671 | params[i] = static_cast<T>(uintParams[i]); |
| 1672 | } |
| 1673 | } |
| 1674 | break; |
| 1675 | |
| 1676 | default: UNREACHABLE(); |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | template <typename VarT> |
| 1682 | void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex, |
| 1683 | sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes, |
| 1684 | bool inRowMajorLayout) |
| 1685 | { |
| 1686 | for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++) |
| 1687 | { |
| 1688 | const VarT &field = fields[uniformIndex]; |
| 1689 | const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name); |
| 1690 | |
| 1691 | if (field.isStruct()) |
| 1692 | { |
| 1693 | bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field)); |
| 1694 | |
| 1695 | for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++) |
| 1696 | { |
| 1697 | encoder->enterAggregateType(); |
| 1698 | |
| 1699 | const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : ""); |
| 1700 | defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout); |
| 1701 | |
| 1702 | encoder->exitAggregateType(); |
| 1703 | } |
| 1704 | } |
| 1705 | else |
| 1706 | { |
| 1707 | bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout); |
| 1708 | |
| 1709 | sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix); |
| 1710 | |
| 1711 | gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize, |
| 1712 | blockIndex, memberInfo); |
| 1713 | |
| 1714 | // add to uniform list, but not index, since uniform block uniforms have no location |
| 1715 | blockUniformIndexes->push_back(mUniforms.size()); |
| 1716 | mUniforms.push_back(newUniform); |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | bool ProgramD3D::defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock, |
| 1722 | const gl::Caps &caps) |
| 1723 | { |
| 1724 | const rx::ShaderD3D* shaderD3D = rx::ShaderD3D::makeShaderD3D(shader.getImplementation()); |
| 1725 | |
| 1726 | // create uniform block entries if they do not exist |
| 1727 | if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX) |
| 1728 | { |
| 1729 | std::vector<unsigned int> blockUniformIndexes; |
| 1730 | const unsigned int blockIndex = mUniformBlocks.size(); |
| 1731 | |
| 1732 | // define member uniforms |
| 1733 | sh::BlockLayoutEncoder *encoder = NULL; |
| 1734 | |
| 1735 | if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD) |
| 1736 | { |
| 1737 | encoder = new sh::Std140BlockEncoder; |
| 1738 | } |
| 1739 | else |
| 1740 | { |
| 1741 | encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED); |
| 1742 | } |
| 1743 | ASSERT(encoder); |
| 1744 | |
| 1745 | defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout); |
| 1746 | |
| 1747 | size_t dataSize = encoder->getBlockSize(); |
| 1748 | |
| 1749 | // create all the uniform blocks |
| 1750 | if (interfaceBlock.arraySize > 0) |
| 1751 | { |
| 1752 | for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++) |
| 1753 | { |
| 1754 | gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize); |
| 1755 | newUniformBlock->memberUniformIndexes = blockUniformIndexes; |
| 1756 | mUniformBlocks.push_back(newUniformBlock); |
| 1757 | } |
| 1758 | } |
| 1759 | else |
| 1760 | { |
| 1761 | gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize); |
| 1762 | newUniformBlock->memberUniformIndexes = blockUniformIndexes; |
| 1763 | mUniformBlocks.push_back(newUniformBlock); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | if (interfaceBlock.staticUse) |
| 1768 | { |
| 1769 | // Assign registers to the uniform blocks |
| 1770 | const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name); |
| 1771 | const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize); |
| 1772 | ASSERT(blockIndex != GL_INVALID_INDEX); |
| 1773 | ASSERT(blockIndex + elementCount <= mUniformBlocks.size()); |
| 1774 | |
| 1775 | unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name); |
| 1776 | |
| 1777 | for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++) |
| 1778 | { |
| 1779 | gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement]; |
| 1780 | ASSERT(uniformBlock->name == interfaceBlock.name); |
| 1781 | |
| 1782 | if (!assignUniformBlockRegister(infoLog, uniformBlock, shader.getType(), |
| 1783 | interfaceBlockRegister + uniformBlockElement, caps)) |
| 1784 | { |
| 1785 | return false; |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | return true; |
| 1791 | } |
| 1792 | |
| 1793 | bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex, |
| 1794 | GLenum samplerType, |
| 1795 | unsigned int samplerCount, |
| 1796 | std::vector<Sampler> &outSamplers, |
| 1797 | GLuint *outUsedRange) |
| 1798 | { |
| 1799 | unsigned int samplerIndex = startSamplerIndex; |
| 1800 | |
| 1801 | do |
| 1802 | { |
| 1803 | if (samplerIndex < outSamplers.size()) |
| 1804 | { |
| 1805 | Sampler& sampler = outSamplers[samplerIndex]; |
| 1806 | sampler.active = true; |
| 1807 | sampler.textureType = GetTextureType(samplerType); |
| 1808 | sampler.logicalTextureUnit = 0; |
| 1809 | *outUsedRange = std::max(samplerIndex + 1, *outUsedRange); |
| 1810 | } |
| 1811 | else |
| 1812 | { |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | samplerIndex++; |
| 1817 | } while (samplerIndex < startSamplerIndex + samplerCount); |
| 1818 | |
| 1819 | return true; |
| 1820 | } |
| 1821 | |
| 1822 | bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps) |
| 1823 | { |
| 1824 | ASSERT(gl::IsSampler(uniform.type)); |
| 1825 | ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX); |
| 1826 | |
| 1827 | if (uniform.vsRegisterIndex != GL_INVALID_INDEX) |
| 1828 | { |
| 1829 | if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS, |
| 1830 | &mUsedVertexSamplerRange)) |
| 1831 | { |
| 1832 | infoLog.append("Vertex shader sampler count exceeds the maximum vertex texture units (%d).", |
| 1833 | mSamplersVS.size()); |
| 1834 | return false; |
| 1835 | } |
| 1836 | |
| 1837 | unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors; |
| 1838 | if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors) |
| 1839 | { |
| 1840 | infoLog.append("Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (%u)", |
| 1841 | caps.maxVertexUniformVectors); |
| 1842 | return false; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | if (uniform.psRegisterIndex != GL_INVALID_INDEX) |
| 1847 | { |
| 1848 | if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS, |
| 1849 | &mUsedPixelSamplerRange)) |
| 1850 | { |
| 1851 | infoLog.append("Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (%d).", |
| 1852 | mSamplersPS.size()); |
| 1853 | return false; |
| 1854 | } |
| 1855 | |
| 1856 | unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors; |
| 1857 | if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors) |
| 1858 | { |
| 1859 | infoLog.append("Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (%u)", |
| 1860 | caps.maxFragmentUniformVectors); |
| 1861 | return false; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | return true; |
| 1866 | } |
| 1867 | |
| 1868 | bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps) |
| 1869 | { |
| 1870 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 1871 | { |
| 1872 | const gl::LinkedUniform &uniform = *mUniforms[uniformIndex]; |
| 1873 | |
| 1874 | if (gl::IsSampler(uniform.type)) |
| 1875 | { |
| 1876 | if (!indexSamplerUniform(uniform, infoLog, caps)) |
| 1877 | { |
| 1878 | return false; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | for (unsigned int arrayElementIndex = 0; arrayElementIndex < uniform.elementCount(); arrayElementIndex++) |
| 1883 | { |
| 1884 | mUniformIndex.push_back(gl::VariableLocation(uniform.name, arrayElementIndex, uniformIndex)); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | return true; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1891 | void ProgramD3D::reset() |
| 1892 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1893 | ProgramImpl::reset(); |
| 1894 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1895 | SafeDeleteContainer(mVertexExecutables); |
| 1896 | SafeDeleteContainer(mPixelExecutables); |
| 1897 | SafeDelete(mGeometryExecutable); |
| 1898 | |
| 1899 | mTransformFeedbackBufferMode = GL_NONE; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1900 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1901 | mVertexHLSL.clear(); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1902 | mVertexWorkarounds = ANGLE_D3D_WORKAROUND_NONE; |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1903 | mShaderVersion = 100; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1904 | |
| 1905 | mPixelHLSL.clear(); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1906 | mPixelWorkarounds = ANGLE_D3D_WORKAROUND_NONE; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1907 | mUsesFragDepth = false; |
| 1908 | mPixelShaderKey.clear(); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1909 | mUsesPointSize = false; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1910 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1911 | SafeDelete(mVertexUniformStorage); |
| 1912 | SafeDelete(mFragmentUniformStorage); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1913 | |
| 1914 | mSamplersPS.clear(); |
| 1915 | mSamplersVS.clear(); |
| 1916 | |
| 1917 | mUsedVertexSamplerRange = 0; |
| 1918 | mUsedPixelSamplerRange = 0; |
| 1919 | mDirtySamplerMapping = true; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | } |