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 | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 10 | #include "Context.h" |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
| 13 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 14 | #include "main.h" |
| 15 | #include "Display.h" |
| 16 | #include "Buffer.h" |
| 17 | #include "Shader.h" |
| 18 | #include "Program.h" |
| 19 | #include "Texture.h" |
| 20 | #include "FrameBuffer.h" |
| 21 | #include "RenderBuffer.h" |
| 22 | #include "mathutil.h" |
| 23 | #include "utilities.h" |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 24 | #include "geometry/backend.h" |
| 25 | #include "geometry/VertexDataManager.h" |
| 26 | #include "geometry/dx9.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 28 | #undef near |
| 29 | #undef far |
| 30 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 31 | namespace gl |
| 32 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 33 | Context::Context(const egl::Config *config) |
| 34 | : mConfig(config) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 35 | { |
| 36 | setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 37 | depthClearValue = 1.0f; |
| 38 | stencilClearValue = 0; |
| 39 | |
| 40 | cullFace = false; |
| 41 | cullMode = GL_BACK; |
| 42 | frontFace = GL_CCW; |
| 43 | depthTest = false; |
| 44 | depthFunc = GL_LESS; |
| 45 | blend = false; |
| 46 | sourceBlendRGB = GL_ONE; |
| 47 | sourceBlendAlpha = GL_ONE; |
| 48 | destBlendRGB = GL_ZERO; |
| 49 | destBlendAlpha = GL_ZERO; |
| 50 | blendEquationRGB = GL_FUNC_ADD; |
| 51 | blendEquationAlpha = GL_FUNC_ADD; |
| 52 | blendColor.red = 0; |
| 53 | blendColor.green = 0; |
| 54 | blendColor.blue = 0; |
| 55 | blendColor.alpha = 0; |
| 56 | stencilTest = false; |
| 57 | stencilFunc = GL_ALWAYS; |
| 58 | stencilRef = 0; |
| 59 | stencilMask = -1; |
| 60 | stencilWritemask = -1; |
| 61 | stencilBackFunc = GL_ALWAYS; |
| 62 | stencilBackRef = 0; |
| 63 | stencilBackMask = - 1; |
| 64 | stencilBackWritemask = -1; |
| 65 | stencilFail = GL_KEEP; |
| 66 | stencilPassDepthFail = GL_KEEP; |
| 67 | stencilPassDepthPass = GL_KEEP; |
| 68 | stencilBackFail = GL_KEEP; |
| 69 | stencilBackPassDepthFail = GL_KEEP; |
| 70 | stencilBackPassDepthPass = GL_KEEP; |
| 71 | polygonOffsetFill = false; |
| 72 | sampleAlphaToCoverage = false; |
| 73 | sampleCoverage = false; |
| 74 | sampleCoverageValue = 1.0f; |
| 75 | sampleCoverageInvert = GL_FALSE; |
| 76 | scissorTest = false; |
| 77 | dither = true; |
| 78 | |
| 79 | viewportX = 0; |
| 80 | viewportY = 0; |
| 81 | viewportWidth = config->mDisplayMode.Width; |
| 82 | viewportHeight = config->mDisplayMode.Height; |
| 83 | zNear = 0.0f; |
| 84 | zFar = 1.0f; |
| 85 | |
| 86 | scissorX = 0; |
| 87 | scissorY = 0; |
| 88 | scissorWidth = config->mDisplayMode.Width; |
| 89 | scissorHeight = config->mDisplayMode.Height; |
| 90 | |
| 91 | colorMaskRed = true; |
| 92 | colorMaskGreen = true; |
| 93 | colorMaskBlue = true; |
| 94 | colorMaskAlpha = true; |
| 95 | depthMask = true; |
| 96 | |
| 97 | // [OpenGL ES 2.0.24] section 3.7 page 83: |
| 98 | // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional |
| 99 | // and cube map texture state vectors respectively associated with them. |
| 100 | // In order that access to these initial textures not be lost, they are treated as texture |
| 101 | // objects all of whose names are 0. |
| 102 | |
| 103 | mTexture2DZero = new Texture2D(); |
| 104 | mTextureCubeMapZero = new TextureCubeMap(); |
| 105 | |
| 106 | mColorbufferZero = NULL; |
| 107 | mDepthbufferZero = NULL; |
| 108 | mStencilbufferZero = NULL; |
| 109 | |
| 110 | activeSampler = 0; |
| 111 | arrayBuffer = 0; |
| 112 | elementArrayBuffer = 0; |
| 113 | bindTextureCubeMap(0); |
| 114 | bindTexture2D(0); |
| 115 | bindFramebuffer(0); |
| 116 | bindRenderbuffer(0); |
| 117 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 118 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 119 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 120 | for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++) |
| 121 | { |
| 122 | samplerTexture[type][sampler] = 0; |
| 123 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | } |
| 125 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 126 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
| 127 | { |
| 128 | mIncompleteTextures[type] = NULL; |
| 129 | } |
| 130 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 131 | currentProgram = 0; |
| 132 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 133 | mBufferBackEnd = NULL; |
| 134 | mVertexDataManager = NULL; |
| 135 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | mInvalidEnum = false; |
| 137 | mInvalidValue = false; |
| 138 | mInvalidOperation = false; |
| 139 | mOutOfMemory = false; |
| 140 | mInvalidFramebufferOperation = false; |
| 141 | } |
| 142 | |
| 143 | Context::~Context() |
| 144 | { |
| 145 | currentProgram = 0; |
| 146 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 147 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
| 148 | { |
| 149 | delete mIncompleteTextures[type]; |
| 150 | } |
| 151 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 152 | delete mTexture2DZero; |
| 153 | delete mTextureCubeMapZero; |
| 154 | |
| 155 | delete mColorbufferZero; |
| 156 | delete mDepthbufferZero; |
| 157 | delete mStencilbufferZero; |
| 158 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 159 | delete mBufferBackEnd; |
| 160 | delete mVertexDataManager; |
| 161 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 162 | while (!mBufferMap.empty()) |
| 163 | { |
| 164 | deleteBuffer(mBufferMap.begin()->first); |
| 165 | } |
| 166 | |
| 167 | while (!mProgramMap.empty()) |
| 168 | { |
| 169 | deleteProgram(mProgramMap.begin()->first); |
| 170 | } |
| 171 | |
| 172 | while (!mShaderMap.empty()) |
| 173 | { |
| 174 | deleteShader(mShaderMap.begin()->first); |
| 175 | } |
| 176 | |
| 177 | while (!mFramebufferMap.empty()) |
| 178 | { |
| 179 | deleteFramebuffer(mFramebufferMap.begin()->first); |
| 180 | } |
| 181 | |
| 182 | while (!mRenderbufferMap.empty()) |
| 183 | { |
| 184 | deleteRenderbuffer(mRenderbufferMap.begin()->first); |
| 185 | } |
| 186 | |
| 187 | while (!mTextureMap.empty()) |
| 188 | { |
| 189 | deleteTexture(mTextureMap.begin()->first); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void Context::makeCurrent(egl::Display *display, egl::Surface *surface) |
| 194 | { |
| 195 | IDirect3DDevice9 *device = display->getDevice(); |
| 196 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 197 | if (!mBufferBackEnd) |
| 198 | { |
| 199 | mBufferBackEnd = new Dx9BackEnd(device); |
| 200 | mVertexDataManager = new VertexDataManager(this, mBufferBackEnd); |
| 201 | } |
| 202 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 203 | // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names |
| 204 | IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget(); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 205 | IDirect3DSurface9 *depthStencil = surface->getDepthStencil(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 206 | |
| 207 | Framebuffer *framebufferZero = new Framebuffer(); |
| 208 | Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 209 | Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil); |
| 210 | Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 211 | |
| 212 | setFramebufferZero(framebufferZero); |
| 213 | setColorbufferZero(colorbufferZero); |
| 214 | setDepthbufferZero(depthbufferZero); |
| 215 | setStencilbufferZero(stencilbufferZero); |
| 216 | |
| 217 | framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0); |
| 218 | framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0); |
| 219 | framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0); |
| 220 | |
| 221 | defaultRenderTarget->Release(); |
| 222 | |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 223 | if (depthStencil) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 224 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 225 | depthStencil->Release(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | void Context::setClearColor(float red, float green, float blue, float alpha) |
| 230 | { |
| 231 | colorClearValue.red = red; |
| 232 | colorClearValue.green = green; |
| 233 | colorClearValue.blue = blue; |
| 234 | colorClearValue.alpha = alpha; |
| 235 | } |
| 236 | |
| 237 | void Context::setClearDepth(float depth) |
| 238 | { |
| 239 | depthClearValue = depth; |
| 240 | } |
| 241 | |
| 242 | void Context::setClearStencil(int stencil) |
| 243 | { |
| 244 | stencilClearValue = stencil; |
| 245 | } |
| 246 | |
| 247 | // Returns an unused buffer name |
| 248 | GLuint Context::createBuffer() |
| 249 | { |
| 250 | unsigned int handle = 1; |
| 251 | |
| 252 | while (mBufferMap.find(handle) != mBufferMap.end()) |
| 253 | { |
| 254 | handle++; |
| 255 | } |
| 256 | |
| 257 | mBufferMap[handle] = NULL; |
| 258 | |
| 259 | return handle; |
| 260 | } |
| 261 | |
| 262 | // Returns an unused shader/program name |
| 263 | GLuint Context::createShader(GLenum type) |
| 264 | { |
| 265 | unsigned int handle = 1; |
| 266 | |
| 267 | while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space |
| 268 | { |
| 269 | handle++; |
| 270 | } |
| 271 | |
| 272 | if (type == GL_VERTEX_SHADER) |
| 273 | { |
| 274 | mShaderMap[handle] = new VertexShader(); |
| 275 | } |
| 276 | else if (type == GL_FRAGMENT_SHADER) |
| 277 | { |
| 278 | mShaderMap[handle] = new FragmentShader(); |
| 279 | } |
| 280 | else UNREACHABLE(); |
| 281 | |
| 282 | return handle; |
| 283 | } |
| 284 | |
| 285 | // Returns an unused program/shader name |
| 286 | GLuint Context::createProgram() |
| 287 | { |
| 288 | unsigned int handle = 1; |
| 289 | |
| 290 | while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space |
| 291 | { |
| 292 | handle++; |
| 293 | } |
| 294 | |
| 295 | mProgramMap[handle] = new Program(); |
| 296 | |
| 297 | return handle; |
| 298 | } |
| 299 | |
| 300 | // Returns an unused texture name |
| 301 | GLuint Context::createTexture() |
| 302 | { |
| 303 | unsigned int handle = 1; |
| 304 | |
| 305 | while (mTextureMap.find(handle) != mTextureMap.end()) |
| 306 | { |
| 307 | handle++; |
| 308 | } |
| 309 | |
| 310 | mTextureMap[handle] = NULL; |
| 311 | |
| 312 | return handle; |
| 313 | } |
| 314 | |
| 315 | // Returns an unused framebuffer name |
| 316 | GLuint Context::createFramebuffer() |
| 317 | { |
| 318 | unsigned int handle = 1; |
| 319 | |
| 320 | while (mFramebufferMap.find(handle) != mFramebufferMap.end()) |
| 321 | { |
| 322 | handle++; |
| 323 | } |
| 324 | |
| 325 | mFramebufferMap[handle] = NULL; |
| 326 | |
| 327 | return handle; |
| 328 | } |
| 329 | |
| 330 | // Returns an unused renderbuffer name |
| 331 | GLuint Context::createRenderbuffer() |
| 332 | { |
| 333 | unsigned int handle = 1; |
| 334 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 335 | while (mRenderbufferMap.find(handle) != mRenderbufferMap.end()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 336 | { |
| 337 | handle++; |
| 338 | } |
| 339 | |
| 340 | mRenderbufferMap[handle] = NULL; |
| 341 | |
| 342 | return handle; |
| 343 | } |
| 344 | |
| 345 | void Context::deleteBuffer(GLuint buffer) |
| 346 | { |
| 347 | BufferMap::iterator bufferObject = mBufferMap.find(buffer); |
| 348 | |
| 349 | if (bufferObject != mBufferMap.end()) |
| 350 | { |
| 351 | detachBuffer(buffer); |
| 352 | |
| 353 | delete bufferObject->second; |
| 354 | mBufferMap.erase(bufferObject); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void Context::deleteShader(GLuint shader) |
| 359 | { |
| 360 | ShaderMap::iterator shaderObject = mShaderMap.find(shader); |
| 361 | |
| 362 | if (shaderObject != mShaderMap.end()) |
| 363 | { |
| 364 | if (!shaderObject->second->isAttached()) |
| 365 | { |
| 366 | delete shaderObject->second; |
| 367 | mShaderMap.erase(shaderObject); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | shaderObject->second->flagForDeletion(); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | void Context::deleteProgram(GLuint program) |
| 377 | { |
| 378 | ProgramMap::iterator programObject = mProgramMap.find(program); |
| 379 | |
| 380 | if (programObject != mProgramMap.end()) |
| 381 | { |
| 382 | if (program != currentProgram) |
| 383 | { |
| 384 | delete programObject->second; |
| 385 | mProgramMap.erase(programObject); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | programObject->second->flagForDeletion(); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void Context::deleteTexture(GLuint texture) |
| 395 | { |
| 396 | TextureMap::iterator textureObject = mTextureMap.find(texture); |
| 397 | |
| 398 | if (textureObject != mTextureMap.end()) |
| 399 | { |
| 400 | detachTexture(texture); |
| 401 | |
| 402 | if (texture != 0) |
| 403 | { |
| 404 | delete textureObject->second; |
| 405 | } |
| 406 | |
| 407 | mTextureMap.erase(textureObject); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void Context::deleteFramebuffer(GLuint framebuffer) |
| 412 | { |
| 413 | FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer); |
| 414 | |
| 415 | if (framebufferObject != mFramebufferMap.end()) |
| 416 | { |
| 417 | detachFramebuffer(framebuffer); |
| 418 | |
| 419 | delete framebufferObject->second; |
| 420 | mFramebufferMap.erase(framebufferObject); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void Context::deleteRenderbuffer(GLuint renderbuffer) |
| 425 | { |
| 426 | RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer); |
| 427 | |
| 428 | if (renderbufferObject != mRenderbufferMap.end()) |
| 429 | { |
| 430 | detachRenderbuffer(renderbuffer); |
| 431 | |
| 432 | delete renderbufferObject->second; |
| 433 | mRenderbufferMap.erase(renderbufferObject); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void Context::bindArrayBuffer(unsigned int buffer) |
| 438 | { |
| 439 | if (buffer != 0 && !getBuffer(buffer)) |
| 440 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 441 | mBufferMap[buffer] = new Buffer(mBufferBackEnd); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | arrayBuffer = buffer; |
| 445 | } |
| 446 | |
| 447 | void Context::bindElementArrayBuffer(unsigned int buffer) |
| 448 | { |
| 449 | if (buffer != 0 && !getBuffer(buffer)) |
| 450 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 451 | mBufferMap[buffer] = new Buffer(mBufferBackEnd); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | elementArrayBuffer = buffer; |
| 455 | } |
| 456 | |
| 457 | void Context::bindTexture2D(GLuint texture) |
| 458 | { |
| 459 | if (!getTexture(texture) || texture == 0) |
| 460 | { |
| 461 | if (texture != 0) |
| 462 | { |
| 463 | mTextureMap[texture] = new Texture2D(); |
| 464 | } |
| 465 | else // Special case: 0 refers to different initial textures based on the target |
| 466 | { |
| 467 | mTextureMap[0] = mTexture2DZero; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | texture2D = texture; |
| 472 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 473 | samplerTexture[SAMPLER_2D][activeSampler] = texture; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | void Context::bindTextureCubeMap(GLuint texture) |
| 477 | { |
| 478 | if (!getTexture(texture) || texture == 0) |
| 479 | { |
| 480 | if (texture != 0) |
| 481 | { |
| 482 | mTextureMap[texture] = new TextureCubeMap(); |
| 483 | } |
| 484 | else // Special case: 0 refers to different initial textures based on the target |
| 485 | { |
| 486 | mTextureMap[0] = mTextureCubeMapZero; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | textureCubeMap = texture; |
| 491 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 492 | samplerTexture[SAMPLER_CUBE][activeSampler] = texture; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void Context::bindFramebuffer(GLuint framebuffer) |
| 496 | { |
| 497 | if (!getFramebuffer(framebuffer)) |
| 498 | { |
| 499 | mFramebufferMap[framebuffer] = new Framebuffer(); |
| 500 | } |
| 501 | |
| 502 | this->framebuffer = framebuffer; |
| 503 | } |
| 504 | |
| 505 | void Context::bindRenderbuffer(GLuint renderbuffer) |
| 506 | { |
| 507 | if (renderbuffer != 0 && !getRenderbuffer(renderbuffer)) |
| 508 | { |
| 509 | mRenderbufferMap[renderbuffer] = new Renderbuffer(); |
| 510 | } |
| 511 | |
| 512 | this->renderbuffer = renderbuffer; |
| 513 | } |
| 514 | |
| 515 | void Context::useProgram(GLuint program) |
| 516 | { |
| 517 | Program *programObject = getCurrentProgram(); |
| 518 | |
| 519 | if (programObject && programObject->isFlaggedForDeletion()) |
| 520 | { |
| 521 | deleteProgram(currentProgram); |
| 522 | } |
| 523 | |
| 524 | currentProgram = program; |
| 525 | } |
| 526 | |
| 527 | void Context::setFramebufferZero(Framebuffer *buffer) |
| 528 | { |
| 529 | delete mFramebufferMap[0]; |
| 530 | mFramebufferMap[0] = buffer; |
| 531 | } |
| 532 | |
| 533 | void Context::setColorbufferZero(Colorbuffer *buffer) |
| 534 | { |
| 535 | delete mColorbufferZero; |
| 536 | mColorbufferZero = buffer; |
| 537 | } |
| 538 | |
| 539 | void Context::setDepthbufferZero(Depthbuffer *buffer) |
| 540 | { |
| 541 | delete mDepthbufferZero; |
| 542 | mDepthbufferZero = buffer; |
| 543 | } |
| 544 | |
| 545 | void Context::setStencilbufferZero(Stencilbuffer *buffer) |
| 546 | { |
| 547 | delete mStencilbufferZero; |
| 548 | mStencilbufferZero = buffer; |
| 549 | } |
| 550 | |
| 551 | void Context::setRenderbuffer(Renderbuffer *buffer) |
| 552 | { |
| 553 | delete mRenderbufferMap[renderbuffer]; |
| 554 | mRenderbufferMap[renderbuffer] = buffer; |
| 555 | } |
| 556 | |
| 557 | Buffer *Context::getBuffer(unsigned int handle) |
| 558 | { |
| 559 | BufferMap::iterator buffer = mBufferMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 560 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 561 | if (buffer == mBufferMap.end()) |
| 562 | { |
| 563 | return NULL; |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | return buffer->second; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | Shader *Context::getShader(unsigned int handle) |
| 572 | { |
| 573 | ShaderMap::iterator shader = mShaderMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 574 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 575 | if (shader == mShaderMap.end()) |
| 576 | { |
| 577 | return NULL; |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | return shader->second; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | Program *Context::getProgram(unsigned int handle) |
| 586 | { |
| 587 | ProgramMap::iterator program = mProgramMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 588 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 589 | if (program == mProgramMap.end()) |
| 590 | { |
| 591 | return NULL; |
| 592 | } |
| 593 | else |
| 594 | { |
| 595 | return program->second; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | Texture *Context::getTexture(unsigned int handle) |
| 600 | { |
| 601 | TextureMap::iterator texture = mTextureMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 602 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 603 | if (texture == mTextureMap.end()) |
| 604 | { |
| 605 | return NULL; |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | return texture->second; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | Framebuffer *Context::getFramebuffer(unsigned int handle) |
| 614 | { |
| 615 | FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 616 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 617 | if (framebuffer == mFramebufferMap.end()) |
| 618 | { |
| 619 | return NULL; |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | return framebuffer->second; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | Renderbuffer *Context::getRenderbuffer(unsigned int handle) |
| 628 | { |
| 629 | RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 630 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 631 | if (renderbuffer == mRenderbufferMap.end()) |
| 632 | { |
| 633 | return NULL; |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | return renderbuffer->second; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | Colorbuffer *Context::getColorbuffer(GLuint handle) |
| 642 | { |
| 643 | if (handle != 0) |
| 644 | { |
| 645 | Renderbuffer *renderbuffer = getRenderbuffer(handle); |
| 646 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 647 | if (renderbuffer && renderbuffer->isColorbuffer()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 648 | { |
| 649 | return static_cast<Colorbuffer*>(renderbuffer); |
| 650 | } |
| 651 | } |
| 652 | else // Special case: 0 refers to different initial render targets based on the attachment type |
| 653 | { |
| 654 | return mColorbufferZero; |
| 655 | } |
| 656 | |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | Depthbuffer *Context::getDepthbuffer(GLuint handle) |
| 661 | { |
| 662 | if (handle != 0) |
| 663 | { |
| 664 | Renderbuffer *renderbuffer = getRenderbuffer(handle); |
| 665 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 666 | if (renderbuffer && renderbuffer->isDepthbuffer()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 667 | { |
| 668 | return static_cast<Depthbuffer*>(renderbuffer); |
| 669 | } |
| 670 | } |
| 671 | else // Special case: 0 refers to different initial render targets based on the attachment type |
| 672 | { |
| 673 | return mDepthbufferZero; |
| 674 | } |
| 675 | |
| 676 | return NULL; |
| 677 | } |
| 678 | |
| 679 | Stencilbuffer *Context::getStencilbuffer(GLuint handle) |
| 680 | { |
| 681 | if (handle != 0) |
| 682 | { |
| 683 | Renderbuffer *renderbuffer = getRenderbuffer(handle); |
| 684 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 685 | if (renderbuffer && renderbuffer->isStencilbuffer()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 686 | { |
| 687 | return static_cast<Stencilbuffer*>(renderbuffer); |
| 688 | } |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | return mStencilbufferZero; |
| 693 | } |
| 694 | |
| 695 | return NULL; |
| 696 | } |
| 697 | |
| 698 | Buffer *Context::getArrayBuffer() |
| 699 | { |
| 700 | return getBuffer(arrayBuffer); |
| 701 | } |
| 702 | |
| 703 | Buffer *Context::getElementArrayBuffer() |
| 704 | { |
| 705 | return getBuffer(elementArrayBuffer); |
| 706 | } |
| 707 | |
| 708 | Program *Context::getCurrentProgram() |
| 709 | { |
| 710 | return getProgram(currentProgram); |
| 711 | } |
| 712 | |
| 713 | Texture2D *Context::getTexture2D() |
| 714 | { |
| 715 | if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target |
| 716 | { |
| 717 | return mTexture2DZero; |
| 718 | } |
| 719 | |
| 720 | return (Texture2D*)getTexture(texture2D); |
| 721 | } |
| 722 | |
| 723 | TextureCubeMap *Context::getTextureCubeMap() |
| 724 | { |
| 725 | if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target |
| 726 | { |
| 727 | return mTextureCubeMapZero; |
| 728 | } |
| 729 | |
| 730 | return (TextureCubeMap*)getTexture(textureCubeMap); |
| 731 | } |
| 732 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 733 | Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 734 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 735 | return getTexture(samplerTexture[type][sampler]); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | Framebuffer *Context::getFramebuffer() |
| 739 | { |
| 740 | return getFramebuffer(framebuffer); |
| 741 | } |
| 742 | |
| 743 | // Applies the render target surface, depth stencil surface, viewport rectangle and |
| 744 | // scissor rectangle to the Direct3D 9 device |
| 745 | bool Context::applyRenderTarget(bool ignoreViewport) |
| 746 | { |
| 747 | IDirect3DDevice9 *device = getDevice(); |
| 748 | Framebuffer *framebufferObject = getFramebuffer(); |
| 749 | |
| 750 | if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE) |
| 751 | { |
| 752 | return false; |
| 753 | } |
| 754 | |
| 755 | IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget(); |
| 756 | IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil(); |
| 757 | |
| 758 | device->SetRenderTarget(0, renderTarget); |
| 759 | device->SetDepthStencilSurface(depthStencil); |
| 760 | |
| 761 | D3DVIEWPORT9 viewport; |
| 762 | D3DSURFACE_DESC desc; |
| 763 | renderTarget->GetDesc(&desc); |
| 764 | |
| 765 | if (ignoreViewport) |
| 766 | { |
| 767 | viewport.X = 0; |
| 768 | viewport.Y = 0; |
| 769 | viewport.Width = desc.Width; |
| 770 | viewport.Height = desc.Height; |
| 771 | viewport.MinZ = 0.0f; |
| 772 | viewport.MaxZ = 1.0f; |
| 773 | } |
| 774 | else |
| 775 | { |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 776 | viewport.X = std::max(viewportX, 0); |
| 777 | viewport.Y = std::max(viewportY, 0); |
| 778 | viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X); |
| 779 | viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 780 | viewport.MinZ = clamp01(zNear); |
| 781 | viewport.MaxZ = clamp01(zFar); |
| 782 | } |
| 783 | |
| 784 | device->SetViewport(&viewport); |
| 785 | |
| 786 | if (scissorTest) |
| 787 | { |
| 788 | RECT rect = {scissorX, |
| 789 | scissorY, |
| 790 | scissorX + scissorWidth, |
| 791 | scissorY + scissorHeight}; |
| 792 | |
| 793 | device->SetScissorRect(&rect); |
| 794 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); |
| 799 | } |
| 800 | |
| 801 | if (currentProgram) |
| 802 | { |
| 803 | D3DSURFACE_DESC description; |
| 804 | renderTarget->GetDesc(&description); |
| 805 | Program *programObject = getCurrentProgram(); |
| 806 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 807 | GLint halfPixelSize = programObject->getUniformLocation("gl_HalfPixelSize"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 808 | GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height}; |
| 809 | programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 810 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 811 | GLint window = programObject->getUniformLocation("gl_Window"); |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 812 | GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f}; |
| 813 | programObject->setUniform4fv(window, 1, (GLfloat*)&whxy); |
| 814 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 815 | GLint depth = programObject->getUniformLocation("gl_Depth"); |
daniel@transgaming.com | 9b5f544 | 2010-03-16 05:43:55 +0000 | [diff] [blame] | 816 | GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f}; |
| 817 | programObject->setUniform2fv(depth, 1, (GLfloat*)&dz); |
| 818 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 819 | GLint near = programObject->getUniformLocation("gl_DepthRange.near"); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 820 | programObject->setUniform1fv(near, 1, &zNear); |
| 821 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 822 | GLint far = programObject->getUniformLocation("gl_DepthRange.far"); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 823 | programObject->setUniform1fv(far, 1, &zFar); |
| 824 | |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 825 | GLint diff = programObject->getUniformLocation("gl_DepthRange.diff"); |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 826 | GLfloat zDiff = zFar - zNear; |
| 827 | programObject->setUniform1fv(diff, 1, &zDiff); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device |
| 834 | void Context::applyState() |
| 835 | { |
| 836 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 79b820b | 2010-03-16 05:48:57 +0000 | [diff] [blame] | 837 | Program *programObject = getCurrentProgram(); |
| 838 | |
| 839 | GLint frontCCW = programObject->getUniformLocation("__frontCCW"); |
| 840 | GLint ccw = (frontFace == GL_CCW); |
| 841 | programObject->setUniform1iv(frontCCW, 1, &ccw); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 842 | |
| 843 | if (cullFace) |
| 844 | { |
| 845 | device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace)); |
| 846 | } |
| 847 | else |
| 848 | { |
| 849 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 850 | } |
| 851 | |
| 852 | if (depthTest) |
| 853 | { |
| 854 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); |
| 855 | device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc)); |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); |
| 860 | } |
| 861 | |
| 862 | if (blend) |
| 863 | { |
| 864 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 865 | |
| 866 | if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && |
| 867 | destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) |
| 868 | { |
| 869 | device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor)); |
| 870 | } |
| 871 | else |
| 872 | { |
| 873 | device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha), |
| 874 | unorm<8>(blendColor.alpha), |
| 875 | unorm<8>(blendColor.alpha), |
| 876 | unorm<8>(blendColor.alpha))); |
| 877 | } |
| 878 | |
| 879 | device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB)); |
| 880 | device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB)); |
| 881 | device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB)); |
| 882 | |
| 883 | if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha) |
| 884 | { |
| 885 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 886 | |
| 887 | device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha)); |
| 888 | device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha)); |
| 889 | device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha)); |
| 890 | |
| 891 | } |
| 892 | else |
| 893 | { |
| 894 | device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); |
| 895 | } |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 900 | } |
| 901 | |
| 902 | if (stencilTest) |
| 903 | { |
| 904 | device->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 905 | device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); |
| 906 | |
| 907 | // FIXME: Unsupported by D3D9 |
| 908 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; |
| 909 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; |
| 910 | const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; |
daniel@transgaming.com | 1436e26 | 2010-03-17 03:58:56 +0000 | [diff] [blame^] | 911 | if(stencilWritemask != stencilBackWritemask || stencilRef != stencilBackRef || stencilMask != stencilBackMask) |
| 912 | { |
| 913 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); |
| 914 | return error(GL_INVALID_OPERATION); |
| 915 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 916 | |
| 917 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask); |
| 918 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc)); |
| 919 | |
| 920 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range |
| 921 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask); |
| 922 | |
| 923 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail)); |
| 924 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail)); |
| 925 | device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass)); |
| 926 | |
| 927 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask); |
| 928 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc)); |
| 929 | |
| 930 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range |
| 931 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask); |
| 932 | |
| 933 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail)); |
| 934 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail)); |
| 935 | device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass)); |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 940 | } |
| 941 | |
| 942 | device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha)); |
| 943 | device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE); |
| 944 | |
| 945 | if (polygonOffsetFill) |
| 946 | { |
| 947 | UNIMPLEMENTED(); // FIXME |
| 948 | } |
| 949 | |
| 950 | if (sampleAlphaToCoverage) |
| 951 | { |
| 952 | UNIMPLEMENTED(); // FIXME |
| 953 | } |
| 954 | |
| 955 | if (sampleCoverage) |
| 956 | { |
| 957 | UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one |
| 958 | } |
| 959 | |
| 960 | device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE); |
| 961 | } |
| 962 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 963 | // Fill in the programAttribute field of the array of TranslatedAttributes based on the active GLSL program. |
| 964 | void Context::lookupAttributeMapping(TranslatedAttribute *attributes) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 965 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 966 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 967 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 968 | if (attributes[i].enabled) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 969 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 970 | attributes[i].programAttribute = getCurrentProgram()->getInputMapping(i); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 973 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 974 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 975 | // The indices parameter to glDrawElements can have two interpretations: |
| 976 | // - as a pointer into client memory |
| 977 | // - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer |
| 978 | // Handle these cases here and return a pointer to the index data. |
| 979 | const Index *Context::adjustIndexPointer(const void *indices) |
| 980 | { |
| 981 | if (elementArrayBuffer) |
| 982 | { |
| 983 | Buffer *buffer = getBuffer(elementArrayBuffer); |
| 984 | return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices)); |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | return static_cast<const Index*>(indices); |
| 989 | } |
| 990 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 991 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 992 | void Context::applyVertexBuffer(GLint first, GLsizei count) |
| 993 | { |
| 994 | TranslatedAttribute translated[MAX_VERTEX_ATTRIBS]; |
| 995 | |
| 996 | mVertexDataManager->preRenderValidate(first, count, translated); |
| 997 | |
| 998 | lookupAttributeMapping(translated); |
| 999 | |
| 1000 | mBufferBackEnd->preDraw(translated); |
| 1001 | } |
| 1002 | |
| 1003 | void Context::applyVertexBuffer(GLsizei count, const void *indices, GLenum indexType) |
| 1004 | { |
| 1005 | TranslatedAttribute translated[MAX_VERTEX_ATTRIBS]; |
| 1006 | |
| 1007 | mVertexDataManager->preRenderValidate(adjustIndexPointer(indices), count, translated); |
| 1008 | |
| 1009 | lookupAttributeMapping(translated); |
| 1010 | |
| 1011 | mBufferBackEnd->preDraw(translated); |
| 1012 | } |
| 1013 | |
| 1014 | // Applies the indices and element array bindings to the Direct3D 9 device |
| 1015 | void Context::applyIndexBuffer(const void *indices, GLsizei count) |
| 1016 | { |
| 1017 | GLsizei length = count * sizeof(Index); |
| 1018 | |
| 1019 | IDirect3DDevice9 *device = getDevice(); |
| 1020 | |
| 1021 | IDirect3DIndexBuffer9 *indexBuffer = NULL; |
| 1022 | void *data; |
| 1023 | |
| 1024 | HRESULT result = device->CreateIndexBuffer(length, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &indexBuffer, NULL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1027 | { |
| 1028 | return error(GL_OUT_OF_MEMORY); |
| 1029 | } |
| 1030 | |
| 1031 | ASSERT(SUCCEEDED(result)); |
| 1032 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1033 | if (indexBuffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1034 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1035 | indexBuffer->Lock(0, length, &data, 0); |
| 1036 | memcpy(data, adjustIndexPointer(indices), length); |
| 1037 | indexBuffer->Unlock(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1038 | |
| 1039 | device->SetIndices(indexBuffer); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1040 | indexBuffer->Release(); // Will only effectively be deleted when no longer in use |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1041 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1042 | |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1043 | startIndex = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | // Applies the shaders and shader constants to the Direct3D 9 device |
| 1047 | void Context::applyShaders() |
| 1048 | { |
| 1049 | IDirect3DDevice9 *device = getDevice(); |
| 1050 | Program *programObject = getCurrentProgram(); |
| 1051 | IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader(); |
| 1052 | IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader(); |
| 1053 | |
| 1054 | device->SetVertexShader(vertexShader); |
| 1055 | device->SetPixelShader(pixelShader); |
| 1056 | |
| 1057 | programObject->applyUniforms(); |
| 1058 | } |
| 1059 | |
| 1060 | // Applies the textures and sampler states to the Direct3D 9 device |
| 1061 | void Context::applyTextures() |
| 1062 | { |
| 1063 | IDirect3DDevice9 *device = getDevice(); |
| 1064 | Program *programObject = getCurrentProgram(); |
| 1065 | |
| 1066 | for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++) |
| 1067 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1068 | int textureUnit = programObject->getSamplerMapping(sampler); |
| 1069 | if (textureUnit != -1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1070 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1071 | SamplerType textureType = programObject->getSamplerType(sampler); |
| 1072 | |
| 1073 | Texture *texture = getSamplerTexture(textureUnit, textureType); |
| 1074 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1075 | if (texture->isComplete()) |
| 1076 | { |
| 1077 | GLenum wrapS = texture->getWrapS(); |
| 1078 | GLenum wrapT = texture->getWrapT(); |
| 1079 | GLenum minFilter = texture->getMinFilter(); |
| 1080 | GLenum magFilter = texture->getMagFilter(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1081 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1082 | device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS)); |
| 1083 | device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1084 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1085 | device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter)); |
| 1086 | D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; |
| 1087 | es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter); |
| 1088 | device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter); |
| 1089 | device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1090 | |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1091 | device->SetTexture(sampler, texture->getTexture()); |
| 1092 | } |
| 1093 | else |
| 1094 | { |
| 1095 | device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture()); |
| 1096 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1097 | } |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1098 | else |
| 1099 | { |
| 1100 | device->SetTexture(sampler, NULL); |
| 1101 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels) |
| 1106 | { |
| 1107 | Framebuffer *framebuffer = getFramebuffer(); |
| 1108 | IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget(); |
| 1109 | IDirect3DDevice9 *device = getDevice(); |
| 1110 | |
| 1111 | D3DSURFACE_DESC desc; |
| 1112 | renderTarget->GetDesc(&desc); |
| 1113 | |
| 1114 | IDirect3DSurface9 *systemSurface; |
| 1115 | HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL); |
| 1116 | |
| 1117 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1118 | { |
| 1119 | return error(GL_OUT_OF_MEMORY); |
| 1120 | } |
| 1121 | |
| 1122 | ASSERT(SUCCEEDED(result)); |
| 1123 | |
| 1124 | if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) |
| 1125 | { |
| 1126 | UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target |
| 1127 | } |
| 1128 | |
| 1129 | result = device->GetRenderTargetData(renderTarget, systemSurface); |
| 1130 | |
| 1131 | if (result == D3DERR_DRIVERINTERNALERROR) |
| 1132 | { |
| 1133 | systemSurface->Release(); |
| 1134 | |
| 1135 | return error(GL_OUT_OF_MEMORY); |
| 1136 | } |
| 1137 | |
| 1138 | if (FAILED(result)) |
| 1139 | { |
| 1140 | UNREACHABLE(); |
| 1141 | systemSurface->Release(); |
| 1142 | |
| 1143 | return; // No sensible error to generate |
| 1144 | } |
| 1145 | |
| 1146 | D3DLOCKED_RECT lock; |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 1147 | RECT rect = {std::max(x, 0), |
| 1148 | std::max(y, 0), |
| 1149 | std::min(x + width, (int)desc.Width), |
| 1150 | std::min(y + height, (int)desc.Height)}; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1151 | |
| 1152 | result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); |
| 1153 | |
| 1154 | if (FAILED(result)) |
| 1155 | { |
| 1156 | UNREACHABLE(); |
| 1157 | systemSurface->Release(); |
| 1158 | |
| 1159 | return; // No sensible error to generate |
| 1160 | } |
| 1161 | |
| 1162 | unsigned char *source = (unsigned char*)lock.pBits; |
| 1163 | unsigned char *dest = (unsigned char*)pixels; |
| 1164 | |
| 1165 | for (int j = 0; j < rect.bottom - rect.top; j++) |
| 1166 | { |
| 1167 | for (int i = 0; i < rect.right - rect.left; i++) |
| 1168 | { |
| 1169 | float r; |
| 1170 | float g; |
| 1171 | float b; |
| 1172 | float a; |
| 1173 | |
| 1174 | switch (desc.Format) |
| 1175 | { |
| 1176 | case D3DFMT_R5G6B5: |
| 1177 | { |
| 1178 | unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch); |
| 1179 | |
| 1180 | a = 1.0f; |
| 1181 | b = (rgb & 0x001F) * (1.0f / 0x001F); |
| 1182 | g = (rgb & 0x07E0) * (1.0f / 0x07E0); |
| 1183 | r = (rgb & 0xF800) * (1.0f / 0xF800); |
| 1184 | } |
| 1185 | break; |
| 1186 | case D3DFMT_X1R5G5B5: |
| 1187 | { |
| 1188 | unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch); |
| 1189 | |
| 1190 | a = 1.0f; |
| 1191 | b = (xrgb & 0x001F) * (1.0f / 0x001F); |
| 1192 | g = (xrgb & 0x03E0) * (1.0f / 0x03E0); |
| 1193 | r = (xrgb & 0x7C00) * (1.0f / 0x7C00); |
| 1194 | } |
| 1195 | break; |
| 1196 | case D3DFMT_A1R5G5B5: |
| 1197 | { |
| 1198 | unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch); |
| 1199 | |
| 1200 | a = (argb & 0x8000) ? 1.0f : 0.0f; |
| 1201 | b = (argb & 0x001F) * (1.0f / 0x001F); |
| 1202 | g = (argb & 0x03E0) * (1.0f / 0x03E0); |
| 1203 | r = (argb & 0x7C00) * (1.0f / 0x7C00); |
| 1204 | } |
| 1205 | break; |
| 1206 | case D3DFMT_A8R8G8B8: |
| 1207 | { |
| 1208 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch); |
| 1209 | |
| 1210 | a = (argb & 0xFF000000) * (1.0f / 0xFF000000); |
| 1211 | b = (argb & 0x000000FF) * (1.0f / 0x000000FF); |
| 1212 | g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 1213 | r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 1214 | } |
| 1215 | break; |
| 1216 | case D3DFMT_X8R8G8B8: |
| 1217 | { |
| 1218 | unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch); |
| 1219 | |
| 1220 | a = 1.0f; |
| 1221 | b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); |
| 1222 | g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 1223 | r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 1224 | } |
| 1225 | break; |
| 1226 | case D3DFMT_A2R10G10B10: |
| 1227 | { |
| 1228 | unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch); |
| 1229 | |
| 1230 | a = (argb & 0xC0000000) * (1.0f / 0xC0000000); |
| 1231 | b = (argb & 0x000003FF) * (1.0f / 0x000003FF); |
| 1232 | g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); |
| 1233 | r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); |
| 1234 | } |
| 1235 | break; |
| 1236 | default: |
| 1237 | UNIMPLEMENTED(); // FIXME |
| 1238 | UNREACHABLE(); |
| 1239 | } |
| 1240 | |
| 1241 | switch (format) |
| 1242 | { |
| 1243 | case GL_RGBA: |
| 1244 | switch (type) |
| 1245 | { |
| 1246 | case GL_UNSIGNED_BYTE: |
| 1247 | dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f); |
| 1248 | dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f); |
| 1249 | dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f); |
| 1250 | dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f); |
| 1251 | break; |
| 1252 | default: UNREACHABLE(); |
| 1253 | } |
| 1254 | break; |
| 1255 | case IMPLEMENTATION_COLOR_READ_FORMAT: |
| 1256 | switch (type) |
| 1257 | { |
| 1258 | case IMPLEMENTATION_COLOR_READ_TYPE: |
| 1259 | break; |
| 1260 | default: UNREACHABLE(); |
| 1261 | } |
| 1262 | break; |
| 1263 | default: UNREACHABLE(); |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | systemSurface->UnlockRect(); |
| 1269 | |
| 1270 | systemSurface->Release(); |
| 1271 | } |
| 1272 | |
| 1273 | void Context::clear(GLbitfield mask) |
| 1274 | { |
| 1275 | IDirect3DDevice9 *device = getDevice(); |
| 1276 | DWORD flags = 0; |
| 1277 | |
| 1278 | if (mask & GL_COLOR_BUFFER_BIT) |
| 1279 | { |
| 1280 | mask &= ~GL_COLOR_BUFFER_BIT; |
| 1281 | flags |= D3DCLEAR_TARGET; |
| 1282 | } |
| 1283 | |
| 1284 | if (mask & GL_DEPTH_BUFFER_BIT) |
| 1285 | { |
| 1286 | mask &= ~GL_DEPTH_BUFFER_BIT; |
| 1287 | if (depthMask) |
| 1288 | { |
| 1289 | flags |= D3DCLEAR_ZBUFFER; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | Framebuffer *framebufferObject = getFramebuffer(); |
| 1294 | IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil(); |
| 1295 | |
| 1296 | GLuint stencilUnmasked = 0x0; |
| 1297 | |
| 1298 | if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil) |
| 1299 | { |
| 1300 | D3DSURFACE_DESC desc; |
| 1301 | depthStencil->GetDesc(&desc); |
| 1302 | |
| 1303 | mask &= ~GL_STENCIL_BUFFER_BIT; |
| 1304 | unsigned int stencilSize = es2dx::GetStencilSize(desc.Format); |
| 1305 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1306 | |
| 1307 | if (stencilUnmasked != 0x0) |
| 1308 | { |
| 1309 | flags |= D3DCLEAR_STENCIL; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | if (mask != 0) |
| 1314 | { |
| 1315 | return error(GL_INVALID_VALUE); |
| 1316 | } |
| 1317 | |
| 1318 | applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport |
| 1319 | |
| 1320 | D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue)); |
| 1321 | float depth = clamp01(depthClearValue); |
| 1322 | int stencil = stencilClearValue & 0x000000FF; |
| 1323 | |
| 1324 | IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget(); |
| 1325 | |
| 1326 | D3DSURFACE_DESC desc; |
| 1327 | renderTarget->GetDesc(&desc); |
| 1328 | |
| 1329 | bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha; |
| 1330 | |
| 1331 | const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) && |
| 1332 | (stencilWritemask & stencilUnmasked) != stencilUnmasked; |
| 1333 | const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) && |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1334 | !(colorMaskRed && colorMaskGreen && |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1335 | colorMaskBlue && alphaUnmasked); |
| 1336 | |
| 1337 | if (needMaskedColorClear || needMaskedStencilClear) |
| 1338 | { |
| 1339 | device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 1340 | device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); |
| 1341 | device->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 1342 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 1343 | device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 1344 | device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 1345 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 1346 | device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 1347 | |
| 1348 | if (flags & D3DCLEAR_TARGET) |
| 1349 | { |
| 1350 | device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) | |
| 1351 | (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) | |
| 1352 | (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) | |
| 1353 | (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0)); |
| 1354 | } |
| 1355 | else |
| 1356 | { |
| 1357 | device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); |
| 1358 | } |
| 1359 | |
| 1360 | if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL)) |
| 1361 | { |
| 1362 | device->SetRenderState(D3DRS_STENCILENABLE, TRUE); |
| 1363 | device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); |
| 1364 | device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); |
| 1365 | device->SetRenderState(D3DRS_STENCILREF, stencil); |
| 1366 | device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1367 | device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1368 | device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); |
| 1369 | device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); |
| 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 1374 | } |
| 1375 | |
| 1376 | device->SetPixelShader(NULL); |
| 1377 | device->SetVertexShader(NULL); |
| 1378 | device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); |
| 1379 | |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 1380 | struct Vertex |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1381 | { |
| 1382 | float x, y, z, w; |
| 1383 | D3DCOLOR diffuse; |
| 1384 | }; |
| 1385 | |
| 1386 | Vertex quad[4]; |
| 1387 | quad[0].x = 0.0f; |
| 1388 | quad[0].y = (float)desc.Height; |
| 1389 | quad[0].z = 0.0f; |
| 1390 | quad[0].w = 1.0f; |
| 1391 | quad[0].diffuse = color; |
| 1392 | |
| 1393 | quad[1].x = (float)desc.Width; |
| 1394 | quad[1].y = (float)desc.Height; |
| 1395 | quad[1].z = 0.0f; |
| 1396 | quad[1].w = 1.0f; |
| 1397 | quad[1].diffuse = color; |
| 1398 | |
| 1399 | quad[2].x = 0.0f; |
| 1400 | quad[2].y = 0.0f; |
| 1401 | quad[2].z = 0.0f; |
| 1402 | quad[2].w = 1.0f; |
| 1403 | quad[2].diffuse = color; |
| 1404 | |
| 1405 | quad[3].x = (float)desc.Width; |
| 1406 | quad[3].y = 0.0f; |
| 1407 | quad[3].z = 0.0f; |
| 1408 | quad[3].w = 1.0f; |
| 1409 | quad[3].diffuse = color; |
| 1410 | |
| 1411 | device->BeginScene(); |
| 1412 | device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex)); |
| 1413 | device->EndScene(); |
| 1414 | |
| 1415 | if (flags & D3DCLEAR_ZBUFFER) |
| 1416 | { |
| 1417 | device->SetRenderState(D3DRS_ZENABLE, TRUE); |
| 1418 | device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); |
| 1419 | device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); |
| 1420 | } |
| 1421 | } |
| 1422 | else |
| 1423 | { |
| 1424 | device->Clear(0, NULL, flags, color, depth, stencil); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | void Context::drawArrays(GLenum mode, GLint first, GLsizei count) |
| 1429 | { |
| 1430 | if (!currentProgram) |
| 1431 | { |
| 1432 | return error(GL_INVALID_OPERATION); |
| 1433 | } |
| 1434 | |
| 1435 | IDirect3DDevice9 *device = getDevice(); |
| 1436 | D3DPRIMITIVETYPE primitiveType; |
| 1437 | int primitiveCount; |
| 1438 | |
| 1439 | if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount)) |
| 1440 | return error(GL_INVALID_ENUM); |
| 1441 | |
| 1442 | if (primitiveCount <= 0) |
| 1443 | { |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | if (!applyRenderTarget(false)) |
| 1448 | { |
| 1449 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
| 1450 | } |
| 1451 | |
| 1452 | applyState(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1453 | applyVertexBuffer(first, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1454 | applyShaders(); |
| 1455 | applyTextures(); |
| 1456 | |
| 1457 | device->BeginScene(); |
| 1458 | device->DrawPrimitive(primitiveType, first, primitiveCount); |
| 1459 | device->EndScene(); |
| 1460 | } |
| 1461 | |
| 1462 | void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices) |
| 1463 | { |
| 1464 | if (!currentProgram) |
| 1465 | { |
| 1466 | return error(GL_INVALID_OPERATION); |
| 1467 | } |
| 1468 | |
| 1469 | if (!indices && !elementArrayBuffer) |
| 1470 | { |
| 1471 | return error(GL_INVALID_OPERATION); |
| 1472 | } |
| 1473 | |
| 1474 | IDirect3DDevice9 *device = getDevice(); |
| 1475 | D3DPRIMITIVETYPE primitiveType; |
| 1476 | int primitiveCount; |
| 1477 | |
| 1478 | if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount)) |
| 1479 | return error(GL_INVALID_ENUM); |
| 1480 | |
| 1481 | if (primitiveCount <= 0) |
| 1482 | { |
| 1483 | return; |
| 1484 | } |
| 1485 | |
| 1486 | if (!applyRenderTarget(false)) |
| 1487 | { |
| 1488 | return error(GL_INVALID_FRAMEBUFFER_OPERATION); |
| 1489 | } |
| 1490 | |
| 1491 | applyState(); |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1492 | applyVertexBuffer(count, indices, type); |
| 1493 | applyIndexBuffer(indices, count); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1494 | applyShaders(); |
| 1495 | applyTextures(); |
| 1496 | |
| 1497 | device->BeginScene(); |
| 1498 | device->DrawIndexedPrimitive(primitiveType, 0, 0, count, startIndex, primitiveCount); |
| 1499 | device->EndScene(); |
| 1500 | } |
| 1501 | |
| 1502 | void Context::finish() |
| 1503 | { |
| 1504 | IDirect3DDevice9 *device = getDevice(); |
| 1505 | IDirect3DQuery9 *occlusionQuery = NULL; |
| 1506 | |
| 1507 | HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery); |
| 1508 | |
| 1509 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1510 | { |
| 1511 | return error(GL_OUT_OF_MEMORY); |
| 1512 | } |
| 1513 | |
| 1514 | ASSERT(SUCCEEDED(result)); |
| 1515 | |
| 1516 | if (occlusionQuery) |
| 1517 | { |
| 1518 | occlusionQuery->Issue(D3DISSUE_BEGIN); |
| 1519 | |
| 1520 | // Render something outside the render target |
| 1521 | device->SetPixelShader(NULL); |
| 1522 | device->SetVertexShader(NULL); |
| 1523 | device->SetFVF(D3DFVF_XYZRHW); |
| 1524 | float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f}; |
| 1525 | device->BeginScene(); |
| 1526 | device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data)); |
| 1527 | device->EndScene(); |
| 1528 | |
| 1529 | occlusionQuery->Issue(D3DISSUE_END); |
| 1530 | |
| 1531 | while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) |
| 1532 | { |
| 1533 | // Keep polling, but allow other threads to do something useful first |
| 1534 | Sleep(0); |
| 1535 | } |
| 1536 | |
| 1537 | occlusionQuery->Release(); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | void Context::flush() |
| 1542 | { |
| 1543 | IDirect3DDevice9 *device = getDevice(); |
| 1544 | IDirect3DQuery9 *eventQuery = NULL; |
| 1545 | |
| 1546 | HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery); |
| 1547 | |
| 1548 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 1549 | { |
| 1550 | return error(GL_OUT_OF_MEMORY); |
| 1551 | } |
| 1552 | |
| 1553 | ASSERT(SUCCEEDED(result)); |
| 1554 | |
| 1555 | if (eventQuery) |
| 1556 | { |
| 1557 | eventQuery->Issue(D3DISSUE_END); |
| 1558 | |
| 1559 | while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE) |
| 1560 | { |
| 1561 | // Keep polling, but allow other threads to do something useful first |
| 1562 | Sleep(0); |
| 1563 | } |
| 1564 | |
| 1565 | eventQuery->Release(); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | void Context::recordInvalidEnum() |
| 1570 | { |
| 1571 | mInvalidEnum = true; |
| 1572 | } |
| 1573 | |
| 1574 | void Context::recordInvalidValue() |
| 1575 | { |
| 1576 | mInvalidValue = true; |
| 1577 | } |
| 1578 | |
| 1579 | void Context::recordInvalidOperation() |
| 1580 | { |
| 1581 | mInvalidOperation = true; |
| 1582 | } |
| 1583 | |
| 1584 | void Context::recordOutOfMemory() |
| 1585 | { |
| 1586 | mOutOfMemory = true; |
| 1587 | } |
| 1588 | |
| 1589 | void Context::recordInvalidFramebufferOperation() |
| 1590 | { |
| 1591 | mInvalidFramebufferOperation = true; |
| 1592 | } |
| 1593 | |
| 1594 | // Get one of the recorded errors and clear its flag, if any. |
| 1595 | // [OpenGL ES 2.0.24] section 2.5 page 13. |
| 1596 | GLenum Context::getError() |
| 1597 | { |
| 1598 | if (mInvalidEnum) |
| 1599 | { |
| 1600 | mInvalidEnum = false; |
| 1601 | |
| 1602 | return GL_INVALID_ENUM; |
| 1603 | } |
| 1604 | |
| 1605 | if (mInvalidValue) |
| 1606 | { |
| 1607 | mInvalidValue = false; |
| 1608 | |
| 1609 | return GL_INVALID_VALUE; |
| 1610 | } |
| 1611 | |
| 1612 | if (mInvalidOperation) |
| 1613 | { |
| 1614 | mInvalidOperation = false; |
| 1615 | |
| 1616 | return GL_INVALID_OPERATION; |
| 1617 | } |
| 1618 | |
| 1619 | if (mOutOfMemory) |
| 1620 | { |
| 1621 | mOutOfMemory = false; |
| 1622 | |
| 1623 | return GL_OUT_OF_MEMORY; |
| 1624 | } |
| 1625 | |
| 1626 | if (mInvalidFramebufferOperation) |
| 1627 | { |
| 1628 | mInvalidFramebufferOperation = false; |
| 1629 | |
| 1630 | return GL_INVALID_FRAMEBUFFER_OPERATION; |
| 1631 | } |
| 1632 | |
| 1633 | return GL_NO_ERROR; |
| 1634 | } |
| 1635 | |
| 1636 | void Context::detachBuffer(GLuint buffer) |
| 1637 | { |
| 1638 | // [OpenGL ES 2.0.24] section 2.9 page 22: |
| 1639 | // If a buffer object is deleted while it is bound, all bindings to that object in the current context |
| 1640 | // (i.e. in the thread that called Delete-Buffers) are reset to zero. |
| 1641 | |
| 1642 | if (arrayBuffer == buffer) |
| 1643 | { |
| 1644 | arrayBuffer = 0; |
| 1645 | } |
| 1646 | |
| 1647 | if (elementArrayBuffer == buffer) |
| 1648 | { |
| 1649 | elementArrayBuffer = 0; |
| 1650 | } |
| 1651 | |
| 1652 | for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) |
| 1653 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1654 | if (vertexAttribute[attribute].mBoundBuffer == buffer) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1655 | { |
daniel@transgaming.com | 0f7aaf5 | 2010-03-11 19:41:38 +0000 | [diff] [blame] | 1656 | vertexAttribute[attribute].mBoundBuffer = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | void Context::detachTexture(GLuint texture) |
| 1662 | { |
| 1663 | // [OpenGL ES 2.0.24] section 3.8 page 84: |
| 1664 | // If a texture object is deleted, it is as if all texture units which are bound to that texture object are |
| 1665 | // rebound to texture object zero |
| 1666 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1667 | for (int type = 0; type < SAMPLER_TYPE_COUNT; type++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1668 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1669 | for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1670 | { |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 1671 | if (samplerTexture[type][sampler] == texture) |
| 1672 | { |
| 1673 | samplerTexture[type][sampler] = 0; |
| 1674 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | // [OpenGL ES 2.0.24] section 4.4 page 112: |
| 1679 | // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is |
| 1680 | // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this |
| 1681 | // image was attached in the currently bound framebuffer. |
| 1682 | |
| 1683 | Framebuffer *framebuffer = getFramebuffer(); |
| 1684 | |
| 1685 | if (framebuffer) |
| 1686 | { |
| 1687 | framebuffer->detachTexture(texture); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | void Context::detachFramebuffer(GLuint framebuffer) |
| 1692 | { |
| 1693 | // [OpenGL ES 2.0.24] section 4.4 page 107: |
| 1694 | // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though |
| 1695 | // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero. |
| 1696 | |
| 1697 | if (this->framebuffer == framebuffer) |
| 1698 | { |
| 1699 | bindFramebuffer(0); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | void Context::detachRenderbuffer(GLuint renderbuffer) |
| 1704 | { |
| 1705 | // [OpenGL ES 2.0.24] section 4.4 page 109: |
| 1706 | // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer |
| 1707 | // had been executed with the target RENDERBUFFER and name of zero. |
| 1708 | |
| 1709 | if (this->renderbuffer == renderbuffer) |
| 1710 | { |
| 1711 | bindRenderbuffer(0); |
| 1712 | } |
| 1713 | |
| 1714 | // [OpenGL ES 2.0.24] section 4.4 page 111: |
| 1715 | // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer, |
| 1716 | // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment |
| 1717 | // point to which this image was attached in the currently bound framebuffer. |
| 1718 | |
| 1719 | Framebuffer *framebuffer = getFramebuffer(); |
| 1720 | |
| 1721 | if (framebuffer) |
| 1722 | { |
| 1723 | framebuffer->detachRenderbuffer(renderbuffer); |
| 1724 | } |
| 1725 | } |
daniel@transgaming.com | 12d5407 | 2010-03-16 06:23:26 +0000 | [diff] [blame] | 1726 | |
| 1727 | Texture *Context::getIncompleteTexture(SamplerType type) |
| 1728 | { |
| 1729 | Texture *t = mIncompleteTextures[type]; |
| 1730 | |
| 1731 | if (t == NULL) |
| 1732 | { |
| 1733 | static const GLubyte color[] = { 0, 0, 0, 255 }; |
| 1734 | |
| 1735 | switch (type) |
| 1736 | { |
| 1737 | default: |
| 1738 | UNREACHABLE(); |
| 1739 | // default falls through to SAMPLER_2D |
| 1740 | |
| 1741 | case SAMPLER_2D: |
| 1742 | { |
| 1743 | Texture2D *incomplete2d = new Texture2D; |
| 1744 | incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1745 | t = incomplete2d; |
| 1746 | } |
| 1747 | break; |
| 1748 | |
| 1749 | case SAMPLER_CUBE: |
| 1750 | { |
| 1751 | TextureCubeMap *incompleteCube = new TextureCubeMap; |
| 1752 | |
| 1753 | incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1754 | incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1755 | incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1756 | incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1757 | incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1758 | incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color); |
| 1759 | |
| 1760 | t = incompleteCube; |
| 1761 | } |
| 1762 | break; |
| 1763 | } |
| 1764 | |
| 1765 | mIncompleteTextures[type] = t; |
| 1766 | } |
| 1767 | |
| 1768 | return t; |
| 1769 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | extern "C" |
| 1773 | { |
| 1774 | gl::Context *glCreateContext(const egl::Config *config) |
| 1775 | { |
| 1776 | return new gl::Context(config); |
| 1777 | } |
| 1778 | |
| 1779 | void glDestroyContext(gl::Context *context) |
| 1780 | { |
| 1781 | delete context; |
| 1782 | |
| 1783 | if (context == gl::getContext()) |
| 1784 | { |
| 1785 | gl::makeCurrent(NULL, NULL, NULL); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface) |
| 1790 | { |
| 1791 | gl::makeCurrent(context, display, surface); |
| 1792 | } |
| 1793 | |
| 1794 | gl::Context *glGetCurrentContext() |
| 1795 | { |
| 1796 | return gl::getContext(); |
| 1797 | } |
| 1798 | } |