blob: ca8cfae69768bab8b9f0a07698dc4a13c6ea35b5 [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.com353569a2010-06-24 13:02:12 +0000225 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000226
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000227 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000228 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
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000272 if (mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0))
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000273 {
274 mPsProfile = "ps_3_0";
275 mVsProfile = "vs_3_0";
276 }
277 else // egl::Display guarantees support for at least 2.0
278 {
279 mPsProfile = "ps_2_0";
280 mVsProfile = "vs_2_0";
281 }
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000282
283 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284}
285
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000286// 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 +0000287void Context::markAllStateDirty()
288{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000289 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000290 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000291 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000292
293 mClearStateDirty = true;
294 mCullStateDirty = true;
295 mDepthStateDirty = true;
296 mMaskStateDirty = true;
297 mBlendStateDirty = true;
298 mStencilStateDirty = true;
299 mPolygonOffsetStateDirty = true;
300 mScissorStateDirty = true;
301 mSampleStateDirty = true;
302 mDitherStateDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000303}
304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305void Context::setClearColor(float red, float green, float blue, float alpha)
306{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000307 mState.colorClearValue.red = red;
308 mState.colorClearValue.green = green;
309 mState.colorClearValue.blue = blue;
310 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311}
312
313void Context::setClearDepth(float depth)
314{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000315 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316}
317
318void Context::setClearStencil(int stencil)
319{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000320 mState.stencilClearValue = stencil;
321}
322
323void Context::setCullFace(bool enabled)
324{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000325 if (mState.cullFace != enabled)
326 {
327 mState.cullFace = enabled;
328 mCullStateDirty = true;
329 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000330}
331
332bool Context::isCullFaceEnabled() const
333{
334 return mState.cullFace;
335}
336
337void Context::setCullMode(GLenum mode)
338{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000339 if (mState.cullMode != mode)
340 {
341 mState.cullMode = mode;
342 mCullStateDirty = true;
343 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000344}
345
346void Context::setFrontFace(GLenum front)
347{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000348 if (mState.frontFace != front)
349 {
350 mState.frontFace = front;
351 mFrontFaceDirty = true;
352 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000353}
354
355void Context::setDepthTest(bool enabled)
356{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000357 if (mState.depthTest != enabled)
358 {
359 mState.depthTest = enabled;
360 mDepthStateDirty = true;
361 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000362}
363
364bool Context::isDepthTestEnabled() const
365{
366 return mState.depthTest;
367}
368
369void Context::setDepthFunc(GLenum depthFunc)
370{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000371 if (mState.depthFunc != depthFunc)
372 {
373 mState.depthFunc = depthFunc;
374 mDepthStateDirty = true;
375 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000376}
377
378void Context::setDepthRange(float zNear, float zFar)
379{
380 mState.zNear = zNear;
381 mState.zFar = zFar;
382}
383
384void Context::setBlend(bool enabled)
385{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000386 if (mState.blend != enabled)
387 {
388 mState.blend = enabled;
389 mBlendStateDirty = true;
390 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000391}
392
393bool Context::isBlendEnabled() const
394{
395 return mState.blend;
396}
397
398void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
399{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000400 if (mState.sourceBlendRGB != sourceRGB ||
401 mState.sourceBlendAlpha != sourceAlpha ||
402 mState.destBlendRGB != destRGB ||
403 mState.destBlendAlpha != destAlpha)
404 {
405 mState.sourceBlendRGB = sourceRGB;
406 mState.destBlendRGB = destRGB;
407 mState.sourceBlendAlpha = sourceAlpha;
408 mState.destBlendAlpha = destAlpha;
409 mBlendStateDirty = true;
410 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000411}
412
413void Context::setBlendColor(float red, float green, float blue, float alpha)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.blendColor.red != red ||
416 mState.blendColor.green != green ||
417 mState.blendColor.blue != blue ||
418 mState.blendColor.alpha != alpha)
419 {
420 mState.blendColor.red = red;
421 mState.blendColor.green = green;
422 mState.blendColor.blue = blue;
423 mState.blendColor.alpha = alpha;
424 mBlendStateDirty = true;
425 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000426}
427
428void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.blendEquationRGB != rgbEquation ||
431 mState.blendEquationAlpha != alphaEquation)
432 {
433 mState.blendEquationRGB = rgbEquation;
434 mState.blendEquationAlpha = alphaEquation;
435 mBlendStateDirty = true;
436 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000437}
438
439void Context::setStencilTest(bool enabled)
440{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000441 if (mState.stencilTest != enabled)
442 {
443 mState.stencilTest = enabled;
444 mStencilStateDirty = true;
445 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000446}
447
448bool Context::isStencilTestEnabled() const
449{
450 return mState.stencilTest;
451}
452
453void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
454{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000455 if (mState.stencilFunc != stencilFunc ||
456 mState.stencilRef != stencilRef ||
457 mState.stencilMask != stencilMask)
458 {
459 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000460 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000461 mState.stencilMask = stencilMask;
462 mStencilStateDirty = true;
463 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000464}
465
466void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
467{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000468 if (mState.stencilBackFunc != stencilBackFunc ||
469 mState.stencilBackRef != stencilBackRef ||
470 mState.stencilBackMask != stencilBackMask)
471 {
472 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000473 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000474 mState.stencilBackMask = stencilBackMask;
475 mStencilStateDirty = true;
476 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000477}
478
479void Context::setStencilWritemask(GLuint stencilWritemask)
480{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000481 if (mState.stencilWritemask != stencilWritemask)
482 {
483 mState.stencilWritemask = stencilWritemask;
484 mStencilStateDirty = true;
485 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000486}
487
488void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
489{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000490 if (mState.stencilBackWritemask != stencilBackWritemask)
491 {
492 mState.stencilBackWritemask = stencilBackWritemask;
493 mStencilStateDirty = true;
494 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000495}
496
497void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
498{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 if (mState.stencilFail != stencilFail ||
500 mState.stencilPassDepthFail != stencilPassDepthFail ||
501 mState.stencilPassDepthPass != stencilPassDepthPass)
502 {
503 mState.stencilFail = stencilFail;
504 mState.stencilPassDepthFail = stencilPassDepthFail;
505 mState.stencilPassDepthPass = stencilPassDepthPass;
506 mStencilStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.stencilBackFail != stencilBackFail ||
513 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
514 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
515 {
516 mState.stencilBackFail = stencilBackFail;
517 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
518 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
519 mStencilStateDirty = true;
520 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000521}
522
523void Context::setPolygonOffsetFill(bool enabled)
524{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000525 if (mState.polygonOffsetFill != enabled)
526 {
527 mState.polygonOffsetFill = enabled;
528 mPolygonOffsetStateDirty = true;
529 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000530}
531
532bool Context::isPolygonOffsetFillEnabled() const
533{
534 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000536}
537
538void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
539{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000540 if (mState.polygonOffsetFactor != factor ||
541 mState.polygonOffsetUnits != units)
542 {
543 mState.polygonOffsetFactor = factor;
544 mState.polygonOffsetUnits = units;
545 mPolygonOffsetStateDirty = true;
546 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000547}
548
549void Context::setSampleAlphaToCoverage(bool enabled)
550{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000551 if (mState.sampleAlphaToCoverage != enabled)
552 {
553 mState.sampleAlphaToCoverage = enabled;
554 mSampleStateDirty = true;
555 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000556}
557
558bool Context::isSampleAlphaToCoverageEnabled() const
559{
560 return mState.sampleAlphaToCoverage;
561}
562
563void Context::setSampleCoverage(bool enabled)
564{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000565 if (mState.sampleCoverage != enabled)
566 {
567 mState.sampleCoverage = enabled;
568 mSampleStateDirty = true;
569 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000570}
571
572bool Context::isSampleCoverageEnabled() const
573{
574 return mState.sampleCoverage;
575}
576
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000577void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579 if (mState.sampleCoverageValue != value ||
580 mState.sampleCoverageInvert != invert)
581 {
582 mState.sampleCoverageValue = value;
583 mState.sampleCoverageInvert = invert;
584 mSampleStateDirty = true;
585 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588void Context::setScissorTest(bool enabled)
589{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590 if (mState.scissorTest != enabled)
591 {
592 mState.scissorTest = enabled;
593 mScissorStateDirty = true;
594 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000595}
596
597bool Context::isScissorTestEnabled() const
598{
599 return mState.scissorTest;
600}
601
602void Context::setDither(bool enabled)
603{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000604 if (mState.dither != enabled)
605 {
606 mState.dither = enabled;
607 mDitherStateDirty = true;
608 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000609}
610
611bool Context::isDitherEnabled() const
612{
613 return mState.dither;
614}
615
616void Context::setLineWidth(GLfloat width)
617{
618 mState.lineWidth = width;
619}
620
621void Context::setGenerateMipmapHint(GLenum hint)
622{
623 mState.generateMipmapHint = hint;
624}
625
626void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
627{
628 mState.viewportX = x;
629 mState.viewportY = y;
630 mState.viewportWidth = width;
631 mState.viewportHeight = height;
632}
633
634void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
635{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000636 if (mState.scissorX != x || mState.scissorY != y ||
637 mState.scissorWidth != width || mState.scissorHeight != height)
638 {
639 mState.scissorX = x;
640 mState.scissorY = y;
641 mState.scissorWidth = width;
642 mState.scissorHeight = height;
643 mScissorStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
648{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000649 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
650 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
651 {
652 mState.colorMaskRed = red;
653 mState.colorMaskGreen = green;
654 mState.colorMaskBlue = blue;
655 mState.colorMaskAlpha = alpha;
656 mMaskStateDirty = true;
657 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000658}
659
660void Context::setDepthMask(bool mask)
661{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000662 if (mState.depthMask != mask)
663 {
664 mState.depthMask = mask;
665 mMaskStateDirty = true;
666 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000667}
668
669void Context::setActiveSampler(int active)
670{
671 mState.activeSampler = active;
672}
673
674GLuint Context::getFramebufferHandle() const
675{
676 return mState.framebuffer;
677}
678
679GLuint Context::getRenderbufferHandle() const
680{
681 return mState.renderbuffer;
682}
683
684GLuint Context::getArrayBufferHandle() const
685{
686 return mState.arrayBuffer;
687}
688
689void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
690{
691 mState.vertexAttribute[attribNum].mEnabled = enabled;
692}
693
694const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
695{
696 return mState.vertexAttribute[attribNum];
697}
698
699void Context::setVertexAttribState(unsigned int attribNum, GLuint boundBuffer, GLint size, GLenum type, bool normalized,
700 GLsizei stride, const void *pointer)
701{
702 mState.vertexAttribute[attribNum].mBoundBuffer = boundBuffer;
703 mState.vertexAttribute[attribNum].mSize = size;
704 mState.vertexAttribute[attribNum].mType = type;
705 mState.vertexAttribute[attribNum].mNormalized = normalized;
706 mState.vertexAttribute[attribNum].mStride = stride;
707 mState.vertexAttribute[attribNum].mPointer = pointer;
708}
709
710const void *Context::getVertexAttribPointer(unsigned int attribNum) const
711{
712 return mState.vertexAttribute[attribNum].mPointer;
713}
714
715// returns entire set of attributes as a block
716const AttributeState *Context::getVertexAttribBlock()
717{
718 return mState.vertexAttribute;
719}
720
721void Context::setPackAlignment(GLint alignment)
722{
723 mState.packAlignment = alignment;
724}
725
726GLint Context::getPackAlignment() const
727{
728 return mState.packAlignment;
729}
730
731void Context::setUnpackAlignment(GLint alignment)
732{
733 mState.unpackAlignment = alignment;
734}
735
736GLint Context::getUnpackAlignment() const
737{
738 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739}
740
741// Returns an unused buffer name
742GLuint Context::createBuffer()
743{
744 unsigned int handle = 1;
745
746 while (mBufferMap.find(handle) != mBufferMap.end())
747 {
748 handle++;
749 }
750
751 mBufferMap[handle] = NULL;
752
753 return handle;
754}
755
756// Returns an unused shader/program name
757GLuint Context::createShader(GLenum type)
758{
759 unsigned int handle = 1;
760
761 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
762 {
763 handle++;
764 }
765
766 if (type == GL_VERTEX_SHADER)
767 {
daniel@transgaming.com95124342010-04-29 03:38:58 +0000768 mShaderMap[handle] = new VertexShader(this, handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769 }
770 else if (type == GL_FRAGMENT_SHADER)
771 {
daniel@transgaming.com95124342010-04-29 03:38:58 +0000772 mShaderMap[handle] = new FragmentShader(this, handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773 }
774 else UNREACHABLE();
775
776 return handle;
777}
778
779// Returns an unused program/shader name
780GLuint Context::createProgram()
781{
782 unsigned int handle = 1;
783
784 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
785 {
786 handle++;
787 }
788
789 mProgramMap[handle] = new Program();
790
791 return handle;
792}
793
794// Returns an unused texture name
795GLuint Context::createTexture()
796{
797 unsigned int handle = 1;
798
799 while (mTextureMap.find(handle) != mTextureMap.end())
800 {
801 handle++;
802 }
803
804 mTextureMap[handle] = NULL;
805
806 return handle;
807}
808
809// Returns an unused framebuffer name
810GLuint Context::createFramebuffer()
811{
812 unsigned int handle = 1;
813
814 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
815 {
816 handle++;
817 }
818
819 mFramebufferMap[handle] = NULL;
820
821 return handle;
822}
823
824// Returns an unused renderbuffer name
825GLuint Context::createRenderbuffer()
826{
827 unsigned int handle = 1;
828
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000829 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830 {
831 handle++;
832 }
833
834 mRenderbufferMap[handle] = NULL;
835
836 return handle;
837}
838
839void Context::deleteBuffer(GLuint buffer)
840{
841 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
842
843 if (bufferObject != mBufferMap.end())
844 {
845 detachBuffer(buffer);
846
847 delete bufferObject->second;
848 mBufferMap.erase(bufferObject);
849 }
850}
851
852void Context::deleteShader(GLuint shader)
853{
854 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
855
856 if (shaderObject != mShaderMap.end())
857 {
858 if (!shaderObject->second->isAttached())
859 {
860 delete shaderObject->second;
861 mShaderMap.erase(shaderObject);
862 }
863 else
864 {
865 shaderObject->second->flagForDeletion();
866 }
867 }
868}
869
870void Context::deleteProgram(GLuint program)
871{
872 ProgramMap::iterator programObject = mProgramMap.find(program);
873
874 if (programObject != mProgramMap.end())
875 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000876 if (program != mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 {
878 delete programObject->second;
879 mProgramMap.erase(programObject);
880 }
881 else
882 {
883 programObject->second->flagForDeletion();
884 }
885 }
886}
887
888void Context::deleteTexture(GLuint texture)
889{
890 TextureMap::iterator textureObject = mTextureMap.find(texture);
891
892 if (textureObject != mTextureMap.end())
893 {
894 detachTexture(texture);
895
896 if (texture != 0)
897 {
898 delete textureObject->second;
899 }
900
901 mTextureMap.erase(textureObject);
902 }
903}
904
905void Context::deleteFramebuffer(GLuint framebuffer)
906{
907 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
908
909 if (framebufferObject != mFramebufferMap.end())
910 {
911 detachFramebuffer(framebuffer);
912
913 delete framebufferObject->second;
914 mFramebufferMap.erase(framebufferObject);
915 }
916}
917
918void Context::deleteRenderbuffer(GLuint renderbuffer)
919{
920 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
921
922 if (renderbufferObject != mRenderbufferMap.end())
923 {
924 detachRenderbuffer(renderbuffer);
925
926 delete renderbufferObject->second;
927 mRenderbufferMap.erase(renderbufferObject);
928 }
929}
930
931void Context::bindArrayBuffer(unsigned int buffer)
932{
933 if (buffer != 0 && !getBuffer(buffer))
934 {
daniel@transgaming.comdefa1c32010-05-18 18:51:45 +0000935 mBufferMap[buffer] = new Buffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936 }
937
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000938 mState.arrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939}
940
941void Context::bindElementArrayBuffer(unsigned int buffer)
942{
943 if (buffer != 0 && !getBuffer(buffer))
944 {
daniel@transgaming.comdefa1c32010-05-18 18:51:45 +0000945 mBufferMap[buffer] = new Buffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946 }
947
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000948 mState.elementArrayBuffer = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindTexture2D(GLuint texture)
952{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000953 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000955 mTextureMap[texture] = new Texture2D(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956 }
957
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000958 mState.texture2D = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000960 mState.samplerTexture[SAMPLER_2D][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
963void Context::bindTextureCubeMap(GLuint texture)
964{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000965 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000967 mTextureMap[texture] = new TextureCubeMap(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968 }
969
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000970 mState.textureCubeMap = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000972 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
975void Context::bindFramebuffer(GLuint framebuffer)
976{
977 if (!getFramebuffer(framebuffer))
978 {
979 mFramebufferMap[framebuffer] = new Framebuffer();
980 }
981
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000982 mState.framebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
985void Context::bindRenderbuffer(GLuint renderbuffer)
986{
987 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
988 {
989 mRenderbufferMap[renderbuffer] = new Renderbuffer();
990 }
991
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000992 mState.renderbuffer = renderbuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::useProgram(GLuint program)
996{
997 Program *programObject = getCurrentProgram();
998
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000999 GLuint priorProgram = mState.currentProgram;
1000 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001001
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002 if (programObject && programObject->isFlaggedForDeletion())
1003 {
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001004 deleteProgram(priorProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
1008void Context::setFramebufferZero(Framebuffer *buffer)
1009{
1010 delete mFramebufferMap[0];
1011 mFramebufferMap[0] = buffer;
1012}
1013
1014void Context::setColorbufferZero(Colorbuffer *buffer)
1015{
1016 delete mColorbufferZero;
1017 mColorbufferZero = buffer;
1018}
1019
1020void Context::setDepthbufferZero(Depthbuffer *buffer)
1021{
1022 delete mDepthbufferZero;
1023 mDepthbufferZero = buffer;
1024}
1025
1026void Context::setStencilbufferZero(Stencilbuffer *buffer)
1027{
1028 delete mStencilbufferZero;
1029 mStencilbufferZero = buffer;
1030}
1031
1032void Context::setRenderbuffer(Renderbuffer *buffer)
1033{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001034 delete mRenderbufferMap[mState.renderbuffer];
1035 mRenderbufferMap[mState.renderbuffer] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036}
1037
1038Buffer *Context::getBuffer(unsigned int handle)
1039{
1040 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001041
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042 if (buffer == mBufferMap.end())
1043 {
1044 return NULL;
1045 }
1046 else
1047 {
1048 return buffer->second;
1049 }
1050}
1051
1052Shader *Context::getShader(unsigned int handle)
1053{
1054 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001055
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056 if (shader == mShaderMap.end())
1057 {
1058 return NULL;
1059 }
1060 else
1061 {
1062 return shader->second;
1063 }
1064}
1065
1066Program *Context::getProgram(unsigned int handle)
1067{
1068 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001069
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 if (program == mProgramMap.end())
1071 {
1072 return NULL;
1073 }
1074 else
1075 {
1076 return program->second;
1077 }
1078}
1079
1080Texture *Context::getTexture(unsigned int handle)
1081{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001082 if (handle == 0) return NULL;
1083
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086 if (texture == mTextureMap.end())
1087 {
1088 return NULL;
1089 }
1090 else
1091 {
1092 return texture->second;
1093 }
1094}
1095
1096Framebuffer *Context::getFramebuffer(unsigned int handle)
1097{
1098 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001099
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100 if (framebuffer == mFramebufferMap.end())
1101 {
1102 return NULL;
1103 }
1104 else
1105 {
1106 return framebuffer->second;
1107 }
1108}
1109
1110Renderbuffer *Context::getRenderbuffer(unsigned int handle)
1111{
1112 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001113
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114 if (renderbuffer == mRenderbufferMap.end())
1115 {
1116 return NULL;
1117 }
1118 else
1119 {
1120 return renderbuffer->second;
1121 }
1122}
1123
1124Colorbuffer *Context::getColorbuffer(GLuint handle)
1125{
1126 if (handle != 0)
1127 {
1128 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1129
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001130 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131 {
1132 return static_cast<Colorbuffer*>(renderbuffer);
1133 }
1134 }
1135 else // Special case: 0 refers to different initial render targets based on the attachment type
1136 {
1137 return mColorbufferZero;
1138 }
1139
1140 return NULL;
1141}
1142
1143Depthbuffer *Context::getDepthbuffer(GLuint handle)
1144{
1145 if (handle != 0)
1146 {
1147 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1148
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001149 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150 {
1151 return static_cast<Depthbuffer*>(renderbuffer);
1152 }
1153 }
1154 else // Special case: 0 refers to different initial render targets based on the attachment type
1155 {
1156 return mDepthbufferZero;
1157 }
1158
1159 return NULL;
1160}
1161
1162Stencilbuffer *Context::getStencilbuffer(GLuint handle)
1163{
1164 if (handle != 0)
1165 {
1166 Renderbuffer *renderbuffer = getRenderbuffer(handle);
1167
daniel@transgaming.come2b22122010-03-11 19:22:14 +00001168 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169 {
1170 return static_cast<Stencilbuffer*>(renderbuffer);
1171 }
1172 }
1173 else
1174 {
1175 return mStencilbufferZero;
1176 }
1177
1178 return NULL;
1179}
1180
1181Buffer *Context::getArrayBuffer()
1182{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001183 return getBuffer(mState.arrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
1186Buffer *Context::getElementArrayBuffer()
1187{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001188 return getBuffer(mState.elementArrayBuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001189}
1190
1191Program *Context::getCurrentProgram()
1192{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001193 return getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194}
1195
1196Texture2D *Context::getTexture2D()
1197{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001198 if (mState.texture2D == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001199 {
1200 return mTexture2DZero;
1201 }
1202
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001203 return (Texture2D*)getTexture(mState.texture2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001204}
1205
1206TextureCubeMap *Context::getTextureCubeMap()
1207{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001208 if (mState.textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001209 {
1210 return mTextureCubeMapZero;
1211 }
1212
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001213 return (TextureCubeMap*)getTexture(mState.textureCubeMap);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001214}
1215
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001216Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001217{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001218 GLuint texid = mState.samplerTexture[type][sampler];
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001219
1220 if (texid == 0)
1221 {
1222 switch (type)
1223 {
1224 default: UNREACHABLE();
1225 case SAMPLER_2D: return mTexture2DZero;
1226 case SAMPLER_CUBE: return mTextureCubeMapZero;
1227 }
1228 }
1229
1230 return getTexture(texid);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001231}
1232
1233Framebuffer *Context::getFramebuffer()
1234{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001235 return getFramebuffer(mState.framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001236}
1237
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001238bool Context::getBooleanv(GLenum pname, GLboolean *params)
1239{
1240 switch (pname)
1241 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001242 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1243 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1244 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001245 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001246 params[0] = mState.colorMaskRed;
1247 params[1] = mState.colorMaskGreen;
1248 params[2] = mState.colorMaskBlue;
1249 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001250 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001251 case GL_CULL_FACE: *params = mState.cullFace;
1252 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1253 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1254 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1255 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1256 case GL_STENCIL_TEST: *params = mState.stencilTest;
1257 case GL_DEPTH_TEST: *params = mState.depthTest;
1258 case GL_BLEND: *params = mState.blend;
1259 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001260 default:
1261 return false;
1262 }
1263
1264 return true;
1265}
1266
1267bool Context::getFloatv(GLenum pname, GLfloat *params)
1268{
1269 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1270 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1271 // GetIntegerv as its native query function. As it would require conversion in any
1272 // case, this should make no difference to the calling application.
1273 switch (pname)
1274 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001275 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1276 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1277 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1278 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1279 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001280 case GL_ALIASED_LINE_WIDTH_RANGE:
1281 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1282 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1283 break;
1284 case GL_ALIASED_POINT_SIZE_RANGE:
1285 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1286 params[1] = gl::ALIASED_POINT_SIZE_RANGE_MAX;
1287 break;
1288 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001289 params[0] = mState.zNear;
1290 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001291 break;
1292 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001293 params[0] = mState.colorClearValue.red;
1294 params[1] = mState.colorClearValue.green;
1295 params[2] = mState.colorClearValue.blue;
1296 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001297 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001298 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001299 params[0] = mState.blendColor.red;
1300 params[1] = mState.blendColor.green;
1301 params[2] = mState.blendColor.blue;
1302 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001303 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001304 default:
1305 return false;
1306 }
1307
1308 return true;
1309}
1310
1311bool Context::getIntegerv(GLenum pname, GLint *params)
1312{
1313 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1314 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1315 // GetIntegerv as its native query function. As it would require conversion in any
1316 // case, this should make no difference to the calling application. You may find it in
1317 // Context::getFloatv.
1318 switch (pname)
1319 {
1320 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1321 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1322 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1323 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1324 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1325 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1326 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1327 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001328 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1329 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1330 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1331 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1332 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer; break;
1333 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer; break;
1334 case GL_FRAMEBUFFER_BINDING: *params = mState.framebuffer; break;
1335 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer; break;
1336 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1337 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1338 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1339 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1340 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1341 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1342 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1343 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1344 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1345 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1346 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1347 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1348 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1349 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1350 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1351 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1352 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1353 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1354 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1355 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1356 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1357 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1358 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1359 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1360 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1361 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1362 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1363 case GL_SUBPIXEL_BITS: *params = 4; break;
1364 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1365 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
1366 case GL_SAMPLE_BUFFERS: *params = 0; break;
1367 case GL_SAMPLES: *params = 0; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1369 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1370 case GL_MAX_VIEWPORT_DIMS:
1371 {
1372 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1373 params[0] = maxDimension;
1374 params[1] = maxDimension;
1375 }
1376 break;
1377 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001378 params[0] = mState.viewportX;
1379 params[1] = mState.viewportY;
1380 params[2] = mState.viewportWidth;
1381 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 break;
1383 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001384 params[0] = mState.scissorX;
1385 params[1] = mState.scissorY;
1386 params[2] = mState.scissorWidth;
1387 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001389 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1390 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 case GL_RED_BITS:
1392 case GL_GREEN_BITS:
1393 case GL_BLUE_BITS:
1394 case GL_ALPHA_BITS:
1395 {
1396 gl::Framebuffer *framebuffer = getFramebuffer();
1397 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1398
1399 if (colorbuffer)
1400 {
1401 switch (pname)
1402 {
1403 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1404 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1405 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1406 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1407 }
1408 }
1409 else
1410 {
1411 *params = 0;
1412 }
1413 }
1414 break;
1415 case GL_DEPTH_BITS:
1416 {
1417 gl::Framebuffer *framebuffer = getFramebuffer();
1418 gl::Depthbuffer *depthbuffer = framebuffer->getDepthbuffer();
1419
1420 if (depthbuffer)
1421 {
1422 *params = depthbuffer->getDepthSize();
1423 }
1424 else
1425 {
1426 *params = 0;
1427 }
1428 }
1429 break;
1430 case GL_STENCIL_BITS:
1431 {
1432 gl::Framebuffer *framebuffer = getFramebuffer();
1433 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1434
1435 if (stencilbuffer)
1436 {
1437 *params = stencilbuffer->getStencilSize();
1438 }
1439 else
1440 {
1441 *params = 0;
1442 }
1443 }
1444 break;
1445 case GL_TEXTURE_BINDING_2D:
1446 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001447 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001448 {
1449 error(GL_INVALID_OPERATION);
1450 return false;
1451 }
1452
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001453 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001454 }
1455 break;
1456 case GL_TEXTURE_BINDING_CUBE_MAP:
1457 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001458 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001459 {
1460 error(GL_INVALID_OPERATION);
1461 return false;
1462 }
1463
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001464 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler];
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001465 }
1466 break;
1467 default:
1468 return false;
1469 }
1470
1471 return true;
1472}
1473
1474bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1475{
1476 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1477 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1478 // to the fact that it is stored internally as a float, and so would require conversion
1479 // if returned from Context::getIntegerv. Since this conversion is already implemented
1480 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1481 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1482 // application.
1483 switch (pname)
1484 {
1485 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1486 case GL_SHADER_BINARY_FORMATS:
1487 {
1488 *type = GL_INT;
1489 *numParams = 0;
1490 }
1491 break;
1492 case GL_MAX_VERTEX_ATTRIBS:
1493 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1494 case GL_MAX_VARYING_VECTORS:
1495 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1496 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1497 case GL_MAX_TEXTURE_IMAGE_UNITS:
1498 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1499 case GL_MAX_RENDERBUFFER_SIZE:
1500 case GL_NUM_SHADER_BINARY_FORMATS:
1501 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1502 case GL_ARRAY_BUFFER_BINDING:
1503 case GL_FRAMEBUFFER_BINDING:
1504 case GL_RENDERBUFFER_BINDING:
1505 case GL_CURRENT_PROGRAM:
1506 case GL_PACK_ALIGNMENT:
1507 case GL_UNPACK_ALIGNMENT:
1508 case GL_GENERATE_MIPMAP_HINT:
1509 case GL_RED_BITS:
1510 case GL_GREEN_BITS:
1511 case GL_BLUE_BITS:
1512 case GL_ALPHA_BITS:
1513 case GL_DEPTH_BITS:
1514 case GL_STENCIL_BITS:
1515 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1516 case GL_CULL_FACE_MODE:
1517 case GL_FRONT_FACE:
1518 case GL_ACTIVE_TEXTURE:
1519 case GL_STENCIL_FUNC:
1520 case GL_STENCIL_VALUE_MASK:
1521 case GL_STENCIL_REF:
1522 case GL_STENCIL_FAIL:
1523 case GL_STENCIL_PASS_DEPTH_FAIL:
1524 case GL_STENCIL_PASS_DEPTH_PASS:
1525 case GL_STENCIL_BACK_FUNC:
1526 case GL_STENCIL_BACK_VALUE_MASK:
1527 case GL_STENCIL_BACK_REF:
1528 case GL_STENCIL_BACK_FAIL:
1529 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1530 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1531 case GL_DEPTH_FUNC:
1532 case GL_BLEND_SRC_RGB:
1533 case GL_BLEND_SRC_ALPHA:
1534 case GL_BLEND_DST_RGB:
1535 case GL_BLEND_DST_ALPHA:
1536 case GL_BLEND_EQUATION_RGB:
1537 case GL_BLEND_EQUATION_ALPHA:
1538 case GL_STENCIL_WRITEMASK:
1539 case GL_STENCIL_BACK_WRITEMASK:
1540 case GL_STENCIL_CLEAR_VALUE:
1541 case GL_SUBPIXEL_BITS:
1542 case GL_MAX_TEXTURE_SIZE:
1543 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1544 case GL_SAMPLE_BUFFERS:
1545 case GL_SAMPLES:
1546 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1547 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1548 case GL_TEXTURE_BINDING_2D:
1549 case GL_TEXTURE_BINDING_CUBE_MAP:
1550 {
1551 *type = GL_INT;
1552 *numParams = 1;
1553 }
1554 break;
1555 case GL_MAX_VIEWPORT_DIMS:
1556 {
1557 *type = GL_INT;
1558 *numParams = 2;
1559 }
1560 break;
1561 case GL_VIEWPORT:
1562 case GL_SCISSOR_BOX:
1563 {
1564 *type = GL_INT;
1565 *numParams = 4;
1566 }
1567 break;
1568 case GL_SHADER_COMPILER:
1569 case GL_SAMPLE_COVERAGE_INVERT:
1570 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001571 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1572 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1573 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1574 case GL_SAMPLE_COVERAGE:
1575 case GL_SCISSOR_TEST:
1576 case GL_STENCIL_TEST:
1577 case GL_DEPTH_TEST:
1578 case GL_BLEND:
1579 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001580 {
1581 *type = GL_BOOL;
1582 *numParams = 1;
1583 }
1584 break;
1585 case GL_COLOR_WRITEMASK:
1586 {
1587 *type = GL_BOOL;
1588 *numParams = 4;
1589 }
1590 break;
1591 case GL_POLYGON_OFFSET_FACTOR:
1592 case GL_POLYGON_OFFSET_UNITS:
1593 case GL_SAMPLE_COVERAGE_VALUE:
1594 case GL_DEPTH_CLEAR_VALUE:
1595 case GL_LINE_WIDTH:
1596 {
1597 *type = GL_FLOAT;
1598 *numParams = 1;
1599 }
1600 break;
1601 case GL_ALIASED_LINE_WIDTH_RANGE:
1602 case GL_ALIASED_POINT_SIZE_RANGE:
1603 case GL_DEPTH_RANGE:
1604 {
1605 *type = GL_FLOAT;
1606 *numParams = 2;
1607 }
1608 break;
1609 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001610 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001611 {
1612 *type = GL_FLOAT;
1613 *numParams = 4;
1614 }
1615 break;
1616 default:
1617 return false;
1618 }
1619
1620 return true;
1621}
1622
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623// Applies the render target surface, depth stencil surface, viewport rectangle and
1624// scissor rectangle to the Direct3D 9 device
1625bool Context::applyRenderTarget(bool ignoreViewport)
1626{
1627 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001629 Framebuffer *framebufferObject = getFramebuffer();
1630
1631 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1632 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001633 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1634
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001635 return false;
1636 }
1637
1638 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1639 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1640
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001641 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1642 if (renderTargetSerial != mAppliedRenderTargetSerial)
1643 {
1644 device->SetRenderTarget(0, renderTarget);
1645 mAppliedRenderTargetSerial = renderTargetSerial;
1646 }
1647
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001648 unsigned int depthbufferSerial = framebufferObject->getDepthbufferSerial();
1649 if (depthbufferSerial != mAppliedDepthbufferSerial)
1650 {
1651 device->SetDepthStencilSurface(depthStencil);
1652 mAppliedDepthbufferSerial = depthbufferSerial;
1653 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654
1655 D3DVIEWPORT9 viewport;
1656 D3DSURFACE_DESC desc;
1657 renderTarget->GetDesc(&desc);
1658
1659 if (ignoreViewport)
1660 {
1661 viewport.X = 0;
1662 viewport.Y = 0;
1663 viewport.Width = desc.Width;
1664 viewport.Height = desc.Height;
1665 viewport.MinZ = 0.0f;
1666 viewport.MaxZ = 1.0f;
1667 }
1668 else
1669 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001670 viewport.X = std::max(mState.viewportX, 0);
1671 viewport.Y = std::max(mState.viewportY, 0);
1672 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1673 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1674 viewport.MinZ = clamp01(mState.zNear);
1675 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 }
1677
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001678 if (viewport.Width <= 0 || viewport.Height <= 0)
1679 {
1680 return false; // Nothing to render
1681 }
1682
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 device->SetViewport(&viewport);
1684
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001687 if (mState.scissorTest)
1688 {
1689 RECT rect = {mState.scissorX,
1690 mState.scissorY,
1691 mState.scissorX + mState.scissorWidth,
1692 mState.scissorY + mState.scissorHeight};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001694 device->SetScissorRect(&rect);
1695 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1696 }
1697 else
1698 {
1699 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1700 }
1701
1702 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703 }
1704
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 Program *programObject = getCurrentProgram();
1708
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001709 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001710 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001712
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001713 GLint window = programObject->getDxWindowLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001714 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1715 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1716 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001717 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1718
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001719 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001720 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001721 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1722
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001723 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001724 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001725
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001726 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001727 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001728
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001729 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001730 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001731 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 }
1733
1734 return true;
1735}
1736
1737// 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 +00001738void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739{
1740 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001741 Program *programObject = getCurrentProgram();
1742
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001743 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001744 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001745 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001747 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001748 GLint alwaysFront = !isTriangleMode(drawMode);
1749 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1750
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001753 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 }
1757 else
1758 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 }
1761
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 mCullStateDirty = false;
1763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 if (mDepthStateDirty)
1766 {
1767 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1770 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 }
1772 else
1773 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001774 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001776
1777 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778 }
1779
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001780 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001782 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001783 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001784 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1785
1786 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1787 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1788 {
1789 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1790 }
1791 else
1792 {
1793 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1794 unorm<8>(mState.blendColor.alpha),
1795 unorm<8>(mState.blendColor.alpha),
1796 unorm<8>(mState.blendColor.alpha)));
1797 }
1798
1799 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1800 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1801 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1802
1803 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1804 mState.destBlendRGB != mState.destBlendAlpha ||
1805 mState.blendEquationRGB != mState.blendEquationAlpha)
1806 {
1807 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1808
1809 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1810 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1811 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1812
1813 }
1814 else
1815 {
1816 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1817 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001818 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001819 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001820 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001821 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001822 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001823
1824 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825 }
1826
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001827 if (mStencilStateDirty || mFrontFaceDirty)
1828 {
1829 if (mState.stencilTest && hasStencil())
1830 {
1831 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1832 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1833
1834 // FIXME: Unsupported by D3D9
1835 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1836 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1837 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1838 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1839 mState.stencilRef != mState.stencilBackRef ||
1840 mState.stencilMask != mState.stencilBackMask)
1841 {
1842 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1843 return error(GL_INVALID_OPERATION);
1844 }
1845
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001846 // get the maximum size of the stencil ref
1847 gl::Framebuffer *framebuffer = getFramebuffer();
1848 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1849 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1850
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1852 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1853 es2dx::ConvertComparison(mState.stencilFunc));
1854
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001855 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 +00001856 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1857
1858 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1859 es2dx::ConvertStencilOp(mState.stencilFail));
1860 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1861 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1862 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1863 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1864
1865 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1866 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1867 es2dx::ConvertComparison(mState.stencilBackFunc));
1868
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001869 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 +00001870 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1871
1872 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1873 es2dx::ConvertStencilOp(mState.stencilBackFail));
1874 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1875 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1876 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1877 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1878 }
1879 else
1880 {
1881 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1882 }
1883
1884 mStencilStateDirty = false;
1885 }
1886
1887 if (mMaskStateDirty)
1888 {
1889 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1890 mState.colorMaskBlue, mState.colorMaskAlpha));
1891 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1892
1893 mMaskStateDirty = false;
1894 }
1895
1896 if (mPolygonOffsetStateDirty)
1897 {
1898 if (mState.polygonOffsetFill)
1899 {
1900 gl::Depthbuffer *depthbuffer = getFramebuffer()->getDepthbuffer();
1901 if (depthbuffer)
1902 {
1903 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1904 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1905 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1906 }
1907 }
1908 else
1909 {
1910 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1911 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1912 }
1913
1914 mPolygonOffsetStateDirty = false;
1915 }
1916
1917 if (mConfig->mMultiSample != 0 && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001919 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001920 {
1921 FIXME("Sample alpha to coverage is unimplemented.");
1922 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001924 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001925 {
1926 FIXME("Sample coverage is unimplemented.");
1927 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001928
1929 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930 }
1931
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001932 if (mDitherStateDirty)
1933 {
1934 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1935
1936 mDitherStateDirty = false;
1937 }
1938
1939 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940}
1941
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001942// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001943void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001945 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001947 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001949 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 }
1951 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001952}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001954GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001955{
1956 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1957
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001958 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001959 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001960 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001961 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001962 }
1963
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001964 lookupAttributeMapping(translated);
1965
1966 mBufferBackEnd->setupAttributesPreDraw(translated);
1967
1968 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1969 {
1970 if (translated[i].enabled && translated[i].nonArray)
1971 {
1972 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1973 if (err != GL_NO_ERROR)
1974 {
1975 return err;
1976 }
1977
1978 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1979
1980 *useIndexing = true;
1981 return GL_NO_ERROR;
1982 }
1983 }
1984
1985 *useIndexing = false;
1986 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987}
1988
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001989GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001990{
1991 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1992
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001993 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001994
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001995 if (err == GL_NO_ERROR)
1996 {
1997 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001998
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001999 mBufferBackEnd->setupAttributesPreDraw(translated);
2000 }
2001
2002 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002003}
2004
2005// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002006GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002007{
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002008 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, getBuffer(mState.elementArrayBuffer), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002009
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002010 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002011 {
2012 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2013 }
2014
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002015 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016}
2017
2018// Applies the shaders and shader constants to the Direct3D 9 device
2019void Context::applyShaders()
2020{
2021 IDirect3DDevice9 *device = getDevice();
2022 Program *programObject = getCurrentProgram();
2023 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2024 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2025
2026 device->SetVertexShader(vertexShader);
2027 device->SetPixelShader(pixelShader);
2028
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002029 if (programObject->getSerial() != mAppliedProgram)
2030 {
2031 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002032 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002033 mAppliedProgram = programObject->getSerial();
2034 }
2035
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036 programObject->applyUniforms();
2037}
2038
2039// Applies the textures and sampler states to the Direct3D 9 device
2040void Context::applyTextures()
2041{
2042 IDirect3DDevice9 *device = getDevice();
2043 Program *programObject = getCurrentProgram();
2044
2045 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2046 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002047 int textureUnit = programObject->getSamplerMapping(sampler);
2048 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002050 SamplerType textureType = programObject->getSamplerType(sampler);
2051
2052 Texture *texture = getSamplerTexture(textureUnit, textureType);
2053
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002054 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002055 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002056 if (texture->isComplete())
2057 {
2058 GLenum wrapS = texture->getWrapS();
2059 GLenum wrapT = texture->getWrapT();
2060 GLenum minFilter = texture->getMinFilter();
2061 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002063 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2064 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002066 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2067 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2068 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2069 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2070 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002072 device->SetTexture(sampler, texture->getTexture());
2073 }
2074 else
2075 {
2076 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2077 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002078 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002079
2080 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002082 else
2083 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002084 if (programObject->isSamplerDirty(sampler))
2085 {
2086 device->SetTexture(sampler, NULL);
2087 programObject->setSamplerDirty(sampler, false);
2088 }
2089 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
2091}
2092
2093void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2094{
2095 Framebuffer *framebuffer = getFramebuffer();
2096 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2097 IDirect3DDevice9 *device = getDevice();
2098
2099 D3DSURFACE_DESC desc;
2100 renderTarget->GetDesc(&desc);
2101
2102 IDirect3DSurface9 *systemSurface;
2103 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2104
2105 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2106 {
2107 return error(GL_OUT_OF_MEMORY);
2108 }
2109
2110 ASSERT(SUCCEEDED(result));
2111
2112 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2113 {
2114 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2115 }
2116
2117 result = device->GetRenderTargetData(renderTarget, systemSurface);
2118
2119 if (result == D3DERR_DRIVERINTERNALERROR)
2120 {
2121 systemSurface->Release();
2122
2123 return error(GL_OUT_OF_MEMORY);
2124 }
2125
2126 if (FAILED(result))
2127 {
2128 UNREACHABLE();
2129 systemSurface->Release();
2130
2131 return; // No sensible error to generate
2132 }
2133
2134 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002135 RECT rect = {std::max(x, 0),
2136 std::max(y, 0),
2137 std::min(x + width, (int)desc.Width),
2138 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139
2140 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2141
2142 if (FAILED(result))
2143 {
2144 UNREACHABLE();
2145 systemSurface->Release();
2146
2147 return; // No sensible error to generate
2148 }
2149
2150 unsigned char *source = (unsigned char*)lock.pBits;
2151 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002152 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002154 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002155
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 for (int j = 0; j < rect.bottom - rect.top; j++)
2157 {
2158 for (int i = 0; i < rect.right - rect.left; i++)
2159 {
2160 float r;
2161 float g;
2162 float b;
2163 float a;
2164
2165 switch (desc.Format)
2166 {
2167 case D3DFMT_R5G6B5:
2168 {
2169 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2170
2171 a = 1.0f;
2172 b = (rgb & 0x001F) * (1.0f / 0x001F);
2173 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2174 r = (rgb & 0xF800) * (1.0f / 0xF800);
2175 }
2176 break;
2177 case D3DFMT_X1R5G5B5:
2178 {
2179 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2180
2181 a = 1.0f;
2182 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2183 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2184 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2185 }
2186 break;
2187 case D3DFMT_A1R5G5B5:
2188 {
2189 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2190
2191 a = (argb & 0x8000) ? 1.0f : 0.0f;
2192 b = (argb & 0x001F) * (1.0f / 0x001F);
2193 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2194 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2195 }
2196 break;
2197 case D3DFMT_A8R8G8B8:
2198 {
2199 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2200
2201 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2202 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2203 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2204 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2205 }
2206 break;
2207 case D3DFMT_X8R8G8B8:
2208 {
2209 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2210
2211 a = 1.0f;
2212 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2213 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2214 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2215 }
2216 break;
2217 case D3DFMT_A2R10G10B10:
2218 {
2219 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2220
2221 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2222 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2223 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2224 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2225 }
2226 break;
2227 default:
2228 UNIMPLEMENTED(); // FIXME
2229 UNREACHABLE();
2230 }
2231
2232 switch (format)
2233 {
2234 case GL_RGBA:
2235 switch (type)
2236 {
2237 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002238 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2239 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2240 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2241 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 break;
2243 default: UNREACHABLE();
2244 }
2245 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002246 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247 switch (type)
2248 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002249 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002250 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2251 ((unsigned short)(31 * b + 0.5f) << 0) |
2252 ((unsigned short)(63 * g + 0.5f) << 5) |
2253 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 break;
2255 default: UNREACHABLE();
2256 }
2257 break;
2258 default: UNREACHABLE();
2259 }
2260 }
2261 }
2262
2263 systemSurface->UnlockRect();
2264
2265 systemSurface->Release();
2266}
2267
2268void Context::clear(GLbitfield mask)
2269{
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002270 Framebuffer *framebufferObject = getFramebuffer();
2271
2272 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2273 {
2274 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2275
2276 return;
2277 }
2278
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002279 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 IDirect3DDevice9 *device = getDevice();
2281 DWORD flags = 0;
2282
2283 if (mask & GL_COLOR_BUFFER_BIT)
2284 {
2285 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002286
2287 if (framebufferObject->getColorbufferType() != GL_NONE)
2288 {
2289 flags |= D3DCLEAR_TARGET;
2290 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 }
2292
2293 if (mask & GL_DEPTH_BUFFER_BIT)
2294 {
2295 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002296 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 {
2298 flags |= D3DCLEAR_ZBUFFER;
2299 }
2300 }
2301
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
2303
2304 GLuint stencilUnmasked = 0x0;
2305
2306 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
2307 {
2308 D3DSURFACE_DESC desc;
2309 depthStencil->GetDesc(&desc);
2310
2311 mask &= ~GL_STENCIL_BUFFER_BIT;
2312 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2313 stencilUnmasked = (0x1 << stencilSize) - 1;
2314
2315 if (stencilUnmasked != 0x0)
2316 {
2317 flags |= D3DCLEAR_STENCIL;
2318 }
2319 }
2320
2321 if (mask != 0)
2322 {
2323 return error(GL_INVALID_VALUE);
2324 }
2325
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002326 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2327 {
2328 return;
2329 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002331 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2332 unorm<8>(mState.colorClearValue.red),
2333 unorm<8>(mState.colorClearValue.green),
2334 unorm<8>(mState.colorClearValue.blue));
2335 float depth = clamp01(mState.depthClearValue);
2336 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
2338 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2339
2340 D3DSURFACE_DESC desc;
2341 renderTarget->GetDesc(&desc);
2342
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002343 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344
2345 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002346 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002348 !(mState.colorMaskRed && mState.colorMaskGreen &&
2349 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350
2351 if (needMaskedColorClear || needMaskedStencilClear)
2352 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002353 // State which is altered in all paths from this point to the clear call is saved.
2354 // State which is altered in only some paths will be flagged dirty in the case that
2355 // that path is taken.
2356 HRESULT hr;
2357 if (mMaskedClearSavedState == NULL)
2358 {
2359 hr = device->BeginStateBlock();
2360 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2361
2362 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2363 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2364 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2365 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2366 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2367 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2368 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2369 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2370 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2371 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2372 device->SetPixelShader(NULL);
2373 device->SetVertexShader(NULL);
2374 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2375 device->SetStreamSourceFreq(0, 1);
2376
2377 hr = device->EndStateBlock(&mMaskedClearSavedState);
2378 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2379 }
2380
2381 ASSERT(mMaskedClearSavedState != NULL);
2382
2383 if (mMaskedClearSavedState != NULL)
2384 {
2385 hr = mMaskedClearSavedState->Capture();
2386 ASSERT(SUCCEEDED(hr));
2387 }
2388
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2390 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2391 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2392 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2393 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2394 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2395 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2396 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2397
2398 if (flags & D3DCLEAR_TARGET)
2399 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002400 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2401 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2402 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2403 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 }
2405 else
2406 {
2407 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2408 }
2409
2410 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2411 {
2412 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2413 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2414 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2415 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002416 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002417 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2419 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002420 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422 else
2423 {
2424 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2425 }
2426
2427 device->SetPixelShader(NULL);
2428 device->SetVertexShader(NULL);
2429 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002430 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002432 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
2434 float x, y, z, w;
2435 D3DCOLOR diffuse;
2436 };
2437
2438 Vertex quad[4];
2439 quad[0].x = 0.0f;
2440 quad[0].y = (float)desc.Height;
2441 quad[0].z = 0.0f;
2442 quad[0].w = 1.0f;
2443 quad[0].diffuse = color;
2444
2445 quad[1].x = (float)desc.Width;
2446 quad[1].y = (float)desc.Height;
2447 quad[1].z = 0.0f;
2448 quad[1].w = 1.0f;
2449 quad[1].diffuse = color;
2450
2451 quad[2].x = 0.0f;
2452 quad[2].y = 0.0f;
2453 quad[2].z = 0.0f;
2454 quad[2].w = 1.0f;
2455 quad[2].diffuse = color;
2456
2457 quad[3].x = (float)desc.Width;
2458 quad[3].y = 0.0f;
2459 quad[3].z = 0.0f;
2460 quad[3].w = 1.0f;
2461 quad[3].diffuse = color;
2462
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002463 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465
2466 if (flags & D3DCLEAR_ZBUFFER)
2467 {
2468 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2469 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2470 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2471 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002472
2473 if (mMaskedClearSavedState != NULL)
2474 {
2475 mMaskedClearSavedState->Apply();
2476 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002478 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 {
2480 device->Clear(0, NULL, flags, color, depth, stencil);
2481 }
2482}
2483
2484void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2485{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002486 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 {
2488 return error(GL_INVALID_OPERATION);
2489 }
2490
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002491 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 IDirect3DDevice9 *device = getDevice();
2493 D3DPRIMITIVETYPE primitiveType;
2494 int primitiveCount;
2495
2496 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2497 return error(GL_INVALID_ENUM);
2498
2499 if (primitiveCount <= 0)
2500 {
2501 return;
2502 }
2503
2504 if (!applyRenderTarget(false))
2505 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002506 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 }
2508
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002509 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002510
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002511 TranslatedIndexData indexInfo;
2512 bool useIndexing;
2513 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002514 if (err != GL_NO_ERROR)
2515 {
2516 return error(err);
2517 }
2518
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519 applyShaders();
2520 applyTextures();
2521
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002522 if (!getCurrentProgram()->validateSamplers())
2523 {
2524 return error(GL_INVALID_OPERATION);
2525 }
2526
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002527 if (!cullSkipsDraw(mode))
2528 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002529 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002530 if (useIndexing)
2531 {
2532 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2533 }
2534 else
2535 {
2536 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2537 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539}
2540
2541void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2542{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002543 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 {
2545 return error(GL_INVALID_OPERATION);
2546 }
2547
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002548 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549 {
2550 return error(GL_INVALID_OPERATION);
2551 }
2552
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002553 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 IDirect3DDevice9 *device = getDevice();
2555 D3DPRIMITIVETYPE primitiveType;
2556 int primitiveCount;
2557
2558 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2559 return error(GL_INVALID_ENUM);
2560
2561 if (primitiveCount <= 0)
2562 {
2563 return;
2564 }
2565
2566 if (!applyRenderTarget(false))
2567 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002568 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569 }
2570
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002571 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002572
2573 TranslatedIndexData indexInfo;
2574 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2575 if (err != GL_NO_ERROR)
2576 {
2577 return error(err);
2578 }
2579
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002580 err = applyVertexBuffer(indexInfo);
2581 if (err != GL_NO_ERROR)
2582 {
2583 return error(err);
2584 }
2585
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586 applyShaders();
2587 applyTextures();
2588
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002589 if (!getCurrentProgram()->validateSamplers())
2590 {
2591 return error(GL_INVALID_OPERATION);
2592 }
2593
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002594 if (!cullSkipsDraw(mode))
2595 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002596 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002597 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 +00002598 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599}
2600
2601void Context::finish()
2602{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002603 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 IDirect3DDevice9 *device = getDevice();
2605 IDirect3DQuery9 *occlusionQuery = NULL;
2606
2607 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2608
2609 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2610 {
2611 return error(GL_OUT_OF_MEMORY);
2612 }
2613
2614 ASSERT(SUCCEEDED(result));
2615
2616 if (occlusionQuery)
2617 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002618 IDirect3DStateBlock9 *savedState = NULL;
2619 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2620
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 occlusionQuery->Issue(D3DISSUE_BEGIN);
2622
2623 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002624 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625 device->SetPixelShader(NULL);
2626 device->SetVertexShader(NULL);
2627 device->SetFVF(D3DFVF_XYZRHW);
2628 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002629 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631
2632 occlusionQuery->Issue(D3DISSUE_END);
2633
2634 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2635 {
2636 // Keep polling, but allow other threads to do something useful first
2637 Sleep(0);
2638 }
2639
2640 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002641
2642 if (savedState)
2643 {
2644 savedState->Apply();
2645 savedState->Release();
2646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002647 }
2648}
2649
2650void Context::flush()
2651{
2652 IDirect3DDevice9 *device = getDevice();
2653 IDirect3DQuery9 *eventQuery = NULL;
2654
2655 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2656
2657 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2658 {
2659 return error(GL_OUT_OF_MEMORY);
2660 }
2661
2662 ASSERT(SUCCEEDED(result));
2663
2664 if (eventQuery)
2665 {
2666 eventQuery->Issue(D3DISSUE_END);
2667
2668 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2669 {
2670 // Keep polling, but allow other threads to do something useful first
2671 Sleep(0);
2672 }
2673
2674 eventQuery->Release();
2675 }
2676}
2677
2678void Context::recordInvalidEnum()
2679{
2680 mInvalidEnum = true;
2681}
2682
2683void Context::recordInvalidValue()
2684{
2685 mInvalidValue = true;
2686}
2687
2688void Context::recordInvalidOperation()
2689{
2690 mInvalidOperation = true;
2691}
2692
2693void Context::recordOutOfMemory()
2694{
2695 mOutOfMemory = true;
2696}
2697
2698void Context::recordInvalidFramebufferOperation()
2699{
2700 mInvalidFramebufferOperation = true;
2701}
2702
2703// Get one of the recorded errors and clear its flag, if any.
2704// [OpenGL ES 2.0.24] section 2.5 page 13.
2705GLenum Context::getError()
2706{
2707 if (mInvalidEnum)
2708 {
2709 mInvalidEnum = false;
2710
2711 return GL_INVALID_ENUM;
2712 }
2713
2714 if (mInvalidValue)
2715 {
2716 mInvalidValue = false;
2717
2718 return GL_INVALID_VALUE;
2719 }
2720
2721 if (mInvalidOperation)
2722 {
2723 mInvalidOperation = false;
2724
2725 return GL_INVALID_OPERATION;
2726 }
2727
2728 if (mOutOfMemory)
2729 {
2730 mOutOfMemory = false;
2731
2732 return GL_OUT_OF_MEMORY;
2733 }
2734
2735 if (mInvalidFramebufferOperation)
2736 {
2737 mInvalidFramebufferOperation = false;
2738
2739 return GL_INVALID_FRAMEBUFFER_OPERATION;
2740 }
2741
2742 return GL_NO_ERROR;
2743}
2744
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002745const char *Context::getPixelShaderProfile()
2746{
2747 return mPsProfile;
2748}
2749
2750const char *Context::getVertexShaderProfile()
2751{
2752 return mVsProfile;
2753}
2754
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755void Context::detachBuffer(GLuint buffer)
2756{
2757 // [OpenGL ES 2.0.24] section 2.9 page 22:
2758 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2759 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2760
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002761 if (mState.arrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002763 mState.arrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 }
2765
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002766 if (mState.elementArrayBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002768 mState.elementArrayBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 }
2770
2771 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2772 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002773 if (mState.vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002775 mState.vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 }
2777 }
2778}
2779
2780void Context::detachTexture(GLuint texture)
2781{
2782 // [OpenGL ES 2.0.24] section 3.8 page 84:
2783 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2784 // rebound to texture object zero
2785
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002786 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002788 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002790 if (mState.samplerTexture[type][sampler] == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002791 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002792 mState.samplerTexture[type][sampler] = 0;
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 }
2795 }
2796
2797 // [OpenGL ES 2.0.24] section 4.4 page 112:
2798 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2799 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2800 // image was attached in the currently bound framebuffer.
2801
2802 Framebuffer *framebuffer = getFramebuffer();
2803
2804 if (framebuffer)
2805 {
2806 framebuffer->detachTexture(texture);
2807 }
2808}
2809
2810void Context::detachFramebuffer(GLuint framebuffer)
2811{
2812 // [OpenGL ES 2.0.24] section 4.4 page 107:
2813 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2814 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2815
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002816 if (mState.framebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 {
2818 bindFramebuffer(0);
2819 }
2820}
2821
2822void Context::detachRenderbuffer(GLuint renderbuffer)
2823{
2824 // [OpenGL ES 2.0.24] section 4.4 page 109:
2825 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2826 // had been executed with the target RENDERBUFFER and name of zero.
2827
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002828 if (mState.renderbuffer == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 {
2830 bindRenderbuffer(0);
2831 }
2832
2833 // [OpenGL ES 2.0.24] section 4.4 page 111:
2834 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2835 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2836 // point to which this image was attached in the currently bound framebuffer.
2837
2838 Framebuffer *framebuffer = getFramebuffer();
2839
2840 if (framebuffer)
2841 {
2842 framebuffer->detachRenderbuffer(renderbuffer);
2843 }
2844}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002845
2846Texture *Context::getIncompleteTexture(SamplerType type)
2847{
2848 Texture *t = mIncompleteTextures[type];
2849
2850 if (t == NULL)
2851 {
2852 static const GLubyte color[] = { 0, 0, 0, 255 };
2853
2854 switch (type)
2855 {
2856 default:
2857 UNREACHABLE();
2858 // default falls through to SAMPLER_2D
2859
2860 case SAMPLER_2D:
2861 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002862 Texture2D *incomplete2d = new Texture2D(this);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002863 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002864 t = incomplete2d;
2865 }
2866 break;
2867
2868 case SAMPLER_CUBE:
2869 {
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00002870 TextureCubeMap *incompleteCube = new TextureCubeMap(this);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002871
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002872 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2873 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2874 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2875 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2876 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2877 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002878
2879 t = incompleteCube;
2880 }
2881 break;
2882 }
2883
2884 mIncompleteTextures[type] = t;
2885 }
2886
2887 return t;
2888}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002889
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002890bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002891{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002892 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002893}
2894
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002895bool Context::isTriangleMode(GLenum drawMode)
2896{
2897 switch (drawMode)
2898 {
2899 case GL_TRIANGLES:
2900 case GL_TRIANGLE_FAN:
2901 case GL_TRIANGLE_STRIP:
2902 return true;
2903 case GL_POINTS:
2904 case GL_LINES:
2905 case GL_LINE_LOOP:
2906 case GL_LINE_STRIP:
2907 return false;
2908 default: UNREACHABLE();
2909 }
2910
2911 return false;
2912}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002913
daniel@transgaming.com5bd0ce32010-05-07 13:03:31 +00002914bool Context::hasStencil()
2915{
2916 Framebuffer *framebufferObject = getFramebuffer();
2917
2918 if (framebufferObject)
2919 {
2920 Stencilbuffer *stencilbufferObject = framebufferObject->getStencilbuffer();
2921
2922 if (stencilbufferObject)
2923 {
2924 return stencilbufferObject->getStencilSize() > 0;
2925 }
2926 }
2927
2928 return false;
2929}
2930
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002931void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2932{
2933 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2934
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002935 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2936 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2937 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2938 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002939
2940 mVertexDataManager->dirtyCurrentValues();
2941}
2942
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002943void Context::initExtensionString()
2944{
2945 if (mBufferBackEnd->supportIntIndices())
2946 {
2947 mExtensionString += "GL_OES_element_index_uint ";
2948 }
2949
2950 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2951 if (end != std::string::npos)
2952 {
2953 mExtensionString.resize(end+1);
2954 }
2955}
2956
2957const char *Context::getExtensionString() const
2958{
2959 return mExtensionString.c_str();
2960}
2961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002962}
2963
2964extern "C"
2965{
2966gl::Context *glCreateContext(const egl::Config *config)
2967{
2968 return new gl::Context(config);
2969}
2970
2971void glDestroyContext(gl::Context *context)
2972{
2973 delete context;
2974
2975 if (context == gl::getContext())
2976 {
2977 gl::makeCurrent(NULL, NULL, NULL);
2978 }
2979}
2980
2981void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2982{
2983 gl::makeCurrent(context, display, surface);
2984}
2985
2986gl::Context *glGetCurrentContext()
2987{
2988 return gl::getContext();
2989}
2990}