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