Context.cpp revision 5669f0d17da2867b59a6d34dd9c83b77c77ea5e5
1// SwiftShader Software Renderer 2// 3// Copyright(c) 2005-2013 TransGaming Inc. 4// 5// All rights reserved. No part of this software may be copied, distributed, transmitted, 6// transcribed, stored in a retrieval system, translated into any human or computer 7// language by any means, or disclosed to third parties without the explicit written 8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express 9// or implied, including but not limited to any patent rights, are granted to you. 10// 11 12// Context.cpp: Implements the es1::Context class, managing all GL state and performing 13// rendering operations. It is the GLES2 specific implementation of EGLContext. 14 15#include "Context.h" 16 17#include "main.h" 18#include "mathutil.h" 19#include "utilities.h" 20#include "ResourceManager.h" 21#include "Buffer.h" 22#include "Framebuffer.h" 23#include "Renderbuffer.h" 24#include "Texture.h" 25#include "VertexDataManager.h" 26#include "IndexDataManager.h" 27#include "libEGL/Display.h" 28#include "libEGL/Surface.h" 29#include "Common/Half.hpp" 30 31#include <EGL/eglext.h> 32 33using std::abs; 34 35namespace es1 36{ 37Context::Context(const egl::Config *config, const Context *shareContext) 38 : modelViewStack(MAX_MODELVIEW_STACK_DEPTH), 39 projectionStack(MAX_PROJECTION_STACK_DEPTH), 40 textureStack0(MAX_TEXTURE_STACK_DEPTH), 41 textureStack1(MAX_TEXTURE_STACK_DEPTH) 42{ 43 sw::Context *context = new sw::Context(); 44 device = new es1::Device(context); 45 46 mVertexDataManager = new VertexDataManager(this); 47 mIndexDataManager = new IndexDataManager(); 48 49 setClearColor(0.0f, 0.0f, 0.0f, 0.0f); 50 51 mState.depthClearValue = 1.0f; 52 mState.stencilClearValue = 0; 53 54 mState.cullFaceEnabled = false; 55 mState.cullMode = GL_BACK; 56 mState.frontFace = GL_CCW; 57 mState.depthTestEnabled = false; 58 mState.depthFunc = GL_LESS; 59 mState.blendEnabled = false; 60 mState.sourceBlendRGB = GL_ONE; 61 mState.sourceBlendAlpha = GL_ONE; 62 mState.destBlendRGB = GL_ZERO; 63 mState.destBlendAlpha = GL_ZERO; 64 mState.blendEquationRGB = GL_FUNC_ADD_OES; 65 mState.blendEquationAlpha = GL_FUNC_ADD_OES; 66 mState.stencilTestEnabled = false; 67 mState.stencilFunc = GL_ALWAYS; 68 mState.stencilRef = 0; 69 mState.stencilMask = -1; 70 mState.stencilWritemask = -1; 71 mState.stencilFail = GL_KEEP; 72 mState.stencilPassDepthFail = GL_KEEP; 73 mState.stencilPassDepthPass = GL_KEEP; 74 mState.polygonOffsetFillEnabled = false; 75 mState.polygonOffsetFactor = 0.0f; 76 mState.polygonOffsetUnits = 0.0f; 77 mState.sampleAlphaToCoverageEnabled = false; 78 mState.sampleCoverageEnabled = false; 79 mState.sampleCoverageValue = 1.0f; 80 mState.sampleCoverageInvert = false; 81 mState.scissorTestEnabled = false; 82 mState.ditherEnabled = true; 83 mState.shadeModel = GL_SMOOTH; 84 mState.generateMipmapHint = GL_DONT_CARE; 85 mState.perspectiveCorrectionHint = GL_DONT_CARE; 86 mState.fogHint = GL_DONT_CARE; 87 88 mState.lineWidth = 1.0f; 89 90 mState.viewportX = 0; 91 mState.viewportY = 0; 92 mState.viewportWidth = 0; 93 mState.viewportHeight = 0; 94 mState.zNear = 0.0f; 95 mState.zFar = 1.0f; 96 97 mState.scissorX = 0; 98 mState.scissorY = 0; 99 mState.scissorWidth = 0; 100 mState.scissorHeight = 0; 101 102 mState.colorMaskRed = true; 103 mState.colorMaskGreen = true; 104 mState.colorMaskBlue = true; 105 mState.colorMaskAlpha = true; 106 mState.depthMask = true; 107 108 for(int i = 0; i < MAX_TEXTURE_UNITS; i++) 109 { 110 mState.textureUnit[i].color = {0, 0, 0, 0}; 111 mState.textureUnit[i].environmentMode = GL_MODULATE; 112 mState.textureUnit[i].combineRGB = GL_MODULATE; 113 mState.textureUnit[i].combineAlpha = GL_MODULATE; 114 mState.textureUnit[i].src0RGB = GL_TEXTURE; 115 mState.textureUnit[i].src1RGB = GL_PREVIOUS; 116 mState.textureUnit[i].src2RGB = GL_CONSTANT; 117 mState.textureUnit[i].src0Alpha = GL_TEXTURE; 118 mState.textureUnit[i].src1Alpha = GL_PREVIOUS; 119 mState.textureUnit[i].src2Alpha = GL_CONSTANT; 120 mState.textureUnit[i].operand0RGB = GL_SRC_COLOR; 121 mState.textureUnit[i].operand1RGB = GL_SRC_COLOR; 122 mState.textureUnit[i].operand2RGB = GL_SRC_ALPHA; 123 mState.textureUnit[i].operand0Alpha = GL_SRC_ALPHA; 124 mState.textureUnit[i].operand1Alpha = GL_SRC_ALPHA; 125 mState.textureUnit[i].operand2Alpha = GL_SRC_ALPHA; 126 } 127 128 if(shareContext) 129 { 130 mResourceManager = shareContext->mResourceManager; 131 mResourceManager->addRef(); 132 } 133 else 134 { 135 mResourceManager = new ResourceManager(); 136 } 137 138 // [OpenGL ES 2.0.24] section 3.7 page 83: 139 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional 140 // and cube map texture state vectors respectively associated with them. 141 // In order that access to these initial textures not be lost, they are treated as texture 142 // objects all of whose names are 0. 143 144 mTexture2DZero = new Texture2D(0); 145 mTextureExternalZero = new TextureExternal(0); 146 147 mState.activeSampler = 0; 148 bindArrayBuffer(0); 149 bindElementArrayBuffer(0); 150 bindTexture2D(0); 151 bindFramebuffer(0); 152 bindRenderbuffer(0); 153 154 mState.packAlignment = 4; 155 mState.unpackAlignment = 4; 156 157 mInvalidEnum = false; 158 mInvalidValue = false; 159 mInvalidOperation = false; 160 mOutOfMemory = false; 161 mInvalidFramebufferOperation = false; 162 mMatrixStackOverflow = false; 163 mMatrixStackUnderflow = false; 164 165 lightingEnabled = false; 166 167 for(int i = 0; i < MAX_LIGHTS; i++) 168 { 169 light[i].enabled = false; 170 light[i].ambient = {0.0f, 0.0f, 0.0f, 1.0f}; 171 light[i].diffuse = {0.0f, 0.0f, 0.0f, 1.0f}; 172 light[i].specular = {0.0f, 0.0f, 0.0f, 1.0f}; 173 light[i].position = {0.0f, 0.0f, 1.0f, 0.0f}; 174 light[i].direction = {0.0f, 0.0f, -1.0f}; 175 light[i].attenuation = {1.0f, 0.0f, 0.0f}; 176 light[i].spotExponent = 0.0f; 177 light[i].spotCutoffAngle = 180.0f; 178 } 179 180 light[0].diffuse = {1.0f, 1.0f, 1.0f, 1.0f}; 181 light[0].specular = {1.0f, 1.0f, 1.0f, 1.0f}; 182 183 globalAmbient = {0.2f, 0.2f, 0.2f, 1.0f}; 184 materialAmbient = {0.2f, 0.2f, 0.2f, 1.0f}; 185 materialDiffuse = {0.8f, 0.8f, 0.8f, 1.0f}; 186 materialSpecular = {0.0f, 0.0f, 0.0f, 1.0f}; 187 materialEmission = {0.0f, 0.0f, 0.0f, 1.0f}; 188 materialShininess = 0.0f; 189 lightModelTwoSide = false; 190 191 matrixMode = GL_MODELVIEW; 192 193 for(int i = 0; i < MAX_TEXTURE_UNITS; i++) 194 { 195 texture2Denabled[i] = false; 196 textureExternalEnabled[i] = false; 197 } 198 199 clientTexture = GL_TEXTURE0; 200 201 setVertexAttrib(sw::Color0, 1.0f, 1.0f, 1.0f, 1.0f); 202 203 for(int i = 0; i < MAX_TEXTURE_UNITS; i++) 204 { 205 setVertexAttrib(sw::TexCoord0 + i, 0.0f, 0.0f, 0.0f, 1.0f); 206 } 207 208 setVertexAttrib(sw::Normal, 0.0f, 0.0f, 1.0f, 1.0f); 209 setVertexAttrib(sw::PointSize, 1.0f, 1.0f, 1.0f, 1.0f); 210 211 clipFlags = 0; 212 213 alphaTestEnabled = false; 214 alphaTestFunc = GL_ALWAYS; 215 alphaTestRef = 0; 216 217 fogEnabled = false; 218 fogMode = GL_EXP; 219 fogDensity = 1.0f; 220 fogStart = 0.0f; 221 fogEnd = 1.0f; 222 fogColor = {0, 0, 0, 0}; 223 224 lineSmoothEnabled = false; 225 colorMaterialEnabled = false; 226 normalizeEnabled = false; 227 rescaleNormalEnabled = false; 228 multisampleEnabled = true; 229 sampleAlphaToOneEnabled = false; 230 231 colorLogicOpEnabled = false; 232 logicalOperation = GL_COPY; 233 234 pointSpriteEnabled = false; 235 pointSmoothEnabled = false; 236 pointSizeMin = 0.0f; 237 pointSizeMax = 1.0f; 238 pointDistanceAttenuation = {1.0f, 0.0f, 0.0f}; 239 pointFadeThresholdSize = 1.0f; 240 241 mHasBeenCurrent = false; 242 243 markAllStateDirty(); 244} 245 246Context::~Context() 247{ 248 while(!mFramebufferMap.empty()) 249 { 250 deleteFramebuffer(mFramebufferMap.begin()->first); 251 } 252 253 for(int type = 0; type < TEXTURE_TYPE_COUNT; type++) 254 { 255 for(int sampler = 0; sampler < MAX_TEXTURE_UNITS; sampler++) 256 { 257 mState.samplerTexture[type][sampler] = nullptr; 258 } 259 } 260 261 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++) 262 { 263 mState.vertexAttribute[i].mBoundBuffer = nullptr; 264 } 265 266 mState.arrayBuffer = nullptr; 267 mState.elementArrayBuffer = nullptr; 268 mState.renderbuffer = nullptr; 269 270 mTexture2DZero = nullptr; 271 mTextureExternalZero = nullptr; 272 273 delete mVertexDataManager; 274 delete mIndexDataManager; 275 276 mResourceManager->release(); 277 delete device; 278} 279 280void Context::makeCurrent(egl::Surface *surface) 281{ 282 if(!mHasBeenCurrent) 283 { 284 mState.viewportX = 0; 285 mState.viewportY = 0; 286 mState.viewportWidth = surface->getWidth(); 287 mState.viewportHeight = surface->getHeight(); 288 289 mState.scissorX = 0; 290 mState.scissorY = 0; 291 mState.scissorWidth = surface->getWidth(); 292 mState.scissorHeight = surface->getHeight(); 293 294 mHasBeenCurrent = true; 295 } 296 297 // Wrap the existing resources into GL objects and assign them to the '0' names 298 egl::Image *defaultRenderTarget = surface->getRenderTarget(); 299 egl::Image *depthStencil = surface->getDepthStencil(); 300 301 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget); 302 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil); 303 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero); 304 305 setFramebufferZero(framebufferZero); 306 307 if(defaultRenderTarget) 308 { 309 defaultRenderTarget->release(); 310 } 311 312 if(depthStencil) 313 { 314 depthStencil->release(); 315 } 316 317 markAllStateDirty(); 318} 319 320int Context::getClientVersion() const 321{ 322 return 1; 323} 324 325// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw. 326void Context::markAllStateDirty() 327{ 328 mDepthStateDirty = true; 329 mMaskStateDirty = true; 330 mBlendStateDirty = true; 331 mStencilStateDirty = true; 332 mPolygonOffsetStateDirty = true; 333 mSampleStateDirty = true; 334 mDitherStateDirty = true; 335 mFrontFaceDirty = true; 336} 337 338void Context::setClearColor(float red, float green, float blue, float alpha) 339{ 340 mState.colorClearValue.red = red; 341 mState.colorClearValue.green = green; 342 mState.colorClearValue.blue = blue; 343 mState.colorClearValue.alpha = alpha; 344} 345 346void Context::setClearDepth(float depth) 347{ 348 mState.depthClearValue = depth; 349} 350 351void Context::setClearStencil(int stencil) 352{ 353 mState.stencilClearValue = stencil; 354} 355 356void Context::setCullFaceEnabled(bool enabled) 357{ 358 mState.cullFaceEnabled = enabled; 359} 360 361bool Context::isCullFaceEnabled() const 362{ 363 return mState.cullFaceEnabled; 364} 365 366void Context::setCullMode(GLenum mode) 367{ 368 mState.cullMode = mode; 369} 370 371void Context::setFrontFace(GLenum front) 372{ 373 if(mState.frontFace != front) 374 { 375 mState.frontFace = front; 376 mFrontFaceDirty = true; 377 } 378} 379 380void Context::setDepthTestEnabled(bool enabled) 381{ 382 if(mState.depthTestEnabled != enabled) 383 { 384 mState.depthTestEnabled = enabled; 385 mDepthStateDirty = true; 386 } 387} 388 389bool Context::isDepthTestEnabled() const 390{ 391 return mState.depthTestEnabled; 392} 393 394void Context::setDepthFunc(GLenum depthFunc) 395{ 396 if(mState.depthFunc != depthFunc) 397 { 398 mState.depthFunc = depthFunc; 399 mDepthStateDirty = true; 400 } 401} 402 403void Context::setDepthRange(float zNear, float zFar) 404{ 405 mState.zNear = zNear; 406 mState.zFar = zFar; 407} 408 409void Context::setAlphaTestEnabled(bool enabled) 410{ 411 alphaTestEnabled = enabled; 412} 413 414bool Context::isAlphaTestEnabled() const 415{ 416 return alphaTestEnabled; 417} 418 419void Context::setAlphaFunc(GLenum alphaFunc, GLclampf reference) 420{ 421 alphaTestFunc = alphaFunc; 422 alphaTestRef = reference; 423} 424 425void Context::setBlendEnabled(bool enabled) 426{ 427 if(mState.blendEnabled != enabled) 428 { 429 mState.blendEnabled = enabled; 430 mBlendStateDirty = true; 431 } 432} 433 434bool Context::isBlendEnabled() const 435{ 436 return mState.blendEnabled; 437} 438 439void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha) 440{ 441 if(mState.sourceBlendRGB != sourceRGB || 442 mState.sourceBlendAlpha != sourceAlpha || 443 mState.destBlendRGB != destRGB || 444 mState.destBlendAlpha != destAlpha) 445 { 446 mState.sourceBlendRGB = sourceRGB; 447 mState.destBlendRGB = destRGB; 448 mState.sourceBlendAlpha = sourceAlpha; 449 mState.destBlendAlpha = destAlpha; 450 mBlendStateDirty = true; 451 } 452} 453 454void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation) 455{ 456 if(mState.blendEquationRGB != rgbEquation || 457 mState.blendEquationAlpha != alphaEquation) 458 { 459 mState.blendEquationRGB = rgbEquation; 460 mState.blendEquationAlpha = alphaEquation; 461 mBlendStateDirty = true; 462 } 463} 464 465void Context::setStencilTestEnabled(bool enabled) 466{ 467 if(mState.stencilTestEnabled != enabled) 468 { 469 mState.stencilTestEnabled = enabled; 470 mStencilStateDirty = true; 471 } 472} 473 474bool Context::isStencilTestEnabled() const 475{ 476 return mState.stencilTestEnabled; 477} 478 479void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask) 480{ 481 if(mState.stencilFunc != stencilFunc || 482 mState.stencilRef != stencilRef || 483 mState.stencilMask != stencilMask) 484 { 485 mState.stencilFunc = stencilFunc; 486 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0; 487 mState.stencilMask = stencilMask; 488 mStencilStateDirty = true; 489 } 490} 491 492void Context::setStencilWritemask(GLuint stencilWritemask) 493{ 494 if(mState.stencilWritemask != stencilWritemask) 495 { 496 mState.stencilWritemask = stencilWritemask; 497 mStencilStateDirty = true; 498 } 499} 500 501void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass) 502{ 503 if(mState.stencilFail != stencilFail || 504 mState.stencilPassDepthFail != stencilPassDepthFail || 505 mState.stencilPassDepthPass != stencilPassDepthPass) 506 { 507 mState.stencilFail = stencilFail; 508 mState.stencilPassDepthFail = stencilPassDepthFail; 509 mState.stencilPassDepthPass = stencilPassDepthPass; 510 mStencilStateDirty = true; 511 } 512} 513 514void Context::setPolygonOffsetFillEnabled(bool enabled) 515{ 516 if(mState.polygonOffsetFillEnabled != enabled) 517 { 518 mState.polygonOffsetFillEnabled = enabled; 519 mPolygonOffsetStateDirty = true; 520 } 521} 522 523bool Context::isPolygonOffsetFillEnabled() const 524{ 525 return mState.polygonOffsetFillEnabled; 526} 527 528void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units) 529{ 530 if(mState.polygonOffsetFactor != factor || 531 mState.polygonOffsetUnits != units) 532 { 533 mState.polygonOffsetFactor = factor; 534 mState.polygonOffsetUnits = units; 535 mPolygonOffsetStateDirty = true; 536 } 537} 538 539void Context::setSampleAlphaToCoverageEnabled(bool enabled) 540{ 541 if(mState.sampleAlphaToCoverageEnabled != enabled) 542 { 543 mState.sampleAlphaToCoverageEnabled = enabled; 544 mSampleStateDirty = true; 545 } 546} 547 548bool Context::isSampleAlphaToCoverageEnabled() const 549{ 550 return mState.sampleAlphaToCoverageEnabled; 551} 552 553void Context::setSampleCoverageEnabled(bool enabled) 554{ 555 if(mState.sampleCoverageEnabled != enabled) 556 { 557 mState.sampleCoverageEnabled = enabled; 558 mSampleStateDirty = true; 559 } 560} 561 562bool Context::isSampleCoverageEnabled() const 563{ 564 return mState.sampleCoverageEnabled; 565} 566 567void Context::setSampleCoverageParams(GLclampf value, bool invert) 568{ 569 if(mState.sampleCoverageValue != value || 570 mState.sampleCoverageInvert != invert) 571 { 572 mState.sampleCoverageValue = value; 573 mState.sampleCoverageInvert = invert; 574 mSampleStateDirty = true; 575 } 576} 577 578void Context::setScissorTestEnabled(bool enabled) 579{ 580 mState.scissorTestEnabled = enabled; 581} 582 583bool Context::isScissorTestEnabled() const 584{ 585 return mState.scissorTestEnabled; 586} 587 588void Context::setShadeModel(GLenum mode) 589{ 590 mState.shadeModel = mode; 591} 592 593void Context::setDitherEnabled(bool enabled) 594{ 595 if(mState.ditherEnabled != enabled) 596 { 597 mState.ditherEnabled = enabled; 598 mDitherStateDirty = true; 599 } 600} 601 602bool Context::isDitherEnabled() const 603{ 604 return mState.ditherEnabled; 605} 606 607void Context::setLightingEnabled(bool enable) 608{ 609 lightingEnabled = enable; 610} 611 612bool Context::isLightingEnabled() const 613{ 614 return lightingEnabled; 615} 616 617void Context::setLightEnabled(int index, bool enable) 618{ 619 light[index].enabled = enable; 620} 621 622bool Context::isLightEnabled(int index) const 623{ 624 return light[index].enabled; 625} 626 627void Context::setLightAmbient(int index, float r, float g, float b, float a) 628{ 629 light[index].ambient = {r, g, b, a}; 630} 631 632void Context::setLightDiffuse(int index, float r, float g, float b, float a) 633{ 634 light[index].diffuse = {r, g, b, a}; 635} 636 637void Context::setLightSpecular(int index, float r, float g, float b, float a) 638{ 639 light[index].specular = {r, g, b, a}; 640} 641 642void Context::setLightPosition(int index, float x, float y, float z, float w) 643{ 644 sw::float4 v = {x, y, z, w}; 645 646 // Transform from object coordinates to eye coordinates 647 v = modelViewStack.current() * v; 648 649 light[index].position = {v.x, v.y, v.z, v.w}; 650} 651 652void Context::setLightDirection(int index, float x, float y, float z) 653{ 654 // FIXME: Transform by inverse of 3x3 model-view matrix 655 light[index].direction = {x, y, z}; 656} 657 658void Context::setLightAttenuationConstant(int index, float constant) 659{ 660 light[index].attenuation.constant = constant; 661} 662 663void Context::setLightAttenuationLinear(int index, float linear) 664{ 665 light[index].attenuation.linear = linear; 666} 667 668void Context::setLightAttenuationQuadratic(int index, float quadratic) 669{ 670 light[index].attenuation.quadratic = quadratic; 671} 672 673void Context::setSpotLightExponent(int index, float exponent) 674{ 675 light[index].spotExponent = exponent; 676} 677 678void Context::setSpotLightCutoff(int index, float cutoff) 679{ 680 light[index].spotCutoffAngle = cutoff; 681} 682 683void Context::setGlobalAmbient(float red, float green, float blue, float alpha) 684{ 685 globalAmbient.red = red; 686 globalAmbient.green = green; 687 globalAmbient.blue = blue; 688 globalAmbient.alpha = alpha; 689} 690 691void Context::setMaterialAmbient(float red, float green, float blue, float alpha) 692{ 693 materialAmbient.red = red; 694 materialAmbient.green = green; 695 materialAmbient.blue = blue; 696 materialAmbient.alpha = alpha; 697} 698 699void Context::setMaterialDiffuse(float red, float green, float blue, float alpha) 700{ 701 materialDiffuse.red = red; 702 materialDiffuse.green = green; 703 materialDiffuse.blue = blue; 704 materialDiffuse.alpha = alpha; 705} 706 707void Context::setMaterialSpecular(float red, float green, float blue, float alpha) 708{ 709 materialSpecular.red = red; 710 materialSpecular.green = green; 711 materialSpecular.blue = blue; 712 materialSpecular.alpha = alpha; 713} 714 715void Context::setMaterialEmission(float red, float green, float blue, float alpha) 716{ 717 materialEmission.red = red; 718 materialEmission.green = green; 719 materialEmission.blue = blue; 720 materialEmission.alpha = alpha; 721} 722 723void Context::setMaterialShininess(float shininess) 724{ 725 materialShininess = shininess; 726} 727 728void Context::setLightModelTwoSide(bool enable) 729{ 730 lightModelTwoSide = enable; 731} 732 733void Context::setFogEnabled(bool enable) 734{ 735 fogEnabled = enable; 736} 737 738bool Context::isFogEnabled() const 739{ 740 return fogEnabled; 741} 742 743void Context::setFogMode(GLenum mode) 744{ 745 fogMode = mode; 746} 747 748void Context::setFogDensity(float fogDensity) 749{ 750 this->fogDensity = fogDensity; 751} 752 753void Context::setFogStart(float fogStart) 754{ 755 this->fogStart = fogStart; 756} 757 758void Context::setFogEnd(float fogEnd) 759{ 760 this->fogEnd = fogEnd; 761} 762 763void Context::setFogColor(float r, float g, float b, float a) 764{ 765 this->fogColor = {r, g, b, a}; 766} 767 768void Context::setTexture2Denabled(bool enable) 769{ 770 texture2Denabled[mState.activeSampler] = enable; 771} 772 773bool Context::isTexture2Denabled() const 774{ 775 return texture2Denabled[mState.activeSampler]; 776} 777 778void Context::setTextureExternalEnabled(bool enable) 779{ 780 textureExternalEnabled[mState.activeSampler] = enable; 781} 782 783bool Context::isTextureExternalEnabled() const 784{ 785 return textureExternalEnabled[mState.activeSampler]; 786} 787 788void Context::setLineWidth(GLfloat width) 789{ 790 mState.lineWidth = width; 791 device->setLineWidth(clamp(width, ALIASED_LINE_WIDTH_RANGE_MIN, ALIASED_LINE_WIDTH_RANGE_MAX)); 792} 793 794void Context::setGenerateMipmapHint(GLenum hint) 795{ 796 mState.generateMipmapHint = hint; 797} 798 799void Context::setPerspectiveCorrectionHint(GLenum hint) 800{ 801 mState.perspectiveCorrectionHint = hint; 802} 803 804void Context::setFogHint(GLenum hint) 805{ 806 mState.fogHint = hint; 807} 808 809void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height) 810{ 811 mState.viewportX = x; 812 mState.viewportY = y; 813 mState.viewportWidth = width; 814 mState.viewportHeight = height; 815} 816 817void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height) 818{ 819 mState.scissorX = x; 820 mState.scissorY = y; 821 mState.scissorWidth = width; 822 mState.scissorHeight = height; 823} 824 825void Context::setColorMask(bool red, bool green, bool blue, bool alpha) 826{ 827 if(mState.colorMaskRed != red || mState.colorMaskGreen != green || 828 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha) 829 { 830 mState.colorMaskRed = red; 831 mState.colorMaskGreen = green; 832 mState.colorMaskBlue = blue; 833 mState.colorMaskAlpha = alpha; 834 mMaskStateDirty = true; 835 } 836} 837 838void Context::setDepthMask(bool mask) 839{ 840 if(mState.depthMask != mask) 841 { 842 mState.depthMask = mask; 843 mMaskStateDirty = true; 844 } 845} 846 847void Context::setActiveSampler(unsigned int active) 848{ 849 mState.activeSampler = active; 850} 851 852GLuint Context::getFramebufferName() const 853{ 854 return mState.framebuffer; 855} 856 857GLuint Context::getRenderbufferName() const 858{ 859 return mState.renderbuffer.name(); 860} 861 862GLuint Context::getArrayBufferName() const 863{ 864 return mState.arrayBuffer.name(); 865} 866 867void Context::setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled) 868{ 869 mState.vertexAttribute[attribNum].mArrayEnabled = enabled; 870} 871 872const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum) 873{ 874 return mState.vertexAttribute[attribNum]; 875} 876 877void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized, 878 GLsizei stride, const void *pointer) 879{ 880 mState.vertexAttribute[attribNum].mBoundBuffer = boundBuffer; 881 mState.vertexAttribute[attribNum].mSize = size; 882 mState.vertexAttribute[attribNum].mType = type; 883 mState.vertexAttribute[attribNum].mNormalized = normalized; 884 mState.vertexAttribute[attribNum].mStride = stride; 885 mState.vertexAttribute[attribNum].mPointer = pointer; 886} 887 888const void *Context::getVertexAttribPointer(unsigned int attribNum) const 889{ 890 return mState.vertexAttribute[attribNum].mPointer; 891} 892 893const VertexAttributeArray &Context::getVertexAttributes() 894{ 895 return mState.vertexAttribute; 896} 897 898void Context::setPackAlignment(GLint alignment) 899{ 900 mState.packAlignment = alignment; 901} 902 903GLint Context::getPackAlignment() const 904{ 905 return mState.packAlignment; 906} 907 908void Context::setUnpackAlignment(GLint alignment) 909{ 910 mState.unpackAlignment = alignment; 911} 912 913GLint Context::getUnpackAlignment() const 914{ 915 return mState.unpackAlignment; 916} 917 918GLuint Context::createBuffer() 919{ 920 return mResourceManager->createBuffer(); 921} 922 923GLuint Context::createTexture() 924{ 925 return mResourceManager->createTexture(); 926} 927 928GLuint Context::createRenderbuffer() 929{ 930 return mResourceManager->createRenderbuffer(); 931} 932 933// Returns an unused framebuffer name 934GLuint Context::createFramebuffer() 935{ 936 GLuint handle = mFramebufferNameSpace.allocate(); 937 938 mFramebufferMap[handle] = nullptr; 939 940 return handle; 941} 942 943void Context::deleteBuffer(GLuint buffer) 944{ 945 if(mResourceManager->getBuffer(buffer)) 946 { 947 detachBuffer(buffer); 948 } 949 950 mResourceManager->deleteBuffer(buffer); 951} 952 953void Context::deleteTexture(GLuint texture) 954{ 955 if(mResourceManager->getTexture(texture)) 956 { 957 detachTexture(texture); 958 } 959 960 mResourceManager->deleteTexture(texture); 961} 962 963void Context::deleteRenderbuffer(GLuint renderbuffer) 964{ 965 if(mResourceManager->getRenderbuffer(renderbuffer)) 966 { 967 detachRenderbuffer(renderbuffer); 968 } 969 970 mResourceManager->deleteRenderbuffer(renderbuffer); 971} 972 973void Context::deleteFramebuffer(GLuint framebuffer) 974{ 975 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer); 976 977 if(framebufferObject != mFramebufferMap.end()) 978 { 979 detachFramebuffer(framebuffer); 980 981 delete framebufferObject->second; 982 mFramebufferNameSpace.remove(framebufferObject->first); 983 mFramebufferMap.erase(framebufferObject); 984 } 985} 986 987Buffer *Context::getBuffer(GLuint handle) 988{ 989 return mResourceManager->getBuffer(handle); 990} 991 992Texture *Context::getTexture(GLuint handle) 993{ 994 return mResourceManager->getTexture(handle); 995} 996 997Renderbuffer *Context::getRenderbuffer(GLuint handle) 998{ 999 return mResourceManager->getRenderbuffer(handle); 1000} 1001 1002Framebuffer *Context::getFramebuffer() 1003{ 1004 return getFramebuffer(mState.framebuffer); 1005} 1006 1007void Context::bindArrayBuffer(unsigned int buffer) 1008{ 1009 mResourceManager->checkBufferAllocation(buffer); 1010 1011 mState.arrayBuffer = getBuffer(buffer); 1012} 1013 1014void Context::bindElementArrayBuffer(unsigned int buffer) 1015{ 1016 mResourceManager->checkBufferAllocation(buffer); 1017 1018 mState.elementArrayBuffer = getBuffer(buffer); 1019} 1020 1021void Context::bindTexture2D(GLuint texture) 1022{ 1023 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D); 1024 1025 mState.samplerTexture[TEXTURE_2D][mState.activeSampler] = getTexture(texture); 1026} 1027 1028void Context::bindTextureExternal(GLuint texture) 1029{ 1030 mResourceManager->checkTextureAllocation(texture, TEXTURE_EXTERNAL); 1031 1032 mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler] = getTexture(texture); 1033} 1034 1035void Context::bindFramebuffer(GLuint framebuffer) 1036{ 1037 if(!getFramebuffer(framebuffer)) 1038 { 1039 mFramebufferMap[framebuffer] = new Framebuffer(); 1040 } 1041 1042 mState.framebuffer = framebuffer; 1043} 1044 1045void Context::bindRenderbuffer(GLuint renderbuffer) 1046{ 1047 mResourceManager->checkRenderbufferAllocation(renderbuffer); 1048 1049 mState.renderbuffer = getRenderbuffer(renderbuffer); 1050} 1051 1052void Context::setFramebufferZero(Framebuffer *buffer) 1053{ 1054 delete mFramebufferMap[0]; 1055 mFramebufferMap[0] = buffer; 1056} 1057 1058void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer) 1059{ 1060 Renderbuffer *renderbufferObject = mState.renderbuffer; 1061 renderbufferObject->setStorage(renderbuffer); 1062} 1063 1064Framebuffer *Context::getFramebuffer(unsigned int handle) 1065{ 1066 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle); 1067 1068 if(framebuffer == mFramebufferMap.end()) 1069 { 1070 return nullptr; 1071 } 1072 else 1073 { 1074 return framebuffer->second; 1075 } 1076} 1077 1078Buffer *Context::getArrayBuffer() 1079{ 1080 return mState.arrayBuffer; 1081} 1082 1083Buffer *Context::getElementArrayBuffer() 1084{ 1085 return mState.elementArrayBuffer; 1086} 1087 1088Texture2D *Context::getTexture2D() 1089{ 1090 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D)); 1091} 1092 1093TextureExternal *Context::getTextureExternal() 1094{ 1095 return static_cast<TextureExternal*>(getSamplerTexture(mState.activeSampler, TEXTURE_EXTERNAL)); 1096} 1097 1098Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type) 1099{ 1100 GLuint texid = mState.samplerTexture[type][sampler].name(); 1101 1102 if(texid == 0) // Special case: 0 refers to different initial textures based on the target 1103 { 1104 switch(type) 1105 { 1106 case TEXTURE_2D: return mTexture2DZero; 1107 case TEXTURE_EXTERNAL: return mTextureExternalZero; 1108 default: UNREACHABLE(type); 1109 } 1110 } 1111 1112 return mState.samplerTexture[type][sampler]; 1113} 1114 1115bool Context::getBooleanv(GLenum pname, GLboolean *params) 1116{ 1117 switch(pname) 1118 { 1119 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break; 1120 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break; 1121 case GL_COLOR_WRITEMASK: 1122 params[0] = mState.colorMaskRed; 1123 params[1] = mState.colorMaskGreen; 1124 params[2] = mState.colorMaskBlue; 1125 params[3] = mState.colorMaskAlpha; 1126 break; 1127 case GL_CULL_FACE: *params = mState.cullFaceEnabled; break; 1128 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFillEnabled; break; 1129 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverageEnabled; break; 1130 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverageEnabled; break; 1131 case GL_SCISSOR_TEST: *params = mState.scissorTestEnabled; break; 1132 case GL_STENCIL_TEST: *params = mState.stencilTestEnabled; break; 1133 case GL_DEPTH_TEST: *params = mState.depthTestEnabled; break; 1134 case GL_BLEND: *params = mState.blendEnabled; break; 1135 case GL_DITHER: *params = mState.ditherEnabled; break; 1136 case GL_LIGHT_MODEL_TWO_SIDE: *params = lightModelTwoSide; break; 1137 default: 1138 return false; 1139 } 1140 1141 return true; 1142} 1143 1144bool Context::getFloatv(GLenum pname, GLfloat *params) 1145{ 1146 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation 1147 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names 1148 // GetIntegerv as its native query function. As it would require conversion in any 1149 // case, this should make no difference to the calling application. 1150 switch(pname) 1151 { 1152 case GL_LINE_WIDTH: *params = mState.lineWidth; break; 1153 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break; 1154 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break; 1155 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break; 1156 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break; 1157 case GL_ALIASED_LINE_WIDTH_RANGE: 1158 params[0] = ALIASED_LINE_WIDTH_RANGE_MIN; 1159 params[1] = ALIASED_LINE_WIDTH_RANGE_MAX; 1160 break; 1161 case GL_ALIASED_POINT_SIZE_RANGE: 1162 params[0] = ALIASED_POINT_SIZE_RANGE_MIN; 1163 params[1] = ALIASED_POINT_SIZE_RANGE_MAX; 1164 break; 1165 case GL_SMOOTH_LINE_WIDTH_RANGE: 1166 params[0] = SMOOTH_LINE_WIDTH_RANGE_MIN; 1167 params[1] = SMOOTH_LINE_WIDTH_RANGE_MAX; 1168 break; 1169 case GL_SMOOTH_POINT_SIZE_RANGE: 1170 params[0] = SMOOTH_POINT_SIZE_RANGE_MIN; 1171 params[1] = SMOOTH_POINT_SIZE_RANGE_MAX; 1172 break; 1173 case GL_DEPTH_RANGE: 1174 params[0] = mState.zNear; 1175 params[1] = mState.zFar; 1176 break; 1177 case GL_COLOR_CLEAR_VALUE: 1178 params[0] = mState.colorClearValue.red; 1179 params[1] = mState.colorClearValue.green; 1180 params[2] = mState.colorClearValue.blue; 1181 params[3] = mState.colorClearValue.alpha; 1182 break; 1183 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: 1184 *params = MAX_TEXTURE_MAX_ANISOTROPY; 1185 break; 1186 case GL_MODELVIEW_MATRIX: 1187 for(int i = 0; i < 16; i++) 1188 { 1189 params[i] = modelViewStack.current()[i % 4][i / 4]; 1190 } 1191 break; 1192 case GL_PROJECTION_MATRIX: 1193 for(int i = 0; i < 16; i++) 1194 { 1195 params[i] = projectionStack.current()[i % 4][i / 4]; 1196 } 1197 break; 1198 default: 1199 return false; 1200 } 1201 1202 return true; 1203} 1204 1205bool Context::getIntegerv(GLenum pname, GLint *params) 1206{ 1207 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation 1208 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names 1209 // GetIntegerv as its native query function. As it would require conversion in any 1210 // case, this should make no difference to the calling application. You may find it in 1211 // Context::getFloatv. 1212 switch(pname) 1213 { 1214 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.name(); break; 1215 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.name(); break; 1216 case GL_FRAMEBUFFER_BINDING_OES: *params = mState.framebuffer; break; 1217 case GL_RENDERBUFFER_BINDING_OES: *params = mState.renderbuffer.name(); break; 1218 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break; 1219 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break; 1220 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break; 1221 case GL_PERSPECTIVE_CORRECTION_HINT: *params = mState.perspectiveCorrectionHint; break; 1222 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break; 1223 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break; 1224 case GL_STENCIL_REF: *params = mState.stencilRef; break; 1225 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break; 1226 case GL_STENCIL_FAIL: *params = mState.stencilFail; break; 1227 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break; 1228 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break; 1229 case GL_DEPTH_FUNC: *params = mState.depthFunc; break; 1230 case GL_BLEND_SRC_RGB_OES: *params = mState.sourceBlendRGB; break; 1231 case GL_BLEND_SRC_ALPHA_OES: *params = mState.sourceBlendAlpha; break; 1232 case GL_BLEND_DST_RGB_OES: *params = mState.destBlendRGB; break; 1233 case GL_BLEND_DST_ALPHA_OES: *params = mState.destBlendAlpha; break; 1234 case GL_BLEND_EQUATION_RGB_OES: *params = mState.blendEquationRGB; break; 1235 case GL_BLEND_EQUATION_ALPHA_OES: *params = mState.blendEquationAlpha; break; 1236 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break; 1237 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break; 1238 case GL_SUBPIXEL_BITS: *params = 4; break; 1239 case GL_MAX_TEXTURE_SIZE: *params = IMPLEMENTATION_MAX_TEXTURE_SIZE; break; 1240 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = NUM_COMPRESSED_TEXTURE_FORMATS; break; 1241 case GL_SAMPLE_BUFFERS: 1242 case GL_SAMPLES: 1243 { 1244 Framebuffer *framebuffer = getFramebuffer(); 1245 int width, height, samples; 1246 1247 if(framebuffer->completeness(width, height, samples) == GL_FRAMEBUFFER_COMPLETE_OES) 1248 { 1249 switch(pname) 1250 { 1251 case GL_SAMPLE_BUFFERS: 1252 if(samples > 1) 1253 { 1254 *params = 1; 1255 } 1256 else 1257 { 1258 *params = 0; 1259 } 1260 break; 1261 case GL_SAMPLES: 1262 *params = samples; 1263 break; 1264 } 1265 } 1266 else 1267 { 1268 *params = 0; 1269 } 1270 } 1271 break; 1272 case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES: 1273 { 1274 Framebuffer *framebuffer = getFramebuffer(); 1275 *params = framebuffer->getImplementationColorReadType(); 1276 } 1277 break; 1278 case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES: 1279 { 1280 Framebuffer *framebuffer = getFramebuffer(); 1281 *params = framebuffer->getImplementationColorReadFormat(); 1282 } 1283 break; 1284 case GL_MAX_VIEWPORT_DIMS: 1285 { 1286 int maxDimension = IMPLEMENTATION_MAX_RENDERBUFFER_SIZE; 1287 params[0] = maxDimension; 1288 params[1] = maxDimension; 1289 } 1290 break; 1291 case GL_COMPRESSED_TEXTURE_FORMATS: 1292 { 1293 for(int i = 0; i < NUM_COMPRESSED_TEXTURE_FORMATS; i++) 1294 { 1295 params[i] = compressedTextureFormats[i]; 1296 } 1297 } 1298 break; 1299 case GL_VIEWPORT: 1300 params[0] = mState.viewportX; 1301 params[1] = mState.viewportY; 1302 params[2] = mState.viewportWidth; 1303 params[3] = mState.viewportHeight; 1304 break; 1305 case GL_SCISSOR_BOX: 1306 params[0] = mState.scissorX; 1307 params[1] = mState.scissorY; 1308 params[2] = mState.scissorWidth; 1309 params[3] = mState.scissorHeight; 1310 break; 1311 case GL_CULL_FACE_MODE: *params = mState.cullMode; break; 1312 case GL_FRONT_FACE: *params = mState.frontFace; break; 1313 case GL_RED_BITS: 1314 case GL_GREEN_BITS: 1315 case GL_BLUE_BITS: 1316 case GL_ALPHA_BITS: 1317 { 1318 Framebuffer *framebuffer = getFramebuffer(); 1319 Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); 1320 1321 if(colorbuffer) 1322 { 1323 switch(pname) 1324 { 1325 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break; 1326 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break; 1327 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break; 1328 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break; 1329 } 1330 } 1331 else 1332 { 1333 *params = 0; 1334 } 1335 } 1336 break; 1337 case GL_DEPTH_BITS: 1338 { 1339 Framebuffer *framebuffer = getFramebuffer(); 1340 Renderbuffer *depthbuffer = framebuffer->getDepthbuffer(); 1341 1342 if(depthbuffer) 1343 { 1344 *params = depthbuffer->getDepthSize(); 1345 } 1346 else 1347 { 1348 *params = 0; 1349 } 1350 } 1351 break; 1352 case GL_STENCIL_BITS: 1353 { 1354 Framebuffer *framebuffer = getFramebuffer(); 1355 Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer(); 1356 1357 if(stencilbuffer) 1358 { 1359 *params = stencilbuffer->getStencilSize(); 1360 } 1361 else 1362 { 1363 *params = 0; 1364 } 1365 } 1366 break; 1367 case GL_TEXTURE_BINDING_2D: *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].name(); break; 1368 case GL_TEXTURE_BINDING_CUBE_MAP_OES: *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].name(); break; 1369 case GL_TEXTURE_BINDING_EXTERNAL_OES: *params = mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler].name(); break; 1370 case GL_MAX_LIGHTS: *params = MAX_LIGHTS; break; 1371 case GL_MAX_MODELVIEW_STACK_DEPTH: *params = MAX_MODELVIEW_STACK_DEPTH; break; 1372 case GL_MAX_PROJECTION_STACK_DEPTH: *params = MAX_PROJECTION_STACK_DEPTH; break; 1373 case GL_MAX_TEXTURE_STACK_DEPTH: *params = MAX_TEXTURE_STACK_DEPTH; break; 1374 case GL_MAX_TEXTURE_UNITS: *params = MAX_TEXTURE_UNITS; break; 1375 case GL_MAX_CLIP_PLANES: *params = MAX_CLIP_PLANES; break; 1376 case GL_POINT_SIZE_ARRAY_TYPE_OES: *params = mState.vertexAttribute[sw::PointSize].mType; break; 1377 case GL_POINT_SIZE_ARRAY_STRIDE_OES: *params = mState.vertexAttribute[sw::PointSize].mStride; break; 1378 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: *params = mState.vertexAttribute[sw::PointSize].mBoundBuffer.name(); break; 1379 case GL_VERTEX_ARRAY_SIZE: *params = mState.vertexAttribute[sw::Position].mSize; break; 1380 case GL_VERTEX_ARRAY_TYPE: *params = mState.vertexAttribute[sw::Position].mType; break; 1381 case GL_VERTEX_ARRAY_STRIDE: *params = mState.vertexAttribute[sw::Position].mStride; break; 1382 case GL_VERTEX_ARRAY_BUFFER_BINDING: *params = mState.vertexAttribute[sw::Position].mBoundBuffer.name(); break; 1383 case GL_NORMAL_ARRAY_TYPE: *params = mState.vertexAttribute[sw::Normal].mType; break; 1384 case GL_NORMAL_ARRAY_STRIDE: *params = mState.vertexAttribute[sw::Normal].mStride; break; 1385 case GL_NORMAL_ARRAY_BUFFER_BINDING: *params = mState.vertexAttribute[sw::Normal].mBoundBuffer.name(); break; 1386 case GL_COLOR_ARRAY_SIZE: *params = mState.vertexAttribute[sw::Color0].mSize; break; 1387 case GL_COLOR_ARRAY_TYPE: *params = mState.vertexAttribute[sw::Color0].mType; break; 1388 case GL_COLOR_ARRAY_STRIDE: *params = mState.vertexAttribute[sw::Color0].mStride; break; 1389 case GL_COLOR_ARRAY_BUFFER_BINDING: *params = mState.vertexAttribute[sw::Color0].mBoundBuffer.name(); break; 1390 case GL_TEXTURE_COORD_ARRAY_SIZE: *params = mState.vertexAttribute[sw::TexCoord0 + mState.activeSampler].mSize; break; 1391 case GL_TEXTURE_COORD_ARRAY_TYPE: *params = mState.vertexAttribute[sw::TexCoord0 + mState.activeSampler].mType; break; 1392 case GL_TEXTURE_COORD_ARRAY_STRIDE: *params = mState.vertexAttribute[sw::TexCoord0 + mState.activeSampler].mStride; break; 1393 case GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING: *params = mState.vertexAttribute[sw::TexCoord0 + mState.activeSampler].mBoundBuffer.name(); break; 1394 default: 1395 return false; 1396 } 1397 1398 return true; 1399} 1400 1401bool Context::getPointerv(GLenum pname, const GLvoid **params) 1402{ 1403 switch(pname) 1404 { 1405 case GL_VERTEX_ARRAY_POINTER: *params = mState.vertexAttribute[sw::Position].mPointer; break; 1406 case GL_NORMAL_ARRAY_POINTER: *params = mState.vertexAttribute[sw::Normal].mPointer; break; 1407 case GL_COLOR_ARRAY_POINTER: *params = mState.vertexAttribute[sw::Color0].mPointer; break; 1408 case GL_POINT_SIZE_ARRAY_POINTER_OES: *params = mState.vertexAttribute[sw::PointSize].mPointer; break; 1409 case GL_TEXTURE_COORD_ARRAY_POINTER: *params = mState.vertexAttribute[sw::TexCoord0 + mState.activeSampler].mPointer; break; 1410 default: 1411 return false; 1412 } 1413 1414 return true; 1415} 1416 1417int Context::getQueryParameterNum(GLenum pname) 1418{ 1419 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation 1420 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due 1421 // to the fact that it is stored internally as a float, and so would require conversion 1422 // if returned from Context::getIntegerv. Since this conversion is already implemented 1423 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we 1424 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling 1425 // application. 1426 switch(pname) 1427 { 1428 case GL_COMPRESSED_TEXTURE_FORMATS: 1429 return NUM_COMPRESSED_TEXTURE_FORMATS; 1430 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: 1431 case GL_ARRAY_BUFFER_BINDING: 1432 case GL_FRAMEBUFFER_BINDING_OES: 1433 case GL_RENDERBUFFER_BINDING_OES: 1434 case GL_PACK_ALIGNMENT: 1435 case GL_UNPACK_ALIGNMENT: 1436 case GL_GENERATE_MIPMAP_HINT: 1437 case GL_RED_BITS: 1438 case GL_GREEN_BITS: 1439 case GL_BLUE_BITS: 1440 case GL_ALPHA_BITS: 1441 case GL_DEPTH_BITS: 1442 case GL_STENCIL_BITS: 1443 case GL_ELEMENT_ARRAY_BUFFER_BINDING: 1444 case GL_CULL_FACE_MODE: 1445 case GL_FRONT_FACE: 1446 case GL_ACTIVE_TEXTURE: 1447 case GL_STENCIL_FUNC: 1448 case GL_STENCIL_VALUE_MASK: 1449 case GL_STENCIL_REF: 1450 case GL_STENCIL_FAIL: 1451 case GL_STENCIL_PASS_DEPTH_FAIL: 1452 case GL_STENCIL_PASS_DEPTH_PASS: 1453 case GL_DEPTH_FUNC: 1454 case GL_BLEND_SRC_RGB_OES: 1455 case GL_BLEND_SRC_ALPHA_OES: 1456 case GL_BLEND_DST_RGB_OES: 1457 case GL_BLEND_DST_ALPHA_OES: 1458 case GL_BLEND_EQUATION_RGB_OES: 1459 case GL_BLEND_EQUATION_ALPHA_OES: 1460 case GL_STENCIL_WRITEMASK: 1461 case GL_STENCIL_CLEAR_VALUE: 1462 case GL_SUBPIXEL_BITS: 1463 case GL_MAX_TEXTURE_SIZE: 1464 case GL_MAX_CUBE_MAP_TEXTURE_SIZE_OES: 1465 case GL_SAMPLE_BUFFERS: 1466 case GL_SAMPLES: 1467 case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES: 1468 case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES: 1469 case GL_TEXTURE_BINDING_2D: 1470 case GL_TEXTURE_BINDING_CUBE_MAP_OES: 1471 case GL_TEXTURE_BINDING_EXTERNAL_OES: 1472 return 1; 1473 case GL_MAX_VIEWPORT_DIMS: 1474 return 2; 1475 case GL_VIEWPORT: 1476 case GL_SCISSOR_BOX: 1477 return 4; 1478 case GL_SAMPLE_COVERAGE_INVERT: 1479 case GL_DEPTH_WRITEMASK: 1480 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled, 1481 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries. 1482 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural 1483 case GL_SAMPLE_COVERAGE: 1484 case GL_SCISSOR_TEST: 1485 case GL_STENCIL_TEST: 1486 case GL_DEPTH_TEST: 1487 case GL_BLEND: 1488 case GL_DITHER: 1489 return 1; 1490 case GL_COLOR_WRITEMASK: 1491 return 4; 1492 case GL_POLYGON_OFFSET_FACTOR: 1493 case GL_POLYGON_OFFSET_UNITS: 1494 case GL_SAMPLE_COVERAGE_VALUE: 1495 case GL_DEPTH_CLEAR_VALUE: 1496 case GL_LINE_WIDTH: 1497 return 1; 1498 case GL_ALIASED_LINE_WIDTH_RANGE: 1499 case GL_ALIASED_POINT_SIZE_RANGE: 1500 case GL_DEPTH_RANGE: 1501 return 2; 1502 case GL_COLOR_CLEAR_VALUE: 1503 return 4; 1504 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: 1505 case GL_MAX_LIGHTS: 1506 case GL_MAX_MODELVIEW_STACK_DEPTH: 1507 case GL_MAX_PROJECTION_STACK_DEPTH: 1508 case GL_MAX_TEXTURE_STACK_DEPTH: 1509 case GL_MAX_TEXTURE_UNITS: 1510 case GL_MAX_CLIP_PLANES: 1511 case GL_POINT_SIZE_ARRAY_TYPE_OES: 1512 case GL_POINT_SIZE_ARRAY_STRIDE_OES: 1513 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: 1514 return 1; 1515 case GL_CURRENT_COLOR: 1516 return 4; 1517 case GL_CURRENT_NORMAL: 1518 return 3; 1519 case GL_CURRENT_TEXTURE_COORDS: 1520 return 4; 1521 case GL_POINT_SIZE: 1522 case GL_POINT_SIZE_MIN: 1523 case GL_POINT_SIZE_MAX: 1524 case GL_POINT_FADE_THRESHOLD_SIZE: 1525 return 1; 1526 case GL_POINT_DISTANCE_ATTENUATION: 1527 return 3; 1528 case GL_SMOOTH_POINT_SIZE_RANGE: 1529 case GL_SMOOTH_LINE_WIDTH_RANGE: 1530 return 2; 1531 case GL_SHADE_MODEL: 1532 case GL_MATRIX_MODE: 1533 case GL_MODELVIEW_STACK_DEPTH: 1534 case GL_PROJECTION_STACK_DEPTH: 1535 case GL_TEXTURE_STACK_DEPTH: 1536 return 1; 1537 case GL_MODELVIEW_MATRIX: 1538 case GL_PROJECTION_MATRIX: 1539 case GL_TEXTURE_MATRIX: 1540 return 16; 1541 case GL_ALPHA_TEST_FUNC: 1542 case GL_ALPHA_TEST_REF: 1543 case GL_BLEND_DST: 1544 case GL_BLEND_SRC: 1545 case GL_LOGIC_OP_MODE: 1546 case GL_VERTEX_ARRAY_SIZE: 1547 case GL_VERTEX_ARRAY_TYPE: 1548 case GL_VERTEX_ARRAY_STRIDE: 1549 case GL_NORMAL_ARRAY_TYPE: 1550 case GL_NORMAL_ARRAY_STRIDE: 1551 case GL_COLOR_ARRAY_SIZE: 1552 case GL_COLOR_ARRAY_TYPE: 1553 case GL_COLOR_ARRAY_STRIDE: 1554 case GL_TEXTURE_COORD_ARRAY_SIZE: 1555 case GL_TEXTURE_COORD_ARRAY_TYPE: 1556 case GL_TEXTURE_COORD_ARRAY_STRIDE: 1557 case GL_VERTEX_ARRAY_POINTER: 1558 case GL_NORMAL_ARRAY_POINTER: 1559 case GL_COLOR_ARRAY_POINTER: 1560 case GL_TEXTURE_COORD_ARRAY_POINTER: 1561 case GL_LIGHT_MODEL_TWO_SIDE: 1562 return 1; 1563 default: 1564 UNREACHABLE(pname); 1565 } 1566 1567 return -1; 1568} 1569 1570bool Context::isQueryParameterInt(GLenum pname) 1571{ 1572 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation 1573 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due 1574 // to the fact that it is stored internally as a float, and so would require conversion 1575 // if returned from Context::getIntegerv. Since this conversion is already implemented 1576 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we 1577 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling 1578 // application. 1579 switch(pname) 1580 { 1581 case GL_COMPRESSED_TEXTURE_FORMATS: 1582 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: 1583 case GL_ARRAY_BUFFER_BINDING: 1584 case GL_FRAMEBUFFER_BINDING_OES: 1585 case GL_RENDERBUFFER_BINDING_OES: 1586 case GL_PACK_ALIGNMENT: 1587 case GL_UNPACK_ALIGNMENT: 1588 case GL_GENERATE_MIPMAP_HINT: 1589 case GL_RED_BITS: 1590 case GL_GREEN_BITS: 1591 case GL_BLUE_BITS: 1592 case GL_ALPHA_BITS: 1593 case GL_DEPTH_BITS: 1594 case GL_STENCIL_BITS: 1595 case GL_ELEMENT_ARRAY_BUFFER_BINDING: 1596 case GL_CULL_FACE_MODE: 1597 case GL_FRONT_FACE: 1598 case GL_ACTIVE_TEXTURE: 1599 case GL_STENCIL_FUNC: 1600 case GL_STENCIL_VALUE_MASK: 1601 case GL_STENCIL_REF: 1602 case GL_STENCIL_FAIL: 1603 case GL_STENCIL_PASS_DEPTH_FAIL: 1604 case GL_STENCIL_PASS_DEPTH_PASS: 1605 case GL_DEPTH_FUNC: 1606 case GL_BLEND_SRC_RGB_OES: 1607 case GL_BLEND_SRC_ALPHA_OES: 1608 case GL_BLEND_DST_RGB_OES: 1609 case GL_BLEND_DST_ALPHA_OES: 1610 case GL_BLEND_EQUATION_RGB_OES: 1611 case GL_BLEND_EQUATION_ALPHA_OES: 1612 case GL_STENCIL_WRITEMASK: 1613 case GL_STENCIL_CLEAR_VALUE: 1614 case GL_SUBPIXEL_BITS: 1615 case GL_MAX_TEXTURE_SIZE: 1616 case GL_MAX_CUBE_MAP_TEXTURE_SIZE_OES: 1617 case GL_SAMPLE_BUFFERS: 1618 case GL_SAMPLES: 1619 case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES: 1620 case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES: 1621 case GL_TEXTURE_BINDING_2D: 1622 case GL_TEXTURE_BINDING_CUBE_MAP_OES: 1623 case GL_TEXTURE_BINDING_EXTERNAL_OES: 1624 case GL_MAX_VIEWPORT_DIMS: 1625 case GL_VIEWPORT: 1626 case GL_SCISSOR_BOX: 1627 case GL_MAX_LIGHTS: 1628 case GL_MAX_MODELVIEW_STACK_DEPTH: 1629 case GL_MAX_PROJECTION_STACK_DEPTH: 1630 case GL_MAX_TEXTURE_STACK_DEPTH: 1631 case GL_MAX_TEXTURE_UNITS: 1632 case GL_MAX_CLIP_PLANES: 1633 case GL_POINT_SIZE_ARRAY_TYPE_OES: 1634 case GL_POINT_SIZE_ARRAY_STRIDE_OES: 1635 case GL_POINT_SIZE_ARRAY_BUFFER_BINDING_OES: 1636 return true; 1637 } 1638 1639 return false; 1640} 1641 1642bool Context::isQueryParameterFloat(GLenum pname) 1643{ 1644 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation 1645 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due 1646 // to the fact that it is stored internally as a float, and so would require conversion 1647 // if returned from Context::getIntegerv. Since this conversion is already implemented 1648 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we 1649 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling 1650 // application. 1651 switch(pname) 1652 { 1653 case GL_POLYGON_OFFSET_FACTOR: 1654 case GL_POLYGON_OFFSET_UNITS: 1655 case GL_SAMPLE_COVERAGE_VALUE: 1656 case GL_DEPTH_CLEAR_VALUE: 1657 case GL_LINE_WIDTH: 1658 case GL_ALIASED_LINE_WIDTH_RANGE: 1659 case GL_ALIASED_POINT_SIZE_RANGE: 1660 case GL_SMOOTH_LINE_WIDTH_RANGE: 1661 case GL_SMOOTH_POINT_SIZE_RANGE: 1662 case GL_DEPTH_RANGE: 1663 case GL_COLOR_CLEAR_VALUE: 1664 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: 1665 case GL_LIGHT_MODEL_AMBIENT: 1666 case GL_POINT_SIZE_MIN: 1667 case GL_POINT_SIZE_MAX: 1668 case GL_POINT_DISTANCE_ATTENUATION: 1669 case GL_POINT_FADE_THRESHOLD_SIZE: 1670 return true; 1671 } 1672 1673 return false; 1674} 1675 1676bool Context::isQueryParameterBool(GLenum pname) 1677{ 1678 switch(pname) 1679 { 1680 case GL_SAMPLE_COVERAGE_INVERT: 1681 case GL_DEPTH_WRITEMASK: 1682 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled, 1683 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries. 1684 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural 1685 case GL_SAMPLE_COVERAGE: 1686 case GL_SCISSOR_TEST: 1687 case GL_STENCIL_TEST: 1688 case GL_DEPTH_TEST: 1689 case GL_BLEND: 1690 case GL_DITHER: 1691 case GL_COLOR_WRITEMASK: 1692 case GL_LIGHT_MODEL_TWO_SIDE: 1693 return true; 1694 } 1695 1696 return false; 1697} 1698 1699bool Context::isQueryParameterPointer(GLenum pname) 1700{ 1701 switch(pname) 1702 { 1703 case GL_VERTEX_ARRAY_POINTER: 1704 case GL_NORMAL_ARRAY_POINTER: 1705 case GL_COLOR_ARRAY_POINTER: 1706 case GL_TEXTURE_COORD_ARRAY_POINTER: 1707 case GL_POINT_SIZE_ARRAY_POINTER_OES: 1708 return true; 1709 } 1710 1711 return false; 1712} 1713 1714// Applies the render target surface, depth stencil surface, viewport rectangle and scissor rectangle 1715bool Context::applyRenderTarget() 1716{ 1717 Framebuffer *framebuffer = getFramebuffer(); 1718 int width, height, samples; 1719 1720 if(!framebuffer || framebuffer->completeness(width, height, samples) != GL_FRAMEBUFFER_COMPLETE_OES) 1721 { 1722 return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES, false); 1723 } 1724 1725 egl::Image *renderTarget = framebuffer->getRenderTarget(); 1726 device->setRenderTarget(0, renderTarget); 1727 if(renderTarget) renderTarget->release(); 1728 1729 egl::Image *depthBuffer = framebuffer->getDepthBuffer(); 1730 device->setDepthBuffer(depthBuffer); 1731 if(depthBuffer) depthBuffer->release(); 1732 1733 egl::Image *stencilBuffer = framebuffer->getStencilBuffer(); 1734 device->setStencilBuffer(stencilBuffer); 1735 if(stencilBuffer) stencilBuffer->release(); 1736 1737 Viewport viewport; 1738 float zNear = clamp01(mState.zNear); 1739 float zFar = clamp01(mState.zFar); 1740 1741 viewport.x0 = mState.viewportX; 1742 viewport.y0 = mState.viewportY; 1743 viewport.width = mState.viewportWidth; 1744 viewport.height = mState.viewportHeight; 1745 viewport.minZ = zNear; 1746 viewport.maxZ = zFar; 1747 1748 device->setViewport(viewport); 1749 1750 if(mState.scissorTestEnabled) 1751 { 1752 sw::Rect scissor = {mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight}; 1753 scissor.clip(0, 0, width, height); 1754 1755 device->setScissorRect(scissor); 1756 device->setScissorEnable(true); 1757 } 1758 else 1759 { 1760 device->setScissorEnable(false); 1761 } 1762 1763 return true; 1764} 1765 1766// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) 1767void Context::applyState(GLenum drawMode) 1768{ 1769 Framebuffer *framebuffer = getFramebuffer(); 1770 1771 if(mState.cullFaceEnabled) 1772 { 1773 device->setCullMode(es2sw::ConvertCullMode(mState.cullMode, mState.frontFace)); 1774 } 1775 else 1776 { 1777 device->setCullMode(sw::CULL_NONE); 1778 } 1779 1780 if(mDepthStateDirty) 1781 { 1782 if(mState.depthTestEnabled) 1783 { 1784 device->setDepthBufferEnable(true); 1785 device->setDepthCompare(es2sw::ConvertDepthComparison(mState.depthFunc)); 1786 } 1787 else 1788 { 1789 device->setDepthBufferEnable(false); 1790 } 1791 1792 mDepthStateDirty = false; 1793 } 1794 1795 if(mBlendStateDirty) 1796 { 1797 if(mState.blendEnabled) 1798 { 1799 device->setAlphaBlendEnable(true); 1800 device->setSeparateAlphaBlendEnable(true); 1801 1802 device->setSourceBlendFactor(es2sw::ConvertBlendFunc(mState.sourceBlendRGB)); 1803 device->setDestBlendFactor(es2sw::ConvertBlendFunc(mState.destBlendRGB)); 1804 device->setBlendOperation(es2sw::ConvertBlendOp(mState.blendEquationRGB)); 1805 1806 device->setSourceBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.sourceBlendAlpha)); 1807 device->setDestBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.destBlendAlpha)); 1808 device->setBlendOperationAlpha(es2sw::ConvertBlendOp(mState.blendEquationAlpha)); 1809 } 1810 else 1811 { 1812 device->setAlphaBlendEnable(false); 1813 } 1814 1815 mBlendStateDirty = false; 1816 } 1817 1818 if(mStencilStateDirty || mFrontFaceDirty) 1819 { 1820 if(mState.stencilTestEnabled && framebuffer->hasStencil()) 1821 { 1822 device->setStencilEnable(true); 1823 device->setTwoSidedStencil(true); 1824 1825 // get the maximum size of the stencil ref 1826 Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer(); 1827 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1; 1828 1829 device->setStencilWriteMask(mState.stencilWritemask); 1830 device->setStencilCompare(es2sw::ConvertStencilComparison(mState.stencilFunc)); 1831 1832 device->setStencilReference((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil); 1833 device->setStencilMask(mState.stencilMask); 1834 1835 device->setStencilFailOperation(es2sw::ConvertStencilOp(mState.stencilFail)); 1836 device->setStencilZFailOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthFail)); 1837 device->setStencilPassOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthPass)); 1838 1839 device->setStencilWriteMaskCCW(mState.stencilWritemask); 1840 device->setStencilCompareCCW(es2sw::ConvertStencilComparison(mState.stencilFunc)); 1841 1842 device->setStencilReferenceCCW((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil); 1843 device->setStencilMaskCCW(mState.stencilMask); 1844 1845 device->setStencilFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilFail)); 1846 device->setStencilZFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthFail)); 1847 device->setStencilPassOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthPass)); 1848 } 1849 else 1850 { 1851 device->setStencilEnable(false); 1852 } 1853 1854 mStencilStateDirty = false; 1855 mFrontFaceDirty = false; 1856 } 1857 1858 if(mMaskStateDirty) 1859 { 1860 device->setColorWriteMask(0, es2sw::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha)); 1861 device->setDepthWriteEnable(mState.depthMask); 1862 1863 mMaskStateDirty = false; 1864 } 1865 1866 if(mPolygonOffsetStateDirty) 1867 { 1868 if(mState.polygonOffsetFillEnabled) 1869 { 1870 Renderbuffer *depthbuffer = framebuffer->getDepthbuffer(); 1871 if(depthbuffer) 1872 { 1873 device->setSlopeDepthBias(mState.polygonOffsetFactor); 1874 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize())); 1875 device->setDepthBias(depthBias); 1876 } 1877 } 1878 else 1879 { 1880 device->setSlopeDepthBias(0); 1881 device->setDepthBias(0); 1882 } 1883 1884 mPolygonOffsetStateDirty = false; 1885 } 1886 1887 if(mSampleStateDirty) 1888 { 1889 if(mState.sampleAlphaToCoverageEnabled) 1890 { 1891 device->setTransparencyAntialiasing(sw::TRANSPARENCY_ALPHA_TO_COVERAGE); 1892 } 1893 else 1894 { 1895 device->setTransparencyAntialiasing(sw::TRANSPARENCY_NONE); 1896 } 1897 1898 if(mState.sampleCoverageEnabled) 1899 { 1900 unsigned int mask = 0; 1901 if(mState.sampleCoverageValue != 0) 1902 { 1903 int width, height, samples; 1904 framebuffer->completeness(width, height, samples); 1905 1906 float threshold = 0.5f; 1907 1908 for(int i = 0; i < samples; i++) 1909 { 1910 mask <<= 1; 1911 1912 if((i + 1) * mState.sampleCoverageValue >= threshold) 1913 { 1914 threshold += 1.0f; 1915 mask |= 1; 1916 } 1917 } 1918 } 1919 1920 if(mState.sampleCoverageInvert) 1921 { 1922 mask = ~mask; 1923 } 1924 1925 device->setMultiSampleMask(mask); 1926 } 1927 else 1928 { 1929 device->setMultiSampleMask(0xFFFFFFFF); 1930 } 1931 1932 mSampleStateDirty = false; 1933 } 1934 1935 if(mDitherStateDirty) 1936 { 1937 // UNIMPLEMENTED(); // FIXME 1938 1939 mDitherStateDirty = false; 1940 } 1941 1942 switch(mState.shadeModel) 1943 { 1944 default: UNREACHABLE(mState.shadeModel); 1945 case GL_SMOOTH: device->setShadingMode(sw::SHADING_GOURAUD); break; 1946 case GL_FLAT: device->setShadingMode(sw::SHADING_FLAT); break; 1947 } 1948 1949 device->setLightingEnable(lightingEnabled); 1950 device->setGlobalAmbient(sw::Color<float>(globalAmbient.red, globalAmbient.green, globalAmbient.blue, globalAmbient.alpha)); 1951 1952 for(int i = 0; i < MAX_LIGHTS; i++) 1953 { 1954 device->setLightEnable(i, light[i].enabled); 1955 device->setLightAmbient(i, sw::Color<float>(light[i].ambient.red, light[i].ambient.green, light[i].ambient.blue, light[i].ambient.alpha)); 1956 device->setLightDiffuse(i, sw::Color<float>(light[i].diffuse.red, light[i].diffuse.green, light[i].diffuse.blue, light[i].diffuse.alpha)); 1957 device->setLightSpecular(i, sw::Color<float>(light[i].specular.red, light[i].specular.green, light[i].specular.blue, light[i].specular.alpha)); 1958 device->setLightAttenuation(i, light[i].attenuation.constant, light[i].attenuation.linear, light[i].attenuation.quadratic); 1959 1960 if(light[i].position.w != 0.0f) 1961 { 1962 device->setLightPosition(i, sw::Point(light[i].position.x / light[i].position.w, light[i].position.y / light[i].position.w, light[i].position.z / light[i].position.w)); 1963 } 1964 else // Directional light 1965 { 1966 // Hack: set the position far way 1967 float max = sw::max(abs(light[i].position.x), abs(light[i].position.y), abs(light[i].position.z)); 1968 device->setLightPosition(i, sw::Point(1e10f * (light[i].position.x / max), 1e10f * (light[i].position.y / max), 1e10f * (light[i].position.z / max))); 1969 } 1970 } 1971 1972 device->setMaterialAmbient(sw::Color<float>(materialAmbient.red, materialAmbient.green, materialAmbient.blue, materialAmbient.alpha)); 1973 device->setMaterialDiffuse(sw::Color<float>(materialDiffuse.red, materialDiffuse.green, materialDiffuse.blue, materialDiffuse.alpha)); 1974 device->setMaterialSpecular(sw::Color<float>(materialSpecular.red, materialSpecular.green, materialSpecular.blue, materialSpecular.alpha)); 1975 device->setMaterialEmission(sw::Color<float>(materialEmission.red, materialEmission.green, materialEmission.blue, materialEmission.alpha)); 1976 device->setMaterialShininess(materialShininess); 1977 1978 device->setDiffuseMaterialSource(sw::MATERIAL_MATERIAL); 1979 device->setSpecularMaterialSource(sw::MATERIAL_MATERIAL); 1980 device->setAmbientMaterialSource(sw::MATERIAL_MATERIAL); 1981 device->setEmissiveMaterialSource(sw::MATERIAL_MATERIAL); 1982 1983 const sw::Matrix Z(1, 0, 0, 0, 1984 0, 1, 0, 0, 1985 0, 0, 0.5, 0.5, 1986 0, 0, 0, 1); // Map depth range from [-1, 1] to [0, 1] 1987 1988 device->setProjectionMatrix(Z * projectionStack.current()); 1989 device->setModelMatrix(modelViewStack.current()); 1990 device->setTextureMatrix(0, textureStack0.current()); 1991 device->setTextureMatrix(1, textureStack1.current()); 1992 device->setTextureTransform(0, textureStack0.isIdentity() ? 0 : 4, false); 1993 device->setTextureTransform(1, textureStack1.isIdentity() ? 0 : 4, false); 1994 device->setTexGen(0, sw::TEXGEN_NONE); 1995 device->setTexGen(1, sw::TEXGEN_NONE); 1996 1997 device->setAlphaTestEnable(alphaTestEnabled); 1998 device->setAlphaCompare(es2sw::ConvertAlphaComparison(alphaTestFunc)); 1999 device->setAlphaReference(alphaTestRef * 0xFF); 2000 2001 device->setFogEnable(fogEnabled); 2002 device->setFogColor(sw::Color<float>(fogColor.red, fogColor.green, fogColor.blue, fogColor.alpha)); 2003 device->setFogDensity(fogDensity); 2004 device->setFogStart(fogStart); 2005 device->setFogEnd(fogEnd); 2006 2007 switch(fogMode) 2008 { 2009 case GL_LINEAR: device->setVertexFogMode(sw::FOG_LINEAR); break; 2010 case GL_EXP: device->setVertexFogMode(sw::FOG_EXP); break; 2011 case GL_EXP2: device->setVertexFogMode(sw::FOG_EXP2); break; 2012 default: UNREACHABLE(fogMode); 2013 } 2014 2015 device->setColorLogicOpEnabled(colorLogicOpEnabled); 2016 device->setLogicalOperation(es2sw::ConvertLogicalOperation(logicalOperation)); 2017 2018 device->setNormalizeNormals(normalizeEnabled || rescaleNormalEnabled); 2019} 2020 2021GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count) 2022{ 2023 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS]; 2024 2025 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes); 2026 if(err != GL_NO_ERROR) 2027 { 2028 return err; 2029 } 2030 2031 device->resetInputStreams(false); 2032 2033 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++) 2034 { 2035 sw::Resource *resource = attributes[i].vertexBuffer; 2036 const void *buffer = (char*)resource->data() + attributes[i].offset; 2037 2038 int stride = attributes[i].stride; 2039 2040 buffer = (char*)buffer + stride * base; 2041 2042 sw::Stream attribute(resource, buffer, stride); 2043 2044 attribute.type = attributes[i].type; 2045 attribute.count = attributes[i].count; 2046 attribute.normalized = attributes[i].normalized; 2047 2048 device->setInputStream(i, attribute); 2049 } 2050 2051 return GL_NO_ERROR; 2052} 2053 2054// Applies the indices and element array bindings 2055GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) 2056{ 2057 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer, indices, indexInfo); 2058 2059 if(err == GL_NO_ERROR) 2060 { 2061 device->setIndexBuffer(indexInfo->indexBuffer); 2062 } 2063 2064 return err; 2065} 2066 2067void Context::applyTextures() 2068{ 2069 for(int unit = 0; unit < MAX_TEXTURE_UNITS; unit++) 2070 { 2071 Texture *texture = nullptr; 2072 2073 if(textureExternalEnabled[unit]) 2074 { 2075 texture = getSamplerTexture(unit, TEXTURE_EXTERNAL); 2076 } 2077 else if(texture2Denabled[unit]) 2078 { 2079 texture = getSamplerTexture(unit, TEXTURE_2D); 2080 } 2081 2082 if(texture && texture->isSamplerComplete()) 2083 { 2084 texture->autoGenerateMipmaps(); 2085 2086 GLenum wrapS = texture->getWrapS(); 2087 GLenum wrapT = texture->getWrapT(); 2088 GLenum minFilter = texture->getMinFilter(); 2089 GLenum magFilter = texture->getMagFilter(); 2090 GLfloat maxAnisotropy = texture->getMaxAnisotropy(); 2091 2092 device->setAddressingModeU(sw::SAMPLER_PIXEL, unit, es2sw::ConvertTextureWrap(wrapS)); 2093 device->setAddressingModeV(sw::SAMPLER_PIXEL, unit, es2sw::ConvertTextureWrap(wrapT)); 2094 2095 device->setTextureFilter(sw::SAMPLER_PIXEL, unit, es2sw::ConvertTextureFilter(minFilter, magFilter, maxAnisotropy)); 2096 device->setMipmapFilter(sw::SAMPLER_PIXEL, unit, es2sw::ConvertMipMapFilter(minFilter)); 2097 device->setMaxAnisotropy(sw::SAMPLER_PIXEL, unit, maxAnisotropy); 2098 2099 applyTexture(unit, texture); 2100 2101 device->setConstantColor(unit, sw::Color<float>(mState.textureUnit[unit].color.red, mState.textureUnit[unit].color.green, mState.textureUnit[unit].color.blue, mState.textureUnit[unit].color.alpha)); 2102 2103 if(mState.textureUnit[unit].environmentMode != GL_COMBINE) 2104 { 2105 device->setFirstArgument(unit, sw::TextureStage::SOURCE_TEXTURE); // Cs 2106 device->setFirstModifier(unit, sw::TextureStage::MODIFIER_COLOR); 2107 device->setSecondArgument(unit, sw::TextureStage::SOURCE_CURRENT); // Cp 2108 device->setSecondModifier(unit, sw::TextureStage::MODIFIER_COLOR); 2109 device->setThirdArgument(unit, sw::TextureStage::SOURCE_CONSTANT); // Cc 2110 device->setThirdModifier(unit, sw::TextureStage::MODIFIER_COLOR); 2111 2112 device->setFirstArgumentAlpha(unit, sw::TextureStage::SOURCE_TEXTURE); // As 2113 device->setFirstModifierAlpha(unit, sw::TextureStage::MODIFIER_ALPHA); 2114 device->setSecondArgumentAlpha(unit, sw::TextureStage::SOURCE_CURRENT); // Ap 2115 device->setSecondModifierAlpha(unit, sw::TextureStage::MODIFIER_ALPHA); 2116 device->setThirdArgumentAlpha(unit, sw::TextureStage::SOURCE_CONSTANT); // Ac 2117 device->setThirdModifierAlpha(unit, sw::TextureStage::MODIFIER_ALPHA); 2118 2119 GLenum texFormat = texture->getFormat(GL_TEXTURE_2D, 0); 2120 2121 switch(mState.textureUnit[unit].environmentMode) 2122 { 2123 case GL_REPLACE: 2124 if(IsAlpha(texFormat)) // GL_ALPHA 2125 { 2126 // Cv = Cp, Av = As 2127 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2); 2128 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG1); 2129 } 2130 else if(IsRGB(texFormat)) // GL_LUMINANCE (or 1) / GL_RGB (or 3) 2131 { 2132 // Cv = Cs, Av = Ap 2133 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1); 2134 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2135 } 2136 else if(IsRGBA(texFormat)) // GL_LUMINANCE_ALPHA (or 2) / GL_RGBA (or 4) 2137 { 2138 // Cv = Cs, Av = As 2139 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1); 2140 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG1); 2141 } 2142 else UNREACHABLE(texFormat); 2143 break; 2144 case GL_MODULATE: 2145 if(IsAlpha(texFormat)) // GL_ALPHA 2146 { 2147 // Cv = Cp, Av = ApAs 2148 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2); 2149 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE); 2150 } 2151 else if(IsRGB(texFormat)) // GL_LUMINANCE (or 1) / GL_RGB (or 3) 2152 { 2153 // Cv = CpCs, Av = Ap 2154 device->setStageOperation(unit, sw::TextureStage::STAGE_MODULATE); 2155 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2156 } 2157 else if(IsRGBA(texFormat)) // GL_LUMINANCE_ALPHA (or 2) / GL_RGBA (or 4) 2158 { 2159 // Cv = CpCs, Av = ApAs 2160 device->setStageOperation(unit, sw::TextureStage::STAGE_MODULATE); 2161 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE); 2162 } 2163 else UNREACHABLE(texFormat); 2164 break; 2165 case GL_DECAL: 2166 if(texFormat == GL_ALPHA || 2167 texFormat == GL_LUMINANCE || 2168 texFormat == GL_LUMINANCE_ALPHA) 2169 { 2170 // undefined // FIXME: Log 2171 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2); 2172 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2173 } 2174 else if(IsRGB(texFormat)) // GL_LUMINANCE (or 1) / GL_RGB (or 3) 2175 { 2176 // Cv = Cs, Av = Ap 2177 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1); 2178 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2179 } 2180 else if(IsRGBA(texFormat)) // GL_LUMINANCE_ALPHA (or 2) / GL_RGBA (or 4) 2181 { 2182 // Cv = Cp(1 - As) + CsAs, Av = Ap 2183 device->setStageOperation(unit, sw::TextureStage::STAGE_BLENDTEXTUREALPHA); // Alpha * (Arg1 - Arg2) + Arg2 2184 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2185 } 2186 else UNREACHABLE(texFormat); 2187 break; 2188 case GL_BLEND: 2189 if(IsAlpha(texFormat)) // GL_ALPHA 2190 { 2191 // Cv = Cp, Av = ApAs 2192 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2); 2193 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE); 2194 } 2195 else if(IsRGB(texFormat)) // GL_LUMINANCE (or 1) / GL_RGB (or 3) 2196 { 2197 // Cv = Cp(1 - Cs) + CcCs, Av = Ap 2198 device->setStageOperation(unit, sw::TextureStage::STAGE_LERP); // Arg3 * (Arg1 - Arg2) + Arg2 2199 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2200 } 2201 else if(IsRGBA(texFormat)) // GL_LUMINANCE_ALPHA (or 2) / GL_RGBA (or 4) 2202 { 2203 // Cv = Cp(1 - Cs) + CcCs, Av = ApAs 2204 device->setStageOperation(unit, sw::TextureStage::STAGE_LERP); // Arg3 * (Arg1 - Arg2) + Arg2 2205 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE); 2206 } 2207 else UNREACHABLE(texFormat); 2208 break; 2209 case GL_ADD: 2210 if(IsAlpha(texFormat)) // GL_ALPHA 2211 { 2212 // Cv = Cp, Av = ApAs 2213 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2); 2214 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE); 2215 } 2216 else if(IsRGB(texFormat)) // GL_LUMINANCE (or 1) / GL_RGB (or 3) 2217 { 2218 // Cv = Cp + Cs, Av = Ap 2219 device->setStageOperation(unit, sw::TextureStage::STAGE_ADD); 2220 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2); 2221 } 2222 else if(IsRGBA(texFormat)) // GL_LUMINANCE_ALPHA (or 2) / GL_RGBA (or 4) 2223 { 2224 // Cv = Cp + Cs, Av = ApAs 2225 device->setStageOperation(unit, sw::TextureStage::STAGE_ADD); 2226 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE); 2227 } 2228 else UNREACHABLE(texFormat); 2229 break; 2230 default: 2231 UNREACHABLE(mState.textureUnit[unit].environmentMode); 2232 } 2233 } 2234 else // GL_COMBINE 2235 { 2236 device->setFirstArgument(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src0RGB)); 2237 device->setFirstModifier(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand0RGB)); 2238 device->setSecondArgument(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src1RGB)); 2239 device->setSecondModifier(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand1RGB)); 2240 device->setThirdArgument(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src2RGB)); 2241 device->setThirdModifier(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand2RGB)); 2242 2243 device->setStageOperation(unit, es2sw::ConvertCombineOperation(mState.textureUnit[unit].combineRGB)); 2244 2245 device->setFirstArgumentAlpha(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src0Alpha)); 2246 device->setFirstModifierAlpha(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand0Alpha)); 2247 device->setSecondArgumentAlpha(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src1Alpha)); 2248 device->setSecondModifierAlpha(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand1Alpha)); 2249 device->setThirdArgumentAlpha(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src2Alpha)); 2250 device->setThirdModifierAlpha(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand2Alpha)); 2251 2252 device->setStageOperationAlpha(unit, es2sw::ConvertCombineOperation(mState.textureUnit[unit].combineAlpha)); 2253 } 2254 } 2255 else 2256 { 2257 applyTexture(unit, nullptr); 2258 2259 device->setFirstArgument(unit, sw::TextureStage::SOURCE_CURRENT); 2260 device->setFirstModifier(unit, sw::TextureStage::MODIFIER_COLOR); 2261 device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1); 2262 2263 device->setFirstArgumentAlpha(unit, sw::TextureStage::SOURCE_CURRENT); 2264 device->setFirstModifierAlpha(unit, sw::TextureStage::MODIFIER_ALPHA); 2265 device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG1); 2266 } 2267 } 2268} 2269 2270void Context::setTextureEnvMode(GLenum texEnvMode) 2271{ 2272 mState.textureUnit[mState.activeSampler].environmentMode = texEnvMode; 2273} 2274 2275void Context::setTextureEnvColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 2276{ 2277 mState.textureUnit[mState.activeSampler].color = {red, green, blue, alpha}; 2278} 2279 2280void Context::setCombineRGB(GLenum combineRGB) 2281{ 2282 mState.textureUnit[mState.activeSampler].combineRGB = combineRGB; 2283} 2284 2285void Context::setCombineAlpha(GLenum combineAlpha) 2286{ 2287 mState.textureUnit[mState.activeSampler].combineAlpha = combineAlpha; 2288} 2289 2290void Context::setOperand0RGB(GLenum operand) 2291{ 2292 mState.textureUnit[mState.activeSampler].operand0RGB = operand; 2293} 2294 2295void Context::setOperand1RGB(GLenum operand) 2296{ 2297 mState.textureUnit[mState.activeSampler].operand1RGB = operand; 2298} 2299 2300void Context::setOperand2RGB(GLenum operand) 2301{ 2302 mState.textureUnit[mState.activeSampler].operand2RGB = operand; 2303} 2304 2305void Context::setOperand0Alpha(GLenum operand) 2306{ 2307 mState.textureUnit[mState.activeSampler].operand0Alpha = operand; 2308} 2309 2310void Context::setOperand1Alpha(GLenum operand) 2311{ 2312 mState.textureUnit[mState.activeSampler].operand1Alpha = operand; 2313} 2314 2315void Context::setOperand2Alpha(GLenum operand) 2316{ 2317 mState.textureUnit[mState.activeSampler].operand2Alpha = operand; 2318} 2319 2320void Context::setSrc0RGB(GLenum src) 2321{ 2322 mState.textureUnit[mState.activeSampler].src0RGB = src; 2323} 2324 2325void Context::setSrc1RGB(GLenum src) 2326{ 2327 mState.textureUnit[mState.activeSampler].src1RGB = src; 2328} 2329 2330void Context::setSrc2RGB(GLenum src) 2331{ 2332 mState.textureUnit[mState.activeSampler].src2RGB = src; 2333} 2334 2335void Context::setSrc0Alpha(GLenum src) 2336{ 2337 mState.textureUnit[mState.activeSampler].src0Alpha = src; 2338} 2339 2340void Context::setSrc1Alpha(GLenum src) 2341{ 2342 mState.textureUnit[mState.activeSampler].src1Alpha = src; 2343} 2344 2345void Context::setSrc2Alpha(GLenum src) 2346{ 2347 mState.textureUnit[mState.activeSampler].src2Alpha = src; 2348} 2349 2350void Context::applyTexture(int index, Texture *baseTexture) 2351{ 2352 sw::Resource *resource = 0; 2353 2354 if(baseTexture) 2355 { 2356 resource = baseTexture->getResource(); 2357 } 2358 2359 device->setTextureResource(index, resource); 2360 2361 if(baseTexture) 2362 { 2363 int levelCount = baseTexture->getLevelCount(); 2364 2365 if(baseTexture->getTarget() == GL_TEXTURE_2D || baseTexture->getTarget() == GL_TEXTURE_EXTERNAL_OES) 2366 { 2367 Texture2D *texture = static_cast<Texture2D*>(baseTexture); 2368 2369 for(int mipmapLevel = 0; mipmapLevel < MIPMAP_LEVELS; mipmapLevel++) 2370 { 2371 int surfaceLevel = mipmapLevel; 2372 2373 if(surfaceLevel < 0) 2374 { 2375 surfaceLevel = 0; 2376 } 2377 else if(surfaceLevel >= levelCount) 2378 { 2379 surfaceLevel = levelCount - 1; 2380 } 2381 2382 egl::Image *surface = texture->getImage(surfaceLevel); 2383 device->setTextureLevel(index, 0, mipmapLevel, surface, sw::TEXTURE_2D); 2384 } 2385 } 2386 else UNIMPLEMENTED(); 2387 } 2388 else 2389 { 2390 device->setTextureLevel(index, 0, 0, 0, sw::TEXTURE_NULL); 2391 } 2392} 2393 2394void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, 2395 GLenum format, GLenum type, GLsizei *bufSize, void* pixels) 2396{ 2397 Framebuffer *framebuffer = getFramebuffer(); 2398 int framebufferWidth, framebufferHeight, framebufferSamples; 2399 2400 if(framebuffer->completeness(framebufferWidth, framebufferHeight, framebufferSamples) != GL_FRAMEBUFFER_COMPLETE_OES) 2401 { 2402 return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES); 2403 } 2404 2405 if(getFramebufferName() != 0 && framebufferSamples != 0) 2406 { 2407 return error(GL_INVALID_OPERATION); 2408 } 2409 2410 if(format != GL_RGBA || type != GL_UNSIGNED_BYTE) 2411 { 2412 if(format != framebuffer->getImplementationColorReadFormat() || type != framebuffer->getImplementationColorReadType()) 2413 { 2414 return error(GL_INVALID_OPERATION); 2415 } 2416 } 2417 2418 GLsizei outputPitch = egl::ComputePitch(width, format, type, mState.packAlignment); 2419 2420 // Sized query sanity check 2421 if(bufSize) 2422 { 2423 int requiredSize = outputPitch * height; 2424 if(requiredSize > *bufSize) 2425 { 2426 return error(GL_INVALID_OPERATION); 2427 } 2428 } 2429 2430 egl::Image *renderTarget = framebuffer->getRenderTarget(); 2431 2432 if(!renderTarget) 2433 { 2434 return error(GL_OUT_OF_MEMORY); 2435 } 2436 2437 sw::Rect rect = {x, y, x + width, y + height}; 2438 rect.clip(0, 0, renderTarget->getWidth(), renderTarget->getHeight()); 2439 2440 unsigned char *source = (unsigned char*)renderTarget->lock(rect.x0, rect.y0, sw::LOCK_READONLY); 2441 unsigned char *dest = (unsigned char*)pixels; 2442 int inputPitch = (int)renderTarget->getPitch(); 2443 2444 for(int j = 0; j < rect.y1 - rect.y0; j++) 2445 { 2446 unsigned short *dest16 = (unsigned short*)dest; 2447 unsigned int *dest32 = (unsigned int*)dest; 2448 2449 if(renderTarget->getInternalFormat() == sw::FORMAT_A8B8G8R8 && 2450 format == GL_RGBA && type == GL_UNSIGNED_BYTE) 2451 { 2452 memcpy(dest, source, (rect.x1 - rect.x0) * 4); 2453 } 2454 else if(renderTarget->getInternalFormat() == sw::FORMAT_A8R8G8B8 && 2455 format == GL_RGBA && type == GL_UNSIGNED_BYTE) 2456 { 2457 for(int i = 0; i < rect.x1 - rect.x0; i++) 2458 { 2459 unsigned int argb = *(unsigned int*)(source + 4 * i); 2460 2461 dest32[i] = (argb & 0xFF00FF00) | ((argb & 0x000000FF) << 16) | ((argb & 0x00FF0000) >> 16); 2462 } 2463 } 2464 else if(renderTarget->getInternalFormat() == sw::FORMAT_X8R8G8B8 && 2465 format == GL_RGBA && type == GL_UNSIGNED_BYTE) 2466 { 2467 for(int i = 0; i < rect.x1 - rect.x0; i++) 2468 { 2469 unsigned int xrgb = *(unsigned int*)(source + 4 * i); 2470 2471 dest32[i] = (xrgb & 0xFF00FF00) | ((xrgb & 0x000000FF) << 16) | ((xrgb & 0x00FF0000) >> 16) | 0xFF000000; 2472 } 2473 } 2474 else if(renderTarget->getInternalFormat() == sw::FORMAT_X8R8G8B8 && 2475 format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE) 2476 { 2477 for(int i = 0; i < rect.x1 - rect.x0; i++) 2478 { 2479 unsigned int xrgb = *(unsigned int*)(source + 4 * i); 2480 2481 dest32[i] = xrgb | 0xFF000000; 2482 } 2483 } 2484 else if(renderTarget->getInternalFormat() == sw::FORMAT_A8R8G8B8 && 2485 format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE) 2486 { 2487 memcpy(dest, source, (rect.x1 - rect.x0) * 4); 2488 } 2489 else if(renderTarget->getInternalFormat() == sw::FORMAT_A1R5G5B5 && 2490 format == GL_BGRA_EXT && type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT) 2491 { 2492 memcpy(dest, source, (rect.x1 - rect.x0) * 2); 2493 } 2494 else if(renderTarget->getInternalFormat() == sw::FORMAT_R5G6B5 && 2495 format == 0x80E0 && type == GL_UNSIGNED_SHORT_5_6_5) // GL_BGR_EXT 2496 { 2497 memcpy(dest, source, (rect.x1 - rect.x0) * 2); 2498 } 2499 else 2500 { 2501 for(int i = 0; i < rect.x1 - rect.x0; i++) 2502 { 2503 float r; 2504 float g; 2505 float b; 2506 float a; 2507 2508 switch(renderTarget->getInternalFormat()) 2509 { 2510 case sw::FORMAT_R5G6B5: 2511 { 2512 unsigned short rgb = *(unsigned short*)(source + 2 * i); 2513 2514 a = 1.0f; 2515 b = (rgb & 0x001F) * (1.0f / 0x001F); 2516 g = (rgb & 0x07E0) * (1.0f / 0x07E0); 2517 r = (rgb & 0xF800) * (1.0f / 0xF800); 2518 } 2519 break; 2520 case sw::FORMAT_A1R5G5B5: 2521 { 2522 unsigned short argb = *(unsigned short*)(source + 2 * i); 2523 2524 a = (argb & 0x8000) ? 1.0f : 0.0f; 2525 b = (argb & 0x001F) * (1.0f / 0x001F); 2526 g = (argb & 0x03E0) * (1.0f / 0x03E0); 2527 r = (argb & 0x7C00) * (1.0f / 0x7C00); 2528 } 2529 break; 2530 case sw::FORMAT_A8R8G8B8: 2531 { 2532 unsigned int argb = *(unsigned int*)(source + 4 * i); 2533 2534 a = (argb & 0xFF000000) * (1.0f / 0xFF000000); 2535 b = (argb & 0x000000FF) * (1.0f / 0x000000FF); 2536 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); 2537 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); 2538 } 2539 break; 2540 case sw::FORMAT_A8B8G8R8: 2541 { 2542 unsigned int abgr = *(unsigned int*)(source + 4 * i); 2543 2544 a = (abgr & 0xFF000000) * (1.0f / 0xFF000000); 2545 b = (abgr & 0x00FF0000) * (1.0f / 0x00FF0000); 2546 g = (abgr & 0x0000FF00) * (1.0f / 0x0000FF00); 2547 r = (abgr & 0x000000FF) * (1.0f / 0x000000FF); 2548 } 2549 break; 2550 case sw::FORMAT_X8R8G8B8: 2551 { 2552 unsigned int xrgb = *(unsigned int*)(source + 4 * i); 2553 2554 a = 1.0f; 2555 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); 2556 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); 2557 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); 2558 } 2559 break; 2560 case sw::FORMAT_X8B8G8R8: 2561 { 2562 unsigned int xbgr = *(unsigned int*)(source + 4 * i); 2563 2564 a = 1.0f; 2565 b = (xbgr & 0x00FF0000) * (1.0f / 0x00FF0000); 2566 g = (xbgr & 0x0000FF00) * (1.0f / 0x0000FF00); 2567 r = (xbgr & 0x000000FF) * (1.0f / 0x000000FF); 2568 } 2569 break; 2570 case sw::FORMAT_A2R10G10B10: 2571 { 2572 unsigned int argb = *(unsigned int*)(source + 4 * i); 2573 2574 a = (argb & 0xC0000000) * (1.0f / 0xC0000000); 2575 b = (argb & 0x000003FF) * (1.0f / 0x000003FF); 2576 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); 2577 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); 2578 } 2579 break; 2580 default: 2581 UNIMPLEMENTED(); // FIXME 2582 UNREACHABLE(renderTarget->getInternalFormat()); 2583 } 2584 2585 switch(format) 2586 { 2587 case GL_RGBA: 2588 switch(type) 2589 { 2590 case GL_UNSIGNED_BYTE: 2591 dest[4 * i + 0] = (unsigned char)(255 * r + 0.5f); 2592 dest[4 * i + 1] = (unsigned char)(255 * g + 0.5f); 2593 dest[4 * i + 2] = (unsigned char)(255 * b + 0.5f); 2594 dest[4 * i + 3] = (unsigned char)(255 * a + 0.5f); 2595 break; 2596 default: UNREACHABLE(type); 2597 } 2598 break; 2599 case GL_BGRA_EXT: 2600 switch(type) 2601 { 2602 case GL_UNSIGNED_BYTE: 2603 dest[4 * i + 0] = (unsigned char)(255 * b + 0.5f); 2604 dest[4 * i + 1] = (unsigned char)(255 * g + 0.5f); 2605 dest[4 * i + 2] = (unsigned char)(255 * r + 0.5f); 2606 dest[4 * i + 3] = (unsigned char)(255 * a + 0.5f); 2607 break; 2608 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: 2609 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section 2610 // this type is packed as follows: 2611 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 2612 // -------------------------------------------------------------------------------- 2613 // | 4th | 3rd | 2nd | 1st component | 2614 // -------------------------------------------------------------------------------- 2615 // in the case of BGRA_EXT, B is the first component, G the second, and so forth. 2616 dest16[i] = 2617 ((unsigned short)(15 * a + 0.5f) << 12)| 2618 ((unsigned short)(15 * r + 0.5f) << 8) | 2619 ((unsigned short)(15 * g + 0.5f) << 4) | 2620 ((unsigned short)(15 * b + 0.5f) << 0); 2621 break; 2622 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: 2623 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section 2624 // this type is packed as follows: 2625 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 2626 // -------------------------------------------------------------------------------- 2627 // | 4th | 3rd | 2nd | 1st component | 2628 // -------------------------------------------------------------------------------- 2629 // in the case of BGRA_EXT, B is the first component, G the second, and so forth. 2630 dest16[i] = 2631 ((unsigned short)( a + 0.5f) << 15) | 2632 ((unsigned short)(31 * r + 0.5f) << 10) | 2633 ((unsigned short)(31 * g + 0.5f) << 5) | 2634 ((unsigned short)(31 * b + 0.5f) << 0); 2635 break; 2636 default: UNREACHABLE(type); 2637 } 2638 break; 2639 case GL_RGB: 2640 switch(type) 2641 { 2642 case GL_UNSIGNED_SHORT_5_6_5: 2643 dest16[i] = 2644 ((unsigned short)(31 * b + 0.5f) << 0) | 2645 ((unsigned short)(63 * g + 0.5f) << 5) | 2646 ((unsigned short)(31 * r + 0.5f) << 11); 2647 break; 2648 default: UNREACHABLE(type); 2649 } 2650 break; 2651 default: UNREACHABLE(format); 2652 } 2653 } 2654 } 2655 2656 source += inputPitch; 2657 dest += outputPitch; 2658 } 2659 2660 renderTarget->unlock(); 2661 renderTarget->release(); 2662} 2663 2664void Context::clear(GLbitfield mask) 2665{ 2666 Framebuffer *framebuffer = getFramebuffer(); 2667 2668 if(!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE_OES) 2669 { 2670 return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES); 2671 } 2672 2673 if(!applyRenderTarget()) 2674 { 2675 return; 2676 } 2677 2678 float depth = clamp01(mState.depthClearValue); 2679 int stencil = mState.stencilClearValue & 0x000000FF; 2680 2681 if(mask & GL_COLOR_BUFFER_BIT) 2682 { 2683 unsigned int rgbaMask = (mState.colorMaskRed ? 0x1 : 0) | 2684 (mState.colorMaskGreen ? 0x2 : 0) | 2685 (mState.colorMaskBlue ? 0x4 : 0) | 2686 (mState.colorMaskAlpha ? 0x8 : 0); 2687 2688 if(rgbaMask != 0) 2689 { 2690 device->clearColor(mState.colorClearValue.red, mState.colorClearValue.green, mState.colorClearValue.blue, mState.colorClearValue.alpha, rgbaMask); 2691 } 2692 } 2693 2694 if(mask & GL_DEPTH_BUFFER_BIT) 2695 { 2696 if(mState.depthMask != 0) 2697 { 2698 device->clearDepth(depth); 2699 } 2700 } 2701 2702 if(mask & GL_STENCIL_BUFFER_BIT) 2703 { 2704 if(mState.stencilWritemask != 0) 2705 { 2706 device->clearStencil(stencil, mState.stencilWritemask); 2707 } 2708 } 2709} 2710 2711void Context::drawArrays(GLenum mode, GLint first, GLsizei count) 2712{ 2713 sw::DrawType primitiveType; 2714 int primitiveCount; 2715 2716 if(!es2sw::ConvertPrimitiveType(mode, count, GL_NONE, primitiveType, primitiveCount)) 2717 return error(GL_INVALID_ENUM); 2718 2719 if(primitiveCount <= 0) 2720 { 2721 return; 2722 } 2723 2724 if(!applyRenderTarget()) 2725 { 2726 return; 2727 } 2728 2729 applyState(mode); 2730 2731 GLenum err = applyVertexBuffer(0, first, count); 2732 if(err != GL_NO_ERROR) 2733 { 2734 return error(err); 2735 } 2736 2737 applyTextures(); 2738 2739 if(!cullSkipsDraw(mode)) 2740 { 2741 device->drawPrimitive(primitiveType, primitiveCount); 2742 } 2743} 2744 2745void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices) 2746{ 2747 if(!indices && !mState.elementArrayBuffer) 2748 { 2749 return error(GL_INVALID_OPERATION); 2750 } 2751 2752 sw::DrawType primitiveType; 2753 int primitiveCount; 2754 2755 if(!es2sw::ConvertPrimitiveType(mode, count, type, primitiveType, primitiveCount)) 2756 return error(GL_INVALID_ENUM); 2757 2758 if(primitiveCount <= 0) 2759 { 2760 return; 2761 } 2762 2763 if(!applyRenderTarget()) 2764 { 2765 return; 2766 } 2767 2768 applyState(mode); 2769 2770 TranslatedIndexData indexInfo; 2771 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo); 2772 if(err != GL_NO_ERROR) 2773 { 2774 return error(err); 2775 } 2776 2777 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; 2778 err = applyVertexBuffer(-(int)indexInfo.minIndex, indexInfo.minIndex, vertexCount); 2779 if(err != GL_NO_ERROR) 2780 { 2781 return error(err); 2782 } 2783 2784 applyTextures(); 2785 2786 if(!cullSkipsDraw(mode)) 2787 { 2788 device->drawIndexedPrimitive(primitiveType, indexInfo.indexOffset, primitiveCount); 2789 } 2790} 2791 2792void Context::drawTexture(GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height) 2793{ 2794 es1::Framebuffer *framebuffer = getFramebuffer(); 2795 es1::Renderbuffer *renderbuffer = framebuffer->getColorbuffer(); 2796 float targetWidth = (float)renderbuffer->getWidth(); 2797 float targetHeight = (float)renderbuffer->getHeight(); 2798 float x0 = 2.0f * x / targetWidth - 1.0f; 2799 float y0 = 2.0f * y / targetHeight - 1.0f; 2800 float x1 = 2.0f * (x + width) / targetWidth - 1.0f; 2801 float y1 = 2.0f * (y + height) / targetHeight - 1.0f; 2802 float Zw = sw::clamp(mState.zNear + z * (mState.zFar - mState.zNear), mState.zNear, mState.zFar); 2803 2804 float vertices[][3] = {{x0, y0, Zw}, 2805 {x0, y1, Zw}, 2806 {x1, y0, Zw}, 2807 {x1, y1, Zw}}; 2808 2809 ASSERT(mState.samplerTexture[TEXTURE_2D][1].name() == 0); // Multi-texturing unimplemented 2810 es1::Texture *texture = getSamplerTexture(0, TEXTURE_2D); 2811 float textureWidth = (float)texture->getWidth(GL_TEXTURE_2D, 0); 2812 float textureHeight = (float)texture->getHeight(GL_TEXTURE_2D, 0); 2813 int Ucr = texture->getCropRectU(); 2814 int Vcr = texture->getCropRectV(); 2815 int Wcr = texture->getCropRectW(); 2816 int Hcr = texture->getCropRectH(); 2817 2818 float texCoords[][2] = {{Ucr / textureWidth, Vcr / textureHeight}, 2819 {Ucr / textureWidth, (Vcr + Hcr) / textureHeight}, 2820 {(Ucr + Wcr) / textureWidth, Vcr / textureHeight}, 2821 {(Ucr + Wcr) / textureWidth, (Vcr + Hcr) / textureHeight}}; 2822 2823 VertexAttribute oldPositionAttribute = mState.vertexAttribute[sw::Position]; 2824 VertexAttribute oldTexCoord0Attribute = mState.vertexAttribute[sw::TexCoord0]; 2825 gl::BindingPointer<Buffer> oldArrayBuffer = mState.arrayBuffer; 2826 mState.arrayBuffer = nullptr; 2827 2828 glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), vertices); 2829 glEnableClientState(GL_VERTEX_ARRAY); 2830 glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(float), texCoords); 2831 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 2832 2833 sw::Matrix P = projectionStack.current(); 2834 sw::Matrix M = modelViewStack.current(); 2835 sw::Matrix T = textureStack0.current(); 2836 2837 projectionStack.identity(); 2838 modelViewStack.identity(); 2839 textureStack0.identity(); 2840 2841 drawArrays(GL_TRIANGLE_STRIP, 0, 4); 2842 2843 // Restore state 2844 mState.vertexAttribute[sw::Position] = oldPositionAttribute; 2845 mState.vertexAttribute[sw::TexCoord0] = oldTexCoord0Attribute; 2846 mState.arrayBuffer = oldArrayBuffer; 2847 oldArrayBuffer = nullptr; 2848 oldPositionAttribute.mBoundBuffer = nullptr; 2849 oldTexCoord0Attribute.mBoundBuffer = nullptr; 2850 textureStack0.load(T); 2851 modelViewStack.load(M); 2852 projectionStack.load(P); 2853} 2854 2855void Context::finish() 2856{ 2857 device->finish(); 2858} 2859 2860void Context::flush() 2861{ 2862 // We don't queue anything without processing it as fast as possible 2863} 2864 2865void Context::recordInvalidEnum() 2866{ 2867 mInvalidEnum = true; 2868} 2869 2870void Context::recordInvalidValue() 2871{ 2872 mInvalidValue = true; 2873} 2874 2875void Context::recordInvalidOperation() 2876{ 2877 mInvalidOperation = true; 2878} 2879 2880void Context::recordOutOfMemory() 2881{ 2882 mOutOfMemory = true; 2883} 2884 2885void Context::recordInvalidFramebufferOperation() 2886{ 2887 mInvalidFramebufferOperation = true; 2888} 2889 2890void Context::recordMatrixStackOverflow() 2891{ 2892 mMatrixStackOverflow = true; 2893} 2894 2895void Context::recordMatrixStackUnderflow() 2896{ 2897 mMatrixStackUnderflow = true; 2898} 2899 2900// Get one of the recorded errors and clear its flag, if any. 2901// [OpenGL ES 2.0.24] section 2.5 page 13. 2902GLenum Context::getError() 2903{ 2904 if(mInvalidEnum) 2905 { 2906 mInvalidEnum = false; 2907 2908 return GL_INVALID_ENUM; 2909 } 2910 2911 if(mInvalidValue) 2912 { 2913 mInvalidValue = false; 2914 2915 return GL_INVALID_VALUE; 2916 } 2917 2918 if(mInvalidOperation) 2919 { 2920 mInvalidOperation = false; 2921 2922 return GL_INVALID_OPERATION; 2923 } 2924 2925 if(mOutOfMemory) 2926 { 2927 mOutOfMemory = false; 2928 2929 return GL_OUT_OF_MEMORY; 2930 } 2931 2932 if(mInvalidFramebufferOperation) 2933 { 2934 mInvalidFramebufferOperation = false; 2935 2936 return GL_INVALID_FRAMEBUFFER_OPERATION_OES; 2937 } 2938 2939 if(mMatrixStackOverflow) 2940 { 2941 mMatrixStackOverflow = false; 2942 2943 return GL_INVALID_FRAMEBUFFER_OPERATION_OES; 2944 } 2945 2946 if(mMatrixStackUnderflow) 2947 { 2948 mMatrixStackUnderflow = false; 2949 2950 return GL_INVALID_FRAMEBUFFER_OPERATION_OES; 2951 } 2952 2953 return GL_NO_ERROR; 2954} 2955 2956int Context::getSupportedMultisampleCount(int requested) 2957{ 2958 int supported = 0; 2959 2960 for(int i = NUM_MULTISAMPLE_COUNTS - 1; i >= 0; i--) 2961 { 2962 if(supported >= requested) 2963 { 2964 return supported; 2965 } 2966 2967 supported = multisampleCount[i]; 2968 } 2969 2970 return supported; 2971} 2972 2973void Context::detachBuffer(GLuint buffer) 2974{ 2975 // [OpenGL ES 2.0.24] section 2.9 page 22: 2976 // If a buffer object is deleted while it is bound, all bindings to that object in the current context 2977 // (i.e. in the thread that called Delete-Buffers) are reset to zero. 2978 2979 if(mState.arrayBuffer.name() == buffer) 2980 { 2981 mState.arrayBuffer = nullptr; 2982 } 2983 2984 if(mState.elementArrayBuffer.name() == buffer) 2985 { 2986 mState.elementArrayBuffer = nullptr; 2987 } 2988 2989 for(int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) 2990 { 2991 if(mState.vertexAttribute[attribute].mBoundBuffer.name() == buffer) 2992 { 2993 mState.vertexAttribute[attribute].mBoundBuffer = nullptr; 2994 } 2995 } 2996} 2997 2998void Context::detachTexture(GLuint texture) 2999{ 3000 // [OpenGL ES 2.0.24] section 3.8 page 84: 3001 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are 3002 // rebound to texture object zero 3003 3004 for(int type = 0; type < TEXTURE_TYPE_COUNT; type++) 3005 { 3006 for(int sampler = 0; sampler < MAX_TEXTURE_UNITS; sampler++) 3007 { 3008 if(mState.samplerTexture[type][sampler].name() == texture) 3009 { 3010 mState.samplerTexture[type][sampler] = nullptr; 3011 } 3012 } 3013 } 3014 3015 // [OpenGL ES 2.0.24] section 4.4 page 112: 3016 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is 3017 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this 3018 // image was attached in the currently bound framebuffer. 3019 3020 Framebuffer *framebuffer = getFramebuffer(); 3021 3022 if(framebuffer) 3023 { 3024 framebuffer->detachTexture(texture); 3025 } 3026} 3027 3028void Context::detachFramebuffer(GLuint framebuffer) 3029{ 3030 // [OpenGL ES 2.0.24] section 4.4 page 107: 3031 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though 3032 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero. 3033 3034 if(mState.framebuffer == framebuffer) 3035 { 3036 bindFramebuffer(0); 3037 } 3038} 3039 3040void Context::detachRenderbuffer(GLuint renderbuffer) 3041{ 3042 // [OpenGL ES 2.0.24] section 4.4 page 109: 3043 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer 3044 // had been executed with the target RENDERBUFFER and name of zero. 3045 3046 if(mState.renderbuffer.name() == renderbuffer) 3047 { 3048 bindRenderbuffer(0); 3049 } 3050 3051 // [OpenGL ES 2.0.24] section 4.4 page 111: 3052 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer, 3053 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment 3054 // point to which this image was attached in the currently bound framebuffer. 3055 3056 Framebuffer *framebuffer = getFramebuffer(); 3057 3058 if(framebuffer) 3059 { 3060 framebuffer->detachRenderbuffer(renderbuffer); 3061 } 3062} 3063 3064bool Context::cullSkipsDraw(GLenum drawMode) 3065{ 3066 return mState.cullFaceEnabled && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode); 3067} 3068 3069bool Context::isTriangleMode(GLenum drawMode) 3070{ 3071 switch(drawMode) 3072 { 3073 case GL_TRIANGLES: 3074 case GL_TRIANGLE_FAN: 3075 case GL_TRIANGLE_STRIP: 3076 return true; 3077 case GL_POINTS: 3078 case GL_LINES: 3079 case GL_LINE_LOOP: 3080 case GL_LINE_STRIP: 3081 return false; 3082 default: UNREACHABLE(drawMode); 3083 } 3084 3085 return false; 3086} 3087 3088void Context::setVertexAttrib(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w) 3089{ 3090 ASSERT(index < MAX_VERTEX_ATTRIBS); 3091 3092 mState.vertexAttribute[index].mCurrentValue[0] = x; 3093 mState.vertexAttribute[index].mCurrentValue[1] = y; 3094 mState.vertexAttribute[index].mCurrentValue[2] = z; 3095 mState.vertexAttribute[index].mCurrentValue[3] = w; 3096 3097 mVertexDataManager->dirtyCurrentValue(index); 3098} 3099 3100void Context::bindTexImage(egl::Surface *surface) 3101{ 3102 es1::Texture2D *textureObject = getTexture2D(); 3103 3104 if(textureObject) 3105 { 3106 textureObject->bindTexImage(surface); 3107 } 3108} 3109 3110EGLenum Context::validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel) 3111{ 3112 switch(target) 3113 { 3114 case EGL_GL_TEXTURE_2D_KHR: 3115 break; 3116 case EGL_GL_RENDERBUFFER_KHR: 3117 break; 3118 default: 3119 return EGL_BAD_PARAMETER; 3120 } 3121 3122 if(textureLevel >= IMPLEMENTATION_MAX_TEXTURE_LEVELS) 3123 { 3124 return EGL_BAD_MATCH; 3125 } 3126 3127 if(target == EGL_GL_TEXTURE_2D_KHR) 3128 { 3129 Texture *texture = getTexture(name); 3130 3131 if(!texture || texture->getTarget() != GL_TEXTURE_2D) 3132 { 3133 return EGL_BAD_PARAMETER; 3134 } 3135 3136 if(texture->isShared(GL_TEXTURE_2D, textureLevel)) // Bound to an EGLSurface or already an EGLImage sibling 3137 { 3138 return EGL_BAD_ACCESS; 3139 } 3140 3141 if(textureLevel != 0 && !texture->isSamplerComplete()) 3142 { 3143 return EGL_BAD_PARAMETER; 3144 } 3145 3146 if(textureLevel == 0 && !(texture->isSamplerComplete() && texture->getLevelCount() == 1)) 3147 { 3148 return EGL_BAD_PARAMETER; 3149 } 3150 } 3151 else if(target == EGL_GL_RENDERBUFFER_KHR) 3152 { 3153 Renderbuffer *renderbuffer = getRenderbuffer(name); 3154 3155 if(!renderbuffer) 3156 { 3157 return EGL_BAD_PARAMETER; 3158 } 3159 3160 if(renderbuffer->isShared()) // Already an EGLImage sibling 3161 { 3162 return EGL_BAD_ACCESS; 3163 } 3164 } 3165 else UNREACHABLE(target); 3166 3167 return EGL_SUCCESS; 3168} 3169 3170egl::Image *Context::createSharedImage(EGLenum target, GLuint name, GLuint textureLevel) 3171{ 3172 if(target == EGL_GL_TEXTURE_2D_KHR) 3173 { 3174 es1::Texture *texture = getTexture(name); 3175 3176 return texture->createSharedImage(GL_TEXTURE_2D, textureLevel); 3177 } 3178 else if(target == EGL_GL_RENDERBUFFER_KHR) 3179 { 3180 es1::Renderbuffer *renderbuffer = getRenderbuffer(name); 3181 3182 return renderbuffer->createSharedImage(); 3183 } 3184 else UNREACHABLE(target); 3185 3186 return 0; 3187} 3188 3189Device *Context::getDevice() 3190{ 3191 return device; 3192} 3193 3194void Context::setMatrixMode(GLenum mode) 3195{ 3196 matrixMode = mode; 3197} 3198 3199sw::MatrixStack &Context::currentMatrixStack() 3200{ 3201 switch(matrixMode) 3202 { 3203 case GL_MODELVIEW: 3204 return modelViewStack; 3205 case GL_PROJECTION: 3206 return projectionStack; 3207 case GL_TEXTURE: 3208 switch(mState.activeSampler) 3209 { 3210 case 0: return textureStack0; 3211 case 1: return textureStack1; 3212 } 3213 break; 3214 } 3215 3216 UNREACHABLE(matrixMode); 3217 return textureStack0; 3218} 3219 3220void Context::loadIdentity() 3221{ 3222 currentMatrixStack().identity(); 3223} 3224 3225void Context::load(const GLfloat *m) 3226{ 3227 currentMatrixStack().load(m); 3228} 3229 3230void Context::pushMatrix() 3231{ 3232 if(!currentMatrixStack().push()) 3233 { 3234 return error(GL_STACK_OVERFLOW); 3235 } 3236} 3237 3238void Context::popMatrix() 3239{ 3240 if(!currentMatrixStack().pop()) 3241 { 3242 return error(GL_STACK_OVERFLOW); 3243 } 3244} 3245 3246void Context::rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) 3247{ 3248 currentMatrixStack().rotate(angle, x, y, z); 3249} 3250 3251void Context::translate(GLfloat x, GLfloat y, GLfloat z) 3252{ 3253 currentMatrixStack().translate(x, y, z); 3254} 3255 3256void Context::scale(GLfloat x, GLfloat y, GLfloat z) 3257{ 3258 currentMatrixStack().scale(x, y, z); 3259} 3260 3261void Context::multiply(const GLfloat *m) 3262{ 3263 currentMatrixStack().multiply(m); 3264} 3265 3266void Context::frustum(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar) 3267{ 3268 currentMatrixStack().frustum(left, right, bottom, top, zNear, zFar); 3269} 3270 3271void Context::ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar) 3272{ 3273 currentMatrixStack().ortho(left, right, bottom, top, zNear, zFar); 3274} 3275 3276void Context::setClipPlane(int index, const float plane[4]) 3277{ 3278 sw::Plane clipPlane = modelViewStack.current() * sw::Plane(plane); 3279 device->setClipPlane(index, &clipPlane.A); 3280} 3281 3282void Context::setClipPlaneEnabled(int index, bool enable) 3283{ 3284 clipFlags = (clipFlags & ~((int)!enable << index)) | ((int)enable << index); 3285 device->setClipFlags(clipFlags); 3286} 3287 3288bool Context::isClipPlaneEnabled(int index) const 3289{ 3290 return (clipFlags & (1 << index)) != 0; 3291} 3292 3293void Context::setColorLogicOpEnabled(bool enable) 3294{ 3295 colorLogicOpEnabled = enable; 3296} 3297 3298bool Context::isColorLogicOpEnabled() const 3299{ 3300 return colorLogicOpEnabled; 3301} 3302 3303void Context::setLogicalOperation(GLenum logicOp) 3304{ 3305 logicalOperation = logicOp; 3306} 3307 3308void Context::setLineSmoothEnabled(bool enable) 3309{ 3310 lineSmoothEnabled = enable; 3311} 3312 3313bool Context::isLineSmoothEnabled() const 3314{ 3315 return lineSmoothEnabled; 3316} 3317 3318void Context::setColorMaterialEnabled(bool enable) 3319{ 3320 colorMaterialEnabled = enable; 3321} 3322 3323bool Context::isColorMaterialEnabled() const 3324{ 3325 return colorMaterialEnabled; 3326} 3327 3328void Context::setNormalizeEnabled(bool enable) 3329{ 3330 normalizeEnabled = enable; 3331} 3332 3333bool Context::isNormalizeEnabled() const 3334{ 3335 return normalizeEnabled; 3336} 3337 3338void Context::setRescaleNormalEnabled(bool enable) 3339{ 3340 rescaleNormalEnabled = enable; 3341} 3342 3343bool Context::isRescaleNormalEnabled() const 3344{ 3345 return rescaleNormalEnabled; 3346} 3347 3348void Context::setVertexArrayEnabled(bool enable) 3349{ 3350 mState.vertexAttribute[sw::Position].mArrayEnabled = enable; 3351} 3352 3353bool Context::isVertexArrayEnabled() const 3354{ 3355 return mState.vertexAttribute[sw::Position].mArrayEnabled; 3356} 3357 3358void Context::setNormalArrayEnabled(bool enable) 3359{ 3360 mState.vertexAttribute[sw::Normal].mArrayEnabled = enable; 3361} 3362 3363bool Context::isNormalArrayEnabled() const 3364{ 3365 return mState.vertexAttribute[sw::Normal].mArrayEnabled; 3366} 3367 3368void Context::setColorArrayEnabled(bool enable) 3369{ 3370 mState.vertexAttribute[sw::Color0].mArrayEnabled = enable; 3371} 3372 3373bool Context::isColorArrayEnabled() const 3374{ 3375 return mState.vertexAttribute[sw::Color0].mArrayEnabled; 3376} 3377 3378void Context::setPointSizeArrayEnabled(bool enable) 3379{ 3380 mState.vertexAttribute[sw::PointSize].mArrayEnabled = enable; 3381} 3382 3383bool Context::isPointSizeArrayEnabled() const 3384{ 3385 return mState.vertexAttribute[sw::PointSize].mArrayEnabled; 3386} 3387 3388void Context::setTextureCoordArrayEnabled(bool enable) 3389{ 3390 mState.vertexAttribute[sw::TexCoord0 + clientTexture].mArrayEnabled = enable; 3391} 3392 3393bool Context::isTextureCoordArrayEnabled() const 3394{ 3395 return mState.vertexAttribute[sw::TexCoord0 + clientTexture].mArrayEnabled; 3396} 3397 3398void Context::setMultisampleEnabled(bool enable) 3399{ 3400 multisampleEnabled = enable; 3401} 3402 3403bool Context::isMultisampleEnabled() const 3404{ 3405 return multisampleEnabled; 3406} 3407 3408void Context::setSampleAlphaToOneEnabled(bool enable) 3409{ 3410 sampleAlphaToOneEnabled = enable; 3411} 3412 3413bool Context::isSampleAlphaToOneEnabled() const 3414{ 3415 return sampleAlphaToOneEnabled; 3416} 3417 3418void Context::setPointSpriteEnabled(bool enable) 3419{ 3420 pointSpriteEnabled = enable; 3421} 3422 3423bool Context::isPointSpriteEnabled() const 3424{ 3425 return pointSpriteEnabled; 3426} 3427 3428void Context::setPointSmoothEnabled(bool enable) 3429{ 3430 pointSmoothEnabled = enable; 3431} 3432 3433bool Context::isPointSmoothEnabled() const 3434{ 3435 return pointSmoothEnabled; 3436} 3437 3438 3439void Context::setPointSizeMin(float min) 3440{ 3441 pointSizeMin = min; 3442} 3443 3444void Context::setPointSizeMax(float max) 3445{ 3446 pointSizeMax = max; 3447} 3448 3449void Context::setPointDistanceAttenuation(float a, float b, float c) 3450{ 3451 pointDistanceAttenuation = {a, b, c}; 3452} 3453 3454void Context::setPointFadeThresholdSize(float threshold) 3455{ 3456 pointFadeThresholdSize = threshold; 3457} 3458 3459void Context::clientActiveTexture(GLenum texture) 3460{ 3461 clientTexture = texture; 3462} 3463 3464GLenum Context::getClientActiveTexture() const 3465{ 3466 return clientTexture; 3467} 3468 3469unsigned int Context::getActiveTexture() const 3470{ 3471 return mState.activeSampler; 3472} 3473 3474} 3475 3476egl::Context *es1CreateContext(const egl::Config *config, const egl::Context *shareContext) 3477{ 3478 ASSERT(!shareContext || shareContext->getClientVersion() == 1); // Should be checked by eglCreateContext 3479 return new es1::Context(config, static_cast<const es1::Context*>(shareContext)); 3480} 3481