daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | af29cac | 2011-05-11 15:36:31 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Context.cpp: Implements the gl::Context class, managing all GL state and performing |
| 8 | // rendering operations. It is the GLES2 specific implementation of EGLContext. |
| 9 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 10 | #include "libGLESv2/Context.h" |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
| 13 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 14 | #include "libEGL/Display.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 15 | |
| 16 | #include "libGLESv2/main.h" |
| 17 | #include "libGLESv2/mathutil.h" |
| 18 | #include "libGLESv2/utilities.h" |
| 19 | #include "libGLESv2/Blit.h" |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 20 | #include "libGLESv2/ResourceManager.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 21 | #include "libGLESv2/Buffer.h" |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 22 | #include "libGLESv2/Fence.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 23 | #include "libGLESv2/FrameBuffer.h" |
| 24 | #include "libGLESv2/Program.h" |
| 25 | #include "libGLESv2/RenderBuffer.h" |
| 26 | #include "libGLESv2/Shader.h" |
| 27 | #include "libGLESv2/Texture.h" |
daniel@transgaming.com | 8fd34bd | 2011-02-18 02:52:14 +0000 | [diff] [blame] | 28 | #include "libGLESv2/VertexDataManager.h" |
| 29 | #include "libGLESv2/IndexDataManager.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 31 | #undef near |
| 32 | #undef far |
| 33 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 34 | namespace |
| 35 | { |
| 36 | enum { CLOSING_INDEX_BUFFER_SIZE = 4096 }; |
| 37 | } |
| 38 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | namespace gl |
| 40 | { |
daniel@transgaming.com | 09c2c1a | 2011-04-13 14:57:16 +0000 | [diff] [blame] | 41 | Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 42 | { |
benvanik@google.com | 1a23334 | 2011-04-28 19:44:39 +0000 | [diff] [blame] | 43 | mFenceHandleAllocator.setBaseHandle(0); |
| 44 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 45 | setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 46 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 47 | mState.depthClearValue = 1.0f; |
| 48 | mState.stencilClearValue = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 49 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 50 | mState.cullFace = false; |
| 51 | mState.cullMode = GL_BACK; |
| 52 | mState.frontFace = GL_CCW; |
| 53 | mState.depthTest = false; |
| 54 | mState.depthFunc = GL_LESS; |
| 55 | mState.blend = false; |
| 56 | mState.sourceBlendRGB = GL_ONE; |
| 57 | mState.sourceBlendAlpha = GL_ONE; |
| 58 | mState.destBlendRGB = GL_ZERO; |
| 59 | mState.destBlendAlpha = GL_ZERO; |
| 60 | mState.blendEquationRGB = GL_FUNC_ADD; |
| 61 | mState.blendEquationAlpha = GL_FUNC_ADD; |
| 62 | mState.blendColor.red = 0; |
| 63 | mState.blendColor.green = 0; |
| 64 | mState.blendColor.blue = 0; |
| 65 | mState.blendColor.alpha = 0; |
| 66 | mState.stencilTest = false; |
| 67 | mState.stencilFunc = GL_ALWAYS; |
| 68 | mState.stencilRef = 0; |
| 69 | mState.stencilMask = -1; |
| 70 | mState.stencilWritemask = -1; |
| 71 | mState.stencilBackFunc = GL_ALWAYS; |
| 72 | mState.stencilBackRef = 0; |
| 73 | mState.stencilBackMask = - 1; |
| 74 | mState.stencilBackWritemask = -1; |
| 75 | mState.stencilFail = GL_KEEP; |
| 76 | mState.stencilPassDepthFail = GL_KEEP; |
| 77 | mState.stencilPassDepthPass = GL_KEEP; |
| 78 | mState.stencilBackFail = GL_KEEP; |
| 79 | mState.stencilBackPassDepthFail = GL_KEEP; |
| 80 | mState.stencilBackPassDepthPass = GL_KEEP; |
| 81 | mState.polygonOffsetFill = false; |
| 82 | mState.polygonOffsetFactor = 0.0f; |
| 83 | mState.polygonOffsetUnits = 0.0f; |
| 84 | mState.sampleAlphaToCoverage = false; |
| 85 | mState.sampleCoverage = false; |
| 86 | mState.sampleCoverageValue = 1.0f; |
daniel@transgaming.com | a36f98e | 2010-05-18 18:51:09 +0000 | [diff] [blame] | 87 | mState.sampleCoverageInvert = false; |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 88 | mState.scissorTest = false; |
| 89 | mState.dither = true; |
| 90 | mState.generateMipmapHint = GL_DONT_CARE; |
alokp@chromium.org | d303ef9 | 2010-09-09 17:30:15 +0000 | [diff] [blame] | 91 | mState.fragmentShaderDerivativeHint = GL_DONT_CARE; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 92 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 93 | mState.lineWidth = 1.0f; |
daniel@transgaming.com | 32e58cd | 2010-03-24 09:44:10 +0000 | [diff] [blame] | 94 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 95 | mState.viewportX = 0; |
| 96 | mState.viewportY = 0; |
| 97 | mState.viewportWidth = config->mDisplayMode.Width; |
| 98 | mState.viewportHeight = config->mDisplayMode.Height; |
| 99 | mState.zNear = 0.0f; |
| 100 | mState.zFar = 1.0f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 102 | mState.scissorX = 0; |
| 103 | mState.scissorY = 0; |
| 104 | mState.scissorWidth = config->mDisplayMode.Width; |
| 105 | mState.scissorHeight = config->mDisplayMode.Height; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 106 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 107 | mState.colorMaskRed = true; |
| 108 | mState.colorMaskGreen = true; |
| 109 | mState.colorMaskBlue = true; |
| 110 | mState.colorMaskAlpha = true; |
| 111 | mState.depthMask = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 112 | |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 113 | if (shareContext != NULL) |
| 114 | { |
| 115 | mResourceManager = shareContext->mResourceManager; |
| 116 | mResourceManager->addRef(); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | mResourceManager = new ResourceManager(); |
| 121 | } |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 122 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 123 | // [OpenGL ES 2.0.24] section 3.7 page 83: |
| 124 | // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional |
| 125 | // and cube map texture state vectors respectively associated with them. |
| 126 | // In order that access to these initial textures not be lost, they are treated as texture |
| 127 | // objects all of whose names are 0. |
| 128 | |
apatrick@chromium.org | 4e3bad4 | 2010-09-15 17:31:48 +0000 | [diff] [blame] | 129 | mTexture2DZero.set(new Texture2D(0)); |
| 130 | mTextureCubeMapZero.set(new TextureCubeMap(0)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 131 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 132 | mState.activeSampler = 0; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 133 | bindArrayBuffer(0); |
| 134 | bindElementArrayBuffer(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | bindTextureCubeMap(0); |
| 136 | bindTexture2D(0); |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 137 | bindReadFramebuffer(0); |
| 138 | bindDrawFramebuffer(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 139 | bindRenderbuffer(0); |
| 140 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 141 | mState.currentProgram = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 143 | mState.packAlignment = 4; |
| 144 | mState.unpackAlignment = 4; |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 145 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 146 | mVertexDataManager = NULL; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 147 | mIndexDataManager = NULL; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 148 | mBlit = NULL; |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 149 | mClosingIB = NULL; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 150 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | mInvalidEnum = false; |
| 152 | mInvalidValue = false; |
| 153 | mInvalidOperation = false; |
| 154 | mOutOfMemory = false; |
| 155 | mInvalidFramebufferOperation = false; |
daniel@transgaming.com | 159acdf | 2010-03-21 04:31:24 +0000 | [diff] [blame] | 156 | |
| 157 | mHasBeenCurrent = false; |
daniel@transgaming.com | 4fa0833 | 2010-05-11 02:29:27 +0000 | [diff] [blame] | 158 | |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 159 | mSupportsCompressedTextures = false; |
| 160 | mSupportsEventQueries = false; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 161 | mMaxSupportedSamples = 0; |
daniel@transgaming.com | 8f05d1a | 2010-06-07 02:06:03 +0000 | [diff] [blame] | 162 | mMaskedClearSavedState = NULL; |
daniel@transgaming.com | 4fa0833 | 2010-05-11 02:29:27 +0000 | [diff] [blame] | 163 | markAllStateDirty(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | Context::~Context() |
| 167 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 168 | if (mState.currentProgram != 0) |
| 169 | { |
| 170 | Program *programObject = mResourceManager->getProgram(mState.currentProgram); |
| 171 | if (programObject) |
| 172 | { |
| 173 | programObject->release(); |
| 174 | } |
| 175 | mState.currentProgram = 0; |
| 176 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 177 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 178 | while (!mFramebufferMap.empty()) |
| 179 | { |
| 180 | deleteFramebuffer(mFramebufferMap.begin()->first); |
| 181 | } |
| 182 | |
daniel@transgaming.com | fe20888 | 2010-09-01 15:47:57 +0000 | [diff] [blame] | 183 | while (!mFenceMap.empty()) |
| 184 | { |
| 185 | deleteFence(mFenceMap.begin()->first); |
| 186 | } |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 187 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 188 | while (!mMultiSampleSupport.empty()) |
| 189 | { |
| 190 | delete [] mMultiSampleSupport.begin()->second; |
| 191 | mMultiSampleSupport.erase(mMultiSampleSupport.begin()); |
| 192 | } |
| 193 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 194 | for (int type = 0; type < TEXTURE_TYPE_COUNT; type++) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 195 | { |
daniel@transgaming.com | 3f74c7a | 2011-05-11 15:36:51 +0000 | [diff] [blame] | 196 | for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++) |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 197 | { |
| 198 | mState.samplerTexture[type][sampler].set(NULL); |
| 199 | } |
| 200 | } |
| 201 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 202 | for (int type = 0; type < TEXTURE_TYPE_COUNT; type++) |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 203 | { |
apatrick@chromium.org | 4e3bad4 | 2010-09-15 17:31:48 +0000 | [diff] [blame] | 204 | mIncompleteTextures[type].set(NULL); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 205 | } |
| 206 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 207 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 208 | { |
| 209 | mState.vertexAttribute[i].mBoundBuffer.set(NULL); |
| 210 | } |
| 211 | |
| 212 | mState.arrayBuffer.set(NULL); |
| 213 | mState.elementArrayBuffer.set(NULL); |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 214 | mState.renderbuffer.set(NULL); |
| 215 | |
apatrick@chromium.org | 4e3bad4 | 2010-09-15 17:31:48 +0000 | [diff] [blame] | 216 | mTexture2DZero.set(NULL); |
| 217 | mTextureCubeMapZero.set(NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 218 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 219 | delete mVertexDataManager; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 220 | delete mIndexDataManager; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 221 | delete mBlit; |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 222 | delete mClosingIB; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 223 | |
daniel@transgaming.com | 8f05d1a | 2010-06-07 02:06:03 +0000 | [diff] [blame] | 224 | if (mMaskedClearSavedState) |
| 225 | { |
| 226 | mMaskedClearSavedState->Release(); |
| 227 | } |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 228 | |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 229 | mResourceManager->release(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void Context::makeCurrent(egl::Display *display, egl::Surface *surface) |
| 233 | { |
| 234 | IDirect3DDevice9 *device = display->getDevice(); |
| 235 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 236 | if (!mHasBeenCurrent) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 237 | { |
daniel@transgaming.com | 353569a | 2010-06-24 13:02:12 +0000 | [diff] [blame] | 238 | mDeviceCaps = display->getDeviceCaps(); |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 239 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 240 | mVertexDataManager = new VertexDataManager(this, device); |
| 241 | mIndexDataManager = new IndexDataManager(this, device); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 242 | mBlit = new Blit(this); |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 243 | |
daniel@transgaming.com | 5d752f2 | 2010-10-07 13:37:20 +0000 | [diff] [blame] | 244 | mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0); |
daniel@transgaming.com | af29cac | 2011-05-11 15:36:31 +0000 | [diff] [blame] | 245 | mSupportsVertexTexture = display->getVertexTextureSupport(); |
daniel@transgaming.com | 5d752f2 | 2010-10-07 13:37:20 +0000 | [diff] [blame] | 246 | |
| 247 | mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight), |
| 248 | (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE); |
| 249 | mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE); |
| 250 | mMaxRenderbufferDimension = mMaxTextureDimension; |
| 251 | mMaxTextureLevel = log2(mMaxTextureDimension) + 1; |
| 252 | TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d", |
| 253 | mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel); |
| 254 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 255 | const D3DFORMAT renderBufferFormats[] = |
| 256 | { |
| 257 | D3DFMT_A8R8G8B8, |
daniel@transgaming.com | 6397754 | 2010-08-24 19:21:02 +0000 | [diff] [blame] | 258 | D3DFMT_X8R8G8B8, |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 259 | D3DFMT_R5G6B5, |
| 260 | D3DFMT_D24S8 |
| 261 | }; |
| 262 | |
| 263 | int max = 0; |
| 264 | for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i) |
| 265 | { |
| 266 | bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; |
| 267 | display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray); |
| 268 | mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray; |
| 269 | |
| 270 | for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) |
| 271 | { |
| 272 | if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) |
| 273 | { |
| 274 | max = j; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | mMaxSupportedSamples = max; |
| 280 | |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 281 | mSupportsEventQueries = display->getEventQuerySupport(); |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 282 | mSupportsCompressedTextures = display->getCompressedTextureSupport(); |
daniel@transgaming.com | 1297d92 | 2010-09-01 15:47:47 +0000 | [diff] [blame] | 283 | mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures); |
| 284 | mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures); |
daniel@transgaming.com | ed828e5 | 2010-10-15 17:57:30 +0000 | [diff] [blame] | 285 | mSupportsLuminanceTextures = display->getLuminanceTextureSupport(); |
| 286 | mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport(); |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 287 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 288 | mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16); |
| 289 | |
daniel@transgaming.com | c808c5a | 2010-05-14 17:31:01 +0000 | [diff] [blame] | 290 | initExtensionString(); |
| 291 | |
| 292 | mState.viewportX = 0; |
| 293 | mState.viewportY = 0; |
| 294 | mState.viewportWidth = surface->getWidth(); |
| 295 | mState.viewportHeight = surface->getHeight(); |
| 296 | |
| 297 | mState.scissorX = 0; |
| 298 | mState.scissorY = 0; |
| 299 | mState.scissorWidth = surface->getWidth(); |
| 300 | mState.scissorHeight = surface->getHeight(); |
| 301 | |
| 302 | mHasBeenCurrent = true; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 303 | } |
| 304 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 305 | // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names |
| 306 | IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget(); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 307 | IDirect3DSurface9 *depthStencil = surface->getDepthStencil(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 308 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 309 | Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget); |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 310 | DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil); |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 311 | Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 312 | |
| 313 | setFramebufferZero(framebufferZero); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 314 | |
daniel@transgaming.com | d36c6a0 | 2010-08-31 12:15:09 +0000 | [diff] [blame] | 315 | if (defaultRenderTarget) |
| 316 | { |
| 317 | defaultRenderTarget->Release(); |
| 318 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 319 | |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 320 | if (depthStencil) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 321 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 322 | depthStencil->Release(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 323 | } |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 324 | |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 325 | markAllStateDirty(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 326 | } |
| 327 | |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 328 | // This function will set all of the state-related dirty flags, so that all state is set during next pre-draw. |
daniel@transgaming.com | 4fa0833 | 2010-05-11 02:29:27 +0000 | [diff] [blame] | 329 | void Context::markAllStateDirty() |
| 330 | { |
daniel@transgaming.com | 38e76e5 | 2011-03-21 16:39:10 +0000 | [diff] [blame] | 331 | for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++) |
| 332 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 333 | mAppliedTextureSerialPS[t] = 0; |
| 334 | } |
| 335 | |
| 336 | for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++) |
| 337 | { |
| 338 | mAppliedTextureSerialVS[t] = 0; |
daniel@transgaming.com | 38e76e5 | 2011-03-21 16:39:10 +0000 | [diff] [blame] | 339 | } |
| 340 | |
daniel@transgaming.com | a9eb5da | 2011-03-21 16:39:16 +0000 | [diff] [blame] | 341 | mAppliedProgramSerial = 0; |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 342 | mAppliedRenderTargetSerial = 0; |
daniel@transgaming.com | 339ae70 | 2010-05-12 03:40:20 +0000 | [diff] [blame] | 343 | mAppliedDepthbufferSerial = 0; |
apatrick@chromium.org | 85dc42b | 2010-09-14 03:10:08 +0000 | [diff] [blame] | 344 | mAppliedStencilbufferSerial = 0; |
vangelis@chromium.org | cf66ebb | 2010-09-14 22:15:43 +0000 | [diff] [blame] | 345 | mDepthStencilInitialized = false; |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 346 | |
| 347 | mClearStateDirty = true; |
| 348 | mCullStateDirty = true; |
| 349 | mDepthStateDirty = true; |
| 350 | mMaskStateDirty = true; |
| 351 | mBlendStateDirty = true; |
| 352 | mStencilStateDirty = true; |
| 353 | mPolygonOffsetStateDirty = true; |
| 354 | mScissorStateDirty = true; |
| 355 | mSampleStateDirty = true; |
| 356 | mDitherStateDirty = true; |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 357 | mFrontFaceDirty = true; |
daniel@transgaming.com | 4fa0833 | 2010-05-11 02:29:27 +0000 | [diff] [blame] | 358 | } |
| 359 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 360 | void Context::setClearColor(float red, float green, float blue, float alpha) |
| 361 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 362 | mState.colorClearValue.red = red; |
| 363 | mState.colorClearValue.green = green; |
| 364 | mState.colorClearValue.blue = blue; |
| 365 | mState.colorClearValue.alpha = alpha; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void Context::setClearDepth(float depth) |
| 369 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 370 | mState.depthClearValue = depth; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void Context::setClearStencil(int stencil) |
| 374 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 375 | mState.stencilClearValue = stencil; |
| 376 | } |
| 377 | |
| 378 | void Context::setCullFace(bool enabled) |
| 379 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 380 | if (mState.cullFace != enabled) |
| 381 | { |
| 382 | mState.cullFace = enabled; |
| 383 | mCullStateDirty = true; |
| 384 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | bool Context::isCullFaceEnabled() const |
| 388 | { |
| 389 | return mState.cullFace; |
| 390 | } |
| 391 | |
| 392 | void Context::setCullMode(GLenum mode) |
| 393 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 394 | if (mState.cullMode != mode) |
| 395 | { |
| 396 | mState.cullMode = mode; |
| 397 | mCullStateDirty = true; |
| 398 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void Context::setFrontFace(GLenum front) |
| 402 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 403 | if (mState.frontFace != front) |
| 404 | { |
| 405 | mState.frontFace = front; |
| 406 | mFrontFaceDirty = true; |
| 407 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void Context::setDepthTest(bool enabled) |
| 411 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 412 | if (mState.depthTest != enabled) |
| 413 | { |
| 414 | mState.depthTest = enabled; |
| 415 | mDepthStateDirty = true; |
| 416 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | bool Context::isDepthTestEnabled() const |
| 420 | { |
| 421 | return mState.depthTest; |
| 422 | } |
| 423 | |
| 424 | void Context::setDepthFunc(GLenum depthFunc) |
| 425 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 426 | if (mState.depthFunc != depthFunc) |
| 427 | { |
| 428 | mState.depthFunc = depthFunc; |
| 429 | mDepthStateDirty = true; |
| 430 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void Context::setDepthRange(float zNear, float zFar) |
| 434 | { |
| 435 | mState.zNear = zNear; |
| 436 | mState.zFar = zFar; |
| 437 | } |
| 438 | |
| 439 | void Context::setBlend(bool enabled) |
| 440 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 441 | if (mState.blend != enabled) |
| 442 | { |
| 443 | mState.blend = enabled; |
| 444 | mBlendStateDirty = true; |
| 445 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | bool Context::isBlendEnabled() const |
| 449 | { |
| 450 | return mState.blend; |
| 451 | } |
| 452 | |
| 453 | void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha) |
| 454 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 455 | if (mState.sourceBlendRGB != sourceRGB || |
| 456 | mState.sourceBlendAlpha != sourceAlpha || |
| 457 | mState.destBlendRGB != destRGB || |
| 458 | mState.destBlendAlpha != destAlpha) |
| 459 | { |
| 460 | mState.sourceBlendRGB = sourceRGB; |
| 461 | mState.destBlendRGB = destRGB; |
| 462 | mState.sourceBlendAlpha = sourceAlpha; |
| 463 | mState.destBlendAlpha = destAlpha; |
| 464 | mBlendStateDirty = true; |
| 465 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void Context::setBlendColor(float red, float green, float blue, float alpha) |
| 469 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 470 | if (mState.blendColor.red != red || |
| 471 | mState.blendColor.green != green || |
| 472 | mState.blendColor.blue != blue || |
| 473 | mState.blendColor.alpha != alpha) |
| 474 | { |
| 475 | mState.blendColor.red = red; |
| 476 | mState.blendColor.green = green; |
| 477 | mState.blendColor.blue = blue; |
| 478 | mState.blendColor.alpha = alpha; |
| 479 | mBlendStateDirty = true; |
| 480 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation) |
| 484 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 485 | if (mState.blendEquationRGB != rgbEquation || |
| 486 | mState.blendEquationAlpha != alphaEquation) |
| 487 | { |
| 488 | mState.blendEquationRGB = rgbEquation; |
| 489 | mState.blendEquationAlpha = alphaEquation; |
| 490 | mBlendStateDirty = true; |
| 491 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void Context::setStencilTest(bool enabled) |
| 495 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 496 | if (mState.stencilTest != enabled) |
| 497 | { |
| 498 | mState.stencilTest = enabled; |
| 499 | mStencilStateDirty = true; |
| 500 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | bool Context::isStencilTestEnabled() const |
| 504 | { |
| 505 | return mState.stencilTest; |
| 506 | } |
| 507 | |
| 508 | void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask) |
| 509 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 510 | if (mState.stencilFunc != stencilFunc || |
| 511 | mState.stencilRef != stencilRef || |
| 512 | mState.stencilMask != stencilMask) |
| 513 | { |
| 514 | mState.stencilFunc = stencilFunc; |
daniel@transgaming.com | dd7948b | 2010-06-02 16:12:34 +0000 | [diff] [blame] | 515 | mState.stencilRef = (stencilRef > 0) ? stencilRef : 0; |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 516 | mState.stencilMask = stencilMask; |
| 517 | mStencilStateDirty = true; |
| 518 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask) |
| 522 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 523 | if (mState.stencilBackFunc != stencilBackFunc || |
| 524 | mState.stencilBackRef != stencilBackRef || |
| 525 | mState.stencilBackMask != stencilBackMask) |
| 526 | { |
| 527 | mState.stencilBackFunc = stencilBackFunc; |
daniel@transgaming.com | dd7948b | 2010-06-02 16:12:34 +0000 | [diff] [blame] | 528 | mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0; |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 529 | mState.stencilBackMask = stencilBackMask; |
| 530 | mStencilStateDirty = true; |
| 531 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void Context::setStencilWritemask(GLuint stencilWritemask) |
| 535 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 536 | if (mState.stencilWritemask != stencilWritemask) |
| 537 | { |
| 538 | mState.stencilWritemask = stencilWritemask; |
| 539 | mStencilStateDirty = true; |
| 540 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void Context::setStencilBackWritemask(GLuint stencilBackWritemask) |
| 544 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 545 | if (mState.stencilBackWritemask != stencilBackWritemask) |
| 546 | { |
| 547 | mState.stencilBackWritemask = stencilBackWritemask; |
| 548 | mStencilStateDirty = true; |
| 549 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass) |
| 553 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 554 | if (mState.stencilFail != stencilFail || |
| 555 | mState.stencilPassDepthFail != stencilPassDepthFail || |
| 556 | mState.stencilPassDepthPass != stencilPassDepthPass) |
| 557 | { |
| 558 | mState.stencilFail = stencilFail; |
| 559 | mState.stencilPassDepthFail = stencilPassDepthFail; |
| 560 | mState.stencilPassDepthPass = stencilPassDepthPass; |
| 561 | mStencilStateDirty = true; |
| 562 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass) |
| 566 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 567 | if (mState.stencilBackFail != stencilBackFail || |
| 568 | mState.stencilBackPassDepthFail != stencilBackPassDepthFail || |
| 569 | mState.stencilBackPassDepthPass != stencilBackPassDepthPass) |
| 570 | { |
| 571 | mState.stencilBackFail = stencilBackFail; |
| 572 | mState.stencilBackPassDepthFail = stencilBackPassDepthFail; |
| 573 | mState.stencilBackPassDepthPass = stencilBackPassDepthPass; |
| 574 | mStencilStateDirty = true; |
| 575 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | void Context::setPolygonOffsetFill(bool enabled) |
| 579 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 580 | if (mState.polygonOffsetFill != enabled) |
| 581 | { |
| 582 | mState.polygonOffsetFill = enabled; |
| 583 | mPolygonOffsetStateDirty = true; |
| 584 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | bool Context::isPolygonOffsetFillEnabled() const |
| 588 | { |
| 589 | return mState.polygonOffsetFill; |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 590 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units) |
| 594 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 595 | if (mState.polygonOffsetFactor != factor || |
| 596 | mState.polygonOffsetUnits != units) |
| 597 | { |
| 598 | mState.polygonOffsetFactor = factor; |
| 599 | mState.polygonOffsetUnits = units; |
| 600 | mPolygonOffsetStateDirty = true; |
| 601 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void Context::setSampleAlphaToCoverage(bool enabled) |
| 605 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 606 | if (mState.sampleAlphaToCoverage != enabled) |
| 607 | { |
| 608 | mState.sampleAlphaToCoverage = enabled; |
| 609 | mSampleStateDirty = true; |
| 610 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | bool Context::isSampleAlphaToCoverageEnabled() const |
| 614 | { |
| 615 | return mState.sampleAlphaToCoverage; |
| 616 | } |
| 617 | |
| 618 | void Context::setSampleCoverage(bool enabled) |
| 619 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 620 | if (mState.sampleCoverage != enabled) |
| 621 | { |
| 622 | mState.sampleCoverage = enabled; |
| 623 | mSampleStateDirty = true; |
| 624 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | bool Context::isSampleCoverageEnabled() const |
| 628 | { |
| 629 | return mState.sampleCoverage; |
| 630 | } |
| 631 | |
daniel@transgaming.com | a36f98e | 2010-05-18 18:51:09 +0000 | [diff] [blame] | 632 | void Context::setSampleCoverageParams(GLclampf value, bool invert) |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 633 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 634 | if (mState.sampleCoverageValue != value || |
| 635 | mState.sampleCoverageInvert != invert) |
| 636 | { |
| 637 | mState.sampleCoverageValue = value; |
| 638 | mState.sampleCoverageInvert = invert; |
| 639 | mSampleStateDirty = true; |
| 640 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | void Context::setScissorTest(bool enabled) |
| 644 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 645 | if (mState.scissorTest != enabled) |
| 646 | { |
| 647 | mState.scissorTest = enabled; |
| 648 | mScissorStateDirty = true; |
| 649 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | bool Context::isScissorTestEnabled() const |
| 653 | { |
| 654 | return mState.scissorTest; |
| 655 | } |
| 656 | |
| 657 | void Context::setDither(bool enabled) |
| 658 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 659 | if (mState.dither != enabled) |
| 660 | { |
| 661 | mState.dither = enabled; |
| 662 | mDitherStateDirty = true; |
| 663 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | bool Context::isDitherEnabled() const |
| 667 | { |
| 668 | return mState.dither; |
| 669 | } |
| 670 | |
| 671 | void Context::setLineWidth(GLfloat width) |
| 672 | { |
| 673 | mState.lineWidth = width; |
| 674 | } |
| 675 | |
| 676 | void Context::setGenerateMipmapHint(GLenum hint) |
| 677 | { |
| 678 | mState.generateMipmapHint = hint; |
| 679 | } |
| 680 | |
alokp@chromium.org | d303ef9 | 2010-09-09 17:30:15 +0000 | [diff] [blame] | 681 | void Context::setFragmentShaderDerivativeHint(GLenum hint) |
| 682 | { |
| 683 | mState.fragmentShaderDerivativeHint = hint; |
| 684 | // TODO: Propagate the hint to shader translator so we can write |
| 685 | // ddx, ddx_coarse, or ddx_fine depending on the hint. |
| 686 | // Ignore for now. It is valid for implementations to ignore hint. |
| 687 | } |
| 688 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 689 | void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height) |
| 690 | { |
| 691 | mState.viewportX = x; |
| 692 | mState.viewportY = y; |
| 693 | mState.viewportWidth = width; |
| 694 | mState.viewportHeight = height; |
| 695 | } |
| 696 | |
| 697 | void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height) |
| 698 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 699 | if (mState.scissorX != x || mState.scissorY != y || |
| 700 | mState.scissorWidth != width || mState.scissorHeight != height) |
| 701 | { |
| 702 | mState.scissorX = x; |
| 703 | mState.scissorY = y; |
| 704 | mState.scissorWidth = width; |
| 705 | mState.scissorHeight = height; |
| 706 | mScissorStateDirty = true; |
| 707 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void Context::setColorMask(bool red, bool green, bool blue, bool alpha) |
| 711 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 712 | if (mState.colorMaskRed != red || mState.colorMaskGreen != green || |
| 713 | mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha) |
| 714 | { |
| 715 | mState.colorMaskRed = red; |
| 716 | mState.colorMaskGreen = green; |
| 717 | mState.colorMaskBlue = blue; |
| 718 | mState.colorMaskAlpha = alpha; |
| 719 | mMaskStateDirty = true; |
| 720 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | void Context::setDepthMask(bool mask) |
| 724 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 725 | if (mState.depthMask != mask) |
| 726 | { |
| 727 | mState.depthMask = mask; |
| 728 | mMaskStateDirty = true; |
| 729 | } |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | void Context::setActiveSampler(int active) |
| 733 | { |
| 734 | mState.activeSampler = active; |
| 735 | } |
| 736 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 737 | GLuint Context::getReadFramebufferHandle() const |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 738 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 739 | return mState.readFramebuffer; |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | GLuint Context::getDrawFramebufferHandle() const |
| 743 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 744 | return mState.drawFramebuffer; |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | GLuint Context::getRenderbufferHandle() const |
| 748 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 749 | return mState.renderbuffer.id(); |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | GLuint Context::getArrayBufferHandle() const |
| 753 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 754 | return mState.arrayBuffer.id(); |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 755 | } |
| 756 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 757 | void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled) |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 758 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 759 | mState.vertexAttribute[attribNum].mArrayEnabled = enabled; |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 760 | } |
| 761 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 762 | const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum) |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 763 | { |
| 764 | return mState.vertexAttribute[attribNum]; |
| 765 | } |
| 766 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 767 | void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized, |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 768 | GLsizei stride, const void *pointer) |
| 769 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 770 | mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer); |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 771 | mState.vertexAttribute[attribNum].mSize = size; |
| 772 | mState.vertexAttribute[attribNum].mType = type; |
| 773 | mState.vertexAttribute[attribNum].mNormalized = normalized; |
| 774 | mState.vertexAttribute[attribNum].mStride = stride; |
| 775 | mState.vertexAttribute[attribNum].mPointer = pointer; |
| 776 | } |
| 777 | |
| 778 | const void *Context::getVertexAttribPointer(unsigned int attribNum) const |
| 779 | { |
| 780 | return mState.vertexAttribute[attribNum].mPointer; |
| 781 | } |
| 782 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 783 | const VertexAttributeArray &Context::getVertexAttributes() |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 784 | { |
| 785 | return mState.vertexAttribute; |
| 786 | } |
| 787 | |
| 788 | void Context::setPackAlignment(GLint alignment) |
| 789 | { |
| 790 | mState.packAlignment = alignment; |
| 791 | } |
| 792 | |
| 793 | GLint Context::getPackAlignment() const |
| 794 | { |
| 795 | return mState.packAlignment; |
| 796 | } |
| 797 | |
| 798 | void Context::setUnpackAlignment(GLint alignment) |
| 799 | { |
| 800 | mState.unpackAlignment = alignment; |
| 801 | } |
| 802 | |
| 803 | GLint Context::getUnpackAlignment() const |
| 804 | { |
| 805 | return mState.unpackAlignment; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 806 | } |
| 807 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 808 | GLuint Context::createBuffer() |
| 809 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 810 | return mResourceManager->createBuffer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 811 | } |
| 812 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 813 | GLuint Context::createProgram() |
| 814 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 815 | return mResourceManager->createProgram(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 816 | } |
| 817 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 818 | GLuint Context::createShader(GLenum type) |
| 819 | { |
| 820 | return mResourceManager->createShader(type); |
| 821 | } |
| 822 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 823 | GLuint Context::createTexture() |
| 824 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 825 | return mResourceManager->createTexture(); |
| 826 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 827 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 828 | GLuint Context::createRenderbuffer() |
| 829 | { |
| 830 | return mResourceManager->createRenderbuffer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 831 | } |
| 832 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 833 | // Returns an unused framebuffer name |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 834 | GLuint Context::createFramebuffer() |
| 835 | { |
benvanik@google.com | 1a23334 | 2011-04-28 19:44:39 +0000 | [diff] [blame] | 836 | GLuint handle = mFramebufferHandleAllocator.allocate(); |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 837 | |
| 838 | mFramebufferMap[handle] = NULL; |
| 839 | |
| 840 | return handle; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 841 | } |
| 842 | |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 843 | GLuint Context::createFence() |
| 844 | { |
benvanik@google.com | 1a23334 | 2011-04-28 19:44:39 +0000 | [diff] [blame] | 845 | GLuint handle = mFenceHandleAllocator.allocate(); |
daniel@transgaming.com | fe20888 | 2010-09-01 15:47:57 +0000 | [diff] [blame] | 846 | |
| 847 | mFenceMap[handle] = new Fence; |
| 848 | |
| 849 | return handle; |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 850 | } |
| 851 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 852 | void Context::deleteBuffer(GLuint buffer) |
| 853 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 854 | if (mResourceManager->getBuffer(buffer)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 855 | { |
| 856 | detachBuffer(buffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 857 | } |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 858 | |
| 859 | mResourceManager->deleteBuffer(buffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | void Context::deleteShader(GLuint shader) |
| 863 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 864 | mResourceManager->deleteShader(shader); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | void Context::deleteProgram(GLuint program) |
| 868 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 869 | mResourceManager->deleteProgram(program); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void Context::deleteTexture(GLuint texture) |
| 873 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 874 | if (mResourceManager->getTexture(texture)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 875 | { |
| 876 | detachTexture(texture); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 877 | } |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 878 | |
| 879 | mResourceManager->deleteTexture(texture); |
| 880 | } |
| 881 | |
| 882 | void Context::deleteRenderbuffer(GLuint renderbuffer) |
| 883 | { |
| 884 | if (mResourceManager->getRenderbuffer(renderbuffer)) |
| 885 | { |
| 886 | detachRenderbuffer(renderbuffer); |
| 887 | } |
| 888 | |
| 889 | mResourceManager->deleteRenderbuffer(renderbuffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | void Context::deleteFramebuffer(GLuint framebuffer) |
| 893 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 894 | FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer); |
| 895 | |
| 896 | if (framebufferObject != mFramebufferMap.end()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 897 | { |
| 898 | detachFramebuffer(framebuffer); |
apatrick@chromium.org | 5525502 | 2010-09-11 02:12:47 +0000 | [diff] [blame] | 899 | |
benvanik@google.com | 1a23334 | 2011-04-28 19:44:39 +0000 | [diff] [blame] | 900 | mFramebufferHandleAllocator.release(framebufferObject->first); |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 901 | delete framebufferObject->second; |
| 902 | mFramebufferMap.erase(framebufferObject); |
| 903 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 904 | } |
daniel@transgaming.com | fe20888 | 2010-09-01 15:47:57 +0000 | [diff] [blame] | 905 | |
| 906 | void Context::deleteFence(GLuint fence) |
| 907 | { |
| 908 | FenceMap::iterator fenceObject = mFenceMap.find(fence); |
| 909 | |
| 910 | if (fenceObject != mFenceMap.end()) |
| 911 | { |
benvanik@google.com | 1a23334 | 2011-04-28 19:44:39 +0000 | [diff] [blame] | 912 | mFenceHandleAllocator.release(fenceObject->first); |
daniel@transgaming.com | fe20888 | 2010-09-01 15:47:57 +0000 | [diff] [blame] | 913 | delete fenceObject->second; |
| 914 | mFenceMap.erase(fenceObject); |
| 915 | } |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 916 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 917 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 918 | Buffer *Context::getBuffer(GLuint handle) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 919 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 920 | return mResourceManager->getBuffer(handle); |
| 921 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 922 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 923 | Shader *Context::getShader(GLuint handle) |
| 924 | { |
| 925 | return mResourceManager->getShader(handle); |
| 926 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 927 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 928 | Program *Context::getProgram(GLuint handle) |
| 929 | { |
| 930 | return mResourceManager->getProgram(handle); |
| 931 | } |
| 932 | |
| 933 | Texture *Context::getTexture(GLuint handle) |
| 934 | { |
| 935 | return mResourceManager->getTexture(handle); |
| 936 | } |
| 937 | |
| 938 | Renderbuffer *Context::getRenderbuffer(GLuint handle) |
| 939 | { |
| 940 | return mResourceManager->getRenderbuffer(handle); |
| 941 | } |
| 942 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 943 | Framebuffer *Context::getReadFramebuffer() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 944 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 945 | return getFramebuffer(mState.readFramebuffer); |
| 946 | } |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 947 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 948 | Framebuffer *Context::getDrawFramebuffer() |
| 949 | { |
| 950 | return getFramebuffer(mState.drawFramebuffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void Context::bindArrayBuffer(unsigned int buffer) |
| 954 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 955 | mResourceManager->checkBufferAllocation(buffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 956 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 957 | mState.arrayBuffer.set(getBuffer(buffer)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | void Context::bindElementArrayBuffer(unsigned int buffer) |
| 961 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 962 | mResourceManager->checkBufferAllocation(buffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 963 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 964 | mState.elementArrayBuffer.set(getBuffer(buffer)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | void Context::bindTexture2D(GLuint texture) |
| 968 | { |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 969 | mResourceManager->checkTextureAllocation(texture, TEXTURE_2D); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 970 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 971 | mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | void Context::bindTextureCubeMap(GLuint texture) |
| 975 | { |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 976 | mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 977 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 978 | mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 979 | } |
| 980 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 981 | void Context::bindReadFramebuffer(GLuint framebuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 982 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 983 | if (!getFramebuffer(framebuffer)) |
| 984 | { |
| 985 | mFramebufferMap[framebuffer] = new Framebuffer(); |
| 986 | } |
| 987 | |
| 988 | mState.readFramebuffer = framebuffer; |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | void Context::bindDrawFramebuffer(GLuint framebuffer) |
| 992 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 993 | if (!getFramebuffer(framebuffer)) |
| 994 | { |
| 995 | mFramebufferMap[framebuffer] = new Framebuffer(); |
| 996 | } |
| 997 | |
| 998 | mState.drawFramebuffer = framebuffer; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void Context::bindRenderbuffer(GLuint renderbuffer) |
| 1002 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1003 | mResourceManager->checkRenderbufferAllocation(renderbuffer); |
| 1004 | |
| 1005 | mState.renderbuffer.set(getRenderbuffer(renderbuffer)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | void Context::useProgram(GLuint program) |
| 1009 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1010 | GLuint priorProgram = mState.currentProgram; |
| 1011 | mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged. |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 1012 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1013 | if (priorProgram != program) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1014 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1015 | Program *newProgram = mResourceManager->getProgram(program); |
| 1016 | Program *oldProgram = mResourceManager->getProgram(priorProgram); |
| 1017 | |
| 1018 | if (newProgram) |
| 1019 | { |
| 1020 | newProgram->addRef(); |
| 1021 | } |
| 1022 | |
| 1023 | if (oldProgram) |
| 1024 | { |
| 1025 | oldProgram->release(); |
| 1026 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1027 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | void Context::setFramebufferZero(Framebuffer *buffer) |
| 1031 | { |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 1032 | delete mFramebufferMap[0]; |
| 1033 | mFramebufferMap[0] = buffer; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1036 | void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1037 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1038 | Renderbuffer *renderbufferObject = mState.renderbuffer.get(); |
| 1039 | renderbufferObject->setStorage(renderbuffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 1042 | Framebuffer *Context::getFramebuffer(unsigned int handle) |
| 1043 | { |
| 1044 | FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle); |
| 1045 | |
| 1046 | if (framebuffer == mFramebufferMap.end()) |
| 1047 | { |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | return framebuffer->second; |
| 1053 | } |
| 1054 | } |
| 1055 | |
daniel@transgaming.com | fe20888 | 2010-09-01 15:47:57 +0000 | [diff] [blame] | 1056 | Fence *Context::getFence(unsigned int handle) |
| 1057 | { |
| 1058 | FenceMap::iterator fence = mFenceMap.find(handle); |
| 1059 | |
| 1060 | if (fence == mFenceMap.end()) |
| 1061 | { |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | return fence->second; |
| 1067 | } |
| 1068 | } |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 1069 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1070 | Buffer *Context::getArrayBuffer() |
| 1071 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1072 | return mState.arrayBuffer.get(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | Buffer *Context::getElementArrayBuffer() |
| 1076 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1077 | return mState.elementArrayBuffer.get(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | Program *Context::getCurrentProgram() |
| 1081 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1082 | return mResourceManager->getProgram(mState.currentProgram); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | Texture2D *Context::getTexture2D() |
| 1086 | { |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 1087 | return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | TextureCubeMap *Context::getTextureCubeMap() |
| 1091 | { |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 1092 | return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 1095 | Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1096 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1097 | GLuint texid = mState.samplerTexture[type][sampler].id(); |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 1098 | |
daniel@transgaming.com | a5a8a0a | 2010-11-19 14:55:32 +0000 | [diff] [blame] | 1099 | if (texid == 0) // Special case: 0 refers to different initial textures based on the target |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 1100 | { |
| 1101 | switch (type) |
| 1102 | { |
| 1103 | default: UNREACHABLE(); |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 1104 | case TEXTURE_2D: return mTexture2DZero.get(); |
| 1105 | case TEXTURE_CUBE: return mTextureCubeMapZero.get(); |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1109 | return mState.samplerTexture[type][sampler].get(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1112 | bool Context::getBooleanv(GLenum pname, GLboolean *params) |
| 1113 | { |
| 1114 | switch (pname) |
| 1115 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1116 | case GL_SHADER_COMPILER: *params = GL_TRUE; break; |
| 1117 | case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break; |
| 1118 | case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1119 | case GL_COLOR_WRITEMASK: |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1120 | params[0] = mState.colorMaskRed; |
| 1121 | params[1] = mState.colorMaskGreen; |
| 1122 | params[2] = mState.colorMaskBlue; |
| 1123 | params[3] = mState.colorMaskAlpha; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1124 | break; |
daniel@transgaming.com | 9d7fc1d | 2010-10-27 15:49:42 +0000 | [diff] [blame] | 1125 | case GL_CULL_FACE: *params = mState.cullFace; break; |
| 1126 | case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break; |
| 1127 | case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break; |
| 1128 | case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break; |
| 1129 | case GL_SCISSOR_TEST: *params = mState.scissorTest; break; |
| 1130 | case GL_STENCIL_TEST: *params = mState.stencilTest; break; |
| 1131 | case GL_DEPTH_TEST: *params = mState.depthTest; break; |
| 1132 | case GL_BLEND: *params = mState.blend; break; |
| 1133 | case GL_DITHER: *params = mState.dither; break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1134 | default: |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
| 1141 | bool Context::getFloatv(GLenum pname, GLfloat *params) |
| 1142 | { |
| 1143 | // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation |
| 1144 | // because it is stored as a float, despite the fact that the GL ES 2.0 spec names |
| 1145 | // GetIntegerv as its native query function. As it would require conversion in any |
| 1146 | // case, this should make no difference to the calling application. |
| 1147 | switch (pname) |
| 1148 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1149 | case GL_LINE_WIDTH: *params = mState.lineWidth; break; |
| 1150 | case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break; |
| 1151 | case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break; |
| 1152 | case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break; |
| 1153 | case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1154 | case GL_ALIASED_LINE_WIDTH_RANGE: |
| 1155 | params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN; |
| 1156 | params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX; |
| 1157 | break; |
| 1158 | case GL_ALIASED_POINT_SIZE_RANGE: |
| 1159 | params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN; |
daniel@transgaming.com | be5a086 | 2010-07-28 19:20:37 +0000 | [diff] [blame] | 1160 | params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1161 | break; |
| 1162 | case GL_DEPTH_RANGE: |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1163 | params[0] = mState.zNear; |
| 1164 | params[1] = mState.zFar; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | case GL_COLOR_CLEAR_VALUE: |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1167 | params[0] = mState.colorClearValue.red; |
| 1168 | params[1] = mState.colorClearValue.green; |
| 1169 | params[2] = mState.colorClearValue.blue; |
| 1170 | params[3] = mState.colorClearValue.alpha; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1171 | break; |
daniel@transgaming.com | c164135 | 2010-04-26 15:33:36 +0000 | [diff] [blame] | 1172 | case GL_BLEND_COLOR: |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1173 | params[0] = mState.blendColor.red; |
| 1174 | params[1] = mState.blendColor.green; |
| 1175 | params[2] = mState.blendColor.blue; |
| 1176 | params[3] = mState.blendColor.alpha; |
daniel@transgaming.com | c164135 | 2010-04-26 15:33:36 +0000 | [diff] [blame] | 1177 | break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1178 | default: |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
| 1182 | return true; |
| 1183 | } |
| 1184 | |
| 1185 | bool Context::getIntegerv(GLenum pname, GLint *params) |
| 1186 | { |
| 1187 | // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation |
| 1188 | // because it is stored as a float, despite the fact that the GL ES 2.0 spec names |
| 1189 | // GetIntegerv as its native query function. As it would require conversion in any |
| 1190 | // case, this should make no difference to the calling application. You may find it in |
| 1191 | // Context::getFloatv. |
| 1192 | switch (pname) |
| 1193 | { |
| 1194 | case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break; |
| 1195 | case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break; |
daniel@transgaming.com | 396c643 | 2010-11-26 16:26:12 +0000 | [diff] [blame] | 1196 | case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break; |
daniel@transgaming.com | af29cac | 2011-05-11 15:36:31 +0000 | [diff] [blame] | 1197 | case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break; |
| 1198 | case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1199 | case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break; |
daniel@transgaming.com | 458da14 | 2010-11-28 02:03:02 +0000 | [diff] [blame] | 1200 | case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break; |
daniel@transgaming.com | 5d752f2 | 2010-10-07 13:37:20 +0000 | [diff] [blame] | 1201 | case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break; |
daniel@transgaming.com | b28a23b | 2010-05-20 19:18:06 +0000 | [diff] [blame] | 1202 | case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break; |
daniel@transgaming.com | b28a23b | 2010-05-20 19:18:06 +0000 | [diff] [blame] | 1203 | case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1204 | case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break; |
| 1205 | case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break; |
daniel@transgaming.com | 9d7fc1d | 2010-10-27 15:49:42 +0000 | [diff] [blame] | 1206 | //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE |
| 1207 | case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break; |
| 1208 | case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break; |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 1209 | case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break; |
daniel@transgaming.com | b28a23b | 2010-05-20 19:18:06 +0000 | [diff] [blame] | 1210 | case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break; |
| 1211 | case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break; |
| 1212 | case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break; |
| 1213 | case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break; |
alokp@chromium.org | d303ef9 | 2010-09-09 17:30:15 +0000 | [diff] [blame] | 1214 | case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break; |
daniel@transgaming.com | b28a23b | 2010-05-20 19:18:06 +0000 | [diff] [blame] | 1215 | case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break; |
| 1216 | case GL_STENCIL_FUNC: *params = mState.stencilFunc; break; |
| 1217 | case GL_STENCIL_REF: *params = mState.stencilRef; break; |
| 1218 | case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break; |
| 1219 | case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break; |
| 1220 | case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break; |
| 1221 | case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break; |
| 1222 | case GL_STENCIL_FAIL: *params = mState.stencilFail; break; |
| 1223 | case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break; |
| 1224 | case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break; |
| 1225 | case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break; |
| 1226 | case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break; |
| 1227 | case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break; |
| 1228 | case GL_DEPTH_FUNC: *params = mState.depthFunc; break; |
| 1229 | case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break; |
| 1230 | case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break; |
| 1231 | case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break; |
| 1232 | case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break; |
| 1233 | case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break; |
| 1234 | case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break; |
| 1235 | case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break; |
| 1236 | case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break; |
| 1237 | case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break; |
| 1238 | case GL_SUBPIXEL_BITS: *params = 4; break; |
daniel@transgaming.com | 5d752f2 | 2010-10-07 13:37:20 +0000 | [diff] [blame] | 1239 | case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break; |
| 1240 | case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break; |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 1241 | case GL_NUM_COMPRESSED_TEXTURE_FORMATS: |
| 1242 | { |
| 1243 | if (supportsCompressedTextures()) |
| 1244 | { |
| 1245 | // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and |
| 1246 | // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported |
| 1247 | *params = 2; |
| 1248 | } |
| 1249 | else |
| 1250 | { |
| 1251 | *params = 0; |
| 1252 | } |
| 1253 | } |
| 1254 | break; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1255 | case GL_MAX_SAMPLES_ANGLE: |
| 1256 | { |
| 1257 | GLsizei maxSamples = getMaxSupportedSamples(); |
| 1258 | if (maxSamples != 0) |
| 1259 | { |
| 1260 | *params = maxSamples; |
| 1261 | } |
| 1262 | else |
| 1263 | { |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
| 1267 | break; |
| 1268 | } |
| 1269 | case GL_SAMPLE_BUFFERS: |
| 1270 | case GL_SAMPLES: |
| 1271 | { |
| 1272 | gl::Framebuffer *framebuffer = getDrawFramebuffer(); |
| 1273 | if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE) |
| 1274 | { |
| 1275 | switch (pname) |
| 1276 | { |
| 1277 | case GL_SAMPLE_BUFFERS: |
| 1278 | if (framebuffer->getSamples() != 0) |
| 1279 | { |
| 1280 | *params = 1; |
| 1281 | } |
| 1282 | else |
| 1283 | { |
| 1284 | *params = 0; |
| 1285 | } |
| 1286 | break; |
| 1287 | case GL_SAMPLES: |
| 1288 | *params = framebuffer->getSamples(); |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | *params = 0; |
| 1295 | } |
| 1296 | } |
| 1297 | break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1298 | case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break; |
| 1299 | case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break; |
| 1300 | case GL_MAX_VIEWPORT_DIMS: |
| 1301 | { |
daniel@transgaming.com | 5d752f2 | 2010-10-07 13:37:20 +0000 | [diff] [blame] | 1302 | int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension()); |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1303 | params[0] = maxDimension; |
| 1304 | params[1] = maxDimension; |
| 1305 | } |
| 1306 | break; |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 1307 | case GL_COMPRESSED_TEXTURE_FORMATS: |
| 1308 | { |
| 1309 | if (supportsCompressedTextures()) |
| 1310 | { |
| 1311 | params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; |
| 1312 | params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
| 1313 | } |
| 1314 | } |
| 1315 | break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1316 | case GL_VIEWPORT: |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1317 | params[0] = mState.viewportX; |
| 1318 | params[1] = mState.viewportY; |
| 1319 | params[2] = mState.viewportWidth; |
| 1320 | params[3] = mState.viewportHeight; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | case GL_SCISSOR_BOX: |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1323 | params[0] = mState.scissorX; |
| 1324 | params[1] = mState.scissorY; |
| 1325 | params[2] = mState.scissorWidth; |
| 1326 | params[3] = mState.scissorHeight; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1327 | break; |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1328 | case GL_CULL_FACE_MODE: *params = mState.cullMode; break; |
| 1329 | case GL_FRONT_FACE: *params = mState.frontFace; break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1330 | case GL_RED_BITS: |
| 1331 | case GL_GREEN_BITS: |
| 1332 | case GL_BLUE_BITS: |
| 1333 | case GL_ALPHA_BITS: |
| 1334 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1335 | gl::Framebuffer *framebuffer = getDrawFramebuffer(); |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1336 | gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 1337 | |
| 1338 | if (colorbuffer) |
| 1339 | { |
| 1340 | switch (pname) |
| 1341 | { |
| 1342 | case GL_RED_BITS: *params = colorbuffer->getRedSize(); break; |
| 1343 | case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break; |
| 1344 | case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break; |
| 1345 | case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break; |
| 1346 | } |
| 1347 | } |
| 1348 | else |
| 1349 | { |
| 1350 | *params = 0; |
| 1351 | } |
| 1352 | } |
| 1353 | break; |
| 1354 | case GL_DEPTH_BITS: |
| 1355 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1356 | gl::Framebuffer *framebuffer = getDrawFramebuffer(); |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1357 | gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer(); |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1358 | |
| 1359 | if (depthbuffer) |
| 1360 | { |
| 1361 | *params = depthbuffer->getDepthSize(); |
| 1362 | } |
| 1363 | else |
| 1364 | { |
| 1365 | *params = 0; |
| 1366 | } |
| 1367 | } |
| 1368 | break; |
| 1369 | case GL_STENCIL_BITS: |
| 1370 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1371 | gl::Framebuffer *framebuffer = getDrawFramebuffer(); |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1372 | gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer(); |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1373 | |
| 1374 | if (stencilbuffer) |
| 1375 | { |
| 1376 | *params = stencilbuffer->getStencilSize(); |
| 1377 | } |
| 1378 | else |
| 1379 | { |
| 1380 | *params = 0; |
| 1381 | } |
| 1382 | } |
| 1383 | break; |
| 1384 | case GL_TEXTURE_BINDING_2D: |
| 1385 | { |
daniel@transgaming.com | 3f74c7a | 2011-05-11 15:36:51 +0000 | [diff] [blame] | 1386 | if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1) |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1387 | { |
| 1388 | error(GL_INVALID_OPERATION); |
| 1389 | return false; |
| 1390 | } |
| 1391 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 1392 | *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id(); |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1393 | } |
| 1394 | break; |
| 1395 | case GL_TEXTURE_BINDING_CUBE_MAP: |
| 1396 | { |
daniel@transgaming.com | 3f74c7a | 2011-05-11 15:36:51 +0000 | [diff] [blame] | 1397 | if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1) |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1398 | { |
| 1399 | error(GL_INVALID_OPERATION); |
| 1400 | return false; |
| 1401 | } |
| 1402 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 1403 | *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id(); |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1404 | } |
| 1405 | break; |
| 1406 | default: |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
| 1413 | bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) |
| 1414 | { |
| 1415 | // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation |
| 1416 | // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due |
| 1417 | // to the fact that it is stored internally as a float, and so would require conversion |
| 1418 | // if returned from Context::getIntegerv. Since this conversion is already implemented |
| 1419 | // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we |
| 1420 | // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling |
| 1421 | // application. |
| 1422 | switch (pname) |
| 1423 | { |
| 1424 | case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ |
| 1425 | case GL_SHADER_BINARY_FORMATS: |
| 1426 | { |
| 1427 | *type = GL_INT; |
| 1428 | *numParams = 0; |
| 1429 | } |
| 1430 | break; |
| 1431 | case GL_MAX_VERTEX_ATTRIBS: |
| 1432 | case GL_MAX_VERTEX_UNIFORM_VECTORS: |
| 1433 | case GL_MAX_VARYING_VECTORS: |
| 1434 | case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: |
| 1435 | case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: |
| 1436 | case GL_MAX_TEXTURE_IMAGE_UNITS: |
| 1437 | case GL_MAX_FRAGMENT_UNIFORM_VECTORS: |
| 1438 | case GL_MAX_RENDERBUFFER_SIZE: |
| 1439 | case GL_NUM_SHADER_BINARY_FORMATS: |
| 1440 | case GL_NUM_COMPRESSED_TEXTURE_FORMATS: |
| 1441 | case GL_ARRAY_BUFFER_BINDING: |
| 1442 | case GL_FRAMEBUFFER_BINDING: |
| 1443 | case GL_RENDERBUFFER_BINDING: |
| 1444 | case GL_CURRENT_PROGRAM: |
| 1445 | case GL_PACK_ALIGNMENT: |
| 1446 | case GL_UNPACK_ALIGNMENT: |
| 1447 | case GL_GENERATE_MIPMAP_HINT: |
alokp@chromium.org | d303ef9 | 2010-09-09 17:30:15 +0000 | [diff] [blame] | 1448 | case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1449 | case GL_RED_BITS: |
| 1450 | case GL_GREEN_BITS: |
| 1451 | case GL_BLUE_BITS: |
| 1452 | case GL_ALPHA_BITS: |
| 1453 | case GL_DEPTH_BITS: |
| 1454 | case GL_STENCIL_BITS: |
| 1455 | case GL_ELEMENT_ARRAY_BUFFER_BINDING: |
| 1456 | case GL_CULL_FACE_MODE: |
| 1457 | case GL_FRONT_FACE: |
| 1458 | case GL_ACTIVE_TEXTURE: |
| 1459 | case GL_STENCIL_FUNC: |
| 1460 | case GL_STENCIL_VALUE_MASK: |
| 1461 | case GL_STENCIL_REF: |
| 1462 | case GL_STENCIL_FAIL: |
| 1463 | case GL_STENCIL_PASS_DEPTH_FAIL: |
| 1464 | case GL_STENCIL_PASS_DEPTH_PASS: |
| 1465 | case GL_STENCIL_BACK_FUNC: |
| 1466 | case GL_STENCIL_BACK_VALUE_MASK: |
| 1467 | case GL_STENCIL_BACK_REF: |
| 1468 | case GL_STENCIL_BACK_FAIL: |
| 1469 | case GL_STENCIL_BACK_PASS_DEPTH_FAIL: |
| 1470 | case GL_STENCIL_BACK_PASS_DEPTH_PASS: |
| 1471 | case GL_DEPTH_FUNC: |
| 1472 | case GL_BLEND_SRC_RGB: |
| 1473 | case GL_BLEND_SRC_ALPHA: |
| 1474 | case GL_BLEND_DST_RGB: |
| 1475 | case GL_BLEND_DST_ALPHA: |
| 1476 | case GL_BLEND_EQUATION_RGB: |
| 1477 | case GL_BLEND_EQUATION_ALPHA: |
| 1478 | case GL_STENCIL_WRITEMASK: |
| 1479 | case GL_STENCIL_BACK_WRITEMASK: |
| 1480 | case GL_STENCIL_CLEAR_VALUE: |
| 1481 | case GL_SUBPIXEL_BITS: |
| 1482 | case GL_MAX_TEXTURE_SIZE: |
| 1483 | case GL_MAX_CUBE_MAP_TEXTURE_SIZE: |
| 1484 | case GL_SAMPLE_BUFFERS: |
| 1485 | case GL_SAMPLES: |
| 1486 | case GL_IMPLEMENTATION_COLOR_READ_TYPE: |
| 1487 | case GL_IMPLEMENTATION_COLOR_READ_FORMAT: |
| 1488 | case GL_TEXTURE_BINDING_2D: |
| 1489 | case GL_TEXTURE_BINDING_CUBE_MAP: |
| 1490 | { |
| 1491 | *type = GL_INT; |
| 1492 | *numParams = 1; |
| 1493 | } |
| 1494 | break; |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 1495 | case GL_MAX_SAMPLES_ANGLE: |
| 1496 | { |
| 1497 | if (getMaxSupportedSamples() != 0) |
| 1498 | { |
| 1499 | *type = GL_INT; |
| 1500 | *numParams = 1; |
| 1501 | } |
| 1502 | else |
| 1503 | { |
| 1504 | return false; |
| 1505 | } |
| 1506 | } |
| 1507 | break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1508 | case GL_MAX_VIEWPORT_DIMS: |
| 1509 | { |
| 1510 | *type = GL_INT; |
| 1511 | *numParams = 2; |
| 1512 | } |
| 1513 | break; |
| 1514 | case GL_VIEWPORT: |
| 1515 | case GL_SCISSOR_BOX: |
| 1516 | { |
| 1517 | *type = GL_INT; |
| 1518 | *numParams = 4; |
| 1519 | } |
| 1520 | break; |
| 1521 | case GL_SHADER_COMPILER: |
| 1522 | case GL_SAMPLE_COVERAGE_INVERT: |
| 1523 | case GL_DEPTH_WRITEMASK: |
daniel@transgaming.com | 79f6677 | 2010-04-13 03:26:09 +0000 | [diff] [blame] | 1524 | case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled, |
| 1525 | case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries. |
| 1526 | case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural |
| 1527 | case GL_SAMPLE_COVERAGE: |
| 1528 | case GL_SCISSOR_TEST: |
| 1529 | case GL_STENCIL_TEST: |
| 1530 | case GL_DEPTH_TEST: |
| 1531 | case GL_BLEND: |
| 1532 | case GL_DITHER: |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1533 | { |
| 1534 | *type = GL_BOOL; |
| 1535 | *numParams = 1; |
| 1536 | } |
| 1537 | break; |
| 1538 | case GL_COLOR_WRITEMASK: |
| 1539 | { |
| 1540 | *type = GL_BOOL; |
| 1541 | *numParams = 4; |
| 1542 | } |
| 1543 | break; |
| 1544 | case GL_POLYGON_OFFSET_FACTOR: |
| 1545 | case GL_POLYGON_OFFSET_UNITS: |
| 1546 | case GL_SAMPLE_COVERAGE_VALUE: |
| 1547 | case GL_DEPTH_CLEAR_VALUE: |
| 1548 | case GL_LINE_WIDTH: |
| 1549 | { |
| 1550 | *type = GL_FLOAT; |
| 1551 | *numParams = 1; |
| 1552 | } |
| 1553 | break; |
| 1554 | case GL_ALIASED_LINE_WIDTH_RANGE: |
| 1555 | case GL_ALIASED_POINT_SIZE_RANGE: |
| 1556 | case GL_DEPTH_RANGE: |
| 1557 | { |
| 1558 | *type = GL_FLOAT; |
| 1559 | *numParams = 2; |
| 1560 | } |
| 1561 | break; |
| 1562 | case GL_COLOR_CLEAR_VALUE: |
daniel@transgaming.com | c164135 | 2010-04-26 15:33:36 +0000 | [diff] [blame] | 1563 | case GL_BLEND_COLOR: |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1564 | { |
| 1565 | *type = GL_FLOAT; |
| 1566 | *numParams = 4; |
| 1567 | } |
| 1568 | break; |
| 1569 | default: |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
| 1573 | return true; |
| 1574 | } |
| 1575 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1576 | // Applies the render target surface, depth stencil surface, viewport rectangle and |
| 1577 | // scissor rectangle to the Direct3D 9 device |
| 1578 | bool Context::applyRenderTarget(bool ignoreViewport) |
| 1579 | { |
| 1580 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 1581 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1582 | Framebuffer *framebufferObject = getDrawFramebuffer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1583 | |
| 1584 | if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 1585 | { |
daniel@transgaming.com | b5a3a6b | 2011-03-21 16:38:46 +0000 | [diff] [blame] | 1586 | return error(GL_INVALID_FRAMEBUFFER_OPERATION, false); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget(); |
daniel@transgaming.com | d36c6a0 | 2010-08-31 12:15:09 +0000 | [diff] [blame] | 1590 | |
| 1591 | if (!renderTarget) |
| 1592 | { |
| 1593 | return false; // Context must be lost |
| 1594 | } |
| 1595 | |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1596 | IDirect3DSurface9 *depthStencil = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1597 | |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 1598 | unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial(); |
| 1599 | if (renderTargetSerial != mAppliedRenderTargetSerial) |
| 1600 | { |
| 1601 | device->SetRenderTarget(0, renderTarget); |
| 1602 | mAppliedRenderTargetSerial = renderTargetSerial; |
daniel@transgaming.com | bc3699d | 2010-08-05 14:48:49 +0000 | [diff] [blame] | 1603 | mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets. |
daniel@transgaming.com | 092bd48 | 2010-05-12 03:39:36 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1606 | unsigned int depthbufferSerial = 0; |
| 1607 | unsigned int stencilbufferSerial = 0; |
| 1608 | if (framebufferObject->getDepthbufferType() != GL_NONE) |
| 1609 | { |
| 1610 | depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil(); |
apatrick@chromium.org | b2bdd06 | 2010-10-05 02:24:30 +0000 | [diff] [blame] | 1611 | if (!depthStencil) |
| 1612 | { |
| 1613 | ERR("Depth stencil pointer unexpectedly null."); |
| 1614 | return false; |
| 1615 | } |
| 1616 | |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1617 | depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial(); |
| 1618 | } |
| 1619 | else if (framebufferObject->getStencilbufferType() != GL_NONE) |
| 1620 | { |
| 1621 | depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil(); |
apatrick@chromium.org | b2bdd06 | 2010-10-05 02:24:30 +0000 | [diff] [blame] | 1622 | if (!depthStencil) |
| 1623 | { |
| 1624 | ERR("Depth stencil pointer unexpectedly null."); |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1628 | stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial(); |
| 1629 | } |
| 1630 | |
| 1631 | if (depthbufferSerial != mAppliedDepthbufferSerial || |
apatrick@chromium.org | 85dc42b | 2010-09-14 03:10:08 +0000 | [diff] [blame] | 1632 | stencilbufferSerial != mAppliedStencilbufferSerial || |
vangelis@chromium.org | cf66ebb | 2010-09-14 22:15:43 +0000 | [diff] [blame] | 1633 | !mDepthStencilInitialized) |
daniel@transgaming.com | 339ae70 | 2010-05-12 03:40:20 +0000 | [diff] [blame] | 1634 | { |
| 1635 | device->SetDepthStencilSurface(depthStencil); |
| 1636 | mAppliedDepthbufferSerial = depthbufferSerial; |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 1637 | mAppliedStencilbufferSerial = stencilbufferSerial; |
vangelis@chromium.org | cf66ebb | 2010-09-14 22:15:43 +0000 | [diff] [blame] | 1638 | mDepthStencilInitialized = true; |
daniel@transgaming.com | 339ae70 | 2010-05-12 03:40:20 +0000 | [diff] [blame] | 1639 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1640 | |
| 1641 | D3DVIEWPORT9 viewport; |
| 1642 | D3DSURFACE_DESC desc; |
| 1643 | renderTarget->GetDesc(&desc); |
| 1644 | |
daniel@transgaming.com | 996675c | 2010-11-17 13:06:29 +0000 | [diff] [blame] | 1645 | float zNear = clamp01(mState.zNear); |
| 1646 | float zFar = clamp01(mState.zFar); |
| 1647 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1648 | if (ignoreViewport) |
| 1649 | { |
| 1650 | viewport.X = 0; |
| 1651 | viewport.Y = 0; |
| 1652 | viewport.Width = desc.Width; |
| 1653 | viewport.Height = desc.Height; |
| 1654 | viewport.MinZ = 0.0f; |
| 1655 | viewport.MaxZ = 1.0f; |
| 1656 | } |
| 1657 | else |
| 1658 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1659 | RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height); |
| 1660 | viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width)); |
| 1661 | viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height)); |
| 1662 | viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X)); |
| 1663 | viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y)); |
daniel@transgaming.com | 996675c | 2010-11-17 13:06:29 +0000 | [diff] [blame] | 1664 | viewport.MinZ = zNear; |
| 1665 | viewport.MaxZ = zFar; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
daniel@transgaming.com | baeb8c5 | 2010-05-05 18:50:39 +0000 | [diff] [blame] | 1668 | if (viewport.Width <= 0 || viewport.Height <= 0) |
| 1669 | { |
| 1670 | return false; // Nothing to render |
| 1671 | } |
| 1672 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1673 | device->SetViewport(&viewport); |
| 1674 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1675 | if (mScissorStateDirty) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1676 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1677 | if (mState.scissorTest) |
| 1678 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1679 | RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height); |
| 1680 | rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width)); |
| 1681 | rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height)); |
| 1682 | rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width)); |
| 1683 | rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height)); |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1684 | device->SetScissorRect(&rect); |
| 1685 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); |
| 1686 | } |
| 1687 | else |
| 1688 | { |
| 1689 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); |
| 1690 | } |
daniel@transgaming.com | bc3699d | 2010-08-05 14:48:49 +0000 | [diff] [blame] | 1691 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1692 | mScissorStateDirty = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1695 | if (mState.currentProgram) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1696 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1697 | Program *programObject = getCurrentProgram(); |
| 1698 | |
daniel@transgaming.com | 91fd1de | 2010-05-18 18:51:40 +0000 | [diff] [blame] | 1699 | GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation(); |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1700 | GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height}; |
daniel@transgaming.com | 3175496 | 2010-11-28 02:02:52 +0000 | [diff] [blame] | 1701 | programObject->setUniform2fv(halfPixelSize, 1, xy); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 1702 | |
daniel@transgaming.com | 3175496 | 2010-11-28 02:02:52 +0000 | [diff] [blame] | 1703 | GLint viewport = programObject->getDxViewportLocation(); |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 1704 | GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f, |
| 1705 | (float)mState.viewportX + mState.viewportWidth / 2.0f, |
| 1706 | (float)mState.viewportY + mState.viewportHeight / 2.0f}; |
daniel@transgaming.com | 3175496 | 2010-11-28 02:02:52 +0000 | [diff] [blame] | 1707 | programObject->setUniform4fv(viewport, 1, whxy); |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 1708 | |
daniel@transgaming.com | 91fd1de | 2010-05-18 18:51:40 +0000 | [diff] [blame] | 1709 | GLint depth = programObject->getDxDepthLocation(); |
daniel@transgaming.com | 996675c | 2010-11-17 13:06:29 +0000 | [diff] [blame] | 1710 | GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f}; |
daniel@transgaming.com | 3175496 | 2010-11-28 02:02:52 +0000 | [diff] [blame] | 1711 | programObject->setUniform2fv(depth, 1, dz); |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 1712 | |
daniel@transgaming.com | 3175496 | 2010-11-28 02:02:52 +0000 | [diff] [blame] | 1713 | GLint depthRange = programObject->getDxDepthRangeLocation(); |
| 1714 | GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear}; |
| 1715 | programObject->setUniform3fv(depthRange, 1, nearFarDiff); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | return true; |
| 1719 | } |
| 1720 | |
| 1721 | // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 1722 | void Context::applyState(GLenum drawMode) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1723 | { |
| 1724 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1725 | Program *programObject = getCurrentProgram(); |
| 1726 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1727 | Framebuffer *framebufferObject = getDrawFramebuffer(); |
| 1728 | |
| 1729 | GLenum adjustedFrontFace = adjustWinding(mState.frontFace); |
| 1730 | |
daniel@transgaming.com | 91fd1de | 2010-05-18 18:51:40 +0000 | [diff] [blame] | 1731 | GLint frontCCW = programObject->getDxFrontCCWLocation(); |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1732 | GLint ccw = (adjustedFrontFace == GL_CCW); |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1733 | programObject->setUniform1iv(frontCCW, 1, &ccw); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1734 | |
daniel@transgaming.com | 91fd1de | 2010-05-18 18:51:40 +0000 | [diff] [blame] | 1735 | GLint pointsOrLines = programObject->getDxPointsOrLinesLocation(); |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 1736 | GLint alwaysFront = !isTriangleMode(drawMode); |
| 1737 | programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront); |
| 1738 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1739 | if (mCullStateDirty || mFrontFaceDirty) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1740 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1741 | if (mState.cullFace) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1742 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1743 | device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1744 | } |
| 1745 | else |
| 1746 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1747 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1750 | mCullStateDirty = false; |
| 1751 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1752 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1753 | if (mDepthStateDirty) |
| 1754 | { |
daniel@transgaming.com | 317887f | 2011-05-11 15:26:12 +0000 | [diff] [blame] | 1755 | if (mState.depthTest) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1756 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1757 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 1758 | device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1759 | } |
| 1760 | else |
| 1761 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1762 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1763 | } |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1764 | |
| 1765 | mDepthStateDirty = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1768 | if (mBlendStateDirty) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1769 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1770 | if (mState.blend) |
daniel@transgaming.com | 1436e26 | 2010-03-17 03:58:56 +0000 | [diff] [blame] | 1771 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1772 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 1773 | |
| 1774 | if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 1775 | mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 1776 | { |
| 1777 | device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor)); |
| 1778 | } |
| 1779 | else |
| 1780 | { |
| 1781 | device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha), |
| 1782 | unorm<8>(mState.blendColor.alpha), |
| 1783 | unorm<8>(mState.blendColor.alpha), |
| 1784 | unorm<8>(mState.blendColor.alpha))); |
| 1785 | } |
| 1786 | |
| 1787 | device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB)); |
| 1788 | device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB)); |
| 1789 | device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB)); |
| 1790 | |
| 1791 | if (mState.sourceBlendRGB != mState.sourceBlendAlpha || |
| 1792 | mState.destBlendRGB != mState.destBlendAlpha || |
| 1793 | mState.blendEquationRGB != mState.blendEquationAlpha) |
| 1794 | { |
| 1795 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1796 | |
| 1797 | device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha)); |
| 1798 | device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha)); |
| 1799 | device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha)); |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1800 | } |
| 1801 | else |
| 1802 | { |
| 1803 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 1804 | } |
daniel@transgaming.com | 1436e26 | 2010-03-17 03:58:56 +0000 | [diff] [blame] | 1805 | } |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1806 | else |
daniel@transgaming.com | aede630 | 2010-04-29 03:35:48 +0000 | [diff] [blame] | 1807 | { |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1808 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
daniel@transgaming.com | aede630 | 2010-04-29 03:35:48 +0000 | [diff] [blame] | 1809 | } |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1810 | |
| 1811 | mBlendStateDirty = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1814 | if (mStencilStateDirty || mFrontFaceDirty) |
| 1815 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1816 | if (mState.stencilTest && framebufferObject->hasStencil()) |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1817 | { |
| 1818 | device->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1819 | device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 1820 | |
| 1821 | // FIXME: Unsupported by D3D9 |
| 1822 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 1823 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 1824 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
| 1825 | if (mState.stencilWritemask != mState.stencilBackWritemask || |
| 1826 | mState.stencilRef != mState.stencilBackRef || |
| 1827 | mState.stencilMask != mState.stencilBackMask) |
| 1828 | { |
| 1829 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 1830 | return error(GL_INVALID_OPERATION); |
| 1831 | } |
| 1832 | |
daniel@transgaming.com | dd7948b | 2010-06-02 16:12:34 +0000 | [diff] [blame] | 1833 | // get the maximum size of the stencil ref |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1834 | gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer(); |
daniel@transgaming.com | dd7948b | 2010-06-02 16:12:34 +0000 | [diff] [blame] | 1835 | GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1; |
| 1836 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1837 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask); |
| 1838 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1839 | es2dx::ConvertComparison(mState.stencilFunc)); |
| 1840 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1841 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil); |
| 1842 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask); |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1843 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1844 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1845 | es2dx::ConvertStencilOp(mState.stencilFail)); |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1846 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1847 | es2dx::ConvertStencilOp(mState.stencilPassDepthFail)); |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1848 | device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1849 | es2dx::ConvertStencilOp(mState.stencilPassDepthPass)); |
| 1850 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1851 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask); |
| 1852 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1853 | es2dx::ConvertComparison(mState.stencilBackFunc)); |
| 1854 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1855 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil); |
| 1856 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask); |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1857 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1858 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1859 | es2dx::ConvertStencilOp(mState.stencilBackFail)); |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1860 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1861 | es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail)); |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 1862 | device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1863 | es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass)); |
| 1864 | } |
| 1865 | else |
| 1866 | { |
| 1867 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1868 | } |
| 1869 | |
| 1870 | mStencilStateDirty = false; |
| 1871 | } |
| 1872 | |
| 1873 | if (mMaskStateDirty) |
| 1874 | { |
| 1875 | device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, |
| 1876 | mState.colorMaskBlue, mState.colorMaskAlpha)); |
| 1877 | device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE); |
| 1878 | |
| 1879 | mMaskStateDirty = false; |
| 1880 | } |
| 1881 | |
| 1882 | if (mPolygonOffsetStateDirty) |
| 1883 | { |
| 1884 | if (mState.polygonOffsetFill) |
| 1885 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 1886 | gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer(); |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1887 | if (depthbuffer) |
| 1888 | { |
| 1889 | device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor)); |
| 1890 | float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize())); |
| 1891 | device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias)); |
| 1892 | } |
| 1893 | } |
| 1894 | else |
| 1895 | { |
| 1896 | device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 1897 | device->SetRenderState(D3DRS_DEPTHBIAS, 0); |
| 1898 | } |
| 1899 | |
| 1900 | mPolygonOffsetStateDirty = false; |
| 1901 | } |
| 1902 | |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1903 | if (mSampleStateDirty) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1904 | { |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1905 | if (framebufferObject->isMultisample()) |
daniel@transgaming.com | a87bdf5 | 2010-04-29 03:38:55 +0000 | [diff] [blame] | 1906 | { |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1907 | if (mState.sampleAlphaToCoverage) |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1908 | { |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1909 | FIXME("Sample alpha to coverage is unimplemented."); |
| 1910 | } |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1911 | |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1912 | device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); |
| 1913 | if (mState.sampleCoverage) |
| 1914 | { |
| 1915 | unsigned int mask = 0; |
| 1916 | if (mState.sampleCoverageValue != 0) |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1917 | { |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1918 | float threshold = 0.5f; |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1919 | |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1920 | for (int i = 0; i < framebufferObject->getSamples(); ++i) |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1921 | { |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1922 | mask <<= 1; |
| 1923 | |
| 1924 | if ((i + 1) * mState.sampleCoverageValue >= threshold) |
| 1925 | { |
| 1926 | threshold += 1.0f; |
| 1927 | mask |= 1; |
| 1928 | } |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1929 | } |
| 1930 | } |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1931 | |
| 1932 | if (mState.sampleCoverageInvert) |
| 1933 | { |
| 1934 | mask = ~mask; |
| 1935 | } |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1936 | |
enne@chromium.org | c5f8dea | 2010-09-21 16:40:30 +0000 | [diff] [blame] | 1937 | device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask); |
| 1938 | } |
| 1939 | else |
| 1940 | { |
| 1941 | device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
| 1942 | } |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 1943 | } |
| 1944 | else |
| 1945 | { |
| 1946 | device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE); |
daniel@transgaming.com | a87bdf5 | 2010-04-29 03:38:55 +0000 | [diff] [blame] | 1947 | } |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1948 | |
| 1949 | mSampleStateDirty = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
daniel@transgaming.com | a79f9d1 | 2010-05-12 03:40:01 +0000 | [diff] [blame] | 1952 | if (mDitherStateDirty) |
| 1953 | { |
| 1954 | device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE); |
| 1955 | |
| 1956 | mDitherStateDirty = false; |
| 1957 | } |
| 1958 | |
| 1959 | mFrontFaceDirty = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 1962 | GLenum Context::applyVertexBuffer(GLint first, GLsizei count) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1963 | { |
daniel@transgaming.com | baa7451 | 2011-04-13 14:56:47 +0000 | [diff] [blame] | 1964 | TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS]; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1965 | |
daniel@transgaming.com | baa7451 | 2011-04-13 14:56:47 +0000 | [diff] [blame] | 1966 | GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 1967 | if (err != GL_NO_ERROR) |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 1968 | { |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 1969 | return err; |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
daniel@transgaming.com | 09c2c1a | 2011-04-13 14:57:16 +0000 | [diff] [blame] | 1972 | return mVertexDeclarationCache.applyDeclaration(attributes, getCurrentProgram()); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1975 | // Applies the indices and element array bindings to the Direct3D 9 device |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 1976 | GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1977 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 1978 | IDirect3DDevice9 *device = getDevice(); |
| 1979 | GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 1980 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 1981 | if (err == GL_NO_ERROR) |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 1982 | { |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 1983 | device->SetIndices(indexInfo->indexBuffer); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 1986 | return err; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | // Applies the shaders and shader constants to the Direct3D 9 device |
| 1990 | void Context::applyShaders() |
| 1991 | { |
| 1992 | IDirect3DDevice9 *device = getDevice(); |
| 1993 | Program *programObject = getCurrentProgram(); |
| 1994 | IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader(); |
| 1995 | IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader(); |
| 1996 | |
| 1997 | device->SetVertexShader(vertexShader); |
| 1998 | device->SetPixelShader(pixelShader); |
| 1999 | |
daniel@transgaming.com | a9eb5da | 2011-03-21 16:39:16 +0000 | [diff] [blame] | 2000 | if (programObject->getSerial() != mAppliedProgramSerial) |
daniel@transgaming.com | 4fa0833 | 2010-05-11 02:29:27 +0000 | [diff] [blame] | 2001 | { |
| 2002 | programObject->dirtyAllUniforms(); |
daniel@transgaming.com | a9eb5da | 2011-03-21 16:39:16 +0000 | [diff] [blame] | 2003 | mAppliedProgramSerial = programObject->getSerial(); |
daniel@transgaming.com | 4fa0833 | 2010-05-11 02:29:27 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2006 | programObject->applyUniforms(); |
| 2007 | } |
| 2008 | |
| 2009 | // Applies the textures and sampler states to the Direct3D 9 device |
| 2010 | void Context::applyTextures() |
| 2011 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2012 | applyTextures(SAMPLER_PIXEL); |
| 2013 | |
| 2014 | if (mSupportsVertexTexture) |
| 2015 | { |
| 2016 | applyTextures(SAMPLER_VERTEX); |
| 2017 | } |
| 2018 | } |
| 2019 | |
daniel@transgaming.com | 9ba680a | 2011-05-11 15:37:11 +0000 | [diff] [blame^] | 2020 | // For each Direct3D 9 sampler of either the pixel or vertex stage, |
| 2021 | // looks up the corresponding OpenGL texture image unit and texture type, |
| 2022 | // and sets the texture and its addressing/filtering state (or NULL when inactive). |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2023 | void Context::applyTextures(SamplerType type) |
| 2024 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2025 | IDirect3DDevice9 *device = getDevice(); |
| 2026 | Program *programObject = getCurrentProgram(); |
| 2027 | |
daniel@transgaming.com | 9ba680a | 2011-05-11 15:37:11 +0000 | [diff] [blame^] | 2028 | int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2029 | |
| 2030 | for (int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2031 | { |
daniel@transgaming.com | 9ba680a | 2011-05-11 15:37:11 +0000 | [diff] [blame^] | 2032 | int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2033 | int d3dSampler = (type == SAMPLER_PIXEL) ? samplerIndex : D3DVERTEXTEXTURESAMPLER0 + samplerIndex; |
| 2034 | unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS; |
| 2035 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 2036 | if (textureUnit != -1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2037 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2038 | TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex); |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 2039 | |
| 2040 | Texture *texture = getSamplerTexture(textureUnit, textureType); |
| 2041 | |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2042 | if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter() || texture->isDirtyImage()) |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 2043 | { |
daniel@transgaming.com | 38e76e5 | 2011-03-21 16:39:10 +0000 | [diff] [blame] | 2044 | IDirect3DBaseTexture9 *d3dTexture = texture->getTexture(); |
| 2045 | |
| 2046 | if (d3dTexture) |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 2047 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2048 | if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyParameter()) |
daniel@transgaming.com | a06aa87 | 2011-03-21 17:22:21 +0000 | [diff] [blame] | 2049 | { |
| 2050 | GLenum wrapS = texture->getWrapS(); |
| 2051 | GLenum wrapT = texture->getWrapT(); |
| 2052 | GLenum minFilter = texture->getMinFilter(); |
| 2053 | GLenum magFilter = texture->getMagFilter(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2054 | |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2055 | device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS)); |
| 2056 | device->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2057 | |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2058 | device->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter)); |
daniel@transgaming.com | a06aa87 | 2011-03-21 17:22:21 +0000 | [diff] [blame] | 2059 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
| 2060 | es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter); |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2061 | device->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 2062 | device->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
daniel@transgaming.com | a06aa87 | 2011-03-21 17:22:21 +0000 | [diff] [blame] | 2063 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2064 | |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2065 | if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->isDirtyImage()) |
daniel@transgaming.com | a06aa87 | 2011-03-21 17:22:21 +0000 | [diff] [blame] | 2066 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2067 | device->SetTexture(d3dSampler, d3dTexture); |
daniel@transgaming.com | a06aa87 | 2011-03-21 17:22:21 +0000 | [diff] [blame] | 2068 | } |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 2069 | } |
| 2070 | else |
| 2071 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2072 | device->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture()); |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 2073 | } |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 2074 | |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2075 | appliedTextureSerial[samplerIndex] = texture->getSerial(); |
daniel@transgaming.com | 38e76e5 | 2011-03-21 16:39:10 +0000 | [diff] [blame] | 2076 | texture->resetDirty(); |
| 2077 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2078 | } |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 2079 | else |
| 2080 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2081 | if (appliedTextureSerial[samplerIndex] != 0) |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 2082 | { |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2083 | device->SetTexture(d3dSampler, NULL); |
| 2084 | appliedTextureSerial[samplerIndex] = 0; |
daniel@transgaming.com | 5a0b0a8 | 2010-05-12 03:45:07 +0000 | [diff] [blame] | 2085 | } |
daniel@transgaming.com | d4a3517 | 2011-05-11 15:36:45 +0000 | [diff] [blame] | 2086 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels) |
| 2091 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 2092 | Framebuffer *framebuffer = getReadFramebuffer(); |
daniel@transgaming.com | bbc5779 | 2010-07-28 19:21:05 +0000 | [diff] [blame] | 2093 | |
| 2094 | if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 2095 | { |
| 2096 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
| 2097 | } |
| 2098 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 2099 | if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0) |
| 2100 | { |
| 2101 | return error(GL_INVALID_OPERATION); |
| 2102 | } |
| 2103 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2104 | IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget(); |
daniel@transgaming.com | d36c6a0 | 2010-08-31 12:15:09 +0000 | [diff] [blame] | 2105 | |
| 2106 | if (!renderTarget) |
| 2107 | { |
| 2108 | return; // Context must be lost, return silently |
| 2109 | } |
| 2110 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2111 | IDirect3DDevice9 *device = getDevice(); |
| 2112 | |
| 2113 | D3DSURFACE_DESC desc; |
| 2114 | renderTarget->GetDesc(&desc); |
| 2115 | |
| 2116 | IDirect3DSurface9 *systemSurface; |
| 2117 | HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 2118 | |
| 2119 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 2120 | { |
| 2121 | return error(GL_OUT_OF_MEMORY); |
| 2122 | } |
| 2123 | |
| 2124 | ASSERT(SUCCEEDED(result)); |
| 2125 | |
| 2126 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 2127 | { |
| 2128 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
| 2129 | } |
| 2130 | |
| 2131 | result = device->GetRenderTargetData(renderTarget, systemSurface); |
| 2132 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2133 | if (FAILED(result)) |
| 2134 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2135 | systemSurface->Release(); |
| 2136 | |
apatrick@chromium.org | 6db8cab | 2010-07-22 20:39:50 +0000 | [diff] [blame] | 2137 | switch (result) |
| 2138 | { |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2139 | case D3DERR_DRIVERINTERNALERROR: |
| 2140 | case D3DERR_DEVICELOST: |
| 2141 | return error(GL_OUT_OF_MEMORY); |
| 2142 | default: |
| 2143 | UNREACHABLE(); |
| 2144 | return; // No sensible error to generate |
apatrick@chromium.org | 6db8cab | 2010-07-22 20:39:50 +0000 | [diff] [blame] | 2145 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | D3DLOCKED_RECT lock; |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2149 | RECT rect = transformPixelRect(x, y, width, height, desc.Height); |
| 2150 | rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width)); |
| 2151 | rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height)); |
| 2152 | rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width)); |
| 2153 | rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2154 | |
| 2155 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 2156 | |
| 2157 | if (FAILED(result)) |
| 2158 | { |
| 2159 | UNREACHABLE(); |
| 2160 | systemSurface->Release(); |
| 2161 | |
| 2162 | return; // No sensible error to generate |
| 2163 | } |
| 2164 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2165 | unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2166 | unsigned char *dest = (unsigned char*)pixels; |
daniel@transgaming.com | afb2395 | 2010-04-13 03:25:54 +0000 | [diff] [blame] | 2167 | unsigned short *dest16 = (unsigned short*)pixels; |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2168 | int inputPitch = -lock.Pitch; |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2169 | GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment); |
daniel@transgaming.com | 713914b | 2010-05-04 03:35:17 +0000 | [diff] [blame] | 2170 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2171 | for (int j = 0; j < rect.bottom - rect.top; j++) |
| 2172 | { |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 2173 | if (desc.Format == D3DFMT_A8R8G8B8 && |
| 2174 | format == GL_BGRA_EXT && |
| 2175 | type == GL_UNSIGNED_BYTE) |
| 2176 | { |
| 2177 | // Fast path for EXT_read_format_bgra, given |
| 2178 | // an RGBA source buffer. Note that buffers with no |
| 2179 | // alpha go through the slow path below. |
| 2180 | memcpy(dest + j * outputPitch, |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2181 | source + j * inputPitch, |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 2182 | (rect.right - rect.left) * 4); |
| 2183 | continue; |
| 2184 | } |
| 2185 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2186 | for (int i = 0; i < rect.right - rect.left; i++) |
| 2187 | { |
| 2188 | float r; |
| 2189 | float g; |
| 2190 | float b; |
| 2191 | float a; |
| 2192 | |
| 2193 | switch (desc.Format) |
| 2194 | { |
| 2195 | case D3DFMT_R5G6B5: |
| 2196 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2197 | unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2198 | |
| 2199 | a = 1.0f; |
| 2200 | b = (rgb & 0x001F) * (1.0f / 0x001F); |
| 2201 | g = (rgb & 0x07E0) * (1.0f / 0x07E0); |
| 2202 | r = (rgb & 0xF800) * (1.0f / 0xF800); |
| 2203 | } |
| 2204 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2205 | case D3DFMT_A1R5G5B5: |
| 2206 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2207 | unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2208 | |
| 2209 | a = (argb & 0x8000) ? 1.0f : 0.0f; |
| 2210 | b = (argb & 0x001F) * (1.0f / 0x001F); |
| 2211 | g = (argb & 0x03E0) * (1.0f / 0x03E0); |
| 2212 | r = (argb & 0x7C00) * (1.0f / 0x7C00); |
| 2213 | } |
| 2214 | break; |
| 2215 | case D3DFMT_A8R8G8B8: |
| 2216 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2217 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2218 | |
| 2219 | a = (argb & 0xFF000000) * (1.0f / 0xFF000000); |
| 2220 | b = (argb & 0x000000FF) * (1.0f / 0x000000FF); |
| 2221 | g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2222 | r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2223 | } |
| 2224 | break; |
| 2225 | case D3DFMT_X8R8G8B8: |
| 2226 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2227 | unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2228 | |
| 2229 | a = 1.0f; |
| 2230 | b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); |
| 2231 | g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2232 | r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2233 | } |
| 2234 | break; |
| 2235 | case D3DFMT_A2R10G10B10: |
| 2236 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2237 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2238 | |
| 2239 | a = (argb & 0xC0000000) * (1.0f / 0xC0000000); |
| 2240 | b = (argb & 0x000003FF) * (1.0f / 0x000003FF); |
| 2241 | g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); |
| 2242 | r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); |
| 2243 | } |
| 2244 | break; |
daniel@transgaming.com | 1297d92 | 2010-09-01 15:47:47 +0000 | [diff] [blame] | 2245 | case D3DFMT_A32B32G32R32F: |
| 2246 | { |
| 2247 | // float formats in D3D are stored rgba, rather than the other way round |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2248 | r = *((float*)(source + 16 * i + j * inputPitch) + 0); |
| 2249 | g = *((float*)(source + 16 * i + j * inputPitch) + 1); |
| 2250 | b = *((float*)(source + 16 * i + j * inputPitch) + 2); |
| 2251 | a = *((float*)(source + 16 * i + j * inputPitch) + 3); |
daniel@transgaming.com | 1297d92 | 2010-09-01 15:47:47 +0000 | [diff] [blame] | 2252 | } |
| 2253 | break; |
| 2254 | case D3DFMT_A16B16G16R16F: |
| 2255 | { |
| 2256 | // float formats in D3D are stored rgba, rather than the other way round |
| 2257 | float abgr[4]; |
| 2258 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 2259 | D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4); |
daniel@transgaming.com | 1297d92 | 2010-09-01 15:47:47 +0000 | [diff] [blame] | 2260 | |
| 2261 | a = abgr[3]; |
| 2262 | b = abgr[2]; |
| 2263 | g = abgr[1]; |
| 2264 | r = abgr[0]; |
| 2265 | } |
| 2266 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2267 | default: |
| 2268 | UNIMPLEMENTED(); // FIXME |
| 2269 | UNREACHABLE(); |
| 2270 | } |
| 2271 | |
| 2272 | switch (format) |
| 2273 | { |
| 2274 | case GL_RGBA: |
| 2275 | switch (type) |
| 2276 | { |
| 2277 | case GL_UNSIGNED_BYTE: |
daniel@transgaming.com | 713914b | 2010-05-04 03:35:17 +0000 | [diff] [blame] | 2278 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); |
| 2279 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2280 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); |
| 2281 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2282 | break; |
| 2283 | default: UNREACHABLE(); |
| 2284 | } |
| 2285 | break; |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 2286 | case GL_BGRA_EXT: |
| 2287 | switch (type) |
| 2288 | { |
| 2289 | case GL_UNSIGNED_BYTE: |
| 2290 | dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f); |
| 2291 | dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); |
| 2292 | dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f); |
| 2293 | dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); |
| 2294 | break; |
| 2295 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 2296 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2297 | // this type is packed as follows: |
| 2298 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2299 | // -------------------------------------------------------------------------------- |
| 2300 | // | 4th | 3rd | 2nd | 1st component | |
| 2301 | // -------------------------------------------------------------------------------- |
| 2302 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2303 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2304 | ((unsigned short)(15 * a + 0.5f) << 12)| |
| 2305 | ((unsigned short)(15 * r + 0.5f) << 8) | |
| 2306 | ((unsigned short)(15 * g + 0.5f) << 4) | |
| 2307 | ((unsigned short)(15 * b + 0.5f) << 0); |
| 2308 | break; |
| 2309 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 2310 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2311 | // this type is packed as follows: |
| 2312 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2313 | // -------------------------------------------------------------------------------- |
| 2314 | // | 4th | 3rd | 2nd | 1st component | |
| 2315 | // -------------------------------------------------------------------------------- |
| 2316 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2317 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2318 | ((unsigned short)( a + 0.5f) << 15) | |
| 2319 | ((unsigned short)(31 * r + 0.5f) << 10) | |
| 2320 | ((unsigned short)(31 * g + 0.5f) << 5) | |
| 2321 | ((unsigned short)(31 * b + 0.5f) << 0); |
| 2322 | break; |
| 2323 | default: UNREACHABLE(); |
| 2324 | } |
| 2325 | break; |
daniel@transgaming.com | afb2395 | 2010-04-13 03:25:54 +0000 | [diff] [blame] | 2326 | case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2327 | switch (type) |
| 2328 | { |
daniel@transgaming.com | afb2395 | 2010-04-13 03:25:54 +0000 | [diff] [blame] | 2329 | case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE |
daniel@transgaming.com | 713914b | 2010-05-04 03:35:17 +0000 | [diff] [blame] | 2330 | dest16[i + j * outputPitch / sizeof(unsigned short)] = |
| 2331 | ((unsigned short)(31 * b + 0.5f) << 0) | |
| 2332 | ((unsigned short)(63 * g + 0.5f) << 5) | |
| 2333 | ((unsigned short)(31 * r + 0.5f) << 11); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | default: UNREACHABLE(); |
| 2336 | } |
| 2337 | break; |
| 2338 | default: UNREACHABLE(); |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | systemSurface->UnlockRect(); |
| 2344 | |
| 2345 | systemSurface->Release(); |
| 2346 | } |
| 2347 | |
| 2348 | void Context::clear(GLbitfield mask) |
| 2349 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 2350 | Framebuffer *framebufferObject = getDrawFramebuffer(); |
daniel@transgaming.com | baeb8c5 | 2010-05-05 18:50:39 +0000 | [diff] [blame] | 2351 | |
| 2352 | if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 2353 | { |
daniel@transgaming.com | b5a3a6b | 2011-03-21 16:38:46 +0000 | [diff] [blame] | 2354 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
daniel@transgaming.com | baeb8c5 | 2010-05-05 18:50:39 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2357 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2358 | IDirect3DDevice9 *device = getDevice(); |
| 2359 | DWORD flags = 0; |
| 2360 | |
| 2361 | if (mask & GL_COLOR_BUFFER_BIT) |
| 2362 | { |
| 2363 | mask &= ~GL_COLOR_BUFFER_BIT; |
daniel@transgaming.com | c6f5340 | 2010-06-24 13:02:19 +0000 | [diff] [blame] | 2364 | |
| 2365 | if (framebufferObject->getColorbufferType() != GL_NONE) |
| 2366 | { |
| 2367 | flags |= D3DCLEAR_TARGET; |
| 2368 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | if (mask & GL_DEPTH_BUFFER_BIT) |
| 2372 | { |
| 2373 | mask &= ~GL_DEPTH_BUFFER_BIT; |
daniel@transgaming.com | c6f5340 | 2010-06-24 13:02:19 +0000 | [diff] [blame] | 2374 | if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2375 | { |
| 2376 | flags |= D3DCLEAR_ZBUFFER; |
| 2377 | } |
| 2378 | } |
| 2379 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2380 | GLuint stencilUnmasked = 0x0; |
| 2381 | |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 2382 | if (mask & GL_STENCIL_BUFFER_BIT) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2383 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2384 | mask &= ~GL_STENCIL_BUFFER_BIT; |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 2385 | if (framebufferObject->getStencilbufferType() != GL_NONE) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2386 | { |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 2387 | IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil(); |
apatrick@chromium.org | b2bdd06 | 2010-10-05 02:24:30 +0000 | [diff] [blame] | 2388 | if (!depthStencil) |
| 2389 | { |
| 2390 | ERR("Depth stencil pointer unexpectedly null."); |
| 2391 | return; |
| 2392 | } |
| 2393 | |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 2394 | D3DSURFACE_DESC desc; |
| 2395 | depthStencil->GetDesc(&desc); |
| 2396 | |
daniel@transgaming.com | d2fd4f2 | 2011-02-01 18:49:11 +0000 | [diff] [blame] | 2397 | unsigned int stencilSize = dx2es::GetStencilSize(desc.Format); |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 2398 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 2399 | |
| 2400 | if (stencilUnmasked != 0x0) |
| 2401 | { |
| 2402 | flags |= D3DCLEAR_STENCIL; |
| 2403 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | if (mask != 0) |
| 2408 | { |
| 2409 | return error(GL_INVALID_VALUE); |
| 2410 | } |
| 2411 | |
daniel@transgaming.com | baeb8c5 | 2010-05-05 18:50:39 +0000 | [diff] [blame] | 2412 | if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport |
| 2413 | { |
| 2414 | return; |
| 2415 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2416 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2417 | D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha), |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2418 | unorm<8>(mState.colorClearValue.red), |
| 2419 | unorm<8>(mState.colorClearValue.green), |
| 2420 | unorm<8>(mState.colorClearValue.blue)); |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2421 | float depth = clamp01(mState.depthClearValue); |
| 2422 | int stencil = mState.stencilClearValue & 0x000000FF; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2423 | |
| 2424 | IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget(); |
| 2425 | |
daniel@transgaming.com | d36c6a0 | 2010-08-31 12:15:09 +0000 | [diff] [blame] | 2426 | if (!renderTarget) |
| 2427 | { |
| 2428 | return; // Context must be lost, return silently |
| 2429 | } |
| 2430 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2431 | D3DSURFACE_DESC desc; |
| 2432 | renderTarget->GetDesc(&desc); |
| 2433 | |
daniel@transgaming.com | d2fd4f2 | 2011-02-01 18:49:11 +0000 | [diff] [blame] | 2434 | bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2435 | |
| 2436 | const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) && |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2437 | (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2438 | const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) && |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2439 | !(mState.colorMaskRed && mState.colorMaskGreen && |
| 2440 | mState.colorMaskBlue && alphaUnmasked); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2441 | |
| 2442 | if (needMaskedColorClear || needMaskedStencilClear) |
| 2443 | { |
daniel@transgaming.com | 8f05d1a | 2010-06-07 02:06:03 +0000 | [diff] [blame] | 2444 | // State which is altered in all paths from this point to the clear call is saved. |
| 2445 | // State which is altered in only some paths will be flagged dirty in the case that |
| 2446 | // that path is taken. |
| 2447 | HRESULT hr; |
| 2448 | if (mMaskedClearSavedState == NULL) |
| 2449 | { |
| 2450 | hr = device->BeginStateBlock(); |
| 2451 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 2452 | |
| 2453 | device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 2454 | device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 2455 | device->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 2456 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 2457 | device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 2458 | device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 2459 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 2460 | device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 2461 | device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 2462 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 2463 | device->SetPixelShader(NULL); |
| 2464 | device->SetVertexShader(NULL); |
| 2465 | device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2466 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 2467 | device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 2468 | device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 2469 | device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 2470 | device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 2471 | device->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 2472 | device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
daniel@transgaming.com | 8f05d1a | 2010-06-07 02:06:03 +0000 | [diff] [blame] | 2473 | |
| 2474 | hr = device->EndStateBlock(&mMaskedClearSavedState); |
| 2475 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 2476 | } |
| 2477 | |
| 2478 | ASSERT(mMaskedClearSavedState != NULL); |
| 2479 | |
| 2480 | if (mMaskedClearSavedState != NULL) |
| 2481 | { |
| 2482 | hr = mMaskedClearSavedState->Capture(); |
| 2483 | ASSERT(SUCCEEDED(hr)); |
| 2484 | } |
| 2485 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2486 | device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 2487 | device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 2488 | device->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 2489 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 2490 | device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 2491 | device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 2492 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 2493 | device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 2494 | |
| 2495 | if (flags & D3DCLEAR_TARGET) |
| 2496 | { |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2497 | device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2498 | } |
| 2499 | else |
| 2500 | { |
| 2501 | device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 2502 | } |
| 2503 | |
| 2504 | if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL)) |
| 2505 | { |
| 2506 | device->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 2507 | device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); |
| 2508 | device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); |
| 2509 | device->SetRenderState(D3DRS_STENCILREF, stencil); |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2510 | device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 2511 | device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2512 | device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); |
| 2513 | device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); |
daniel@transgaming.com | 8f05d1a | 2010-06-07 02:06:03 +0000 | [diff] [blame] | 2514 | mStencilStateDirty = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2515 | } |
| 2516 | else |
| 2517 | { |
| 2518 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 2519 | } |
| 2520 | |
| 2521 | device->SetPixelShader(NULL); |
| 2522 | device->SetVertexShader(NULL); |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2523 | device->SetFVF(D3DFVF_XYZRHW); |
| 2524 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 2525 | device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); |
| 2526 | device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); |
| 2527 | device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); |
| 2528 | device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); |
| 2529 | device->SetRenderState(D3DRS_TEXTUREFACTOR, color); |
| 2530 | device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2531 | |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2532 | float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges |
| 2533 | quad[0][0] = -0.5f; |
| 2534 | quad[0][1] = desc.Height - 0.5f; |
| 2535 | quad[0][2] = 0.0f; |
| 2536 | quad[0][3] = 1.0f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2537 | |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2538 | quad[1][0] = desc.Width - 0.5f; |
| 2539 | quad[1][1] = desc.Height - 0.5f; |
| 2540 | quad[1][2] = 0.0f; |
| 2541 | quad[1][3] = 1.0f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2542 | |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2543 | quad[2][0] = -0.5f; |
| 2544 | quad[2][1] = -0.5f; |
| 2545 | quad[2][2] = 0.0f; |
| 2546 | quad[2][3] = 1.0f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2547 | |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2548 | quad[3][0] = desc.Width - 0.5f; |
| 2549 | quad[3][1] = -0.5f; |
| 2550 | quad[3][2] = 0.0f; |
| 2551 | quad[3][3] = 1.0f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2552 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2553 | display->startScene(); |
daniel@transgaming.com | 1615be2 | 2011-02-09 16:30:06 +0000 | [diff] [blame] | 2554 | device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4])); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2555 | |
| 2556 | if (flags & D3DCLEAR_ZBUFFER) |
| 2557 | { |
| 2558 | device->SetRenderState(D3DRS_ZENABLE, TRUE); |
| 2559 | device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); |
| 2560 | device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); |
| 2561 | } |
daniel@transgaming.com | 8f05d1a | 2010-06-07 02:06:03 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (mMaskedClearSavedState != NULL) |
| 2564 | { |
| 2565 | mMaskedClearSavedState->Apply(); |
| 2566 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2567 | } |
daniel@transgaming.com | 8ede24f | 2010-05-05 18:47:58 +0000 | [diff] [blame] | 2568 | else if (flags) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2569 | { |
| 2570 | device->Clear(0, NULL, flags, color, depth, stencil); |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | void Context::drawArrays(GLenum mode, GLint first, GLsizei count) |
| 2575 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2576 | if (!mState.currentProgram) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2577 | { |
| 2578 | return error(GL_INVALID_OPERATION); |
| 2579 | } |
| 2580 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2581 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2582 | IDirect3DDevice9 *device = getDevice(); |
| 2583 | D3DPRIMITIVETYPE primitiveType; |
| 2584 | int primitiveCount; |
| 2585 | |
| 2586 | if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount)) |
| 2587 | return error(GL_INVALID_ENUM); |
| 2588 | |
| 2589 | if (primitiveCount <= 0) |
| 2590 | { |
| 2591 | return; |
| 2592 | } |
| 2593 | |
| 2594 | if (!applyRenderTarget(false)) |
| 2595 | { |
daniel@transgaming.com | baeb8c5 | 2010-05-05 18:50:39 +0000 | [diff] [blame] | 2596 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 2599 | applyState(mode); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 2600 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 2601 | GLenum err = applyVertexBuffer(first, count); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 2602 | if (err != GL_NO_ERROR) |
| 2603 | { |
| 2604 | return error(err); |
| 2605 | } |
| 2606 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2607 | applyShaders(); |
| 2608 | applyTextures(); |
| 2609 | |
daniel@transgaming.com | f494c9c | 2011-05-11 15:37:05 +0000 | [diff] [blame] | 2610 | if (!getCurrentProgram()->validateSamplers(false)) |
daniel@transgaming.com | c3a0e94 | 2010-04-29 03:35:45 +0000 | [diff] [blame] | 2611 | { |
| 2612 | return error(GL_INVALID_OPERATION); |
| 2613 | } |
| 2614 | |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2615 | if (!cullSkipsDraw(mode)) |
| 2616 | { |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2617 | display->startScene(); |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 2618 | |
| 2619 | device->DrawPrimitive(primitiveType, 0, primitiveCount); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2620 | |
| 2621 | if (mode == GL_LINE_LOOP) // Draw the last segment separately |
| 2622 | { |
| 2623 | drawClosingLine(first, first + count - 1); |
| 2624 | } |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2625 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2628 | void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2629 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2630 | if (!mState.currentProgram) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2631 | { |
| 2632 | return error(GL_INVALID_OPERATION); |
| 2633 | } |
| 2634 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 2635 | if (!indices && !mState.elementArrayBuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2636 | { |
| 2637 | return error(GL_INVALID_OPERATION); |
| 2638 | } |
| 2639 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2640 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2641 | IDirect3DDevice9 *device = getDevice(); |
| 2642 | D3DPRIMITIVETYPE primitiveType; |
| 2643 | int primitiveCount; |
| 2644 | |
| 2645 | if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount)) |
| 2646 | return error(GL_INVALID_ENUM); |
| 2647 | |
| 2648 | if (primitiveCount <= 0) |
| 2649 | { |
| 2650 | return; |
| 2651 | } |
| 2652 | |
| 2653 | if (!applyRenderTarget(false)) |
| 2654 | { |
daniel@transgaming.com | baeb8c5 | 2010-05-05 18:50:39 +0000 | [diff] [blame] | 2655 | return; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 2658 | applyState(mode); |
daniel@transgaming.com | 41d8dd8 | 2010-05-12 03:45:03 +0000 | [diff] [blame] | 2659 | |
| 2660 | TranslatedIndexData indexInfo; |
| 2661 | GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo); |
| 2662 | if (err != GL_NO_ERROR) |
| 2663 | { |
| 2664 | return error(err); |
| 2665 | } |
| 2666 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 2667 | GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; |
| 2668 | err = applyVertexBuffer(indexInfo.minIndex, vertexCount); |
daniel@transgaming.com | 838bcea | 2010-05-20 19:17:42 +0000 | [diff] [blame] | 2669 | if (err != GL_NO_ERROR) |
| 2670 | { |
| 2671 | return error(err); |
| 2672 | } |
| 2673 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2674 | applyShaders(); |
| 2675 | applyTextures(); |
| 2676 | |
daniel@transgaming.com | f494c9c | 2011-05-11 15:37:05 +0000 | [diff] [blame] | 2677 | if (!getCurrentProgram()->validateSamplers(false)) |
daniel@transgaming.com | c3a0e94 | 2010-04-29 03:35:45 +0000 | [diff] [blame] | 2678 | { |
| 2679 | return error(GL_INVALID_OPERATION); |
| 2680 | } |
| 2681 | |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2682 | if (!cullSkipsDraw(mode)) |
| 2683 | { |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2684 | display->startScene(); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2685 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 2686 | device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2687 | |
| 2688 | if (mode == GL_LINE_LOOP) // Draw the last segment separately |
| 2689 | { |
| 2690 | drawClosingLine(count, type, indices); |
| 2691 | } |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2692 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | void Context::finish() |
| 2696 | { |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2697 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2698 | IDirect3DDevice9 *device = getDevice(); |
| 2699 | IDirect3DQuery9 *occlusionQuery = NULL; |
| 2700 | |
| 2701 | HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery); |
| 2702 | |
| 2703 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 2704 | { |
| 2705 | return error(GL_OUT_OF_MEMORY); |
| 2706 | } |
| 2707 | |
| 2708 | ASSERT(SUCCEEDED(result)); |
| 2709 | |
| 2710 | if (occlusionQuery) |
| 2711 | { |
daniel@transgaming.com | a71cdd7 | 2010-05-12 16:51:14 +0000 | [diff] [blame] | 2712 | IDirect3DStateBlock9 *savedState = NULL; |
| 2713 | device->CreateStateBlock(D3DSBT_ALL, &savedState); |
| 2714 | |
apatrick@chromium.org | 575e791 | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 2715 | HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN); |
| 2716 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2717 | |
| 2718 | // Render something outside the render target |
| 2719 | device->SetPixelShader(NULL); |
| 2720 | device->SetVertexShader(NULL); |
| 2721 | device->SetFVF(D3DFVF_XYZRHW); |
| 2722 | float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f}; |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 2723 | display->startScene(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2724 | device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2725 | |
apatrick@chromium.org | 575e791 | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 2726 | result = occlusionQuery->Issue(D3DISSUE_END); |
| 2727 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2728 | |
| 2729 | while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) |
| 2730 | { |
| 2731 | // Keep polling, but allow other threads to do something useful first |
| 2732 | Sleep(0); |
| 2733 | } |
| 2734 | |
| 2735 | occlusionQuery->Release(); |
daniel@transgaming.com | a71cdd7 | 2010-05-12 16:51:14 +0000 | [diff] [blame] | 2736 | |
| 2737 | if (savedState) |
| 2738 | { |
| 2739 | savedState->Apply(); |
| 2740 | savedState->Release(); |
| 2741 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | void Context::flush() |
| 2746 | { |
| 2747 | IDirect3DDevice9 *device = getDevice(); |
| 2748 | IDirect3DQuery9 *eventQuery = NULL; |
| 2749 | |
| 2750 | HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery); |
| 2751 | |
| 2752 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 2753 | { |
| 2754 | return error(GL_OUT_OF_MEMORY); |
| 2755 | } |
| 2756 | |
| 2757 | ASSERT(SUCCEEDED(result)); |
| 2758 | |
| 2759 | if (eventQuery) |
| 2760 | { |
apatrick@chromium.org | 575e791 | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 2761 | HRESULT result = eventQuery->Issue(D3DISSUE_END); |
| 2762 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2763 | |
apatrick@chromium.org | 575e791 | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 2764 | result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2765 | eventQuery->Release(); |
apatrick@chromium.org | 575e791 | 2010-08-25 18:07:12 +0000 | [diff] [blame] | 2766 | |
| 2767 | if (result == D3DERR_DEVICELOST) |
| 2768 | { |
| 2769 | error(GL_OUT_OF_MEMORY); |
| 2770 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2774 | void Context::drawClosingLine(unsigned int first, unsigned int last) |
| 2775 | { |
| 2776 | IDirect3DDevice9 *device = getDevice(); |
| 2777 | IDirect3DIndexBuffer9 *indexBuffer = NULL; |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2778 | bool succeeded = false; |
| 2779 | UINT offset; |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2780 | |
| 2781 | if (supports32bitIndices()) |
| 2782 | { |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2783 | const int spaceNeeded = 2 * sizeof(unsigned int); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2784 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2785 | if (!mClosingIB) |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2786 | { |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2787 | mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32); |
| 2788 | } |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2789 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2790 | mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT); |
| 2791 | |
| 2792 | unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset)); |
| 2793 | if (data) |
| 2794 | { |
| 2795 | data[0] = last; |
| 2796 | data[1] = first; |
| 2797 | mClosingIB->unmap(); |
| 2798 | offset /= 4; |
| 2799 | succeeded = true; |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | else |
| 2803 | { |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2804 | const int spaceNeeded = 2 * sizeof(unsigned short); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2805 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2806 | if (!mClosingIB) |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2807 | { |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2808 | mClosingIB = new StreamingIndexBuffer(device, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16); |
| 2809 | } |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2810 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2811 | mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT); |
| 2812 | |
| 2813 | unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset)); |
| 2814 | if (data) |
| 2815 | { |
| 2816 | data[0] = last; |
| 2817 | data[1] = first; |
| 2818 | mClosingIB->unmap(); |
| 2819 | offset /= 2; |
| 2820 | succeeded = true; |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2824 | if (succeeded) |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2825 | { |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2826 | device->SetIndices(mClosingIB->getBuffer()); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2827 | |
jbauman@chromium.org | 399c35f | 2011-04-28 23:19:51 +0000 | [diff] [blame] | 2828 | device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, offset, 1); |
daniel@transgaming.com | ddcd737 | 2011-01-08 05:46:33 +0000 | [diff] [blame] | 2829 | } |
| 2830 | else |
| 2831 | { |
| 2832 | ERR("Could not create an index buffer for closing a line loop."); |
| 2833 | error(GL_OUT_OF_MEMORY); |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices) |
| 2838 | { |
| 2839 | unsigned int first = 0; |
| 2840 | unsigned int last = 0; |
| 2841 | |
| 2842 | if (mState.elementArrayBuffer.get()) |
| 2843 | { |
| 2844 | Buffer *indexBuffer = mState.elementArrayBuffer.get(); |
| 2845 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
| 2846 | indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset; |
| 2847 | } |
| 2848 | |
| 2849 | switch (type) |
| 2850 | { |
| 2851 | case GL_UNSIGNED_BYTE: |
| 2852 | first = static_cast<const GLubyte*>(indices)[0]; |
| 2853 | last = static_cast<const GLubyte*>(indices)[count - 1]; |
| 2854 | break; |
| 2855 | case GL_UNSIGNED_SHORT: |
| 2856 | first = static_cast<const GLushort*>(indices)[0]; |
| 2857 | last = static_cast<const GLushort*>(indices)[count - 1]; |
| 2858 | break; |
| 2859 | case GL_UNSIGNED_INT: |
| 2860 | first = static_cast<const GLuint*>(indices)[0]; |
| 2861 | last = static_cast<const GLuint*>(indices)[count - 1]; |
| 2862 | break; |
| 2863 | default: UNREACHABLE(); |
| 2864 | } |
| 2865 | |
| 2866 | drawClosingLine(first, last); |
| 2867 | } |
| 2868 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2869 | void Context::recordInvalidEnum() |
| 2870 | { |
| 2871 | mInvalidEnum = true; |
| 2872 | } |
| 2873 | |
| 2874 | void Context::recordInvalidValue() |
| 2875 | { |
| 2876 | mInvalidValue = true; |
| 2877 | } |
| 2878 | |
| 2879 | void Context::recordInvalidOperation() |
| 2880 | { |
| 2881 | mInvalidOperation = true; |
| 2882 | } |
| 2883 | |
| 2884 | void Context::recordOutOfMemory() |
| 2885 | { |
| 2886 | mOutOfMemory = true; |
| 2887 | } |
| 2888 | |
| 2889 | void Context::recordInvalidFramebufferOperation() |
| 2890 | { |
| 2891 | mInvalidFramebufferOperation = true; |
| 2892 | } |
| 2893 | |
| 2894 | // Get one of the recorded errors and clear its flag, if any. |
| 2895 | // [OpenGL ES 2.0.24] section 2.5 page 13. |
| 2896 | GLenum Context::getError() |
| 2897 | { |
| 2898 | if (mInvalidEnum) |
| 2899 | { |
| 2900 | mInvalidEnum = false; |
| 2901 | |
| 2902 | return GL_INVALID_ENUM; |
| 2903 | } |
| 2904 | |
| 2905 | if (mInvalidValue) |
| 2906 | { |
| 2907 | mInvalidValue = false; |
| 2908 | |
| 2909 | return GL_INVALID_VALUE; |
| 2910 | } |
| 2911 | |
| 2912 | if (mInvalidOperation) |
| 2913 | { |
| 2914 | mInvalidOperation = false; |
| 2915 | |
| 2916 | return GL_INVALID_OPERATION; |
| 2917 | } |
| 2918 | |
| 2919 | if (mOutOfMemory) |
| 2920 | { |
| 2921 | mOutOfMemory = false; |
| 2922 | |
| 2923 | return GL_OUT_OF_MEMORY; |
| 2924 | } |
| 2925 | |
| 2926 | if (mInvalidFramebufferOperation) |
| 2927 | { |
| 2928 | mInvalidFramebufferOperation = false; |
| 2929 | |
| 2930 | return GL_INVALID_FRAMEBUFFER_OPERATION; |
| 2931 | } |
| 2932 | |
| 2933 | return GL_NO_ERROR; |
| 2934 | } |
| 2935 | |
daniel@transgaming.com | be5a086 | 2010-07-28 19:20:37 +0000 | [diff] [blame] | 2936 | bool Context::supportsShaderModel3() const |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 2937 | { |
daniel@transgaming.com | be5a086 | 2010-07-28 19:20:37 +0000 | [diff] [blame] | 2938 | return mSupportsShaderModel3; |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
daniel@transgaming.com | 396c643 | 2010-11-26 16:26:12 +0000 | [diff] [blame] | 2941 | int Context::getMaximumVaryingVectors() const |
| 2942 | { |
| 2943 | return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2; |
| 2944 | } |
| 2945 | |
daniel@transgaming.com | af29cac | 2011-05-11 15:36:31 +0000 | [diff] [blame] | 2946 | int Context::getMaximumVertexTextureImageUnits() const |
| 2947 | { |
| 2948 | return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0; |
| 2949 | } |
| 2950 | |
| 2951 | int Context::getMaximumCombinedTextureImageUnits() const |
| 2952 | { |
| 2953 | return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits(); |
| 2954 | } |
| 2955 | |
daniel@transgaming.com | 458da14 | 2010-11-28 02:03:02 +0000 | [diff] [blame] | 2956 | int Context::getMaximumFragmentUniformVectors() const |
| 2957 | { |
| 2958 | return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2; |
| 2959 | } |
| 2960 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 2961 | int Context::getMaxSupportedSamples() const |
| 2962 | { |
| 2963 | return mMaxSupportedSamples; |
| 2964 | } |
| 2965 | |
| 2966 | int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const |
| 2967 | { |
| 2968 | if (requested == 0) |
| 2969 | { |
| 2970 | return requested; |
| 2971 | } |
| 2972 | |
| 2973 | std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format); |
| 2974 | if (itr == mMultiSampleSupport.end()) |
| 2975 | { |
| 2976 | return -1; |
| 2977 | } |
| 2978 | |
| 2979 | for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) |
| 2980 | { |
| 2981 | if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) |
| 2982 | { |
| 2983 | return i; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | return -1; |
| 2988 | } |
| 2989 | |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 2990 | bool Context::supportsEventQueries() const |
| 2991 | { |
| 2992 | return mSupportsEventQueries; |
| 2993 | } |
| 2994 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 2995 | bool Context::supportsCompressedTextures() const |
| 2996 | { |
| 2997 | return mSupportsCompressedTextures; |
| 2998 | } |
| 2999 | |
daniel@transgaming.com | 0a337e9 | 2010-08-28 17:38:27 +0000 | [diff] [blame] | 3000 | bool Context::supportsFloatTextures() const |
| 3001 | { |
| 3002 | return mSupportsFloatTextures; |
| 3003 | } |
| 3004 | |
| 3005 | bool Context::supportsFloatLinearFilter() const |
| 3006 | { |
| 3007 | return mSupportsFloatLinearFilter; |
| 3008 | } |
| 3009 | |
daniel@transgaming.com | 1297d92 | 2010-09-01 15:47:47 +0000 | [diff] [blame] | 3010 | bool Context::supportsFloatRenderableTextures() const |
| 3011 | { |
| 3012 | return mSupportsFloatRenderableTextures; |
| 3013 | } |
| 3014 | |
daniel@transgaming.com | 0a337e9 | 2010-08-28 17:38:27 +0000 | [diff] [blame] | 3015 | bool Context::supportsHalfFloatTextures() const |
| 3016 | { |
| 3017 | return mSupportsHalfFloatTextures; |
| 3018 | } |
| 3019 | |
| 3020 | bool Context::supportsHalfFloatLinearFilter() const |
| 3021 | { |
| 3022 | return mSupportsHalfFloatLinearFilter; |
| 3023 | } |
| 3024 | |
daniel@transgaming.com | 1297d92 | 2010-09-01 15:47:47 +0000 | [diff] [blame] | 3025 | bool Context::supportsHalfFloatRenderableTextures() const |
| 3026 | { |
| 3027 | return mSupportsHalfFloatRenderableTextures; |
| 3028 | } |
| 3029 | |
daniel@transgaming.com | 5d752f2 | 2010-10-07 13:37:20 +0000 | [diff] [blame] | 3030 | int Context::getMaximumRenderbufferDimension() const |
| 3031 | { |
| 3032 | return mMaxRenderbufferDimension; |
| 3033 | } |
| 3034 | |
| 3035 | int Context::getMaximumTextureDimension() const |
| 3036 | { |
| 3037 | return mMaxTextureDimension; |
| 3038 | } |
| 3039 | |
| 3040 | int Context::getMaximumCubeTextureDimension() const |
| 3041 | { |
| 3042 | return mMaxCubeTextureDimension; |
| 3043 | } |
| 3044 | |
| 3045 | int Context::getMaximumTextureLevel() const |
| 3046 | { |
| 3047 | return mMaxTextureLevel; |
| 3048 | } |
| 3049 | |
daniel@transgaming.com | ed828e5 | 2010-10-15 17:57:30 +0000 | [diff] [blame] | 3050 | bool Context::supportsLuminanceTextures() const |
| 3051 | { |
| 3052 | return mSupportsLuminanceTextures; |
| 3053 | } |
| 3054 | |
| 3055 | bool Context::supportsLuminanceAlphaTextures() const |
| 3056 | { |
| 3057 | return mSupportsLuminanceAlphaTextures; |
| 3058 | } |
| 3059 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 3060 | bool Context::supports32bitIndices() const |
| 3061 | { |
| 3062 | return mSupports32bitIndices; |
| 3063 | } |
| 3064 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3065 | void Context::detachBuffer(GLuint buffer) |
| 3066 | { |
| 3067 | // [OpenGL ES 2.0.24] section 2.9 page 22: |
| 3068 | // If a buffer object is deleted while it is bound, all bindings to that object in the current context |
| 3069 | // (i.e. in the thread that called Delete-Buffers) are reset to zero. |
| 3070 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3071 | if (mState.arrayBuffer.id() == buffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3072 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3073 | mState.arrayBuffer.set(NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3076 | if (mState.elementArrayBuffer.id() == buffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3077 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3078 | mState.elementArrayBuffer.set(NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) |
| 3082 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3083 | if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3084 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3085 | mState.vertexAttribute[attribute].mBoundBuffer.set(NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | void Context::detachTexture(GLuint texture) |
| 3091 | { |
| 3092 | // [OpenGL ES 2.0.24] section 3.8 page 84: |
| 3093 | // If a texture object is deleted, it is as if all texture units which are bound to that texture object are |
| 3094 | // rebound to texture object zero |
| 3095 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 3096 | for (int type = 0; type < TEXTURE_TYPE_COUNT; type++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3097 | { |
daniel@transgaming.com | 3f74c7a | 2011-05-11 15:36:51 +0000 | [diff] [blame] | 3098 | for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3099 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3100 | if (mState.samplerTexture[type][sampler].id() == texture) |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 3101 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3102 | mState.samplerTexture[type][sampler].set(NULL); |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 3103 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | // [OpenGL ES 2.0.24] section 4.4 page 112: |
| 3108 | // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is |
| 3109 | // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this |
| 3110 | // image was attached in the currently bound framebuffer. |
| 3111 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3112 | Framebuffer *readFramebuffer = getReadFramebuffer(); |
| 3113 | Framebuffer *drawFramebuffer = getDrawFramebuffer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3114 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3115 | if (readFramebuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3116 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3117 | readFramebuffer->detachTexture(texture); |
| 3118 | } |
| 3119 | |
| 3120 | if (drawFramebuffer && drawFramebuffer != readFramebuffer) |
| 3121 | { |
| 3122 | drawFramebuffer->detachTexture(texture); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | void Context::detachFramebuffer(GLuint framebuffer) |
| 3127 | { |
| 3128 | // [OpenGL ES 2.0.24] section 4.4 page 107: |
| 3129 | // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though |
| 3130 | // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero. |
| 3131 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 3132 | if (mState.readFramebuffer == framebuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3133 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3134 | bindReadFramebuffer(0); |
| 3135 | } |
| 3136 | |
apatrick@chromium.org | ff8bdfb | 2010-09-15 17:27:49 +0000 | [diff] [blame] | 3137 | if (mState.drawFramebuffer == framebuffer) |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3138 | { |
| 3139 | bindDrawFramebuffer(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | void Context::detachRenderbuffer(GLuint renderbuffer) |
| 3144 | { |
| 3145 | // [OpenGL ES 2.0.24] section 4.4 page 109: |
| 3146 | // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer |
| 3147 | // had been executed with the target RENDERBUFFER and name of zero. |
| 3148 | |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3149 | if (mState.renderbuffer.id() == renderbuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3150 | { |
| 3151 | bindRenderbuffer(0); |
| 3152 | } |
| 3153 | |
| 3154 | // [OpenGL ES 2.0.24] section 4.4 page 111: |
| 3155 | // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer, |
| 3156 | // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment |
| 3157 | // point to which this image was attached in the currently bound framebuffer. |
| 3158 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3159 | Framebuffer *readFramebuffer = getReadFramebuffer(); |
| 3160 | Framebuffer *drawFramebuffer = getDrawFramebuffer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3161 | |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3162 | if (readFramebuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3163 | { |
daniel@transgaming.com | a27ff1e | 2010-08-24 19:20:11 +0000 | [diff] [blame] | 3164 | readFramebuffer->detachRenderbuffer(renderbuffer); |
| 3165 | } |
| 3166 | |
| 3167 | if (drawFramebuffer && drawFramebuffer != readFramebuffer) |
| 3168 | { |
| 3169 | drawFramebuffer->detachRenderbuffer(renderbuffer); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3170 | } |
| 3171 | } |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3172 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 3173 | Texture *Context::getIncompleteTexture(TextureType type) |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3174 | { |
apatrick@chromium.org | 4e3bad4 | 2010-09-15 17:31:48 +0000 | [diff] [blame] | 3175 | Texture *t = mIncompleteTextures[type].get(); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3176 | |
| 3177 | if (t == NULL) |
| 3178 | { |
| 3179 | static const GLubyte color[] = { 0, 0, 0, 255 }; |
| 3180 | |
| 3181 | switch (type) |
| 3182 | { |
| 3183 | default: |
| 3184 | UNREACHABLE(); |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 3185 | // default falls through to TEXTURE_2D |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3186 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 3187 | case TEXTURE_2D: |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3188 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3189 | Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID); |
daniel@transgaming.com | 8a0a2db | 2011-03-21 16:38:20 +0000 | [diff] [blame] | 3190 | incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3191 | t = incomplete2d; |
| 3192 | } |
| 3193 | break; |
| 3194 | |
daniel@transgaming.com | 0e64dd6 | 2011-05-11 15:36:37 +0000 | [diff] [blame] | 3195 | case TEXTURE_CUBE: |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3196 | { |
daniel@transgaming.com | 9ecb9f9 | 2010-07-28 19:21:12 +0000 | [diff] [blame] | 3197 | TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3198 | |
daniel@transgaming.com | 8a0a2db | 2011-03-21 16:38:20 +0000 | [diff] [blame] | 3199 | incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 3200 | incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 3201 | incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 3202 | incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 3203 | incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 3204 | incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3205 | |
| 3206 | t = incompleteCube; |
| 3207 | } |
| 3208 | break; |
| 3209 | } |
| 3210 | |
apatrick@chromium.org | 4e3bad4 | 2010-09-15 17:31:48 +0000 | [diff] [blame] | 3211 | mIncompleteTextures[type].set(t); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | return t; |
| 3215 | } |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 3216 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 3217 | bool Context::cullSkipsDraw(GLenum drawMode) |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 3218 | { |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 3219 | return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode); |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 3222 | bool Context::isTriangleMode(GLenum drawMode) |
| 3223 | { |
| 3224 | switch (drawMode) |
| 3225 | { |
| 3226 | case GL_TRIANGLES: |
| 3227 | case GL_TRIANGLE_FAN: |
| 3228 | case GL_TRIANGLE_STRIP: |
| 3229 | return true; |
| 3230 | case GL_POINTS: |
| 3231 | case GL_LINES: |
| 3232 | case GL_LINE_LOOP: |
| 3233 | case GL_LINE_STRIP: |
| 3234 | return false; |
| 3235 | default: UNREACHABLE(); |
| 3236 | } |
| 3237 | |
| 3238 | return false; |
| 3239 | } |
daniel@transgaming.com | e4b08c8 | 2010-04-20 18:53:06 +0000 | [diff] [blame] | 3240 | |
| 3241 | void Context::setVertexAttrib(GLuint index, const GLfloat *values) |
| 3242 | { |
| 3243 | ASSERT(index < gl::MAX_VERTEX_ATTRIBS); |
| 3244 | |
daniel@transgaming.com | 428d158 | 2010-05-04 03:35:25 +0000 | [diff] [blame] | 3245 | mState.vertexAttribute[index].mCurrentValue[0] = values[0]; |
| 3246 | mState.vertexAttribute[index].mCurrentValue[1] = values[1]; |
| 3247 | mState.vertexAttribute[index].mCurrentValue[2] = values[2]; |
| 3248 | mState.vertexAttribute[index].mCurrentValue[3] = values[3]; |
daniel@transgaming.com | e4b08c8 | 2010-04-20 18:53:06 +0000 | [diff] [blame] | 3249 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 3250 | mVertexDataManager->dirtyCurrentValue(index); |
daniel@transgaming.com | e4b08c8 | 2010-04-20 18:53:06 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 3253 | void Context::initExtensionString() |
| 3254 | { |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 3255 | mExtensionString += "GL_OES_packed_depth_stencil "; |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 3256 | mExtensionString += "GL_EXT_texture_format_BGRA8888 "; |
| 3257 | mExtensionString += "GL_EXT_read_format_bgra "; |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3258 | mExtensionString += "GL_ANGLE_framebuffer_blit "; |
daniel@transgaming.com | d36c297 | 2010-08-24 19:21:07 +0000 | [diff] [blame] | 3259 | mExtensionString += "GL_OES_rgb8_rgba8 "; |
alokp@chromium.org | d303ef9 | 2010-09-09 17:30:15 +0000 | [diff] [blame] | 3260 | mExtensionString += "GL_OES_standard_derivatives "; |
daniel@transgaming.com | cdacc8e | 2010-07-28 19:20:50 +0000 | [diff] [blame] | 3261 | |
apatrick@chromium.org | d3bd0ad | 2010-08-30 18:55:36 +0000 | [diff] [blame] | 3262 | if (supportsEventQueries()) |
| 3263 | { |
| 3264 | mExtensionString += "GL_NV_fence "; |
| 3265 | } |
| 3266 | |
daniel@transgaming.com | 0186813 | 2010-08-24 19:21:17 +0000 | [diff] [blame] | 3267 | if (supportsCompressedTextures()) |
| 3268 | { |
| 3269 | mExtensionString += "GL_EXT_texture_compression_dxt1 "; |
| 3270 | } |
daniel@transgaming.com | d470a1b | 2010-08-24 19:20:48 +0000 | [diff] [blame] | 3271 | |
daniel@transgaming.com | 0a337e9 | 2010-08-28 17:38:27 +0000 | [diff] [blame] | 3272 | if (supportsFloatTextures()) |
| 3273 | { |
| 3274 | mExtensionString += "GL_OES_texture_float "; |
| 3275 | } |
| 3276 | |
| 3277 | if (supportsHalfFloatTextures()) |
| 3278 | { |
| 3279 | mExtensionString += "GL_OES_texture_half_float "; |
| 3280 | } |
| 3281 | |
| 3282 | if (supportsFloatLinearFilter()) |
| 3283 | { |
| 3284 | mExtensionString += "GL_OES_texture_float_linear "; |
| 3285 | } |
| 3286 | |
| 3287 | if (supportsHalfFloatLinearFilter()) |
| 3288 | { |
| 3289 | mExtensionString += "GL_OES_texture_half_float_linear "; |
| 3290 | } |
| 3291 | |
daniel@transgaming.com | 3ea20e7 | 2010-08-24 19:20:58 +0000 | [diff] [blame] | 3292 | if (getMaxSupportedSamples() != 0) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3293 | { |
| 3294 | mExtensionString += "GL_ANGLE_framebuffer_multisample "; |
| 3295 | } |
| 3296 | |
daniel@transgaming.com | 8392138 | 2011-01-08 05:46:00 +0000 | [diff] [blame] | 3297 | if (supports32bitIndices()) |
daniel@transgaming.com | 3e4c600 | 2010-05-05 18:50:13 +0000 | [diff] [blame] | 3298 | { |
| 3299 | mExtensionString += "GL_OES_element_index_uint "; |
| 3300 | } |
| 3301 | |
| 3302 | std::string::size_type end = mExtensionString.find_last_not_of(' '); |
| 3303 | if (end != std::string::npos) |
| 3304 | { |
| 3305 | mExtensionString.resize(end+1); |
| 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | const char *Context::getExtensionString() const |
| 3310 | { |
| 3311 | return mExtensionString.c_str(); |
| 3312 | } |
| 3313 | |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3314 | void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, |
| 3315 | GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, |
| 3316 | GLbitfield mask) |
| 3317 | { |
| 3318 | IDirect3DDevice9 *device = getDevice(); |
| 3319 | |
| 3320 | Framebuffer *readFramebuffer = getReadFramebuffer(); |
| 3321 | Framebuffer *drawFramebuffer = getDrawFramebuffer(); |
| 3322 | |
| 3323 | if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE || |
| 3324 | !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 3325 | { |
| 3326 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
| 3327 | } |
| 3328 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3329 | if (drawFramebuffer->getSamples() != 0) |
| 3330 | { |
| 3331 | return error(GL_INVALID_OPERATION); |
| 3332 | } |
| 3333 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 3334 | int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth(); |
| 3335 | int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight(); |
| 3336 | int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth(); |
| 3337 | int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight(); |
| 3338 | |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3339 | RECT sourceRect; |
| 3340 | RECT destRect; |
| 3341 | |
| 3342 | if (srcX0 < srcX1) |
| 3343 | { |
| 3344 | sourceRect.left = srcX0; |
| 3345 | sourceRect.right = srcX1; |
| 3346 | destRect.left = dstX0; |
| 3347 | destRect.right = dstX1; |
| 3348 | } |
| 3349 | else |
| 3350 | { |
| 3351 | sourceRect.left = srcX1; |
| 3352 | destRect.left = dstX1; |
| 3353 | sourceRect.right = srcX0; |
| 3354 | destRect.right = dstX0; |
| 3355 | } |
| 3356 | |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3357 | if (srcY0 < srcY1) |
| 3358 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 3359 | sourceRect.top = readBufferHeight - srcY1; |
| 3360 | destRect.top = drawBufferHeight - dstY1; |
| 3361 | sourceRect.bottom = readBufferHeight - srcY0; |
| 3362 | destRect.bottom = drawBufferHeight - dstY0; |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3363 | } |
| 3364 | else |
| 3365 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 3366 | sourceRect.top = readBufferHeight - srcY0; |
| 3367 | destRect.top = drawBufferHeight - dstY0; |
| 3368 | sourceRect.bottom = readBufferHeight - srcY1; |
| 3369 | destRect.bottom = drawBufferHeight - dstY1; |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | RECT sourceScissoredRect = sourceRect; |
| 3373 | RECT destScissoredRect = destRect; |
| 3374 | |
| 3375 | if (mState.scissorTest) |
| 3376 | { |
| 3377 | // Only write to parts of the destination framebuffer which pass the scissor test |
| 3378 | // Please note: the destRect is now in D3D-style coordinates, so the *top* of the |
| 3379 | // rect will be checked against scissorY, rather than the bottom. |
| 3380 | if (destRect.left < mState.scissorX) |
| 3381 | { |
| 3382 | int xDiff = mState.scissorX - destRect.left; |
| 3383 | destScissoredRect.left = mState.scissorX; |
| 3384 | sourceScissoredRect.left += xDiff; |
| 3385 | } |
| 3386 | |
| 3387 | if (destRect.right > mState.scissorX + mState.scissorWidth) |
| 3388 | { |
| 3389 | int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth); |
| 3390 | destScissoredRect.right = mState.scissorX + mState.scissorWidth; |
| 3391 | sourceScissoredRect.right -= xDiff; |
| 3392 | } |
| 3393 | |
| 3394 | if (destRect.top < mState.scissorY) |
| 3395 | { |
| 3396 | int yDiff = mState.scissorY - destRect.top; |
| 3397 | destScissoredRect.top = mState.scissorY; |
| 3398 | sourceScissoredRect.top += yDiff; |
| 3399 | } |
| 3400 | |
| 3401 | if (destRect.bottom > mState.scissorY + mState.scissorHeight) |
| 3402 | { |
| 3403 | int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight); |
| 3404 | destScissoredRect.bottom = mState.scissorY + mState.scissorHeight; |
| 3405 | sourceScissoredRect.bottom -= yDiff; |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | bool blitRenderTarget = false; |
| 3410 | bool blitDepthStencil = false; |
| 3411 | |
| 3412 | RECT sourceTrimmedRect = sourceScissoredRect; |
| 3413 | RECT destTrimmedRect = destScissoredRect; |
| 3414 | |
| 3415 | // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of |
| 3416 | // the actual draw and read surfaces. |
| 3417 | if (sourceTrimmedRect.left < 0) |
| 3418 | { |
| 3419 | int xDiff = 0 - sourceTrimmedRect.left; |
| 3420 | sourceTrimmedRect.left = 0; |
| 3421 | destTrimmedRect.left += xDiff; |
| 3422 | } |
| 3423 | |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3424 | if (sourceTrimmedRect.right > readBufferWidth) |
| 3425 | { |
| 3426 | int xDiff = sourceTrimmedRect.right - readBufferWidth; |
| 3427 | sourceTrimmedRect.right = readBufferWidth; |
| 3428 | destTrimmedRect.right -= xDiff; |
| 3429 | } |
| 3430 | |
| 3431 | if (sourceTrimmedRect.top < 0) |
| 3432 | { |
| 3433 | int yDiff = 0 - sourceTrimmedRect.top; |
| 3434 | sourceTrimmedRect.top = 0; |
| 3435 | destTrimmedRect.top += yDiff; |
| 3436 | } |
| 3437 | |
| 3438 | if (sourceTrimmedRect.bottom > readBufferHeight) |
| 3439 | { |
| 3440 | int yDiff = sourceTrimmedRect.bottom - readBufferHeight; |
| 3441 | sourceTrimmedRect.bottom = readBufferHeight; |
| 3442 | destTrimmedRect.bottom -= yDiff; |
| 3443 | } |
| 3444 | |
| 3445 | if (destTrimmedRect.left < 0) |
| 3446 | { |
| 3447 | int xDiff = 0 - destTrimmedRect.left; |
| 3448 | destTrimmedRect.left = 0; |
| 3449 | sourceTrimmedRect.left += xDiff; |
| 3450 | } |
| 3451 | |
| 3452 | if (destTrimmedRect.right > drawBufferWidth) |
| 3453 | { |
| 3454 | int xDiff = destTrimmedRect.right - drawBufferWidth; |
| 3455 | destTrimmedRect.right = drawBufferWidth; |
| 3456 | sourceTrimmedRect.right -= xDiff; |
| 3457 | } |
| 3458 | |
| 3459 | if (destTrimmedRect.top < 0) |
| 3460 | { |
| 3461 | int yDiff = 0 - destTrimmedRect.top; |
| 3462 | destTrimmedRect.top = 0; |
| 3463 | sourceTrimmedRect.top += yDiff; |
| 3464 | } |
| 3465 | |
| 3466 | if (destTrimmedRect.bottom > drawBufferHeight) |
| 3467 | { |
| 3468 | int yDiff = destTrimmedRect.bottom - drawBufferHeight; |
| 3469 | destTrimmedRect.bottom = drawBufferHeight; |
| 3470 | sourceTrimmedRect.bottom -= yDiff; |
| 3471 | } |
| 3472 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3473 | bool partialBufferCopy = false; |
daniel@transgaming.com | 3aba733 | 2011-01-14 15:08:35 +0000 | [diff] [blame] | 3474 | if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight || |
| 3475 | sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth || |
| 3476 | destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight || |
| 3477 | destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth || |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3478 | sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0) |
| 3479 | { |
| 3480 | partialBufferCopy = true; |
| 3481 | } |
| 3482 | |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3483 | if (mask & GL_COLOR_BUFFER_BIT) |
| 3484 | { |
enne@chromium.org | 0fa7463 | 2010-09-21 16:18:52 +0000 | [diff] [blame] | 3485 | const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D || |
| 3486 | readFramebuffer->getColorbufferType() == GL_RENDERBUFFER; |
| 3487 | const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D || |
| 3488 | drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER; |
| 3489 | if (!validReadType || !validDrawType || |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3490 | readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat()) |
| 3491 | { |
| 3492 | ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation"); |
| 3493 | return error(GL_INVALID_OPERATION); |
| 3494 | } |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3495 | |
| 3496 | if (partialBufferCopy && readFramebuffer->getSamples() != 0) |
| 3497 | { |
| 3498 | return error(GL_INVALID_OPERATION); |
| 3499 | } |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3500 | |
| 3501 | blitRenderTarget = true; |
| 3502 | |
| 3503 | } |
| 3504 | |
| 3505 | if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)) |
| 3506 | { |
| 3507 | DepthStencilbuffer *readDSBuffer = NULL; |
| 3508 | DepthStencilbuffer *drawDSBuffer = NULL; |
| 3509 | |
| 3510 | // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have |
| 3511 | // both a depth and stencil buffer, it will be the same buffer. |
| 3512 | |
| 3513 | if (mask & GL_DEPTH_BUFFER_BIT) |
| 3514 | { |
| 3515 | if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer()) |
| 3516 | { |
| 3517 | if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() || |
| 3518 | readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat()) |
| 3519 | { |
| 3520 | return error(GL_INVALID_OPERATION); |
| 3521 | } |
| 3522 | |
| 3523 | blitDepthStencil = true; |
| 3524 | readDSBuffer = readFramebuffer->getDepthbuffer(); |
| 3525 | drawDSBuffer = drawFramebuffer->getDepthbuffer(); |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | if (mask & GL_STENCIL_BUFFER_BIT) |
| 3530 | { |
| 3531 | if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer()) |
| 3532 | { |
| 3533 | if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() || |
| 3534 | readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat()) |
| 3535 | { |
| 3536 | return error(GL_INVALID_OPERATION); |
| 3537 | } |
| 3538 | |
| 3539 | blitDepthStencil = true; |
| 3540 | readDSBuffer = readFramebuffer->getStencilbuffer(); |
| 3541 | drawDSBuffer = drawFramebuffer->getStencilbuffer(); |
| 3542 | } |
| 3543 | } |
| 3544 | |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3545 | if (partialBufferCopy) |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3546 | { |
| 3547 | ERR("Only whole-buffer depth and stencil blits are supported by this implementation."); |
| 3548 | return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted |
| 3549 | } |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3550 | |
daniel@transgaming.com | 97446d2 | 2010-08-24 19:20:54 +0000 | [diff] [blame] | 3551 | if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) || |
| 3552 | (readDSBuffer && readDSBuffer->getSamples() != 0)) |
daniel@transgaming.com | 1f135d8 | 2010-08-24 19:20:36 +0000 | [diff] [blame] | 3553 | { |
| 3554 | return error(GL_INVALID_OPERATION); |
| 3555 | } |
daniel@transgaming.com | 4cbc590 | 2010-08-24 19:20:26 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | if (blitRenderTarget || blitDepthStencil) |
| 3559 | { |
| 3560 | egl::Display *display = getDisplay(); |
| 3561 | display->endScene(); |
| 3562 | |
| 3563 | if (blitRenderTarget) |
| 3564 | { |
| 3565 | HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect, |
| 3566 | drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE); |
| 3567 | |
| 3568 | if (FAILED(result)) |
| 3569 | { |
| 3570 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 3571 | return; |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | if (blitDepthStencil) |
| 3576 | { |
| 3577 | HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE); |
| 3578 | |
| 3579 | if (FAILED(result)) |
| 3580 | { |
| 3581 | ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); |
| 3582 | return; |
| 3583 | } |
| 3584 | } |
| 3585 | } |
| 3586 | } |
| 3587 | |
daniel@transgaming.com | 09c2c1a | 2011-04-13 14:57:16 +0000 | [diff] [blame] | 3588 | VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0) |
| 3589 | { |
| 3590 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 3591 | { |
| 3592 | mVertexDeclCache[i].vertexDeclaration = NULL; |
| 3593 | mVertexDeclCache[i].lruCount = 0; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | VertexDeclarationCache::~VertexDeclarationCache() |
| 3598 | { |
| 3599 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 3600 | { |
| 3601 | if (mVertexDeclCache[i].vertexDeclaration) |
| 3602 | { |
| 3603 | mVertexDeclCache[i].vertexDeclaration->Release(); |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | GLenum VertexDeclarationCache::applyDeclaration(TranslatedAttribute attributes[], Program *program) |
| 3609 | { |
| 3610 | IDirect3DDevice9 *device = getDevice(); |
| 3611 | |
| 3612 | D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1]; |
| 3613 | D3DVERTEXELEMENT9 *element = &elements[0]; |
| 3614 | |
| 3615 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 3616 | { |
| 3617 | if (attributes[i].active) |
| 3618 | { |
| 3619 | device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride); |
| 3620 | |
| 3621 | element->Stream = i; |
| 3622 | element->Offset = 0; |
| 3623 | element->Type = attributes[i].type; |
| 3624 | element->Method = D3DDECLMETHOD_DEFAULT; |
| 3625 | element->Usage = D3DDECLUSAGE_TEXCOORD; |
| 3626 | element->UsageIndex = program->getSemanticIndex(i); |
| 3627 | element++; |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | static const D3DVERTEXELEMENT9 end = D3DDECL_END(); |
| 3632 | *(element++) = end; |
| 3633 | |
| 3634 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 3635 | { |
| 3636 | VertexDeclCacheEntry *entry = &mVertexDeclCache[i]; |
| 3637 | if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration) |
| 3638 | { |
| 3639 | entry->lruCount = ++mMaxLru; |
| 3640 | device->SetVertexDeclaration(entry->vertexDeclaration); |
| 3641 | |
| 3642 | return GL_NO_ERROR; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | VertexDeclCacheEntry *lastCache = mVertexDeclCache; |
| 3647 | |
| 3648 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 3649 | { |
| 3650 | if (mVertexDeclCache[i].lruCount < lastCache->lruCount) |
| 3651 | { |
| 3652 | lastCache = &mVertexDeclCache[i]; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | if (lastCache->vertexDeclaration != NULL) |
| 3657 | { |
| 3658 | lastCache->vertexDeclaration->Release(); |
| 3659 | lastCache->vertexDeclaration = NULL; |
| 3660 | } |
| 3661 | |
| 3662 | memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)); |
| 3663 | device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration); |
| 3664 | device->SetVertexDeclaration(lastCache->vertexDeclaration); |
| 3665 | lastCache->lruCount = ++mMaxLru; |
| 3666 | |
| 3667 | return GL_NO_ERROR; |
| 3668 | } |
| 3669 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | extern "C" |
| 3673 | { |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 3674 | gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3675 | { |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 3676 | return new gl::Context(config, shareContext); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | void glDestroyContext(gl::Context *context) |
| 3680 | { |
| 3681 | delete context; |
| 3682 | |
| 3683 | if (context == gl::getContext()) |
| 3684 | { |
| 3685 | gl::makeCurrent(NULL, NULL, NULL); |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface) |
| 3690 | { |
| 3691 | gl::makeCurrent(context, display, surface); |
| 3692 | } |
| 3693 | |
| 3694 | gl::Context *glGetCurrentContext() |
| 3695 | { |
| 3696 | return gl::getContext(); |
| 3697 | } |
| 3698 | } |