blob: 9ee64d71a365dffe8d44b011adf9d24cd038cffd [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.com9ecb9f92010-07-28 19:21:12 +0000123 mTexture2DZero = new Texture2D(0);
124 mTextureCubeMapZero = new TextureCubeMap(0);
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;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000130 bindArrayBuffer(0);
131 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 bindTextureCubeMap(0);
133 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000134 bindReadFramebuffer(0);
135 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindRenderbuffer(0);
137
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000138 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 {
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000140 mIncompleteTextures[type] = NULL;
141 }
142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000145 mState.packAlignment = 4;
146 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mBufferBackEnd = NULL;
149 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000151 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000160
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000161 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000162 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163}
164
165Context::~Context()
166{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000167 if (mState.currentProgram != 0)
168 {
169 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
170 if (programObject)
171 {
172 programObject->release();
173 }
174 mState.currentProgram = 0;
175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000177 while (!mFramebufferMap.empty())
178 {
179 deleteFramebuffer(mFramebufferMap.begin()->first);
180 }
181
182 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
183 {
184 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
185 {
186 mState.samplerTexture[type][sampler].set(NULL);
187 }
188 }
189
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000190 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
191 {
192 delete mIncompleteTextures[type];
193 }
194
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000195 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
196 {
197 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
198 }
199
200 mState.arrayBuffer.set(NULL);
201 mState.elementArrayBuffer.set(NULL);
202 mState.texture2D.set(NULL);
203 mState.textureCubeMap.set(NULL);
204 mState.renderbuffer.set(NULL);
205
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206 delete mTexture2DZero;
207 delete mTextureCubeMapZero;
208
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000209 delete mBufferBackEnd;
210 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000211 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000212 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000213
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000214 if (mMaskedClearSavedState)
215 {
216 mMaskedClearSavedState->Release();
217 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000218
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000219 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220}
221
222void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
223{
224 IDirect3DDevice9 *device = display->getDevice();
225
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000226 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000227 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000228 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000229
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000230 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000231 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000232 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000233 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000234
235 initExtensionString();
236
237 mState.viewportX = 0;
238 mState.viewportY = 0;
239 mState.viewportWidth = surface->getWidth();
240 mState.viewportHeight = surface->getHeight();
241
242 mState.scissorX = 0;
243 mState.scissorY = 0;
244 mState.scissorWidth = surface->getWidth();
245 mState.scissorHeight = surface->getHeight();
246
247 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000248 }
249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
251 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000252 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000255 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000256 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257
258 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
260 defaultRenderTarget->Release();
261
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000262 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000264 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000266
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000267 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000268
269 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270}
271
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000272// 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 +0000273void Context::markAllStateDirty()
274{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000275 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000276 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000277 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000278
279 mClearStateDirty = true;
280 mCullStateDirty = true;
281 mDepthStateDirty = true;
282 mMaskStateDirty = true;
283 mBlendStateDirty = true;
284 mStencilStateDirty = true;
285 mPolygonOffsetStateDirty = true;
286 mScissorStateDirty = true;
287 mSampleStateDirty = true;
288 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000289 mFrontFaceDirty = true;
290
291 if (mBufferBackEnd != NULL)
292 {
293 mBufferBackEnd->invalidate();
294 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000295}
296
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297void Context::setClearColor(float red, float green, float blue, float alpha)
298{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000299 mState.colorClearValue.red = red;
300 mState.colorClearValue.green = green;
301 mState.colorClearValue.blue = blue;
302 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303}
304
305void Context::setClearDepth(float depth)
306{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000307 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308}
309
310void Context::setClearStencil(int stencil)
311{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000312 mState.stencilClearValue = stencil;
313}
314
315void Context::setCullFace(bool enabled)
316{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000317 if (mState.cullFace != enabled)
318 {
319 mState.cullFace = enabled;
320 mCullStateDirty = true;
321 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000322}
323
324bool Context::isCullFaceEnabled() const
325{
326 return mState.cullFace;
327}
328
329void Context::setCullMode(GLenum mode)
330{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000331 if (mState.cullMode != mode)
332 {
333 mState.cullMode = mode;
334 mCullStateDirty = true;
335 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000336}
337
338void Context::setFrontFace(GLenum front)
339{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000340 if (mState.frontFace != front)
341 {
342 mState.frontFace = front;
343 mFrontFaceDirty = true;
344 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000345}
346
347void Context::setDepthTest(bool enabled)
348{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000349 if (mState.depthTest != enabled)
350 {
351 mState.depthTest = enabled;
352 mDepthStateDirty = true;
353 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000354}
355
356bool Context::isDepthTestEnabled() const
357{
358 return mState.depthTest;
359}
360
361void Context::setDepthFunc(GLenum depthFunc)
362{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000363 if (mState.depthFunc != depthFunc)
364 {
365 mState.depthFunc = depthFunc;
366 mDepthStateDirty = true;
367 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000368}
369
370void Context::setDepthRange(float zNear, float zFar)
371{
372 mState.zNear = zNear;
373 mState.zFar = zFar;
374}
375
376void Context::setBlend(bool enabled)
377{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000378 if (mState.blend != enabled)
379 {
380 mState.blend = enabled;
381 mBlendStateDirty = true;
382 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000383}
384
385bool Context::isBlendEnabled() const
386{
387 return mState.blend;
388}
389
390void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
391{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000392 if (mState.sourceBlendRGB != sourceRGB ||
393 mState.sourceBlendAlpha != sourceAlpha ||
394 mState.destBlendRGB != destRGB ||
395 mState.destBlendAlpha != destAlpha)
396 {
397 mState.sourceBlendRGB = sourceRGB;
398 mState.destBlendRGB = destRGB;
399 mState.sourceBlendAlpha = sourceAlpha;
400 mState.destBlendAlpha = destAlpha;
401 mBlendStateDirty = true;
402 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403}
404
405void Context::setBlendColor(float red, float green, float blue, float alpha)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.blendColor.red != red ||
408 mState.blendColor.green != green ||
409 mState.blendColor.blue != blue ||
410 mState.blendColor.alpha != alpha)
411 {
412 mState.blendColor.red = red;
413 mState.blendColor.green = green;
414 mState.blendColor.blue = blue;
415 mState.blendColor.alpha = alpha;
416 mBlendStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.blendEquationRGB != rgbEquation ||
423 mState.blendEquationAlpha != alphaEquation)
424 {
425 mState.blendEquationRGB = rgbEquation;
426 mState.blendEquationAlpha = alphaEquation;
427 mBlendStateDirty = true;
428 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000429}
430
431void Context::setStencilTest(bool enabled)
432{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000433 if (mState.stencilTest != enabled)
434 {
435 mState.stencilTest = enabled;
436 mStencilStateDirty = true;
437 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000438}
439
440bool Context::isStencilTestEnabled() const
441{
442 return mState.stencilTest;
443}
444
445void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
446{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000447 if (mState.stencilFunc != stencilFunc ||
448 mState.stencilRef != stencilRef ||
449 mState.stencilMask != stencilMask)
450 {
451 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000452 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 mState.stencilMask = stencilMask;
454 mStencilStateDirty = true;
455 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000456}
457
458void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
459{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000460 if (mState.stencilBackFunc != stencilBackFunc ||
461 mState.stencilBackRef != stencilBackRef ||
462 mState.stencilBackMask != stencilBackMask)
463 {
464 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000465 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 mState.stencilBackMask = stencilBackMask;
467 mStencilStateDirty = true;
468 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000469}
470
471void Context::setStencilWritemask(GLuint stencilWritemask)
472{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000473 if (mState.stencilWritemask != stencilWritemask)
474 {
475 mState.stencilWritemask = stencilWritemask;
476 mStencilStateDirty = true;
477 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000478}
479
480void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
481{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000482 if (mState.stencilBackWritemask != stencilBackWritemask)
483 {
484 mState.stencilBackWritemask = stencilBackWritemask;
485 mStencilStateDirty = true;
486 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000487}
488
489void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilFail != stencilFail ||
492 mState.stencilPassDepthFail != stencilPassDepthFail ||
493 mState.stencilPassDepthPass != stencilPassDepthPass)
494 {
495 mState.stencilFail = stencilFail;
496 mState.stencilPassDepthFail = stencilPassDepthFail;
497 mState.stencilPassDepthPass = stencilPassDepthPass;
498 mStencilStateDirty = true;
499 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000500}
501
502void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilBackFail != stencilBackFail ||
505 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
506 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
507 {
508 mState.stencilBackFail = stencilBackFail;
509 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
510 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setPolygonOffsetFill(bool enabled)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.polygonOffsetFill != enabled)
518 {
519 mState.polygonOffsetFill = enabled;
520 mPolygonOffsetStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524bool Context::isPolygonOffsetFillEnabled() const
525{
526 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000527
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000528}
529
530void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
531{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000532 if (mState.polygonOffsetFactor != factor ||
533 mState.polygonOffsetUnits != units)
534 {
535 mState.polygonOffsetFactor = factor;
536 mState.polygonOffsetUnits = units;
537 mPolygonOffsetStateDirty = true;
538 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000539}
540
541void Context::setSampleAlphaToCoverage(bool enabled)
542{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 if (mState.sampleAlphaToCoverage != enabled)
544 {
545 mState.sampleAlphaToCoverage = enabled;
546 mSampleStateDirty = true;
547 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000548}
549
550bool Context::isSampleAlphaToCoverageEnabled() const
551{
552 return mState.sampleAlphaToCoverage;
553}
554
555void Context::setSampleCoverage(bool enabled)
556{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000557 if (mState.sampleCoverage != enabled)
558 {
559 mState.sampleCoverage = enabled;
560 mSampleStateDirty = true;
561 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000562}
563
564bool Context::isSampleCoverageEnabled() const
565{
566 return mState.sampleCoverage;
567}
568
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000569void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000570{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571 if (mState.sampleCoverageValue != value ||
572 mState.sampleCoverageInvert != invert)
573 {
574 mState.sampleCoverageValue = value;
575 mState.sampleCoverageInvert = invert;
576 mSampleStateDirty = true;
577 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578}
579
580void Context::setScissorTest(bool enabled)
581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.scissorTest != enabled)
583 {
584 mState.scissorTest = enabled;
585 mScissorStateDirty = true;
586 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000587}
588
589bool Context::isScissorTestEnabled() const
590{
591 return mState.scissorTest;
592}
593
594void Context::setDither(bool enabled)
595{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 if (mState.dither != enabled)
597 {
598 mState.dither = enabled;
599 mDitherStateDirty = true;
600 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000601}
602
603bool Context::isDitherEnabled() const
604{
605 return mState.dither;
606}
607
608void Context::setLineWidth(GLfloat width)
609{
610 mState.lineWidth = width;
611}
612
613void Context::setGenerateMipmapHint(GLenum hint)
614{
615 mState.generateMipmapHint = hint;
616}
617
618void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
619{
620 mState.viewportX = x;
621 mState.viewportY = y;
622 mState.viewportWidth = width;
623 mState.viewportHeight = height;
624}
625
626void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
627{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000628 if (mState.scissorX != x || mState.scissorY != y ||
629 mState.scissorWidth != width || mState.scissorHeight != height)
630 {
631 mState.scissorX = x;
632 mState.scissorY = y;
633 mState.scissorWidth = width;
634 mState.scissorHeight = height;
635 mScissorStateDirty = true;
636 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000637}
638
639void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
640{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000641 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
642 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
643 {
644 mState.colorMaskRed = red;
645 mState.colorMaskGreen = green;
646 mState.colorMaskBlue = blue;
647 mState.colorMaskAlpha = alpha;
648 mMaskStateDirty = true;
649 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652void Context::setDepthMask(bool mask)
653{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000654 if (mState.depthMask != mask)
655 {
656 mState.depthMask = mask;
657 mMaskStateDirty = true;
658 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000659}
660
661void Context::setActiveSampler(int active)
662{
663 mState.activeSampler = active;
664}
665
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000666GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000667{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000668 return mState.readFramebuffer;
669}
670
671GLuint Context::getDrawFramebufferHandle() const
672{
673 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000674}
675
676GLuint Context::getRenderbufferHandle() const
677{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000678 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000679}
680
681GLuint Context::getArrayBufferHandle() const
682{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000683 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000684}
685
686void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
687{
688 mState.vertexAttribute[attribNum].mEnabled = enabled;
689}
690
691const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
692{
693 return mState.vertexAttribute[attribNum];
694}
695
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000696void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000697 GLsizei stride, const void *pointer)
698{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000699 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700 mState.vertexAttribute[attribNum].mSize = size;
701 mState.vertexAttribute[attribNum].mType = type;
702 mState.vertexAttribute[attribNum].mNormalized = normalized;
703 mState.vertexAttribute[attribNum].mStride = stride;
704 mState.vertexAttribute[attribNum].mPointer = pointer;
705}
706
707const void *Context::getVertexAttribPointer(unsigned int attribNum) const
708{
709 return mState.vertexAttribute[attribNum].mPointer;
710}
711
712// returns entire set of attributes as a block
713const AttributeState *Context::getVertexAttribBlock()
714{
715 return mState.vertexAttribute;
716}
717
718void Context::setPackAlignment(GLint alignment)
719{
720 mState.packAlignment = alignment;
721}
722
723GLint Context::getPackAlignment() const
724{
725 return mState.packAlignment;
726}
727
728void Context::setUnpackAlignment(GLint alignment)
729{
730 mState.unpackAlignment = alignment;
731}
732
733GLint Context::getUnpackAlignment() const
734{
735 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736}
737
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738GLuint Context::createBuffer()
739{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000740 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743GLuint Context::createProgram()
744{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000745 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000746}
747
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000748GLuint Context::createShader(GLenum type)
749{
750 return mResourceManager->createShader(type);
751}
752
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753GLuint Context::createTexture()
754{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000755 return mResourceManager->createTexture();
756}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000758GLuint Context::createRenderbuffer()
759{
760 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761}
762
763// Returns an unused framebuffer name
764GLuint Context::createFramebuffer()
765{
766 unsigned int handle = 1;
767
768 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
769 {
770 handle++;
771 }
772
773 mFramebufferMap[handle] = NULL;
774
775 return handle;
776}
777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778void Context::deleteBuffer(GLuint buffer)
779{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000780 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781 {
782 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000784
785 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786}
787
788void Context::deleteShader(GLuint shader)
789{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000790 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791}
792
793void Context::deleteProgram(GLuint program)
794{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000795 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
798void Context::deleteTexture(GLuint texture)
799{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801 {
802 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000804
805 mResourceManager->deleteTexture(texture);
806}
807
808void Context::deleteRenderbuffer(GLuint renderbuffer)
809{
810 if (mResourceManager->getRenderbuffer(renderbuffer))
811 {
812 detachRenderbuffer(renderbuffer);
813 }
814
815 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
818void Context::deleteFramebuffer(GLuint framebuffer)
819{
820 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
821
822 if (framebufferObject != mFramebufferMap.end())
823 {
824 detachFramebuffer(framebuffer);
825
826 delete framebufferObject->second;
827 mFramebufferMap.erase(framebufferObject);
828 }
829}
830
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000831Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000833 return mResourceManager->getBuffer(handle);
834}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000836Shader *Context::getShader(GLuint handle)
837{
838 return mResourceManager->getShader(handle);
839}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000841Program *Context::getProgram(GLuint handle)
842{
843 return mResourceManager->getProgram(handle);
844}
845
846Texture *Context::getTexture(GLuint handle)
847{
848 return mResourceManager->getTexture(handle);
849}
850
851Renderbuffer *Context::getRenderbuffer(GLuint handle)
852{
853 return mResourceManager->getRenderbuffer(handle);
854}
855
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000856Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000857{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000858 return getFramebuffer(mState.readFramebuffer);
859}
860
861Framebuffer *Context::getDrawFramebuffer()
862{
863 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864}
865
866void Context::bindArrayBuffer(unsigned int buffer)
867{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000870 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871}
872
873void Context::bindElementArrayBuffer(unsigned int buffer)
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000877 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878}
879
880void Context::bindTexture2D(GLuint texture)
881{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000882 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000884 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000886 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
889void Context::bindTextureCubeMap(GLuint texture)
890{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000891 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000893 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000895 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896}
897
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000898void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899{
900 if (!getFramebuffer(framebuffer))
901 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000902 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903 }
904
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000905 mState.readFramebuffer = framebuffer;
906}
907
908void Context::bindDrawFramebuffer(GLuint framebuffer)
909{
910 if (!getFramebuffer(framebuffer))
911 {
912 mFramebufferMap[framebuffer] = new Framebuffer();
913 }
914
915 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916}
917
918void Context::bindRenderbuffer(GLuint renderbuffer)
919{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000920 mResourceManager->checkRenderbufferAllocation(renderbuffer);
921
922 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923}
924
925void Context::useProgram(GLuint program)
926{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000927 GLuint priorProgram = mState.currentProgram;
928 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000929
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000930 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000932 Program *newProgram = mResourceManager->getProgram(program);
933 Program *oldProgram = mResourceManager->getProgram(priorProgram);
934
935 if (newProgram)
936 {
937 newProgram->addRef();
938 }
939
940 if (oldProgram)
941 {
942 oldProgram->release();
943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945}
946
947void Context::setFramebufferZero(Framebuffer *buffer)
948{
949 delete mFramebufferMap[0];
950 mFramebufferMap[0] = buffer;
951}
952
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000953void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
956 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
959Framebuffer *Context::getFramebuffer(unsigned int handle)
960{
961 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000962
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963 if (framebuffer == mFramebufferMap.end())
964 {
965 return NULL;
966 }
967 else
968 {
969 return framebuffer->second;
970 }
971}
972
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973Buffer *Context::getArrayBuffer()
974{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000975 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976}
977
978Buffer *Context::getElementArrayBuffer()
979{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000980 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981}
982
983Program *Context::getCurrentProgram()
984{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000985 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
988Texture2D *Context::getTexture2D()
989{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000990 if (mState.texture2D.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991 {
992 return mTexture2DZero;
993 }
994
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000995 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996}
997
998TextureCubeMap *Context::getTextureCubeMap()
999{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001000 if (mState.textureCubeMap.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001 {
1002 return mTextureCubeMapZero;
1003 }
1004
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001005 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001008Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001010 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001011
1012 if (texid == 0)
1013 {
1014 switch (type)
1015 {
1016 default: UNREACHABLE();
1017 case SAMPLER_2D: return mTexture2DZero;
1018 case SAMPLER_CUBE: return mTextureCubeMapZero;
1019 }
1020 }
1021
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001022 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001025bool Context::getBooleanv(GLenum pname, GLboolean *params)
1026{
1027 switch (pname)
1028 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001029 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1030 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1031 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001032 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001033 params[0] = mState.colorMaskRed;
1034 params[1] = mState.colorMaskGreen;
1035 params[2] = mState.colorMaskBlue;
1036 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001037 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001038 case GL_CULL_FACE: *params = mState.cullFace;
1039 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1040 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1041 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1042 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1043 case GL_STENCIL_TEST: *params = mState.stencilTest;
1044 case GL_DEPTH_TEST: *params = mState.depthTest;
1045 case GL_BLEND: *params = mState.blend;
1046 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001047 default:
1048 return false;
1049 }
1050
1051 return true;
1052}
1053
1054bool Context::getFloatv(GLenum pname, GLfloat *params)
1055{
1056 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1057 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1058 // GetIntegerv as its native query function. As it would require conversion in any
1059 // case, this should make no difference to the calling application.
1060 switch (pname)
1061 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001062 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1063 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1064 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1065 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1066 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001067 case GL_ALIASED_LINE_WIDTH_RANGE:
1068 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1069 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1070 break;
1071 case GL_ALIASED_POINT_SIZE_RANGE:
1072 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001073 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 +00001074 break;
1075 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001076 params[0] = mState.zNear;
1077 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001078 break;
1079 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001080 params[0] = mState.colorClearValue.red;
1081 params[1] = mState.colorClearValue.green;
1082 params[2] = mState.colorClearValue.blue;
1083 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001084 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001085 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001086 params[0] = mState.blendColor.red;
1087 params[1] = mState.blendColor.green;
1088 params[2] = mState.blendColor.blue;
1089 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001090 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001091 default:
1092 return false;
1093 }
1094
1095 return true;
1096}
1097
1098bool Context::getIntegerv(GLenum pname, GLint *params)
1099{
1100 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv 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. You may find it in
1104 // Context::getFloatv.
1105 switch (pname)
1106 {
1107 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1108 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1109 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1110 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1111 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1112 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1113 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1114 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001115 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1116 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1117 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1118 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001119 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1120 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001121 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1122 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1123 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001124 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001125 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1126 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1127 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1128 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1129 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1130 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1131 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1132 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1133 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1134 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1135 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1136 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1137 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1138 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1139 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1140 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1141 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1142 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1143 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1144 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1145 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1146 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1147 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1148 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1149 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1150 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1151 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1152 case GL_SUBPIXEL_BITS: *params = 4; break;
1153 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1154 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
1155 case GL_SAMPLE_BUFFERS: *params = 0; break;
1156 case GL_SAMPLES: *params = 0; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001157 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1158 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1159 case GL_MAX_VIEWPORT_DIMS:
1160 {
1161 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1162 params[0] = maxDimension;
1163 params[1] = maxDimension;
1164 }
1165 break;
1166 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.viewportX;
1168 params[1] = mState.viewportY;
1169 params[2] = mState.viewportWidth;
1170 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001171 break;
1172 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001173 params[0] = mState.scissorX;
1174 params[1] = mState.scissorY;
1175 params[2] = mState.scissorWidth;
1176 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001177 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001178 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1179 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001180 case GL_RED_BITS:
1181 case GL_GREEN_BITS:
1182 case GL_BLUE_BITS:
1183 case GL_ALPHA_BITS:
1184 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001185 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001186 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1187
1188 if (colorbuffer)
1189 {
1190 switch (pname)
1191 {
1192 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1193 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1194 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1195 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1196 }
1197 }
1198 else
1199 {
1200 *params = 0;
1201 }
1202 }
1203 break;
1204 case GL_DEPTH_BITS:
1205 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001206 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001207 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001208
1209 if (depthbuffer)
1210 {
1211 *params = depthbuffer->getDepthSize();
1212 }
1213 else
1214 {
1215 *params = 0;
1216 }
1217 }
1218 break;
1219 case GL_STENCIL_BITS:
1220 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001221 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001222 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001223
1224 if (stencilbuffer)
1225 {
1226 *params = stencilbuffer->getStencilSize();
1227 }
1228 else
1229 {
1230 *params = 0;
1231 }
1232 }
1233 break;
1234 case GL_TEXTURE_BINDING_2D:
1235 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001236 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001237 {
1238 error(GL_INVALID_OPERATION);
1239 return false;
1240 }
1241
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001242 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001243 }
1244 break;
1245 case GL_TEXTURE_BINDING_CUBE_MAP:
1246 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001247 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001248 {
1249 error(GL_INVALID_OPERATION);
1250 return false;
1251 }
1252
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001253 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001254 }
1255 break;
1256 default:
1257 return false;
1258 }
1259
1260 return true;
1261}
1262
1263bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1264{
1265 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1266 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1267 // to the fact that it is stored internally as a float, and so would require conversion
1268 // if returned from Context::getIntegerv. Since this conversion is already implemented
1269 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1270 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1271 // application.
1272 switch (pname)
1273 {
1274 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1275 case GL_SHADER_BINARY_FORMATS:
1276 {
1277 *type = GL_INT;
1278 *numParams = 0;
1279 }
1280 break;
1281 case GL_MAX_VERTEX_ATTRIBS:
1282 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1283 case GL_MAX_VARYING_VECTORS:
1284 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1285 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1286 case GL_MAX_TEXTURE_IMAGE_UNITS:
1287 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1288 case GL_MAX_RENDERBUFFER_SIZE:
1289 case GL_NUM_SHADER_BINARY_FORMATS:
1290 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1291 case GL_ARRAY_BUFFER_BINDING:
1292 case GL_FRAMEBUFFER_BINDING:
1293 case GL_RENDERBUFFER_BINDING:
1294 case GL_CURRENT_PROGRAM:
1295 case GL_PACK_ALIGNMENT:
1296 case GL_UNPACK_ALIGNMENT:
1297 case GL_GENERATE_MIPMAP_HINT:
1298 case GL_RED_BITS:
1299 case GL_GREEN_BITS:
1300 case GL_BLUE_BITS:
1301 case GL_ALPHA_BITS:
1302 case GL_DEPTH_BITS:
1303 case GL_STENCIL_BITS:
1304 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1305 case GL_CULL_FACE_MODE:
1306 case GL_FRONT_FACE:
1307 case GL_ACTIVE_TEXTURE:
1308 case GL_STENCIL_FUNC:
1309 case GL_STENCIL_VALUE_MASK:
1310 case GL_STENCIL_REF:
1311 case GL_STENCIL_FAIL:
1312 case GL_STENCIL_PASS_DEPTH_FAIL:
1313 case GL_STENCIL_PASS_DEPTH_PASS:
1314 case GL_STENCIL_BACK_FUNC:
1315 case GL_STENCIL_BACK_VALUE_MASK:
1316 case GL_STENCIL_BACK_REF:
1317 case GL_STENCIL_BACK_FAIL:
1318 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1319 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1320 case GL_DEPTH_FUNC:
1321 case GL_BLEND_SRC_RGB:
1322 case GL_BLEND_SRC_ALPHA:
1323 case GL_BLEND_DST_RGB:
1324 case GL_BLEND_DST_ALPHA:
1325 case GL_BLEND_EQUATION_RGB:
1326 case GL_BLEND_EQUATION_ALPHA:
1327 case GL_STENCIL_WRITEMASK:
1328 case GL_STENCIL_BACK_WRITEMASK:
1329 case GL_STENCIL_CLEAR_VALUE:
1330 case GL_SUBPIXEL_BITS:
1331 case GL_MAX_TEXTURE_SIZE:
1332 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1333 case GL_SAMPLE_BUFFERS:
1334 case GL_SAMPLES:
1335 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1336 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1337 case GL_TEXTURE_BINDING_2D:
1338 case GL_TEXTURE_BINDING_CUBE_MAP:
1339 {
1340 *type = GL_INT;
1341 *numParams = 1;
1342 }
1343 break;
1344 case GL_MAX_VIEWPORT_DIMS:
1345 {
1346 *type = GL_INT;
1347 *numParams = 2;
1348 }
1349 break;
1350 case GL_VIEWPORT:
1351 case GL_SCISSOR_BOX:
1352 {
1353 *type = GL_INT;
1354 *numParams = 4;
1355 }
1356 break;
1357 case GL_SHADER_COMPILER:
1358 case GL_SAMPLE_COVERAGE_INVERT:
1359 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001360 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1361 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1362 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1363 case GL_SAMPLE_COVERAGE:
1364 case GL_SCISSOR_TEST:
1365 case GL_STENCIL_TEST:
1366 case GL_DEPTH_TEST:
1367 case GL_BLEND:
1368 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001369 {
1370 *type = GL_BOOL;
1371 *numParams = 1;
1372 }
1373 break;
1374 case GL_COLOR_WRITEMASK:
1375 {
1376 *type = GL_BOOL;
1377 *numParams = 4;
1378 }
1379 break;
1380 case GL_POLYGON_OFFSET_FACTOR:
1381 case GL_POLYGON_OFFSET_UNITS:
1382 case GL_SAMPLE_COVERAGE_VALUE:
1383 case GL_DEPTH_CLEAR_VALUE:
1384 case GL_LINE_WIDTH:
1385 {
1386 *type = GL_FLOAT;
1387 *numParams = 1;
1388 }
1389 break;
1390 case GL_ALIASED_LINE_WIDTH_RANGE:
1391 case GL_ALIASED_POINT_SIZE_RANGE:
1392 case GL_DEPTH_RANGE:
1393 {
1394 *type = GL_FLOAT;
1395 *numParams = 2;
1396 }
1397 break;
1398 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001399 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001400 {
1401 *type = GL_FLOAT;
1402 *numParams = 4;
1403 }
1404 break;
1405 default:
1406 return false;
1407 }
1408
1409 return true;
1410}
1411
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001412// Applies the render target surface, depth stencil surface, viewport rectangle and
1413// scissor rectangle to the Direct3D 9 device
1414bool Context::applyRenderTarget(bool ignoreViewport)
1415{
1416 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001417
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001418 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001419
1420 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1421 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001422 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1423
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001424 return false;
1425 }
1426
1427 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001428 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001429
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001430 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1431 if (renderTargetSerial != mAppliedRenderTargetSerial)
1432 {
1433 device->SetRenderTarget(0, renderTarget);
1434 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001435 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001436 }
1437
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001438 unsigned int depthbufferSerial = 0;
1439 unsigned int stencilbufferSerial = 0;
1440 if (framebufferObject->getDepthbufferType() != GL_NONE)
1441 {
1442 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1443 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1444 }
1445 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1446 {
1447 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1448 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1449 }
1450
1451 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1452 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001453 {
1454 device->SetDepthStencilSurface(depthStencil);
1455 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001456 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001457 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458
1459 D3DVIEWPORT9 viewport;
1460 D3DSURFACE_DESC desc;
1461 renderTarget->GetDesc(&desc);
1462
1463 if (ignoreViewport)
1464 {
1465 viewport.X = 0;
1466 viewport.Y = 0;
1467 viewport.Width = desc.Width;
1468 viewport.Height = desc.Height;
1469 viewport.MinZ = 0.0f;
1470 viewport.MaxZ = 1.0f;
1471 }
1472 else
1473 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001474 viewport.X = std::max(mState.viewportX, 0);
1475 viewport.Y = std::max(mState.viewportY, 0);
1476 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1477 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1478 viewport.MinZ = clamp01(mState.zNear);
1479 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001480 }
1481
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001482 if (viewport.Width <= 0 || viewport.Height <= 0)
1483 {
1484 return false; // Nothing to render
1485 }
1486
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487 device->SetViewport(&viewport);
1488
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001489 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001491 if (mState.scissorTest)
1492 {
1493 RECT rect = {mState.scissorX,
1494 mState.scissorY,
1495 mState.scissorX + mState.scissorWidth,
1496 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001497 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1498 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001499 device->SetScissorRect(&rect);
1500 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1501 }
1502 else
1503 {
1504 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1505 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001506
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001507 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001508 }
1509
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001510 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001512 Program *programObject = getCurrentProgram();
1513
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001514 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001515 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001517
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001518 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001519 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1520 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1521 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001522 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1523
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001524 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001525 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001526 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1527
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001528 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001529 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001530
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001531 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001532 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001533
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001534 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001535 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001536 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001537 }
1538
1539 return true;
1540}
1541
1542// 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 +00001543void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001544{
1545 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001546 Program *programObject = getCurrentProgram();
1547
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001548 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001549 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001550 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001551
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001552 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001553 GLint alwaysFront = !isTriangleMode(drawMode);
1554 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1555
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001556 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001557
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001558 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001560 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001561 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001562 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563 }
1564 else
1565 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001566 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567 }
1568
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001569 mCullStateDirty = false;
1570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001571
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001572 if (mDepthStateDirty)
1573 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001574 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001575 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001576 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1577 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001578 }
1579 else
1580 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001581 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001583
1584 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585 }
1586
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001587 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001589 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001590 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001591 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1592
1593 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1594 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1595 {
1596 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1597 }
1598 else
1599 {
1600 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1601 unorm<8>(mState.blendColor.alpha),
1602 unorm<8>(mState.blendColor.alpha),
1603 unorm<8>(mState.blendColor.alpha)));
1604 }
1605
1606 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1607 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1608 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1609
1610 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1611 mState.destBlendRGB != mState.destBlendAlpha ||
1612 mState.blendEquationRGB != mState.blendEquationAlpha)
1613 {
1614 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1615
1616 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1617 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1618 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1619
1620 }
1621 else
1622 {
1623 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1624 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001625 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001626 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001627 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001628 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001629 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001630
1631 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632 }
1633
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001634 if (mStencilStateDirty || mFrontFaceDirty)
1635 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001636 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001637 {
1638 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1639 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1640
1641 // FIXME: Unsupported by D3D9
1642 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1643 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1644 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1645 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1646 mState.stencilRef != mState.stencilBackRef ||
1647 mState.stencilMask != mState.stencilBackMask)
1648 {
1649 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1650 return error(GL_INVALID_OPERATION);
1651 }
1652
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001653 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001654 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001655 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1656
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001657 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1658 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1659 es2dx::ConvertComparison(mState.stencilFunc));
1660
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001661 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 +00001662 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1663
1664 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1665 es2dx::ConvertStencilOp(mState.stencilFail));
1666 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1667 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1668 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1669 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1670
1671 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1672 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1673 es2dx::ConvertComparison(mState.stencilBackFunc));
1674
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001675 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 +00001676 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1677
1678 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1679 es2dx::ConvertStencilOp(mState.stencilBackFail));
1680 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1681 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1682 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1683 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1684 }
1685 else
1686 {
1687 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1688 }
1689
1690 mStencilStateDirty = false;
1691 }
1692
1693 if (mMaskStateDirty)
1694 {
1695 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1696 mState.colorMaskBlue, mState.colorMaskAlpha));
1697 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1698
1699 mMaskStateDirty = false;
1700 }
1701
1702 if (mPolygonOffsetStateDirty)
1703 {
1704 if (mState.polygonOffsetFill)
1705 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001706 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001707 if (depthbuffer)
1708 {
1709 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1710 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1711 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1712 }
1713 }
1714 else
1715 {
1716 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1717 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1718 }
1719
1720 mPolygonOffsetStateDirty = false;
1721 }
1722
1723 if (mConfig->mMultiSample != 0 && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001725 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001726 {
1727 FIXME("Sample alpha to coverage is unimplemented.");
1728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001730 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001731 {
1732 FIXME("Sample coverage is unimplemented.");
1733 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001734
1735 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 }
1737
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 if (mDitherStateDirty)
1739 {
1740 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1741
1742 mDitherStateDirty = false;
1743 }
1744
1745 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746}
1747
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001748// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001749void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001751 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001753 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001755 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 }
1757 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001758}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001760GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001761{
1762 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1763
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001764 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001765 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001766 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001767 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001768 }
1769
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001770 lookupAttributeMapping(translated);
1771
1772 mBufferBackEnd->setupAttributesPreDraw(translated);
1773
1774 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1775 {
1776 if (translated[i].enabled && translated[i].nonArray)
1777 {
1778 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1779 if (err != GL_NO_ERROR)
1780 {
1781 return err;
1782 }
1783
1784 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1785
1786 *useIndexing = true;
1787 return GL_NO_ERROR;
1788 }
1789 }
1790
1791 *useIndexing = false;
1792 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001793}
1794
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001795GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001796{
1797 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1798
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001799 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001800
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001801 if (err == GL_NO_ERROR)
1802 {
1803 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001804
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001805 mBufferBackEnd->setupAttributesPreDraw(translated);
1806 }
1807
1808 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001809}
1810
1811// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001812GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001813{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001814 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001815
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001816 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001817 {
1818 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1819 }
1820
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001821 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822}
1823
1824// Applies the shaders and shader constants to the Direct3D 9 device
1825void Context::applyShaders()
1826{
1827 IDirect3DDevice9 *device = getDevice();
1828 Program *programObject = getCurrentProgram();
1829 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1830 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1831
1832 device->SetVertexShader(vertexShader);
1833 device->SetPixelShader(pixelShader);
1834
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001835 if (programObject->getSerial() != mAppliedProgram)
1836 {
1837 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001838 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001839 mAppliedProgram = programObject->getSerial();
1840 }
1841
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 programObject->applyUniforms();
1843}
1844
1845// Applies the textures and sampler states to the Direct3D 9 device
1846void Context::applyTextures()
1847{
1848 IDirect3DDevice9 *device = getDevice();
1849 Program *programObject = getCurrentProgram();
1850
1851 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1852 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001853 int textureUnit = programObject->getSamplerMapping(sampler);
1854 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001856 SamplerType textureType = programObject->getSamplerType(sampler);
1857
1858 Texture *texture = getSamplerTexture(textureUnit, textureType);
1859
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001860 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001861 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001862 if (texture->isComplete())
1863 {
1864 GLenum wrapS = texture->getWrapS();
1865 GLenum wrapT = texture->getWrapT();
1866 GLenum minFilter = texture->getMinFilter();
1867 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001869 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1870 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001872 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1873 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1874 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1875 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1876 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001878 device->SetTexture(sampler, texture->getTexture());
1879 }
1880 else
1881 {
1882 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1883 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001884 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001885
1886 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001888 else
1889 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001890 if (programObject->isSamplerDirty(sampler))
1891 {
1892 device->SetTexture(sampler, NULL);
1893 programObject->setSamplerDirty(sampler, false);
1894 }
1895 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896 }
1897}
1898
1899void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1900{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001901 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00001902
1903 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1904 {
1905 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1906 }
1907
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1909 IDirect3DDevice9 *device = getDevice();
1910
1911 D3DSURFACE_DESC desc;
1912 renderTarget->GetDesc(&desc);
1913
1914 IDirect3DSurface9 *systemSurface;
1915 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1916
1917 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1918 {
1919 return error(GL_OUT_OF_MEMORY);
1920 }
1921
1922 ASSERT(SUCCEEDED(result));
1923
1924 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1925 {
1926 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1927 }
1928
1929 result = device->GetRenderTargetData(renderTarget, systemSurface);
1930
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931 if (FAILED(result))
1932 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 systemSurface->Release();
1934
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00001935 switch (result)
1936 {
1937 case D3DERR_DRIVERINTERNALERROR:
1938 case D3DERR_DEVICELOST:
1939 return error(GL_OUT_OF_MEMORY);
1940 default:
1941 UNREACHABLE();
1942 return; // No sensible error to generate
1943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
1945
1946 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001947 RECT rect = {std::max(x, 0),
1948 std::max(y, 0),
1949 std::min(x + width, (int)desc.Width),
1950 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951
1952 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1953
1954 if (FAILED(result))
1955 {
1956 UNREACHABLE();
1957 systemSurface->Release();
1958
1959 return; // No sensible error to generate
1960 }
1961
1962 unsigned char *source = (unsigned char*)lock.pBits;
1963 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001964 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001966 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00001967
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 for (int j = 0; j < rect.bottom - rect.top; j++)
1969 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00001970 if (desc.Format == D3DFMT_A8R8G8B8 &&
1971 format == GL_BGRA_EXT &&
1972 type == GL_UNSIGNED_BYTE)
1973 {
1974 // Fast path for EXT_read_format_bgra, given
1975 // an RGBA source buffer. Note that buffers with no
1976 // alpha go through the slow path below.
1977 memcpy(dest + j * outputPitch,
1978 source + j * lock.Pitch,
1979 (rect.right - rect.left) * 4);
1980 continue;
1981 }
1982
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001983 for (int i = 0; i < rect.right - rect.left; i++)
1984 {
1985 float r;
1986 float g;
1987 float b;
1988 float a;
1989
1990 switch (desc.Format)
1991 {
1992 case D3DFMT_R5G6B5:
1993 {
1994 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1995
1996 a = 1.0f;
1997 b = (rgb & 0x001F) * (1.0f / 0x001F);
1998 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1999 r = (rgb & 0xF800) * (1.0f / 0xF800);
2000 }
2001 break;
2002 case D3DFMT_X1R5G5B5:
2003 {
2004 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2005
2006 a = 1.0f;
2007 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2008 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2009 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2010 }
2011 break;
2012 case D3DFMT_A1R5G5B5:
2013 {
2014 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2015
2016 a = (argb & 0x8000) ? 1.0f : 0.0f;
2017 b = (argb & 0x001F) * (1.0f / 0x001F);
2018 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2019 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2020 }
2021 break;
2022 case D3DFMT_A8R8G8B8:
2023 {
2024 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2025
2026 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2027 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2028 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2029 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2030 }
2031 break;
2032 case D3DFMT_X8R8G8B8:
2033 {
2034 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2035
2036 a = 1.0f;
2037 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2038 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2039 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2040 }
2041 break;
2042 case D3DFMT_A2R10G10B10:
2043 {
2044 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2045
2046 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2047 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2048 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2049 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2050 }
2051 break;
2052 default:
2053 UNIMPLEMENTED(); // FIXME
2054 UNREACHABLE();
2055 }
2056
2057 switch (format)
2058 {
2059 case GL_RGBA:
2060 switch (type)
2061 {
2062 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002063 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2064 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2065 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2066 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067 break;
2068 default: UNREACHABLE();
2069 }
2070 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002071 case GL_BGRA_EXT:
2072 switch (type)
2073 {
2074 case GL_UNSIGNED_BYTE:
2075 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2076 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2077 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2078 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2079 break;
2080 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2081 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2082 // this type is packed as follows:
2083 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2084 // --------------------------------------------------------------------------------
2085 // | 4th | 3rd | 2nd | 1st component |
2086 // --------------------------------------------------------------------------------
2087 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2088 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2089 ((unsigned short)(15 * a + 0.5f) << 12)|
2090 ((unsigned short)(15 * r + 0.5f) << 8) |
2091 ((unsigned short)(15 * g + 0.5f) << 4) |
2092 ((unsigned short)(15 * b + 0.5f) << 0);
2093 break;
2094 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2095 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2096 // this type is packed as follows:
2097 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2098 // --------------------------------------------------------------------------------
2099 // | 4th | 3rd | 2nd | 1st component |
2100 // --------------------------------------------------------------------------------
2101 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2102 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2103 ((unsigned short)( a + 0.5f) << 15) |
2104 ((unsigned short)(31 * r + 0.5f) << 10) |
2105 ((unsigned short)(31 * g + 0.5f) << 5) |
2106 ((unsigned short)(31 * b + 0.5f) << 0);
2107 break;
2108 default: UNREACHABLE();
2109 }
2110 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002111 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 switch (type)
2113 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002114 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002115 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2116 ((unsigned short)(31 * b + 0.5f) << 0) |
2117 ((unsigned short)(63 * g + 0.5f) << 5) |
2118 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119 break;
2120 default: UNREACHABLE();
2121 }
2122 break;
2123 default: UNREACHABLE();
2124 }
2125 }
2126 }
2127
2128 systemSurface->UnlockRect();
2129
2130 systemSurface->Release();
2131}
2132
2133void Context::clear(GLbitfield mask)
2134{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002135 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002136
2137 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2138 {
2139 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2140
2141 return;
2142 }
2143
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002144 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 IDirect3DDevice9 *device = getDevice();
2146 DWORD flags = 0;
2147
2148 if (mask & GL_COLOR_BUFFER_BIT)
2149 {
2150 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002151
2152 if (framebufferObject->getColorbufferType() != GL_NONE)
2153 {
2154 flags |= D3DCLEAR_TARGET;
2155 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 }
2157
2158 if (mask & GL_DEPTH_BUFFER_BIT)
2159 {
2160 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002161 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 {
2163 flags |= D3DCLEAR_ZBUFFER;
2164 }
2165 }
2166
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 GLuint stencilUnmasked = 0x0;
2168
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002169 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002172 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002174 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2175 D3DSURFACE_DESC desc;
2176 depthStencil->GetDesc(&desc);
2177
2178 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2179 stencilUnmasked = (0x1 << stencilSize) - 1;
2180
2181 if (stencilUnmasked != 0x0)
2182 {
2183 flags |= D3DCLEAR_STENCIL;
2184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
2186 }
2187
2188 if (mask != 0)
2189 {
2190 return error(GL_INVALID_VALUE);
2191 }
2192
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002193 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2194 {
2195 return;
2196 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002198 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2199 unorm<8>(mState.colorClearValue.red),
2200 unorm<8>(mState.colorClearValue.green),
2201 unorm<8>(mState.colorClearValue.blue));
2202 float depth = clamp01(mState.depthClearValue);
2203 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204
2205 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2206
2207 D3DSURFACE_DESC desc;
2208 renderTarget->GetDesc(&desc);
2209
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002210 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211
2212 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002213 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002215 !(mState.colorMaskRed && mState.colorMaskGreen &&
2216 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217
2218 if (needMaskedColorClear || needMaskedStencilClear)
2219 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002220 // State which is altered in all paths from this point to the clear call is saved.
2221 // State which is altered in only some paths will be flagged dirty in the case that
2222 // that path is taken.
2223 HRESULT hr;
2224 if (mMaskedClearSavedState == NULL)
2225 {
2226 hr = device->BeginStateBlock();
2227 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2228
2229 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2230 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2231 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2232 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2233 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2234 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2235 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2236 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2237 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2238 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2239 device->SetPixelShader(NULL);
2240 device->SetVertexShader(NULL);
2241 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2242 device->SetStreamSourceFreq(0, 1);
2243
2244 hr = device->EndStateBlock(&mMaskedClearSavedState);
2245 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2246 }
2247
2248 ASSERT(mMaskedClearSavedState != NULL);
2249
2250 if (mMaskedClearSavedState != NULL)
2251 {
2252 hr = mMaskedClearSavedState->Capture();
2253 ASSERT(SUCCEEDED(hr));
2254 }
2255
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2257 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2258 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2259 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2260 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2261 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2262 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2263 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2264
2265 if (flags & D3DCLEAR_TARGET)
2266 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002267 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2268 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2269 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2270 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
2272 else
2273 {
2274 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2275 }
2276
2277 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2278 {
2279 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2280 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2281 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2282 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002283 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002284 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2286 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002287 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 }
2289 else
2290 {
2291 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2292 }
2293
2294 device->SetPixelShader(NULL);
2295 device->SetVertexShader(NULL);
2296 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002297 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002299 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 {
2301 float x, y, z, w;
2302 D3DCOLOR diffuse;
2303 };
2304
2305 Vertex quad[4];
2306 quad[0].x = 0.0f;
2307 quad[0].y = (float)desc.Height;
2308 quad[0].z = 0.0f;
2309 quad[0].w = 1.0f;
2310 quad[0].diffuse = color;
2311
2312 quad[1].x = (float)desc.Width;
2313 quad[1].y = (float)desc.Height;
2314 quad[1].z = 0.0f;
2315 quad[1].w = 1.0f;
2316 quad[1].diffuse = color;
2317
2318 quad[2].x = 0.0f;
2319 quad[2].y = 0.0f;
2320 quad[2].z = 0.0f;
2321 quad[2].w = 1.0f;
2322 quad[2].diffuse = color;
2323
2324 quad[3].x = (float)desc.Width;
2325 quad[3].y = 0.0f;
2326 quad[3].z = 0.0f;
2327 quad[3].w = 1.0f;
2328 quad[3].diffuse = color;
2329
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002330 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332
2333 if (flags & D3DCLEAR_ZBUFFER)
2334 {
2335 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2336 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2337 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2338 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002339
2340 if (mMaskedClearSavedState != NULL)
2341 {
2342 mMaskedClearSavedState->Apply();
2343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002345 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346 {
2347 device->Clear(0, NULL, flags, color, depth, stencil);
2348 }
2349}
2350
2351void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2352{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002353 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 {
2355 return error(GL_INVALID_OPERATION);
2356 }
2357
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002358 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 IDirect3DDevice9 *device = getDevice();
2360 D3DPRIMITIVETYPE primitiveType;
2361 int primitiveCount;
2362
2363 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2364 return error(GL_INVALID_ENUM);
2365
2366 if (primitiveCount <= 0)
2367 {
2368 return;
2369 }
2370
2371 if (!applyRenderTarget(false))
2372 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002373 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 }
2375
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002376 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002377
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002378 TranslatedIndexData indexInfo;
2379 bool useIndexing;
2380 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002381 if (err != GL_NO_ERROR)
2382 {
2383 return error(err);
2384 }
2385
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386 applyShaders();
2387 applyTextures();
2388
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002389 if (!getCurrentProgram()->validateSamplers())
2390 {
2391 return error(GL_INVALID_OPERATION);
2392 }
2393
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002394 if (!cullSkipsDraw(mode))
2395 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002396 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002397 if (useIndexing)
2398 {
2399 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2400 }
2401 else
2402 {
2403 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2404 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002405 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406}
2407
2408void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2409{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002410 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 {
2412 return error(GL_INVALID_OPERATION);
2413 }
2414
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002415 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 {
2417 return error(GL_INVALID_OPERATION);
2418 }
2419
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002420 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 IDirect3DDevice9 *device = getDevice();
2422 D3DPRIMITIVETYPE primitiveType;
2423 int primitiveCount;
2424
2425 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2426 return error(GL_INVALID_ENUM);
2427
2428 if (primitiveCount <= 0)
2429 {
2430 return;
2431 }
2432
2433 if (!applyRenderTarget(false))
2434 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002435 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 }
2437
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002438 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002439
2440 TranslatedIndexData indexInfo;
2441 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2442 if (err != GL_NO_ERROR)
2443 {
2444 return error(err);
2445 }
2446
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002447 err = applyVertexBuffer(indexInfo);
2448 if (err != GL_NO_ERROR)
2449 {
2450 return error(err);
2451 }
2452
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 applyShaders();
2454 applyTextures();
2455
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002456 if (!getCurrentProgram()->validateSamplers())
2457 {
2458 return error(GL_INVALID_OPERATION);
2459 }
2460
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002461 if (!cullSkipsDraw(mode))
2462 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002463 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002464 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 +00002465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466}
2467
2468void Context::finish()
2469{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002470 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 IDirect3DDevice9 *device = getDevice();
2472 IDirect3DQuery9 *occlusionQuery = NULL;
2473
2474 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2475
2476 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2477 {
2478 return error(GL_OUT_OF_MEMORY);
2479 }
2480
2481 ASSERT(SUCCEEDED(result));
2482
2483 if (occlusionQuery)
2484 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002485 IDirect3DStateBlock9 *savedState = NULL;
2486 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2487
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 occlusionQuery->Issue(D3DISSUE_BEGIN);
2489
2490 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002491 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 device->SetPixelShader(NULL);
2493 device->SetVertexShader(NULL);
2494 device->SetFVF(D3DFVF_XYZRHW);
2495 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002496 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498
2499 occlusionQuery->Issue(D3DISSUE_END);
2500
2501 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2502 {
2503 // Keep polling, but allow other threads to do something useful first
2504 Sleep(0);
2505 }
2506
2507 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002508
2509 if (savedState)
2510 {
2511 savedState->Apply();
2512 savedState->Release();
2513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 }
2515}
2516
2517void Context::flush()
2518{
2519 IDirect3DDevice9 *device = getDevice();
2520 IDirect3DQuery9 *eventQuery = NULL;
2521
2522 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2523
2524 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2525 {
2526 return error(GL_OUT_OF_MEMORY);
2527 }
2528
2529 ASSERT(SUCCEEDED(result));
2530
2531 if (eventQuery)
2532 {
2533 eventQuery->Issue(D3DISSUE_END);
2534
2535 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2536 {
2537 // Keep polling, but allow other threads to do something useful first
2538 Sleep(0);
2539 }
2540
2541 eventQuery->Release();
2542 }
2543}
2544
2545void Context::recordInvalidEnum()
2546{
2547 mInvalidEnum = true;
2548}
2549
2550void Context::recordInvalidValue()
2551{
2552 mInvalidValue = true;
2553}
2554
2555void Context::recordInvalidOperation()
2556{
2557 mInvalidOperation = true;
2558}
2559
2560void Context::recordOutOfMemory()
2561{
2562 mOutOfMemory = true;
2563}
2564
2565void Context::recordInvalidFramebufferOperation()
2566{
2567 mInvalidFramebufferOperation = true;
2568}
2569
2570// Get one of the recorded errors and clear its flag, if any.
2571// [OpenGL ES 2.0.24] section 2.5 page 13.
2572GLenum Context::getError()
2573{
2574 if (mInvalidEnum)
2575 {
2576 mInvalidEnum = false;
2577
2578 return GL_INVALID_ENUM;
2579 }
2580
2581 if (mInvalidValue)
2582 {
2583 mInvalidValue = false;
2584
2585 return GL_INVALID_VALUE;
2586 }
2587
2588 if (mInvalidOperation)
2589 {
2590 mInvalidOperation = false;
2591
2592 return GL_INVALID_OPERATION;
2593 }
2594
2595 if (mOutOfMemory)
2596 {
2597 mOutOfMemory = false;
2598
2599 return GL_OUT_OF_MEMORY;
2600 }
2601
2602 if (mInvalidFramebufferOperation)
2603 {
2604 mInvalidFramebufferOperation = false;
2605
2606 return GL_INVALID_FRAMEBUFFER_OPERATION;
2607 }
2608
2609 return GL_NO_ERROR;
2610}
2611
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002612bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002613{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002614 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002615}
2616
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617void Context::detachBuffer(GLuint buffer)
2618{
2619 // [OpenGL ES 2.0.24] section 2.9 page 22:
2620 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2621 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2622
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002623 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002625 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626 }
2627
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002628 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002630 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631 }
2632
2633 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2634 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002635 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002637 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638 }
2639 }
2640}
2641
2642void Context::detachTexture(GLuint texture)
2643{
2644 // [OpenGL ES 2.0.24] section 3.8 page 84:
2645 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2646 // rebound to texture object zero
2647
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002648 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002650 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002652 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002653 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002654 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002655 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656 }
2657 }
2658
2659 // [OpenGL ES 2.0.24] section 4.4 page 112:
2660 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2661 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2662 // image was attached in the currently bound framebuffer.
2663
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002664 Framebuffer *readFramebuffer = getReadFramebuffer();
2665 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002667 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002669 readFramebuffer->detachTexture(texture);
2670 }
2671
2672 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2673 {
2674 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 }
2676}
2677
2678void Context::detachFramebuffer(GLuint framebuffer)
2679{
2680 // [OpenGL ES 2.0.24] section 4.4 page 107:
2681 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2682 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2683
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002684 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002686 bindReadFramebuffer(0);
2687 }
2688
2689 if (mState.drawFramebuffer == framebuffer)
2690 {
2691 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 }
2693}
2694
2695void Context::detachRenderbuffer(GLuint renderbuffer)
2696{
2697 // [OpenGL ES 2.0.24] section 4.4 page 109:
2698 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2699 // had been executed with the target RENDERBUFFER and name of zero.
2700
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002701 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702 {
2703 bindRenderbuffer(0);
2704 }
2705
2706 // [OpenGL ES 2.0.24] section 4.4 page 111:
2707 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2708 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2709 // point to which this image was attached in the currently bound framebuffer.
2710
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002711 Framebuffer *readFramebuffer = getReadFramebuffer();
2712 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002714 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002716 readFramebuffer->detachRenderbuffer(renderbuffer);
2717 }
2718
2719 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2720 {
2721 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722 }
2723}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002724
2725Texture *Context::getIncompleteTexture(SamplerType type)
2726{
2727 Texture *t = mIncompleteTextures[type];
2728
2729 if (t == NULL)
2730 {
2731 static const GLubyte color[] = { 0, 0, 0, 255 };
2732
2733 switch (type)
2734 {
2735 default:
2736 UNREACHABLE();
2737 // default falls through to SAMPLER_2D
2738
2739 case SAMPLER_2D:
2740 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002741 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002742 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002743 t = incomplete2d;
2744 }
2745 break;
2746
2747 case SAMPLER_CUBE:
2748 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002749 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002750
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002751 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2752 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2753 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2754 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2755 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2756 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002757
2758 t = incompleteCube;
2759 }
2760 break;
2761 }
2762
2763 mIncompleteTextures[type] = t;
2764 }
2765
2766 return t;
2767}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002768
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002769bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002770{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002771 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002772}
2773
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002774bool Context::isTriangleMode(GLenum drawMode)
2775{
2776 switch (drawMode)
2777 {
2778 case GL_TRIANGLES:
2779 case GL_TRIANGLE_FAN:
2780 case GL_TRIANGLE_STRIP:
2781 return true;
2782 case GL_POINTS:
2783 case GL_LINES:
2784 case GL_LINE_LOOP:
2785 case GL_LINE_STRIP:
2786 return false;
2787 default: UNREACHABLE();
2788 }
2789
2790 return false;
2791}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002792
2793void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2794{
2795 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2796
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002797 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2798 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2799 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2800 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002801
2802 mVertexDataManager->dirtyCurrentValues();
2803}
2804
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002805void Context::initExtensionString()
2806{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002807 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002808 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
2809 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002810
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002811 if (mBufferBackEnd->supportIntIndices())
2812 {
2813 mExtensionString += "GL_OES_element_index_uint ";
2814 }
2815
2816 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2817 if (end != std::string::npos)
2818 {
2819 mExtensionString.resize(end+1);
2820 }
2821}
2822
2823const char *Context::getExtensionString() const
2824{
2825 return mExtensionString.c_str();
2826}
2827
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828}
2829
2830extern "C"
2831{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00002832gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00002834 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835}
2836
2837void glDestroyContext(gl::Context *context)
2838{
2839 delete context;
2840
2841 if (context == gl::getContext())
2842 {
2843 gl::makeCurrent(NULL, NULL, NULL);
2844 }
2845}
2846
2847void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2848{
2849 gl::makeCurrent(context, display, surface);
2850}
2851
2852gl::Context *glGetCurrentContext()
2853{
2854 return gl::getContext();
2855}
2856}