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