blob: eab929e0e0d07d5c3473dcb521c002cbb34d26e7 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
22#include "libGLESv2/FrameBuffer.h"
23#include "libGLESv2/Program.h"
24#include "libGLESv2/RenderBuffer.h"
25#include "libGLESv2/Shader.h"
26#include "libGLESv2/Texture.h"
27#include "libGLESv2/geometry/backend.h"
28#include "libGLESv2/geometry/VertexDataManager.h"
29#include "libGLESv2/geometry/IndexDataManager.h"
30#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000038 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039{
40 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000041
daniel@transgaming.com428d1582010-05-04 03:35:25 +000042 mState.depthClearValue = 1.0f;
43 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
daniel@transgaming.com428d1582010-05-04 03:35:25 +000045 mState.cullFace = false;
46 mState.cullMode = GL_BACK;
47 mState.frontFace = GL_CCW;
48 mState.depthTest = false;
49 mState.depthFunc = GL_LESS;
50 mState.blend = false;
51 mState.sourceBlendRGB = GL_ONE;
52 mState.sourceBlendAlpha = GL_ONE;
53 mState.destBlendRGB = GL_ZERO;
54 mState.destBlendAlpha = GL_ZERO;
55 mState.blendEquationRGB = GL_FUNC_ADD;
56 mState.blendEquationAlpha = GL_FUNC_ADD;
57 mState.blendColor.red = 0;
58 mState.blendColor.green = 0;
59 mState.blendColor.blue = 0;
60 mState.blendColor.alpha = 0;
61 mState.stencilTest = false;
62 mState.stencilFunc = GL_ALWAYS;
63 mState.stencilRef = 0;
64 mState.stencilMask = -1;
65 mState.stencilWritemask = -1;
66 mState.stencilBackFunc = GL_ALWAYS;
67 mState.stencilBackRef = 0;
68 mState.stencilBackMask = - 1;
69 mState.stencilBackWritemask = -1;
70 mState.stencilFail = GL_KEEP;
71 mState.stencilPassDepthFail = GL_KEEP;
72 mState.stencilPassDepthPass = GL_KEEP;
73 mState.stencilBackFail = GL_KEEP;
74 mState.stencilBackPassDepthFail = GL_KEEP;
75 mState.stencilBackPassDepthPass = GL_KEEP;
76 mState.polygonOffsetFill = false;
77 mState.polygonOffsetFactor = 0.0f;
78 mState.polygonOffsetUnits = 0.0f;
79 mState.sampleAlphaToCoverage = false;
80 mState.sampleCoverage = false;
81 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000082 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000083 mState.scissorTest = false;
84 mState.dither = true;
85 mState.generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
daniel@transgaming.com428d1582010-05-04 03:35:25 +000087 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.viewportX = 0;
90 mState.viewportY = 0;
91 mState.viewportWidth = config->mDisplayMode.Width;
92 mState.viewportHeight = config->mDisplayMode.Height;
93 mState.zNear = 0.0f;
94 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.scissorX = 0;
97 mState.scissorY = 0;
98 mState.scissorWidth = config->mDisplayMode.Width;
99 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000101 mState.colorMaskRed = true;
102 mState.colorMaskGreen = true;
103 mState.colorMaskBlue = true;
104 mState.colorMaskAlpha = true;
105 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000107 if (shareContext != NULL)
108 {
109 mResourceManager = shareContext->mResourceManager;
110 mResourceManager->addRef();
111 }
112 else
113 {
114 mResourceManager = new ResourceManager();
115 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 // [OpenGL ES 2.0.24] section 3.7 page 83:
118 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
119 // and cube map texture state vectors respectively associated with them.
120 // In order that access to these initial textures not be lost, they are treated as texture
121 // objects all of whose names are 0.
122
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000123 mTexture2DZero = new Texture2D();
124 mTextureCubeMapZero = new TextureCubeMap();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
126 mColorbufferZero = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000127 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000129 mState.activeSampler = 0;
130 mState.arrayBuffer = 0;
131 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 bindTextureCubeMap(0);
133 bindTexture2D(0);
134 bindFramebuffer(0);
135 bindRenderbuffer(0);
136
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000137 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000139 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
140 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000141 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143 }
144
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000145 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
146 {
147 mIncompleteTextures[type] = NULL;
148 }
149
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000150 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000152 mState.packAlignment = 4;
153 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000154
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000155 mBufferBackEnd = NULL;
156 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000157 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000158 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000159
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160 mInvalidEnum = false;
161 mInvalidValue = false;
162 mInvalidOperation = false;
163 mOutOfMemory = false;
164 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000165
166 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000167
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000168 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000169 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170}
171
172Context::~Context()
173{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000174 if (mState.currentProgram != 0)
175 {
176 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
177 if (programObject)
178 {
179 programObject->release();
180 }
181 mState.currentProgram = 0;
182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000184 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
185 {
186 delete mIncompleteTextures[type];
187 }
188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189 delete mTexture2DZero;
190 delete mTextureCubeMapZero;
191
192 delete mColorbufferZero;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000193 delete mDepthStencilbufferZero;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000195 delete mBufferBackEnd;
196 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000197 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000198 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000199
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200 while (!mFramebufferMap.empty())
201 {
202 deleteFramebuffer(mFramebufferMap.begin()->first);
203 }
204
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000205 if (mMaskedClearSavedState)
206 {
207 mMaskedClearSavedState->Release();
208 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000209
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000210 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211}
212
213void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
214{
215 IDirect3DDevice9 *device = display->getDevice();
216
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000217 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000218 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000219 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000220
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000221 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000222 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000223 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000224 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000225
226 initExtensionString();
227
228 mState.viewportX = 0;
229 mState.viewportY = 0;
230 mState.viewportWidth = surface->getWidth();
231 mState.viewportHeight = surface->getHeight();
232
233 mState.scissorX = 0;
234 mState.scissorY = 0;
235 mState.scissorWidth = surface->getWidth();
236 mState.scissorHeight = surface->getHeight();
237
238 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 }
240
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
242 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000243 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
daniel@transgaming.com0190f842010-07-28 19:21:02 +0000245 Framebuffer *framebufferZero = new Framebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000247 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248
249 setFramebufferZero(framebufferZero);
250 setColorbufferZero(colorbufferZero);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000251 setDepthStencilbufferZero(depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252
253 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
254 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
255 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
256
257 defaultRenderTarget->Release();
258
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000259 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000261 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000263
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000264 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000265
266 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267}
268
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000269// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000270void Context::markAllStateDirty()
271{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000272 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000273 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000274 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000275
276 mClearStateDirty = true;
277 mCullStateDirty = true;
278 mDepthStateDirty = true;
279 mMaskStateDirty = true;
280 mBlendStateDirty = true;
281 mStencilStateDirty = true;
282 mPolygonOffsetStateDirty = true;
283 mScissorStateDirty = true;
284 mSampleStateDirty = true;
285 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000286 mFrontFaceDirty = true;
287
288 if (mBufferBackEnd != NULL)
289 {
290 mBufferBackEnd->invalidate();
291 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000292}
293
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294void Context::setClearColor(float red, float green, float blue, float alpha)
295{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000296 mState.colorClearValue.red = red;
297 mState.colorClearValue.green = green;
298 mState.colorClearValue.blue = blue;
299 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300}
301
302void Context::setClearDepth(float depth)
303{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000304 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305}
306
307void Context::setClearStencil(int stencil)
308{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000309 mState.stencilClearValue = stencil;
310}
311
312void Context::setCullFace(bool enabled)
313{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000314 if (mState.cullFace != enabled)
315 {
316 mState.cullFace = enabled;
317 mCullStateDirty = true;
318 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000319}
320
321bool Context::isCullFaceEnabled() const
322{
323 return mState.cullFace;
324}
325
326void Context::setCullMode(GLenum mode)
327{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000328 if (mState.cullMode != mode)
329 {
330 mState.cullMode = mode;
331 mCullStateDirty = true;
332 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000333}
334
335void Context::setFrontFace(GLenum front)
336{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000337 if (mState.frontFace != front)
338 {
339 mState.frontFace = front;
340 mFrontFaceDirty = true;
341 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000342}
343
344void Context::setDepthTest(bool enabled)
345{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000346 if (mState.depthTest != enabled)
347 {
348 mState.depthTest = enabled;
349 mDepthStateDirty = true;
350 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351}
352
353bool Context::isDepthTestEnabled() const
354{
355 return mState.depthTest;
356}
357
358void Context::setDepthFunc(GLenum depthFunc)
359{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000360 if (mState.depthFunc != depthFunc)
361 {
362 mState.depthFunc = depthFunc;
363 mDepthStateDirty = true;
364 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000365}
366
367void Context::setDepthRange(float zNear, float zFar)
368{
369 mState.zNear = zNear;
370 mState.zFar = zFar;
371}
372
373void Context::setBlend(bool enabled)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.blend != enabled)
376 {
377 mState.blend = enabled;
378 mBlendStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382bool Context::isBlendEnabled() const
383{
384 return mState.blend;
385}
386
387void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
388{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000389 if (mState.sourceBlendRGB != sourceRGB ||
390 mState.sourceBlendAlpha != sourceAlpha ||
391 mState.destBlendRGB != destRGB ||
392 mState.destBlendAlpha != destAlpha)
393 {
394 mState.sourceBlendRGB = sourceRGB;
395 mState.destBlendRGB = destRGB;
396 mState.sourceBlendAlpha = sourceAlpha;
397 mState.destBlendAlpha = destAlpha;
398 mBlendStateDirty = true;
399 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000400}
401
402void Context::setBlendColor(float red, float green, float blue, float alpha)
403{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000404 if (mState.blendColor.red != red ||
405 mState.blendColor.green != green ||
406 mState.blendColor.blue != blue ||
407 mState.blendColor.alpha != alpha)
408 {
409 mState.blendColor.red = red;
410 mState.blendColor.green = green;
411 mState.blendColor.blue = blue;
412 mState.blendColor.alpha = alpha;
413 mBlendStateDirty = true;
414 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000415}
416
417void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
418{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000419 if (mState.blendEquationRGB != rgbEquation ||
420 mState.blendEquationAlpha != alphaEquation)
421 {
422 mState.blendEquationRGB = rgbEquation;
423 mState.blendEquationAlpha = alphaEquation;
424 mBlendStateDirty = true;
425 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000426}
427
428void Context::setStencilTest(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.stencilTest != enabled)
431 {
432 mState.stencilTest = enabled;
433 mStencilStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isStencilTestEnabled() const
438{
439 return mState.stencilTest;
440}
441
442void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.stencilFunc != stencilFunc ||
445 mState.stencilRef != stencilRef ||
446 mState.stencilMask != stencilMask)
447 {
448 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000449 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000450 mState.stencilMask = stencilMask;
451 mStencilStateDirty = true;
452 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000453}
454
455void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
456{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000457 if (mState.stencilBackFunc != stencilBackFunc ||
458 mState.stencilBackRef != stencilBackRef ||
459 mState.stencilBackMask != stencilBackMask)
460 {
461 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000462 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000463 mState.stencilBackMask = stencilBackMask;
464 mStencilStateDirty = true;
465 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000466}
467
468void Context::setStencilWritemask(GLuint stencilWritemask)
469{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000470 if (mState.stencilWritemask != stencilWritemask)
471 {
472 mState.stencilWritemask = stencilWritemask;
473 mStencilStateDirty = true;
474 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000475}
476
477void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
478{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000479 if (mState.stencilBackWritemask != stencilBackWritemask)
480 {
481 mState.stencilBackWritemask = stencilBackWritemask;
482 mStencilStateDirty = true;
483 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000484}
485
486void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
487{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000488 if (mState.stencilFail != stencilFail ||
489 mState.stencilPassDepthFail != stencilPassDepthFail ||
490 mState.stencilPassDepthPass != stencilPassDepthPass)
491 {
492 mState.stencilFail = stencilFail;
493 mState.stencilPassDepthFail = stencilPassDepthFail;
494 mState.stencilPassDepthPass = stencilPassDepthPass;
495 mStencilStateDirty = true;
496 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000497}
498
499void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
500{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000501 if (mState.stencilBackFail != stencilBackFail ||
502 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
503 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
504 {
505 mState.stencilBackFail = stencilBackFail;
506 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
507 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
508 mStencilStateDirty = true;
509 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000510}
511
512void Context::setPolygonOffsetFill(bool enabled)
513{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000514 if (mState.polygonOffsetFill != enabled)
515 {
516 mState.polygonOffsetFill = enabled;
517 mPolygonOffsetStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521bool Context::isPolygonOffsetFillEnabled() const
522{
523 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000525}
526
527void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
528{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000529 if (mState.polygonOffsetFactor != factor ||
530 mState.polygonOffsetUnits != units)
531 {
532 mState.polygonOffsetFactor = factor;
533 mState.polygonOffsetUnits = units;
534 mPolygonOffsetStateDirty = true;
535 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000536}
537
538void Context::setSampleAlphaToCoverage(bool enabled)
539{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000540 if (mState.sampleAlphaToCoverage != enabled)
541 {
542 mState.sampleAlphaToCoverage = enabled;
543 mSampleStateDirty = true;
544 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000545}
546
547bool Context::isSampleAlphaToCoverageEnabled() const
548{
549 return mState.sampleAlphaToCoverage;
550}
551
552void Context::setSampleCoverage(bool enabled)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.sampleCoverage != enabled)
555 {
556 mState.sampleCoverage = enabled;
557 mSampleStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561bool Context::isSampleCoverageEnabled() const
562{
563 return mState.sampleCoverage;
564}
565
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000566void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.sampleCoverageValue != value ||
569 mState.sampleCoverageInvert != invert)
570 {
571 mState.sampleCoverageValue = value;
572 mState.sampleCoverageInvert = invert;
573 mSampleStateDirty = true;
574 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000575}
576
577void Context::setScissorTest(bool enabled)
578{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579 if (mState.scissorTest != enabled)
580 {
581 mState.scissorTest = enabled;
582 mScissorStateDirty = true;
583 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000584}
585
586bool Context::isScissorTestEnabled() const
587{
588 return mState.scissorTest;
589}
590
591void Context::setDither(bool enabled)
592{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000593 if (mState.dither != enabled)
594 {
595 mState.dither = enabled;
596 mDitherStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600bool Context::isDitherEnabled() const
601{
602 return mState.dither;
603}
604
605void Context::setLineWidth(GLfloat width)
606{
607 mState.lineWidth = width;
608}
609
610void Context::setGenerateMipmapHint(GLenum hint)
611{
612 mState.generateMipmapHint = hint;
613}
614
615void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
616{
617 mState.viewportX = x;
618 mState.viewportY = y;
619 mState.viewportWidth = width;
620 mState.viewportHeight = height;
621}
622
623void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
624{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000625 if (mState.scissorX != x || mState.scissorY != y ||
626 mState.scissorWidth != width || mState.scissorHeight != height)
627 {
628 mState.scissorX = x;
629 mState.scissorY = y;
630 mState.scissorWidth = width;
631 mState.scissorHeight = height;
632 mScissorStateDirty = true;
633 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634}
635
636void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
637{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000638 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
639 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
640 {
641 mState.colorMaskRed = red;
642 mState.colorMaskGreen = green;
643 mState.colorMaskBlue = blue;
644 mState.colorMaskAlpha = alpha;
645 mMaskStateDirty = true;
646 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000647}
648
649void Context::setDepthMask(bool mask)
650{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000651 if (mState.depthMask != mask)
652 {
653 mState.depthMask = mask;
654 mMaskStateDirty = true;
655 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000656}
657
658void Context::setActiveSampler(int active)
659{
660 mState.activeSampler = active;
661}
662
663GLuint Context::getFramebufferHandle() const
664{
665 return mState.framebuffer;
666}
667
668GLuint Context::getRenderbufferHandle() const
669{
670 return mState.renderbuffer;
671}
672
673GLuint Context::getArrayBufferHandle() const
674{
675 return mState.arrayBuffer;
676}
677
678void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
679{
680 mState.vertexAttribute[attribNum].mEnabled = enabled;
681}
682
683const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
684{
685 return mState.vertexAttribute[attribNum];
686}
687
688void Context::setVertexAttribState(unsigned int attribNum, GLuint boundBuffer, GLint size, GLenum type, bool normalized,
689 GLsizei stride, const void *pointer)
690{
691 mState.vertexAttribute[attribNum].mBoundBuffer = boundBuffer;
692 mState.vertexAttribute[attribNum].mSize = size;
693 mState.vertexAttribute[attribNum].mType = type;
694 mState.vertexAttribute[attribNum].mNormalized = normalized;
695 mState.vertexAttribute[attribNum].mStride = stride;
696 mState.vertexAttribute[attribNum].mPointer = pointer;
697}
698
699const void *Context::getVertexAttribPointer(unsigned int attribNum) const
700{
701 return mState.vertexAttribute[attribNum].mPointer;
702}
703
704// returns entire set of attributes as a block
705const AttributeState *Context::getVertexAttribBlock()
706{
707 return mState.vertexAttribute;
708}
709
710void Context::setPackAlignment(GLint alignment)
711{
712 mState.packAlignment = alignment;
713}
714
715GLint Context::getPackAlignment() const
716{
717 return mState.packAlignment;
718}
719
720void Context::setUnpackAlignment(GLint alignment)
721{
722 mState.unpackAlignment = alignment;
723}
724
725GLint Context::getUnpackAlignment() const
726{
727 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728}
729
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730GLuint Context::createBuffer()
731{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000732 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733}
734
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735GLuint Context::createProgram()
736{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000737 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738}
739
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000740GLuint Context::createShader(GLenum type)
741{
742 return mResourceManager->createShader(type);
743}
744
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745GLuint Context::createTexture()
746{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000747 return mResourceManager->createTexture();
748}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000750GLuint Context::createRenderbuffer()
751{
752 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753}
754
755// Returns an unused framebuffer name
756GLuint Context::createFramebuffer()
757{
758 unsigned int handle = 1;
759
760 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
761 {
762 handle++;
763 }
764
765 mFramebufferMap[handle] = NULL;
766
767 return handle;
768}
769
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770void Context::deleteBuffer(GLuint buffer)
771{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000772 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773 {
774 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000776
777 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
780void Context::deleteShader(GLuint shader)
781{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000782 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783}
784
785void Context::deleteProgram(GLuint program)
786{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000787 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
790void Context::deleteTexture(GLuint texture)
791{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000792 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793 {
794 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000796
797 mResourceManager->deleteTexture(texture);
798}
799
800void Context::deleteRenderbuffer(GLuint renderbuffer)
801{
802 if (mResourceManager->getRenderbuffer(renderbuffer))
803 {
804 detachRenderbuffer(renderbuffer);
805 }
806
807 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808}
809
810void Context::deleteFramebuffer(GLuint framebuffer)
811{
812 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
813
814 if (framebufferObject != mFramebufferMap.end())
815 {
816 detachFramebuffer(framebuffer);
817
818 delete framebufferObject->second;
819 mFramebufferMap.erase(framebufferObject);
820 }
821}
822
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000823Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000825 return mResourceManager->getBuffer(handle);
826}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000828Shader *Context::getShader(GLuint handle)
829{
830 return mResourceManager->getShader(handle);
831}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000833Program *Context::getProgram(GLuint handle)
834{
835 return mResourceManager->getProgram(handle);
836}
837
838Texture *Context::getTexture(GLuint handle)
839{
840 return mResourceManager->getTexture(handle);
841}
842
843Renderbuffer *Context::getRenderbuffer(GLuint handle)
844{
845 return mResourceManager->getRenderbuffer(handle);
846}
847
848Framebuffer *Context::getFramebuffer()
849{
850 return getFramebuffer(mState.framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853void Context::bindArrayBuffer(unsigned int buffer)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000857 mState.arrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860void Context::bindElementArrayBuffer(unsigned int buffer)
861{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000862 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000864 mState.elementArrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::bindTexture2D(GLuint texture)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000871 mState.texture2D = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000873 mState.samplerTexture[SAMPLER_2D][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
876void Context::bindTextureCubeMap(GLuint texture)
877{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000880 mState.textureCubeMap = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000882 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883}
884
885void Context::bindFramebuffer(GLuint framebuffer)
886{
887 if (!getFramebuffer(framebuffer))
888 {
daniel@transgaming.com0190f842010-07-28 19:21:02 +0000889 mFramebufferMap[framebuffer] = new Framebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890 }
891
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000892 mState.framebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893}
894
895void Context::bindRenderbuffer(GLuint renderbuffer)
896{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000897 mState.renderbuffer = renderbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
900void Context::useProgram(GLuint program)
901{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000902 GLuint priorProgram = mState.currentProgram;
903 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000904
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000905 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907 Program *newProgram = mResourceManager->getProgram(program);
908 Program *oldProgram = mResourceManager->getProgram(priorProgram);
909
910 if (newProgram)
911 {
912 newProgram->addRef();
913 }
914
915 if (oldProgram)
916 {
917 oldProgram->release();
918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920}
921
922void Context::setFramebufferZero(Framebuffer *buffer)
923{
924 delete mFramebufferMap[0];
925 mFramebufferMap[0] = buffer;
926}
927
928void Context::setColorbufferZero(Colorbuffer *buffer)
929{
930 delete mColorbufferZero;
931 mColorbufferZero = buffer;
932}
933
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000934void Context::setDepthStencilbufferZero(DepthStencilbuffer *buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000936 delete mDepthStencilbufferZero;
937 mDepthStencilbufferZero = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938}
939
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000940void Context::setRenderbuffer(Renderbuffer *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942 mResourceManager->deleteRenderbuffer(mState.renderbuffer);
943 mResourceManager->setRenderbuffer(mState.renderbuffer, renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
946Framebuffer *Context::getFramebuffer(unsigned int handle)
947{
948 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000949
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950 if (framebuffer == mFramebufferMap.end())
951 {
952 return NULL;
953 }
954 else
955 {
956 return framebuffer->second;
957 }
958}
959
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960Colorbuffer *Context::getColorbuffer(GLuint handle)
961{
962 if (handle != 0)
963 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964 Renderbuffer *renderbuffer = mResourceManager->getRenderbuffer(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000966 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967 {
968 return static_cast<Colorbuffer*>(renderbuffer);
969 }
970 }
971 else // Special case: 0 refers to different initial render targets based on the attachment type
972 {
973 return mColorbufferZero;
974 }
975
976 return NULL;
977}
978
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000979DepthStencilbuffer *Context::getDepthbuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980{
981 if (handle != 0)
982 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000983 Renderbuffer *renderbuffer = mResourceManager->getRenderbuffer(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000985 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000987 return static_cast<DepthStencilbuffer*>(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988 }
989 }
990 else // Special case: 0 refers to different initial render targets based on the attachment type
991 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000992 return mDepthStencilbufferZero;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993 }
994
995 return NULL;
996}
997
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000998DepthStencilbuffer *Context::getStencilbuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999{
1000 if (handle != 0)
1001 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001002 Renderbuffer *renderbuffer = mResourceManager->getRenderbuffer(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001004 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001006 return static_cast<DepthStencilbuffer*>(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007 }
1008 }
1009 else
1010 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001011 return mDepthStencilbufferZero;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012 }
1013
1014 return NULL;
1015}
1016
1017Buffer *Context::getArrayBuffer()
1018{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001019 return mResourceManager->getBuffer(mState.arrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020}
1021
1022Buffer *Context::getElementArrayBuffer()
1023{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001024 return mResourceManager->getBuffer(mState.elementArrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
1027Program *Context::getCurrentProgram()
1028{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001029 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
1032Texture2D *Context::getTexture2D()
1033{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001034 if (mState.texture2D == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035 {
1036 return mTexture2DZero;
1037 }
1038
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001039 return (Texture2D*)mResourceManager->getTexture(mState.texture2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040}
1041
1042TextureCubeMap *Context::getTextureCubeMap()
1043{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001044 if (mState.textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045 {
1046 return mTextureCubeMapZero;
1047 }
1048
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001049 return (TextureCubeMap*)mResourceManager->getTexture(mState.textureCubeMap);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050}
1051
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001052Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001054 GLuint texid = mState.samplerTexture[type][sampler];
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001055
1056 if (texid == 0)
1057 {
1058 switch (type)
1059 {
1060 default: UNREACHABLE();
1061 case SAMPLER_2D: return mTexture2DZero;
1062 case SAMPLER_CUBE: return mTextureCubeMapZero;
1063 }
1064 }
1065
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001066 return mResourceManager->getTexture(texid);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001069bool Context::getBooleanv(GLenum pname, GLboolean *params)
1070{
1071 switch (pname)
1072 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001073 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1074 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1075 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001076 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001077 params[0] = mState.colorMaskRed;
1078 params[1] = mState.colorMaskGreen;
1079 params[2] = mState.colorMaskBlue;
1080 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001081 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001082 case GL_CULL_FACE: *params = mState.cullFace;
1083 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1084 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1085 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1086 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1087 case GL_STENCIL_TEST: *params = mState.stencilTest;
1088 case GL_DEPTH_TEST: *params = mState.depthTest;
1089 case GL_BLEND: *params = mState.blend;
1090 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001091 default:
1092 return false;
1093 }
1094
1095 return true;
1096}
1097
1098bool Context::getFloatv(GLenum pname, GLfloat *params)
1099{
1100 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1101 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1102 // GetIntegerv as its native query function. As it would require conversion in any
1103 // case, this should make no difference to the calling application.
1104 switch (pname)
1105 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001106 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1107 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1108 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1109 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1110 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001111 case GL_ALIASED_LINE_WIDTH_RANGE:
1112 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1113 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1114 break;
1115 case GL_ALIASED_POINT_SIZE_RANGE:
1116 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001117 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001118 break;
1119 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001120 params[0] = mState.zNear;
1121 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001122 break;
1123 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001124 params[0] = mState.colorClearValue.red;
1125 params[1] = mState.colorClearValue.green;
1126 params[2] = mState.colorClearValue.blue;
1127 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001129 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001130 params[0] = mState.blendColor.red;
1131 params[1] = mState.blendColor.green;
1132 params[2] = mState.blendColor.blue;
1133 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001134 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001135 default:
1136 return false;
1137 }
1138
1139 return true;
1140}
1141
1142bool Context::getIntegerv(GLenum pname, GLint *params)
1143{
1144 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1145 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1146 // GetIntegerv as its native query function. As it would require conversion in any
1147 // case, this should make no difference to the calling application. You may find it in
1148 // Context::getFloatv.
1149 switch (pname)
1150 {
1151 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1152 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1153 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1154 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1155 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1156 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1157 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1158 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001159 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1160 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1161 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1162 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1163 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer; break;
1164 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer; break;
1165 case GL_FRAMEBUFFER_BINDING: *params = mState.framebuffer; break;
1166 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer; break;
1167 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1168 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1169 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1170 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1171 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1172 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1173 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1174 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1175 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1176 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1177 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1178 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1179 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1180 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1181 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1182 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1183 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1184 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1185 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1186 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1187 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1188 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1189 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1190 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1191 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1192 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1193 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1194 case GL_SUBPIXEL_BITS: *params = 4; break;
1195 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1196 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
1197 case GL_SAMPLE_BUFFERS: *params = 0; break;
1198 case GL_SAMPLES: *params = 0; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001199 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1200 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1201 case GL_MAX_VIEWPORT_DIMS:
1202 {
1203 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1204 params[0] = maxDimension;
1205 params[1] = maxDimension;
1206 }
1207 break;
1208 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001209 params[0] = mState.viewportX;
1210 params[1] = mState.viewportY;
1211 params[2] = mState.viewportWidth;
1212 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001213 break;
1214 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001215 params[0] = mState.scissorX;
1216 params[1] = mState.scissorY;
1217 params[2] = mState.scissorWidth;
1218 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001219 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001220 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1221 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001222 case GL_RED_BITS:
1223 case GL_GREEN_BITS:
1224 case GL_BLUE_BITS:
1225 case GL_ALPHA_BITS:
1226 {
1227 gl::Framebuffer *framebuffer = getFramebuffer();
1228 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1229
1230 if (colorbuffer)
1231 {
1232 switch (pname)
1233 {
1234 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1235 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1236 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1237 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1238 }
1239 }
1240 else
1241 {
1242 *params = 0;
1243 }
1244 }
1245 break;
1246 case GL_DEPTH_BITS:
1247 {
1248 gl::Framebuffer *framebuffer = getFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001249 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001250
1251 if (depthbuffer)
1252 {
1253 *params = depthbuffer->getDepthSize();
1254 }
1255 else
1256 {
1257 *params = 0;
1258 }
1259 }
1260 break;
1261 case GL_STENCIL_BITS:
1262 {
1263 gl::Framebuffer *framebuffer = getFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001264 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001265
1266 if (stencilbuffer)
1267 {
1268 *params = stencilbuffer->getStencilSize();
1269 }
1270 else
1271 {
1272 *params = 0;
1273 }
1274 }
1275 break;
1276 case GL_TEXTURE_BINDING_2D:
1277 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001278 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001279 {
1280 error(GL_INVALID_OPERATION);
1281 return false;
1282 }
1283
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001284 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001285 }
1286 break;
1287 case GL_TEXTURE_BINDING_CUBE_MAP:
1288 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001289 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001290 {
1291 error(GL_INVALID_OPERATION);
1292 return false;
1293 }
1294
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001295 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001296 }
1297 break;
1298 default:
1299 return false;
1300 }
1301
1302 return true;
1303}
1304
1305bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1306{
1307 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1308 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1309 // to the fact that it is stored internally as a float, and so would require conversion
1310 // if returned from Context::getIntegerv. Since this conversion is already implemented
1311 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1312 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1313 // application.
1314 switch (pname)
1315 {
1316 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1317 case GL_SHADER_BINARY_FORMATS:
1318 {
1319 *type = GL_INT;
1320 *numParams = 0;
1321 }
1322 break;
1323 case GL_MAX_VERTEX_ATTRIBS:
1324 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1325 case GL_MAX_VARYING_VECTORS:
1326 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1327 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1328 case GL_MAX_TEXTURE_IMAGE_UNITS:
1329 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1330 case GL_MAX_RENDERBUFFER_SIZE:
1331 case GL_NUM_SHADER_BINARY_FORMATS:
1332 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1333 case GL_ARRAY_BUFFER_BINDING:
1334 case GL_FRAMEBUFFER_BINDING:
1335 case GL_RENDERBUFFER_BINDING:
1336 case GL_CURRENT_PROGRAM:
1337 case GL_PACK_ALIGNMENT:
1338 case GL_UNPACK_ALIGNMENT:
1339 case GL_GENERATE_MIPMAP_HINT:
1340 case GL_RED_BITS:
1341 case GL_GREEN_BITS:
1342 case GL_BLUE_BITS:
1343 case GL_ALPHA_BITS:
1344 case GL_DEPTH_BITS:
1345 case GL_STENCIL_BITS:
1346 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1347 case GL_CULL_FACE_MODE:
1348 case GL_FRONT_FACE:
1349 case GL_ACTIVE_TEXTURE:
1350 case GL_STENCIL_FUNC:
1351 case GL_STENCIL_VALUE_MASK:
1352 case GL_STENCIL_REF:
1353 case GL_STENCIL_FAIL:
1354 case GL_STENCIL_PASS_DEPTH_FAIL:
1355 case GL_STENCIL_PASS_DEPTH_PASS:
1356 case GL_STENCIL_BACK_FUNC:
1357 case GL_STENCIL_BACK_VALUE_MASK:
1358 case GL_STENCIL_BACK_REF:
1359 case GL_STENCIL_BACK_FAIL:
1360 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1361 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1362 case GL_DEPTH_FUNC:
1363 case GL_BLEND_SRC_RGB:
1364 case GL_BLEND_SRC_ALPHA:
1365 case GL_BLEND_DST_RGB:
1366 case GL_BLEND_DST_ALPHA:
1367 case GL_BLEND_EQUATION_RGB:
1368 case GL_BLEND_EQUATION_ALPHA:
1369 case GL_STENCIL_WRITEMASK:
1370 case GL_STENCIL_BACK_WRITEMASK:
1371 case GL_STENCIL_CLEAR_VALUE:
1372 case GL_SUBPIXEL_BITS:
1373 case GL_MAX_TEXTURE_SIZE:
1374 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1375 case GL_SAMPLE_BUFFERS:
1376 case GL_SAMPLES:
1377 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1378 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1379 case GL_TEXTURE_BINDING_2D:
1380 case GL_TEXTURE_BINDING_CUBE_MAP:
1381 {
1382 *type = GL_INT;
1383 *numParams = 1;
1384 }
1385 break;
1386 case GL_MAX_VIEWPORT_DIMS:
1387 {
1388 *type = GL_INT;
1389 *numParams = 2;
1390 }
1391 break;
1392 case GL_VIEWPORT:
1393 case GL_SCISSOR_BOX:
1394 {
1395 *type = GL_INT;
1396 *numParams = 4;
1397 }
1398 break;
1399 case GL_SHADER_COMPILER:
1400 case GL_SAMPLE_COVERAGE_INVERT:
1401 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001402 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1403 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1404 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1405 case GL_SAMPLE_COVERAGE:
1406 case GL_SCISSOR_TEST:
1407 case GL_STENCIL_TEST:
1408 case GL_DEPTH_TEST:
1409 case GL_BLEND:
1410 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001411 {
1412 *type = GL_BOOL;
1413 *numParams = 1;
1414 }
1415 break;
1416 case GL_COLOR_WRITEMASK:
1417 {
1418 *type = GL_BOOL;
1419 *numParams = 4;
1420 }
1421 break;
1422 case GL_POLYGON_OFFSET_FACTOR:
1423 case GL_POLYGON_OFFSET_UNITS:
1424 case GL_SAMPLE_COVERAGE_VALUE:
1425 case GL_DEPTH_CLEAR_VALUE:
1426 case GL_LINE_WIDTH:
1427 {
1428 *type = GL_FLOAT;
1429 *numParams = 1;
1430 }
1431 break;
1432 case GL_ALIASED_LINE_WIDTH_RANGE:
1433 case GL_ALIASED_POINT_SIZE_RANGE:
1434 case GL_DEPTH_RANGE:
1435 {
1436 *type = GL_FLOAT;
1437 *numParams = 2;
1438 }
1439 break;
1440 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001441 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001442 {
1443 *type = GL_FLOAT;
1444 *numParams = 4;
1445 }
1446 break;
1447 default:
1448 return false;
1449 }
1450
1451 return true;
1452}
1453
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001454// Applies the render target surface, depth stencil surface, viewport rectangle and
1455// scissor rectangle to the Direct3D 9 device
1456bool Context::applyRenderTarget(bool ignoreViewport)
1457{
1458 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001460 Framebuffer *framebufferObject = getFramebuffer();
1461
1462 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1463 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001464 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1465
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001466 return false;
1467 }
1468
1469 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001470 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001471
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001472 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1473 if (renderTargetSerial != mAppliedRenderTargetSerial)
1474 {
1475 device->SetRenderTarget(0, renderTarget);
1476 mAppliedRenderTargetSerial = renderTargetSerial;
1477 }
1478
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001479 unsigned int depthbufferSerial = 0;
1480 unsigned int stencilbufferSerial = 0;
1481 if (framebufferObject->getDepthbufferType() != GL_NONE)
1482 {
1483 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1484 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1485 }
1486 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1487 {
1488 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1489 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1490 }
1491
1492 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1493 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001494 {
1495 device->SetDepthStencilSurface(depthStencil);
1496 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001497 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499
1500 D3DVIEWPORT9 viewport;
1501 D3DSURFACE_DESC desc;
1502 renderTarget->GetDesc(&desc);
1503
1504 if (ignoreViewport)
1505 {
1506 viewport.X = 0;
1507 viewport.Y = 0;
1508 viewport.Width = desc.Width;
1509 viewport.Height = desc.Height;
1510 viewport.MinZ = 0.0f;
1511 viewport.MaxZ = 1.0f;
1512 }
1513 else
1514 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001515 viewport.X = std::max(mState.viewportX, 0);
1516 viewport.Y = std::max(mState.viewportY, 0);
1517 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1518 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1519 viewport.MinZ = clamp01(mState.zNear);
1520 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001521 }
1522
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001523 if (viewport.Width <= 0 || viewport.Height <= 0)
1524 {
1525 return false; // Nothing to render
1526 }
1527
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001528 device->SetViewport(&viewport);
1529
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001530 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001532 if (mState.scissorTest)
1533 {
1534 RECT rect = {mState.scissorX,
1535 mState.scissorY,
1536 mState.scissorX + mState.scissorWidth,
1537 mState.scissorY + mState.scissorHeight};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001539 device->SetScissorRect(&rect);
1540 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1541 }
1542 else
1543 {
1544 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1545 }
1546
1547 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001548 }
1549
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001550 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001551 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552 Program *programObject = getCurrentProgram();
1553
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001554 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001555 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001556 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001557
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001558 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001559 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1560 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1561 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001562 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1563
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001564 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001565 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001566 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1567
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001568 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001569 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001570
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001571 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001572 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001573
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001574 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001575 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001576 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 }
1578
1579 return true;
1580}
1581
1582// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001583void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584{
1585 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001586 Program *programObject = getCurrentProgram();
1587
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001588 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001589 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001590 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001592 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001593 GLint alwaysFront = !isTriangleMode(drawMode);
1594 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1595
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001596 Framebuffer *framebufferObject = getFramebuffer();
1597
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001598 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001600 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001601 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001602 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603 }
1604 else
1605 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001606 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001607 }
1608
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001609 mCullStateDirty = false;
1610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001612 if (mDepthStateDirty)
1613 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001614 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001616 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1617 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 }
1619 else
1620 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001621 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001623
1624 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625 }
1626
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001627 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001629 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001630 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001631 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1632
1633 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1634 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1635 {
1636 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1637 }
1638 else
1639 {
1640 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1641 unorm<8>(mState.blendColor.alpha),
1642 unorm<8>(mState.blendColor.alpha),
1643 unorm<8>(mState.blendColor.alpha)));
1644 }
1645
1646 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1647 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1648 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1649
1650 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1651 mState.destBlendRGB != mState.destBlendAlpha ||
1652 mState.blendEquationRGB != mState.blendEquationAlpha)
1653 {
1654 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1655
1656 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1657 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1658 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1659
1660 }
1661 else
1662 {
1663 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1664 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001665 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001666 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001667 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001669 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001670
1671 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 }
1673
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001674 if (mStencilStateDirty || mFrontFaceDirty)
1675 {
1676 if (mState.stencilTest && hasStencil())
1677 {
1678 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1679 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1680
1681 // FIXME: Unsupported by D3D9
1682 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1683 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1684 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1685 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1686 mState.stencilRef != mState.stencilBackRef ||
1687 mState.stencilMask != mState.stencilBackMask)
1688 {
1689 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1690 return error(GL_INVALID_OPERATION);
1691 }
1692
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001693 // get the maximum size of the stencil ref
1694 gl::Framebuffer *framebuffer = getFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001695 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001696 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1697
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001698 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1699 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1700 es2dx::ConvertComparison(mState.stencilFunc));
1701
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001702 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001703 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1704
1705 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1706 es2dx::ConvertStencilOp(mState.stencilFail));
1707 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1708 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1709 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1710 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1711
1712 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1713 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1714 es2dx::ConvertComparison(mState.stencilBackFunc));
1715
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001716 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001717 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1718
1719 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1720 es2dx::ConvertStencilOp(mState.stencilBackFail));
1721 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1722 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1723 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1724 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1725 }
1726 else
1727 {
1728 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1729 }
1730
1731 mStencilStateDirty = false;
1732 }
1733
1734 if (mMaskStateDirty)
1735 {
1736 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1737 mState.colorMaskBlue, mState.colorMaskAlpha));
1738 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1739
1740 mMaskStateDirty = false;
1741 }
1742
1743 if (mPolygonOffsetStateDirty)
1744 {
1745 if (mState.polygonOffsetFill)
1746 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001747 gl::DepthStencilbuffer *depthbuffer = getFramebuffer()->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 if (depthbuffer)
1749 {
1750 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1751 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1752 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1753 }
1754 }
1755 else
1756 {
1757 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1758 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1759 }
1760
1761 mPolygonOffsetStateDirty = false;
1762 }
1763
1764 if (mConfig->mMultiSample != 0 && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001766 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001767 {
1768 FIXME("Sample alpha to coverage is unimplemented.");
1769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001771 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001772 {
1773 FIXME("Sample coverage is unimplemented.");
1774 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001775
1776 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777 }
1778
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001779 if (mDitherStateDirty)
1780 {
1781 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1782
1783 mDitherStateDirty = false;
1784 }
1785
1786 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787}
1788
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001789// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001790void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001792 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001794 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001796 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797 }
1798 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001799}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001801GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001802{
1803 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1804
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001805 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001806 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001807 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001808 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001809 }
1810
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001811 lookupAttributeMapping(translated);
1812
1813 mBufferBackEnd->setupAttributesPreDraw(translated);
1814
1815 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1816 {
1817 if (translated[i].enabled && translated[i].nonArray)
1818 {
1819 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1820 if (err != GL_NO_ERROR)
1821 {
1822 return err;
1823 }
1824
1825 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1826
1827 *useIndexing = true;
1828 return GL_NO_ERROR;
1829 }
1830 }
1831
1832 *useIndexing = false;
1833 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001834}
1835
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001836GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001837{
1838 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1839
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001840 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001841
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001842 if (err == GL_NO_ERROR)
1843 {
1844 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001845
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001846 mBufferBackEnd->setupAttributesPreDraw(translated);
1847 }
1848
1849 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001850}
1851
1852// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001853GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001855 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mResourceManager->getBuffer(mState.elementArrayBuffer), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001856
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001857 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001858 {
1859 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1860 }
1861
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001862 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863}
1864
1865// Applies the shaders and shader constants to the Direct3D 9 device
1866void Context::applyShaders()
1867{
1868 IDirect3DDevice9 *device = getDevice();
1869 Program *programObject = getCurrentProgram();
1870 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1871 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1872
1873 device->SetVertexShader(vertexShader);
1874 device->SetPixelShader(pixelShader);
1875
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001876 if (programObject->getSerial() != mAppliedProgram)
1877 {
1878 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001879 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001880 mAppliedProgram = programObject->getSerial();
1881 }
1882
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 programObject->applyUniforms();
1884}
1885
1886// Applies the textures and sampler states to the Direct3D 9 device
1887void Context::applyTextures()
1888{
1889 IDirect3DDevice9 *device = getDevice();
1890 Program *programObject = getCurrentProgram();
1891
1892 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1893 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001894 int textureUnit = programObject->getSamplerMapping(sampler);
1895 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001897 SamplerType textureType = programObject->getSamplerType(sampler);
1898
1899 Texture *texture = getSamplerTexture(textureUnit, textureType);
1900
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001901 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001902 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001903 if (texture->isComplete())
1904 {
1905 GLenum wrapS = texture->getWrapS();
1906 GLenum wrapT = texture->getWrapT();
1907 GLenum minFilter = texture->getMinFilter();
1908 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001910 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1911 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001913 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1914 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1915 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1916 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1917 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001919 device->SetTexture(sampler, texture->getTexture());
1920 }
1921 else
1922 {
1923 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1924 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001925 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001926
1927 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001929 else
1930 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001931 if (programObject->isSamplerDirty(sampler))
1932 {
1933 device->SetTexture(sampler, NULL);
1934 programObject->setSamplerDirty(sampler, false);
1935 }
1936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 }
1938}
1939
1940void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1941{
1942 Framebuffer *framebuffer = getFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00001943
1944 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1945 {
1946 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1947 }
1948
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1950 IDirect3DDevice9 *device = getDevice();
1951
1952 D3DSURFACE_DESC desc;
1953 renderTarget->GetDesc(&desc);
1954
1955 IDirect3DSurface9 *systemSurface;
1956 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1957
1958 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1959 {
1960 return error(GL_OUT_OF_MEMORY);
1961 }
1962
1963 ASSERT(SUCCEEDED(result));
1964
1965 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1966 {
1967 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1968 }
1969
1970 result = device->GetRenderTargetData(renderTarget, systemSurface);
1971
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 if (FAILED(result))
1973 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 systemSurface->Release();
1975
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00001976 switch (result)
1977 {
1978 case D3DERR_DRIVERINTERNALERROR:
1979 case D3DERR_DEVICELOST:
1980 return error(GL_OUT_OF_MEMORY);
1981 default:
1982 UNREACHABLE();
1983 return; // No sensible error to generate
1984 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985 }
1986
1987 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001988 RECT rect = {std::max(x, 0),
1989 std::max(y, 0),
1990 std::min(x + width, (int)desc.Width),
1991 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
1993 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1994
1995 if (FAILED(result))
1996 {
1997 UNREACHABLE();
1998 systemSurface->Release();
1999
2000 return; // No sensible error to generate
2001 }
2002
2003 unsigned char *source = (unsigned char*)lock.pBits;
2004 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002005 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002007 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 for (int j = 0; j < rect.bottom - rect.top; j++)
2010 {
2011 for (int i = 0; i < rect.right - rect.left; i++)
2012 {
2013 float r;
2014 float g;
2015 float b;
2016 float a;
2017
2018 switch (desc.Format)
2019 {
2020 case D3DFMT_R5G6B5:
2021 {
2022 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2023
2024 a = 1.0f;
2025 b = (rgb & 0x001F) * (1.0f / 0x001F);
2026 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2027 r = (rgb & 0xF800) * (1.0f / 0xF800);
2028 }
2029 break;
2030 case D3DFMT_X1R5G5B5:
2031 {
2032 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2033
2034 a = 1.0f;
2035 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2036 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2037 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2038 }
2039 break;
2040 case D3DFMT_A1R5G5B5:
2041 {
2042 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2043
2044 a = (argb & 0x8000) ? 1.0f : 0.0f;
2045 b = (argb & 0x001F) * (1.0f / 0x001F);
2046 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2047 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2048 }
2049 break;
2050 case D3DFMT_A8R8G8B8:
2051 {
2052 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2053
2054 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2055 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2056 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2057 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2058 }
2059 break;
2060 case D3DFMT_X8R8G8B8:
2061 {
2062 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2063
2064 a = 1.0f;
2065 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2066 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2067 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2068 }
2069 break;
2070 case D3DFMT_A2R10G10B10:
2071 {
2072 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2073
2074 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2075 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2076 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2077 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2078 }
2079 break;
2080 default:
2081 UNIMPLEMENTED(); // FIXME
2082 UNREACHABLE();
2083 }
2084
2085 switch (format)
2086 {
2087 case GL_RGBA:
2088 switch (type)
2089 {
2090 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002091 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2092 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2093 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2094 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 break;
2096 default: UNREACHABLE();
2097 }
2098 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002099 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 switch (type)
2101 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002102 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002103 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2104 ((unsigned short)(31 * b + 0.5f) << 0) |
2105 ((unsigned short)(63 * g + 0.5f) << 5) |
2106 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 break;
2108 default: UNREACHABLE();
2109 }
2110 break;
2111 default: UNREACHABLE();
2112 }
2113 }
2114 }
2115
2116 systemSurface->UnlockRect();
2117
2118 systemSurface->Release();
2119}
2120
2121void Context::clear(GLbitfield mask)
2122{
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002123 Framebuffer *framebufferObject = getFramebuffer();
2124
2125 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2126 {
2127 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2128
2129 return;
2130 }
2131
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002132 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 IDirect3DDevice9 *device = getDevice();
2134 DWORD flags = 0;
2135
2136 if (mask & GL_COLOR_BUFFER_BIT)
2137 {
2138 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002139
2140 if (framebufferObject->getColorbufferType() != GL_NONE)
2141 {
2142 flags |= D3DCLEAR_TARGET;
2143 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 }
2145
2146 if (mask & GL_DEPTH_BUFFER_BIT)
2147 {
2148 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002149 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 {
2151 flags |= D3DCLEAR_ZBUFFER;
2152 }
2153 }
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 GLuint stencilUnmasked = 0x0;
2156
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002157 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002160 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002162 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2163 D3DSURFACE_DESC desc;
2164 depthStencil->GetDesc(&desc);
2165
2166 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2167 stencilUnmasked = (0x1 << stencilSize) - 1;
2168
2169 if (stencilUnmasked != 0x0)
2170 {
2171 flags |= D3DCLEAR_STENCIL;
2172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 }
2174 }
2175
2176 if (mask != 0)
2177 {
2178 return error(GL_INVALID_VALUE);
2179 }
2180
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002181 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2182 {
2183 return;
2184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002186 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2187 unorm<8>(mState.colorClearValue.red),
2188 unorm<8>(mState.colorClearValue.green),
2189 unorm<8>(mState.colorClearValue.blue));
2190 float depth = clamp01(mState.depthClearValue);
2191 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192
2193 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2194
2195 D3DSURFACE_DESC desc;
2196 renderTarget->GetDesc(&desc);
2197
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002198 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199
2200 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002201 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002202 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002203 !(mState.colorMaskRed && mState.colorMaskGreen &&
2204 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205
2206 if (needMaskedColorClear || needMaskedStencilClear)
2207 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002208 // State which is altered in all paths from this point to the clear call is saved.
2209 // State which is altered in only some paths will be flagged dirty in the case that
2210 // that path is taken.
2211 HRESULT hr;
2212 if (mMaskedClearSavedState == NULL)
2213 {
2214 hr = device->BeginStateBlock();
2215 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2216
2217 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2218 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2219 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2220 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2221 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2222 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2223 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2224 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2225 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2226 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2227 device->SetPixelShader(NULL);
2228 device->SetVertexShader(NULL);
2229 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2230 device->SetStreamSourceFreq(0, 1);
2231
2232 hr = device->EndStateBlock(&mMaskedClearSavedState);
2233 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2234 }
2235
2236 ASSERT(mMaskedClearSavedState != NULL);
2237
2238 if (mMaskedClearSavedState != NULL)
2239 {
2240 hr = mMaskedClearSavedState->Capture();
2241 ASSERT(SUCCEEDED(hr));
2242 }
2243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2245 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2246 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2247 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2248 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2249 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2250 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2251 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2252
2253 if (flags & D3DCLEAR_TARGET)
2254 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002255 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2256 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2257 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2258 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 }
2260 else
2261 {
2262 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2263 }
2264
2265 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2266 {
2267 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2268 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2269 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2270 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002271 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002272 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2274 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002275 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 }
2277 else
2278 {
2279 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2280 }
2281
2282 device->SetPixelShader(NULL);
2283 device->SetVertexShader(NULL);
2284 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002285 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002287 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 {
2289 float x, y, z, w;
2290 D3DCOLOR diffuse;
2291 };
2292
2293 Vertex quad[4];
2294 quad[0].x = 0.0f;
2295 quad[0].y = (float)desc.Height;
2296 quad[0].z = 0.0f;
2297 quad[0].w = 1.0f;
2298 quad[0].diffuse = color;
2299
2300 quad[1].x = (float)desc.Width;
2301 quad[1].y = (float)desc.Height;
2302 quad[1].z = 0.0f;
2303 quad[1].w = 1.0f;
2304 quad[1].diffuse = color;
2305
2306 quad[2].x = 0.0f;
2307 quad[2].y = 0.0f;
2308 quad[2].z = 0.0f;
2309 quad[2].w = 1.0f;
2310 quad[2].diffuse = color;
2311
2312 quad[3].x = (float)desc.Width;
2313 quad[3].y = 0.0f;
2314 quad[3].z = 0.0f;
2315 quad[3].w = 1.0f;
2316 quad[3].diffuse = color;
2317
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002318 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320
2321 if (flags & D3DCLEAR_ZBUFFER)
2322 {
2323 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2324 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2325 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2326 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002327
2328 if (mMaskedClearSavedState != NULL)
2329 {
2330 mMaskedClearSavedState->Apply();
2331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002333 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334 {
2335 device->Clear(0, NULL, flags, color, depth, stencil);
2336 }
2337}
2338
2339void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2340{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002341 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342 {
2343 return error(GL_INVALID_OPERATION);
2344 }
2345
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002346 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 IDirect3DDevice9 *device = getDevice();
2348 D3DPRIMITIVETYPE primitiveType;
2349 int primitiveCount;
2350
2351 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2352 return error(GL_INVALID_ENUM);
2353
2354 if (primitiveCount <= 0)
2355 {
2356 return;
2357 }
2358
2359 if (!applyRenderTarget(false))
2360 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002361 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 }
2363
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002364 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002365
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002366 TranslatedIndexData indexInfo;
2367 bool useIndexing;
2368 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002369 if (err != GL_NO_ERROR)
2370 {
2371 return error(err);
2372 }
2373
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 applyShaders();
2375 applyTextures();
2376
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002377 if (!getCurrentProgram()->validateSamplers())
2378 {
2379 return error(GL_INVALID_OPERATION);
2380 }
2381
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002382 if (!cullSkipsDraw(mode))
2383 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002384 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002385 if (useIndexing)
2386 {
2387 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2388 }
2389 else
2390 {
2391 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2392 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002393 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394}
2395
2396void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2397{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002398 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399 {
2400 return error(GL_INVALID_OPERATION);
2401 }
2402
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002403 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 {
2405 return error(GL_INVALID_OPERATION);
2406 }
2407
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002408 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 IDirect3DDevice9 *device = getDevice();
2410 D3DPRIMITIVETYPE primitiveType;
2411 int primitiveCount;
2412
2413 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2414 return error(GL_INVALID_ENUM);
2415
2416 if (primitiveCount <= 0)
2417 {
2418 return;
2419 }
2420
2421 if (!applyRenderTarget(false))
2422 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002423 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424 }
2425
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002426 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002427
2428 TranslatedIndexData indexInfo;
2429 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2430 if (err != GL_NO_ERROR)
2431 {
2432 return error(err);
2433 }
2434
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002435 err = applyVertexBuffer(indexInfo);
2436 if (err != GL_NO_ERROR)
2437 {
2438 return error(err);
2439 }
2440
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 applyShaders();
2442 applyTextures();
2443
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002444 if (!getCurrentProgram()->validateSamplers())
2445 {
2446 return error(GL_INVALID_OPERATION);
2447 }
2448
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002449 if (!cullSkipsDraw(mode))
2450 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002451 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002452 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454}
2455
2456void Context::finish()
2457{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002458 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 IDirect3DDevice9 *device = getDevice();
2460 IDirect3DQuery9 *occlusionQuery = NULL;
2461
2462 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2463
2464 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2465 {
2466 return error(GL_OUT_OF_MEMORY);
2467 }
2468
2469 ASSERT(SUCCEEDED(result));
2470
2471 if (occlusionQuery)
2472 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002473 IDirect3DStateBlock9 *savedState = NULL;
2474 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2475
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 occlusionQuery->Issue(D3DISSUE_BEGIN);
2477
2478 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002479 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 device->SetPixelShader(NULL);
2481 device->SetVertexShader(NULL);
2482 device->SetFVF(D3DFVF_XYZRHW);
2483 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002484 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486
2487 occlusionQuery->Issue(D3DISSUE_END);
2488
2489 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2490 {
2491 // Keep polling, but allow other threads to do something useful first
2492 Sleep(0);
2493 }
2494
2495 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002496
2497 if (savedState)
2498 {
2499 savedState->Apply();
2500 savedState->Release();
2501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502 }
2503}
2504
2505void Context::flush()
2506{
2507 IDirect3DDevice9 *device = getDevice();
2508 IDirect3DQuery9 *eventQuery = NULL;
2509
2510 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2511
2512 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2513 {
2514 return error(GL_OUT_OF_MEMORY);
2515 }
2516
2517 ASSERT(SUCCEEDED(result));
2518
2519 if (eventQuery)
2520 {
2521 eventQuery->Issue(D3DISSUE_END);
2522
2523 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2524 {
2525 // Keep polling, but allow other threads to do something useful first
2526 Sleep(0);
2527 }
2528
2529 eventQuery->Release();
2530 }
2531}
2532
2533void Context::recordInvalidEnum()
2534{
2535 mInvalidEnum = true;
2536}
2537
2538void Context::recordInvalidValue()
2539{
2540 mInvalidValue = true;
2541}
2542
2543void Context::recordInvalidOperation()
2544{
2545 mInvalidOperation = true;
2546}
2547
2548void Context::recordOutOfMemory()
2549{
2550 mOutOfMemory = true;
2551}
2552
2553void Context::recordInvalidFramebufferOperation()
2554{
2555 mInvalidFramebufferOperation = true;
2556}
2557
2558// Get one of the recorded errors and clear its flag, if any.
2559// [OpenGL ES 2.0.24] section 2.5 page 13.
2560GLenum Context::getError()
2561{
2562 if (mInvalidEnum)
2563 {
2564 mInvalidEnum = false;
2565
2566 return GL_INVALID_ENUM;
2567 }
2568
2569 if (mInvalidValue)
2570 {
2571 mInvalidValue = false;
2572
2573 return GL_INVALID_VALUE;
2574 }
2575
2576 if (mInvalidOperation)
2577 {
2578 mInvalidOperation = false;
2579
2580 return GL_INVALID_OPERATION;
2581 }
2582
2583 if (mOutOfMemory)
2584 {
2585 mOutOfMemory = false;
2586
2587 return GL_OUT_OF_MEMORY;
2588 }
2589
2590 if (mInvalidFramebufferOperation)
2591 {
2592 mInvalidFramebufferOperation = false;
2593
2594 return GL_INVALID_FRAMEBUFFER_OPERATION;
2595 }
2596
2597 return GL_NO_ERROR;
2598}
2599
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002600bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002601{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002602 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002603}
2604
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605void Context::detachBuffer(GLuint buffer)
2606{
2607 // [OpenGL ES 2.0.24] section 2.9 page 22:
2608 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2609 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2610
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002611 if (mState.arrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002613 mState.arrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 }
2615
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002616 if (mState.elementArrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002618 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 }
2620
2621 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2622 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002623 if (mState.vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002625 mState.vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626 }
2627 }
2628}
2629
2630void Context::detachTexture(GLuint texture)
2631{
2632 // [OpenGL ES 2.0.24] section 3.8 page 84:
2633 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2634 // rebound to texture object zero
2635
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002636 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002638 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002640 if (mState.samplerTexture[type][sampler] == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002641 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002642 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644 }
2645 }
2646
2647 // [OpenGL ES 2.0.24] section 4.4 page 112:
2648 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2649 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2650 // image was attached in the currently bound framebuffer.
2651
2652 Framebuffer *framebuffer = getFramebuffer();
2653
2654 if (framebuffer)
2655 {
2656 framebuffer->detachTexture(texture);
2657 }
2658}
2659
2660void Context::detachFramebuffer(GLuint framebuffer)
2661{
2662 // [OpenGL ES 2.0.24] section 4.4 page 107:
2663 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2664 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2665
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002666 if (mState.framebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 {
2668 bindFramebuffer(0);
2669 }
2670}
2671
2672void Context::detachRenderbuffer(GLuint renderbuffer)
2673{
2674 // [OpenGL ES 2.0.24] section 4.4 page 109:
2675 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2676 // had been executed with the target RENDERBUFFER and name of zero.
2677
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002678 if (mState.renderbuffer == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 {
2680 bindRenderbuffer(0);
2681 }
2682
2683 // [OpenGL ES 2.0.24] section 4.4 page 111:
2684 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2685 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2686 // point to which this image was attached in the currently bound framebuffer.
2687
2688 Framebuffer *framebuffer = getFramebuffer();
2689
2690 if (framebuffer)
2691 {
2692 framebuffer->detachRenderbuffer(renderbuffer);
2693 }
2694}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002695
2696Texture *Context::getIncompleteTexture(SamplerType type)
2697{
2698 Texture *t = mIncompleteTextures[type];
2699
2700 if (t == NULL)
2701 {
2702 static const GLubyte color[] = { 0, 0, 0, 255 };
2703
2704 switch (type)
2705 {
2706 default:
2707 UNREACHABLE();
2708 // default falls through to SAMPLER_2D
2709
2710 case SAMPLER_2D:
2711 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00002712 Texture2D *incomplete2d = new Texture2D();
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002713 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002714 t = incomplete2d;
2715 }
2716 break;
2717
2718 case SAMPLER_CUBE:
2719 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00002720 TextureCubeMap *incompleteCube = new TextureCubeMap();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002721
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002722 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2723 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2724 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2725 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2726 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2727 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002728
2729 t = incompleteCube;
2730 }
2731 break;
2732 }
2733
2734 mIncompleteTextures[type] = t;
2735 }
2736
2737 return t;
2738}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002739
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002740bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002741{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002742 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002743}
2744
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002745bool Context::isTriangleMode(GLenum drawMode)
2746{
2747 switch (drawMode)
2748 {
2749 case GL_TRIANGLES:
2750 case GL_TRIANGLE_FAN:
2751 case GL_TRIANGLE_STRIP:
2752 return true;
2753 case GL_POINTS:
2754 case GL_LINES:
2755 case GL_LINE_LOOP:
2756 case GL_LINE_STRIP:
2757 return false;
2758 default: UNREACHABLE();
2759 }
2760
2761 return false;
2762}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002763
daniel@transgaming.com5bd0ce32010-05-07 13:03:31 +00002764bool Context::hasStencil()
2765{
2766 Framebuffer *framebufferObject = getFramebuffer();
2767
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002768 if (framebufferObject && framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com5bd0ce32010-05-07 13:03:31 +00002769 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002770 DepthStencilbuffer *stencilbufferObject = framebufferObject->getStencilbuffer();
daniel@transgaming.com5bd0ce32010-05-07 13:03:31 +00002771
2772 if (stencilbufferObject)
2773 {
2774 return stencilbufferObject->getStencilSize() > 0;
2775 }
2776 }
2777
2778 return false;
2779}
2780
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002781void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2782{
2783 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2784
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002785 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2786 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2787 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2788 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002789
2790 mVertexDataManager->dirtyCurrentValues();
2791}
2792
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002793void Context::initExtensionString()
2794{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002795 mExtensionString += "GL_OES_packed_depth_stencil ";
2796
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002797 if (mBufferBackEnd->supportIntIndices())
2798 {
2799 mExtensionString += "GL_OES_element_index_uint ";
2800 }
2801
2802 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2803 if (end != std::string::npos)
2804 {
2805 mExtensionString.resize(end+1);
2806 }
2807}
2808
2809const char *Context::getExtensionString() const
2810{
2811 return mExtensionString.c_str();
2812}
2813
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814}
2815
2816extern "C"
2817{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00002818gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00002820 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821}
2822
2823void glDestroyContext(gl::Context *context)
2824{
2825 delete context;
2826
2827 if (context == gl::getContext())
2828 {
2829 gl::makeCurrent(NULL, NULL, NULL);
2830 }
2831}
2832
2833void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2834{
2835 gl::makeCurrent(context, display, surface);
2836}
2837
2838gl::Context *glGetCurrentContext()
2839{
2840 return gl::getContext();
2841}
2842}