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); |
| 40 | depthClearValue = 1.0f; |
| 41 | stencilClearValue = 0; |
| 42 | |
| 43 | cullFace = false; |
| 44 | cullMode = GL_BACK; |
| 45 | frontFace = GL_CCW; |
| 46 | depthTest = false; |
| 47 | depthFunc = GL_LESS; |
| 48 | blend = false; |
| 49 | sourceBlendRGB = GL_ONE; |
| 50 | sourceBlendAlpha = GL_ONE; |
| 51 | destBlendRGB = GL_ZERO; |
| 52 | destBlendAlpha = GL_ZERO; |
| 53 | blendEquationRGB = GL_FUNC_ADD; |
| 54 | blendEquationAlpha = GL_FUNC_ADD; |
| 55 | blendColor.red = 0; |
| 56 | blendColor.green = 0; |
| 57 | blendColor.blue = 0; |
| 58 | blendColor.alpha = 0; |
| 59 | stencilTest = false; |
| 60 | stencilFunc = GL_ALWAYS; |
| 61 | stencilRef = 0; |
| 62 | stencilMask = -1; |
| 63 | stencilWritemask = -1; |
| 64 | stencilBackFunc = GL_ALWAYS; |
| 65 | stencilBackRef = 0; |
| 66 | stencilBackMask = - 1; |
| 67 | stencilBackWritemask = -1; |
| 68 | stencilFail = GL_KEEP; |
| 69 | stencilPassDepthFail = GL_KEEP; |
| 70 | stencilPassDepthPass = GL_KEEP; |
| 71 | stencilBackFail = GL_KEEP; |
| 72 | stencilBackPassDepthFail = GL_KEEP; |
| 73 | stencilBackPassDepthPass = GL_KEEP; |
| 74 | polygonOffsetFill = false; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 75 | polygonOffsetFactor = 0.0f; |
| 76 | polygonOffsetUnits = 0.0f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | sampleAlphaToCoverage = false; |
| 78 | sampleCoverage = false; |
| 79 | sampleCoverageValue = 1.0f; |
| 80 | sampleCoverageInvert = GL_FALSE; |
| 81 | scissorTest = false; |
| 82 | dither = true; |
daniel@transgaming.com | 5949aa1 | 2010-03-21 04:31:15 +0000 | [diff] [blame] | 83 | generateMipmapHint = GL_DONT_CARE; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 84 | |
daniel@transgaming.com | 32e58cd | 2010-03-24 09:44:10 +0000 | [diff] [blame] | 85 | lineWidth = 1.0f; |
| 86 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 87 | viewportX = 0; |
| 88 | viewportY = 0; |
| 89 | viewportWidth = config->mDisplayMode.Width; |
| 90 | viewportHeight = config->mDisplayMode.Height; |
| 91 | zNear = 0.0f; |
| 92 | zFar = 1.0f; |
| 93 | |
| 94 | scissorX = 0; |
| 95 | scissorY = 0; |
| 96 | scissorWidth = config->mDisplayMode.Width; |
| 97 | scissorHeight = config->mDisplayMode.Height; |
| 98 | |
| 99 | colorMaskRed = true; |
| 100 | colorMaskGreen = true; |
| 101 | colorMaskBlue = true; |
| 102 | colorMaskAlpha = true; |
| 103 | depthMask = true; |
| 104 | |
| 105 | // [OpenGL ES 2.0.24] section 3.7 page 83: |
| 106 | // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional |
| 107 | // and cube map texture state vectors respectively associated with them. |
| 108 | // In order that access to these initial textures not be lost, they are treated as texture |
| 109 | // objects all of whose names are 0. |
| 110 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 111 | mTexture2DZero = new Texture2D(this); |
| 112 | mTextureCubeMapZero = new TextureCubeMap(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | |
| 114 | mColorbufferZero = NULL; |
| 115 | mDepthbufferZero = NULL; |
| 116 | mStencilbufferZero = NULL; |
| 117 | |
| 118 | activeSampler = 0; |
| 119 | arrayBuffer = 0; |
| 120 | elementArrayBuffer = 0; |
| 121 | bindTextureCubeMap(0); |
| 122 | bindTexture2D(0); |
| 123 | bindFramebuffer(0); |
| 124 | bindRenderbuffer(0); |
| 125 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 126 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 127 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 128 | for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++) |
| 129 | { |
| 130 | samplerTexture[type][sampler] = 0; |
| 131 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 132 | } |
| 133 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 134 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
| 135 | { |
| 136 | mIncompleteTextures[type] = NULL; |
| 137 | } |
| 138 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 139 | currentProgram = 0; |
| 140 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 141 | packAlignment = 4; |
| 142 | unpackAlignment = 4; |
| 143 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 144 | mBufferBackEnd = NULL; |
| 145 | mVertexDataManager = NULL; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 146 | mIndexDataManager = NULL; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 147 | mBlit = NULL; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 148 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 149 | mInvalidEnum = false; |
| 150 | mInvalidValue = false; |
| 151 | mInvalidOperation = false; |
| 152 | mOutOfMemory = false; |
| 153 | mInvalidFramebufferOperation = false; |
daniel@transgaming.com | 159acdf | 2010-03-21 04:31:24 +0000 | [diff] [blame] | 154 | |
| 155 | mHasBeenCurrent = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | Context::~Context() |
| 159 | { |
| 160 | currentProgram = 0; |
| 161 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 162 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
| 163 | { |
| 164 | delete mIncompleteTextures[type]; |
| 165 | } |
| 166 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 167 | delete mTexture2DZero; |
| 168 | delete mTextureCubeMapZero; |
| 169 | |
| 170 | delete mColorbufferZero; |
| 171 | delete mDepthbufferZero; |
| 172 | delete mStencilbufferZero; |
| 173 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 174 | delete mBufferBackEnd; |
| 175 | delete mVertexDataManager; |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 176 | delete mIndexDataManager; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 177 | delete mBlit; |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 178 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | while (!mBufferMap.empty()) |
| 180 | { |
| 181 | deleteBuffer(mBufferMap.begin()->first); |
| 182 | } |
| 183 | |
| 184 | while (!mProgramMap.empty()) |
| 185 | { |
| 186 | deleteProgram(mProgramMap.begin()->first); |
| 187 | } |
| 188 | |
| 189 | while (!mShaderMap.empty()) |
| 190 | { |
| 191 | deleteShader(mShaderMap.begin()->first); |
| 192 | } |
| 193 | |
| 194 | while (!mFramebufferMap.empty()) |
| 195 | { |
| 196 | deleteFramebuffer(mFramebufferMap.begin()->first); |
| 197 | } |
| 198 | |
| 199 | while (!mRenderbufferMap.empty()) |
| 200 | { |
| 201 | deleteRenderbuffer(mRenderbufferMap.begin()->first); |
| 202 | } |
| 203 | |
| 204 | while (!mTextureMap.empty()) |
| 205 | { |
| 206 | deleteTexture(mTextureMap.begin()->first); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void Context::makeCurrent(egl::Display *display, egl::Surface *surface) |
| 211 | { |
| 212 | IDirect3DDevice9 *device = display->getDevice(); |
| 213 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 214 | if (!mBufferBackEnd) |
| 215 | { |
| 216 | mBufferBackEnd = new Dx9BackEnd(device); |
| 217 | mVertexDataManager = new VertexDataManager(this, mBufferBackEnd); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 218 | mIndexDataManager = new IndexDataManager(this, mBufferBackEnd); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 219 | mBlit = new Blit(this); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 220 | } |
| 221 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 222 | // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names |
| 223 | IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget(); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 224 | IDirect3DSurface9 *depthStencil = surface->getDepthStencil(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 225 | |
| 226 | Framebuffer *framebufferZero = new Framebuffer(); |
| 227 | Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 228 | Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil); |
| 229 | Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 230 | |
| 231 | setFramebufferZero(framebufferZero); |
| 232 | setColorbufferZero(colorbufferZero); |
| 233 | setDepthbufferZero(depthbufferZero); |
| 234 | setStencilbufferZero(stencilbufferZero); |
| 235 | |
| 236 | framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0); |
| 237 | framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0); |
| 238 | framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0); |
| 239 | |
daniel@transgaming.com | 159acdf | 2010-03-21 04:31:24 +0000 | [diff] [blame] | 240 | if(!mHasBeenCurrent) |
| 241 | { |
| 242 | viewportX = 0; |
| 243 | viewportY = 0; |
| 244 | viewportWidth = surface->getWidth(); |
| 245 | viewportHeight = surface->getHeight(); |
| 246 | |
| 247 | scissorX = 0; |
| 248 | scissorY = 0; |
| 249 | scissorWidth = surface->getWidth(); |
| 250 | scissorHeight = surface->getHeight(); |
| 251 | |
| 252 | mHasBeenCurrent = true; |
| 253 | } |
| 254 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 255 | defaultRenderTarget->Release(); |
| 256 | |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 257 | if (depthStencil) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 258 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 259 | depthStencil->Release(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 260 | } |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 261 | |
| 262 | D3DCAPS9 capabilities; |
| 263 | device->GetDeviceCaps(&capabilities); |
| 264 | |
| 265 | if (capabilities.PixelShaderVersion == D3DPS_VERSION(3, 0)) |
| 266 | { |
| 267 | mPsProfile = "ps_3_0"; |
| 268 | mVsProfile = "vs_3_0"; |
| 269 | } |
| 270 | else // egl::Display guarantees support for at least 2.0 |
| 271 | { |
| 272 | mPsProfile = "ps_2_0"; |
| 273 | mVsProfile = "vs_2_0"; |
| 274 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void Context::setClearColor(float red, float green, float blue, float alpha) |
| 278 | { |
| 279 | colorClearValue.red = red; |
| 280 | colorClearValue.green = green; |
| 281 | colorClearValue.blue = blue; |
| 282 | colorClearValue.alpha = alpha; |
| 283 | } |
| 284 | |
| 285 | void Context::setClearDepth(float depth) |
| 286 | { |
| 287 | depthClearValue = depth; |
| 288 | } |
| 289 | |
| 290 | void Context::setClearStencil(int stencil) |
| 291 | { |
| 292 | stencilClearValue = stencil; |
| 293 | } |
| 294 | |
| 295 | // Returns an unused buffer name |
| 296 | GLuint Context::createBuffer() |
| 297 | { |
| 298 | unsigned int handle = 1; |
| 299 | |
| 300 | while (mBufferMap.find(handle) != mBufferMap.end()) |
| 301 | { |
| 302 | handle++; |
| 303 | } |
| 304 | |
| 305 | mBufferMap[handle] = NULL; |
| 306 | |
| 307 | return handle; |
| 308 | } |
| 309 | |
| 310 | // Returns an unused shader/program name |
| 311 | GLuint Context::createShader(GLenum type) |
| 312 | { |
| 313 | unsigned int handle = 1; |
| 314 | |
| 315 | while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space |
| 316 | { |
| 317 | handle++; |
| 318 | } |
| 319 | |
| 320 | if (type == GL_VERTEX_SHADER) |
| 321 | { |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 322 | mShaderMap[handle] = new VertexShader(handle); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 323 | } |
| 324 | else if (type == GL_FRAGMENT_SHADER) |
| 325 | { |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 326 | mShaderMap[handle] = new FragmentShader(handle); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 327 | } |
| 328 | else UNREACHABLE(); |
| 329 | |
| 330 | return handle; |
| 331 | } |
| 332 | |
| 333 | // Returns an unused program/shader name |
| 334 | GLuint Context::createProgram() |
| 335 | { |
| 336 | unsigned int handle = 1; |
| 337 | |
| 338 | while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space |
| 339 | { |
| 340 | handle++; |
| 341 | } |
| 342 | |
| 343 | mProgramMap[handle] = new Program(); |
| 344 | |
| 345 | return handle; |
| 346 | } |
| 347 | |
| 348 | // Returns an unused texture name |
| 349 | GLuint Context::createTexture() |
| 350 | { |
| 351 | unsigned int handle = 1; |
| 352 | |
| 353 | while (mTextureMap.find(handle) != mTextureMap.end()) |
| 354 | { |
| 355 | handle++; |
| 356 | } |
| 357 | |
| 358 | mTextureMap[handle] = NULL; |
| 359 | |
| 360 | return handle; |
| 361 | } |
| 362 | |
| 363 | // Returns an unused framebuffer name |
| 364 | GLuint Context::createFramebuffer() |
| 365 | { |
| 366 | unsigned int handle = 1; |
| 367 | |
| 368 | while (mFramebufferMap.find(handle) != mFramebufferMap.end()) |
| 369 | { |
| 370 | handle++; |
| 371 | } |
| 372 | |
| 373 | mFramebufferMap[handle] = NULL; |
| 374 | |
| 375 | return handle; |
| 376 | } |
| 377 | |
| 378 | // Returns an unused renderbuffer name |
| 379 | GLuint Context::createRenderbuffer() |
| 380 | { |
| 381 | unsigned int handle = 1; |
| 382 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 383 | while (mRenderbufferMap.find(handle) != mRenderbufferMap.end()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 384 | { |
| 385 | handle++; |
| 386 | } |
| 387 | |
| 388 | mRenderbufferMap[handle] = NULL; |
| 389 | |
| 390 | return handle; |
| 391 | } |
| 392 | |
| 393 | void Context::deleteBuffer(GLuint buffer) |
| 394 | { |
| 395 | BufferMap::iterator bufferObject = mBufferMap.find(buffer); |
| 396 | |
| 397 | if (bufferObject != mBufferMap.end()) |
| 398 | { |
| 399 | detachBuffer(buffer); |
| 400 | |
| 401 | delete bufferObject->second; |
| 402 | mBufferMap.erase(bufferObject); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void Context::deleteShader(GLuint shader) |
| 407 | { |
| 408 | ShaderMap::iterator shaderObject = mShaderMap.find(shader); |
| 409 | |
| 410 | if (shaderObject != mShaderMap.end()) |
| 411 | { |
| 412 | if (!shaderObject->second->isAttached()) |
| 413 | { |
| 414 | delete shaderObject->second; |
| 415 | mShaderMap.erase(shaderObject); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | shaderObject->second->flagForDeletion(); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void Context::deleteProgram(GLuint program) |
| 425 | { |
| 426 | ProgramMap::iterator programObject = mProgramMap.find(program); |
| 427 | |
| 428 | if (programObject != mProgramMap.end()) |
| 429 | { |
| 430 | if (program != currentProgram) |
| 431 | { |
| 432 | delete programObject->second; |
| 433 | mProgramMap.erase(programObject); |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | programObject->second->flagForDeletion(); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void Context::deleteTexture(GLuint texture) |
| 443 | { |
| 444 | TextureMap::iterator textureObject = mTextureMap.find(texture); |
| 445 | |
| 446 | if (textureObject != mTextureMap.end()) |
| 447 | { |
| 448 | detachTexture(texture); |
| 449 | |
| 450 | if (texture != 0) |
| 451 | { |
| 452 | delete textureObject->second; |
| 453 | } |
| 454 | |
| 455 | mTextureMap.erase(textureObject); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void Context::deleteFramebuffer(GLuint framebuffer) |
| 460 | { |
| 461 | FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer); |
| 462 | |
| 463 | if (framebufferObject != mFramebufferMap.end()) |
| 464 | { |
| 465 | detachFramebuffer(framebuffer); |
| 466 | |
| 467 | delete framebufferObject->second; |
| 468 | mFramebufferMap.erase(framebufferObject); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void Context::deleteRenderbuffer(GLuint renderbuffer) |
| 473 | { |
| 474 | RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer); |
| 475 | |
| 476 | if (renderbufferObject != mRenderbufferMap.end()) |
| 477 | { |
| 478 | detachRenderbuffer(renderbuffer); |
| 479 | |
| 480 | delete renderbufferObject->second; |
| 481 | mRenderbufferMap.erase(renderbufferObject); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | void Context::bindArrayBuffer(unsigned int buffer) |
| 486 | { |
| 487 | if (buffer != 0 && !getBuffer(buffer)) |
| 488 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 489 | mBufferMap[buffer] = new Buffer(mBufferBackEnd); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | arrayBuffer = buffer; |
| 493 | } |
| 494 | |
| 495 | void Context::bindElementArrayBuffer(unsigned int buffer) |
| 496 | { |
| 497 | if (buffer != 0 && !getBuffer(buffer)) |
| 498 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 499 | mBufferMap[buffer] = new Buffer(mBufferBackEnd); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | elementArrayBuffer = buffer; |
| 503 | } |
| 504 | |
| 505 | void Context::bindTexture2D(GLuint texture) |
| 506 | { |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 507 | if (!getTexture(texture) && texture != 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 508 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 509 | mTextureMap[texture] = new Texture2D(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | texture2D = texture; |
| 513 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 514 | samplerTexture[SAMPLER_2D][activeSampler] = texture; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void Context::bindTextureCubeMap(GLuint texture) |
| 518 | { |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 519 | if (!getTexture(texture) && texture != 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 520 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 521 | mTextureMap[texture] = new TextureCubeMap(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | textureCubeMap = texture; |
| 525 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 526 | samplerTexture[SAMPLER_CUBE][activeSampler] = texture; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void Context::bindFramebuffer(GLuint framebuffer) |
| 530 | { |
| 531 | if (!getFramebuffer(framebuffer)) |
| 532 | { |
| 533 | mFramebufferMap[framebuffer] = new Framebuffer(); |
| 534 | } |
| 535 | |
| 536 | this->framebuffer = framebuffer; |
| 537 | } |
| 538 | |
| 539 | void Context::bindRenderbuffer(GLuint renderbuffer) |
| 540 | { |
| 541 | if (renderbuffer != 0 && !getRenderbuffer(renderbuffer)) |
| 542 | { |
| 543 | mRenderbufferMap[renderbuffer] = new Renderbuffer(); |
| 544 | } |
| 545 | |
| 546 | this->renderbuffer = renderbuffer; |
| 547 | } |
| 548 | |
| 549 | void Context::useProgram(GLuint program) |
| 550 | { |
| 551 | Program *programObject = getCurrentProgram(); |
| 552 | |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 553 | GLuint priorProgram = currentProgram; |
| 554 | currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged. |
| 555 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 556 | if (programObject && programObject->isFlaggedForDeletion()) |
| 557 | { |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 558 | deleteProgram(priorProgram); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 559 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | void Context::setFramebufferZero(Framebuffer *buffer) |
| 563 | { |
| 564 | delete mFramebufferMap[0]; |
| 565 | mFramebufferMap[0] = buffer; |
| 566 | } |
| 567 | |
| 568 | void Context::setColorbufferZero(Colorbuffer *buffer) |
| 569 | { |
| 570 | delete mColorbufferZero; |
| 571 | mColorbufferZero = buffer; |
| 572 | } |
| 573 | |
| 574 | void Context::setDepthbufferZero(Depthbuffer *buffer) |
| 575 | { |
| 576 | delete mDepthbufferZero; |
| 577 | mDepthbufferZero = buffer; |
| 578 | } |
| 579 | |
| 580 | void Context::setStencilbufferZero(Stencilbuffer *buffer) |
| 581 | { |
| 582 | delete mStencilbufferZero; |
| 583 | mStencilbufferZero = buffer; |
| 584 | } |
| 585 | |
| 586 | void Context::setRenderbuffer(Renderbuffer *buffer) |
| 587 | { |
| 588 | delete mRenderbufferMap[renderbuffer]; |
| 589 | mRenderbufferMap[renderbuffer] = buffer; |
| 590 | } |
| 591 | |
| 592 | Buffer *Context::getBuffer(unsigned int handle) |
| 593 | { |
| 594 | BufferMap::iterator buffer = mBufferMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 595 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 596 | if (buffer == mBufferMap.end()) |
| 597 | { |
| 598 | return NULL; |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | return buffer->second; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | Shader *Context::getShader(unsigned int handle) |
| 607 | { |
| 608 | ShaderMap::iterator shader = mShaderMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 609 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 610 | if (shader == mShaderMap.end()) |
| 611 | { |
| 612 | return NULL; |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | return shader->second; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | Program *Context::getProgram(unsigned int handle) |
| 621 | { |
| 622 | ProgramMap::iterator program = mProgramMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 623 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 624 | if (program == mProgramMap.end()) |
| 625 | { |
| 626 | return NULL; |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | return program->second; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | Texture *Context::getTexture(unsigned int handle) |
| 635 | { |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 636 | if (handle == 0) return NULL; |
| 637 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 638 | TextureMap::iterator texture = mTextureMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 639 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 640 | if (texture == mTextureMap.end()) |
| 641 | { |
| 642 | return NULL; |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | return texture->second; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | Framebuffer *Context::getFramebuffer(unsigned int handle) |
| 651 | { |
| 652 | FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 653 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 654 | if (framebuffer == mFramebufferMap.end()) |
| 655 | { |
| 656 | return NULL; |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | return framebuffer->second; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | Renderbuffer *Context::getRenderbuffer(unsigned int handle) |
| 665 | { |
| 666 | RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 667 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 668 | if (renderbuffer == mRenderbufferMap.end()) |
| 669 | { |
| 670 | return NULL; |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | return renderbuffer->second; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | Colorbuffer *Context::getColorbuffer(GLuint handle) |
| 679 | { |
| 680 | if (handle != 0) |
| 681 | { |
| 682 | Renderbuffer *renderbuffer = getRenderbuffer(handle); |
| 683 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 684 | if (renderbuffer && renderbuffer->isColorbuffer()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 685 | { |
| 686 | return static_cast<Colorbuffer*>(renderbuffer); |
| 687 | } |
| 688 | } |
| 689 | else // Special case: 0 refers to different initial render targets based on the attachment type |
| 690 | { |
| 691 | return mColorbufferZero; |
| 692 | } |
| 693 | |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | Depthbuffer *Context::getDepthbuffer(GLuint handle) |
| 698 | { |
| 699 | if (handle != 0) |
| 700 | { |
| 701 | Renderbuffer *renderbuffer = getRenderbuffer(handle); |
| 702 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 703 | if (renderbuffer && renderbuffer->isDepthbuffer()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 704 | { |
| 705 | return static_cast<Depthbuffer*>(renderbuffer); |
| 706 | } |
| 707 | } |
| 708 | else // Special case: 0 refers to different initial render targets based on the attachment type |
| 709 | { |
| 710 | return mDepthbufferZero; |
| 711 | } |
| 712 | |
| 713 | return NULL; |
| 714 | } |
| 715 | |
| 716 | Stencilbuffer *Context::getStencilbuffer(GLuint handle) |
| 717 | { |
| 718 | if (handle != 0) |
| 719 | { |
| 720 | Renderbuffer *renderbuffer = getRenderbuffer(handle); |
| 721 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 722 | if (renderbuffer && renderbuffer->isStencilbuffer()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 723 | { |
| 724 | return static_cast<Stencilbuffer*>(renderbuffer); |
| 725 | } |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | return mStencilbufferZero; |
| 730 | } |
| 731 | |
| 732 | return NULL; |
| 733 | } |
| 734 | |
| 735 | Buffer *Context::getArrayBuffer() |
| 736 | { |
| 737 | return getBuffer(arrayBuffer); |
| 738 | } |
| 739 | |
| 740 | Buffer *Context::getElementArrayBuffer() |
| 741 | { |
| 742 | return getBuffer(elementArrayBuffer); |
| 743 | } |
| 744 | |
| 745 | Program *Context::getCurrentProgram() |
| 746 | { |
| 747 | return getProgram(currentProgram); |
| 748 | } |
| 749 | |
| 750 | Texture2D *Context::getTexture2D() |
| 751 | { |
| 752 | if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target |
| 753 | { |
| 754 | return mTexture2DZero; |
| 755 | } |
| 756 | |
| 757 | return (Texture2D*)getTexture(texture2D); |
| 758 | } |
| 759 | |
| 760 | TextureCubeMap *Context::getTextureCubeMap() |
| 761 | { |
| 762 | if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target |
| 763 | { |
| 764 | return mTextureCubeMapZero; |
| 765 | } |
| 766 | |
| 767 | return (TextureCubeMap*)getTexture(textureCubeMap); |
| 768 | } |
| 769 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 770 | Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 771 | { |
daniel@transgaming.com | 4195fc4 | 2010-04-08 03:51:09 +0000 | [diff] [blame] | 772 | GLuint texid = samplerTexture[type][sampler]; |
| 773 | |
| 774 | if (texid == 0) |
| 775 | { |
| 776 | switch (type) |
| 777 | { |
| 778 | default: UNREACHABLE(); |
| 779 | case SAMPLER_2D: return mTexture2DZero; |
| 780 | case SAMPLER_CUBE: return mTextureCubeMapZero; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return getTexture(texid); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | Framebuffer *Context::getFramebuffer() |
| 788 | { |
| 789 | return getFramebuffer(framebuffer); |
| 790 | } |
| 791 | |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 792 | bool Context::getBooleanv(GLenum pname, GLboolean *params) |
| 793 | { |
| 794 | switch (pname) |
| 795 | { |
| 796 | case GL_SHADER_COMPILER: *params = GL_TRUE; break; |
| 797 | case GL_SAMPLE_COVERAGE_INVERT: *params = sampleCoverageInvert; break; |
| 798 | case GL_DEPTH_WRITEMASK: *params = depthMask; break; |
| 799 | case GL_COLOR_WRITEMASK: |
| 800 | params[0] = colorMaskRed; |
| 801 | params[1] = colorMaskGreen; |
| 802 | params[2] = colorMaskBlue; |
| 803 | params[3] = colorMaskAlpha; |
| 804 | break; |
daniel@transgaming.com | 79f6677 | 2010-04-13 03:26:09 +0000 | [diff] [blame] | 805 | case GL_CULL_FACE: *params = cullFace; |
| 806 | case GL_POLYGON_OFFSET_FILL: *params = polygonOffsetFill; |
| 807 | case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = sampleAlphaToCoverage; |
| 808 | case GL_SAMPLE_COVERAGE: *params = sampleCoverage; |
| 809 | case GL_SCISSOR_TEST: *params = scissorTest; |
| 810 | case GL_STENCIL_TEST: *params = stencilTest; |
| 811 | case GL_DEPTH_TEST: *params = depthTest; |
| 812 | case GL_BLEND: *params = blend; |
| 813 | case GL_DITHER: *params = dither; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 814 | default: |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | bool Context::getFloatv(GLenum pname, GLfloat *params) |
| 822 | { |
| 823 | // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation |
| 824 | // because it is stored as a float, despite the fact that the GL ES 2.0 spec names |
| 825 | // GetIntegerv as its native query function. As it would require conversion in any |
| 826 | // case, this should make no difference to the calling application. |
| 827 | switch (pname) |
| 828 | { |
| 829 | case GL_LINE_WIDTH: *params = lineWidth; break; |
| 830 | case GL_SAMPLE_COVERAGE_VALUE: *params = sampleCoverageValue; break; |
| 831 | case GL_DEPTH_CLEAR_VALUE: *params = depthClearValue; break; |
| 832 | case GL_POLYGON_OFFSET_FACTOR: *params = polygonOffsetFactor; break; |
| 833 | case GL_POLYGON_OFFSET_UNITS: *params = polygonOffsetUnits; break; |
| 834 | case GL_ALIASED_LINE_WIDTH_RANGE: |
| 835 | params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN; |
| 836 | params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX; |
| 837 | break; |
| 838 | case GL_ALIASED_POINT_SIZE_RANGE: |
| 839 | params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN; |
| 840 | params[1] = gl::ALIASED_POINT_SIZE_RANGE_MAX; |
| 841 | break; |
| 842 | case GL_DEPTH_RANGE: |
| 843 | params[0] = zNear; |
| 844 | params[1] = zFar; |
| 845 | break; |
| 846 | case GL_COLOR_CLEAR_VALUE: |
| 847 | params[0] = colorClearValue.red; |
| 848 | params[1] = colorClearValue.green; |
| 849 | params[2] = colorClearValue.blue; |
| 850 | params[3] = colorClearValue.alpha; |
| 851 | break; |
daniel@transgaming.com | c164135 | 2010-04-26 15:33:36 +0000 | [diff] [blame] | 852 | case GL_BLEND_COLOR: |
| 853 | params[0] = blendColor.red; |
| 854 | params[1] = blendColor.green; |
| 855 | params[2] = blendColor.blue; |
| 856 | params[3] = blendColor.alpha; |
| 857 | break; |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 858 | default: |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | bool Context::getIntegerv(GLenum pname, GLint *params) |
| 866 | { |
| 867 | // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation |
| 868 | // because it is stored as a float, despite the fact that the GL ES 2.0 spec names |
| 869 | // GetIntegerv as its native query function. As it would require conversion in any |
| 870 | // case, this should make no difference to the calling application. You may find it in |
| 871 | // Context::getFloatv. |
| 872 | switch (pname) |
| 873 | { |
| 874 | case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break; |
| 875 | case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break; |
| 876 | case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break; |
| 877 | case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break; |
| 878 | case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break; |
| 879 | case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break; |
| 880 | case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break; |
| 881 | case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break; |
| 882 | case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break; |
| 883 | case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break; |
| 884 | case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break; |
| 885 | case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break; |
| 886 | case GL_ARRAY_BUFFER_BINDING: *params = arrayBuffer; break; |
| 887 | case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = elementArrayBuffer; break; |
| 888 | case GL_FRAMEBUFFER_BINDING: *params = framebuffer; break; |
| 889 | case GL_RENDERBUFFER_BINDING: *params = renderbuffer; break; |
| 890 | case GL_CURRENT_PROGRAM: *params = currentProgram; break; |
| 891 | case GL_PACK_ALIGNMENT: *params = packAlignment; break; |
| 892 | case GL_UNPACK_ALIGNMENT: *params = unpackAlignment; break; |
| 893 | case GL_GENERATE_MIPMAP_HINT: *params = generateMipmapHint; break; |
| 894 | case GL_ACTIVE_TEXTURE: *params = activeSampler; break; |
| 895 | case GL_STENCIL_FUNC: *params = stencilFunc; break; |
| 896 | case GL_STENCIL_REF: *params = stencilRef; break; |
| 897 | case GL_STENCIL_VALUE_MASK: *params = stencilMask; break; |
| 898 | case GL_STENCIL_BACK_FUNC: *params = stencilBackFunc; break; |
| 899 | case GL_STENCIL_BACK_REF: *params = stencilBackRef; break; |
| 900 | case GL_STENCIL_BACK_VALUE_MASK: *params = stencilBackMask; break; |
| 901 | case GL_STENCIL_FAIL: *params = stencilFail; break; |
| 902 | case GL_STENCIL_PASS_DEPTH_FAIL: *params = stencilPassDepthFail; break; |
| 903 | case GL_STENCIL_PASS_DEPTH_PASS: *params = stencilPassDepthPass; break; |
| 904 | case GL_STENCIL_BACK_FAIL: *params = stencilBackFail; break; |
| 905 | case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = stencilBackPassDepthFail; break; |
| 906 | case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = stencilBackPassDepthPass; break; |
| 907 | case GL_DEPTH_FUNC: *params = depthFunc; break; |
| 908 | case GL_BLEND_SRC_RGB: *params = sourceBlendRGB; break; |
| 909 | case GL_BLEND_SRC_ALPHA: *params = sourceBlendAlpha; break; |
| 910 | case GL_BLEND_DST_RGB: *params = destBlendRGB; break; |
| 911 | case GL_BLEND_DST_ALPHA: *params = destBlendAlpha; break; |
| 912 | case GL_BLEND_EQUATION_RGB: *params = blendEquationRGB; break; |
| 913 | case GL_BLEND_EQUATION_ALPHA: *params = blendEquationAlpha; break; |
| 914 | case GL_STENCIL_WRITEMASK: *params = stencilWritemask; break; |
| 915 | case GL_STENCIL_BACK_WRITEMASK: *params = stencilBackWritemask; break; |
| 916 | case GL_STENCIL_CLEAR_VALUE: *params = stencilClearValue; break; |
| 917 | case GL_SUBPIXEL_BITS: *params = 4; break; |
| 918 | case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break; |
| 919 | case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break; |
| 920 | case GL_SAMPLE_BUFFERS: *params = 0; break; |
| 921 | case GL_SAMPLES: *params = 0; break; |
| 922 | case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break; |
| 923 | case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break; |
| 924 | case GL_MAX_VIEWPORT_DIMS: |
| 925 | { |
| 926 | int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE); |
| 927 | params[0] = maxDimension; |
| 928 | params[1] = maxDimension; |
| 929 | } |
| 930 | break; |
| 931 | case GL_VIEWPORT: |
| 932 | params[0] = viewportX; |
| 933 | params[1] = viewportY; |
| 934 | params[2] = viewportWidth; |
| 935 | params[3] = viewportHeight; |
| 936 | break; |
| 937 | case GL_SCISSOR_BOX: |
| 938 | params[0] = scissorX; |
| 939 | params[1] = scissorY; |
| 940 | params[2] = scissorWidth; |
| 941 | params[3] = scissorHeight; |
| 942 | break; |
| 943 | case GL_CULL_FACE_MODE: *params = cullMode; break; |
| 944 | case GL_FRONT_FACE: *params = frontFace; break; |
| 945 | case GL_RED_BITS: |
| 946 | case GL_GREEN_BITS: |
| 947 | case GL_BLUE_BITS: |
| 948 | case GL_ALPHA_BITS: |
| 949 | { |
| 950 | gl::Framebuffer *framebuffer = getFramebuffer(); |
| 951 | gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 952 | |
| 953 | if (colorbuffer) |
| 954 | { |
| 955 | switch (pname) |
| 956 | { |
| 957 | case GL_RED_BITS: *params = colorbuffer->getRedSize(); break; |
| 958 | case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break; |
| 959 | case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break; |
| 960 | case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break; |
| 961 | } |
| 962 | } |
| 963 | else |
| 964 | { |
| 965 | *params = 0; |
| 966 | } |
| 967 | } |
| 968 | break; |
| 969 | case GL_DEPTH_BITS: |
| 970 | { |
| 971 | gl::Framebuffer *framebuffer = getFramebuffer(); |
| 972 | gl::Depthbuffer *depthbuffer = framebuffer->getDepthbuffer(); |
| 973 | |
| 974 | if (depthbuffer) |
| 975 | { |
| 976 | *params = depthbuffer->getDepthSize(); |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | *params = 0; |
| 981 | } |
| 982 | } |
| 983 | break; |
| 984 | case GL_STENCIL_BITS: |
| 985 | { |
| 986 | gl::Framebuffer *framebuffer = getFramebuffer(); |
| 987 | gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer(); |
| 988 | |
| 989 | if (stencilbuffer) |
| 990 | { |
| 991 | *params = stencilbuffer->getStencilSize(); |
| 992 | } |
| 993 | else |
| 994 | { |
| 995 | *params = 0; |
| 996 | } |
| 997 | } |
| 998 | break; |
| 999 | case GL_TEXTURE_BINDING_2D: |
| 1000 | { |
| 1001 | if (activeSampler < 0 || activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1) |
| 1002 | { |
| 1003 | error(GL_INVALID_OPERATION); |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | *params = samplerTexture[SAMPLER_2D][activeSampler]; |
| 1008 | } |
| 1009 | break; |
| 1010 | case GL_TEXTURE_BINDING_CUBE_MAP: |
| 1011 | { |
| 1012 | if (activeSampler < 0 || activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1) |
| 1013 | { |
| 1014 | error(GL_INVALID_OPERATION); |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | *params = samplerTexture[SAMPLER_CUBE][activeSampler]; |
| 1019 | } |
| 1020 | break; |
| 1021 | default: |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
| 1025 | return true; |
| 1026 | } |
| 1027 | |
| 1028 | bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) |
| 1029 | { |
| 1030 | // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation |
| 1031 | // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due |
| 1032 | // to the fact that it is stored internally as a float, and so would require conversion |
| 1033 | // if returned from Context::getIntegerv. Since this conversion is already implemented |
| 1034 | // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we |
| 1035 | // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling |
| 1036 | // application. |
| 1037 | switch (pname) |
| 1038 | { |
| 1039 | case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ |
| 1040 | case GL_SHADER_BINARY_FORMATS: |
| 1041 | { |
| 1042 | *type = GL_INT; |
| 1043 | *numParams = 0; |
| 1044 | } |
| 1045 | break; |
| 1046 | case GL_MAX_VERTEX_ATTRIBS: |
| 1047 | case GL_MAX_VERTEX_UNIFORM_VECTORS: |
| 1048 | case GL_MAX_VARYING_VECTORS: |
| 1049 | case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: |
| 1050 | case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: |
| 1051 | case GL_MAX_TEXTURE_IMAGE_UNITS: |
| 1052 | case GL_MAX_FRAGMENT_UNIFORM_VECTORS: |
| 1053 | case GL_MAX_RENDERBUFFER_SIZE: |
| 1054 | case GL_NUM_SHADER_BINARY_FORMATS: |
| 1055 | case GL_NUM_COMPRESSED_TEXTURE_FORMATS: |
| 1056 | case GL_ARRAY_BUFFER_BINDING: |
| 1057 | case GL_FRAMEBUFFER_BINDING: |
| 1058 | case GL_RENDERBUFFER_BINDING: |
| 1059 | case GL_CURRENT_PROGRAM: |
| 1060 | case GL_PACK_ALIGNMENT: |
| 1061 | case GL_UNPACK_ALIGNMENT: |
| 1062 | case GL_GENERATE_MIPMAP_HINT: |
| 1063 | case GL_RED_BITS: |
| 1064 | case GL_GREEN_BITS: |
| 1065 | case GL_BLUE_BITS: |
| 1066 | case GL_ALPHA_BITS: |
| 1067 | case GL_DEPTH_BITS: |
| 1068 | case GL_STENCIL_BITS: |
| 1069 | case GL_ELEMENT_ARRAY_BUFFER_BINDING: |
| 1070 | case GL_CULL_FACE_MODE: |
| 1071 | case GL_FRONT_FACE: |
| 1072 | case GL_ACTIVE_TEXTURE: |
| 1073 | case GL_STENCIL_FUNC: |
| 1074 | case GL_STENCIL_VALUE_MASK: |
| 1075 | case GL_STENCIL_REF: |
| 1076 | case GL_STENCIL_FAIL: |
| 1077 | case GL_STENCIL_PASS_DEPTH_FAIL: |
| 1078 | case GL_STENCIL_PASS_DEPTH_PASS: |
| 1079 | case GL_STENCIL_BACK_FUNC: |
| 1080 | case GL_STENCIL_BACK_VALUE_MASK: |
| 1081 | case GL_STENCIL_BACK_REF: |
| 1082 | case GL_STENCIL_BACK_FAIL: |
| 1083 | case GL_STENCIL_BACK_PASS_DEPTH_FAIL: |
| 1084 | case GL_STENCIL_BACK_PASS_DEPTH_PASS: |
| 1085 | case GL_DEPTH_FUNC: |
| 1086 | case GL_BLEND_SRC_RGB: |
| 1087 | case GL_BLEND_SRC_ALPHA: |
| 1088 | case GL_BLEND_DST_RGB: |
| 1089 | case GL_BLEND_DST_ALPHA: |
| 1090 | case GL_BLEND_EQUATION_RGB: |
| 1091 | case GL_BLEND_EQUATION_ALPHA: |
| 1092 | case GL_STENCIL_WRITEMASK: |
| 1093 | case GL_STENCIL_BACK_WRITEMASK: |
| 1094 | case GL_STENCIL_CLEAR_VALUE: |
| 1095 | case GL_SUBPIXEL_BITS: |
| 1096 | case GL_MAX_TEXTURE_SIZE: |
| 1097 | case GL_MAX_CUBE_MAP_TEXTURE_SIZE: |
| 1098 | case GL_SAMPLE_BUFFERS: |
| 1099 | case GL_SAMPLES: |
| 1100 | case GL_IMPLEMENTATION_COLOR_READ_TYPE: |
| 1101 | case GL_IMPLEMENTATION_COLOR_READ_FORMAT: |
| 1102 | case GL_TEXTURE_BINDING_2D: |
| 1103 | case GL_TEXTURE_BINDING_CUBE_MAP: |
| 1104 | { |
| 1105 | *type = GL_INT; |
| 1106 | *numParams = 1; |
| 1107 | } |
| 1108 | break; |
| 1109 | case GL_MAX_VIEWPORT_DIMS: |
| 1110 | { |
| 1111 | *type = GL_INT; |
| 1112 | *numParams = 2; |
| 1113 | } |
| 1114 | break; |
| 1115 | case GL_VIEWPORT: |
| 1116 | case GL_SCISSOR_BOX: |
| 1117 | { |
| 1118 | *type = GL_INT; |
| 1119 | *numParams = 4; |
| 1120 | } |
| 1121 | break; |
| 1122 | case GL_SHADER_COMPILER: |
| 1123 | case GL_SAMPLE_COVERAGE_INVERT: |
| 1124 | case GL_DEPTH_WRITEMASK: |
daniel@transgaming.com | 79f6677 | 2010-04-13 03:26:09 +0000 | [diff] [blame] | 1125 | case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled, |
| 1126 | case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries. |
| 1127 | case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural |
| 1128 | case GL_SAMPLE_COVERAGE: |
| 1129 | case GL_SCISSOR_TEST: |
| 1130 | case GL_STENCIL_TEST: |
| 1131 | case GL_DEPTH_TEST: |
| 1132 | case GL_BLEND: |
| 1133 | case GL_DITHER: |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1134 | { |
| 1135 | *type = GL_BOOL; |
| 1136 | *numParams = 1; |
| 1137 | } |
| 1138 | break; |
| 1139 | case GL_COLOR_WRITEMASK: |
| 1140 | { |
| 1141 | *type = GL_BOOL; |
| 1142 | *numParams = 4; |
| 1143 | } |
| 1144 | break; |
| 1145 | case GL_POLYGON_OFFSET_FACTOR: |
| 1146 | case GL_POLYGON_OFFSET_UNITS: |
| 1147 | case GL_SAMPLE_COVERAGE_VALUE: |
| 1148 | case GL_DEPTH_CLEAR_VALUE: |
| 1149 | case GL_LINE_WIDTH: |
| 1150 | { |
| 1151 | *type = GL_FLOAT; |
| 1152 | *numParams = 1; |
| 1153 | } |
| 1154 | break; |
| 1155 | case GL_ALIASED_LINE_WIDTH_RANGE: |
| 1156 | case GL_ALIASED_POINT_SIZE_RANGE: |
| 1157 | case GL_DEPTH_RANGE: |
| 1158 | { |
| 1159 | *type = GL_FLOAT; |
| 1160 | *numParams = 2; |
| 1161 | } |
| 1162 | break; |
| 1163 | case GL_COLOR_CLEAR_VALUE: |
daniel@transgaming.com | c164135 | 2010-04-26 15:33:36 +0000 | [diff] [blame] | 1164 | case GL_BLEND_COLOR: |
daniel@transgaming.com | 777f267 | 2010-04-07 03:25:16 +0000 | [diff] [blame] | 1165 | { |
| 1166 | *type = GL_FLOAT; |
| 1167 | *numParams = 4; |
| 1168 | } |
| 1169 | break; |
| 1170 | default: |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | return true; |
| 1175 | } |
| 1176 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1177 | // Applies the render target surface, depth stencil surface, viewport rectangle and |
| 1178 | // scissor rectangle to the Direct3D 9 device |
| 1179 | bool Context::applyRenderTarget(bool ignoreViewport) |
| 1180 | { |
| 1181 | IDirect3DDevice9 *device = getDevice(); |
| 1182 | Framebuffer *framebufferObject = getFramebuffer(); |
| 1183 | |
| 1184 | if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 1185 | { |
| 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget(); |
| 1190 | IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil(); |
| 1191 | |
| 1192 | device->SetRenderTarget(0, renderTarget); |
| 1193 | device->SetDepthStencilSurface(depthStencil); |
| 1194 | |
| 1195 | D3DVIEWPORT9 viewport; |
| 1196 | D3DSURFACE_DESC desc; |
| 1197 | renderTarget->GetDesc(&desc); |
| 1198 | |
| 1199 | if (ignoreViewport) |
| 1200 | { |
| 1201 | viewport.X = 0; |
| 1202 | viewport.Y = 0; |
| 1203 | viewport.Width = desc.Width; |
| 1204 | viewport.Height = desc.Height; |
| 1205 | viewport.MinZ = 0.0f; |
| 1206 | viewport.MaxZ = 1.0f; |
| 1207 | } |
| 1208 | else |
| 1209 | { |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 1210 | viewport.X = std::max(viewportX, 0); |
| 1211 | viewport.Y = std::max(viewportY, 0); |
| 1212 | viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X); |
| 1213 | viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1214 | viewport.MinZ = clamp01(zNear); |
| 1215 | viewport.MaxZ = clamp01(zFar); |
| 1216 | } |
| 1217 | |
| 1218 | device->SetViewport(&viewport); |
| 1219 | |
| 1220 | if (scissorTest) |
| 1221 | { |
| 1222 | RECT rect = {scissorX, |
| 1223 | scissorY, |
| 1224 | scissorX + scissorWidth, |
| 1225 | scissorY + scissorHeight}; |
| 1226 | |
| 1227 | device->SetScissorRect(&rect); |
| 1228 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); |
| 1229 | } |
| 1230 | else |
| 1231 | { |
| 1232 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); |
| 1233 | } |
| 1234 | |
| 1235 | if (currentProgram) |
| 1236 | { |
| 1237 | D3DSURFACE_DESC description; |
| 1238 | renderTarget->GetDesc(&description); |
| 1239 | Program *programObject = getCurrentProgram(); |
| 1240 | |
daniel@transgaming.com | 09fbfef | 2010-04-22 13:35:31 +0000 | [diff] [blame] | 1241 | GLint halfPixelSize = programObject->getUniformLocation("dx_HalfPixelSize"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1242 | GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height}; |
| 1243 | programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 1244 | |
daniel@transgaming.com | 09fbfef | 2010-04-22 13:35:31 +0000 | [diff] [blame] | 1245 | GLint window = programObject->getUniformLocation("dx_Window"); |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 1246 | GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f}; |
| 1247 | programObject->setUniform4fv(window, 1, (GLfloat*)&whxy); |
| 1248 | |
daniel@transgaming.com | 09fbfef | 2010-04-22 13:35:31 +0000 | [diff] [blame] | 1249 | GLint depth = programObject->getUniformLocation("dx_Depth"); |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 1250 | GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f}; |
| 1251 | programObject->setUniform2fv(depth, 1, (GLfloat*)&dz); |
| 1252 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1253 | GLint near = programObject->getUniformLocation("gl_DepthRange.near"); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 1254 | programObject->setUniform1fv(near, 1, &zNear); |
| 1255 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1256 | GLint far = programObject->getUniformLocation("gl_DepthRange.far"); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 1257 | programObject->setUniform1fv(far, 1, &zFar); |
| 1258 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1259 | GLint diff = programObject->getUniformLocation("gl_DepthRange.diff"); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 1260 | GLfloat zDiff = zFar - zNear; |
| 1261 | programObject->setUniform1fv(diff, 1, &zDiff); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | return true; |
| 1265 | } |
| 1266 | |
| 1267 | // 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] | 1268 | void Context::applyState(GLenum drawMode) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1269 | { |
| 1270 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1271 | Program *programObject = getCurrentProgram(); |
| 1272 | |
daniel@transgaming.com | 09fbfef | 2010-04-22 13:35:31 +0000 | [diff] [blame] | 1273 | GLint frontCCW = programObject->getUniformLocation("dx_FrontCCW"); |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 1274 | GLint ccw = (frontFace == GL_CCW); |
| 1275 | programObject->setUniform1iv(frontCCW, 1, &ccw); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1276 | |
daniel@transgaming.com | 09fbfef | 2010-04-22 13:35:31 +0000 | [diff] [blame] | 1277 | GLint pointsOrLines = programObject->getUniformLocation("dx_PointsOrLines"); |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 1278 | GLint alwaysFront = !isTriangleMode(drawMode); |
| 1279 | programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront); |
| 1280 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1281 | if (cullFace) |
| 1282 | { |
| 1283 | device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace)); |
| 1284 | } |
| 1285 | else |
| 1286 | { |
| 1287 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1288 | } |
| 1289 | |
| 1290 | if (depthTest) |
| 1291 | { |
| 1292 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 1293 | device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc)); |
| 1294 | } |
| 1295 | else |
| 1296 | { |
| 1297 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 1298 | } |
| 1299 | |
| 1300 | if (blend) |
| 1301 | { |
| 1302 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 1303 | |
| 1304 | if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 1305 | destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 1306 | { |
| 1307 | device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor)); |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha), |
| 1312 | unorm<8>(blendColor.alpha), |
| 1313 | unorm<8>(blendColor.alpha), |
| 1314 | unorm<8>(blendColor.alpha))); |
| 1315 | } |
| 1316 | |
| 1317 | device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB)); |
| 1318 | device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB)); |
| 1319 | device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB)); |
| 1320 | |
| 1321 | if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha) |
| 1322 | { |
| 1323 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 1324 | |
| 1325 | device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha)); |
| 1326 | device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha)); |
| 1327 | device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha)); |
| 1328 | |
| 1329 | } |
| 1330 | else |
| 1331 | { |
| 1332 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 1333 | } |
| 1334 | } |
| 1335 | else |
| 1336 | { |
| 1337 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1338 | } |
| 1339 | |
| 1340 | if (stencilTest) |
| 1341 | { |
| 1342 | device->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1343 | device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 1344 | |
| 1345 | // FIXME: Unsupported by D3D9 |
| 1346 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 1347 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 1348 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
daniel@transgaming.com | 1436e26 | 2010-03-17 03:58:56 +0000 | [diff] [blame] | 1349 | if(stencilWritemask != stencilBackWritemask || stencilRef != stencilBackRef || stencilMask != stencilBackMask) |
| 1350 | { |
| 1351 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 1352 | return error(GL_INVALID_OPERATION); |
| 1353 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1354 | |
| 1355 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask); |
| 1356 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc)); |
| 1357 | |
| 1358 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range |
| 1359 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask); |
| 1360 | |
| 1361 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail)); |
| 1362 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail)); |
| 1363 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass)); |
| 1364 | |
| 1365 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask); |
| 1366 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc)); |
| 1367 | |
| 1368 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range |
| 1369 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask); |
| 1370 | |
| 1371 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail)); |
| 1372 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail)); |
| 1373 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass)); |
| 1374 | } |
| 1375 | else |
| 1376 | { |
| 1377 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1378 | } |
| 1379 | |
| 1380 | device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha)); |
| 1381 | device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE); |
| 1382 | |
| 1383 | if (polygonOffsetFill) |
| 1384 | { |
daniel@transgaming.com | aede630 | 2010-04-29 03:35:48 +0000 | [diff] [blame^] | 1385 | gl::Depthbuffer *depthbuffer = getFramebuffer()->getDepthbuffer(); |
| 1386 | if (depthbuffer) |
| 1387 | { |
| 1388 | device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&polygonOffsetFactor)); |
| 1389 | float depthBias = ldexp(polygonOffsetUnits, -(int)(depthbuffer->getDepthSize())); |
| 1390 | device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias)); |
| 1391 | } |
| 1392 | } |
| 1393 | else |
| 1394 | { |
| 1395 | device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); |
| 1396 | device->SetRenderState(D3DRS_DEPTHBIAS, 0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | if (sampleAlphaToCoverage) |
| 1400 | { |
| 1401 | UNIMPLEMENTED(); // FIXME |
| 1402 | } |
| 1403 | |
| 1404 | if (sampleCoverage) |
| 1405 | { |
| 1406 | UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one |
| 1407 | } |
| 1408 | |
| 1409 | device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE); |
| 1410 | } |
| 1411 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 1412 | // 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] | 1413 | void Context::lookupAttributeMapping(TranslatedAttribute *attributes) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1414 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1415 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1416 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1417 | if (attributes[i].enabled) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1418 | { |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 1419 | attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1422 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1423 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1424 | // The indices parameter to glDrawElements can have two interpretations: |
| 1425 | // - as a pointer into client memory |
| 1426 | // - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer |
| 1427 | // Handle these cases here and return a pointer to the index data. |
| 1428 | const Index *Context::adjustIndexPointer(const void *indices) |
| 1429 | { |
| 1430 | if (elementArrayBuffer) |
| 1431 | { |
| 1432 | Buffer *buffer = getBuffer(elementArrayBuffer); |
| 1433 | return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices)); |
| 1434 | } |
| 1435 | else |
| 1436 | { |
| 1437 | return static_cast<const Index*>(indices); |
| 1438 | } |
| 1439 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1440 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1441 | void Context::applyVertexBuffer(GLint first, GLsizei count) |
| 1442 | { |
| 1443 | TranslatedAttribute translated[MAX_VERTEX_ATTRIBS]; |
| 1444 | |
| 1445 | mVertexDataManager->preRenderValidate(first, count, translated); |
| 1446 | |
| 1447 | lookupAttributeMapping(translated); |
| 1448 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1449 | mBufferBackEnd->setupAttributesPreDraw(translated); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1452 | void Context::applyVertexBuffer(const TranslatedIndexData &indexInfo) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1453 | { |
| 1454 | TranslatedAttribute translated[MAX_VERTEX_ATTRIBS]; |
| 1455 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1456 | mVertexDataManager->preRenderValidate(indexInfo, translated); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1457 | |
| 1458 | lookupAttributeMapping(translated); |
| 1459 | |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1460 | mBufferBackEnd->setupAttributesPreDraw(translated); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | // Applies the indices and element array bindings to the Direct3D 9 device |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1464 | TranslatedIndexData Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type) |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1465 | { |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1466 | TranslatedIndexData indexInfo = mIndexDataManager->preRenderValidate(mode, type, count, getBuffer(elementArrayBuffer), indices); |
| 1467 | mBufferBackEnd->setupIndicesPreDraw(indexInfo); |
| 1468 | return indexInfo; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | // Applies the shaders and shader constants to the Direct3D 9 device |
| 1472 | void Context::applyShaders() |
| 1473 | { |
| 1474 | IDirect3DDevice9 *device = getDevice(); |
| 1475 | Program *programObject = getCurrentProgram(); |
| 1476 | IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader(); |
| 1477 | IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader(); |
| 1478 | |
| 1479 | device->SetVertexShader(vertexShader); |
| 1480 | device->SetPixelShader(pixelShader); |
| 1481 | |
| 1482 | programObject->applyUniforms(); |
| 1483 | } |
| 1484 | |
| 1485 | // Applies the textures and sampler states to the Direct3D 9 device |
| 1486 | void Context::applyTextures() |
| 1487 | { |
| 1488 | IDirect3DDevice9 *device = getDevice(); |
| 1489 | Program *programObject = getCurrentProgram(); |
| 1490 | |
| 1491 | for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++) |
| 1492 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1493 | int textureUnit = programObject->getSamplerMapping(sampler); |
| 1494 | if (textureUnit != -1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1495 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1496 | SamplerType textureType = programObject->getSamplerType(sampler); |
| 1497 | |
| 1498 | Texture *texture = getSamplerTexture(textureUnit, textureType); |
| 1499 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1500 | if (texture->isComplete()) |
| 1501 | { |
| 1502 | GLenum wrapS = texture->getWrapS(); |
| 1503 | GLenum wrapT = texture->getWrapT(); |
| 1504 | GLenum minFilter = texture->getMinFilter(); |
| 1505 | GLenum magFilter = texture->getMagFilter(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1506 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1507 | device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS)); |
| 1508 | device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1509 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1510 | device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter)); |
| 1511 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
| 1512 | es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter); |
| 1513 | device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 1514 | device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1515 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1516 | device->SetTexture(sampler, texture->getTexture()); |
| 1517 | } |
| 1518 | else |
| 1519 | { |
| 1520 | device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture()); |
| 1521 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1522 | } |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1523 | else |
| 1524 | { |
| 1525 | device->SetTexture(sampler, NULL); |
| 1526 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels) |
| 1531 | { |
| 1532 | Framebuffer *framebuffer = getFramebuffer(); |
| 1533 | IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget(); |
| 1534 | IDirect3DDevice9 *device = getDevice(); |
| 1535 | |
| 1536 | D3DSURFACE_DESC desc; |
| 1537 | renderTarget->GetDesc(&desc); |
| 1538 | |
| 1539 | IDirect3DSurface9 *systemSurface; |
| 1540 | HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 1541 | |
| 1542 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1543 | { |
| 1544 | return error(GL_OUT_OF_MEMORY); |
| 1545 | } |
| 1546 | |
| 1547 | ASSERT(SUCCEEDED(result)); |
| 1548 | |
| 1549 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 1550 | { |
| 1551 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
| 1552 | } |
| 1553 | |
| 1554 | result = device->GetRenderTargetData(renderTarget, systemSurface); |
| 1555 | |
| 1556 | if (result == D3DERR_DRIVERINTERNALERROR) |
| 1557 | { |
| 1558 | systemSurface->Release(); |
| 1559 | |
| 1560 | return error(GL_OUT_OF_MEMORY); |
| 1561 | } |
| 1562 | |
| 1563 | if (FAILED(result)) |
| 1564 | { |
| 1565 | UNREACHABLE(); |
| 1566 | systemSurface->Release(); |
| 1567 | |
| 1568 | return; // No sensible error to generate |
| 1569 | } |
| 1570 | |
| 1571 | D3DLOCKED_RECT lock; |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 1572 | RECT rect = {std::max(x, 0), |
| 1573 | std::max(y, 0), |
| 1574 | std::min(x + width, (int)desc.Width), |
| 1575 | std::min(y + height, (int)desc.Height)}; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1576 | |
| 1577 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 1578 | |
| 1579 | if (FAILED(result)) |
| 1580 | { |
| 1581 | UNREACHABLE(); |
| 1582 | systemSurface->Release(); |
| 1583 | |
| 1584 | return; // No sensible error to generate |
| 1585 | } |
| 1586 | |
| 1587 | unsigned char *source = (unsigned char*)lock.pBits; |
| 1588 | unsigned char *dest = (unsigned char*)pixels; |
daniel@transgaming.com | afb2395 | 2010-04-13 03:25:54 +0000 | [diff] [blame] | 1589 | unsigned short *dest16 = (unsigned short*)pixels; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1590 | |
| 1591 | for (int j = 0; j < rect.bottom - rect.top; j++) |
| 1592 | { |
| 1593 | for (int i = 0; i < rect.right - rect.left; i++) |
| 1594 | { |
| 1595 | float r; |
| 1596 | float g; |
| 1597 | float b; |
| 1598 | float a; |
| 1599 | |
| 1600 | switch (desc.Format) |
| 1601 | { |
| 1602 | case D3DFMT_R5G6B5: |
| 1603 | { |
| 1604 | unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch); |
| 1605 | |
| 1606 | a = 1.0f; |
| 1607 | b = (rgb & 0x001F) * (1.0f / 0x001F); |
| 1608 | g = (rgb & 0x07E0) * (1.0f / 0x07E0); |
| 1609 | r = (rgb & 0xF800) * (1.0f / 0xF800); |
| 1610 | } |
| 1611 | break; |
| 1612 | case D3DFMT_X1R5G5B5: |
| 1613 | { |
| 1614 | unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch); |
| 1615 | |
| 1616 | a = 1.0f; |
| 1617 | b = (xrgb & 0x001F) * (1.0f / 0x001F); |
| 1618 | g = (xrgb & 0x03E0) * (1.0f / 0x03E0); |
| 1619 | r = (xrgb & 0x7C00) * (1.0f / 0x7C00); |
| 1620 | } |
| 1621 | break; |
| 1622 | case D3DFMT_A1R5G5B5: |
| 1623 | { |
| 1624 | unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch); |
| 1625 | |
| 1626 | a = (argb & 0x8000) ? 1.0f : 0.0f; |
| 1627 | b = (argb & 0x001F) * (1.0f / 0x001F); |
| 1628 | g = (argb & 0x03E0) * (1.0f / 0x03E0); |
| 1629 | r = (argb & 0x7C00) * (1.0f / 0x7C00); |
| 1630 | } |
| 1631 | break; |
| 1632 | case D3DFMT_A8R8G8B8: |
| 1633 | { |
| 1634 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch); |
| 1635 | |
| 1636 | a = (argb & 0xFF000000) * (1.0f / 0xFF000000); |
| 1637 | b = (argb & 0x000000FF) * (1.0f / 0x000000FF); |
| 1638 | g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 1639 | r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 1640 | } |
| 1641 | break; |
| 1642 | case D3DFMT_X8R8G8B8: |
| 1643 | { |
| 1644 | unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch); |
| 1645 | |
| 1646 | a = 1.0f; |
| 1647 | b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); |
| 1648 | g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 1649 | r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 1650 | } |
| 1651 | break; |
| 1652 | case D3DFMT_A2R10G10B10: |
| 1653 | { |
| 1654 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch); |
| 1655 | |
| 1656 | a = (argb & 0xC0000000) * (1.0f / 0xC0000000); |
| 1657 | b = (argb & 0x000003FF) * (1.0f / 0x000003FF); |
| 1658 | g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); |
| 1659 | r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); |
| 1660 | } |
| 1661 | break; |
| 1662 | default: |
| 1663 | UNIMPLEMENTED(); // FIXME |
| 1664 | UNREACHABLE(); |
| 1665 | } |
| 1666 | |
| 1667 | switch (format) |
| 1668 | { |
| 1669 | case GL_RGBA: |
| 1670 | switch (type) |
| 1671 | { |
| 1672 | case GL_UNSIGNED_BYTE: |
| 1673 | dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f); |
| 1674 | dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f); |
| 1675 | dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f); |
| 1676 | dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f); |
| 1677 | break; |
| 1678 | default: UNREACHABLE(); |
| 1679 | } |
| 1680 | break; |
daniel@transgaming.com | afb2395 | 2010-04-13 03:25:54 +0000 | [diff] [blame] | 1681 | case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1682 | switch (type) |
| 1683 | { |
daniel@transgaming.com | afb2395 | 2010-04-13 03:25:54 +0000 | [diff] [blame] | 1684 | case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE |
| 1685 | dest16[i + j * width] = ((unsigned short)(31 * b + 0.5f) << 0) | |
| 1686 | ((unsigned short)(63 * g + 0.5f) << 5) | |
| 1687 | ((unsigned short)(31 * r + 0.5f) << 11); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1688 | break; |
| 1689 | default: UNREACHABLE(); |
| 1690 | } |
| 1691 | break; |
| 1692 | default: UNREACHABLE(); |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | systemSurface->UnlockRect(); |
| 1698 | |
| 1699 | systemSurface->Release(); |
| 1700 | } |
| 1701 | |
| 1702 | void Context::clear(GLbitfield mask) |
| 1703 | { |
| 1704 | IDirect3DDevice9 *device = getDevice(); |
| 1705 | DWORD flags = 0; |
| 1706 | |
| 1707 | if (mask & GL_COLOR_BUFFER_BIT) |
| 1708 | { |
| 1709 | mask &= ~GL_COLOR_BUFFER_BIT; |
| 1710 | flags |= D3DCLEAR_TARGET; |
| 1711 | } |
| 1712 | |
| 1713 | if (mask & GL_DEPTH_BUFFER_BIT) |
| 1714 | { |
| 1715 | mask &= ~GL_DEPTH_BUFFER_BIT; |
| 1716 | if (depthMask) |
| 1717 | { |
| 1718 | flags |= D3DCLEAR_ZBUFFER; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | Framebuffer *framebufferObject = getFramebuffer(); |
| 1723 | IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil(); |
| 1724 | |
| 1725 | GLuint stencilUnmasked = 0x0; |
| 1726 | |
| 1727 | if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil) |
| 1728 | { |
| 1729 | D3DSURFACE_DESC desc; |
| 1730 | depthStencil->GetDesc(&desc); |
| 1731 | |
| 1732 | mask &= ~GL_STENCIL_BUFFER_BIT; |
| 1733 | unsigned int stencilSize = es2dx::GetStencilSize(desc.Format); |
| 1734 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1735 | |
| 1736 | if (stencilUnmasked != 0x0) |
| 1737 | { |
| 1738 | flags |= D3DCLEAR_STENCIL; |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | if (mask != 0) |
| 1743 | { |
| 1744 | return error(GL_INVALID_VALUE); |
| 1745 | } |
| 1746 | |
| 1747 | applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport |
| 1748 | |
| 1749 | D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue)); |
| 1750 | float depth = clamp01(depthClearValue); |
| 1751 | int stencil = stencilClearValue & 0x000000FF; |
| 1752 | |
| 1753 | IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget(); |
| 1754 | |
| 1755 | D3DSURFACE_DESC desc; |
| 1756 | renderTarget->GetDesc(&desc); |
| 1757 | |
| 1758 | bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha; |
| 1759 | |
| 1760 | const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) && |
| 1761 | (stencilWritemask & stencilUnmasked) != stencilUnmasked; |
| 1762 | const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) && |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1763 | !(colorMaskRed && colorMaskGreen && |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1764 | colorMaskBlue && alphaUnmasked); |
| 1765 | |
| 1766 | if (needMaskedColorClear || needMaskedStencilClear) |
| 1767 | { |
| 1768 | device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1769 | device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1770 | device->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1771 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1772 | device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1773 | device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1774 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1775 | device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1776 | |
| 1777 | if (flags & D3DCLEAR_TARGET) |
| 1778 | { |
| 1779 | device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) | |
| 1780 | (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) | |
| 1781 | (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) | |
| 1782 | (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0)); |
| 1783 | } |
| 1784 | else |
| 1785 | { |
| 1786 | device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1787 | } |
| 1788 | |
| 1789 | if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL)) |
| 1790 | { |
| 1791 | device->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1792 | device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); |
| 1793 | device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); |
| 1794 | device->SetRenderState(D3DRS_STENCILREF, stencil); |
| 1795 | device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1796 | device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1797 | device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); |
| 1798 | device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); |
| 1799 | } |
| 1800 | else |
| 1801 | { |
| 1802 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1803 | } |
| 1804 | |
| 1805 | device->SetPixelShader(NULL); |
| 1806 | device->SetVertexShader(NULL); |
| 1807 | device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); |
| 1808 | |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1809 | struct Vertex |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1810 | { |
| 1811 | float x, y, z, w; |
| 1812 | D3DCOLOR diffuse; |
| 1813 | }; |
| 1814 | |
| 1815 | Vertex quad[4]; |
| 1816 | quad[0].x = 0.0f; |
| 1817 | quad[0].y = (float)desc.Height; |
| 1818 | quad[0].z = 0.0f; |
| 1819 | quad[0].w = 1.0f; |
| 1820 | quad[0].diffuse = color; |
| 1821 | |
| 1822 | quad[1].x = (float)desc.Width; |
| 1823 | quad[1].y = (float)desc.Height; |
| 1824 | quad[1].z = 0.0f; |
| 1825 | quad[1].w = 1.0f; |
| 1826 | quad[1].diffuse = color; |
| 1827 | |
| 1828 | quad[2].x = 0.0f; |
| 1829 | quad[2].y = 0.0f; |
| 1830 | quad[2].z = 0.0f; |
| 1831 | quad[2].w = 1.0f; |
| 1832 | quad[2].diffuse = color; |
| 1833 | |
| 1834 | quad[3].x = (float)desc.Width; |
| 1835 | quad[3].y = 0.0f; |
| 1836 | quad[3].z = 0.0f; |
| 1837 | quad[3].w = 1.0f; |
| 1838 | quad[3].diffuse = color; |
| 1839 | |
| 1840 | device->BeginScene(); |
| 1841 | device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex)); |
| 1842 | device->EndScene(); |
| 1843 | |
| 1844 | if (flags & D3DCLEAR_ZBUFFER) |
| 1845 | { |
| 1846 | device->SetRenderState(D3DRS_ZENABLE, TRUE); |
| 1847 | device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); |
| 1848 | device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); |
| 1849 | } |
| 1850 | } |
| 1851 | else |
| 1852 | { |
| 1853 | device->Clear(0, NULL, flags, color, depth, stencil); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | void Context::drawArrays(GLenum mode, GLint first, GLsizei count) |
| 1858 | { |
| 1859 | if (!currentProgram) |
| 1860 | { |
| 1861 | return error(GL_INVALID_OPERATION); |
| 1862 | } |
| 1863 | |
| 1864 | IDirect3DDevice9 *device = getDevice(); |
| 1865 | D3DPRIMITIVETYPE primitiveType; |
| 1866 | int primitiveCount; |
| 1867 | |
| 1868 | if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount)) |
| 1869 | return error(GL_INVALID_ENUM); |
| 1870 | |
| 1871 | if (primitiveCount <= 0) |
| 1872 | { |
| 1873 | return; |
| 1874 | } |
| 1875 | |
| 1876 | if (!applyRenderTarget(false)) |
| 1877 | { |
| 1878 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
| 1879 | } |
| 1880 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 1881 | applyState(mode); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1882 | applyVertexBuffer(first, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1883 | applyShaders(); |
| 1884 | applyTextures(); |
| 1885 | |
daniel@transgaming.com | c3a0e94 | 2010-04-29 03:35:45 +0000 | [diff] [blame] | 1886 | if (!getCurrentProgram()->validateSamplers()) |
| 1887 | { |
| 1888 | return error(GL_INVALID_OPERATION); |
| 1889 | } |
| 1890 | |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 1891 | if (!cullSkipsDraw(mode)) |
| 1892 | { |
| 1893 | device->BeginScene(); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1894 | device->DrawPrimitive(primitiveType, 0, primitiveCount); |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 1895 | device->EndScene(); |
| 1896 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices) |
| 1900 | { |
| 1901 | if (!currentProgram) |
| 1902 | { |
| 1903 | return error(GL_INVALID_OPERATION); |
| 1904 | } |
| 1905 | |
| 1906 | if (!indices && !elementArrayBuffer) |
| 1907 | { |
| 1908 | return error(GL_INVALID_OPERATION); |
| 1909 | } |
| 1910 | |
| 1911 | IDirect3DDevice9 *device = getDevice(); |
| 1912 | D3DPRIMITIVETYPE primitiveType; |
| 1913 | int primitiveCount; |
| 1914 | |
| 1915 | if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount)) |
| 1916 | return error(GL_INVALID_ENUM); |
| 1917 | |
| 1918 | if (primitiveCount <= 0) |
| 1919 | { |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | if (!applyRenderTarget(false)) |
| 1924 | { |
| 1925 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
| 1926 | } |
| 1927 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 1928 | applyState(mode); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1929 | TranslatedIndexData indexInfo = applyIndexBuffer(indices, count, mode, type); |
| 1930 | applyVertexBuffer(indexInfo); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1931 | applyShaders(); |
| 1932 | applyTextures(); |
| 1933 | |
daniel@transgaming.com | c3a0e94 | 2010-04-29 03:35:45 +0000 | [diff] [blame] | 1934 | if (!getCurrentProgram()->validateSamplers()) |
| 1935 | { |
| 1936 | return error(GL_INVALID_OPERATION); |
| 1937 | } |
| 1938 | |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 1939 | if (!cullSkipsDraw(mode)) |
| 1940 | { |
| 1941 | device->BeginScene(); |
daniel@transgaming.com | f8b58a0 | 2010-03-26 04:08:45 +0000 | [diff] [blame] | 1942 | device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/sizeof(Index), primitiveCount); |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 1943 | device->EndScene(); |
| 1944 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | void Context::finish() |
| 1948 | { |
| 1949 | IDirect3DDevice9 *device = getDevice(); |
| 1950 | IDirect3DQuery9 *occlusionQuery = NULL; |
| 1951 | |
| 1952 | HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery); |
| 1953 | |
| 1954 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1955 | { |
| 1956 | return error(GL_OUT_OF_MEMORY); |
| 1957 | } |
| 1958 | |
| 1959 | ASSERT(SUCCEEDED(result)); |
| 1960 | |
| 1961 | if (occlusionQuery) |
| 1962 | { |
| 1963 | occlusionQuery->Issue(D3DISSUE_BEGIN); |
| 1964 | |
| 1965 | // Render something outside the render target |
| 1966 | device->SetPixelShader(NULL); |
| 1967 | device->SetVertexShader(NULL); |
| 1968 | device->SetFVF(D3DFVF_XYZRHW); |
| 1969 | float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f}; |
| 1970 | device->BeginScene(); |
| 1971 | device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data)); |
| 1972 | device->EndScene(); |
| 1973 | |
| 1974 | occlusionQuery->Issue(D3DISSUE_END); |
| 1975 | |
| 1976 | while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) |
| 1977 | { |
| 1978 | // Keep polling, but allow other threads to do something useful first |
| 1979 | Sleep(0); |
| 1980 | } |
| 1981 | |
| 1982 | occlusionQuery->Release(); |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | void Context::flush() |
| 1987 | { |
| 1988 | IDirect3DDevice9 *device = getDevice(); |
| 1989 | IDirect3DQuery9 *eventQuery = NULL; |
| 1990 | |
| 1991 | HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery); |
| 1992 | |
| 1993 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1994 | { |
| 1995 | return error(GL_OUT_OF_MEMORY); |
| 1996 | } |
| 1997 | |
| 1998 | ASSERT(SUCCEEDED(result)); |
| 1999 | |
| 2000 | if (eventQuery) |
| 2001 | { |
| 2002 | eventQuery->Issue(D3DISSUE_END); |
| 2003 | |
| 2004 | while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) |
| 2005 | { |
| 2006 | // Keep polling, but allow other threads to do something useful first |
| 2007 | Sleep(0); |
| 2008 | } |
| 2009 | |
| 2010 | eventQuery->Release(); |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | void Context::recordInvalidEnum() |
| 2015 | { |
| 2016 | mInvalidEnum = true; |
| 2017 | } |
| 2018 | |
| 2019 | void Context::recordInvalidValue() |
| 2020 | { |
| 2021 | mInvalidValue = true; |
| 2022 | } |
| 2023 | |
| 2024 | void Context::recordInvalidOperation() |
| 2025 | { |
| 2026 | mInvalidOperation = true; |
| 2027 | } |
| 2028 | |
| 2029 | void Context::recordOutOfMemory() |
| 2030 | { |
| 2031 | mOutOfMemory = true; |
| 2032 | } |
| 2033 | |
| 2034 | void Context::recordInvalidFramebufferOperation() |
| 2035 | { |
| 2036 | mInvalidFramebufferOperation = true; |
| 2037 | } |
| 2038 | |
| 2039 | // Get one of the recorded errors and clear its flag, if any. |
| 2040 | // [OpenGL ES 2.0.24] section 2.5 page 13. |
| 2041 | GLenum Context::getError() |
| 2042 | { |
| 2043 | if (mInvalidEnum) |
| 2044 | { |
| 2045 | mInvalidEnum = false; |
| 2046 | |
| 2047 | return GL_INVALID_ENUM; |
| 2048 | } |
| 2049 | |
| 2050 | if (mInvalidValue) |
| 2051 | { |
| 2052 | mInvalidValue = false; |
| 2053 | |
| 2054 | return GL_INVALID_VALUE; |
| 2055 | } |
| 2056 | |
| 2057 | if (mInvalidOperation) |
| 2058 | { |
| 2059 | mInvalidOperation = false; |
| 2060 | |
| 2061 | return GL_INVALID_OPERATION; |
| 2062 | } |
| 2063 | |
| 2064 | if (mOutOfMemory) |
| 2065 | { |
| 2066 | mOutOfMemory = false; |
| 2067 | |
| 2068 | return GL_OUT_OF_MEMORY; |
| 2069 | } |
| 2070 | |
| 2071 | if (mInvalidFramebufferOperation) |
| 2072 | { |
| 2073 | mInvalidFramebufferOperation = false; |
| 2074 | |
| 2075 | return GL_INVALID_FRAMEBUFFER_OPERATION; |
| 2076 | } |
| 2077 | |
| 2078 | return GL_NO_ERROR; |
| 2079 | } |
| 2080 | |
daniel@transgaming.com | 296ca9c | 2010-04-03 20:56:07 +0000 | [diff] [blame] | 2081 | const char *Context::getPixelShaderProfile() |
| 2082 | { |
| 2083 | return mPsProfile; |
| 2084 | } |
| 2085 | |
| 2086 | const char *Context::getVertexShaderProfile() |
| 2087 | { |
| 2088 | return mVsProfile; |
| 2089 | } |
| 2090 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2091 | void Context::detachBuffer(GLuint buffer) |
| 2092 | { |
| 2093 | // [OpenGL ES 2.0.24] section 2.9 page 22: |
| 2094 | // If a buffer object is deleted while it is bound, all bindings to that object in the current context |
| 2095 | // (i.e. in the thread that called Delete-Buffers) are reset to zero. |
| 2096 | |
| 2097 | if (arrayBuffer == buffer) |
| 2098 | { |
| 2099 | arrayBuffer = 0; |
| 2100 | } |
| 2101 | |
| 2102 | if (elementArrayBuffer == buffer) |
| 2103 | { |
| 2104 | elementArrayBuffer = 0; |
| 2105 | } |
| 2106 | |
| 2107 | for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) |
| 2108 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 2109 | if (vertexAttribute[attribute].mBoundBuffer == buffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2110 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 2111 | vertexAttribute[attribute].mBoundBuffer = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | void Context::detachTexture(GLuint texture) |
| 2117 | { |
| 2118 | // [OpenGL ES 2.0.24] section 3.8 page 84: |
| 2119 | // If a texture object is deleted, it is as if all texture units which are bound to that texture object are |
| 2120 | // rebound to texture object zero |
| 2121 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 2122 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2123 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 2124 | for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2125 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 2126 | if (samplerTexture[type][sampler] == texture) |
| 2127 | { |
| 2128 | samplerTexture[type][sampler] = 0; |
| 2129 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | // [OpenGL ES 2.0.24] section 4.4 page 112: |
| 2134 | // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is |
| 2135 | // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this |
| 2136 | // image was attached in the currently bound framebuffer. |
| 2137 | |
| 2138 | Framebuffer *framebuffer = getFramebuffer(); |
| 2139 | |
| 2140 | if (framebuffer) |
| 2141 | { |
| 2142 | framebuffer->detachTexture(texture); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | void Context::detachFramebuffer(GLuint framebuffer) |
| 2147 | { |
| 2148 | // [OpenGL ES 2.0.24] section 4.4 page 107: |
| 2149 | // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though |
| 2150 | // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero. |
| 2151 | |
| 2152 | if (this->framebuffer == framebuffer) |
| 2153 | { |
| 2154 | bindFramebuffer(0); |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | void Context::detachRenderbuffer(GLuint renderbuffer) |
| 2159 | { |
| 2160 | // [OpenGL ES 2.0.24] section 4.4 page 109: |
| 2161 | // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer |
| 2162 | // had been executed with the target RENDERBUFFER and name of zero. |
| 2163 | |
| 2164 | if (this->renderbuffer == renderbuffer) |
| 2165 | { |
| 2166 | bindRenderbuffer(0); |
| 2167 | } |
| 2168 | |
| 2169 | // [OpenGL ES 2.0.24] section 4.4 page 111: |
| 2170 | // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer, |
| 2171 | // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment |
| 2172 | // point to which this image was attached in the currently bound framebuffer. |
| 2173 | |
| 2174 | Framebuffer *framebuffer = getFramebuffer(); |
| 2175 | |
| 2176 | if (framebuffer) |
| 2177 | { |
| 2178 | framebuffer->detachRenderbuffer(renderbuffer); |
| 2179 | } |
| 2180 | } |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 2181 | |
| 2182 | Texture *Context::getIncompleteTexture(SamplerType type) |
| 2183 | { |
| 2184 | Texture *t = mIncompleteTextures[type]; |
| 2185 | |
| 2186 | if (t == NULL) |
| 2187 | { |
| 2188 | static const GLubyte color[] = { 0, 0, 0, 255 }; |
| 2189 | |
| 2190 | switch (type) |
| 2191 | { |
| 2192 | default: |
| 2193 | UNREACHABLE(); |
| 2194 | // default falls through to SAMPLER_2D |
| 2195 | |
| 2196 | case SAMPLER_2D: |
| 2197 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 2198 | Texture2D *incomplete2d = new Texture2D(this); |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 2199 | 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] | 2200 | t = incomplete2d; |
| 2201 | } |
| 2202 | break; |
| 2203 | |
| 2204 | case SAMPLER_CUBE: |
| 2205 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 2206 | TextureCubeMap *incompleteCube = new TextureCubeMap(this); |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 2207 | |
daniel@transgaming.com | 3489e3a | 2010-03-21 04:31:11 +0000 | [diff] [blame] | 2208 | incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 2209 | incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 2210 | incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 2211 | incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 2212 | incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); |
| 2213 | 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] | 2214 | |
| 2215 | t = incompleteCube; |
| 2216 | } |
| 2217 | break; |
| 2218 | } |
| 2219 | |
| 2220 | mIncompleteTextures[type] = t; |
| 2221 | } |
| 2222 | |
| 2223 | return t; |
| 2224 | } |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2225 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 2226 | bool Context::cullSkipsDraw(GLenum drawMode) |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2227 | { |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 2228 | return cullFace && cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode); |
daniel@transgaming.com | ace5e66 | 2010-03-21 04:31:20 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
daniel@transgaming.com | 5af6427 | 2010-04-15 20:45:12 +0000 | [diff] [blame] | 2231 | bool Context::isTriangleMode(GLenum drawMode) |
| 2232 | { |
| 2233 | switch (drawMode) |
| 2234 | { |
| 2235 | case GL_TRIANGLES: |
| 2236 | case GL_TRIANGLE_FAN: |
| 2237 | case GL_TRIANGLE_STRIP: |
| 2238 | return true; |
| 2239 | case GL_POINTS: |
| 2240 | case GL_LINES: |
| 2241 | case GL_LINE_LOOP: |
| 2242 | case GL_LINE_STRIP: |
| 2243 | return false; |
| 2244 | default: UNREACHABLE(); |
| 2245 | } |
| 2246 | |
| 2247 | return false; |
| 2248 | } |
daniel@transgaming.com | e4b08c8 | 2010-04-20 18:53:06 +0000 | [diff] [blame] | 2249 | |
| 2250 | void Context::setVertexAttrib(GLuint index, const GLfloat *values) |
| 2251 | { |
| 2252 | ASSERT(index < gl::MAX_VERTEX_ATTRIBS); |
| 2253 | |
| 2254 | vertexAttribute[index].mCurrentValue[0] = values[0]; |
| 2255 | vertexAttribute[index].mCurrentValue[1] = values[1]; |
| 2256 | vertexAttribute[index].mCurrentValue[2] = values[2]; |
| 2257 | vertexAttribute[index].mCurrentValue[3] = values[3]; |
| 2258 | |
| 2259 | mVertexDataManager->dirtyCurrentValues(); |
| 2260 | } |
| 2261 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | extern "C" |
| 2265 | { |
| 2266 | gl::Context *glCreateContext(const egl::Config *config) |
| 2267 | { |
| 2268 | return new gl::Context(config); |
| 2269 | } |
| 2270 | |
| 2271 | void glDestroyContext(gl::Context *context) |
| 2272 | { |
| 2273 | delete context; |
| 2274 | |
| 2275 | if (context == gl::getContext()) |
| 2276 | { |
| 2277 | gl::makeCurrent(NULL, NULL, NULL); |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface) |
| 2282 | { |
| 2283 | gl::makeCurrent(context, display, surface); |
| 2284 | } |
| 2285 | |
| 2286 | gl::Context *glGetCurrentContext() |
| 2287 | { |
| 2288 | return gl::getContext(); |
| 2289 | } |
| 2290 | } |