blob: 0defe4ad56a0d131a215e5f2b66db934ef649da4 [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"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034namespace gl
35{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000036Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000037 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
39 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000040
daniel@transgaming.com428d1582010-05-04 03:35:25 +000041 mState.depthClearValue = 1.0f;
42 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
daniel@transgaming.com428d1582010-05-04 03:35:25 +000044 mState.cullFace = false;
45 mState.cullMode = GL_BACK;
46 mState.frontFace = GL_CCW;
47 mState.depthTest = false;
48 mState.depthFunc = GL_LESS;
49 mState.blend = false;
50 mState.sourceBlendRGB = GL_ONE;
51 mState.sourceBlendAlpha = GL_ONE;
52 mState.destBlendRGB = GL_ZERO;
53 mState.destBlendAlpha = GL_ZERO;
54 mState.blendEquationRGB = GL_FUNC_ADD;
55 mState.blendEquationAlpha = GL_FUNC_ADD;
56 mState.blendColor.red = 0;
57 mState.blendColor.green = 0;
58 mState.blendColor.blue = 0;
59 mState.blendColor.alpha = 0;
60 mState.stencilTest = false;
61 mState.stencilFunc = GL_ALWAYS;
62 mState.stencilRef = 0;
63 mState.stencilMask = -1;
64 mState.stencilWritemask = -1;
65 mState.stencilBackFunc = GL_ALWAYS;
66 mState.stencilBackRef = 0;
67 mState.stencilBackMask = - 1;
68 mState.stencilBackWritemask = -1;
69 mState.stencilFail = GL_KEEP;
70 mState.stencilPassDepthFail = GL_KEEP;
71 mState.stencilPassDepthPass = GL_KEEP;
72 mState.stencilBackFail = GL_KEEP;
73 mState.stencilBackPassDepthFail = GL_KEEP;
74 mState.stencilBackPassDepthPass = GL_KEEP;
75 mState.polygonOffsetFill = false;
76 mState.polygonOffsetFactor = 0.0f;
77 mState.polygonOffsetUnits = 0.0f;
78 mState.sampleAlphaToCoverage = false;
79 mState.sampleCoverage = false;
80 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000081 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000082 mState.scissorTest = false;
83 mState.dither = true;
84 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000085 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
daniel@transgaming.com428d1582010-05-04 03:35:25 +000087 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.viewportX = 0;
90 mState.viewportY = 0;
91 mState.viewportWidth = config->mDisplayMode.Width;
92 mState.viewportHeight = config->mDisplayMode.Height;
93 mState.zNear = 0.0f;
94 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.scissorX = 0;
97 mState.scissorY = 0;
98 mState.scissorWidth = config->mDisplayMode.Width;
99 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000101 mState.colorMaskRed = true;
102 mState.colorMaskGreen = true;
103 mState.colorMaskBlue = true;
104 mState.colorMaskAlpha = true;
105 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000107 if (shareContext != NULL)
108 {
109 mResourceManager = shareContext->mResourceManager;
110 mResourceManager->addRef();
111 }
112 else
113 {
114 mResourceManager = new ResourceManager();
115 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 // [OpenGL ES 2.0.24] section 3.7 page 83:
118 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
119 // and cube map texture state vectors respectively associated with them.
120 // In order that access to these initial textures not be lost, they are treated as texture
121 // objects all of whose names are 0.
122
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000123 mTexture2DZero.set(new Texture2D(0));
124 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000126 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000127 bindArrayBuffer(0);
128 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129 bindTextureCubeMap(0);
130 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000131 bindReadFramebuffer(0);
132 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 bindRenderbuffer(0);
134
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000135 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.packAlignment = 4;
138 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000139
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000140 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000141 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000142 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000143
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144 mInvalidEnum = false;
145 mInvalidValue = false;
146 mInvalidOperation = false;
147 mOutOfMemory = false;
148 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000149
150 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000151
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000152 mSupportsCompressedTextures = false;
153 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000154 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000155 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000156 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157}
158
159Context::~Context()
160{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000161 if (mState.currentProgram != 0)
162 {
163 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
164 if (programObject)
165 {
166 programObject->release();
167 }
168 mState.currentProgram = 0;
169 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000171 while (!mFramebufferMap.empty())
172 {
173 deleteFramebuffer(mFramebufferMap.begin()->first);
174 }
175
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000176 while (!mFenceMap.empty())
177 {
178 deleteFence(mFenceMap.begin()->first);
179 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000180
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000181 while (!mMultiSampleSupport.empty())
182 {
183 delete [] mMultiSampleSupport.begin()->second;
184 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
185 }
186
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000187 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
188 {
189 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
190 {
191 mState.samplerTexture[type][sampler].set(NULL);
192 }
193 }
194
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000195 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
196 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000197 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 }
199
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000200 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
201 {
202 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
203 }
204
205 mState.arrayBuffer.set(NULL);
206 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 mState.renderbuffer.set(NULL);
208
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000209 mTexture2DZero.set(NULL);
210 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000212 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000213 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000214 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000215
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000216 if (mMaskedClearSavedState)
217 {
218 mMaskedClearSavedState->Release();
219 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000220
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000221 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222}
223
224void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
225{
226 IDirect3DDevice9 *device = display->getDevice();
227
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000228 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000229 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000230 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000231
daniel@transgaming.com83921382011-01-08 05:46:00 +0000232 mVertexDataManager = new VertexDataManager(this, device);
233 mIndexDataManager = new IndexDataManager(this, device);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000234 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000235
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000236 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
237
238 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
239 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
240 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
241 mMaxRenderbufferDimension = mMaxTextureDimension;
242 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
243 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
244 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
245
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000246 const D3DFORMAT renderBufferFormats[] =
247 {
248 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000249 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000250 D3DFMT_R5G6B5,
251 D3DFMT_D24S8
252 };
253
254 int max = 0;
255 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
256 {
257 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
258 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
259 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
260
261 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
262 {
263 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
264 {
265 max = j;
266 }
267 }
268 }
269
270 mMaxSupportedSamples = max;
271
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000272 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000273 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000274 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
275 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000276 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
277 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000278
daniel@transgaming.com83921382011-01-08 05:46:00 +0000279 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
280
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000281 initExtensionString();
282
283 mState.viewportX = 0;
284 mState.viewportY = 0;
285 mState.viewportWidth = surface->getWidth();
286 mState.viewportHeight = surface->getHeight();
287
288 mState.scissorX = 0;
289 mState.scissorY = 0;
290 mState.scissorWidth = surface->getWidth();
291 mState.scissorHeight = surface->getHeight();
292
293 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000294 }
295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
297 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000298 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000301 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000302 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
304 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000306 if (defaultRenderTarget)
307 {
308 defaultRenderTarget->Release();
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000311 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000313 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000315
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000316 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317}
318
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000319// 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 +0000320void Context::markAllStateDirty()
321{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000322 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000323 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000324 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000325 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000326 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000327
328 mClearStateDirty = true;
329 mCullStateDirty = true;
330 mDepthStateDirty = true;
331 mMaskStateDirty = true;
332 mBlendStateDirty = true;
333 mStencilStateDirty = true;
334 mPolygonOffsetStateDirty = true;
335 mScissorStateDirty = true;
336 mSampleStateDirty = true;
337 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000338 mFrontFaceDirty = true;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000339}
340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341void Context::setClearColor(float red, float green, float blue, float alpha)
342{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000343 mState.colorClearValue.red = red;
344 mState.colorClearValue.green = green;
345 mState.colorClearValue.blue = blue;
346 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
349void Context::setClearDepth(float depth)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearStencil(int stencil)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.stencilClearValue = stencil;
357}
358
359void Context::setCullFace(bool enabled)
360{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000361 if (mState.cullFace != enabled)
362 {
363 mState.cullFace = enabled;
364 mCullStateDirty = true;
365 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000366}
367
368bool Context::isCullFaceEnabled() const
369{
370 return mState.cullFace;
371}
372
373void Context::setCullMode(GLenum mode)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.cullMode != mode)
376 {
377 mState.cullMode = mode;
378 mCullStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382void Context::setFrontFace(GLenum front)
383{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000384 if (mState.frontFace != front)
385 {
386 mState.frontFace = front;
387 mFrontFaceDirty = true;
388 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389}
390
391void Context::setDepthTest(bool enabled)
392{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393 if (mState.depthTest != enabled)
394 {
395 mState.depthTest = enabled;
396 mDepthStateDirty = true;
397 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398}
399
400bool Context::isDepthTestEnabled() const
401{
402 return mState.depthTest;
403}
404
405void Context::setDepthFunc(GLenum depthFunc)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.depthFunc != depthFunc)
408 {
409 mState.depthFunc = depthFunc;
410 mDepthStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414void Context::setDepthRange(float zNear, float zFar)
415{
416 mState.zNear = zNear;
417 mState.zFar = zFar;
418}
419
420void Context::setBlend(bool enabled)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.blend != enabled)
423 {
424 mState.blend = enabled;
425 mBlendStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429bool Context::isBlendEnabled() const
430{
431 return mState.blend;
432}
433
434void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
435{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000436 if (mState.sourceBlendRGB != sourceRGB ||
437 mState.sourceBlendAlpha != sourceAlpha ||
438 mState.destBlendRGB != destRGB ||
439 mState.destBlendAlpha != destAlpha)
440 {
441 mState.sourceBlendRGB = sourceRGB;
442 mState.destBlendRGB = destRGB;
443 mState.sourceBlendAlpha = sourceAlpha;
444 mState.destBlendAlpha = destAlpha;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449void Context::setBlendColor(float red, float green, float blue, float alpha)
450{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000451 if (mState.blendColor.red != red ||
452 mState.blendColor.green != green ||
453 mState.blendColor.blue != blue ||
454 mState.blendColor.alpha != alpha)
455 {
456 mState.blendColor.red = red;
457 mState.blendColor.green = green;
458 mState.blendColor.blue = blue;
459 mState.blendColor.alpha = alpha;
460 mBlendStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
465{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 if (mState.blendEquationRGB != rgbEquation ||
467 mState.blendEquationAlpha != alphaEquation)
468 {
469 mState.blendEquationRGB = rgbEquation;
470 mState.blendEquationAlpha = alphaEquation;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475void Context::setStencilTest(bool enabled)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.stencilTest != enabled)
478 {
479 mState.stencilTest = enabled;
480 mStencilStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484bool Context::isStencilTestEnabled() const
485{
486 return mState.stencilTest;
487}
488
489void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilFunc != stencilFunc ||
492 mState.stencilRef != stencilRef ||
493 mState.stencilMask != stencilMask)
494 {
495 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000496 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 mState.stencilMask = stencilMask;
498 mStencilStateDirty = true;
499 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000500}
501
502void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilBackFunc != stencilBackFunc ||
505 mState.stencilBackRef != stencilBackRef ||
506 mState.stencilBackMask != stencilBackMask)
507 {
508 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000509 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 mState.stencilBackMask = stencilBackMask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilWritemask(GLuint stencilWritemask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilWritemask != stencilWritemask)
518 {
519 mState.stencilWritemask = stencilWritemask;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
525{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000526 if (mState.stencilBackWritemask != stencilBackWritemask)
527 {
528 mState.stencilBackWritemask = stencilBackWritemask;
529 mStencilStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.stencilFail != stencilFail ||
536 mState.stencilPassDepthFail != stencilPassDepthFail ||
537 mState.stencilPassDepthPass != stencilPassDepthPass)
538 {
539 mState.stencilFail = stencilFail;
540 mState.stencilPassDepthFail = stencilPassDepthFail;
541 mState.stencilPassDepthPass = stencilPassDepthPass;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
547{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 if (mState.stencilBackFail != stencilBackFail ||
549 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
550 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
551 {
552 mState.stencilBackFail = stencilBackFail;
553 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
554 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
555 mStencilStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setPolygonOffsetFill(bool enabled)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.polygonOffsetFill != enabled)
562 {
563 mState.polygonOffsetFill = enabled;
564 mPolygonOffsetStateDirty = true;
565 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000566}
567
568bool Context::isPolygonOffsetFillEnabled() const
569{
570 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.polygonOffsetFactor != factor ||
577 mState.polygonOffsetUnits != units)
578 {
579 mState.polygonOffsetFactor = factor;
580 mState.polygonOffsetUnits = units;
581 mPolygonOffsetStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585void Context::setSampleAlphaToCoverage(bool enabled)
586{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000587 if (mState.sampleAlphaToCoverage != enabled)
588 {
589 mState.sampleAlphaToCoverage = enabled;
590 mSampleStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594bool Context::isSampleAlphaToCoverageEnabled() const
595{
596 return mState.sampleAlphaToCoverage;
597}
598
599void Context::setSampleCoverage(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.sampleCoverage != enabled)
602 {
603 mState.sampleCoverage = enabled;
604 mSampleStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isSampleCoverageEnabled() const
609{
610 return mState.sampleCoverage;
611}
612
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000613void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.sampleCoverageValue != value ||
616 mState.sampleCoverageInvert != invert)
617 {
618 mState.sampleCoverageValue = value;
619 mState.sampleCoverageInvert = invert;
620 mSampleStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624void Context::setScissorTest(bool enabled)
625{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000626 if (mState.scissorTest != enabled)
627 {
628 mState.scissorTest = enabled;
629 mScissorStateDirty = true;
630 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000631}
632
633bool Context::isScissorTestEnabled() const
634{
635 return mState.scissorTest;
636}
637
638void Context::setDither(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.dither != enabled)
641 {
642 mState.dither = enabled;
643 mDitherStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isDitherEnabled() const
648{
649 return mState.dither;
650}
651
652void Context::setLineWidth(GLfloat width)
653{
654 mState.lineWidth = width;
655}
656
657void Context::setGenerateMipmapHint(GLenum hint)
658{
659 mState.generateMipmapHint = hint;
660}
661
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000662void Context::setFragmentShaderDerivativeHint(GLenum hint)
663{
664 mState.fragmentShaderDerivativeHint = hint;
665 // TODO: Propagate the hint to shader translator so we can write
666 // ddx, ddx_coarse, or ddx_fine depending on the hint.
667 // Ignore for now. It is valid for implementations to ignore hint.
668}
669
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
671{
672 mState.viewportX = x;
673 mState.viewportY = y;
674 mState.viewportWidth = width;
675 mState.viewportHeight = height;
676}
677
678void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.scissorX != x || mState.scissorY != y ||
681 mState.scissorWidth != width || mState.scissorHeight != height)
682 {
683 mState.scissorX = x;
684 mState.scissorY = y;
685 mState.scissorWidth = width;
686 mState.scissorHeight = height;
687 mScissorStateDirty = true;
688 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689}
690
691void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
694 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
695 {
696 mState.colorMaskRed = red;
697 mState.colorMaskGreen = green;
698 mState.colorMaskBlue = blue;
699 mState.colorMaskAlpha = alpha;
700 mMaskStateDirty = true;
701 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702}
703
704void Context::setDepthMask(bool mask)
705{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000706 if (mState.depthMask != mask)
707 {
708 mState.depthMask = mask;
709 mMaskStateDirty = true;
710 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713void Context::setActiveSampler(int active)
714{
715 mState.activeSampler = active;
716}
717
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000718GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000720 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000721}
722
723GLuint Context::getDrawFramebufferHandle() const
724{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000725 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000726}
727
728GLuint Context::getRenderbufferHandle() const
729{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000730 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733GLuint Context::getArrayBufferHandle() const
734{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000735 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
daniel@transgaming.com83921382011-01-08 05:46:00 +0000738void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000740 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000741}
742
daniel@transgaming.com83921382011-01-08 05:46:00 +0000743const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744{
745 return mState.vertexAttribute[attribNum];
746}
747
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749 GLsizei stride, const void *pointer)
750{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000751 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000752 mState.vertexAttribute[attribNum].mSize = size;
753 mState.vertexAttribute[attribNum].mType = type;
754 mState.vertexAttribute[attribNum].mNormalized = normalized;
755 mState.vertexAttribute[attribNum].mStride = stride;
756 mState.vertexAttribute[attribNum].mPointer = pointer;
757}
758
759const void *Context::getVertexAttribPointer(unsigned int attribNum) const
760{
761 return mState.vertexAttribute[attribNum].mPointer;
762}
763
daniel@transgaming.com83921382011-01-08 05:46:00 +0000764const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000765{
766 return mState.vertexAttribute;
767}
768
769void Context::setPackAlignment(GLint alignment)
770{
771 mState.packAlignment = alignment;
772}
773
774GLint Context::getPackAlignment() const
775{
776 return mState.packAlignment;
777}
778
779void Context::setUnpackAlignment(GLint alignment)
780{
781 mState.unpackAlignment = alignment;
782}
783
784GLint Context::getUnpackAlignment() const
785{
786 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787}
788
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789GLuint Context::createBuffer()
790{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000791 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792}
793
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794GLuint Context::createProgram()
795{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000796 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000799GLuint Context::createShader(GLenum type)
800{
801 return mResourceManager->createShader(type);
802}
803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804GLuint Context::createTexture()
805{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000806 return mResourceManager->createTexture();
807}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000809GLuint Context::createRenderbuffer()
810{
811 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000814// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815GLuint Context::createFramebuffer()
816{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000817 unsigned int handle = 1;
818
819 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
820 {
821 handle++;
822 }
823
824 mFramebufferMap[handle] = NULL;
825
826 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827}
828
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000829GLuint Context::createFence()
830{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000831 unsigned int handle = 0;
832
833 while (mFenceMap.find(handle) != mFenceMap.end())
834 {
835 handle++;
836 }
837
838 mFenceMap[handle] = new Fence;
839
840 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000841}
842
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843void Context::deleteBuffer(GLuint buffer)
844{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000845 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846 {
847 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000849
850 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853void Context::deleteShader(GLuint shader)
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858void Context::deleteProgram(GLuint program)
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void Context::deleteTexture(GLuint texture)
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866 {
867 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869
870 mResourceManager->deleteTexture(texture);
871}
872
873void Context::deleteRenderbuffer(GLuint renderbuffer)
874{
875 if (mResourceManager->getRenderbuffer(renderbuffer))
876 {
877 detachRenderbuffer(renderbuffer);
878 }
879
880 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
883void Context::deleteFramebuffer(GLuint framebuffer)
884{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000885 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
886
887 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888 {
889 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000890
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000891 delete framebufferObject->second;
892 mFramebufferMap.erase(framebufferObject);
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000895
896void Context::deleteFence(GLuint fence)
897{
898 FenceMap::iterator fenceObject = mFenceMap.find(fence);
899
900 if (fenceObject != mFenceMap.end())
901 {
902 delete fenceObject->second;
903 mFenceMap.erase(fenceObject);
904 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000905}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000909 return mResourceManager->getBuffer(handle);
910}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912Shader *Context::getShader(GLuint handle)
913{
914 return mResourceManager->getShader(handle);
915}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917Program *Context::getProgram(GLuint handle)
918{
919 return mResourceManager->getProgram(handle);
920}
921
922Texture *Context::getTexture(GLuint handle)
923{
924 return mResourceManager->getTexture(handle);
925}
926
927Renderbuffer *Context::getRenderbuffer(GLuint handle)
928{
929 return mResourceManager->getRenderbuffer(handle);
930}
931
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000932Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000933{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000934 return getFramebuffer(mState.readFramebuffer);
935}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000936
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000937Framebuffer *Context::getDrawFramebuffer()
938{
939 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
942void Context::bindArrayBuffer(unsigned int buffer)
943{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000946 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947}
948
949void Context::bindElementArrayBuffer(unsigned int buffer)
950{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000953 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
956void Context::bindTexture2D(GLuint texture)
957{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000958 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000960 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
963void Context::bindTextureCubeMap(GLuint texture)
964{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +0000967 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968}
969
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000970void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000972 if (!getFramebuffer(framebuffer))
973 {
974 mFramebufferMap[framebuffer] = new Framebuffer();
975 }
976
977 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000978}
979
980void Context::bindDrawFramebuffer(GLuint framebuffer)
981{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000982 if (!getFramebuffer(framebuffer))
983 {
984 mFramebufferMap[framebuffer] = new Framebuffer();
985 }
986
987 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988}
989
990void Context::bindRenderbuffer(GLuint renderbuffer)
991{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000992 mResourceManager->checkRenderbufferAllocation(renderbuffer);
993
994 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995}
996
997void Context::useProgram(GLuint program)
998{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000999 GLuint priorProgram = mState.currentProgram;
1000 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001001
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001002 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001004 Program *newProgram = mResourceManager->getProgram(program);
1005 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1006
1007 if (newProgram)
1008 {
1009 newProgram->addRef();
1010 }
1011
1012 if (oldProgram)
1013 {
1014 oldProgram->release();
1015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
1018
1019void Context::setFramebufferZero(Framebuffer *buffer)
1020{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001021 delete mFramebufferMap[0];
1022 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001025void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001027 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1028 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001031Framebuffer *Context::getFramebuffer(unsigned int handle)
1032{
1033 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1034
1035 if (framebuffer == mFramebufferMap.end())
1036 {
1037 return NULL;
1038 }
1039 else
1040 {
1041 return framebuffer->second;
1042 }
1043}
1044
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001045Fence *Context::getFence(unsigned int handle)
1046{
1047 FenceMap::iterator fence = mFenceMap.find(handle);
1048
1049 if (fence == mFenceMap.end())
1050 {
1051 return NULL;
1052 }
1053 else
1054 {
1055 return fence->second;
1056 }
1057}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001058
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059Buffer *Context::getArrayBuffer()
1060{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001061 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062}
1063
1064Buffer *Context::getElementArrayBuffer()
1065{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001066 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
1069Program *Context::getCurrentProgram()
1070{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001071 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074Texture2D *Context::getTexture2D()
1075{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001076 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, SAMPLER_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079TextureCubeMap *Context::getTextureCubeMap()
1080{
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001081 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, SAMPLER_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082}
1083
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001084Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001086 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001087
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001088 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001089 {
1090 switch (type)
1091 {
1092 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001093 case SAMPLER_2D: return mTexture2DZero.get();
1094 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001095 }
1096 }
1097
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001101bool Context::getBooleanv(GLenum pname, GLboolean *params)
1102{
1103 switch (pname)
1104 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001105 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1106 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1107 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001108 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001109 params[0] = mState.colorMaskRed;
1110 params[1] = mState.colorMaskGreen;
1111 params[2] = mState.colorMaskBlue;
1112 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001114 case GL_CULL_FACE: *params = mState.cullFace; break;
1115 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1116 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1117 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1118 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1119 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1120 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1121 case GL_BLEND: *params = mState.blend; break;
1122 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001123 default:
1124 return false;
1125 }
1126
1127 return true;
1128}
1129
1130bool Context::getFloatv(GLenum pname, GLfloat *params)
1131{
1132 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1133 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1134 // GetIntegerv as its native query function. As it would require conversion in any
1135 // case, this should make no difference to the calling application.
1136 switch (pname)
1137 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001138 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1139 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1140 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1141 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1142 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001143 case GL_ALIASED_LINE_WIDTH_RANGE:
1144 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1145 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1146 break;
1147 case GL_ALIASED_POINT_SIZE_RANGE:
1148 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001149 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 +00001150 break;
1151 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001152 params[0] = mState.zNear;
1153 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001154 break;
1155 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001156 params[0] = mState.colorClearValue.red;
1157 params[1] = mState.colorClearValue.green;
1158 params[2] = mState.colorClearValue.blue;
1159 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001160 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001161 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001162 params[0] = mState.blendColor.red;
1163 params[1] = mState.blendColor.green;
1164 params[2] = mState.blendColor.blue;
1165 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001166 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001167 default:
1168 return false;
1169 }
1170
1171 return true;
1172}
1173
1174bool Context::getIntegerv(GLenum pname, GLint *params)
1175{
1176 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1177 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1178 // GetIntegerv as its native query function. As it would require conversion in any
1179 // case, this should make no difference to the calling application. You may find it in
1180 // Context::getFloatv.
1181 switch (pname)
1182 {
1183 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1184 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001185 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001186 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1187 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1188 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001189 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001190 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001191 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001192 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001193 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1194 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001195 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1196 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1197 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001198 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001199 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1200 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1201 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1202 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001203 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1205 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1206 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1207 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1208 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1209 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1210 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1211 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1212 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1213 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1214 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1215 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1216 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1217 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1218 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1219 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1220 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1221 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1222 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1223 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1224 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1225 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1226 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1227 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001228 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1229 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001230 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1231 {
1232 if (supportsCompressedTextures())
1233 {
1234 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1235 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1236 *params = 2;
1237 }
1238 else
1239 {
1240 *params = 0;
1241 }
1242 }
1243 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001244 case GL_MAX_SAMPLES_ANGLE:
1245 {
1246 GLsizei maxSamples = getMaxSupportedSamples();
1247 if (maxSamples != 0)
1248 {
1249 *params = maxSamples;
1250 }
1251 else
1252 {
1253 return false;
1254 }
1255
1256 break;
1257 }
1258 case GL_SAMPLE_BUFFERS:
1259 case GL_SAMPLES:
1260 {
1261 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1262 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1263 {
1264 switch (pname)
1265 {
1266 case GL_SAMPLE_BUFFERS:
1267 if (framebuffer->getSamples() != 0)
1268 {
1269 *params = 1;
1270 }
1271 else
1272 {
1273 *params = 0;
1274 }
1275 break;
1276 case GL_SAMPLES:
1277 *params = framebuffer->getSamples();
1278 break;
1279 }
1280 }
1281 else
1282 {
1283 *params = 0;
1284 }
1285 }
1286 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001287 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1288 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1289 case GL_MAX_VIEWPORT_DIMS:
1290 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001291 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001292 params[0] = maxDimension;
1293 params[1] = maxDimension;
1294 }
1295 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001296 case GL_COMPRESSED_TEXTURE_FORMATS:
1297 {
1298 if (supportsCompressedTextures())
1299 {
1300 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1301 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1302 }
1303 }
1304 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001305 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001306 params[0] = mState.viewportX;
1307 params[1] = mState.viewportY;
1308 params[2] = mState.viewportWidth;
1309 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 break;
1311 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001312 params[0] = mState.scissorX;
1313 params[1] = mState.scissorY;
1314 params[2] = mState.scissorWidth;
1315 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1318 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001319 case GL_RED_BITS:
1320 case GL_GREEN_BITS:
1321 case GL_BLUE_BITS:
1322 case GL_ALPHA_BITS:
1323 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001324 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1326
1327 if (colorbuffer)
1328 {
1329 switch (pname)
1330 {
1331 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1332 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1333 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1334 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1335 }
1336 }
1337 else
1338 {
1339 *params = 0;
1340 }
1341 }
1342 break;
1343 case GL_DEPTH_BITS:
1344 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001345 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001346 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001347
1348 if (depthbuffer)
1349 {
1350 *params = depthbuffer->getDepthSize();
1351 }
1352 else
1353 {
1354 *params = 0;
1355 }
1356 }
1357 break;
1358 case GL_STENCIL_BITS:
1359 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001360 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001361 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001362
1363 if (stencilbuffer)
1364 {
1365 *params = stencilbuffer->getStencilSize();
1366 }
1367 else
1368 {
1369 *params = 0;
1370 }
1371 }
1372 break;
1373 case GL_TEXTURE_BINDING_2D:
1374 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001375 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001376 {
1377 error(GL_INVALID_OPERATION);
1378 return false;
1379 }
1380
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001381 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 }
1383 break;
1384 case GL_TEXTURE_BINDING_CUBE_MAP:
1385 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001386 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 {
1388 error(GL_INVALID_OPERATION);
1389 return false;
1390 }
1391
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001392 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001393 }
1394 break;
1395 default:
1396 return false;
1397 }
1398
1399 return true;
1400}
1401
1402bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1403{
1404 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1405 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1406 // to the fact that it is stored internally as a float, and so would require conversion
1407 // if returned from Context::getIntegerv. Since this conversion is already implemented
1408 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1409 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1410 // application.
1411 switch (pname)
1412 {
1413 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1414 case GL_SHADER_BINARY_FORMATS:
1415 {
1416 *type = GL_INT;
1417 *numParams = 0;
1418 }
1419 break;
1420 case GL_MAX_VERTEX_ATTRIBS:
1421 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1422 case GL_MAX_VARYING_VECTORS:
1423 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1424 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1425 case GL_MAX_TEXTURE_IMAGE_UNITS:
1426 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1427 case GL_MAX_RENDERBUFFER_SIZE:
1428 case GL_NUM_SHADER_BINARY_FORMATS:
1429 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1430 case GL_ARRAY_BUFFER_BINDING:
1431 case GL_FRAMEBUFFER_BINDING:
1432 case GL_RENDERBUFFER_BINDING:
1433 case GL_CURRENT_PROGRAM:
1434 case GL_PACK_ALIGNMENT:
1435 case GL_UNPACK_ALIGNMENT:
1436 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001437 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001438 case GL_RED_BITS:
1439 case GL_GREEN_BITS:
1440 case GL_BLUE_BITS:
1441 case GL_ALPHA_BITS:
1442 case GL_DEPTH_BITS:
1443 case GL_STENCIL_BITS:
1444 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1445 case GL_CULL_FACE_MODE:
1446 case GL_FRONT_FACE:
1447 case GL_ACTIVE_TEXTURE:
1448 case GL_STENCIL_FUNC:
1449 case GL_STENCIL_VALUE_MASK:
1450 case GL_STENCIL_REF:
1451 case GL_STENCIL_FAIL:
1452 case GL_STENCIL_PASS_DEPTH_FAIL:
1453 case GL_STENCIL_PASS_DEPTH_PASS:
1454 case GL_STENCIL_BACK_FUNC:
1455 case GL_STENCIL_BACK_VALUE_MASK:
1456 case GL_STENCIL_BACK_REF:
1457 case GL_STENCIL_BACK_FAIL:
1458 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1459 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1460 case GL_DEPTH_FUNC:
1461 case GL_BLEND_SRC_RGB:
1462 case GL_BLEND_SRC_ALPHA:
1463 case GL_BLEND_DST_RGB:
1464 case GL_BLEND_DST_ALPHA:
1465 case GL_BLEND_EQUATION_RGB:
1466 case GL_BLEND_EQUATION_ALPHA:
1467 case GL_STENCIL_WRITEMASK:
1468 case GL_STENCIL_BACK_WRITEMASK:
1469 case GL_STENCIL_CLEAR_VALUE:
1470 case GL_SUBPIXEL_BITS:
1471 case GL_MAX_TEXTURE_SIZE:
1472 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1473 case GL_SAMPLE_BUFFERS:
1474 case GL_SAMPLES:
1475 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1476 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1477 case GL_TEXTURE_BINDING_2D:
1478 case GL_TEXTURE_BINDING_CUBE_MAP:
1479 {
1480 *type = GL_INT;
1481 *numParams = 1;
1482 }
1483 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001484 case GL_MAX_SAMPLES_ANGLE:
1485 {
1486 if (getMaxSupportedSamples() != 0)
1487 {
1488 *type = GL_INT;
1489 *numParams = 1;
1490 }
1491 else
1492 {
1493 return false;
1494 }
1495 }
1496 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001497 case GL_MAX_VIEWPORT_DIMS:
1498 {
1499 *type = GL_INT;
1500 *numParams = 2;
1501 }
1502 break;
1503 case GL_VIEWPORT:
1504 case GL_SCISSOR_BOX:
1505 {
1506 *type = GL_INT;
1507 *numParams = 4;
1508 }
1509 break;
1510 case GL_SHADER_COMPILER:
1511 case GL_SAMPLE_COVERAGE_INVERT:
1512 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001513 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1514 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1515 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1516 case GL_SAMPLE_COVERAGE:
1517 case GL_SCISSOR_TEST:
1518 case GL_STENCIL_TEST:
1519 case GL_DEPTH_TEST:
1520 case GL_BLEND:
1521 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001522 {
1523 *type = GL_BOOL;
1524 *numParams = 1;
1525 }
1526 break;
1527 case GL_COLOR_WRITEMASK:
1528 {
1529 *type = GL_BOOL;
1530 *numParams = 4;
1531 }
1532 break;
1533 case GL_POLYGON_OFFSET_FACTOR:
1534 case GL_POLYGON_OFFSET_UNITS:
1535 case GL_SAMPLE_COVERAGE_VALUE:
1536 case GL_DEPTH_CLEAR_VALUE:
1537 case GL_LINE_WIDTH:
1538 {
1539 *type = GL_FLOAT;
1540 *numParams = 1;
1541 }
1542 break;
1543 case GL_ALIASED_LINE_WIDTH_RANGE:
1544 case GL_ALIASED_POINT_SIZE_RANGE:
1545 case GL_DEPTH_RANGE:
1546 {
1547 *type = GL_FLOAT;
1548 *numParams = 2;
1549 }
1550 break;
1551 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001552 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001553 {
1554 *type = GL_FLOAT;
1555 *numParams = 4;
1556 }
1557 break;
1558 default:
1559 return false;
1560 }
1561
1562 return true;
1563}
1564
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565// Applies the render target surface, depth stencil surface, viewport rectangle and
1566// scissor rectangle to the Direct3D 9 device
1567bool Context::applyRenderTarget(bool ignoreViewport)
1568{
1569 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001570
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001571 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572
1573 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1574 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001575 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1576
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 return false;
1578 }
1579
1580 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001581
1582 if (!renderTarget)
1583 {
1584 return false; // Context must be lost
1585 }
1586
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001587 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001589 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1590 if (renderTargetSerial != mAppliedRenderTargetSerial)
1591 {
1592 device->SetRenderTarget(0, renderTarget);
1593 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001594 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 +00001595 }
1596
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001597 unsigned int depthbufferSerial = 0;
1598 unsigned int stencilbufferSerial = 0;
1599 if (framebufferObject->getDepthbufferType() != GL_NONE)
1600 {
1601 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001602 if (!depthStencil)
1603 {
1604 ERR("Depth stencil pointer unexpectedly null.");
1605 return false;
1606 }
1607
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001608 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1609 }
1610 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1611 {
1612 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001613 if (!depthStencil)
1614 {
1615 ERR("Depth stencil pointer unexpectedly null.");
1616 return false;
1617 }
1618
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001619 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1620 }
1621
1622 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001623 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001624 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001625 {
1626 device->SetDepthStencilSurface(depthStencil);
1627 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001628 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001629 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001630 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631
1632 D3DVIEWPORT9 viewport;
1633 D3DSURFACE_DESC desc;
1634 renderTarget->GetDesc(&desc);
1635
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001636 float zNear = clamp01(mState.zNear);
1637 float zFar = clamp01(mState.zFar);
1638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639 if (ignoreViewport)
1640 {
1641 viewport.X = 0;
1642 viewport.Y = 0;
1643 viewport.Width = desc.Width;
1644 viewport.Height = desc.Height;
1645 viewport.MinZ = 0.0f;
1646 viewport.MaxZ = 1.0f;
1647 }
1648 else
1649 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001650 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, desc.Height);
1651 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1652 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1653 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(desc.Width) - static_cast<LONG>(viewport.X));
1654 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(desc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001655 viewport.MinZ = zNear;
1656 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 }
1658
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001659 if (viewport.Width <= 0 || viewport.Height <= 0)
1660 {
1661 return false; // Nothing to render
1662 }
1663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 device->SetViewport(&viewport);
1665
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001666 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 if (mState.scissorTest)
1669 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001670 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, desc.Height);
1671 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
1672 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
1673 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
1674 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 device->SetScissorRect(&rect);
1676 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1677 }
1678 else
1679 {
1680 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1681 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001683 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 }
1685
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001686 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 Program *programObject = getCurrentProgram();
1689
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001690 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001691 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001692 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001693
daniel@transgaming.com31754962010-11-28 02:02:52 +00001694 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1696 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1697 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001698 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001701 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001702 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001703
daniel@transgaming.com31754962010-11-28 02:02:52 +00001704 GLint depthRange = programObject->getDxDepthRangeLocation();
1705 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1706 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 }
1708
1709 return true;
1710}
1711
1712// 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 +00001713void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714{
1715 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001716 Program *programObject = getCurrentProgram();
1717
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001718 Framebuffer *framebufferObject = getDrawFramebuffer();
1719
1720 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001723 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001724 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001726 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001727 GLint alwaysFront = !isTriangleMode(drawMode);
1728 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1729
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001730 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001734 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
1736 else
1737 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 mCullStateDirty = false;
1742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 if (mDepthStateDirty)
1745 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001746 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1749 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 }
1751 else
1752 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001753 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001755
1756 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 }
1758
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001762 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1764
1765 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1766 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1767 {
1768 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1769 }
1770 else
1771 {
1772 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1773 unorm<8>(mState.blendColor.alpha),
1774 unorm<8>(mState.blendColor.alpha),
1775 unorm<8>(mState.blendColor.alpha)));
1776 }
1777
1778 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1779 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1780 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1781
1782 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1783 mState.destBlendRGB != mState.destBlendAlpha ||
1784 mState.blendEquationRGB != mState.blendEquationAlpha)
1785 {
1786 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1787
1788 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1789 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1790 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001791 }
1792 else
1793 {
1794 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1795 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001796 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001797 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001798 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001799 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001800 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001801
1802 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 }
1804
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001805 if (mStencilStateDirty || mFrontFaceDirty)
1806 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001807 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 {
1809 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1810 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1811
1812 // FIXME: Unsupported by D3D9
1813 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1814 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1815 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1816 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1817 mState.stencilRef != mState.stencilBackRef ||
1818 mState.stencilMask != mState.stencilBackMask)
1819 {
1820 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1821 return error(GL_INVALID_OPERATION);
1822 }
1823
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001824 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001825 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001826 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1827
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001828 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1829 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830 es2dx::ConvertComparison(mState.stencilFunc));
1831
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001832 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1833 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001834
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001835 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001836 es2dx::ConvertStencilOp(mState.stencilFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001837 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001839 device->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1841
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001842 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1843 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001844 es2dx::ConvertComparison(mState.stencilBackFunc));
1845
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001846 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1847 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001849 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 es2dx::ConvertStencilOp(mState.stencilBackFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001851 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001853 device->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001854 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1855 }
1856 else
1857 {
1858 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1859 }
1860
1861 mStencilStateDirty = false;
1862 }
1863
1864 if (mMaskStateDirty)
1865 {
1866 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1867 mState.colorMaskBlue, mState.colorMaskAlpha));
1868 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1869
1870 mMaskStateDirty = false;
1871 }
1872
1873 if (mPolygonOffsetStateDirty)
1874 {
1875 if (mState.polygonOffsetFill)
1876 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001877 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001878 if (depthbuffer)
1879 {
1880 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1881 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1882 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1883 }
1884 }
1885 else
1886 {
1887 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1888 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1889 }
1890
1891 mPolygonOffsetStateDirty = false;
1892 }
1893
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001894 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001896 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001897 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001898 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001899 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001900 FIXME("Sample alpha to coverage is unimplemented.");
1901 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001902
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001903 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1904 if (mState.sampleCoverage)
1905 {
1906 unsigned int mask = 0;
1907 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001908 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001909 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001910
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001911 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001912 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001913 mask <<= 1;
1914
1915 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1916 {
1917 threshold += 1.0f;
1918 mask |= 1;
1919 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001920 }
1921 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001922
1923 if (mState.sampleCoverageInvert)
1924 {
1925 mask = ~mask;
1926 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001927
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001928 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1929 }
1930 else
1931 {
1932 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1933 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001934 }
1935 else
1936 {
1937 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001938 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001939
1940 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 }
1942
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943 if (mDitherStateDirty)
1944 {
1945 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1946
1947 mDitherStateDirty = false;
1948 }
1949
1950 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951}
1952
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001953// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001954void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001956 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001958 if (attributes[i].active)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001960 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 }
1962 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001963}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964
daniel@transgaming.com83921382011-01-08 05:46:00 +00001965GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001966{
1967 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1968
daniel@transgaming.com83921382011-01-08 05:46:00 +00001969 GLenum err = mVertexDataManager->prepareVertexData(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001970 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001971 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001972 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001973 }
1974
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001975 lookupAttributeMapping(translated);
1976
daniel@transgaming.com83921382011-01-08 05:46:00 +00001977 mVertexDataManager->setupAttributes(translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001978
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001979 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001980}
1981
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001982// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001983GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001984{
daniel@transgaming.com83921382011-01-08 05:46:00 +00001985 IDirect3DDevice9 *device = getDevice();
1986 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001987
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001988 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001989 {
daniel@transgaming.com83921382011-01-08 05:46:00 +00001990 device->SetIndices(indexInfo->indexBuffer);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001991 }
1992
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001993 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994}
1995
1996// Applies the shaders and shader constants to the Direct3D 9 device
1997void Context::applyShaders()
1998{
1999 IDirect3DDevice9 *device = getDevice();
2000 Program *programObject = getCurrentProgram();
2001 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2002 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2003
2004 device->SetVertexShader(vertexShader);
2005 device->SetPixelShader(pixelShader);
2006
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002007 if (programObject->getSerial() != mAppliedProgram)
2008 {
2009 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002010 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002011 mAppliedProgram = programObject->getSerial();
2012 }
2013
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 programObject->applyUniforms();
2015}
2016
2017// Applies the textures and sampler states to the Direct3D 9 device
2018void Context::applyTextures()
2019{
2020 IDirect3DDevice9 *device = getDevice();
2021 Program *programObject = getCurrentProgram();
2022
2023 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2024 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002025 int textureUnit = programObject->getSamplerMapping(sampler);
2026 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002028 SamplerType textureType = programObject->getSamplerType(sampler);
2029
2030 Texture *texture = getSamplerTexture(textureUnit, textureType);
2031
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002032 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002033 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002034 if (texture->isComplete())
2035 {
2036 GLenum wrapS = texture->getWrapS();
2037 GLenum wrapT = texture->getWrapT();
2038 GLenum minFilter = texture->getMinFilter();
2039 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002041 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2042 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002044 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2045 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2046 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2047 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2048 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002050 device->SetTexture(sampler, texture->getTexture());
2051 }
2052 else
2053 {
2054 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2055 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002056 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002057
2058 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002060 else
2061 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002062 if (programObject->isSamplerDirty(sampler))
2063 {
2064 device->SetTexture(sampler, NULL);
2065 programObject->setSamplerDirty(sampler, false);
2066 }
2067 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068 }
2069}
2070
2071void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2072{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002073 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002074
2075 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2076 {
2077 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2078 }
2079
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002080 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2081 {
2082 return error(GL_INVALID_OPERATION);
2083 }
2084
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002086
2087 if (!renderTarget)
2088 {
2089 return; // Context must be lost, return silently
2090 }
2091
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 IDirect3DDevice9 *device = getDevice();
2093
2094 D3DSURFACE_DESC desc;
2095 renderTarget->GetDesc(&desc);
2096
2097 IDirect3DSurface9 *systemSurface;
2098 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2099
2100 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2101 {
2102 return error(GL_OUT_OF_MEMORY);
2103 }
2104
2105 ASSERT(SUCCEEDED(result));
2106
2107 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2108 {
2109 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2110 }
2111
2112 result = device->GetRenderTargetData(renderTarget, systemSurface);
2113
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 if (FAILED(result))
2115 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 systemSurface->Release();
2117
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002118 switch (result)
2119 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002120 case D3DERR_DRIVERINTERNALERROR:
2121 case D3DERR_DEVICELOST:
2122 return error(GL_OUT_OF_MEMORY);
2123 default:
2124 UNREACHABLE();
2125 return; // No sensible error to generate
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 }
2128
2129 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002130 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2131 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2132 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2133 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2134 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135
2136 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2137
2138 if (FAILED(result))
2139 {
2140 UNREACHABLE();
2141 systemSurface->Release();
2142
2143 return; // No sensible error to generate
2144 }
2145
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002146 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002148 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002149 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002150 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 for (int j = 0; j < rect.bottom - rect.top; j++)
2153 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002154 if (desc.Format == D3DFMT_A8R8G8B8 &&
2155 format == GL_BGRA_EXT &&
2156 type == GL_UNSIGNED_BYTE)
2157 {
2158 // Fast path for EXT_read_format_bgra, given
2159 // an RGBA source buffer. Note that buffers with no
2160 // alpha go through the slow path below.
2161 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002162 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002163 (rect.right - rect.left) * 4);
2164 continue;
2165 }
2166
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 for (int i = 0; i < rect.right - rect.left; i++)
2168 {
2169 float r;
2170 float g;
2171 float b;
2172 float a;
2173
2174 switch (desc.Format)
2175 {
2176 case D3DFMT_R5G6B5:
2177 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002178 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179
2180 a = 1.0f;
2181 b = (rgb & 0x001F) * (1.0f / 0x001F);
2182 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2183 r = (rgb & 0xF800) * (1.0f / 0xF800);
2184 }
2185 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 case D3DFMT_A1R5G5B5:
2187 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002188 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189
2190 a = (argb & 0x8000) ? 1.0f : 0.0f;
2191 b = (argb & 0x001F) * (1.0f / 0x001F);
2192 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2193 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2194 }
2195 break;
2196 case D3DFMT_A8R8G8B8:
2197 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002198 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199
2200 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2201 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2202 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2203 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2204 }
2205 break;
2206 case D3DFMT_X8R8G8B8:
2207 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002208 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209
2210 a = 1.0f;
2211 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2212 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2213 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2214 }
2215 break;
2216 case D3DFMT_A2R10G10B10:
2217 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002218 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219
2220 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2221 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2222 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2223 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2224 }
2225 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002226 case D3DFMT_A32B32G32R32F:
2227 {
2228 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002229 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2230 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2231 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2232 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002233 }
2234 break;
2235 case D3DFMT_A16B16G16R16F:
2236 {
2237 // float formats in D3D are stored rgba, rather than the other way round
2238 float abgr[4];
2239
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002240 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002241
2242 a = abgr[3];
2243 b = abgr[2];
2244 g = abgr[1];
2245 r = abgr[0];
2246 }
2247 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 default:
2249 UNIMPLEMENTED(); // FIXME
2250 UNREACHABLE();
2251 }
2252
2253 switch (format)
2254 {
2255 case GL_RGBA:
2256 switch (type)
2257 {
2258 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002259 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2260 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2261 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2262 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 break;
2264 default: UNREACHABLE();
2265 }
2266 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002267 case GL_BGRA_EXT:
2268 switch (type)
2269 {
2270 case GL_UNSIGNED_BYTE:
2271 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2272 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2273 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2274 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2275 break;
2276 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2277 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2278 // this type is packed as follows:
2279 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2280 // --------------------------------------------------------------------------------
2281 // | 4th | 3rd | 2nd | 1st component |
2282 // --------------------------------------------------------------------------------
2283 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2284 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2285 ((unsigned short)(15 * a + 0.5f) << 12)|
2286 ((unsigned short)(15 * r + 0.5f) << 8) |
2287 ((unsigned short)(15 * g + 0.5f) << 4) |
2288 ((unsigned short)(15 * b + 0.5f) << 0);
2289 break;
2290 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2291 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2292 // this type is packed as follows:
2293 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2294 // --------------------------------------------------------------------------------
2295 // | 4th | 3rd | 2nd | 1st component |
2296 // --------------------------------------------------------------------------------
2297 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2298 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2299 ((unsigned short)( a + 0.5f) << 15) |
2300 ((unsigned short)(31 * r + 0.5f) << 10) |
2301 ((unsigned short)(31 * g + 0.5f) << 5) |
2302 ((unsigned short)(31 * b + 0.5f) << 0);
2303 break;
2304 default: UNREACHABLE();
2305 }
2306 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002307 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308 switch (type)
2309 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002310 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002311 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2312 ((unsigned short)(31 * b + 0.5f) << 0) |
2313 ((unsigned short)(63 * g + 0.5f) << 5) |
2314 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 break;
2316 default: UNREACHABLE();
2317 }
2318 break;
2319 default: UNREACHABLE();
2320 }
2321 }
2322 }
2323
2324 systemSurface->UnlockRect();
2325
2326 systemSurface->Release();
2327}
2328
2329void Context::clear(GLbitfield mask)
2330{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002331 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002332
2333 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2334 {
2335 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2336
2337 return;
2338 }
2339
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002340 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341 IDirect3DDevice9 *device = getDevice();
2342 DWORD flags = 0;
2343
2344 if (mask & GL_COLOR_BUFFER_BIT)
2345 {
2346 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002347
2348 if (framebufferObject->getColorbufferType() != GL_NONE)
2349 {
2350 flags |= D3DCLEAR_TARGET;
2351 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 }
2353
2354 if (mask & GL_DEPTH_BUFFER_BIT)
2355 {
2356 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002357 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 {
2359 flags |= D3DCLEAR_ZBUFFER;
2360 }
2361 }
2362
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 GLuint stencilUnmasked = 0x0;
2364
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002365 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002368 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002370 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002371 if (!depthStencil)
2372 {
2373 ERR("Depth stencil pointer unexpectedly null.");
2374 return;
2375 }
2376
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002377 D3DSURFACE_DESC desc;
2378 depthStencil->GetDesc(&desc);
2379
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002380 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002381 stencilUnmasked = (0x1 << stencilSize) - 1;
2382
2383 if (stencilUnmasked != 0x0)
2384 {
2385 flags |= D3DCLEAR_STENCIL;
2386 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 }
2388 }
2389
2390 if (mask != 0)
2391 {
2392 return error(GL_INVALID_VALUE);
2393 }
2394
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002395 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2396 {
2397 return;
2398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002400 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002401 unorm<8>(mState.colorClearValue.red),
2402 unorm<8>(mState.colorClearValue.green),
2403 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002404 float depth = clamp01(mState.depthClearValue);
2405 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406
2407 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2408
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002409 if (!renderTarget)
2410 {
2411 return; // Context must be lost, return silently
2412 }
2413
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 D3DSURFACE_DESC desc;
2415 renderTarget->GetDesc(&desc);
2416
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002417 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418
2419 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002420 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002422 !(mState.colorMaskRed && mState.colorMaskGreen &&
2423 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
2425 if (needMaskedColorClear || needMaskedStencilClear)
2426 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002427 // State which is altered in all paths from this point to the clear call is saved.
2428 // State which is altered in only some paths will be flagged dirty in the case that
2429 // that path is taken.
2430 HRESULT hr;
2431 if (mMaskedClearSavedState == NULL)
2432 {
2433 hr = device->BeginStateBlock();
2434 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2435
2436 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2437 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2438 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2439 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2440 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2441 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2442 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2443 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2444 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2445 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2446 device->SetPixelShader(NULL);
2447 device->SetVertexShader(NULL);
2448 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002449 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2450 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2451 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2452 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2453 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2454 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2455 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002456
2457 hr = device->EndStateBlock(&mMaskedClearSavedState);
2458 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2459 }
2460
2461 ASSERT(mMaskedClearSavedState != NULL);
2462
2463 if (mMaskedClearSavedState != NULL)
2464 {
2465 hr = mMaskedClearSavedState->Capture();
2466 ASSERT(SUCCEEDED(hr));
2467 }
2468
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2470 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2471 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2472 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2473 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2474 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2475 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2476 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2477
2478 if (flags & D3DCLEAR_TARGET)
2479 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002480 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 }
2482 else
2483 {
2484 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2485 }
2486
2487 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2488 {
2489 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2490 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2491 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2492 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002493 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002494 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2496 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002497 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 }
2499 else
2500 {
2501 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2502 }
2503
2504 device->SetPixelShader(NULL);
2505 device->SetVertexShader(NULL);
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002506 device->SetFVF(D3DFVF_XYZRHW);
2507 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2508 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2509 device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2510 device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2511 device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2512 device->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2513 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002515 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2516 quad[0][0] = -0.5f;
2517 quad[0][1] = desc.Height - 0.5f;
2518 quad[0][2] = 0.0f;
2519 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002521 quad[1][0] = desc.Width - 0.5f;
2522 quad[1][1] = desc.Height - 0.5f;
2523 quad[1][2] = 0.0f;
2524 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002526 quad[2][0] = -0.5f;
2527 quad[2][1] = -0.5f;
2528 quad[2][2] = 0.0f;
2529 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002531 quad[3][0] = desc.Width - 0.5f;
2532 quad[3][1] = -0.5f;
2533 quad[3][2] = 0.0f;
2534 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002536 display->startScene();
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002537 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538
2539 if (flags & D3DCLEAR_ZBUFFER)
2540 {
2541 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2542 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2543 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2544 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002545
2546 if (mMaskedClearSavedState != NULL)
2547 {
2548 mMaskedClearSavedState->Apply();
2549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002551 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 {
2553 device->Clear(0, NULL, flags, color, depth, stencil);
2554 }
2555}
2556
2557void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2558{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002559 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560 {
2561 return error(GL_INVALID_OPERATION);
2562 }
2563
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002564 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 IDirect3DDevice9 *device = getDevice();
2566 D3DPRIMITIVETYPE primitiveType;
2567 int primitiveCount;
2568
2569 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2570 return error(GL_INVALID_ENUM);
2571
2572 if (primitiveCount <= 0)
2573 {
2574 return;
2575 }
2576
2577 if (!applyRenderTarget(false))
2578 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002579 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 }
2581
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002582 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002583
daniel@transgaming.com83921382011-01-08 05:46:00 +00002584 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002585 if (err != GL_NO_ERROR)
2586 {
2587 return error(err);
2588 }
2589
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 applyShaders();
2591 applyTextures();
2592
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002593 if (!getCurrentProgram()->validateSamplers())
2594 {
2595 return error(GL_INVALID_OPERATION);
2596 }
2597
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002598 if (!cullSkipsDraw(mode))
2599 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002600 display->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002601
2602 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002603
2604 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2605 {
2606 drawClosingLine(first, first + count - 1);
2607 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002608 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609}
2610
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002611void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002613 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 {
2615 return error(GL_INVALID_OPERATION);
2616 }
2617
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002618 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 {
2620 return error(GL_INVALID_OPERATION);
2621 }
2622
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002623 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 IDirect3DDevice9 *device = getDevice();
2625 D3DPRIMITIVETYPE primitiveType;
2626 int primitiveCount;
2627
2628 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2629 return error(GL_INVALID_ENUM);
2630
2631 if (primitiveCount <= 0)
2632 {
2633 return;
2634 }
2635
2636 if (!applyRenderTarget(false))
2637 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002638 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639 }
2640
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002641 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002642
2643 TranslatedIndexData indexInfo;
2644 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2645 if (err != GL_NO_ERROR)
2646 {
2647 return error(err);
2648 }
2649
daniel@transgaming.com83921382011-01-08 05:46:00 +00002650 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2651 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002652 if (err != GL_NO_ERROR)
2653 {
2654 return error(err);
2655 }
2656
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 applyShaders();
2658 applyTextures();
2659
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002660 if (!getCurrentProgram()->validateSamplers())
2661 {
2662 return error(GL_INVALID_OPERATION);
2663 }
2664
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002665 if (!cullSkipsDraw(mode))
2666 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002667 display->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002668
daniel@transgaming.com83921382011-01-08 05:46:00 +00002669 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002670
2671 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2672 {
2673 drawClosingLine(count, type, indices);
2674 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002675 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676}
2677
2678void Context::finish()
2679{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002680 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 IDirect3DDevice9 *device = getDevice();
2682 IDirect3DQuery9 *occlusionQuery = NULL;
2683
2684 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2685
2686 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2687 {
2688 return error(GL_OUT_OF_MEMORY);
2689 }
2690
2691 ASSERT(SUCCEEDED(result));
2692
2693 if (occlusionQuery)
2694 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002695 IDirect3DStateBlock9 *savedState = NULL;
2696 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2697
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002698 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2699 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700
2701 // Render something outside the render target
2702 device->SetPixelShader(NULL);
2703 device->SetVertexShader(NULL);
2704 device->SetFVF(D3DFVF_XYZRHW);
2705 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002706 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002709 result = occlusionQuery->Issue(D3DISSUE_END);
2710 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711
2712 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2713 {
2714 // Keep polling, but allow other threads to do something useful first
2715 Sleep(0);
2716 }
2717
2718 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002719
2720 if (savedState)
2721 {
2722 savedState->Apply();
2723 savedState->Release();
2724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 }
2726}
2727
2728void Context::flush()
2729{
2730 IDirect3DDevice9 *device = getDevice();
2731 IDirect3DQuery9 *eventQuery = NULL;
2732
2733 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2734
2735 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2736 {
2737 return error(GL_OUT_OF_MEMORY);
2738 }
2739
2740 ASSERT(SUCCEEDED(result));
2741
2742 if (eventQuery)
2743 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002744 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2745 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002747 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002749
2750 if (result == D3DERR_DEVICELOST)
2751 {
2752 error(GL_OUT_OF_MEMORY);
2753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754 }
2755}
2756
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002757void Context::drawClosingLine(unsigned int first, unsigned int last)
2758{
2759 IDirect3DDevice9 *device = getDevice();
2760 IDirect3DIndexBuffer9 *indexBuffer = NULL;
2761 HRESULT result = D3DERR_INVALIDCALL;
2762
2763 if (supports32bitIndices())
2764 {
2765 result = device->CreateIndexBuffer(8, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &indexBuffer, 0);
2766
2767 if (SUCCEEDED(result))
2768 {
2769 unsigned int *data;
2770 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2771
2772 if (SUCCEEDED(result))
2773 {
2774 data[0] = last;
2775 data[1] = first;
2776 }
2777 }
2778 }
2779 else
2780 {
2781 result = device->CreateIndexBuffer(4, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &indexBuffer, 0);
2782
2783 if (SUCCEEDED(result))
2784 {
2785 unsigned short *data;
2786 result = indexBuffer->Lock(0, 0, (void**)&data, 0);
2787
2788 if (SUCCEEDED(result))
2789 {
2790 data[0] = last;
2791 data[1] = first;
2792 }
2793 }
2794 }
2795
2796 if (SUCCEEDED(result))
2797 {
2798 indexBuffer->Unlock();
2799 device->SetIndices(indexBuffer);
2800
2801 device->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, 2, 0, 1);
2802
2803 indexBuffer->Release();
2804 }
2805 else
2806 {
2807 ERR("Could not create an index buffer for closing a line loop.");
2808 error(GL_OUT_OF_MEMORY);
2809 }
2810}
2811
2812void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2813{
2814 unsigned int first = 0;
2815 unsigned int last = 0;
2816
2817 if (mState.elementArrayBuffer.get())
2818 {
2819 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2820 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2821 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2822 }
2823
2824 switch (type)
2825 {
2826 case GL_UNSIGNED_BYTE:
2827 first = static_cast<const GLubyte*>(indices)[0];
2828 last = static_cast<const GLubyte*>(indices)[count - 1];
2829 break;
2830 case GL_UNSIGNED_SHORT:
2831 first = static_cast<const GLushort*>(indices)[0];
2832 last = static_cast<const GLushort*>(indices)[count - 1];
2833 break;
2834 case GL_UNSIGNED_INT:
2835 first = static_cast<const GLuint*>(indices)[0];
2836 last = static_cast<const GLuint*>(indices)[count - 1];
2837 break;
2838 default: UNREACHABLE();
2839 }
2840
2841 drawClosingLine(first, last);
2842}
2843
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002844void Context::recordInvalidEnum()
2845{
2846 mInvalidEnum = true;
2847}
2848
2849void Context::recordInvalidValue()
2850{
2851 mInvalidValue = true;
2852}
2853
2854void Context::recordInvalidOperation()
2855{
2856 mInvalidOperation = true;
2857}
2858
2859void Context::recordOutOfMemory()
2860{
2861 mOutOfMemory = true;
2862}
2863
2864void Context::recordInvalidFramebufferOperation()
2865{
2866 mInvalidFramebufferOperation = true;
2867}
2868
2869// Get one of the recorded errors and clear its flag, if any.
2870// [OpenGL ES 2.0.24] section 2.5 page 13.
2871GLenum Context::getError()
2872{
2873 if (mInvalidEnum)
2874 {
2875 mInvalidEnum = false;
2876
2877 return GL_INVALID_ENUM;
2878 }
2879
2880 if (mInvalidValue)
2881 {
2882 mInvalidValue = false;
2883
2884 return GL_INVALID_VALUE;
2885 }
2886
2887 if (mInvalidOperation)
2888 {
2889 mInvalidOperation = false;
2890
2891 return GL_INVALID_OPERATION;
2892 }
2893
2894 if (mOutOfMemory)
2895 {
2896 mOutOfMemory = false;
2897
2898 return GL_OUT_OF_MEMORY;
2899 }
2900
2901 if (mInvalidFramebufferOperation)
2902 {
2903 mInvalidFramebufferOperation = false;
2904
2905 return GL_INVALID_FRAMEBUFFER_OPERATION;
2906 }
2907
2908 return GL_NO_ERROR;
2909}
2910
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002911bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002912{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002913 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002914}
2915
daniel@transgaming.com396c6432010-11-26 16:26:12 +00002916int Context::getMaximumVaryingVectors() const
2917{
2918 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
2919}
2920
daniel@transgaming.com458da142010-11-28 02:03:02 +00002921int Context::getMaximumFragmentUniformVectors() const
2922{
2923 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
2924}
2925
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002926int Context::getMaxSupportedSamples() const
2927{
2928 return mMaxSupportedSamples;
2929}
2930
2931int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2932{
2933 if (requested == 0)
2934 {
2935 return requested;
2936 }
2937
2938 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2939 if (itr == mMultiSampleSupport.end())
2940 {
2941 return -1;
2942 }
2943
2944 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2945 {
2946 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2947 {
2948 return i;
2949 }
2950 }
2951
2952 return -1;
2953}
2954
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002955bool Context::supportsEventQueries() const
2956{
2957 return mSupportsEventQueries;
2958}
2959
daniel@transgaming.com01868132010-08-24 19:21:17 +00002960bool Context::supportsCompressedTextures() const
2961{
2962 return mSupportsCompressedTextures;
2963}
2964
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002965bool Context::supportsFloatTextures() const
2966{
2967 return mSupportsFloatTextures;
2968}
2969
2970bool Context::supportsFloatLinearFilter() const
2971{
2972 return mSupportsFloatLinearFilter;
2973}
2974
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002975bool Context::supportsFloatRenderableTextures() const
2976{
2977 return mSupportsFloatRenderableTextures;
2978}
2979
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002980bool Context::supportsHalfFloatTextures() const
2981{
2982 return mSupportsHalfFloatTextures;
2983}
2984
2985bool Context::supportsHalfFloatLinearFilter() const
2986{
2987 return mSupportsHalfFloatLinearFilter;
2988}
2989
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002990bool Context::supportsHalfFloatRenderableTextures() const
2991{
2992 return mSupportsHalfFloatRenderableTextures;
2993}
2994
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002995int Context::getMaximumRenderbufferDimension() const
2996{
2997 return mMaxRenderbufferDimension;
2998}
2999
3000int Context::getMaximumTextureDimension() const
3001{
3002 return mMaxTextureDimension;
3003}
3004
3005int Context::getMaximumCubeTextureDimension() const
3006{
3007 return mMaxCubeTextureDimension;
3008}
3009
3010int Context::getMaximumTextureLevel() const
3011{
3012 return mMaxTextureLevel;
3013}
3014
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003015bool Context::supportsLuminanceTextures() const
3016{
3017 return mSupportsLuminanceTextures;
3018}
3019
3020bool Context::supportsLuminanceAlphaTextures() const
3021{
3022 return mSupportsLuminanceAlphaTextures;
3023}
3024
daniel@transgaming.com83921382011-01-08 05:46:00 +00003025bool Context::supports32bitIndices() const
3026{
3027 return mSupports32bitIndices;
3028}
3029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030void Context::detachBuffer(GLuint buffer)
3031{
3032 // [OpenGL ES 2.0.24] section 2.9 page 22:
3033 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3034 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3035
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003036 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003038 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039 }
3040
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003041 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003042 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003043 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003044 }
3045
3046 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3047 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003048 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003049 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003050 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003051 }
3052 }
3053}
3054
3055void Context::detachTexture(GLuint texture)
3056{
3057 // [OpenGL ES 2.0.24] section 3.8 page 84:
3058 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3059 // rebound to texture object zero
3060
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003061 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003063 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003065 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003066 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003067 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003068 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003069 }
3070 }
3071
3072 // [OpenGL ES 2.0.24] section 4.4 page 112:
3073 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3074 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3075 // image was attached in the currently bound framebuffer.
3076
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003077 Framebuffer *readFramebuffer = getReadFramebuffer();
3078 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003080 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003082 readFramebuffer->detachTexture(texture);
3083 }
3084
3085 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3086 {
3087 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003088 }
3089}
3090
3091void Context::detachFramebuffer(GLuint framebuffer)
3092{
3093 // [OpenGL ES 2.0.24] section 4.4 page 107:
3094 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3095 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3096
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003097 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003098 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003099 bindReadFramebuffer(0);
3100 }
3101
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003102 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003103 {
3104 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003105 }
3106}
3107
3108void Context::detachRenderbuffer(GLuint renderbuffer)
3109{
3110 // [OpenGL ES 2.0.24] section 4.4 page 109:
3111 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3112 // had been executed with the target RENDERBUFFER and name of zero.
3113
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003114 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003115 {
3116 bindRenderbuffer(0);
3117 }
3118
3119 // [OpenGL ES 2.0.24] section 4.4 page 111:
3120 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3121 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3122 // point to which this image was attached in the currently bound framebuffer.
3123
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003124 Framebuffer *readFramebuffer = getReadFramebuffer();
3125 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003126
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003127 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003129 readFramebuffer->detachRenderbuffer(renderbuffer);
3130 }
3131
3132 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3133 {
3134 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003135 }
3136}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003137
3138Texture *Context::getIncompleteTexture(SamplerType type)
3139{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003140 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003141
3142 if (t == NULL)
3143 {
3144 static const GLubyte color[] = { 0, 0, 0, 255 };
3145
3146 switch (type)
3147 {
3148 default:
3149 UNREACHABLE();
3150 // default falls through to SAMPLER_2D
3151
3152 case SAMPLER_2D:
3153 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003154 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003155 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003156 t = incomplete2d;
3157 }
3158 break;
3159
3160 case SAMPLER_CUBE:
3161 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003162 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003163
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003164 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3165 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3166 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3167 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3168 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3169 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003170
3171 t = incompleteCube;
3172 }
3173 break;
3174 }
3175
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003176 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003177 }
3178
3179 return t;
3180}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003181
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003182bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003183{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003184 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003185}
3186
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003187bool Context::isTriangleMode(GLenum drawMode)
3188{
3189 switch (drawMode)
3190 {
3191 case GL_TRIANGLES:
3192 case GL_TRIANGLE_FAN:
3193 case GL_TRIANGLE_STRIP:
3194 return true;
3195 case GL_POINTS:
3196 case GL_LINES:
3197 case GL_LINE_LOOP:
3198 case GL_LINE_STRIP:
3199 return false;
3200 default: UNREACHABLE();
3201 }
3202
3203 return false;
3204}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003205
3206void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3207{
3208 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3209
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003210 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3211 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3212 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3213 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003214
daniel@transgaming.com83921382011-01-08 05:46:00 +00003215 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003216}
3217
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003218void Context::initExtensionString()
3219{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003220 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003221 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3222 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003223 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003224 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003225 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003226
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003227 if (supportsEventQueries())
3228 {
3229 mExtensionString += "GL_NV_fence ";
3230 }
3231
daniel@transgaming.com01868132010-08-24 19:21:17 +00003232 if (supportsCompressedTextures())
3233 {
3234 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3235 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003236
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003237 if (supportsFloatTextures())
3238 {
3239 mExtensionString += "GL_OES_texture_float ";
3240 }
3241
3242 if (supportsHalfFloatTextures())
3243 {
3244 mExtensionString += "GL_OES_texture_half_float ";
3245 }
3246
3247 if (supportsFloatLinearFilter())
3248 {
3249 mExtensionString += "GL_OES_texture_float_linear ";
3250 }
3251
3252 if (supportsHalfFloatLinearFilter())
3253 {
3254 mExtensionString += "GL_OES_texture_half_float_linear ";
3255 }
3256
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003257 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003258 {
3259 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3260 }
3261
daniel@transgaming.com83921382011-01-08 05:46:00 +00003262 if (supports32bitIndices())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003263 {
3264 mExtensionString += "GL_OES_element_index_uint ";
3265 }
3266
3267 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3268 if (end != std::string::npos)
3269 {
3270 mExtensionString.resize(end+1);
3271 }
3272}
3273
3274const char *Context::getExtensionString() const
3275{
3276 return mExtensionString.c_str();
3277}
3278
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003279void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3280 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3281 GLbitfield mask)
3282{
3283 IDirect3DDevice9 *device = getDevice();
3284
3285 Framebuffer *readFramebuffer = getReadFramebuffer();
3286 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3287
3288 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3289 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3290 {
3291 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3292 }
3293
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003294 if (drawFramebuffer->getSamples() != 0)
3295 {
3296 return error(GL_INVALID_OPERATION);
3297 }
3298
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003299 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3300 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3301 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3302 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3303
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003304 RECT sourceRect;
3305 RECT destRect;
3306
3307 if (srcX0 < srcX1)
3308 {
3309 sourceRect.left = srcX0;
3310 sourceRect.right = srcX1;
3311 destRect.left = dstX0;
3312 destRect.right = dstX1;
3313 }
3314 else
3315 {
3316 sourceRect.left = srcX1;
3317 destRect.left = dstX1;
3318 sourceRect.right = srcX0;
3319 destRect.right = dstX0;
3320 }
3321
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003322 if (srcY0 < srcY1)
3323 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003324 sourceRect.top = readBufferHeight - srcY1;
3325 destRect.top = drawBufferHeight - dstY1;
3326 sourceRect.bottom = readBufferHeight - srcY0;
3327 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003328 }
3329 else
3330 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003331 sourceRect.top = readBufferHeight - srcY0;
3332 destRect.top = drawBufferHeight - dstY0;
3333 sourceRect.bottom = readBufferHeight - srcY1;
3334 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003335 }
3336
3337 RECT sourceScissoredRect = sourceRect;
3338 RECT destScissoredRect = destRect;
3339
3340 if (mState.scissorTest)
3341 {
3342 // Only write to parts of the destination framebuffer which pass the scissor test
3343 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3344 // rect will be checked against scissorY, rather than the bottom.
3345 if (destRect.left < mState.scissorX)
3346 {
3347 int xDiff = mState.scissorX - destRect.left;
3348 destScissoredRect.left = mState.scissorX;
3349 sourceScissoredRect.left += xDiff;
3350 }
3351
3352 if (destRect.right > mState.scissorX + mState.scissorWidth)
3353 {
3354 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3355 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3356 sourceScissoredRect.right -= xDiff;
3357 }
3358
3359 if (destRect.top < mState.scissorY)
3360 {
3361 int yDiff = mState.scissorY - destRect.top;
3362 destScissoredRect.top = mState.scissorY;
3363 sourceScissoredRect.top += yDiff;
3364 }
3365
3366 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3367 {
3368 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3369 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3370 sourceScissoredRect.bottom -= yDiff;
3371 }
3372 }
3373
3374 bool blitRenderTarget = false;
3375 bool blitDepthStencil = false;
3376
3377 RECT sourceTrimmedRect = sourceScissoredRect;
3378 RECT destTrimmedRect = destScissoredRect;
3379
3380 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3381 // the actual draw and read surfaces.
3382 if (sourceTrimmedRect.left < 0)
3383 {
3384 int xDiff = 0 - sourceTrimmedRect.left;
3385 sourceTrimmedRect.left = 0;
3386 destTrimmedRect.left += xDiff;
3387 }
3388
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003389 if (sourceTrimmedRect.right > readBufferWidth)
3390 {
3391 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3392 sourceTrimmedRect.right = readBufferWidth;
3393 destTrimmedRect.right -= xDiff;
3394 }
3395
3396 if (sourceTrimmedRect.top < 0)
3397 {
3398 int yDiff = 0 - sourceTrimmedRect.top;
3399 sourceTrimmedRect.top = 0;
3400 destTrimmedRect.top += yDiff;
3401 }
3402
3403 if (sourceTrimmedRect.bottom > readBufferHeight)
3404 {
3405 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3406 sourceTrimmedRect.bottom = readBufferHeight;
3407 destTrimmedRect.bottom -= yDiff;
3408 }
3409
3410 if (destTrimmedRect.left < 0)
3411 {
3412 int xDiff = 0 - destTrimmedRect.left;
3413 destTrimmedRect.left = 0;
3414 sourceTrimmedRect.left += xDiff;
3415 }
3416
3417 if (destTrimmedRect.right > drawBufferWidth)
3418 {
3419 int xDiff = destTrimmedRect.right - drawBufferWidth;
3420 destTrimmedRect.right = drawBufferWidth;
3421 sourceTrimmedRect.right -= xDiff;
3422 }
3423
3424 if (destTrimmedRect.top < 0)
3425 {
3426 int yDiff = 0 - destTrimmedRect.top;
3427 destTrimmedRect.top = 0;
3428 sourceTrimmedRect.top += yDiff;
3429 }
3430
3431 if (destTrimmedRect.bottom > drawBufferHeight)
3432 {
3433 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3434 destTrimmedRect.bottom = drawBufferHeight;
3435 sourceTrimmedRect.bottom -= yDiff;
3436 }
3437
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003438 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003439 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3440 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3441 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3442 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003443 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3444 {
3445 partialBufferCopy = true;
3446 }
3447
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003448 if (mask & GL_COLOR_BUFFER_BIT)
3449 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003450 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3451 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3452 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3453 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3454 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003455 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3456 {
3457 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3458 return error(GL_INVALID_OPERATION);
3459 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003460
3461 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3462 {
3463 return error(GL_INVALID_OPERATION);
3464 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003465
3466 blitRenderTarget = true;
3467
3468 }
3469
3470 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3471 {
3472 DepthStencilbuffer *readDSBuffer = NULL;
3473 DepthStencilbuffer *drawDSBuffer = NULL;
3474
3475 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3476 // both a depth and stencil buffer, it will be the same buffer.
3477
3478 if (mask & GL_DEPTH_BUFFER_BIT)
3479 {
3480 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3481 {
3482 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3483 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3484 {
3485 return error(GL_INVALID_OPERATION);
3486 }
3487
3488 blitDepthStencil = true;
3489 readDSBuffer = readFramebuffer->getDepthbuffer();
3490 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3491 }
3492 }
3493
3494 if (mask & GL_STENCIL_BUFFER_BIT)
3495 {
3496 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3497 {
3498 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3499 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3500 {
3501 return error(GL_INVALID_OPERATION);
3502 }
3503
3504 blitDepthStencil = true;
3505 readDSBuffer = readFramebuffer->getStencilbuffer();
3506 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3507 }
3508 }
3509
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003510 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003511 {
3512 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3513 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3514 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003515
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003516 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3517 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003518 {
3519 return error(GL_INVALID_OPERATION);
3520 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003521 }
3522
3523 if (blitRenderTarget || blitDepthStencil)
3524 {
3525 egl::Display *display = getDisplay();
3526 display->endScene();
3527
3528 if (blitRenderTarget)
3529 {
3530 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3531 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3532
3533 if (FAILED(result))
3534 {
3535 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3536 return;
3537 }
3538 }
3539
3540 if (blitDepthStencil)
3541 {
3542 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3543
3544 if (FAILED(result))
3545 {
3546 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3547 return;
3548 }
3549 }
3550 }
3551}
3552
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003553}
3554
3555extern "C"
3556{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003557gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003558{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003559 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003560}
3561
3562void glDestroyContext(gl::Context *context)
3563{
3564 delete context;
3565
3566 if (context == gl::getContext())
3567 {
3568 gl::makeCurrent(NULL, NULL, NULL);
3569 }
3570}
3571
3572void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3573{
3574 gl::makeCurrent(context, display, surface);
3575}
3576
3577gl::Context *glGetCurrentContext()
3578{
3579 return gl::getContext();
3580}
3581}