blob: ac12d29ccca234d0ff3f7abd58da6f06e9ea8a88 [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"
20#include "libGLESv2/Buffer.h"
21#include "libGLESv2/FrameBuffer.h"
22#include "libGLESv2/Program.h"
23#include "libGLESv2/RenderBuffer.h"
24#include "libGLESv2/Shader.h"
25#include "libGLESv2/Texture.h"
26#include "libGLESv2/geometry/backend.h"
27#include "libGLESv2/geometry/VertexDataManager.h"
28#include "libGLESv2/geometry/IndexDataManager.h"
29#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000036Context::Context(const egl::Config *config)
37 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
39 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000040
daniel@transgaming.com428d1582010-05-04 03:35:25 +000041 mState.depthClearValue = 1.0f;
42 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
daniel@transgaming.com428d1582010-05-04 03:35:25 +000044 mState.cullFace = false;
45 mState.cullMode = GL_BACK;
46 mState.frontFace = GL_CCW;
47 mState.depthTest = false;
48 mState.depthFunc = GL_LESS;
49 mState.blend = false;
50 mState.sourceBlendRGB = GL_ONE;
51 mState.sourceBlendAlpha = GL_ONE;
52 mState.destBlendRGB = GL_ZERO;
53 mState.destBlendAlpha = GL_ZERO;
54 mState.blendEquationRGB = GL_FUNC_ADD;
55 mState.blendEquationAlpha = GL_FUNC_ADD;
56 mState.blendColor.red = 0;
57 mState.blendColor.green = 0;
58 mState.blendColor.blue = 0;
59 mState.blendColor.alpha = 0;
60 mState.stencilTest = false;
61 mState.stencilFunc = GL_ALWAYS;
62 mState.stencilRef = 0;
63 mState.stencilMask = -1;
64 mState.stencilWritemask = -1;
65 mState.stencilBackFunc = GL_ALWAYS;
66 mState.stencilBackRef = 0;
67 mState.stencilBackMask = - 1;
68 mState.stencilBackWritemask = -1;
69 mState.stencilFail = GL_KEEP;
70 mState.stencilPassDepthFail = GL_KEEP;
71 mState.stencilPassDepthPass = GL_KEEP;
72 mState.stencilBackFail = GL_KEEP;
73 mState.stencilBackPassDepthFail = GL_KEEP;
74 mState.stencilBackPassDepthPass = GL_KEEP;
75 mState.polygonOffsetFill = false;
76 mState.polygonOffsetFactor = 0.0f;
77 mState.polygonOffsetUnits = 0.0f;
78 mState.sampleAlphaToCoverage = false;
79 mState.sampleCoverage = false;
80 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000081 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000082 mState.scissorTest = false;
83 mState.dither = true;
84 mState.generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085
daniel@transgaming.com428d1582010-05-04 03:35:25 +000086 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000087
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.viewportX = 0;
89 mState.viewportY = 0;
90 mState.viewportWidth = config->mDisplayMode.Width;
91 mState.viewportHeight = config->mDisplayMode.Height;
92 mState.zNear = 0.0f;
93 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.scissorX = 0;
96 mState.scissorY = 0;
97 mState.scissorWidth = config->mDisplayMode.Width;
98 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000100 mState.colorMaskRed = true;
101 mState.colorMaskGreen = true;
102 mState.colorMaskBlue = true;
103 mState.colorMaskAlpha = true;
104 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
106 // [OpenGL ES 2.0.24] section 3.7 page 83:
107 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
108 // and cube map texture state vectors respectively associated with them.
109 // In order that access to these initial textures not be lost, they are treated as texture
110 // objects all of whose names are 0.
111
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000112 mTexture2DZero = new Texture2D(this);
113 mTextureCubeMapZero = new TextureCubeMap(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
115 mColorbufferZero = NULL;
116 mDepthbufferZero = NULL;
117 mStencilbufferZero = NULL;
118
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000119 mState.activeSampler = 0;
120 mState.arrayBuffer = 0;
121 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122 bindTextureCubeMap(0);
123 bindTexture2D(0);
124 bindFramebuffer(0);
125 bindRenderbuffer(0);
126
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000127 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000129 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
130 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000131 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000132 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 }
134
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000135 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
136 {
137 mIncompleteTextures[type] = NULL;
138 }
139
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000140 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000142 mState.packAlignment = 4;
143 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000144
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000145 mBufferBackEnd = NULL;
146 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000147 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000148 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150 mInvalidEnum = false;
151 mInvalidValue = false;
152 mInvalidOperation = false;
153 mOutOfMemory = false;
154 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000155
156 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000157
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000158 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000159 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162Context::~Context()
163{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000164 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000165
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000166 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
167 {
168 delete mIncompleteTextures[type];
169 }
170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171 delete mTexture2DZero;
172 delete mTextureCubeMapZero;
173
174 delete mColorbufferZero;
175 delete mDepthbufferZero;
176 delete mStencilbufferZero;
177
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000178 delete mBufferBackEnd;
179 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000180 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000181 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000182
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183 while (!mBufferMap.empty())
184 {
185 deleteBuffer(mBufferMap.begin()->first);
186 }
187
188 while (!mProgramMap.empty())
189 {
190 deleteProgram(mProgramMap.begin()->first);
191 }
192
193 while (!mShaderMap.empty())
194 {
195 deleteShader(mShaderMap.begin()->first);
196 }
197
198 while (!mFramebufferMap.empty())
199 {
200 deleteFramebuffer(mFramebufferMap.begin()->first);
201 }
202
203 while (!mRenderbufferMap.empty())
204 {
205 deleteRenderbuffer(mRenderbufferMap.begin()->first);
206 }
207
208 while (!mTextureMap.empty())
209 {
210 deleteTexture(mTextureMap.begin()->first);
211 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000212
213 if (mMaskedClearSavedState)
214 {
215 mMaskedClearSavedState->Release();
216 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217}
218
219void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
220{
221 IDirect3DDevice9 *device = display->getDevice();
222
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000223 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000224 {
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000225 device->GetDeviceCaps(&mDeviceCaps);
226
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000227 mBufferBackEnd = new Dx9BackEnd(device);
228 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000229 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000230 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000231
232 initExtensionString();
233
234 mState.viewportX = 0;
235 mState.viewportY = 0;
236 mState.viewportWidth = surface->getWidth();
237 mState.viewportHeight = surface->getHeight();
238
239 mState.scissorX = 0;
240 mState.scissorY = 0;
241 mState.scissorWidth = surface->getWidth();
242 mState.scissorHeight = surface->getHeight();
243
244 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000245 }
246
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
248 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000249 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250
251 Framebuffer *framebufferZero = new Framebuffer();
252 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000253 Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil);
254 Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255
256 setFramebufferZero(framebufferZero);
257 setColorbufferZero(colorbufferZero);
258 setDepthbufferZero(depthbufferZero);
259 setStencilbufferZero(stencilbufferZero);
260
261 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
262 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
263 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
264
265 defaultRenderTarget->Release();
266
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000267 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000269 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000271
272 D3DCAPS9 capabilities;
273 device->GetDeviceCaps(&capabilities);
274
275 if (capabilities.PixelShaderVersion == D3DPS_VERSION(3, 0))
276 {
277 mPsProfile = "ps_3_0";
278 mVsProfile = "vs_3_0";
279 }
280 else // egl::Display guarantees support for at least 2.0
281 {
282 mPsProfile = "ps_2_0";
283 mVsProfile = "vs_2_0";
284 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000285
286 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287}
288
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000289// 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 +0000290void Context::markAllStateDirty()
291{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000292 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000293 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000294 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000295
296 mClearStateDirty = true;
297 mCullStateDirty = true;
298 mDepthStateDirty = true;
299 mMaskStateDirty = true;
300 mBlendStateDirty = true;
301 mStencilStateDirty = true;
302 mPolygonOffsetStateDirty = true;
303 mScissorStateDirty = true;
304 mSampleStateDirty = true;
305 mDitherStateDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000306}
307
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308void Context::setClearColor(float red, float green, float blue, float alpha)
309{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000310 mState.colorClearValue.red = red;
311 mState.colorClearValue.green = green;
312 mState.colorClearValue.blue = blue;
313 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314}
315
316void Context::setClearDepth(float depth)
317{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000318 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319}
320
321void Context::setClearStencil(int stencil)
322{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000323 mState.stencilClearValue = stencil;
324}
325
326void Context::setCullFace(bool enabled)
327{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000328 if (mState.cullFace != enabled)
329 {
330 mState.cullFace = enabled;
331 mCullStateDirty = true;
332 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000333}
334
335bool Context::isCullFaceEnabled() const
336{
337 return mState.cullFace;
338}
339
340void Context::setCullMode(GLenum mode)
341{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000342 if (mState.cullMode != mode)
343 {
344 mState.cullMode = mode;
345 mCullStateDirty = true;
346 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000347}
348
349void Context::setFrontFace(GLenum front)
350{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000351 if (mState.frontFace != front)
352 {
353 mState.frontFace = front;
354 mFrontFaceDirty = true;
355 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356}
357
358void Context::setDepthTest(bool enabled)
359{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000360 if (mState.depthTest != enabled)
361 {
362 mState.depthTest = enabled;
363 mDepthStateDirty = true;
364 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000365}
366
367bool Context::isDepthTestEnabled() const
368{
369 return mState.depthTest;
370}
371
372void Context::setDepthFunc(GLenum depthFunc)
373{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000374 if (mState.depthFunc != depthFunc)
375 {
376 mState.depthFunc = depthFunc;
377 mDepthStateDirty = true;
378 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000379}
380
381void Context::setDepthRange(float zNear, float zFar)
382{
383 mState.zNear = zNear;
384 mState.zFar = zFar;
385}
386
387void Context::setBlend(bool enabled)
388{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000389 if (mState.blend != enabled)
390 {
391 mState.blend = enabled;
392 mBlendStateDirty = true;
393 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000394}
395
396bool Context::isBlendEnabled() const
397{
398 return mState.blend;
399}
400
401void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
402{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000403 if (mState.sourceBlendRGB != sourceRGB ||
404 mState.sourceBlendAlpha != sourceAlpha ||
405 mState.destBlendRGB != destRGB ||
406 mState.destBlendAlpha != destAlpha)
407 {
408 mState.sourceBlendRGB = sourceRGB;
409 mState.destBlendRGB = destRGB;
410 mState.sourceBlendAlpha = sourceAlpha;
411 mState.destBlendAlpha = destAlpha;
412 mBlendStateDirty = true;
413 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000414}
415
416void Context::setBlendColor(float red, float green, float blue, float alpha)
417{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000418 if (mState.blendColor.red != red ||
419 mState.blendColor.green != green ||
420 mState.blendColor.blue != blue ||
421 mState.blendColor.alpha != alpha)
422 {
423 mState.blendColor.red = red;
424 mState.blendColor.green = green;
425 mState.blendColor.blue = blue;
426 mState.blendColor.alpha = alpha;
427 mBlendStateDirty = true;
428 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000429}
430
431void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
432{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000433 if (mState.blendEquationRGB != rgbEquation ||
434 mState.blendEquationAlpha != alphaEquation)
435 {
436 mState.blendEquationRGB = rgbEquation;
437 mState.blendEquationAlpha = alphaEquation;
438 mBlendStateDirty = true;
439 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000440}
441
442void Context::setStencilTest(bool enabled)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.stencilTest != enabled)
445 {
446 mState.stencilTest = enabled;
447 mStencilStateDirty = true;
448 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000449}
450
451bool Context::isStencilTestEnabled() const
452{
453 return mState.stencilTest;
454}
455
456void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
457{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000458 if (mState.stencilFunc != stencilFunc ||
459 mState.stencilRef != stencilRef ||
460 mState.stencilMask != stencilMask)
461 {
462 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000463 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000464 mState.stencilMask = stencilMask;
465 mStencilStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.stencilBackFunc != stencilBackFunc ||
472 mState.stencilBackRef != stencilBackRef ||
473 mState.stencilBackMask != stencilBackMask)
474 {
475 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000476 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 mState.stencilBackMask = stencilBackMask;
478 mStencilStateDirty = true;
479 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000480}
481
482void Context::setStencilWritemask(GLuint stencilWritemask)
483{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000484 if (mState.stencilWritemask != stencilWritemask)
485 {
486 mState.stencilWritemask = stencilWritemask;
487 mStencilStateDirty = true;
488 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000489}
490
491void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
492{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000493 if (mState.stencilBackWritemask != stencilBackWritemask)
494 {
495 mState.stencilBackWritemask = stencilBackWritemask;
496 mStencilStateDirty = true;
497 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000498}
499
500void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
501{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000502 if (mState.stencilFail != stencilFail ||
503 mState.stencilPassDepthFail != stencilPassDepthFail ||
504 mState.stencilPassDepthPass != stencilPassDepthPass)
505 {
506 mState.stencilFail = stencilFail;
507 mState.stencilPassDepthFail = stencilPassDepthFail;
508 mState.stencilPassDepthPass = stencilPassDepthPass;
509 mStencilStateDirty = true;
510 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000511}
512
513void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
514{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 if (mState.stencilBackFail != stencilBackFail ||
516 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
517 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
518 {
519 mState.stencilBackFail = stencilBackFail;
520 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
521 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
522 mStencilStateDirty = true;
523 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000524}
525
526void Context::setPolygonOffsetFill(bool enabled)
527{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000528 if (mState.polygonOffsetFill != enabled)
529 {
530 mState.polygonOffsetFill = enabled;
531 mPolygonOffsetStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535bool Context::isPolygonOffsetFillEnabled() const
536{
537 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000538
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000539}
540
541void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
542{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 if (mState.polygonOffsetFactor != factor ||
544 mState.polygonOffsetUnits != units)
545 {
546 mState.polygonOffsetFactor = factor;
547 mState.polygonOffsetUnits = units;
548 mPolygonOffsetStateDirty = true;
549 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000550}
551
552void Context::setSampleAlphaToCoverage(bool enabled)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.sampleAlphaToCoverage != enabled)
555 {
556 mState.sampleAlphaToCoverage = enabled;
557 mSampleStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561bool Context::isSampleAlphaToCoverageEnabled() const
562{
563 return mState.sampleAlphaToCoverage;
564}
565
566void Context::setSampleCoverage(bool enabled)
567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.sampleCoverage != enabled)
569 {
570 mState.sampleCoverage = enabled;
571 mSampleStateDirty = true;
572 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000573}
574
575bool Context::isSampleCoverageEnabled() const
576{
577 return mState.sampleCoverage;
578}
579
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000580void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.sampleCoverageValue != value ||
583 mState.sampleCoverageInvert != invert)
584 {
585 mState.sampleCoverageValue = value;
586 mState.sampleCoverageInvert = invert;
587 mSampleStateDirty = true;
588 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000589}
590
591void Context::setScissorTest(bool enabled)
592{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000593 if (mState.scissorTest != enabled)
594 {
595 mState.scissorTest = enabled;
596 mScissorStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600bool Context::isScissorTestEnabled() const
601{
602 return mState.scissorTest;
603}
604
605void Context::setDither(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.dither != enabled)
608 {
609 mState.dither = enabled;
610 mDitherStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isDitherEnabled() const
615{
616 return mState.dither;
617}
618
619void Context::setLineWidth(GLfloat width)
620{
621 mState.lineWidth = width;
622}
623
624void Context::setGenerateMipmapHint(GLenum hint)
625{
626 mState.generateMipmapHint = hint;
627}
628
629void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
630{
631 mState.viewportX = x;
632 mState.viewportY = y;
633 mState.viewportWidth = width;
634 mState.viewportHeight = height;
635}
636
637void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
638{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000639 if (mState.scissorX != x || mState.scissorY != y ||
640 mState.scissorWidth != width || mState.scissorHeight != height)
641 {
642 mState.scissorX = x;
643 mState.scissorY = y;
644 mState.scissorWidth = width;
645 mState.scissorHeight = height;
646 mScissorStateDirty = true;
647 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000648}
649
650void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
651{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000652 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
653 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
654 {
655 mState.colorMaskRed = red;
656 mState.colorMaskGreen = green;
657 mState.colorMaskBlue = blue;
658 mState.colorMaskAlpha = alpha;
659 mMaskStateDirty = true;
660 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000661}
662
663void Context::setDepthMask(bool mask)
664{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000665 if (mState.depthMask != mask)
666 {
667 mState.depthMask = mask;
668 mMaskStateDirty = true;
669 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670}
671
672void Context::setActiveSampler(int active)
673{
674 mState.activeSampler = active;
675}
676
677GLuint Context::getFramebufferHandle() const
678{
679 return mState.framebuffer;
680}
681
682GLuint Context::getRenderbufferHandle() const
683{
684 return mState.renderbuffer;
685}
686
687GLuint Context::getArrayBufferHandle() const
688{
689 return mState.arrayBuffer;
690}
691
692void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
693{
694 mState.vertexAttribute[attribNum].mEnabled = enabled;
695}
696
697const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
698{
699 return mState.vertexAttribute[attribNum];
700}
701
702void Context::setVertexAttribState(unsigned int attribNum, GLuint boundBuffer, GLint size, GLenum type, bool normalized,
703 GLsizei stride, const void *pointer)
704{
705 mState.vertexAttribute[attribNum].mBoundBuffer = boundBuffer;
706 mState.vertexAttribute[attribNum].mSize = size;
707 mState.vertexAttribute[attribNum].mType = type;
708 mState.vertexAttribute[attribNum].mNormalized = normalized;
709 mState.vertexAttribute[attribNum].mStride = stride;
710 mState.vertexAttribute[attribNum].mPointer = pointer;
711}
712
713const void *Context::getVertexAttribPointer(unsigned int attribNum) const
714{
715 return mState.vertexAttribute[attribNum].mPointer;
716}
717
718// returns entire set of attributes as a block
719const AttributeState *Context::getVertexAttribBlock()
720{
721 return mState.vertexAttribute;
722}
723
724void Context::setPackAlignment(GLint alignment)
725{
726 mState.packAlignment = alignment;
727}
728
729GLint Context::getPackAlignment() const
730{
731 return mState.packAlignment;
732}
733
734void Context::setUnpackAlignment(GLint alignment)
735{
736 mState.unpackAlignment = alignment;
737}
738
739GLint Context::getUnpackAlignment() const
740{
741 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
744// Returns an unused buffer name
745GLuint Context::createBuffer()
746{
747 unsigned int handle = 1;
748
749 while (mBufferMap.find(handle) != mBufferMap.end())
750 {
751 handle++;
752 }
753
754 mBufferMap[handle] = NULL;
755
756 return handle;
757}
758
759// Returns an unused shader/program name
760GLuint Context::createShader(GLenum type)
761{
762 unsigned int handle = 1;
763
764 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
765 {
766 handle++;
767 }
768
769 if (type == GL_VERTEX_SHADER)
770 {
daniel@transgaming.com95124342010-04-29 03:38:58 +0000771 mShaderMap[handle] = new VertexShader(this, handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000772 }
773 else if (type == GL_FRAGMENT_SHADER)
774 {
daniel@transgaming.com95124342010-04-29 03:38:58 +0000775 mShaderMap[handle] = new FragmentShader(this, handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776 }
777 else UNREACHABLE();
778
779 return handle;
780}
781
782// Returns an unused program/shader name
783GLuint Context::createProgram()
784{
785 unsigned int handle = 1;
786
787 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
788 {
789 handle++;
790 }
791
792 mProgramMap[handle] = new Program();
793
794 return handle;
795}
796
797// Returns an unused texture name
798GLuint Context::createTexture()
799{
800 unsigned int handle = 1;
801
802 while (mTextureMap.find(handle) != mTextureMap.end())
803 {
804 handle++;
805 }
806
807 mTextureMap[handle] = NULL;
808
809 return handle;
810}
811
812// Returns an unused framebuffer name
813GLuint Context::createFramebuffer()
814{
815 unsigned int handle = 1;
816
817 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
818 {
819 handle++;
820 }
821
822 mFramebufferMap[handle] = NULL;
823
824 return handle;
825}
826
827// Returns an unused renderbuffer name
828GLuint Context::createRenderbuffer()
829{
830 unsigned int handle = 1;
831
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000832 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 {
834 handle++;
835 }
836
837 mRenderbufferMap[handle] = NULL;
838
839 return handle;
840}
841
842void Context::deleteBuffer(GLuint buffer)
843{
844 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
845
846 if (bufferObject != mBufferMap.end())
847 {
848 detachBuffer(buffer);
849
850 delete bufferObject->second;
851 mBufferMap.erase(bufferObject);
852 }
853}
854
855void Context::deleteShader(GLuint shader)
856{
857 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
858
859 if (shaderObject != mShaderMap.end())
860 {
861 if (!shaderObject->second->isAttached())
862 {
863 delete shaderObject->second;
864 mShaderMap.erase(shaderObject);
865 }
866 else
867 {
868 shaderObject->second->flagForDeletion();
869 }
870 }
871}
872
873void Context::deleteProgram(GLuint program)
874{
875 ProgramMap::iterator programObject = mProgramMap.find(program);
876
877 if (programObject != mProgramMap.end())
878 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000879 if (program != mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880 {
881 delete programObject->second;
882 mProgramMap.erase(programObject);
883 }
884 else
885 {
886 programObject->second->flagForDeletion();
887 }
888 }
889}
890
891void Context::deleteTexture(GLuint texture)
892{
893 TextureMap::iterator textureObject = mTextureMap.find(texture);
894
895 if (textureObject != mTextureMap.end())
896 {
897 detachTexture(texture);
898
899 if (texture != 0)
900 {
901 delete textureObject->second;
902 }
903
904 mTextureMap.erase(textureObject);
905 }
906}
907
908void Context::deleteFramebuffer(GLuint framebuffer)
909{
910 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
911
912 if (framebufferObject != mFramebufferMap.end())
913 {
914 detachFramebuffer(framebuffer);
915
916 delete framebufferObject->second;
917 mFramebufferMap.erase(framebufferObject);
918 }
919}
920
921void Context::deleteRenderbuffer(GLuint renderbuffer)
922{
923 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
924
925 if (renderbufferObject != mRenderbufferMap.end())
926 {
927 detachRenderbuffer(renderbuffer);
928
929 delete renderbufferObject->second;
930 mRenderbufferMap.erase(renderbufferObject);
931 }
932}
933
934void Context::bindArrayBuffer(unsigned int buffer)
935{
936 if (buffer != 0 && !getBuffer(buffer))
937 {
daniel@transgaming.comdefa1c32010-05-18 18:51:45 +0000938 mBufferMap[buffer] = new Buffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939 }
940
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000941 mState.arrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
944void Context::bindElementArrayBuffer(unsigned int buffer)
945{
946 if (buffer != 0 && !getBuffer(buffer))
947 {
daniel@transgaming.comdefa1c32010-05-18 18:51:45 +0000948 mBufferMap[buffer] = new Buffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949 }
950
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000951 mState.elementArrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindTexture2D(GLuint texture)
955{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000956 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000958 mTextureMap[texture] = new Texture2D(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959 }
960
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000961 mState.texture2D = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000963 mState.samplerTexture[SAMPLER_2D][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
966void Context::bindTextureCubeMap(GLuint texture)
967{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000968 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000970 mTextureMap[texture] = new TextureCubeMap(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971 }
972
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000973 mState.textureCubeMap = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000975 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976}
977
978void Context::bindFramebuffer(GLuint framebuffer)
979{
980 if (!getFramebuffer(framebuffer))
981 {
982 mFramebufferMap[framebuffer] = new Framebuffer();
983 }
984
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000985 mState.framebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
988void Context::bindRenderbuffer(GLuint renderbuffer)
989{
990 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
991 {
992 mRenderbufferMap[renderbuffer] = new Renderbuffer();
993 }
994
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000995 mState.renderbuffer = renderbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996}
997
998void Context::useProgram(GLuint program)
999{
1000 Program *programObject = getCurrentProgram();
1001
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001002 GLuint priorProgram = mState.currentProgram;
1003 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001004
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005 if (programObject && programObject->isFlaggedForDeletion())
1006 {
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001007 deleteProgram(priorProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
1011void Context::setFramebufferZero(Framebuffer *buffer)
1012{
1013 delete mFramebufferMap[0];
1014 mFramebufferMap[0] = buffer;
1015}
1016
1017void Context::setColorbufferZero(Colorbuffer *buffer)
1018{
1019 delete mColorbufferZero;
1020 mColorbufferZero = buffer;
1021}
1022
1023void Context::setDepthbufferZero(Depthbuffer *buffer)
1024{
1025 delete mDepthbufferZero;
1026 mDepthbufferZero = buffer;
1027}
1028
1029void Context::setStencilbufferZero(Stencilbuffer *buffer)
1030{
1031 delete mStencilbufferZero;
1032 mStencilbufferZero = buffer;
1033}
1034
1035void Context::setRenderbuffer(Renderbuffer *buffer)
1036{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001037 delete mRenderbufferMap[mState.renderbuffer];
1038 mRenderbufferMap[mState.renderbuffer] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039}
1040
1041Buffer *Context::getBuffer(unsigned int handle)
1042{
1043 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001044
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045 if (buffer == mBufferMap.end())
1046 {
1047 return NULL;
1048 }
1049 else
1050 {
1051 return buffer->second;
1052 }
1053}
1054
1055Shader *Context::getShader(unsigned int handle)
1056{
1057 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001058
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059 if (shader == mShaderMap.end())
1060 {
1061 return NULL;
1062 }
1063 else
1064 {
1065 return shader->second;
1066 }
1067}
1068
1069Program *Context::getProgram(unsigned int handle)
1070{
1071 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001072
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073 if (program == mProgramMap.end())
1074 {
1075 return NULL;
1076 }
1077 else
1078 {
1079 return program->second;
1080 }
1081}
1082
1083Texture *Context::getTexture(unsigned int handle)
1084{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001085 if (handle == 0) return NULL;
1086
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001088
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089 if (texture == mTextureMap.end())
1090 {
1091 return NULL;
1092 }
1093 else
1094 {
1095 return texture->second;
1096 }
1097}
1098
1099Framebuffer *Context::getFramebuffer(unsigned int handle)
1100{
1101 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103 if (framebuffer == mFramebufferMap.end())
1104 {
1105 return NULL;
1106 }
1107 else
1108 {
1109 return framebuffer->second;
1110 }
1111}
1112
1113Renderbuffer *Context::getRenderbuffer(unsigned int handle)
1114{
1115 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117 if (renderbuffer == mRenderbufferMap.end())
1118 {
1119 return NULL;
1120 }
1121 else
1122 {
1123 return renderbuffer->second;
1124 }
1125}
1126
1127Colorbuffer *Context::getColorbuffer(GLuint handle)
1128{
1129 if (handle != 0)
1130 {
1131 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1132
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001133 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134 {
1135 return static_cast<Colorbuffer*>(renderbuffer);
1136 }
1137 }
1138 else // Special case: 0 refers to different initial render targets based on the attachment type
1139 {
1140 return mColorbufferZero;
1141 }
1142
1143 return NULL;
1144}
1145
1146Depthbuffer *Context::getDepthbuffer(GLuint handle)
1147{
1148 if (handle != 0)
1149 {
1150 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1151
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001152 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153 {
1154 return static_cast<Depthbuffer*>(renderbuffer);
1155 }
1156 }
1157 else // Special case: 0 refers to different initial render targets based on the attachment type
1158 {
1159 return mDepthbufferZero;
1160 }
1161
1162 return NULL;
1163}
1164
1165Stencilbuffer *Context::getStencilbuffer(GLuint handle)
1166{
1167 if (handle != 0)
1168 {
1169 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1170
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001171 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172 {
1173 return static_cast<Stencilbuffer*>(renderbuffer);
1174 }
1175 }
1176 else
1177 {
1178 return mStencilbufferZero;
1179 }
1180
1181 return NULL;
1182}
1183
1184Buffer *Context::getArrayBuffer()
1185{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001186 return getBuffer(mState.arrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187}
1188
1189Buffer *Context::getElementArrayBuffer()
1190{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001191 return getBuffer(mState.elementArrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001192}
1193
1194Program *Context::getCurrentProgram()
1195{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001196 return getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001197}
1198
1199Texture2D *Context::getTexture2D()
1200{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001201 if (mState.texture2D == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001202 {
1203 return mTexture2DZero;
1204 }
1205
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001206 return (Texture2D*)getTexture(mState.texture2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001207}
1208
1209TextureCubeMap *Context::getTextureCubeMap()
1210{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001211 if (mState.textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001212 {
1213 return mTextureCubeMapZero;
1214 }
1215
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001216 return (TextureCubeMap*)getTexture(mState.textureCubeMap);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001217}
1218
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001219Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001221 GLuint texid = mState.samplerTexture[type][sampler];
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001222
1223 if (texid == 0)
1224 {
1225 switch (type)
1226 {
1227 default: UNREACHABLE();
1228 case SAMPLER_2D: return mTexture2DZero;
1229 case SAMPLER_CUBE: return mTextureCubeMapZero;
1230 }
1231 }
1232
1233 return getTexture(texid);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001234}
1235
1236Framebuffer *Context::getFramebuffer()
1237{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001238 return getFramebuffer(mState.framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239}
1240
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001241bool Context::getBooleanv(GLenum pname, GLboolean *params)
1242{
1243 switch (pname)
1244 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001245 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1246 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1247 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001248 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001249 params[0] = mState.colorMaskRed;
1250 params[1] = mState.colorMaskGreen;
1251 params[2] = mState.colorMaskBlue;
1252 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001253 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001254 case GL_CULL_FACE: *params = mState.cullFace;
1255 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1256 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1257 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1258 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1259 case GL_STENCIL_TEST: *params = mState.stencilTest;
1260 case GL_DEPTH_TEST: *params = mState.depthTest;
1261 case GL_BLEND: *params = mState.blend;
1262 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001263 default:
1264 return false;
1265 }
1266
1267 return true;
1268}
1269
1270bool Context::getFloatv(GLenum pname, GLfloat *params)
1271{
1272 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1273 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1274 // GetIntegerv as its native query function. As it would require conversion in any
1275 // case, this should make no difference to the calling application.
1276 switch (pname)
1277 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001278 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1279 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1280 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1281 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1282 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001283 case GL_ALIASED_LINE_WIDTH_RANGE:
1284 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1285 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1286 break;
1287 case GL_ALIASED_POINT_SIZE_RANGE:
1288 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1289 params[1] = gl::ALIASED_POINT_SIZE_RANGE_MAX;
1290 break;
1291 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001292 params[0] = mState.zNear;
1293 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001294 break;
1295 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001296 params[0] = mState.colorClearValue.red;
1297 params[1] = mState.colorClearValue.green;
1298 params[2] = mState.colorClearValue.blue;
1299 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001300 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001301 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001302 params[0] = mState.blendColor.red;
1303 params[1] = mState.blendColor.green;
1304 params[2] = mState.blendColor.blue;
1305 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001306 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001307 default:
1308 return false;
1309 }
1310
1311 return true;
1312}
1313
1314bool Context::getIntegerv(GLenum pname, GLint *params)
1315{
1316 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1317 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1318 // GetIntegerv as its native query function. As it would require conversion in any
1319 // case, this should make no difference to the calling application. You may find it in
1320 // Context::getFloatv.
1321 switch (pname)
1322 {
1323 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1324 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1325 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1326 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1327 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1328 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1329 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1330 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001331 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1332 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1333 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1334 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1335 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer; break;
1336 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer; break;
1337 case GL_FRAMEBUFFER_BINDING: *params = mState.framebuffer; break;
1338 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer; break;
1339 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1340 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1341 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1342 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1343 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1344 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1345 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1346 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1347 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1348 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1349 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1350 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1351 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1352 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1353 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1354 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1355 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1356 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1357 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1358 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1359 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1360 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1361 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1362 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1363 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1364 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1365 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1366 case GL_SUBPIXEL_BITS: *params = 4; break;
1367 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1368 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
1369 case GL_SAMPLE_BUFFERS: *params = 0; break;
1370 case GL_SAMPLES: *params = 0; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1372 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1373 case GL_MAX_VIEWPORT_DIMS:
1374 {
1375 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1376 params[0] = maxDimension;
1377 params[1] = maxDimension;
1378 }
1379 break;
1380 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001381 params[0] = mState.viewportX;
1382 params[1] = mState.viewportY;
1383 params[2] = mState.viewportWidth;
1384 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385 break;
1386 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001387 params[0] = mState.scissorX;
1388 params[1] = mState.scissorY;
1389 params[2] = mState.scissorWidth;
1390 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001392 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1393 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001394 case GL_RED_BITS:
1395 case GL_GREEN_BITS:
1396 case GL_BLUE_BITS:
1397 case GL_ALPHA_BITS:
1398 {
1399 gl::Framebuffer *framebuffer = getFramebuffer();
1400 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1401
1402 if (colorbuffer)
1403 {
1404 switch (pname)
1405 {
1406 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1407 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1408 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1409 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1410 }
1411 }
1412 else
1413 {
1414 *params = 0;
1415 }
1416 }
1417 break;
1418 case GL_DEPTH_BITS:
1419 {
1420 gl::Framebuffer *framebuffer = getFramebuffer();
1421 gl::Depthbuffer *depthbuffer = framebuffer->getDepthbuffer();
1422
1423 if (depthbuffer)
1424 {
1425 *params = depthbuffer->getDepthSize();
1426 }
1427 else
1428 {
1429 *params = 0;
1430 }
1431 }
1432 break;
1433 case GL_STENCIL_BITS:
1434 {
1435 gl::Framebuffer *framebuffer = getFramebuffer();
1436 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1437
1438 if (stencilbuffer)
1439 {
1440 *params = stencilbuffer->getStencilSize();
1441 }
1442 else
1443 {
1444 *params = 0;
1445 }
1446 }
1447 break;
1448 case GL_TEXTURE_BINDING_2D:
1449 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001450 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001451 {
1452 error(GL_INVALID_OPERATION);
1453 return false;
1454 }
1455
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001456 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001457 }
1458 break;
1459 case GL_TEXTURE_BINDING_CUBE_MAP:
1460 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001461 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001462 {
1463 error(GL_INVALID_OPERATION);
1464 return false;
1465 }
1466
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001467 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001468 }
1469 break;
1470 default:
1471 return false;
1472 }
1473
1474 return true;
1475}
1476
1477bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1478{
1479 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1480 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1481 // to the fact that it is stored internally as a float, and so would require conversion
1482 // if returned from Context::getIntegerv. Since this conversion is already implemented
1483 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1484 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1485 // application.
1486 switch (pname)
1487 {
1488 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1489 case GL_SHADER_BINARY_FORMATS:
1490 {
1491 *type = GL_INT;
1492 *numParams = 0;
1493 }
1494 break;
1495 case GL_MAX_VERTEX_ATTRIBS:
1496 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1497 case GL_MAX_VARYING_VECTORS:
1498 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1499 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1500 case GL_MAX_TEXTURE_IMAGE_UNITS:
1501 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1502 case GL_MAX_RENDERBUFFER_SIZE:
1503 case GL_NUM_SHADER_BINARY_FORMATS:
1504 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1505 case GL_ARRAY_BUFFER_BINDING:
1506 case GL_FRAMEBUFFER_BINDING:
1507 case GL_RENDERBUFFER_BINDING:
1508 case GL_CURRENT_PROGRAM:
1509 case GL_PACK_ALIGNMENT:
1510 case GL_UNPACK_ALIGNMENT:
1511 case GL_GENERATE_MIPMAP_HINT:
1512 case GL_RED_BITS:
1513 case GL_GREEN_BITS:
1514 case GL_BLUE_BITS:
1515 case GL_ALPHA_BITS:
1516 case GL_DEPTH_BITS:
1517 case GL_STENCIL_BITS:
1518 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1519 case GL_CULL_FACE_MODE:
1520 case GL_FRONT_FACE:
1521 case GL_ACTIVE_TEXTURE:
1522 case GL_STENCIL_FUNC:
1523 case GL_STENCIL_VALUE_MASK:
1524 case GL_STENCIL_REF:
1525 case GL_STENCIL_FAIL:
1526 case GL_STENCIL_PASS_DEPTH_FAIL:
1527 case GL_STENCIL_PASS_DEPTH_PASS:
1528 case GL_STENCIL_BACK_FUNC:
1529 case GL_STENCIL_BACK_VALUE_MASK:
1530 case GL_STENCIL_BACK_REF:
1531 case GL_STENCIL_BACK_FAIL:
1532 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1533 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1534 case GL_DEPTH_FUNC:
1535 case GL_BLEND_SRC_RGB:
1536 case GL_BLEND_SRC_ALPHA:
1537 case GL_BLEND_DST_RGB:
1538 case GL_BLEND_DST_ALPHA:
1539 case GL_BLEND_EQUATION_RGB:
1540 case GL_BLEND_EQUATION_ALPHA:
1541 case GL_STENCIL_WRITEMASK:
1542 case GL_STENCIL_BACK_WRITEMASK:
1543 case GL_STENCIL_CLEAR_VALUE:
1544 case GL_SUBPIXEL_BITS:
1545 case GL_MAX_TEXTURE_SIZE:
1546 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1547 case GL_SAMPLE_BUFFERS:
1548 case GL_SAMPLES:
1549 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1550 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1551 case GL_TEXTURE_BINDING_2D:
1552 case GL_TEXTURE_BINDING_CUBE_MAP:
1553 {
1554 *type = GL_INT;
1555 *numParams = 1;
1556 }
1557 break;
1558 case GL_MAX_VIEWPORT_DIMS:
1559 {
1560 *type = GL_INT;
1561 *numParams = 2;
1562 }
1563 break;
1564 case GL_VIEWPORT:
1565 case GL_SCISSOR_BOX:
1566 {
1567 *type = GL_INT;
1568 *numParams = 4;
1569 }
1570 break;
1571 case GL_SHADER_COMPILER:
1572 case GL_SAMPLE_COVERAGE_INVERT:
1573 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001574 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1575 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1576 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1577 case GL_SAMPLE_COVERAGE:
1578 case GL_SCISSOR_TEST:
1579 case GL_STENCIL_TEST:
1580 case GL_DEPTH_TEST:
1581 case GL_BLEND:
1582 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001583 {
1584 *type = GL_BOOL;
1585 *numParams = 1;
1586 }
1587 break;
1588 case GL_COLOR_WRITEMASK:
1589 {
1590 *type = GL_BOOL;
1591 *numParams = 4;
1592 }
1593 break;
1594 case GL_POLYGON_OFFSET_FACTOR:
1595 case GL_POLYGON_OFFSET_UNITS:
1596 case GL_SAMPLE_COVERAGE_VALUE:
1597 case GL_DEPTH_CLEAR_VALUE:
1598 case GL_LINE_WIDTH:
1599 {
1600 *type = GL_FLOAT;
1601 *numParams = 1;
1602 }
1603 break;
1604 case GL_ALIASED_LINE_WIDTH_RANGE:
1605 case GL_ALIASED_POINT_SIZE_RANGE:
1606 case GL_DEPTH_RANGE:
1607 {
1608 *type = GL_FLOAT;
1609 *numParams = 2;
1610 }
1611 break;
1612 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001613 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001614 {
1615 *type = GL_FLOAT;
1616 *numParams = 4;
1617 }
1618 break;
1619 default:
1620 return false;
1621 }
1622
1623 return true;
1624}
1625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626// Applies the render target surface, depth stencil surface, viewport rectangle and
1627// scissor rectangle to the Direct3D 9 device
1628bool Context::applyRenderTarget(bool ignoreViewport)
1629{
1630 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001631
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632 Framebuffer *framebufferObject = getFramebuffer();
1633
1634 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1635 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001636 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1637
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638 return false;
1639 }
1640
1641 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1642 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1643
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001644 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1645 if (renderTargetSerial != mAppliedRenderTargetSerial)
1646 {
1647 device->SetRenderTarget(0, renderTarget);
1648 mAppliedRenderTargetSerial = renderTargetSerial;
1649 }
1650
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001651 unsigned int depthbufferSerial = framebufferObject->getDepthbufferSerial();
1652 if (depthbufferSerial != mAppliedDepthbufferSerial)
1653 {
1654 device->SetDepthStencilSurface(depthStencil);
1655 mAppliedDepthbufferSerial = depthbufferSerial;
1656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657
1658 D3DVIEWPORT9 viewport;
1659 D3DSURFACE_DESC desc;
1660 renderTarget->GetDesc(&desc);
1661
1662 if (ignoreViewport)
1663 {
1664 viewport.X = 0;
1665 viewport.Y = 0;
1666 viewport.Width = desc.Width;
1667 viewport.Height = desc.Height;
1668 viewport.MinZ = 0.0f;
1669 viewport.MaxZ = 1.0f;
1670 }
1671 else
1672 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001673 viewport.X = std::max(mState.viewportX, 0);
1674 viewport.Y = std::max(mState.viewportY, 0);
1675 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1676 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1677 viewport.MinZ = clamp01(mState.zNear);
1678 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001679 }
1680
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001681 if (viewport.Width <= 0 || viewport.Height <= 0)
1682 {
1683 return false; // Nothing to render
1684 }
1685
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686 device->SetViewport(&viewport);
1687
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001688 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001690 if (mState.scissorTest)
1691 {
1692 RECT rect = {mState.scissorX,
1693 mState.scissorY,
1694 mState.scissorX + mState.scissorWidth,
1695 mState.scissorY + mState.scissorHeight};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001697 device->SetScissorRect(&rect);
1698 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1699 }
1700 else
1701 {
1702 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1703 }
1704
1705 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 }
1707
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001708 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001710 Program *programObject = getCurrentProgram();
1711
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001712 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001713 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001715
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001716 GLint window = programObject->getDxWindowLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001717 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1718 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1719 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001720 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001723 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001724 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1725
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001726 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001727 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001728
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001729 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001730 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001733 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001734 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
1736
1737 return true;
1738}
1739
1740// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001741void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742{
1743 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001744 Program *programObject = getCurrentProgram();
1745
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001746 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001747 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001748 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001750 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001751 GLint alwaysFront = !isTriangleMode(drawMode);
1752 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1753
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760 else
1761 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
1764
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 mCullStateDirty = false;
1766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001768 if (mDepthStateDirty)
1769 {
1770 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1773 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 }
1775 else
1776 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001777 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001779
1780 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781 }
1782
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001783 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001785 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001786 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001787 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1788
1789 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1790 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1791 {
1792 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1793 }
1794 else
1795 {
1796 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1797 unorm<8>(mState.blendColor.alpha),
1798 unorm<8>(mState.blendColor.alpha),
1799 unorm<8>(mState.blendColor.alpha)));
1800 }
1801
1802 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1803 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1804 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1805
1806 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1807 mState.destBlendRGB != mState.destBlendAlpha ||
1808 mState.blendEquationRGB != mState.blendEquationAlpha)
1809 {
1810 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1811
1812 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1813 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1814 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1815
1816 }
1817 else
1818 {
1819 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1820 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001821 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001823 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001825 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826
1827 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828 }
1829
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830 if (mStencilStateDirty || mFrontFaceDirty)
1831 {
1832 if (mState.stencilTest && hasStencil())
1833 {
1834 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1835 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1836
1837 // FIXME: Unsupported by D3D9
1838 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1839 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1840 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1841 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1842 mState.stencilRef != mState.stencilBackRef ||
1843 mState.stencilMask != mState.stencilBackMask)
1844 {
1845 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1846 return error(GL_INVALID_OPERATION);
1847 }
1848
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001849 // get the maximum size of the stencil ref
1850 gl::Framebuffer *framebuffer = getFramebuffer();
1851 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1852 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1853
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1855 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1856 es2dx::ConvertComparison(mState.stencilFunc));
1857
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001858 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 +00001859 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1860
1861 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1862 es2dx::ConvertStencilOp(mState.stencilFail));
1863 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1864 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1865 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1866 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1867
1868 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1869 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1870 es2dx::ConvertComparison(mState.stencilBackFunc));
1871
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001872 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 +00001873 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1874
1875 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1876 es2dx::ConvertStencilOp(mState.stencilBackFail));
1877 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1878 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1879 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1880 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1881 }
1882 else
1883 {
1884 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1885 }
1886
1887 mStencilStateDirty = false;
1888 }
1889
1890 if (mMaskStateDirty)
1891 {
1892 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1893 mState.colorMaskBlue, mState.colorMaskAlpha));
1894 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1895
1896 mMaskStateDirty = false;
1897 }
1898
1899 if (mPolygonOffsetStateDirty)
1900 {
1901 if (mState.polygonOffsetFill)
1902 {
1903 gl::Depthbuffer *depthbuffer = getFramebuffer()->getDepthbuffer();
1904 if (depthbuffer)
1905 {
1906 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1907 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1908 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1909 }
1910 }
1911 else
1912 {
1913 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1914 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1915 }
1916
1917 mPolygonOffsetStateDirty = false;
1918 }
1919
1920 if (mConfig->mMultiSample != 0 && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001922 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001923 {
1924 FIXME("Sample alpha to coverage is unimplemented.");
1925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001927 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001928 {
1929 FIXME("Sample coverage is unimplemented.");
1930 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001931
1932 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 }
1934
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001935 if (mDitherStateDirty)
1936 {
1937 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1938
1939 mDitherStateDirty = false;
1940 }
1941
1942 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943}
1944
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001945// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001946void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001948 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001950 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001952 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 }
1954 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001955}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001957GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001958{
1959 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1960
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001961 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001962 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001963 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001964 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001965 }
1966
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001967 lookupAttributeMapping(translated);
1968
1969 mBufferBackEnd->setupAttributesPreDraw(translated);
1970
1971 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1972 {
1973 if (translated[i].enabled && translated[i].nonArray)
1974 {
1975 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1976 if (err != GL_NO_ERROR)
1977 {
1978 return err;
1979 }
1980
1981 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1982
1983 *useIndexing = true;
1984 return GL_NO_ERROR;
1985 }
1986 }
1987
1988 *useIndexing = false;
1989 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001990}
1991
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001992GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001993{
1994 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1995
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001996 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001997
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001998 if (err == GL_NO_ERROR)
1999 {
2000 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002001
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002002 mBufferBackEnd->setupAttributesPreDraw(translated);
2003 }
2004
2005 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002006}
2007
2008// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002009GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002010{
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002011 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, getBuffer(mState.elementArrayBuffer), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002012
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002013 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002014 {
2015 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2016 }
2017
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002018 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019}
2020
2021// Applies the shaders and shader constants to the Direct3D 9 device
2022void Context::applyShaders()
2023{
2024 IDirect3DDevice9 *device = getDevice();
2025 Program *programObject = getCurrentProgram();
2026 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2027 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2028
2029 device->SetVertexShader(vertexShader);
2030 device->SetPixelShader(pixelShader);
2031
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002032 if (programObject->getSerial() != mAppliedProgram)
2033 {
2034 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002035 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002036 mAppliedProgram = programObject->getSerial();
2037 }
2038
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039 programObject->applyUniforms();
2040}
2041
2042// Applies the textures and sampler states to the Direct3D 9 device
2043void Context::applyTextures()
2044{
2045 IDirect3DDevice9 *device = getDevice();
2046 Program *programObject = getCurrentProgram();
2047
2048 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2049 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002050 int textureUnit = programObject->getSamplerMapping(sampler);
2051 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002053 SamplerType textureType = programObject->getSamplerType(sampler);
2054
2055 Texture *texture = getSamplerTexture(textureUnit, textureType);
2056
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002057 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002058 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002059 if (texture->isComplete())
2060 {
2061 GLenum wrapS = texture->getWrapS();
2062 GLenum wrapT = texture->getWrapT();
2063 GLenum minFilter = texture->getMinFilter();
2064 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002066 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2067 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002069 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2070 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2071 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2072 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2073 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002075 device->SetTexture(sampler, texture->getTexture());
2076 }
2077 else
2078 {
2079 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2080 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002081 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002082
2083 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002085 else
2086 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002087 if (programObject->isSamplerDirty(sampler))
2088 {
2089 device->SetTexture(sampler, NULL);
2090 programObject->setSamplerDirty(sampler, false);
2091 }
2092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 }
2094}
2095
2096void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2097{
2098 Framebuffer *framebuffer = getFramebuffer();
2099 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2100 IDirect3DDevice9 *device = getDevice();
2101
2102 D3DSURFACE_DESC desc;
2103 renderTarget->GetDesc(&desc);
2104
2105 IDirect3DSurface9 *systemSurface;
2106 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2107
2108 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2109 {
2110 return error(GL_OUT_OF_MEMORY);
2111 }
2112
2113 ASSERT(SUCCEEDED(result));
2114
2115 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2116 {
2117 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2118 }
2119
2120 result = device->GetRenderTargetData(renderTarget, systemSurface);
2121
2122 if (result == D3DERR_DRIVERINTERNALERROR)
2123 {
2124 systemSurface->Release();
2125
2126 return error(GL_OUT_OF_MEMORY);
2127 }
2128
2129 if (FAILED(result))
2130 {
2131 UNREACHABLE();
2132 systemSurface->Release();
2133
2134 return; // No sensible error to generate
2135 }
2136
2137 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002138 RECT rect = {std::max(x, 0),
2139 std::max(y, 0),
2140 std::min(x + width, (int)desc.Width),
2141 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142
2143 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2144
2145 if (FAILED(result))
2146 {
2147 UNREACHABLE();
2148 systemSurface->Release();
2149
2150 return; // No sensible error to generate
2151 }
2152
2153 unsigned char *source = (unsigned char*)lock.pBits;
2154 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002155 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002157 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002158
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 for (int j = 0; j < rect.bottom - rect.top; j++)
2160 {
2161 for (int i = 0; i < rect.right - rect.left; i++)
2162 {
2163 float r;
2164 float g;
2165 float b;
2166 float a;
2167
2168 switch (desc.Format)
2169 {
2170 case D3DFMT_R5G6B5:
2171 {
2172 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2173
2174 a = 1.0f;
2175 b = (rgb & 0x001F) * (1.0f / 0x001F);
2176 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2177 r = (rgb & 0xF800) * (1.0f / 0xF800);
2178 }
2179 break;
2180 case D3DFMT_X1R5G5B5:
2181 {
2182 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2183
2184 a = 1.0f;
2185 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2186 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2187 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2188 }
2189 break;
2190 case D3DFMT_A1R5G5B5:
2191 {
2192 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2193
2194 a = (argb & 0x8000) ? 1.0f : 0.0f;
2195 b = (argb & 0x001F) * (1.0f / 0x001F);
2196 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2197 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2198 }
2199 break;
2200 case D3DFMT_A8R8G8B8:
2201 {
2202 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2203
2204 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2205 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2206 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2207 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2208 }
2209 break;
2210 case D3DFMT_X8R8G8B8:
2211 {
2212 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2213
2214 a = 1.0f;
2215 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2216 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2217 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2218 }
2219 break;
2220 case D3DFMT_A2R10G10B10:
2221 {
2222 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2223
2224 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2225 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2226 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2227 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2228 }
2229 break;
2230 default:
2231 UNIMPLEMENTED(); // FIXME
2232 UNREACHABLE();
2233 }
2234
2235 switch (format)
2236 {
2237 case GL_RGBA:
2238 switch (type)
2239 {
2240 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002241 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2242 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2243 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2244 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 break;
2246 default: UNREACHABLE();
2247 }
2248 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002249 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 switch (type)
2251 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002252 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002253 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2254 ((unsigned short)(31 * b + 0.5f) << 0) |
2255 ((unsigned short)(63 * g + 0.5f) << 5) |
2256 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 break;
2258 default: UNREACHABLE();
2259 }
2260 break;
2261 default: UNREACHABLE();
2262 }
2263 }
2264 }
2265
2266 systemSurface->UnlockRect();
2267
2268 systemSurface->Release();
2269}
2270
2271void Context::clear(GLbitfield mask)
2272{
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002273 Framebuffer *framebufferObject = getFramebuffer();
2274
2275 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2276 {
2277 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2278
2279 return;
2280 }
2281
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002282 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283 IDirect3DDevice9 *device = getDevice();
2284 DWORD flags = 0;
2285
2286 if (mask & GL_COLOR_BUFFER_BIT)
2287 {
2288 mask &= ~GL_COLOR_BUFFER_BIT;
2289 flags |= D3DCLEAR_TARGET;
2290 }
2291
2292 if (mask & GL_DEPTH_BUFFER_BIT)
2293 {
2294 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002295 if (mState.depthMask)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 {
2297 flags |= D3DCLEAR_ZBUFFER;
2298 }
2299 }
2300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
2302
2303 GLuint stencilUnmasked = 0x0;
2304
2305 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
2306 {
2307 D3DSURFACE_DESC desc;
2308 depthStencil->GetDesc(&desc);
2309
2310 mask &= ~GL_STENCIL_BUFFER_BIT;
2311 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2312 stencilUnmasked = (0x1 << stencilSize) - 1;
2313
2314 if (stencilUnmasked != 0x0)
2315 {
2316 flags |= D3DCLEAR_STENCIL;
2317 }
2318 }
2319
2320 if (mask != 0)
2321 {
2322 return error(GL_INVALID_VALUE);
2323 }
2324
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002325 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2326 {
2327 return;
2328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002330 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2331 unorm<8>(mState.colorClearValue.red),
2332 unorm<8>(mState.colorClearValue.green),
2333 unorm<8>(mState.colorClearValue.blue));
2334 float depth = clamp01(mState.depthClearValue);
2335 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
2337 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2338
2339 D3DSURFACE_DESC desc;
2340 renderTarget->GetDesc(&desc);
2341
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002342 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343
2344 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002345 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002347 !(mState.colorMaskRed && mState.colorMaskGreen &&
2348 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349
2350 if (needMaskedColorClear || needMaskedStencilClear)
2351 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002352 // State which is altered in all paths from this point to the clear call is saved.
2353 // State which is altered in only some paths will be flagged dirty in the case that
2354 // that path is taken.
2355 HRESULT hr;
2356 if (mMaskedClearSavedState == NULL)
2357 {
2358 hr = device->BeginStateBlock();
2359 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2360
2361 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2362 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2363 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2364 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2365 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2366 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2367 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2368 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2369 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2370 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2371 device->SetPixelShader(NULL);
2372 device->SetVertexShader(NULL);
2373 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2374 device->SetStreamSourceFreq(0, 1);
2375
2376 hr = device->EndStateBlock(&mMaskedClearSavedState);
2377 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2378 }
2379
2380 ASSERT(mMaskedClearSavedState != NULL);
2381
2382 if (mMaskedClearSavedState != NULL)
2383 {
2384 hr = mMaskedClearSavedState->Capture();
2385 ASSERT(SUCCEEDED(hr));
2386 }
2387
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2389 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2390 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2391 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2392 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2393 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2394 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2395 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2396
2397 if (flags & D3DCLEAR_TARGET)
2398 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002399 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2400 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2401 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2402 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 }
2404 else
2405 {
2406 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2407 }
2408
2409 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2410 {
2411 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2412 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2413 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2414 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002415 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002416 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2418 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002419 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 }
2421 else
2422 {
2423 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2424 }
2425
2426 device->SetPixelShader(NULL);
2427 device->SetVertexShader(NULL);
2428 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002429 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002431 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 {
2433 float x, y, z, w;
2434 D3DCOLOR diffuse;
2435 };
2436
2437 Vertex quad[4];
2438 quad[0].x = 0.0f;
2439 quad[0].y = (float)desc.Height;
2440 quad[0].z = 0.0f;
2441 quad[0].w = 1.0f;
2442 quad[0].diffuse = color;
2443
2444 quad[1].x = (float)desc.Width;
2445 quad[1].y = (float)desc.Height;
2446 quad[1].z = 0.0f;
2447 quad[1].w = 1.0f;
2448 quad[1].diffuse = color;
2449
2450 quad[2].x = 0.0f;
2451 quad[2].y = 0.0f;
2452 quad[2].z = 0.0f;
2453 quad[2].w = 1.0f;
2454 quad[2].diffuse = color;
2455
2456 quad[3].x = (float)desc.Width;
2457 quad[3].y = 0.0f;
2458 quad[3].z = 0.0f;
2459 quad[3].w = 1.0f;
2460 quad[3].diffuse = color;
2461
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002462 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464
2465 if (flags & D3DCLEAR_ZBUFFER)
2466 {
2467 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2468 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2469 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2470 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002471
2472 if (mMaskedClearSavedState != NULL)
2473 {
2474 mMaskedClearSavedState->Apply();
2475 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002477 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 {
2479 device->Clear(0, NULL, flags, color, depth, stencil);
2480 }
2481}
2482
2483void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2484{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002485 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486 {
2487 return error(GL_INVALID_OPERATION);
2488 }
2489
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002490 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 IDirect3DDevice9 *device = getDevice();
2492 D3DPRIMITIVETYPE primitiveType;
2493 int primitiveCount;
2494
2495 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2496 return error(GL_INVALID_ENUM);
2497
2498 if (primitiveCount <= 0)
2499 {
2500 return;
2501 }
2502
2503 if (!applyRenderTarget(false))
2504 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002505 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506 }
2507
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002508 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002509
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002510 TranslatedIndexData indexInfo;
2511 bool useIndexing;
2512 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002513 if (err != GL_NO_ERROR)
2514 {
2515 return error(err);
2516 }
2517
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 applyShaders();
2519 applyTextures();
2520
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002521 if (!getCurrentProgram()->validateSamplers())
2522 {
2523 return error(GL_INVALID_OPERATION);
2524 }
2525
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002526 if (!cullSkipsDraw(mode))
2527 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002528 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002529 if (useIndexing)
2530 {
2531 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2532 }
2533 else
2534 {
2535 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2536 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538}
2539
2540void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2541{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002542 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 {
2544 return error(GL_INVALID_OPERATION);
2545 }
2546
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002547 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548 {
2549 return error(GL_INVALID_OPERATION);
2550 }
2551
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002552 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 IDirect3DDevice9 *device = getDevice();
2554 D3DPRIMITIVETYPE primitiveType;
2555 int primitiveCount;
2556
2557 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2558 return error(GL_INVALID_ENUM);
2559
2560 if (primitiveCount <= 0)
2561 {
2562 return;
2563 }
2564
2565 if (!applyRenderTarget(false))
2566 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002567 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568 }
2569
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002570 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002571
2572 TranslatedIndexData indexInfo;
2573 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2574 if (err != GL_NO_ERROR)
2575 {
2576 return error(err);
2577 }
2578
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002579 err = applyVertexBuffer(indexInfo);
2580 if (err != GL_NO_ERROR)
2581 {
2582 return error(err);
2583 }
2584
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002585 applyShaders();
2586 applyTextures();
2587
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002588 if (!getCurrentProgram()->validateSamplers())
2589 {
2590 return error(GL_INVALID_OPERATION);
2591 }
2592
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002593 if (!cullSkipsDraw(mode))
2594 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002595 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002596 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 +00002597 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598}
2599
2600void Context::finish()
2601{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002602 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 IDirect3DDevice9 *device = getDevice();
2604 IDirect3DQuery9 *occlusionQuery = NULL;
2605
2606 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2607
2608 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2609 {
2610 return error(GL_OUT_OF_MEMORY);
2611 }
2612
2613 ASSERT(SUCCEEDED(result));
2614
2615 if (occlusionQuery)
2616 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002617 IDirect3DStateBlock9 *savedState = NULL;
2618 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2619
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 occlusionQuery->Issue(D3DISSUE_BEGIN);
2621
2622 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002623 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 device->SetPixelShader(NULL);
2625 device->SetVertexShader(NULL);
2626 device->SetFVF(D3DFVF_XYZRHW);
2627 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002628 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630
2631 occlusionQuery->Issue(D3DISSUE_END);
2632
2633 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2634 {
2635 // Keep polling, but allow other threads to do something useful first
2636 Sleep(0);
2637 }
2638
2639 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002640
2641 if (savedState)
2642 {
2643 savedState->Apply();
2644 savedState->Release();
2645 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 }
2647}
2648
2649void Context::flush()
2650{
2651 IDirect3DDevice9 *device = getDevice();
2652 IDirect3DQuery9 *eventQuery = NULL;
2653
2654 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2655
2656 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2657 {
2658 return error(GL_OUT_OF_MEMORY);
2659 }
2660
2661 ASSERT(SUCCEEDED(result));
2662
2663 if (eventQuery)
2664 {
2665 eventQuery->Issue(D3DISSUE_END);
2666
2667 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2668 {
2669 // Keep polling, but allow other threads to do something useful first
2670 Sleep(0);
2671 }
2672
2673 eventQuery->Release();
2674 }
2675}
2676
2677void Context::recordInvalidEnum()
2678{
2679 mInvalidEnum = true;
2680}
2681
2682void Context::recordInvalidValue()
2683{
2684 mInvalidValue = true;
2685}
2686
2687void Context::recordInvalidOperation()
2688{
2689 mInvalidOperation = true;
2690}
2691
2692void Context::recordOutOfMemory()
2693{
2694 mOutOfMemory = true;
2695}
2696
2697void Context::recordInvalidFramebufferOperation()
2698{
2699 mInvalidFramebufferOperation = true;
2700}
2701
2702// Get one of the recorded errors and clear its flag, if any.
2703// [OpenGL ES 2.0.24] section 2.5 page 13.
2704GLenum Context::getError()
2705{
2706 if (mInvalidEnum)
2707 {
2708 mInvalidEnum = false;
2709
2710 return GL_INVALID_ENUM;
2711 }
2712
2713 if (mInvalidValue)
2714 {
2715 mInvalidValue = false;
2716
2717 return GL_INVALID_VALUE;
2718 }
2719
2720 if (mInvalidOperation)
2721 {
2722 mInvalidOperation = false;
2723
2724 return GL_INVALID_OPERATION;
2725 }
2726
2727 if (mOutOfMemory)
2728 {
2729 mOutOfMemory = false;
2730
2731 return GL_OUT_OF_MEMORY;
2732 }
2733
2734 if (mInvalidFramebufferOperation)
2735 {
2736 mInvalidFramebufferOperation = false;
2737
2738 return GL_INVALID_FRAMEBUFFER_OPERATION;
2739 }
2740
2741 return GL_NO_ERROR;
2742}
2743
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002744const char *Context::getPixelShaderProfile()
2745{
2746 return mPsProfile;
2747}
2748
2749const char *Context::getVertexShaderProfile()
2750{
2751 return mVsProfile;
2752}
2753
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754void Context::detachBuffer(GLuint buffer)
2755{
2756 // [OpenGL ES 2.0.24] section 2.9 page 22:
2757 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2758 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2759
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002760 if (mState.arrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002762 mState.arrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002763 }
2764
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002765 if (mState.elementArrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002767 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 }
2769
2770 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2771 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002772 if (mState.vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002774 mState.vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775 }
2776 }
2777}
2778
2779void Context::detachTexture(GLuint texture)
2780{
2781 // [OpenGL ES 2.0.24] section 3.8 page 84:
2782 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2783 // rebound to texture object zero
2784
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002785 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002787 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002789 if (mState.samplerTexture[type][sampler] == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002790 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002791 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002793 }
2794 }
2795
2796 // [OpenGL ES 2.0.24] section 4.4 page 112:
2797 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2798 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2799 // image was attached in the currently bound framebuffer.
2800
2801 Framebuffer *framebuffer = getFramebuffer();
2802
2803 if (framebuffer)
2804 {
2805 framebuffer->detachTexture(texture);
2806 }
2807}
2808
2809void Context::detachFramebuffer(GLuint framebuffer)
2810{
2811 // [OpenGL ES 2.0.24] section 4.4 page 107:
2812 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2813 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2814
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002815 if (mState.framebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 {
2817 bindFramebuffer(0);
2818 }
2819}
2820
2821void Context::detachRenderbuffer(GLuint renderbuffer)
2822{
2823 // [OpenGL ES 2.0.24] section 4.4 page 109:
2824 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2825 // had been executed with the target RENDERBUFFER and name of zero.
2826
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002827 if (mState.renderbuffer == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828 {
2829 bindRenderbuffer(0);
2830 }
2831
2832 // [OpenGL ES 2.0.24] section 4.4 page 111:
2833 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2834 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2835 // point to which this image was attached in the currently bound framebuffer.
2836
2837 Framebuffer *framebuffer = getFramebuffer();
2838
2839 if (framebuffer)
2840 {
2841 framebuffer->detachRenderbuffer(renderbuffer);
2842 }
2843}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002844
2845Texture *Context::getIncompleteTexture(SamplerType type)
2846{
2847 Texture *t = mIncompleteTextures[type];
2848
2849 if (t == NULL)
2850 {
2851 static const GLubyte color[] = { 0, 0, 0, 255 };
2852
2853 switch (type)
2854 {
2855 default:
2856 UNREACHABLE();
2857 // default falls through to SAMPLER_2D
2858
2859 case SAMPLER_2D:
2860 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002861 Texture2D *incomplete2d = new Texture2D(this);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002862 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002863 t = incomplete2d;
2864 }
2865 break;
2866
2867 case SAMPLER_CUBE:
2868 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002869 TextureCubeMap *incompleteCube = new TextureCubeMap(this);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002870
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002871 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2872 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2873 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2874 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2875 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2876 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002877
2878 t = incompleteCube;
2879 }
2880 break;
2881 }
2882
2883 mIncompleteTextures[type] = t;
2884 }
2885
2886 return t;
2887}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002888
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002889bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002890{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002891 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002892}
2893
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002894bool Context::isTriangleMode(GLenum drawMode)
2895{
2896 switch (drawMode)
2897 {
2898 case GL_TRIANGLES:
2899 case GL_TRIANGLE_FAN:
2900 case GL_TRIANGLE_STRIP:
2901 return true;
2902 case GL_POINTS:
2903 case GL_LINES:
2904 case GL_LINE_LOOP:
2905 case GL_LINE_STRIP:
2906 return false;
2907 default: UNREACHABLE();
2908 }
2909
2910 return false;
2911}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002912
daniel@transgaming.com5bd0ce32010-05-07 13:03:31 +00002913bool Context::hasStencil()
2914{
2915 Framebuffer *framebufferObject = getFramebuffer();
2916
2917 if (framebufferObject)
2918 {
2919 Stencilbuffer *stencilbufferObject = framebufferObject->getStencilbuffer();
2920
2921 if (stencilbufferObject)
2922 {
2923 return stencilbufferObject->getStencilSize() > 0;
2924 }
2925 }
2926
2927 return false;
2928}
2929
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002930void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2931{
2932 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2933
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002934 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2935 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2936 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2937 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002938
2939 mVertexDataManager->dirtyCurrentValues();
2940}
2941
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002942void Context::initExtensionString()
2943{
2944 if (mBufferBackEnd->supportIntIndices())
2945 {
2946 mExtensionString += "GL_OES_element_index_uint ";
2947 }
2948
2949 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2950 if (end != std::string::npos)
2951 {
2952 mExtensionString.resize(end+1);
2953 }
2954}
2955
2956const char *Context::getExtensionString() const
2957{
2958 return mExtensionString.c_str();
2959}
2960
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002961}
2962
2963extern "C"
2964{
2965gl::Context *glCreateContext(const egl::Config *config)
2966{
2967 return new gl::Context(config);
2968}
2969
2970void glDestroyContext(gl::Context *context)
2971{
2972 delete context;
2973
2974 if (context == gl::getContext())
2975 {
2976 gl::makeCurrent(NULL, NULL, NULL);
2977 }
2978}
2979
2980void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2981{
2982 gl::makeCurrent(context, display, surface);
2983}
2984
2985gl::Context *glGetCurrentContext()
2986{
2987 return gl::getContext();
2988}
2989}