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