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