blob: 65b65a52291c5d4bc0d40629931c88d50bab3224 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000087 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000090
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.viewportX = 0;
92 mState.viewportY = 0;
93 mState.viewportWidth = config->mDisplayMode.Width;
94 mState.viewportHeight = config->mDisplayMode.Height;
95 mState.zNear = 0.0f;
96 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.scissorX = 0;
99 mState.scissorY = 0;
100 mState.scissorWidth = config->mDisplayMode.Width;
101 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.colorMaskRed = true;
104 mState.colorMaskGreen = true;
105 mState.colorMaskBlue = true;
106 mState.colorMaskAlpha = true;
107 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000109 if (shareContext != NULL)
110 {
111 mResourceManager = shareContext->mResourceManager;
112 mResourceManager->addRef();
113 }
114 else
115 {
116 mResourceManager = new ResourceManager();
117 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 // [OpenGL ES 2.0.24] section 3.7 page 83:
120 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
121 // and cube map texture state vectors respectively associated with them.
122 // In order that access to these initial textures not be lost, they are treated as texture
123 // objects all of whose names are 0.
124
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000125 mTexture2DZero.set(new Texture2D(0));
126 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000128 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000129 bindArrayBuffer(0);
130 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 bindTextureCubeMap(0);
132 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000133 bindReadFramebuffer(0);
134 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindRenderbuffer(0);
136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000139 mState.packAlignment = 4;
140 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000141
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 mBufferBackEnd = NULL;
143 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000144 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000145 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 mInvalidEnum = false;
148 mInvalidValue = false;
149 mInvalidOperation = false;
150 mOutOfMemory = false;
151 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000152
153 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000154
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000155 mSupportsCompressedTextures = false;
156 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000157 mMaxSupportedSamples = 0;
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.comda13f3e2010-07-28 19:20:56 +0000164 if (mState.currentProgram != 0)
165 {
166 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
167 if (programObject)
168 {
169 programObject->release();
170 }
171 mState.currentProgram = 0;
172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000174 while (!mFramebufferMap.empty())
175 {
176 deleteFramebuffer(mFramebufferMap.begin()->first);
177 }
178
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000179 while (!mFenceMap.empty())
180 {
181 deleteFence(mFenceMap.begin()->first);
182 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000183
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000184 while (!mMultiSampleSupport.empty())
185 {
186 delete [] mMultiSampleSupport.begin()->second;
187 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
188 }
189
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000190 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
191 {
192 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
193 {
194 mState.samplerTexture[type][sampler].set(NULL);
195 }
196 }
197
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000200 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000201 }
202
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
204 {
205 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
206 }
207
208 mState.arrayBuffer.set(NULL);
209 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 mState.renderbuffer.set(NULL);
211
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000212 mTexture2DZero.set(NULL);
213 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000215 delete mBufferBackEnd;
216 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000217 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000218 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000220 if (mMaskedClearSavedState)
221 {
222 mMaskedClearSavedState->Release();
223 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000224
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000225 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
229{
230 IDirect3DDevice9 *device = display->getDevice();
231
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000232 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000233 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000234 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000235
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000236 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000238 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000239 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000240
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000241 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
242
243 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
244 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
245 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
246 mMaxRenderbufferDimension = mMaxTextureDimension;
247 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
248 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
249 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
250
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000251 const D3DFORMAT renderBufferFormats[] =
252 {
253 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000254 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000255 D3DFMT_R5G6B5,
256 D3DFMT_D24S8
257 };
258
259 int max = 0;
260 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
261 {
262 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
263 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
264 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
265
266 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
267 {
268 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
269 {
270 max = j;
271 }
272 }
273 }
274
275 mMaxSupportedSamples = max;
276
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000277 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000278 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000279 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
280 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000281 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
282 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000283
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000284 initExtensionString();
285
286 mState.viewportX = 0;
287 mState.viewportY = 0;
288 mState.viewportWidth = surface->getWidth();
289 mState.viewportHeight = surface->getHeight();
290
291 mState.scissorX = 0;
292 mState.scissorY = 0;
293 mState.scissorWidth = surface->getWidth();
294 mState.scissorHeight = surface->getHeight();
295
296 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000297 }
298
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
300 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000301 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000304 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000305 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
307 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000309 if (defaultRenderTarget)
310 {
311 defaultRenderTarget->Release();
312 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000314 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000316 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000318
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000319 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320}
321
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000322// 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 +0000323void Context::markAllStateDirty()
324{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000326 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000327 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000328 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000329 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000330
331 mClearStateDirty = true;
332 mCullStateDirty = true;
333 mDepthStateDirty = true;
334 mMaskStateDirty = true;
335 mBlendStateDirty = true;
336 mStencilStateDirty = true;
337 mPolygonOffsetStateDirty = true;
338 mScissorStateDirty = true;
339 mSampleStateDirty = true;
340 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000341 mFrontFaceDirty = true;
342
343 if (mBufferBackEnd != NULL)
344 {
345 mBufferBackEnd->invalidate();
346 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000347}
348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349void Context::setClearColor(float red, float green, float blue, float alpha)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.colorClearValue.red = red;
352 mState.colorClearValue.green = green;
353 mState.colorClearValue.blue = blue;
354 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355}
356
357void Context::setClearDepth(float depth)
358{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000359 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362void Context::setClearStencil(int stencil)
363{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000364 mState.stencilClearValue = stencil;
365}
366
367void Context::setCullFace(bool enabled)
368{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000369 if (mState.cullFace != enabled)
370 {
371 mState.cullFace = enabled;
372 mCullStateDirty = true;
373 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000374}
375
376bool Context::isCullFaceEnabled() const
377{
378 return mState.cullFace;
379}
380
381void Context::setCullMode(GLenum mode)
382{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000383 if (mState.cullMode != mode)
384 {
385 mState.cullMode = mode;
386 mCullStateDirty = true;
387 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000388}
389
390void Context::setFrontFace(GLenum front)
391{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000392 if (mState.frontFace != front)
393 {
394 mState.frontFace = front;
395 mFrontFaceDirty = true;
396 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000397}
398
399void Context::setDepthTest(bool enabled)
400{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000401 if (mState.depthTest != enabled)
402 {
403 mState.depthTest = enabled;
404 mDepthStateDirty = true;
405 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000406}
407
408bool Context::isDepthTestEnabled() const
409{
410 return mState.depthTest;
411}
412
413void Context::setDepthFunc(GLenum depthFunc)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.depthFunc != depthFunc)
416 {
417 mState.depthFunc = depthFunc;
418 mDepthStateDirty = true;
419 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420}
421
422void Context::setDepthRange(float zNear, float zFar)
423{
424 mState.zNear = zNear;
425 mState.zFar = zFar;
426}
427
428void Context::setBlend(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.blend != enabled)
431 {
432 mState.blend = enabled;
433 mBlendStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isBlendEnabled() const
438{
439 return mState.blend;
440}
441
442void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.sourceBlendRGB != sourceRGB ||
445 mState.sourceBlendAlpha != sourceAlpha ||
446 mState.destBlendRGB != destRGB ||
447 mState.destBlendAlpha != destAlpha)
448 {
449 mState.sourceBlendRGB = sourceRGB;
450 mState.destBlendRGB = destRGB;
451 mState.sourceBlendAlpha = sourceAlpha;
452 mState.destBlendAlpha = destAlpha;
453 mBlendStateDirty = true;
454 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000455}
456
457void Context::setBlendColor(float red, float green, float blue, float alpha)
458{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000459 if (mState.blendColor.red != red ||
460 mState.blendColor.green != green ||
461 mState.blendColor.blue != blue ||
462 mState.blendColor.alpha != alpha)
463 {
464 mState.blendColor.red = red;
465 mState.blendColor.green = green;
466 mState.blendColor.blue = blue;
467 mState.blendColor.alpha = alpha;
468 mBlendStateDirty = true;
469 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000470}
471
472void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
473{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000474 if (mState.blendEquationRGB != rgbEquation ||
475 mState.blendEquationAlpha != alphaEquation)
476 {
477 mState.blendEquationRGB = rgbEquation;
478 mState.blendEquationAlpha = alphaEquation;
479 mBlendStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setStencilTest(bool enabled)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.stencilTest != enabled)
486 {
487 mState.stencilTest = enabled;
488 mStencilStateDirty = true;
489 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000490}
491
492bool Context::isStencilTestEnabled() const
493{
494 return mState.stencilTest;
495}
496
497void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
498{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 if (mState.stencilFunc != stencilFunc ||
500 mState.stencilRef != stencilRef ||
501 mState.stencilMask != stencilMask)
502 {
503 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000504 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 mState.stencilMask = stencilMask;
506 mStencilStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.stencilBackFunc != stencilBackFunc ||
513 mState.stencilBackRef != stencilBackRef ||
514 mState.stencilBackMask != stencilBackMask)
515 {
516 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000517 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000518 mState.stencilBackMask = stencilBackMask;
519 mStencilStateDirty = true;
520 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000521}
522
523void Context::setStencilWritemask(GLuint stencilWritemask)
524{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000525 if (mState.stencilWritemask != stencilWritemask)
526 {
527 mState.stencilWritemask = stencilWritemask;
528 mStencilStateDirty = true;
529 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000530}
531
532void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
533{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000534 if (mState.stencilBackWritemask != stencilBackWritemask)
535 {
536 mState.stencilBackWritemask = stencilBackWritemask;
537 mStencilStateDirty = true;
538 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000539}
540
541void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
542{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 if (mState.stencilFail != stencilFail ||
544 mState.stencilPassDepthFail != stencilPassDepthFail ||
545 mState.stencilPassDepthPass != stencilPassDepthPass)
546 {
547 mState.stencilFail = stencilFail;
548 mState.stencilPassDepthFail = stencilPassDepthFail;
549 mState.stencilPassDepthPass = stencilPassDepthPass;
550 mStencilStateDirty = true;
551 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000552}
553
554void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
555{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 if (mState.stencilBackFail != stencilBackFail ||
557 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
558 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
559 {
560 mState.stencilBackFail = stencilBackFail;
561 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
562 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
563 mStencilStateDirty = true;
564 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000565}
566
567void Context::setPolygonOffsetFill(bool enabled)
568{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569 if (mState.polygonOffsetFill != enabled)
570 {
571 mState.polygonOffsetFill = enabled;
572 mPolygonOffsetStateDirty = true;
573 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000574}
575
576bool Context::isPolygonOffsetFillEnabled() const
577{
578 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000580}
581
582void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
583{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000584 if (mState.polygonOffsetFactor != factor ||
585 mState.polygonOffsetUnits != units)
586 {
587 mState.polygonOffsetFactor = factor;
588 mState.polygonOffsetUnits = units;
589 mPolygonOffsetStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setSampleAlphaToCoverage(bool enabled)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.sampleAlphaToCoverage != enabled)
596 {
597 mState.sampleAlphaToCoverage = enabled;
598 mSampleStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602bool Context::isSampleAlphaToCoverageEnabled() const
603{
604 return mState.sampleAlphaToCoverage;
605}
606
607void Context::setSampleCoverage(bool enabled)
608{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000609 if (mState.sampleCoverage != enabled)
610 {
611 mState.sampleCoverage = enabled;
612 mSampleStateDirty = true;
613 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614}
615
616bool Context::isSampleCoverageEnabled() const
617{
618 return mState.sampleCoverage;
619}
620
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000621void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.sampleCoverageValue != value ||
624 mState.sampleCoverageInvert != invert)
625 {
626 mState.sampleCoverageValue = value;
627 mState.sampleCoverageInvert = invert;
628 mSampleStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setScissorTest(bool enabled)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.scissorTest != enabled)
635 {
636 mState.scissorTest = enabled;
637 mScissorStateDirty = true;
638 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000639}
640
641bool Context::isScissorTestEnabled() const
642{
643 return mState.scissorTest;
644}
645
646void Context::setDither(bool enabled)
647{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000648 if (mState.dither != enabled)
649 {
650 mState.dither = enabled;
651 mDitherStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655bool Context::isDitherEnabled() const
656{
657 return mState.dither;
658}
659
660void Context::setLineWidth(GLfloat width)
661{
662 mState.lineWidth = width;
663}
664
665void Context::setGenerateMipmapHint(GLenum hint)
666{
667 mState.generateMipmapHint = hint;
668}
669
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000670void Context::setFragmentShaderDerivativeHint(GLenum hint)
671{
672 mState.fragmentShaderDerivativeHint = hint;
673 // TODO: Propagate the hint to shader translator so we can write
674 // ddx, ddx_coarse, or ddx_fine depending on the hint.
675 // Ignore for now. It is valid for implementations to ignore hint.
676}
677
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
680 mState.viewportX = x;
681 mState.viewportY = y;
682 mState.viewportWidth = width;
683 mState.viewportHeight = height;
684}
685
686void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
687{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000688 if (mState.scissorX != x || mState.scissorY != y ||
689 mState.scissorWidth != width || mState.scissorHeight != height)
690 {
691 mState.scissorX = x;
692 mState.scissorY = y;
693 mState.scissorWidth = width;
694 mState.scissorHeight = height;
695 mScissorStateDirty = true;
696 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000697}
698
699void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
700{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000701 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
702 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
703 {
704 mState.colorMaskRed = red;
705 mState.colorMaskGreen = green;
706 mState.colorMaskBlue = blue;
707 mState.colorMaskAlpha = alpha;
708 mMaskStateDirty = true;
709 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000710}
711
712void Context::setDepthMask(bool mask)
713{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000714 if (mState.depthMask != mask)
715 {
716 mState.depthMask = mask;
717 mMaskStateDirty = true;
718 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719}
720
721void Context::setActiveSampler(int active)
722{
723 mState.activeSampler = active;
724}
725
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000726GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000727{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000728 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000729}
730
731GLuint Context::getDrawFramebufferHandle() const
732{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000733 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734}
735
736GLuint Context::getRenderbufferHandle() const
737{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000738 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739}
740
741GLuint Context::getArrayBufferHandle() const
742{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000743 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744}
745
746void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
747{
748 mState.vertexAttribute[attribNum].mEnabled = enabled;
749}
750
751const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
752{
753 return mState.vertexAttribute[attribNum];
754}
755
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000756void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757 GLsizei stride, const void *pointer)
758{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000759 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760 mState.vertexAttribute[attribNum].mSize = size;
761 mState.vertexAttribute[attribNum].mType = type;
762 mState.vertexAttribute[attribNum].mNormalized = normalized;
763 mState.vertexAttribute[attribNum].mStride = stride;
764 mState.vertexAttribute[attribNum].mPointer = pointer;
765}
766
767const void *Context::getVertexAttribPointer(unsigned int attribNum) const
768{
769 return mState.vertexAttribute[attribNum].mPointer;
770}
771
772// returns entire set of attributes as a block
773const AttributeState *Context::getVertexAttribBlock()
774{
775 return mState.vertexAttribute;
776}
777
778void Context::setPackAlignment(GLint alignment)
779{
780 mState.packAlignment = alignment;
781}
782
783GLint Context::getPackAlignment() const
784{
785 return mState.packAlignment;
786}
787
788void Context::setUnpackAlignment(GLint alignment)
789{
790 mState.unpackAlignment = alignment;
791}
792
793GLint Context::getUnpackAlignment() const
794{
795 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798GLuint Context::createBuffer()
799{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803GLuint Context::createProgram()
804{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000808GLuint Context::createShader(GLenum type)
809{
810 return mResourceManager->createShader(type);
811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createTexture()
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 return mResourceManager->createTexture();
816}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000818GLuint Context::createRenderbuffer()
819{
820 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000823// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createFramebuffer()
825{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000826 unsigned int handle = 1;
827
828 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
829 {
830 handle++;
831 }
832
833 mFramebufferMap[handle] = NULL;
834
835 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000838GLuint Context::createFence()
839{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000840 unsigned int handle = 0;
841
842 while (mFenceMap.find(handle) != mFenceMap.end())
843 {
844 handle++;
845 }
846
847 mFenceMap[handle] = new Fence;
848
849 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000850}
851
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852void Context::deleteBuffer(GLuint buffer)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteShader(GLuint shader)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteProgram(GLuint program)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteTexture(GLuint texture)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878
879 mResourceManager->deleteTexture(texture);
880}
881
882void Context::deleteRenderbuffer(GLuint renderbuffer)
883{
884 if (mResourceManager->getRenderbuffer(renderbuffer))
885 {
886 detachRenderbuffer(renderbuffer);
887 }
888
889 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
892void Context::deleteFramebuffer(GLuint framebuffer)
893{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
895
896 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897 {
898 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000899
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000900 delete framebufferObject->second;
901 mFramebufferMap.erase(framebufferObject);
902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000904
905void Context::deleteFence(GLuint fence)
906{
907 FenceMap::iterator fenceObject = mFenceMap.find(fence);
908
909 if (fenceObject != mFenceMap.end())
910 {
911 delete fenceObject->second;
912 mFenceMap.erase(fenceObject);
913 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000914}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918 return mResourceManager->getBuffer(handle);
919}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921Shader *Context::getShader(GLuint handle)
922{
923 return mResourceManager->getShader(handle);
924}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926Program *Context::getProgram(GLuint handle)
927{
928 return mResourceManager->getProgram(handle);
929}
930
931Texture *Context::getTexture(GLuint handle)
932{
933 return mResourceManager->getTexture(handle);
934}
935
936Renderbuffer *Context::getRenderbuffer(GLuint handle)
937{
938 return mResourceManager->getRenderbuffer(handle);
939}
940
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000941Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943 return getFramebuffer(mState.readFramebuffer);
944}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000945
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946Framebuffer *Context::getDrawFramebuffer()
947{
948 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindArrayBuffer(unsigned int buffer)
952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
958void Context::bindElementArrayBuffer(unsigned int buffer)
959{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000960 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000962 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
965void Context::bindTexture2D(GLuint texture)
966{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000969 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
972void Context::bindTextureCubeMap(GLuint texture)
973{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000976 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000979void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000981 if (!getFramebuffer(framebuffer))
982 {
983 mFramebufferMap[framebuffer] = new Framebuffer();
984 }
985
986 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000987}
988
989void Context::bindDrawFramebuffer(GLuint framebuffer)
990{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000991 if (!getFramebuffer(framebuffer))
992 {
993 mFramebufferMap[framebuffer] = new Framebuffer();
994 }
995
996 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
999void Context::bindRenderbuffer(GLuint renderbuffer)
1000{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001001 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1002
1003 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
1006void Context::useProgram(GLuint program)
1007{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001008 GLuint priorProgram = mState.currentProgram;
1009 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001010
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001011 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 Program *newProgram = mResourceManager->getProgram(program);
1014 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1015
1016 if (newProgram)
1017 {
1018 newProgram->addRef();
1019 }
1020
1021 if (oldProgram)
1022 {
1023 oldProgram->release();
1024 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
1028void Context::setFramebufferZero(Framebuffer *buffer)
1029{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001030 delete mFramebufferMap[0];
1031 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032}
1033
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001034void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001036 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1037 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001040Framebuffer *Context::getFramebuffer(unsigned int handle)
1041{
1042 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1043
1044 if (framebuffer == mFramebufferMap.end())
1045 {
1046 return NULL;
1047 }
1048 else
1049 {
1050 return framebuffer->second;
1051 }
1052}
1053
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001054Fence *Context::getFence(unsigned int handle)
1055{
1056 FenceMap::iterator fence = mFenceMap.find(handle);
1057
1058 if (fence == mFenceMap.end())
1059 {
1060 return NULL;
1061 }
1062 else
1063 {
1064 return fence->second;
1065 }
1066}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001067
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068Buffer *Context::getArrayBuffer()
1069{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001070 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
1073Buffer *Context::getElementArrayBuffer()
1074{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001075 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076}
1077
1078Program *Context::getCurrentProgram()
1079{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001080 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
1083Texture2D *Context::getTexture2D()
1084{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001085 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088TextureCubeMap *Context::getTextureCubeMap()
1089{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001090 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091}
1092
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001093Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001095 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001096
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001097 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001098 {
1099 switch (type)
1100 {
1101 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001102 case SAMPLER_2D: return mTexture2DZero.get();
1103 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001104 }
1105 }
1106
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001107 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108}
1109
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001110bool Context::getBooleanv(GLenum pname, GLboolean *params)
1111{
1112 switch (pname)
1113 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001114 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1115 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1116 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001117 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001118 params[0] = mState.colorMaskRed;
1119 params[1] = mState.colorMaskGreen;
1120 params[2] = mState.colorMaskBlue;
1121 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001122 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001123 case GL_CULL_FACE: *params = mState.cullFace; break;
1124 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1125 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1126 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1127 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1128 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1129 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1130 case GL_BLEND: *params = mState.blend; break;
1131 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001132 default:
1133 return false;
1134 }
1135
1136 return true;
1137}
1138
1139bool Context::getFloatv(GLenum pname, GLfloat *params)
1140{
1141 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1142 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1143 // GetIntegerv as its native query function. As it would require conversion in any
1144 // case, this should make no difference to the calling application.
1145 switch (pname)
1146 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001147 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1148 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1149 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1150 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1151 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001152 case GL_ALIASED_LINE_WIDTH_RANGE:
1153 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1154 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1155 break;
1156 case GL_ALIASED_POINT_SIZE_RANGE:
1157 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001158 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001159 break;
1160 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 params[0] = mState.zNear;
1162 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001163 break;
1164 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001165 params[0] = mState.colorClearValue.red;
1166 params[1] = mState.colorClearValue.green;
1167 params[2] = mState.colorClearValue.blue;
1168 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001170 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.blendColor.red;
1172 params[1] = mState.blendColor.green;
1173 params[2] = mState.blendColor.blue;
1174 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001175 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001176 default:
1177 return false;
1178 }
1179
1180 return true;
1181}
1182
1183bool Context::getIntegerv(GLenum pname, GLint *params)
1184{
1185 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1186 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1187 // GetIntegerv as its native query function. As it would require conversion in any
1188 // case, this should make no difference to the calling application. You may find it in
1189 // Context::getFloatv.
1190 switch (pname)
1191 {
1192 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1193 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001194 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001195 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1196 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1197 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1198 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001199 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001200 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001201 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001202 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1203 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001204 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1205 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1206 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001207 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001208 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1209 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1210 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1211 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001212 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001213 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1214 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1215 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1216 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1217 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1218 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1219 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1220 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1221 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1222 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1223 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1224 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1225 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1226 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1227 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1228 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1229 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1230 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1231 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1232 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1233 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1234 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1235 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1236 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001237 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1238 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001239 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1240 {
1241 if (supportsCompressedTextures())
1242 {
1243 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1244 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1245 *params = 2;
1246 }
1247 else
1248 {
1249 *params = 0;
1250 }
1251 }
1252 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001253 case GL_MAX_SAMPLES_ANGLE:
1254 {
1255 GLsizei maxSamples = getMaxSupportedSamples();
1256 if (maxSamples != 0)
1257 {
1258 *params = maxSamples;
1259 }
1260 else
1261 {
1262 return false;
1263 }
1264
1265 break;
1266 }
1267 case GL_SAMPLE_BUFFERS:
1268 case GL_SAMPLES:
1269 {
1270 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1271 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1272 {
1273 switch (pname)
1274 {
1275 case GL_SAMPLE_BUFFERS:
1276 if (framebuffer->getSamples() != 0)
1277 {
1278 *params = 1;
1279 }
1280 else
1281 {
1282 *params = 0;
1283 }
1284 break;
1285 case GL_SAMPLES:
1286 *params = framebuffer->getSamples();
1287 break;
1288 }
1289 }
1290 else
1291 {
1292 *params = 0;
1293 }
1294 }
1295 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001296 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1297 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1298 case GL_MAX_VIEWPORT_DIMS:
1299 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001300 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001301 params[0] = maxDimension;
1302 params[1] = maxDimension;
1303 }
1304 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001305 case GL_COMPRESSED_TEXTURE_FORMATS:
1306 {
1307 if (supportsCompressedTextures())
1308 {
1309 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1310 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1311 }
1312 }
1313 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001314 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001315 params[0] = mState.viewportX;
1316 params[1] = mState.viewportY;
1317 params[2] = mState.viewportWidth;
1318 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001319 break;
1320 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 params[0] = mState.scissorX;
1322 params[1] = mState.scissorY;
1323 params[2] = mState.scissorWidth;
1324 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001326 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1327 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 case GL_RED_BITS:
1329 case GL_GREEN_BITS:
1330 case GL_BLUE_BITS:
1331 case GL_ALPHA_BITS:
1332 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001333 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1335
1336 if (colorbuffer)
1337 {
1338 switch (pname)
1339 {
1340 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1341 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1342 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1343 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1344 }
1345 }
1346 else
1347 {
1348 *params = 0;
1349 }
1350 }
1351 break;
1352 case GL_DEPTH_BITS:
1353 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001354 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001355 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001356
1357 if (depthbuffer)
1358 {
1359 *params = depthbuffer->getDepthSize();
1360 }
1361 else
1362 {
1363 *params = 0;
1364 }
1365 }
1366 break;
1367 case GL_STENCIL_BITS:
1368 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001369 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001370 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371
1372 if (stencilbuffer)
1373 {
1374 *params = stencilbuffer->getStencilSize();
1375 }
1376 else
1377 {
1378 *params = 0;
1379 }
1380 }
1381 break;
1382 case GL_TEXTURE_BINDING_2D:
1383 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001384 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385 {
1386 error(GL_INVALID_OPERATION);
1387 return false;
1388 }
1389
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001390 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 }
1392 break;
1393 case GL_TEXTURE_BINDING_CUBE_MAP:
1394 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001395 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001396 {
1397 error(GL_INVALID_OPERATION);
1398 return false;
1399 }
1400
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001401 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 }
1403 break;
1404 default:
1405 return false;
1406 }
1407
1408 return true;
1409}
1410
1411bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1412{
1413 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1414 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1415 // to the fact that it is stored internally as a float, and so would require conversion
1416 // if returned from Context::getIntegerv. Since this conversion is already implemented
1417 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1418 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1419 // application.
1420 switch (pname)
1421 {
1422 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1423 case GL_SHADER_BINARY_FORMATS:
1424 {
1425 *type = GL_INT;
1426 *numParams = 0;
1427 }
1428 break;
1429 case GL_MAX_VERTEX_ATTRIBS:
1430 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1431 case GL_MAX_VARYING_VECTORS:
1432 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1433 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1434 case GL_MAX_TEXTURE_IMAGE_UNITS:
1435 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1436 case GL_MAX_RENDERBUFFER_SIZE:
1437 case GL_NUM_SHADER_BINARY_FORMATS:
1438 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1439 case GL_ARRAY_BUFFER_BINDING:
1440 case GL_FRAMEBUFFER_BINDING:
1441 case GL_RENDERBUFFER_BINDING:
1442 case GL_CURRENT_PROGRAM:
1443 case GL_PACK_ALIGNMENT:
1444 case GL_UNPACK_ALIGNMENT:
1445 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001446 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001447 case GL_RED_BITS:
1448 case GL_GREEN_BITS:
1449 case GL_BLUE_BITS:
1450 case GL_ALPHA_BITS:
1451 case GL_DEPTH_BITS:
1452 case GL_STENCIL_BITS:
1453 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1454 case GL_CULL_FACE_MODE:
1455 case GL_FRONT_FACE:
1456 case GL_ACTIVE_TEXTURE:
1457 case GL_STENCIL_FUNC:
1458 case GL_STENCIL_VALUE_MASK:
1459 case GL_STENCIL_REF:
1460 case GL_STENCIL_FAIL:
1461 case GL_STENCIL_PASS_DEPTH_FAIL:
1462 case GL_STENCIL_PASS_DEPTH_PASS:
1463 case GL_STENCIL_BACK_FUNC:
1464 case GL_STENCIL_BACK_VALUE_MASK:
1465 case GL_STENCIL_BACK_REF:
1466 case GL_STENCIL_BACK_FAIL:
1467 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1468 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1469 case GL_DEPTH_FUNC:
1470 case GL_BLEND_SRC_RGB:
1471 case GL_BLEND_SRC_ALPHA:
1472 case GL_BLEND_DST_RGB:
1473 case GL_BLEND_DST_ALPHA:
1474 case GL_BLEND_EQUATION_RGB:
1475 case GL_BLEND_EQUATION_ALPHA:
1476 case GL_STENCIL_WRITEMASK:
1477 case GL_STENCIL_BACK_WRITEMASK:
1478 case GL_STENCIL_CLEAR_VALUE:
1479 case GL_SUBPIXEL_BITS:
1480 case GL_MAX_TEXTURE_SIZE:
1481 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1482 case GL_SAMPLE_BUFFERS:
1483 case GL_SAMPLES:
1484 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1485 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1486 case GL_TEXTURE_BINDING_2D:
1487 case GL_TEXTURE_BINDING_CUBE_MAP:
1488 {
1489 *type = GL_INT;
1490 *numParams = 1;
1491 }
1492 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001493 case GL_MAX_SAMPLES_ANGLE:
1494 {
1495 if (getMaxSupportedSamples() != 0)
1496 {
1497 *type = GL_INT;
1498 *numParams = 1;
1499 }
1500 else
1501 {
1502 return false;
1503 }
1504 }
1505 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001506 case GL_MAX_VIEWPORT_DIMS:
1507 {
1508 *type = GL_INT;
1509 *numParams = 2;
1510 }
1511 break;
1512 case GL_VIEWPORT:
1513 case GL_SCISSOR_BOX:
1514 {
1515 *type = GL_INT;
1516 *numParams = 4;
1517 }
1518 break;
1519 case GL_SHADER_COMPILER:
1520 case GL_SAMPLE_COVERAGE_INVERT:
1521 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001522 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1523 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1524 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1525 case GL_SAMPLE_COVERAGE:
1526 case GL_SCISSOR_TEST:
1527 case GL_STENCIL_TEST:
1528 case GL_DEPTH_TEST:
1529 case GL_BLEND:
1530 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001531 {
1532 *type = GL_BOOL;
1533 *numParams = 1;
1534 }
1535 break;
1536 case GL_COLOR_WRITEMASK:
1537 {
1538 *type = GL_BOOL;
1539 *numParams = 4;
1540 }
1541 break;
1542 case GL_POLYGON_OFFSET_FACTOR:
1543 case GL_POLYGON_OFFSET_UNITS:
1544 case GL_SAMPLE_COVERAGE_VALUE:
1545 case GL_DEPTH_CLEAR_VALUE:
1546 case GL_LINE_WIDTH:
1547 {
1548 *type = GL_FLOAT;
1549 *numParams = 1;
1550 }
1551 break;
1552 case GL_ALIASED_LINE_WIDTH_RANGE:
1553 case GL_ALIASED_POINT_SIZE_RANGE:
1554 case GL_DEPTH_RANGE:
1555 {
1556 *type = GL_FLOAT;
1557 *numParams = 2;
1558 }
1559 break;
1560 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001561 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001562 {
1563 *type = GL_FLOAT;
1564 *numParams = 4;
1565 }
1566 break;
1567 default:
1568 return false;
1569 }
1570
1571 return true;
1572}
1573
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574// Applies the render target surface, depth stencil surface, viewport rectangle and
1575// scissor rectangle to the Direct3D 9 device
1576bool Context::applyRenderTarget(bool ignoreViewport)
1577{
1578 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001579
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001580 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581
1582 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1583 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001584 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1585
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586 return false;
1587 }
1588
1589 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001590
1591 if (!renderTarget)
1592 {
1593 return false; // Context must be lost
1594 }
1595
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001596 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001598 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1599 if (renderTargetSerial != mAppliedRenderTargetSerial)
1600 {
1601 device->SetRenderTarget(0, renderTarget);
1602 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001603 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001604 }
1605
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001606 unsigned int depthbufferSerial = 0;
1607 unsigned int stencilbufferSerial = 0;
1608 if (framebufferObject->getDepthbufferType() != GL_NONE)
1609 {
1610 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001611 if (!depthStencil)
1612 {
1613 ERR("Depth stencil pointer unexpectedly null.");
1614 return false;
1615 }
1616
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001617 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1618 }
1619 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1620 {
1621 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001622 if (!depthStencil)
1623 {
1624 ERR("Depth stencil pointer unexpectedly null.");
1625 return false;
1626 }
1627
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001628 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1629 }
1630
1631 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001632 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001633 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001634 {
1635 device->SetDepthStencilSurface(depthStencil);
1636 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001637 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001638 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640
1641 D3DVIEWPORT9 viewport;
1642 D3DSURFACE_DESC desc;
1643 renderTarget->GetDesc(&desc);
1644
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001645 float zNear = clamp01(mState.zNear);
1646 float zFar = clamp01(mState.zFar);
1647
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 if (ignoreViewport)
1649 {
1650 viewport.X = 0;
1651 viewport.Y = 0;
1652 viewport.Width = desc.Width;
1653 viewport.Height = desc.Height;
1654 viewport.MinZ = 0.0f;
1655 viewport.MaxZ = 1.0f;
1656 }
1657 else
1658 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001659 viewport.X = std::max(mState.viewportX, 0);
1660 viewport.Y = std::max(mState.viewportY, 0);
1661 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1662 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001663 viewport.MinZ = zNear;
1664 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 }
1666
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001667 if (viewport.Width <= 0 || viewport.Height <= 0)
1668 {
1669 return false; // Nothing to render
1670 }
1671
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 device->SetViewport(&viewport);
1673
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001674 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 if (mState.scissorTest)
1677 {
1678 RECT rect = {mState.scissorX,
1679 mState.scissorY,
1680 mState.scissorX + mState.scissorWidth,
1681 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1683 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001684 device->SetScissorRect(&rect);
1685 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1686 }
1687 else
1688 {
1689 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1690 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001691
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001692 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 }
1694
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 Program *programObject = getCurrentProgram();
1698
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001699 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001700 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001702
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001703 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001704 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1705 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1706 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001707 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1708
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001709 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001710 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001711 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1712
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001713 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001714 programObject->setUniform1fv(near, 1, &zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001715
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001716 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001717 programObject->setUniform1fv(far, 1, &zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001718
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001719 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001720 GLfloat zDiff = zFar - zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001721 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722 }
1723
1724 return true;
1725}
1726
1727// 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 +00001728void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729{
1730 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001731 Program *programObject = getCurrentProgram();
1732
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001733 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001734 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001735 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001737 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001738 GLint alwaysFront = !isTriangleMode(drawMode);
1739 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1740
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001741 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001742
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748 }
1749 else
1750 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001751 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 }
1753
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 mCullStateDirty = false;
1755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 if (mDepthStateDirty)
1758 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001759 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1762 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
1764 else
1765 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001766 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001768
1769 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770 }
1771
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001774 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001775 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001776 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1777
1778 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1779 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1780 {
1781 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1782 }
1783 else
1784 {
1785 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1786 unorm<8>(mState.blendColor.alpha),
1787 unorm<8>(mState.blendColor.alpha),
1788 unorm<8>(mState.blendColor.alpha)));
1789 }
1790
1791 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1792 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1793 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1794
1795 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1796 mState.destBlendRGB != mState.destBlendAlpha ||
1797 mState.blendEquationRGB != mState.blendEquationAlpha)
1798 {
1799 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1800
1801 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1802 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1803 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1804
1805 }
1806 else
1807 {
1808 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1809 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001810 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001811 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001812 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001814 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001815
1816 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817 }
1818
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001819 if (mStencilStateDirty || mFrontFaceDirty)
1820 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001821 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 {
1823 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1824 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1825
1826 // FIXME: Unsupported by D3D9
1827 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1828 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1829 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1830 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1831 mState.stencilRef != mState.stencilBackRef ||
1832 mState.stencilMask != mState.stencilBackMask)
1833 {
1834 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1835 return error(GL_INVALID_OPERATION);
1836 }
1837
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001838 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001839 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001840 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1841
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001842 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1843 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1844 es2dx::ConvertComparison(mState.stencilFunc));
1845
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001846 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 +00001847 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1848
1849 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1850 es2dx::ConvertStencilOp(mState.stencilFail));
1851 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1852 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1853 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1854 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1855
1856 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1857 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1858 es2dx::ConvertComparison(mState.stencilBackFunc));
1859
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001860 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 +00001861 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1862
1863 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1864 es2dx::ConvertStencilOp(mState.stencilBackFail));
1865 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1866 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1867 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1868 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1869 }
1870 else
1871 {
1872 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1873 }
1874
1875 mStencilStateDirty = false;
1876 }
1877
1878 if (mMaskStateDirty)
1879 {
1880 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1881 mState.colorMaskBlue, mState.colorMaskAlpha));
1882 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1883
1884 mMaskStateDirty = false;
1885 }
1886
1887 if (mPolygonOffsetStateDirty)
1888 {
1889 if (mState.polygonOffsetFill)
1890 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001891 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001892 if (depthbuffer)
1893 {
1894 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1895 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1896 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1897 }
1898 }
1899 else
1900 {
1901 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1902 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1903 }
1904
1905 mPolygonOffsetStateDirty = false;
1906 }
1907
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001908 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001910 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001911 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001912 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001913 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001914 FIXME("Sample alpha to coverage is unimplemented.");
1915 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001917 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1918 if (mState.sampleCoverage)
1919 {
1920 unsigned int mask = 0;
1921 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001922 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001923 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001924
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001925 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001926 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001927 mask <<= 1;
1928
1929 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1930 {
1931 threshold += 1.0f;
1932 mask |= 1;
1933 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001934 }
1935 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001936
1937 if (mState.sampleCoverageInvert)
1938 {
1939 mask = ~mask;
1940 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001941
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001942 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1943 }
1944 else
1945 {
1946 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1947 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001948 }
1949 else
1950 {
1951 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001952 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001953
1954 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 }
1956
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001957 if (mDitherStateDirty)
1958 {
1959 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1960
1961 mDitherStateDirty = false;
1962 }
1963
1964 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965}
1966
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001967// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001968void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001970 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001972 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001974 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975 }
1976 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001977}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001979GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001980{
1981 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1982
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001983 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001984 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001985 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001986 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001987 }
1988
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001989 lookupAttributeMapping(translated);
1990
1991 mBufferBackEnd->setupAttributesPreDraw(translated);
1992
1993 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1994 {
1995 if (translated[i].enabled && translated[i].nonArray)
1996 {
1997 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1998 if (err != GL_NO_ERROR)
1999 {
2000 return err;
2001 }
2002
2003 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2004
2005 *useIndexing = true;
2006 return GL_NO_ERROR;
2007 }
2008 }
2009
2010 *useIndexing = false;
2011 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002012}
2013
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002014GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002015{
2016 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2017
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002018 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002019
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002020 if (err == GL_NO_ERROR)
2021 {
2022 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002023
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002024 mBufferBackEnd->setupAttributesPreDraw(translated);
2025 }
2026
2027 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002028}
2029
2030// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002031GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002032{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002033 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002034
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002035 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002036 {
2037 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2038 }
2039
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002040 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041}
2042
2043// Applies the shaders and shader constants to the Direct3D 9 device
2044void Context::applyShaders()
2045{
2046 IDirect3DDevice9 *device = getDevice();
2047 Program *programObject = getCurrentProgram();
2048 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2049 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2050
2051 device->SetVertexShader(vertexShader);
2052 device->SetPixelShader(pixelShader);
2053
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002054 if (programObject->getSerial() != mAppliedProgram)
2055 {
2056 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002057 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002058 mAppliedProgram = programObject->getSerial();
2059 }
2060
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 programObject->applyUniforms();
2062}
2063
2064// Applies the textures and sampler states to the Direct3D 9 device
2065void Context::applyTextures()
2066{
2067 IDirect3DDevice9 *device = getDevice();
2068 Program *programObject = getCurrentProgram();
2069
2070 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2071 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002072 int textureUnit = programObject->getSamplerMapping(sampler);
2073 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002075 SamplerType textureType = programObject->getSamplerType(sampler);
2076
2077 Texture *texture = getSamplerTexture(textureUnit, textureType);
2078
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002079 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002080 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002081 if (texture->isComplete())
2082 {
2083 GLenum wrapS = texture->getWrapS();
2084 GLenum wrapT = texture->getWrapT();
2085 GLenum minFilter = texture->getMinFilter();
2086 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002088 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2089 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002091 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2092 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2093 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2094 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2095 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002097 device->SetTexture(sampler, texture->getTexture());
2098 }
2099 else
2100 {
2101 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2102 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002103 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002104
2105 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002107 else
2108 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002109 if (programObject->isSamplerDirty(sampler))
2110 {
2111 device->SetTexture(sampler, NULL);
2112 programObject->setSamplerDirty(sampler, false);
2113 }
2114 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 }
2116}
2117
2118void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2119{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002120 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002121
2122 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2123 {
2124 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2125 }
2126
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002127 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2128 {
2129 return error(GL_INVALID_OPERATION);
2130 }
2131
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002133
2134 if (!renderTarget)
2135 {
2136 return; // Context must be lost, return silently
2137 }
2138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 IDirect3DDevice9 *device = getDevice();
2140
2141 D3DSURFACE_DESC desc;
2142 renderTarget->GetDesc(&desc);
2143
2144 IDirect3DSurface9 *systemSurface;
2145 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2146
2147 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2148 {
2149 return error(GL_OUT_OF_MEMORY);
2150 }
2151
2152 ASSERT(SUCCEEDED(result));
2153
2154 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2155 {
2156 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2157 }
2158
2159 result = device->GetRenderTargetData(renderTarget, systemSurface);
2160
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 if (FAILED(result))
2162 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 systemSurface->Release();
2164
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002165 switch (result)
2166 {
2167 case D3DERR_DRIVERINTERNALERROR:
2168 case D3DERR_DEVICELOST:
2169 return error(GL_OUT_OF_MEMORY);
2170 default:
2171 UNREACHABLE();
2172 return; // No sensible error to generate
2173 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 }
2175
2176 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002177 RECT rect = {std::max(x, 0),
2178 std::max(y, 0),
2179 std::min(x + width, (int)desc.Width),
2180 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181
2182 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2183
2184 if (FAILED(result))
2185 {
2186 UNREACHABLE();
2187 systemSurface->Release();
2188
2189 return; // No sensible error to generate
2190 }
2191
2192 unsigned char *source = (unsigned char*)lock.pBits;
2193 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002194 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002196 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002197
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198 for (int j = 0; j < rect.bottom - rect.top; j++)
2199 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002200 if (desc.Format == D3DFMT_A8R8G8B8 &&
2201 format == GL_BGRA_EXT &&
2202 type == GL_UNSIGNED_BYTE)
2203 {
2204 // Fast path for EXT_read_format_bgra, given
2205 // an RGBA source buffer. Note that buffers with no
2206 // alpha go through the slow path below.
2207 memcpy(dest + j * outputPitch,
2208 source + j * lock.Pitch,
2209 (rect.right - rect.left) * 4);
2210 continue;
2211 }
2212
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 for (int i = 0; i < rect.right - rect.left; i++)
2214 {
2215 float r;
2216 float g;
2217 float b;
2218 float a;
2219
2220 switch (desc.Format)
2221 {
2222 case D3DFMT_R5G6B5:
2223 {
2224 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2225
2226 a = 1.0f;
2227 b = (rgb & 0x001F) * (1.0f / 0x001F);
2228 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2229 r = (rgb & 0xF800) * (1.0f / 0xF800);
2230 }
2231 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 case D3DFMT_A1R5G5B5:
2233 {
2234 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2235
2236 a = (argb & 0x8000) ? 1.0f : 0.0f;
2237 b = (argb & 0x001F) * (1.0f / 0x001F);
2238 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2239 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2240 }
2241 break;
2242 case D3DFMT_A8R8G8B8:
2243 {
2244 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2245
2246 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2247 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2248 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2249 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2250 }
2251 break;
2252 case D3DFMT_X8R8G8B8:
2253 {
2254 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2255
2256 a = 1.0f;
2257 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2258 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2259 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2260 }
2261 break;
2262 case D3DFMT_A2R10G10B10:
2263 {
2264 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2265
2266 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2267 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2268 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2269 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2270 }
2271 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002272 case D3DFMT_A32B32G32R32F:
2273 {
2274 // float formats in D3D are stored rgba, rather than the other way round
2275 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2276 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2277 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2278 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2279 }
2280 break;
2281 case D3DFMT_A16B16G16R16F:
2282 {
2283 // float formats in D3D are stored rgba, rather than the other way round
2284 float abgr[4];
2285
2286 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2287
2288 a = abgr[3];
2289 b = abgr[2];
2290 g = abgr[1];
2291 r = abgr[0];
2292 }
2293 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 default:
2295 UNIMPLEMENTED(); // FIXME
2296 UNREACHABLE();
2297 }
2298
2299 switch (format)
2300 {
2301 case GL_RGBA:
2302 switch (type)
2303 {
2304 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002305 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2306 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2307 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2308 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309 break;
2310 default: UNREACHABLE();
2311 }
2312 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002313 case GL_BGRA_EXT:
2314 switch (type)
2315 {
2316 case GL_UNSIGNED_BYTE:
2317 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2318 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2319 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2320 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2321 break;
2322 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2323 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2324 // this type is packed as follows:
2325 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2326 // --------------------------------------------------------------------------------
2327 // | 4th | 3rd | 2nd | 1st component |
2328 // --------------------------------------------------------------------------------
2329 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2330 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2331 ((unsigned short)(15 * a + 0.5f) << 12)|
2332 ((unsigned short)(15 * r + 0.5f) << 8) |
2333 ((unsigned short)(15 * g + 0.5f) << 4) |
2334 ((unsigned short)(15 * b + 0.5f) << 0);
2335 break;
2336 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2337 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2338 // this type is packed as follows:
2339 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2340 // --------------------------------------------------------------------------------
2341 // | 4th | 3rd | 2nd | 1st component |
2342 // --------------------------------------------------------------------------------
2343 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2344 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2345 ((unsigned short)( a + 0.5f) << 15) |
2346 ((unsigned short)(31 * r + 0.5f) << 10) |
2347 ((unsigned short)(31 * g + 0.5f) << 5) |
2348 ((unsigned short)(31 * b + 0.5f) << 0);
2349 break;
2350 default: UNREACHABLE();
2351 }
2352 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002353 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 switch (type)
2355 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002356 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002357 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2358 ((unsigned short)(31 * b + 0.5f) << 0) |
2359 ((unsigned short)(63 * g + 0.5f) << 5) |
2360 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 break;
2362 default: UNREACHABLE();
2363 }
2364 break;
2365 default: UNREACHABLE();
2366 }
2367 }
2368 }
2369
2370 systemSurface->UnlockRect();
2371
2372 systemSurface->Release();
2373}
2374
2375void Context::clear(GLbitfield mask)
2376{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002377 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002378
2379 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2380 {
2381 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2382
2383 return;
2384 }
2385
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002386 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 IDirect3DDevice9 *device = getDevice();
2388 DWORD flags = 0;
2389
2390 if (mask & GL_COLOR_BUFFER_BIT)
2391 {
2392 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002393
2394 if (framebufferObject->getColorbufferType() != GL_NONE)
2395 {
2396 flags |= D3DCLEAR_TARGET;
2397 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 }
2399
2400 if (mask & GL_DEPTH_BUFFER_BIT)
2401 {
2402 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002403 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 {
2405 flags |= D3DCLEAR_ZBUFFER;
2406 }
2407 }
2408
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409 GLuint stencilUnmasked = 0x0;
2410
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002411 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002414 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002416 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002417 if (!depthStencil)
2418 {
2419 ERR("Depth stencil pointer unexpectedly null.");
2420 return;
2421 }
2422
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002423 D3DSURFACE_DESC desc;
2424 depthStencil->GetDesc(&desc);
2425
2426 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2427 stencilUnmasked = (0x1 << stencilSize) - 1;
2428
2429 if (stencilUnmasked != 0x0)
2430 {
2431 flags |= D3DCLEAR_STENCIL;
2432 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 }
2434 }
2435
2436 if (mask != 0)
2437 {
2438 return error(GL_INVALID_VALUE);
2439 }
2440
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002441 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2442 {
2443 return;
2444 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002446 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2447 unorm<8>(mState.colorClearValue.red),
2448 unorm<8>(mState.colorClearValue.green),
2449 unorm<8>(mState.colorClearValue.blue));
2450 float depth = clamp01(mState.depthClearValue);
2451 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452
2453 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2454
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002455 if (!renderTarget)
2456 {
2457 return; // Context must be lost, return silently
2458 }
2459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 D3DSURFACE_DESC desc;
2461 renderTarget->GetDesc(&desc);
2462
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002463 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464
2465 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002466 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002468 !(mState.colorMaskRed && mState.colorMaskGreen &&
2469 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470
2471 if (needMaskedColorClear || needMaskedStencilClear)
2472 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002473 // State which is altered in all paths from this point to the clear call is saved.
2474 // State which is altered in only some paths will be flagged dirty in the case that
2475 // that path is taken.
2476 HRESULT hr;
2477 if (mMaskedClearSavedState == NULL)
2478 {
2479 hr = device->BeginStateBlock();
2480 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2481
2482 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2483 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2484 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2485 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2486 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2487 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2488 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2489 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2490 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2491 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2492 device->SetPixelShader(NULL);
2493 device->SetVertexShader(NULL);
2494 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2495 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002496 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002497
2498 hr = device->EndStateBlock(&mMaskedClearSavedState);
2499 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2500 }
2501
2502 ASSERT(mMaskedClearSavedState != NULL);
2503
2504 if (mMaskedClearSavedState != NULL)
2505 {
2506 hr = mMaskedClearSavedState->Capture();
2507 ASSERT(SUCCEEDED(hr));
2508 }
2509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2511 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2512 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2513 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2514 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2515 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2516 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2517 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2518
2519 if (flags & D3DCLEAR_TARGET)
2520 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002521 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2522 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2523 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2524 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 }
2526 else
2527 {
2528 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2529 }
2530
2531 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2532 {
2533 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2534 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2535 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2536 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002537 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002538 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2540 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002541 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 }
2543 else
2544 {
2545 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2546 }
2547
2548 device->SetPixelShader(NULL);
2549 device->SetVertexShader(NULL);
2550 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002551 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002552 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002554 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 {
2556 float x, y, z, w;
2557 D3DCOLOR diffuse;
2558 };
2559
2560 Vertex quad[4];
2561 quad[0].x = 0.0f;
2562 quad[0].y = (float)desc.Height;
2563 quad[0].z = 0.0f;
2564 quad[0].w = 1.0f;
2565 quad[0].diffuse = color;
2566
2567 quad[1].x = (float)desc.Width;
2568 quad[1].y = (float)desc.Height;
2569 quad[1].z = 0.0f;
2570 quad[1].w = 1.0f;
2571 quad[1].diffuse = color;
2572
2573 quad[2].x = 0.0f;
2574 quad[2].y = 0.0f;
2575 quad[2].z = 0.0f;
2576 quad[2].w = 1.0f;
2577 quad[2].diffuse = color;
2578
2579 quad[3].x = (float)desc.Width;
2580 quad[3].y = 0.0f;
2581 quad[3].z = 0.0f;
2582 quad[3].w = 1.0f;
2583 quad[3].diffuse = color;
2584
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002585 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587
2588 if (flags & D3DCLEAR_ZBUFFER)
2589 {
2590 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2591 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2592 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2593 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002594
2595 if (mMaskedClearSavedState != NULL)
2596 {
2597 mMaskedClearSavedState->Apply();
2598 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002600 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601 {
2602 device->Clear(0, NULL, flags, color, depth, stencil);
2603 }
2604}
2605
2606void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2607{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002608 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 {
2610 return error(GL_INVALID_OPERATION);
2611 }
2612
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002613 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 IDirect3DDevice9 *device = getDevice();
2615 D3DPRIMITIVETYPE primitiveType;
2616 int primitiveCount;
2617
2618 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2619 return error(GL_INVALID_ENUM);
2620
2621 if (primitiveCount <= 0)
2622 {
2623 return;
2624 }
2625
2626 if (!applyRenderTarget(false))
2627 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002628 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 }
2630
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002631 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002632
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002633 TranslatedIndexData indexInfo;
2634 bool useIndexing;
2635 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002636 if (err != GL_NO_ERROR)
2637 {
2638 return error(err);
2639 }
2640
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641 applyShaders();
2642 applyTextures();
2643
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002644 if (!getCurrentProgram()->validateSamplers())
2645 {
2646 return error(GL_INVALID_OPERATION);
2647 }
2648
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002649 if (!cullSkipsDraw(mode))
2650 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002651 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002652 if (useIndexing)
2653 {
2654 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2655 }
2656 else
2657 {
2658 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2659 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002660 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661}
2662
2663void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2664{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002665 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 {
2667 return error(GL_INVALID_OPERATION);
2668 }
2669
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002670 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 {
2672 return error(GL_INVALID_OPERATION);
2673 }
2674
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002675 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676 IDirect3DDevice9 *device = getDevice();
2677 D3DPRIMITIVETYPE primitiveType;
2678 int primitiveCount;
2679
2680 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2681 return error(GL_INVALID_ENUM);
2682
2683 if (primitiveCount <= 0)
2684 {
2685 return;
2686 }
2687
2688 if (!applyRenderTarget(false))
2689 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002690 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691 }
2692
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002693 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002694
2695 TranslatedIndexData indexInfo;
2696 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2697 if (err != GL_NO_ERROR)
2698 {
2699 return error(err);
2700 }
2701
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002702 err = applyVertexBuffer(indexInfo);
2703 if (err != GL_NO_ERROR)
2704 {
2705 return error(err);
2706 }
2707
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708 applyShaders();
2709 applyTextures();
2710
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002711 if (!getCurrentProgram()->validateSamplers())
2712 {
2713 return error(GL_INVALID_OPERATION);
2714 }
2715
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002716 if (!cullSkipsDraw(mode))
2717 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002718 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002719 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 +00002720 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721}
2722
2723void Context::finish()
2724{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002725 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 IDirect3DDevice9 *device = getDevice();
2727 IDirect3DQuery9 *occlusionQuery = NULL;
2728
2729 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2730
2731 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2732 {
2733 return error(GL_OUT_OF_MEMORY);
2734 }
2735
2736 ASSERT(SUCCEEDED(result));
2737
2738 if (occlusionQuery)
2739 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002740 IDirect3DStateBlock9 *savedState = NULL;
2741 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2742
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002743 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2744 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002745
2746 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002747 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 device->SetPixelShader(NULL);
2749 device->SetVertexShader(NULL);
2750 device->SetFVF(D3DFVF_XYZRHW);
2751 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002752 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002755 result = occlusionQuery->Issue(D3DISSUE_END);
2756 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757
2758 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2759 {
2760 // Keep polling, but allow other threads to do something useful first
2761 Sleep(0);
2762 }
2763
2764 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002765
2766 if (savedState)
2767 {
2768 savedState->Apply();
2769 savedState->Release();
2770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 }
2772}
2773
2774void Context::flush()
2775{
2776 IDirect3DDevice9 *device = getDevice();
2777 IDirect3DQuery9 *eventQuery = NULL;
2778
2779 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2780
2781 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2782 {
2783 return error(GL_OUT_OF_MEMORY);
2784 }
2785
2786 ASSERT(SUCCEEDED(result));
2787
2788 if (eventQuery)
2789 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002790 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2791 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002793 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002795
2796 if (result == D3DERR_DEVICELOST)
2797 {
2798 error(GL_OUT_OF_MEMORY);
2799 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 }
2801}
2802
2803void Context::recordInvalidEnum()
2804{
2805 mInvalidEnum = true;
2806}
2807
2808void Context::recordInvalidValue()
2809{
2810 mInvalidValue = true;
2811}
2812
2813void Context::recordInvalidOperation()
2814{
2815 mInvalidOperation = true;
2816}
2817
2818void Context::recordOutOfMemory()
2819{
2820 mOutOfMemory = true;
2821}
2822
2823void Context::recordInvalidFramebufferOperation()
2824{
2825 mInvalidFramebufferOperation = true;
2826}
2827
2828// Get one of the recorded errors and clear its flag, if any.
2829// [OpenGL ES 2.0.24] section 2.5 page 13.
2830GLenum Context::getError()
2831{
2832 if (mInvalidEnum)
2833 {
2834 mInvalidEnum = false;
2835
2836 return GL_INVALID_ENUM;
2837 }
2838
2839 if (mInvalidValue)
2840 {
2841 mInvalidValue = false;
2842
2843 return GL_INVALID_VALUE;
2844 }
2845
2846 if (mInvalidOperation)
2847 {
2848 mInvalidOperation = false;
2849
2850 return GL_INVALID_OPERATION;
2851 }
2852
2853 if (mOutOfMemory)
2854 {
2855 mOutOfMemory = false;
2856
2857 return GL_OUT_OF_MEMORY;
2858 }
2859
2860 if (mInvalidFramebufferOperation)
2861 {
2862 mInvalidFramebufferOperation = false;
2863
2864 return GL_INVALID_FRAMEBUFFER_OPERATION;
2865 }
2866
2867 return GL_NO_ERROR;
2868}
2869
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002870bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002871{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002872 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002873}
2874
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002875int Context::getMaximumVaryingVectors() const
2876{
2877 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2878}
2879
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002880int Context::getMaxSupportedSamples() const
2881{
2882 return mMaxSupportedSamples;
2883}
2884
2885int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2886{
2887 if (requested == 0)
2888 {
2889 return requested;
2890 }
2891
2892 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2893 if (itr == mMultiSampleSupport.end())
2894 {
2895 return -1;
2896 }
2897
2898 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2899 {
2900 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2901 {
2902 return i;
2903 }
2904 }
2905
2906 return -1;
2907}
2908
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002909bool Context::supportsEventQueries() const
2910{
2911 return mSupportsEventQueries;
2912}
2913
daniel@transgaming.com01868132010-08-24 19:21:17 +00002914bool Context::supportsCompressedTextures() const
2915{
2916 return mSupportsCompressedTextures;
2917}
2918
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002919bool Context::supportsFloatTextures() const
2920{
2921 return mSupportsFloatTextures;
2922}
2923
2924bool Context::supportsFloatLinearFilter() const
2925{
2926 return mSupportsFloatLinearFilter;
2927}
2928
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002929bool Context::supportsFloatRenderableTextures() const
2930{
2931 return mSupportsFloatRenderableTextures;
2932}
2933
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002934bool Context::supportsHalfFloatTextures() const
2935{
2936 return mSupportsHalfFloatTextures;
2937}
2938
2939bool Context::supportsHalfFloatLinearFilter() const
2940{
2941 return mSupportsHalfFloatLinearFilter;
2942}
2943
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002944bool Context::supportsHalfFloatRenderableTextures() const
2945{
2946 return mSupportsHalfFloatRenderableTextures;
2947}
2948
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002949int Context::getMaximumRenderbufferDimension() const
2950{
2951 return mMaxRenderbufferDimension;
2952}
2953
2954int Context::getMaximumTextureDimension() const
2955{
2956 return mMaxTextureDimension;
2957}
2958
2959int Context::getMaximumCubeTextureDimension() const
2960{
2961 return mMaxCubeTextureDimension;
2962}
2963
2964int Context::getMaximumTextureLevel() const
2965{
2966 return mMaxTextureLevel;
2967}
2968
daniel@transgaming.comed828e52010-10-15 17:57:30 +00002969bool Context::supportsLuminanceTextures() const
2970{
2971 return mSupportsLuminanceTextures;
2972}
2973
2974bool Context::supportsLuminanceAlphaTextures() const
2975{
2976 return mSupportsLuminanceAlphaTextures;
2977}
2978
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002979void Context::detachBuffer(GLuint buffer)
2980{
2981 // [OpenGL ES 2.0.24] section 2.9 page 22:
2982 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2983 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2984
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002985 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002986 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002987 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002988 }
2989
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002990 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002992 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002993 }
2994
2995 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2996 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002997 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002998 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002999 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000 }
3001 }
3002}
3003
3004void Context::detachTexture(GLuint texture)
3005{
3006 // [OpenGL ES 2.0.24] section 3.8 page 84:
3007 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3008 // rebound to texture object zero
3009
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003010 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003012 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003013 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003014 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003015 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003016 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003018 }
3019 }
3020
3021 // [OpenGL ES 2.0.24] section 4.4 page 112:
3022 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3023 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3024 // image was attached in the currently bound framebuffer.
3025
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003026 Framebuffer *readFramebuffer = getReadFramebuffer();
3027 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003028
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003029 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003031 readFramebuffer->detachTexture(texture);
3032 }
3033
3034 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3035 {
3036 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037 }
3038}
3039
3040void Context::detachFramebuffer(GLuint framebuffer)
3041{
3042 // [OpenGL ES 2.0.24] section 4.4 page 107:
3043 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3044 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3045
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003046 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003047 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003048 bindReadFramebuffer(0);
3049 }
3050
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003051 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003052 {
3053 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003054 }
3055}
3056
3057void Context::detachRenderbuffer(GLuint renderbuffer)
3058{
3059 // [OpenGL ES 2.0.24] section 4.4 page 109:
3060 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3061 // had been executed with the target RENDERBUFFER and name of zero.
3062
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003063 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064 {
3065 bindRenderbuffer(0);
3066 }
3067
3068 // [OpenGL ES 2.0.24] section 4.4 page 111:
3069 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3070 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3071 // point to which this image was attached in the currently bound framebuffer.
3072
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003073 Framebuffer *readFramebuffer = getReadFramebuffer();
3074 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003075
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003076 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003077 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003078 readFramebuffer->detachRenderbuffer(renderbuffer);
3079 }
3080
3081 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3082 {
3083 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003084 }
3085}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003086
3087Texture *Context::getIncompleteTexture(SamplerType type)
3088{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003089 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003090
3091 if (t == NULL)
3092 {
3093 static const GLubyte color[] = { 0, 0, 0, 255 };
3094
3095 switch (type)
3096 {
3097 default:
3098 UNREACHABLE();
3099 // default falls through to SAMPLER_2D
3100
3101 case SAMPLER_2D:
3102 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003103 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003104 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003105 t = incomplete2d;
3106 }
3107 break;
3108
3109 case SAMPLER_CUBE:
3110 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003111 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003112
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003113 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3114 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3115 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3116 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3117 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3118 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003119
3120 t = incompleteCube;
3121 }
3122 break;
3123 }
3124
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003125 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003126 }
3127
3128 return t;
3129}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003130
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003131bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003132{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003133 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003134}
3135
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003136bool Context::isTriangleMode(GLenum drawMode)
3137{
3138 switch (drawMode)
3139 {
3140 case GL_TRIANGLES:
3141 case GL_TRIANGLE_FAN:
3142 case GL_TRIANGLE_STRIP:
3143 return true;
3144 case GL_POINTS:
3145 case GL_LINES:
3146 case GL_LINE_LOOP:
3147 case GL_LINE_STRIP:
3148 return false;
3149 default: UNREACHABLE();
3150 }
3151
3152 return false;
3153}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003154
3155void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3156{
3157 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3158
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003159 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3160 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3161 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3162 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003163
3164 mVertexDataManager->dirtyCurrentValues();
3165}
3166
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003167void Context::initExtensionString()
3168{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003169 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003170 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3171 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003172 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003173 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003174 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003175
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003176 if (supportsEventQueries())
3177 {
3178 mExtensionString += "GL_NV_fence ";
3179 }
3180
daniel@transgaming.com01868132010-08-24 19:21:17 +00003181 if (supportsCompressedTextures())
3182 {
3183 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3184 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003185
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003186 if (supportsFloatTextures())
3187 {
3188 mExtensionString += "GL_OES_texture_float ";
3189 }
3190
3191 if (supportsHalfFloatTextures())
3192 {
3193 mExtensionString += "GL_OES_texture_half_float ";
3194 }
3195
3196 if (supportsFloatLinearFilter())
3197 {
3198 mExtensionString += "GL_OES_texture_float_linear ";
3199 }
3200
3201 if (supportsHalfFloatLinearFilter())
3202 {
3203 mExtensionString += "GL_OES_texture_half_float_linear ";
3204 }
3205
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003206 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003207 {
3208 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3209 }
3210
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003211 if (mBufferBackEnd->supportIntIndices())
3212 {
3213 mExtensionString += "GL_OES_element_index_uint ";
3214 }
3215
3216 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3217 if (end != std::string::npos)
3218 {
3219 mExtensionString.resize(end+1);
3220 }
3221}
3222
3223const char *Context::getExtensionString() const
3224{
3225 return mExtensionString.c_str();
3226}
3227
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003228void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3229 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3230 GLbitfield mask)
3231{
3232 IDirect3DDevice9 *device = getDevice();
3233
3234 Framebuffer *readFramebuffer = getReadFramebuffer();
3235 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3236
3237 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3238 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3239 {
3240 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3241 }
3242
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003243 if (drawFramebuffer->getSamples() != 0)
3244 {
3245 return error(GL_INVALID_OPERATION);
3246 }
3247
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003248 RECT sourceRect;
3249 RECT destRect;
3250
3251 if (srcX0 < srcX1)
3252 {
3253 sourceRect.left = srcX0;
3254 sourceRect.right = srcX1;
3255 destRect.left = dstX0;
3256 destRect.right = dstX1;
3257 }
3258 else
3259 {
3260 sourceRect.left = srcX1;
3261 destRect.left = dstX1;
3262 sourceRect.right = srcX0;
3263 destRect.right = dstX0;
3264 }
3265
3266 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3267 // flip our Y-values here
3268 if (srcY0 < srcY1)
3269 {
3270 sourceRect.bottom = srcY1;
3271 destRect.bottom = dstY1;
3272 sourceRect.top = srcY0;
3273 destRect.top = dstY0;
3274 }
3275 else
3276 {
3277 sourceRect.bottom = srcY0;
3278 destRect.bottom = dstY0;
3279 sourceRect.top = srcY1;
3280 destRect.top = dstY1;
3281 }
3282
3283 RECT sourceScissoredRect = sourceRect;
3284 RECT destScissoredRect = destRect;
3285
3286 if (mState.scissorTest)
3287 {
3288 // Only write to parts of the destination framebuffer which pass the scissor test
3289 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3290 // rect will be checked against scissorY, rather than the bottom.
3291 if (destRect.left < mState.scissorX)
3292 {
3293 int xDiff = mState.scissorX - destRect.left;
3294 destScissoredRect.left = mState.scissorX;
3295 sourceScissoredRect.left += xDiff;
3296 }
3297
3298 if (destRect.right > mState.scissorX + mState.scissorWidth)
3299 {
3300 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3301 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3302 sourceScissoredRect.right -= xDiff;
3303 }
3304
3305 if (destRect.top < mState.scissorY)
3306 {
3307 int yDiff = mState.scissorY - destRect.top;
3308 destScissoredRect.top = mState.scissorY;
3309 sourceScissoredRect.top += yDiff;
3310 }
3311
3312 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3313 {
3314 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3315 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3316 sourceScissoredRect.bottom -= yDiff;
3317 }
3318 }
3319
3320 bool blitRenderTarget = false;
3321 bool blitDepthStencil = false;
3322
3323 RECT sourceTrimmedRect = sourceScissoredRect;
3324 RECT destTrimmedRect = destScissoredRect;
3325
3326 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3327 // the actual draw and read surfaces.
3328 if (sourceTrimmedRect.left < 0)
3329 {
3330 int xDiff = 0 - sourceTrimmedRect.left;
3331 sourceTrimmedRect.left = 0;
3332 destTrimmedRect.left += xDiff;
3333 }
3334
3335 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3336 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3337 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3338 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3339
3340 if (sourceTrimmedRect.right > readBufferWidth)
3341 {
3342 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3343 sourceTrimmedRect.right = readBufferWidth;
3344 destTrimmedRect.right -= xDiff;
3345 }
3346
3347 if (sourceTrimmedRect.top < 0)
3348 {
3349 int yDiff = 0 - sourceTrimmedRect.top;
3350 sourceTrimmedRect.top = 0;
3351 destTrimmedRect.top += yDiff;
3352 }
3353
3354 if (sourceTrimmedRect.bottom > readBufferHeight)
3355 {
3356 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3357 sourceTrimmedRect.bottom = readBufferHeight;
3358 destTrimmedRect.bottom -= yDiff;
3359 }
3360
3361 if (destTrimmedRect.left < 0)
3362 {
3363 int xDiff = 0 - destTrimmedRect.left;
3364 destTrimmedRect.left = 0;
3365 sourceTrimmedRect.left += xDiff;
3366 }
3367
3368 if (destTrimmedRect.right > drawBufferWidth)
3369 {
3370 int xDiff = destTrimmedRect.right - drawBufferWidth;
3371 destTrimmedRect.right = drawBufferWidth;
3372 sourceTrimmedRect.right -= xDiff;
3373 }
3374
3375 if (destTrimmedRect.top < 0)
3376 {
3377 int yDiff = 0 - destTrimmedRect.top;
3378 destTrimmedRect.top = 0;
3379 sourceTrimmedRect.top += yDiff;
3380 }
3381
3382 if (destTrimmedRect.bottom > drawBufferHeight)
3383 {
3384 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3385 destTrimmedRect.bottom = drawBufferHeight;
3386 sourceTrimmedRect.bottom -= yDiff;
3387 }
3388
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003389 bool partialBufferCopy = false;
3390 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3391 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3392 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3393 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3394 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3395 {
3396 partialBufferCopy = true;
3397 }
3398
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003399 if (mask & GL_COLOR_BUFFER_BIT)
3400 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003401 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3402 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3403 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3404 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3405 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003406 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3407 {
3408 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3409 return error(GL_INVALID_OPERATION);
3410 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003411
3412 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3413 {
3414 return error(GL_INVALID_OPERATION);
3415 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003416
3417 blitRenderTarget = true;
3418
3419 }
3420
3421 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3422 {
3423 DepthStencilbuffer *readDSBuffer = NULL;
3424 DepthStencilbuffer *drawDSBuffer = NULL;
3425
3426 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3427 // both a depth and stencil buffer, it will be the same buffer.
3428
3429 if (mask & GL_DEPTH_BUFFER_BIT)
3430 {
3431 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3432 {
3433 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3434 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3435 {
3436 return error(GL_INVALID_OPERATION);
3437 }
3438
3439 blitDepthStencil = true;
3440 readDSBuffer = readFramebuffer->getDepthbuffer();
3441 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3442 }
3443 }
3444
3445 if (mask & GL_STENCIL_BUFFER_BIT)
3446 {
3447 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3448 {
3449 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3450 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3451 {
3452 return error(GL_INVALID_OPERATION);
3453 }
3454
3455 blitDepthStencil = true;
3456 readDSBuffer = readFramebuffer->getStencilbuffer();
3457 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3458 }
3459 }
3460
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003461 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003462 {
3463 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3464 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3465 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003466
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003467 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3468 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003469 {
3470 return error(GL_INVALID_OPERATION);
3471 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003472 }
3473
3474 if (blitRenderTarget || blitDepthStencil)
3475 {
3476 egl::Display *display = getDisplay();
3477 display->endScene();
3478
3479 if (blitRenderTarget)
3480 {
3481 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3482 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3483
3484 if (FAILED(result))
3485 {
3486 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3487 return;
3488 }
3489 }
3490
3491 if (blitDepthStencil)
3492 {
3493 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3494
3495 if (FAILED(result))
3496 {
3497 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3498 return;
3499 }
3500 }
3501 }
3502}
3503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003504}
3505
3506extern "C"
3507{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003508gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003509{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003510 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003511}
3512
3513void glDestroyContext(gl::Context *context)
3514{
3515 delete context;
3516
3517 if (context == gl::getContext())
3518 {
3519 gl::makeCurrent(NULL, NULL, NULL);
3520 }
3521}
3522
3523void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3524{
3525 gl::makeCurrent(context, display, surface);
3526}
3527
3528gl::Context *glGetCurrentContext()
3529{
3530 return gl::getContext();
3531}
3532}