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