blob: ef0fe643346d0f9ca64befc2f15697d5e76b1d3c [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000087 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000090
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.viewportX = 0;
92 mState.viewportY = 0;
93 mState.viewportWidth = config->mDisplayMode.Width;
94 mState.viewportHeight = config->mDisplayMode.Height;
95 mState.zNear = 0.0f;
96 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.scissorX = 0;
99 mState.scissorY = 0;
100 mState.scissorWidth = config->mDisplayMode.Width;
101 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.colorMaskRed = true;
104 mState.colorMaskGreen = true;
105 mState.colorMaskBlue = true;
106 mState.colorMaskAlpha = true;
107 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000109 if (shareContext != NULL)
110 {
111 mResourceManager = shareContext->mResourceManager;
112 mResourceManager->addRef();
113 }
114 else
115 {
116 mResourceManager = new ResourceManager();
117 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 // [OpenGL ES 2.0.24] section 3.7 page 83:
120 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
121 // and cube map texture state vectors respectively associated with them.
122 // In order that access to these initial textures not be lost, they are treated as texture
123 // objects all of whose names are 0.
124
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000125 mTexture2DZero.set(new Texture2D(0));
126 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000128 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000129 bindArrayBuffer(0);
130 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 bindTextureCubeMap(0);
132 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000133 bindReadFramebuffer(0);
134 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindRenderbuffer(0);
136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000139 mState.packAlignment = 4;
140 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000141
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 mBufferBackEnd = NULL;
143 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000144 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000145 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 mInvalidEnum = false;
148 mInvalidValue = false;
149 mInvalidOperation = false;
150 mOutOfMemory = false;
151 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000152
153 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000154
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000155 mSupportsCompressedTextures = false;
156 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000157 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000158 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000159 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162Context::~Context()
163{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000164 if (mState.currentProgram != 0)
165 {
166 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
167 if (programObject)
168 {
169 programObject->release();
170 }
171 mState.currentProgram = 0;
172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000174 while (!mFramebufferMap.empty())
175 {
176 deleteFramebuffer(mFramebufferMap.begin()->first);
177 }
178
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000179 while (!mFenceMap.empty())
180 {
181 deleteFence(mFenceMap.begin()->first);
182 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000183
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000184 while (!mMultiSampleSupport.empty())
185 {
186 delete [] mMultiSampleSupport.begin()->second;
187 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
188 }
189
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000190 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
191 {
192 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
193 {
194 mState.samplerTexture[type][sampler].set(NULL);
195 }
196 }
197
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000200 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000201 }
202
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
204 {
205 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
206 }
207
208 mState.arrayBuffer.set(NULL);
209 mState.elementArrayBuffer.set(NULL);
210 mState.texture2D.set(NULL);
211 mState.textureCubeMap.set(NULL);
212 mState.renderbuffer.set(NULL);
213
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000214 mTexture2DZero.set(NULL);
215 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000217 delete mBufferBackEnd;
218 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000219 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000220 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000221
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000222 if (mMaskedClearSavedState)
223 {
224 mMaskedClearSavedState->Release();
225 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000226
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000227 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228}
229
230void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
231{
232 IDirect3DDevice9 *device = display->getDevice();
233
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000234 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000235 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000236 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000237
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000243 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
244
245 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
246 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
247 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
248 mMaxRenderbufferDimension = mMaxTextureDimension;
249 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
250 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
251 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
252
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000253 const D3DFORMAT renderBufferFormats[] =
254 {
255 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000256 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000257 D3DFMT_R5G6B5,
258 D3DFMT_D24S8
259 };
260
261 int max = 0;
262 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
263 {
264 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
265 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
266 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
267
268 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
269 {
270 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
271 {
272 max = j;
273 }
274 }
275 }
276
277 mMaxSupportedSamples = max;
278
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000279 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000280 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000281 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
282 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000283 mSupportsLuminanceTextures = display->getLuminanceTextureSupport();
284 mSupportsLuminanceAlphaTextures = display->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000285
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000286 initExtensionString();
287
288 mState.viewportX = 0;
289 mState.viewportY = 0;
290 mState.viewportWidth = surface->getWidth();
291 mState.viewportHeight = surface->getHeight();
292
293 mState.scissorX = 0;
294 mState.scissorY = 0;
295 mState.scissorWidth = surface->getWidth();
296 mState.scissorHeight = surface->getHeight();
297
298 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000299 }
300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
302 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000303 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000306 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000307 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
309 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000311 if (defaultRenderTarget)
312 {
313 defaultRenderTarget->Release();
314 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000316 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000318 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000320
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000321 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322}
323
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000324// 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 +0000325void Context::markAllStateDirty()
326{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000327 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000328 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000329 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000330 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000331 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000332
333 mClearStateDirty = true;
334 mCullStateDirty = true;
335 mDepthStateDirty = true;
336 mMaskStateDirty = true;
337 mBlendStateDirty = true;
338 mStencilStateDirty = true;
339 mPolygonOffsetStateDirty = true;
340 mScissorStateDirty = true;
341 mSampleStateDirty = true;
342 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000343 mFrontFaceDirty = true;
344
345 if (mBufferBackEnd != NULL)
346 {
347 mBufferBackEnd->invalidate();
348 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000349}
350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351void Context::setClearColor(float red, float green, float blue, float alpha)
352{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000353 mState.colorClearValue.red = red;
354 mState.colorClearValue.green = green;
355 mState.colorClearValue.blue = blue;
356 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357}
358
359void Context::setClearDepth(float depth)
360{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000361 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362}
363
364void Context::setClearStencil(int stencil)
365{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000366 mState.stencilClearValue = stencil;
367}
368
369void Context::setCullFace(bool enabled)
370{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000371 if (mState.cullFace != enabled)
372 {
373 mState.cullFace = enabled;
374 mCullStateDirty = true;
375 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000376}
377
378bool Context::isCullFaceEnabled() const
379{
380 return mState.cullFace;
381}
382
383void Context::setCullMode(GLenum mode)
384{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000385 if (mState.cullMode != mode)
386 {
387 mState.cullMode = mode;
388 mCullStateDirty = true;
389 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000390}
391
392void Context::setFrontFace(GLenum front)
393{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000394 if (mState.frontFace != front)
395 {
396 mState.frontFace = front;
397 mFrontFaceDirty = true;
398 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000399}
400
401void Context::setDepthTest(bool enabled)
402{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000403 if (mState.depthTest != enabled)
404 {
405 mState.depthTest = enabled;
406 mDepthStateDirty = true;
407 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000408}
409
410bool Context::isDepthTestEnabled() const
411{
412 return mState.depthTest;
413}
414
415void Context::setDepthFunc(GLenum depthFunc)
416{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000417 if (mState.depthFunc != depthFunc)
418 {
419 mState.depthFunc = depthFunc;
420 mDepthStateDirty = true;
421 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000422}
423
424void Context::setDepthRange(float zNear, float zFar)
425{
426 mState.zNear = zNear;
427 mState.zFar = zFar;
428}
429
430void Context::setBlend(bool enabled)
431{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000432 if (mState.blend != enabled)
433 {
434 mState.blend = enabled;
435 mBlendStateDirty = true;
436 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000437}
438
439bool Context::isBlendEnabled() const
440{
441 return mState.blend;
442}
443
444void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
445{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000446 if (mState.sourceBlendRGB != sourceRGB ||
447 mState.sourceBlendAlpha != sourceAlpha ||
448 mState.destBlendRGB != destRGB ||
449 mState.destBlendAlpha != destAlpha)
450 {
451 mState.sourceBlendRGB = sourceRGB;
452 mState.destBlendRGB = destRGB;
453 mState.sourceBlendAlpha = sourceAlpha;
454 mState.destBlendAlpha = destAlpha;
455 mBlendStateDirty = true;
456 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000457}
458
459void Context::setBlendColor(float red, float green, float blue, float alpha)
460{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000461 if (mState.blendColor.red != red ||
462 mState.blendColor.green != green ||
463 mState.blendColor.blue != blue ||
464 mState.blendColor.alpha != alpha)
465 {
466 mState.blendColor.red = red;
467 mState.blendColor.green = green;
468 mState.blendColor.blue = blue;
469 mState.blendColor.alpha = alpha;
470 mBlendStateDirty = true;
471 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000472}
473
474void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
475{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000476 if (mState.blendEquationRGB != rgbEquation ||
477 mState.blendEquationAlpha != alphaEquation)
478 {
479 mState.blendEquationRGB = rgbEquation;
480 mState.blendEquationAlpha = alphaEquation;
481 mBlendStateDirty = true;
482 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000483}
484
485void Context::setStencilTest(bool enabled)
486{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000487 if (mState.stencilTest != enabled)
488 {
489 mState.stencilTest = enabled;
490 mStencilStateDirty = true;
491 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000492}
493
494bool Context::isStencilTestEnabled() const
495{
496 return mState.stencilTest;
497}
498
499void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
500{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000501 if (mState.stencilFunc != stencilFunc ||
502 mState.stencilRef != stencilRef ||
503 mState.stencilMask != stencilMask)
504 {
505 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000506 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000507 mState.stencilMask = stencilMask;
508 mStencilStateDirty = true;
509 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000510}
511
512void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
513{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000514 if (mState.stencilBackFunc != stencilBackFunc ||
515 mState.stencilBackRef != stencilBackRef ||
516 mState.stencilBackMask != stencilBackMask)
517 {
518 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000519 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000520 mState.stencilBackMask = stencilBackMask;
521 mStencilStateDirty = true;
522 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000523}
524
525void Context::setStencilWritemask(GLuint stencilWritemask)
526{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000527 if (mState.stencilWritemask != stencilWritemask)
528 {
529 mState.stencilWritemask = stencilWritemask;
530 mStencilStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.stencilBackWritemask != stencilBackWritemask)
537 {
538 mState.stencilBackWritemask = stencilBackWritemask;
539 mStencilStateDirty = true;
540 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000541}
542
543void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
544{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000545 if (mState.stencilFail != stencilFail ||
546 mState.stencilPassDepthFail != stencilPassDepthFail ||
547 mState.stencilPassDepthPass != stencilPassDepthPass)
548 {
549 mState.stencilFail = stencilFail;
550 mState.stencilPassDepthFail = stencilPassDepthFail;
551 mState.stencilPassDepthPass = stencilPassDepthPass;
552 mStencilStateDirty = true;
553 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000554}
555
556void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
557{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000558 if (mState.stencilBackFail != stencilBackFail ||
559 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
560 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
561 {
562 mState.stencilBackFail = stencilBackFail;
563 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
564 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
565 mStencilStateDirty = true;
566 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000567}
568
569void Context::setPolygonOffsetFill(bool enabled)
570{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571 if (mState.polygonOffsetFill != enabled)
572 {
573 mState.polygonOffsetFill = enabled;
574 mPolygonOffsetStateDirty = true;
575 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000576}
577
578bool Context::isPolygonOffsetFillEnabled() const
579{
580 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000582}
583
584void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
585{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000586 if (mState.polygonOffsetFactor != factor ||
587 mState.polygonOffsetUnits != units)
588 {
589 mState.polygonOffsetFactor = factor;
590 mState.polygonOffsetUnits = units;
591 mPolygonOffsetStateDirty = true;
592 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000593}
594
595void Context::setSampleAlphaToCoverage(bool enabled)
596{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000597 if (mState.sampleAlphaToCoverage != enabled)
598 {
599 mState.sampleAlphaToCoverage = enabled;
600 mSampleStateDirty = true;
601 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000602}
603
604bool Context::isSampleAlphaToCoverageEnabled() const
605{
606 return mState.sampleAlphaToCoverage;
607}
608
609void Context::setSampleCoverage(bool enabled)
610{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000611 if (mState.sampleCoverage != enabled)
612 {
613 mState.sampleCoverage = enabled;
614 mSampleStateDirty = true;
615 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000616}
617
618bool Context::isSampleCoverageEnabled() const
619{
620 return mState.sampleCoverage;
621}
622
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000623void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000624{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000625 if (mState.sampleCoverageValue != value ||
626 mState.sampleCoverageInvert != invert)
627 {
628 mState.sampleCoverageValue = value;
629 mState.sampleCoverageInvert = invert;
630 mSampleStateDirty = true;
631 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000632}
633
634void Context::setScissorTest(bool enabled)
635{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000636 if (mState.scissorTest != enabled)
637 {
638 mState.scissorTest = enabled;
639 mScissorStateDirty = true;
640 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643bool Context::isScissorTestEnabled() const
644{
645 return mState.scissorTest;
646}
647
648void Context::setDither(bool enabled)
649{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000650 if (mState.dither != enabled)
651 {
652 mState.dither = enabled;
653 mDitherStateDirty = true;
654 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000655}
656
657bool Context::isDitherEnabled() const
658{
659 return mState.dither;
660}
661
662void Context::setLineWidth(GLfloat width)
663{
664 mState.lineWidth = width;
665}
666
667void Context::setGenerateMipmapHint(GLenum hint)
668{
669 mState.generateMipmapHint = hint;
670}
671
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000672void Context::setFragmentShaderDerivativeHint(GLenum hint)
673{
674 mState.fragmentShaderDerivativeHint = hint;
675 // TODO: Propagate the hint to shader translator so we can write
676 // ddx, ddx_coarse, or ddx_fine depending on the hint.
677 // Ignore for now. It is valid for implementations to ignore hint.
678}
679
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000680void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
681{
682 mState.viewportX = x;
683 mState.viewportY = y;
684 mState.viewportWidth = width;
685 mState.viewportHeight = height;
686}
687
688void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
689{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000690 if (mState.scissorX != x || mState.scissorY != y ||
691 mState.scissorWidth != width || mState.scissorHeight != height)
692 {
693 mState.scissorX = x;
694 mState.scissorY = y;
695 mState.scissorWidth = width;
696 mState.scissorHeight = height;
697 mScissorStateDirty = true;
698 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000699}
700
701void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
702{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000703 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
704 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
705 {
706 mState.colorMaskRed = red;
707 mState.colorMaskGreen = green;
708 mState.colorMaskBlue = blue;
709 mState.colorMaskAlpha = alpha;
710 mMaskStateDirty = true;
711 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000712}
713
714void Context::setDepthMask(bool mask)
715{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000716 if (mState.depthMask != mask)
717 {
718 mState.depthMask = mask;
719 mMaskStateDirty = true;
720 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000721}
722
723void Context::setActiveSampler(int active)
724{
725 mState.activeSampler = active;
726}
727
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000728GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000729{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000730 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000731}
732
733GLuint Context::getDrawFramebufferHandle() const
734{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000735 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738GLuint Context::getRenderbufferHandle() const
739{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000740 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000741}
742
743GLuint Context::getArrayBufferHandle() const
744{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000745 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746}
747
748void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
749{
750 mState.vertexAttribute[attribNum].mEnabled = enabled;
751}
752
753const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
754{
755 return mState.vertexAttribute[attribNum];
756}
757
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000758void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759 GLsizei stride, const void *pointer)
760{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000761 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000762 mState.vertexAttribute[attribNum].mSize = size;
763 mState.vertexAttribute[attribNum].mType = type;
764 mState.vertexAttribute[attribNum].mNormalized = normalized;
765 mState.vertexAttribute[attribNum].mStride = stride;
766 mState.vertexAttribute[attribNum].mPointer = pointer;
767}
768
769const void *Context::getVertexAttribPointer(unsigned int attribNum) const
770{
771 return mState.vertexAttribute[attribNum].mPointer;
772}
773
774// returns entire set of attributes as a block
775const AttributeState *Context::getVertexAttribBlock()
776{
777 return mState.vertexAttribute;
778}
779
780void Context::setPackAlignment(GLint alignment)
781{
782 mState.packAlignment = alignment;
783}
784
785GLint Context::getPackAlignment() const
786{
787 return mState.packAlignment;
788}
789
790void Context::setUnpackAlignment(GLint alignment)
791{
792 mState.unpackAlignment = alignment;
793}
794
795GLint Context::getUnpackAlignment() const
796{
797 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800GLuint Context::createBuffer()
801{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000802 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803}
804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805GLuint Context::createProgram()
806{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000807 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808}
809
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810GLuint Context::createShader(GLenum type)
811{
812 return mResourceManager->createShader(type);
813}
814
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815GLuint Context::createTexture()
816{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000817 return mResourceManager->createTexture();
818}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000820GLuint Context::createRenderbuffer()
821{
822 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823}
824
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000825// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826GLuint Context::createFramebuffer()
827{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000828 unsigned int handle = 1;
829
830 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
831 {
832 handle++;
833 }
834
835 mFramebufferMap[handle] = NULL;
836
837 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000840GLuint Context::createFence()
841{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000842 unsigned int handle = 0;
843
844 while (mFenceMap.find(handle) != mFenceMap.end())
845 {
846 handle++;
847 }
848
849 mFenceMap[handle] = new Fence;
850
851 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000852}
853
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854void Context::deleteBuffer(GLuint buffer)
855{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 {
858 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860
861 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
864void Context::deleteShader(GLuint shader)
865{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000866 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867}
868
869void Context::deleteProgram(GLuint program)
870{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000871 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872}
873
874void Context::deleteTexture(GLuint texture)
875{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000876 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 {
878 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000880
881 mResourceManager->deleteTexture(texture);
882}
883
884void Context::deleteRenderbuffer(GLuint renderbuffer)
885{
886 if (mResourceManager->getRenderbuffer(renderbuffer))
887 {
888 detachRenderbuffer(renderbuffer);
889 }
890
891 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
894void Context::deleteFramebuffer(GLuint framebuffer)
895{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000896 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
897
898 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899 {
900 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000901
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000902 delete framebufferObject->second;
903 mFramebufferMap.erase(framebufferObject);
904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000906
907void Context::deleteFence(GLuint fence)
908{
909 FenceMap::iterator fenceObject = mFenceMap.find(fence);
910
911 if (fenceObject != mFenceMap.end())
912 {
913 delete fenceObject->second;
914 mFenceMap.erase(fenceObject);
915 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920 return mResourceManager->getBuffer(handle);
921}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923Shader *Context::getShader(GLuint handle)
924{
925 return mResourceManager->getShader(handle);
926}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928Program *Context::getProgram(GLuint handle)
929{
930 return mResourceManager->getProgram(handle);
931}
932
933Texture *Context::getTexture(GLuint handle)
934{
935 return mResourceManager->getTexture(handle);
936}
937
938Renderbuffer *Context::getRenderbuffer(GLuint handle)
939{
940 return mResourceManager->getRenderbuffer(handle);
941}
942
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000944{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000945 return getFramebuffer(mState.readFramebuffer);
946}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000947
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000948Framebuffer *Context::getDrawFramebuffer()
949{
950 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951}
952
953void Context::bindArrayBuffer(unsigned int buffer)
954{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000955 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
960void Context::bindElementArrayBuffer(unsigned int buffer)
961{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000964 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
967void Context::bindTexture2D(GLuint texture)
968{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000971 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000973 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
976void Context::bindTextureCubeMap(GLuint texture)
977{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000978 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000980 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000982 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000985void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000987 if (!getFramebuffer(framebuffer))
988 {
989 mFramebufferMap[framebuffer] = new Framebuffer();
990 }
991
992 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000993}
994
995void Context::bindDrawFramebuffer(GLuint framebuffer)
996{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000997 if (!getFramebuffer(framebuffer))
998 {
999 mFramebufferMap[framebuffer] = new Framebuffer();
1000 }
1001
1002 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003}
1004
1005void Context::bindRenderbuffer(GLuint renderbuffer)
1006{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001007 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1008
1009 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010}
1011
1012void Context::useProgram(GLuint program)
1013{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001014 GLuint priorProgram = mState.currentProgram;
1015 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001016
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001017 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001019 Program *newProgram = mResourceManager->getProgram(program);
1020 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1021
1022 if (newProgram)
1023 {
1024 newProgram->addRef();
1025 }
1026
1027 if (oldProgram)
1028 {
1029 oldProgram->release();
1030 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032}
1033
1034void Context::setFramebufferZero(Framebuffer *buffer)
1035{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001036 delete mFramebufferMap[0];
1037 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001040void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001042 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1043 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044}
1045
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001046Framebuffer *Context::getFramebuffer(unsigned int handle)
1047{
1048 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1049
1050 if (framebuffer == mFramebufferMap.end())
1051 {
1052 return NULL;
1053 }
1054 else
1055 {
1056 return framebuffer->second;
1057 }
1058}
1059
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001060Fence *Context::getFence(unsigned int handle)
1061{
1062 FenceMap::iterator fence = mFenceMap.find(handle);
1063
1064 if (fence == mFenceMap.end())
1065 {
1066 return NULL;
1067 }
1068 else
1069 {
1070 return fence->second;
1071 }
1072}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001073
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074Buffer *Context::getArrayBuffer()
1075{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001076 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079Buffer *Context::getElementArrayBuffer()
1080{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001081 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082}
1083
1084Program *Context::getCurrentProgram()
1085{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001086 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
1089Texture2D *Context::getTexture2D()
1090{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 if (mState.texture2D.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001093 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094 }
1095
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
1099TextureCubeMap *Context::getTextureCubeMap()
1100{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 if (mState.textureCubeMap.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001103 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104 }
1105
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001106 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001109Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001111 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001112
1113 if (texid == 0)
1114 {
1115 switch (type)
1116 {
1117 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001118 case SAMPLER_2D: return mTexture2DZero.get();
1119 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001120 }
1121 }
1122
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001123 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124}
1125
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001126bool Context::getBooleanv(GLenum pname, GLboolean *params)
1127{
1128 switch (pname)
1129 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001130 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1131 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1132 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001133 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001134 params[0] = mState.colorMaskRed;
1135 params[1] = mState.colorMaskGreen;
1136 params[2] = mState.colorMaskBlue;
1137 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001138 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001139 case GL_CULL_FACE: *params = mState.cullFace; break;
1140 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1141 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1142 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1143 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1144 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1145 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1146 case GL_BLEND: *params = mState.blend; break;
1147 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001148 default:
1149 return false;
1150 }
1151
1152 return true;
1153}
1154
1155bool Context::getFloatv(GLenum pname, GLfloat *params)
1156{
1157 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1158 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1159 // GetIntegerv as its native query function. As it would require conversion in any
1160 // case, this should make no difference to the calling application.
1161 switch (pname)
1162 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001163 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1164 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1165 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1166 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1167 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001168 case GL_ALIASED_LINE_WIDTH_RANGE:
1169 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1170 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1171 break;
1172 case GL_ALIASED_POINT_SIZE_RANGE:
1173 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001174 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 +00001175 break;
1176 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001177 params[0] = mState.zNear;
1178 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 break;
1180 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001181 params[0] = mState.colorClearValue.red;
1182 params[1] = mState.colorClearValue.green;
1183 params[2] = mState.colorClearValue.blue;
1184 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001185 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001186 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001187 params[0] = mState.blendColor.red;
1188 params[1] = mState.blendColor.green;
1189 params[2] = mState.blendColor.blue;
1190 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001191 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001192 default:
1193 return false;
1194 }
1195
1196 return true;
1197}
1198
1199bool Context::getIntegerv(GLenum pname, GLint *params)
1200{
1201 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1202 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1203 // GetIntegerv as its native query function. As it would require conversion in any
1204 // case, this should make no difference to the calling application. You may find it in
1205 // Context::getFloatv.
1206 switch (pname)
1207 {
1208 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1209 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1210 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1211 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1212 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1213 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1214 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001215 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001216 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001217 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001218 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1219 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001220 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1221 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1222 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001223 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001224 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1225 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1226 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1227 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001228 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001229 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1230 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1231 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1232 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1233 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1234 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1235 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1236 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1237 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1238 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1239 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1240 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1241 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1242 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1243 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1244 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1245 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1246 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1247 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1248 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1249 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1250 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1251 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1252 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001253 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1254 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001255 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1256 {
1257 if (supportsCompressedTextures())
1258 {
1259 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1260 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1261 *params = 2;
1262 }
1263 else
1264 {
1265 *params = 0;
1266 }
1267 }
1268 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001269 case GL_MAX_SAMPLES_ANGLE:
1270 {
1271 GLsizei maxSamples = getMaxSupportedSamples();
1272 if (maxSamples != 0)
1273 {
1274 *params = maxSamples;
1275 }
1276 else
1277 {
1278 return false;
1279 }
1280
1281 break;
1282 }
1283 case GL_SAMPLE_BUFFERS:
1284 case GL_SAMPLES:
1285 {
1286 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1287 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1288 {
1289 switch (pname)
1290 {
1291 case GL_SAMPLE_BUFFERS:
1292 if (framebuffer->getSamples() != 0)
1293 {
1294 *params = 1;
1295 }
1296 else
1297 {
1298 *params = 0;
1299 }
1300 break;
1301 case GL_SAMPLES:
1302 *params = framebuffer->getSamples();
1303 break;
1304 }
1305 }
1306 else
1307 {
1308 *params = 0;
1309 }
1310 }
1311 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001312 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1313 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1314 case GL_MAX_VIEWPORT_DIMS:
1315 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001316 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001317 params[0] = maxDimension;
1318 params[1] = maxDimension;
1319 }
1320 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001321 case GL_COMPRESSED_TEXTURE_FORMATS:
1322 {
1323 if (supportsCompressedTextures())
1324 {
1325 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1326 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1327 }
1328 }
1329 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001330 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001331 params[0] = mState.viewportX;
1332 params[1] = mState.viewportY;
1333 params[2] = mState.viewportWidth;
1334 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001335 break;
1336 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001337 params[0] = mState.scissorX;
1338 params[1] = mState.scissorY;
1339 params[2] = mState.scissorWidth;
1340 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001341 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001342 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1343 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001344 case GL_RED_BITS:
1345 case GL_GREEN_BITS:
1346 case GL_BLUE_BITS:
1347 case GL_ALPHA_BITS:
1348 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001349 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001350 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1351
1352 if (colorbuffer)
1353 {
1354 switch (pname)
1355 {
1356 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1357 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1358 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1359 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1360 }
1361 }
1362 else
1363 {
1364 *params = 0;
1365 }
1366 }
1367 break;
1368 case GL_DEPTH_BITS:
1369 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001370 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001371 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001372
1373 if (depthbuffer)
1374 {
1375 *params = depthbuffer->getDepthSize();
1376 }
1377 else
1378 {
1379 *params = 0;
1380 }
1381 }
1382 break;
1383 case GL_STENCIL_BITS:
1384 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001385 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001386 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387
1388 if (stencilbuffer)
1389 {
1390 *params = stencilbuffer->getStencilSize();
1391 }
1392 else
1393 {
1394 *params = 0;
1395 }
1396 }
1397 break;
1398 case GL_TEXTURE_BINDING_2D:
1399 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001400 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001401 {
1402 error(GL_INVALID_OPERATION);
1403 return false;
1404 }
1405
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001406 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001407 }
1408 break;
1409 case GL_TEXTURE_BINDING_CUBE_MAP:
1410 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001411 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001412 {
1413 error(GL_INVALID_OPERATION);
1414 return false;
1415 }
1416
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001417 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001418 }
1419 break;
1420 default:
1421 return false;
1422 }
1423
1424 return true;
1425}
1426
1427bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1428{
1429 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1430 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1431 // to the fact that it is stored internally as a float, and so would require conversion
1432 // if returned from Context::getIntegerv. Since this conversion is already implemented
1433 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1434 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1435 // application.
1436 switch (pname)
1437 {
1438 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1439 case GL_SHADER_BINARY_FORMATS:
1440 {
1441 *type = GL_INT;
1442 *numParams = 0;
1443 }
1444 break;
1445 case GL_MAX_VERTEX_ATTRIBS:
1446 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1447 case GL_MAX_VARYING_VECTORS:
1448 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1449 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1450 case GL_MAX_TEXTURE_IMAGE_UNITS:
1451 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1452 case GL_MAX_RENDERBUFFER_SIZE:
1453 case GL_NUM_SHADER_BINARY_FORMATS:
1454 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1455 case GL_ARRAY_BUFFER_BINDING:
1456 case GL_FRAMEBUFFER_BINDING:
1457 case GL_RENDERBUFFER_BINDING:
1458 case GL_CURRENT_PROGRAM:
1459 case GL_PACK_ALIGNMENT:
1460 case GL_UNPACK_ALIGNMENT:
1461 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001462 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001463 case GL_RED_BITS:
1464 case GL_GREEN_BITS:
1465 case GL_BLUE_BITS:
1466 case GL_ALPHA_BITS:
1467 case GL_DEPTH_BITS:
1468 case GL_STENCIL_BITS:
1469 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1470 case GL_CULL_FACE_MODE:
1471 case GL_FRONT_FACE:
1472 case GL_ACTIVE_TEXTURE:
1473 case GL_STENCIL_FUNC:
1474 case GL_STENCIL_VALUE_MASK:
1475 case GL_STENCIL_REF:
1476 case GL_STENCIL_FAIL:
1477 case GL_STENCIL_PASS_DEPTH_FAIL:
1478 case GL_STENCIL_PASS_DEPTH_PASS:
1479 case GL_STENCIL_BACK_FUNC:
1480 case GL_STENCIL_BACK_VALUE_MASK:
1481 case GL_STENCIL_BACK_REF:
1482 case GL_STENCIL_BACK_FAIL:
1483 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1484 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1485 case GL_DEPTH_FUNC:
1486 case GL_BLEND_SRC_RGB:
1487 case GL_BLEND_SRC_ALPHA:
1488 case GL_BLEND_DST_RGB:
1489 case GL_BLEND_DST_ALPHA:
1490 case GL_BLEND_EQUATION_RGB:
1491 case GL_BLEND_EQUATION_ALPHA:
1492 case GL_STENCIL_WRITEMASK:
1493 case GL_STENCIL_BACK_WRITEMASK:
1494 case GL_STENCIL_CLEAR_VALUE:
1495 case GL_SUBPIXEL_BITS:
1496 case GL_MAX_TEXTURE_SIZE:
1497 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1498 case GL_SAMPLE_BUFFERS:
1499 case GL_SAMPLES:
1500 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1501 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1502 case GL_TEXTURE_BINDING_2D:
1503 case GL_TEXTURE_BINDING_CUBE_MAP:
1504 {
1505 *type = GL_INT;
1506 *numParams = 1;
1507 }
1508 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001509 case GL_MAX_SAMPLES_ANGLE:
1510 {
1511 if (getMaxSupportedSamples() != 0)
1512 {
1513 *type = GL_INT;
1514 *numParams = 1;
1515 }
1516 else
1517 {
1518 return false;
1519 }
1520 }
1521 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001522 case GL_MAX_VIEWPORT_DIMS:
1523 {
1524 *type = GL_INT;
1525 *numParams = 2;
1526 }
1527 break;
1528 case GL_VIEWPORT:
1529 case GL_SCISSOR_BOX:
1530 {
1531 *type = GL_INT;
1532 *numParams = 4;
1533 }
1534 break;
1535 case GL_SHADER_COMPILER:
1536 case GL_SAMPLE_COVERAGE_INVERT:
1537 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001538 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1539 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1540 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1541 case GL_SAMPLE_COVERAGE:
1542 case GL_SCISSOR_TEST:
1543 case GL_STENCIL_TEST:
1544 case GL_DEPTH_TEST:
1545 case GL_BLEND:
1546 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001547 {
1548 *type = GL_BOOL;
1549 *numParams = 1;
1550 }
1551 break;
1552 case GL_COLOR_WRITEMASK:
1553 {
1554 *type = GL_BOOL;
1555 *numParams = 4;
1556 }
1557 break;
1558 case GL_POLYGON_OFFSET_FACTOR:
1559 case GL_POLYGON_OFFSET_UNITS:
1560 case GL_SAMPLE_COVERAGE_VALUE:
1561 case GL_DEPTH_CLEAR_VALUE:
1562 case GL_LINE_WIDTH:
1563 {
1564 *type = GL_FLOAT;
1565 *numParams = 1;
1566 }
1567 break;
1568 case GL_ALIASED_LINE_WIDTH_RANGE:
1569 case GL_ALIASED_POINT_SIZE_RANGE:
1570 case GL_DEPTH_RANGE:
1571 {
1572 *type = GL_FLOAT;
1573 *numParams = 2;
1574 }
1575 break;
1576 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001577 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001578 {
1579 *type = GL_FLOAT;
1580 *numParams = 4;
1581 }
1582 break;
1583 default:
1584 return false;
1585 }
1586
1587 return true;
1588}
1589
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590// Applies the render target surface, depth stencil surface, viewport rectangle and
1591// scissor rectangle to the Direct3D 9 device
1592bool Context::applyRenderTarget(bool ignoreViewport)
1593{
1594 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001595
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001596 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
1598 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1599 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001600 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1601
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 return false;
1603 }
1604
1605 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001606
1607 if (!renderTarget)
1608 {
1609 return false; // Context must be lost
1610 }
1611
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001612 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001614 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1615 if (renderTargetSerial != mAppliedRenderTargetSerial)
1616 {
1617 device->SetRenderTarget(0, renderTarget);
1618 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001619 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 +00001620 }
1621
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001622 unsigned int depthbufferSerial = 0;
1623 unsigned int stencilbufferSerial = 0;
1624 if (framebufferObject->getDepthbufferType() != GL_NONE)
1625 {
1626 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001627 if (!depthStencil)
1628 {
1629 ERR("Depth stencil pointer unexpectedly null.");
1630 return false;
1631 }
1632
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001633 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1634 }
1635 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1636 {
1637 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001638 if (!depthStencil)
1639 {
1640 ERR("Depth stencil pointer unexpectedly null.");
1641 return false;
1642 }
1643
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001644 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1645 }
1646
1647 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001648 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001649 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001650 {
1651 device->SetDepthStencilSurface(depthStencil);
1652 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001653 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001654 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001655 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656
1657 D3DVIEWPORT9 viewport;
1658 D3DSURFACE_DESC desc;
1659 renderTarget->GetDesc(&desc);
1660
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001661 float zNear = clamp01(mState.zNear);
1662 float zFar = clamp01(mState.zFar);
1663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 if (ignoreViewport)
1665 {
1666 viewport.X = 0;
1667 viewport.Y = 0;
1668 viewport.Width = desc.Width;
1669 viewport.Height = desc.Height;
1670 viewport.MinZ = 0.0f;
1671 viewport.MaxZ = 1.0f;
1672 }
1673 else
1674 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001675 viewport.X = std::max(mState.viewportX, 0);
1676 viewport.Y = std::max(mState.viewportY, 0);
1677 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1678 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001679 viewport.MinZ = zNear;
1680 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 }
1682
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001683 if (viewport.Width <= 0 || viewport.Height <= 0)
1684 {
1685 return false; // Nothing to render
1686 }
1687
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 device->SetViewport(&viewport);
1689
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001690 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001692 if (mState.scissorTest)
1693 {
1694 RECT rect = {mState.scissorX,
1695 mState.scissorY,
1696 mState.scissorX + mState.scissorWidth,
1697 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001698 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1699 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001700 device->SetScissorRect(&rect);
1701 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1702 }
1703 else
1704 {
1705 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1706 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001707
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001708 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 }
1710
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001711 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713 Program *programObject = getCurrentProgram();
1714
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001715 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001716 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001718
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001719 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001720 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1721 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1722 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001723 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1724
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001725 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001726 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001727 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1728
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001729 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001730 programObject->setUniform1fv(near, 1, &zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001733 programObject->setUniform1fv(far, 1, &zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001734
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001735 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001736 GLfloat zDiff = zFar - zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001737 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
1739
1740 return true;
1741}
1742
1743// 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 +00001744void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745{
1746 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001747 Program *programObject = getCurrentProgram();
1748
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001749 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001750 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001751 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001753 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001754 GLint alwaysFront = !isTriangleMode(drawMode);
1755 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1756
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001757 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001758
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 }
1765 else
1766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768 }
1769
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001770 mCullStateDirty = false;
1771 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001773 if (mDepthStateDirty)
1774 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001775 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001777 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1778 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779 }
1780 else
1781 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001782 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001784
1785 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786 }
1787
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001788 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001790 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001791 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001792 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1793
1794 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1795 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1796 {
1797 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1798 }
1799 else
1800 {
1801 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1802 unorm<8>(mState.blendColor.alpha),
1803 unorm<8>(mState.blendColor.alpha),
1804 unorm<8>(mState.blendColor.alpha)));
1805 }
1806
1807 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1808 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1809 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1810
1811 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1812 mState.destBlendRGB != mState.destBlendAlpha ||
1813 mState.blendEquationRGB != mState.blendEquationAlpha)
1814 {
1815 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1816
1817 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1818 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1819 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1820
1821 }
1822 else
1823 {
1824 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1825 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001826 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001827 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001828 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001829 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001830 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001831
1832 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 }
1834
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001835 if (mStencilStateDirty || mFrontFaceDirty)
1836 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001837 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 {
1839 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1840 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1841
1842 // FIXME: Unsupported by D3D9
1843 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1844 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1845 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1846 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1847 mState.stencilRef != mState.stencilBackRef ||
1848 mState.stencilMask != mState.stencilBackMask)
1849 {
1850 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1851 return error(GL_INVALID_OPERATION);
1852 }
1853
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001854 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001855 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001856 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1857
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001858 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1859 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1860 es2dx::ConvertComparison(mState.stencilFunc));
1861
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001862 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1864
1865 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1866 es2dx::ConvertStencilOp(mState.stencilFail));
1867 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1868 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1869 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1870 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1871
1872 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1873 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1874 es2dx::ConvertComparison(mState.stencilBackFunc));
1875
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001876 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1878
1879 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1880 es2dx::ConvertStencilOp(mState.stencilBackFail));
1881 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1882 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1883 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1884 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1885 }
1886 else
1887 {
1888 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1889 }
1890
1891 mStencilStateDirty = false;
1892 }
1893
1894 if (mMaskStateDirty)
1895 {
1896 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1897 mState.colorMaskBlue, mState.colorMaskAlpha));
1898 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1899
1900 mMaskStateDirty = false;
1901 }
1902
1903 if (mPolygonOffsetStateDirty)
1904 {
1905 if (mState.polygonOffsetFill)
1906 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001907 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 if (depthbuffer)
1909 {
1910 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1911 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1912 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1913 }
1914 }
1915 else
1916 {
1917 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1918 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1919 }
1920
1921 mPolygonOffsetStateDirty = false;
1922 }
1923
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001924 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001926 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001927 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001928 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930 FIXME("Sample alpha to coverage is unimplemented.");
1931 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001932
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001933 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1934 if (mState.sampleCoverage)
1935 {
1936 unsigned int mask = 0;
1937 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001938 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001939 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001940
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001941 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001942 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001943 mask <<= 1;
1944
1945 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1946 {
1947 threshold += 1.0f;
1948 mask |= 1;
1949 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001950 }
1951 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001952
1953 if (mState.sampleCoverageInvert)
1954 {
1955 mask = ~mask;
1956 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001957
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001958 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1959 }
1960 else
1961 {
1962 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1963 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001964 }
1965 else
1966 {
1967 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001968 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001969
1970 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971 }
1972
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001973 if (mDitherStateDirty)
1974 {
1975 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1976
1977 mDitherStateDirty = false;
1978 }
1979
1980 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981}
1982
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001983// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001984void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001986 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001988 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001990 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 }
1992 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001993}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001995GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001996{
1997 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1998
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001999 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002000 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002001 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002002 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002003 }
2004
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002005 lookupAttributeMapping(translated);
2006
2007 mBufferBackEnd->setupAttributesPreDraw(translated);
2008
2009 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2010 {
2011 if (translated[i].enabled && translated[i].nonArray)
2012 {
2013 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
2014 if (err != GL_NO_ERROR)
2015 {
2016 return err;
2017 }
2018
2019 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2020
2021 *useIndexing = true;
2022 return GL_NO_ERROR;
2023 }
2024 }
2025
2026 *useIndexing = false;
2027 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002028}
2029
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002030GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002031{
2032 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2033
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002034 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002035
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002036 if (err == GL_NO_ERROR)
2037 {
2038 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002039
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002040 mBufferBackEnd->setupAttributesPreDraw(translated);
2041 }
2042
2043 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002044}
2045
2046// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002047GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002048{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002049 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002050
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002051 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002052 {
2053 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2054 }
2055
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002056 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057}
2058
2059// Applies the shaders and shader constants to the Direct3D 9 device
2060void Context::applyShaders()
2061{
2062 IDirect3DDevice9 *device = getDevice();
2063 Program *programObject = getCurrentProgram();
2064 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2065 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2066
2067 device->SetVertexShader(vertexShader);
2068 device->SetPixelShader(pixelShader);
2069
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002070 if (programObject->getSerial() != mAppliedProgram)
2071 {
2072 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002073 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002074 mAppliedProgram = programObject->getSerial();
2075 }
2076
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 programObject->applyUniforms();
2078}
2079
2080// Applies the textures and sampler states to the Direct3D 9 device
2081void Context::applyTextures()
2082{
2083 IDirect3DDevice9 *device = getDevice();
2084 Program *programObject = getCurrentProgram();
2085
2086 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2087 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002088 int textureUnit = programObject->getSamplerMapping(sampler);
2089 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002091 SamplerType textureType = programObject->getSamplerType(sampler);
2092
2093 Texture *texture = getSamplerTexture(textureUnit, textureType);
2094
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002095 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002096 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002097 if (texture->isComplete())
2098 {
2099 GLenum wrapS = texture->getWrapS();
2100 GLenum wrapT = texture->getWrapT();
2101 GLenum minFilter = texture->getMinFilter();
2102 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002104 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2105 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002107 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2108 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2109 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2110 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2111 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002113 device->SetTexture(sampler, texture->getTexture());
2114 }
2115 else
2116 {
2117 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2118 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002119 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002120
2121 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002123 else
2124 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002125 if (programObject->isSamplerDirty(sampler))
2126 {
2127 device->SetTexture(sampler, NULL);
2128 programObject->setSamplerDirty(sampler, false);
2129 }
2130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 }
2132}
2133
2134void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2135{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002136 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002137
2138 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2139 {
2140 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2141 }
2142
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002143 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2144 {
2145 return error(GL_INVALID_OPERATION);
2146 }
2147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002149
2150 if (!renderTarget)
2151 {
2152 return; // Context must be lost, return silently
2153 }
2154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 IDirect3DDevice9 *device = getDevice();
2156
2157 D3DSURFACE_DESC desc;
2158 renderTarget->GetDesc(&desc);
2159
2160 IDirect3DSurface9 *systemSurface;
2161 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2162
2163 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2164 {
2165 return error(GL_OUT_OF_MEMORY);
2166 }
2167
2168 ASSERT(SUCCEEDED(result));
2169
2170 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2171 {
2172 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2173 }
2174
2175 result = device->GetRenderTargetData(renderTarget, systemSurface);
2176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 if (FAILED(result))
2178 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 systemSurface->Release();
2180
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002181 switch (result)
2182 {
2183 case D3DERR_DRIVERINTERNALERROR:
2184 case D3DERR_DEVICELOST:
2185 return error(GL_OUT_OF_MEMORY);
2186 default:
2187 UNREACHABLE();
2188 return; // No sensible error to generate
2189 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 }
2191
2192 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002193 RECT rect = {std::max(x, 0),
2194 std::max(y, 0),
2195 std::min(x + width, (int)desc.Width),
2196 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197
2198 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2199
2200 if (FAILED(result))
2201 {
2202 UNREACHABLE();
2203 systemSurface->Release();
2204
2205 return; // No sensible error to generate
2206 }
2207
2208 unsigned char *source = (unsigned char*)lock.pBits;
2209 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002210 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002212 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002213
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 for (int j = 0; j < rect.bottom - rect.top; j++)
2215 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002216 if (desc.Format == D3DFMT_A8R8G8B8 &&
2217 format == GL_BGRA_EXT &&
2218 type == GL_UNSIGNED_BYTE)
2219 {
2220 // Fast path for EXT_read_format_bgra, given
2221 // an RGBA source buffer. Note that buffers with no
2222 // alpha go through the slow path below.
2223 memcpy(dest + j * outputPitch,
2224 source + j * lock.Pitch,
2225 (rect.right - rect.left) * 4);
2226 continue;
2227 }
2228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229 for (int i = 0; i < rect.right - rect.left; i++)
2230 {
2231 float r;
2232 float g;
2233 float b;
2234 float a;
2235
2236 switch (desc.Format)
2237 {
2238 case D3DFMT_R5G6B5:
2239 {
2240 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2241
2242 a = 1.0f;
2243 b = (rgb & 0x001F) * (1.0f / 0x001F);
2244 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2245 r = (rgb & 0xF800) * (1.0f / 0xF800);
2246 }
2247 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 case D3DFMT_A1R5G5B5:
2249 {
2250 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2251
2252 a = (argb & 0x8000) ? 1.0f : 0.0f;
2253 b = (argb & 0x001F) * (1.0f / 0x001F);
2254 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2255 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2256 }
2257 break;
2258 case D3DFMT_A8R8G8B8:
2259 {
2260 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2261
2262 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2263 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2264 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2265 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2266 }
2267 break;
2268 case D3DFMT_X8R8G8B8:
2269 {
2270 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2271
2272 a = 1.0f;
2273 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2274 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2275 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2276 }
2277 break;
2278 case D3DFMT_A2R10G10B10:
2279 {
2280 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2281
2282 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2283 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2284 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2285 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2286 }
2287 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002288 case D3DFMT_A32B32G32R32F:
2289 {
2290 // float formats in D3D are stored rgba, rather than the other way round
2291 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2292 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2293 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2294 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2295 }
2296 break;
2297 case D3DFMT_A16B16G16R16F:
2298 {
2299 // float formats in D3D are stored rgba, rather than the other way round
2300 float abgr[4];
2301
2302 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2303
2304 a = abgr[3];
2305 b = abgr[2];
2306 g = abgr[1];
2307 r = abgr[0];
2308 }
2309 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 default:
2311 UNIMPLEMENTED(); // FIXME
2312 UNREACHABLE();
2313 }
2314
2315 switch (format)
2316 {
2317 case GL_RGBA:
2318 switch (type)
2319 {
2320 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002321 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2322 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2323 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2324 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 break;
2326 default: UNREACHABLE();
2327 }
2328 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002329 case GL_BGRA_EXT:
2330 switch (type)
2331 {
2332 case GL_UNSIGNED_BYTE:
2333 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2334 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2335 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2336 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2337 break;
2338 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2339 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2340 // this type is packed as follows:
2341 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2342 // --------------------------------------------------------------------------------
2343 // | 4th | 3rd | 2nd | 1st component |
2344 // --------------------------------------------------------------------------------
2345 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2346 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2347 ((unsigned short)(15 * a + 0.5f) << 12)|
2348 ((unsigned short)(15 * r + 0.5f) << 8) |
2349 ((unsigned short)(15 * g + 0.5f) << 4) |
2350 ((unsigned short)(15 * b + 0.5f) << 0);
2351 break;
2352 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2353 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2354 // this type is packed as follows:
2355 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2356 // --------------------------------------------------------------------------------
2357 // | 4th | 3rd | 2nd | 1st component |
2358 // --------------------------------------------------------------------------------
2359 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2360 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2361 ((unsigned short)( a + 0.5f) << 15) |
2362 ((unsigned short)(31 * r + 0.5f) << 10) |
2363 ((unsigned short)(31 * g + 0.5f) << 5) |
2364 ((unsigned short)(31 * b + 0.5f) << 0);
2365 break;
2366 default: UNREACHABLE();
2367 }
2368 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002369 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 switch (type)
2371 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002372 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002373 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2374 ((unsigned short)(31 * b + 0.5f) << 0) |
2375 ((unsigned short)(63 * g + 0.5f) << 5) |
2376 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 break;
2378 default: UNREACHABLE();
2379 }
2380 break;
2381 default: UNREACHABLE();
2382 }
2383 }
2384 }
2385
2386 systemSurface->UnlockRect();
2387
2388 systemSurface->Release();
2389}
2390
2391void Context::clear(GLbitfield mask)
2392{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002393 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002394
2395 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2396 {
2397 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2398
2399 return;
2400 }
2401
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002402 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 IDirect3DDevice9 *device = getDevice();
2404 DWORD flags = 0;
2405
2406 if (mask & GL_COLOR_BUFFER_BIT)
2407 {
2408 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002409
2410 if (framebufferObject->getColorbufferType() != GL_NONE)
2411 {
2412 flags |= D3DCLEAR_TARGET;
2413 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 }
2415
2416 if (mask & GL_DEPTH_BUFFER_BIT)
2417 {
2418 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002419 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 {
2421 flags |= D3DCLEAR_ZBUFFER;
2422 }
2423 }
2424
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 GLuint stencilUnmasked = 0x0;
2426
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002427 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002430 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002432 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002433 if (!depthStencil)
2434 {
2435 ERR("Depth stencil pointer unexpectedly null.");
2436 return;
2437 }
2438
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002439 D3DSURFACE_DESC desc;
2440 depthStencil->GetDesc(&desc);
2441
2442 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2443 stencilUnmasked = (0x1 << stencilSize) - 1;
2444
2445 if (stencilUnmasked != 0x0)
2446 {
2447 flags |= D3DCLEAR_STENCIL;
2448 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 }
2450 }
2451
2452 if (mask != 0)
2453 {
2454 return error(GL_INVALID_VALUE);
2455 }
2456
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002457 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2458 {
2459 return;
2460 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002462 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2463 unorm<8>(mState.colorClearValue.red),
2464 unorm<8>(mState.colorClearValue.green),
2465 unorm<8>(mState.colorClearValue.blue));
2466 float depth = clamp01(mState.depthClearValue);
2467 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468
2469 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2470
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002471 if (!renderTarget)
2472 {
2473 return; // Context must be lost, return silently
2474 }
2475
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 D3DSURFACE_DESC desc;
2477 renderTarget->GetDesc(&desc);
2478
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002479 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480
2481 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002482 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002484 !(mState.colorMaskRed && mState.colorMaskGreen &&
2485 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486
2487 if (needMaskedColorClear || needMaskedStencilClear)
2488 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002489 // State which is altered in all paths from this point to the clear call is saved.
2490 // State which is altered in only some paths will be flagged dirty in the case that
2491 // that path is taken.
2492 HRESULT hr;
2493 if (mMaskedClearSavedState == NULL)
2494 {
2495 hr = device->BeginStateBlock();
2496 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2497
2498 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2499 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2500 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2501 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2502 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2503 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2504 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2505 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2506 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2507 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2508 device->SetPixelShader(NULL);
2509 device->SetVertexShader(NULL);
2510 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2511 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002512 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002513
2514 hr = device->EndStateBlock(&mMaskedClearSavedState);
2515 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2516 }
2517
2518 ASSERT(mMaskedClearSavedState != NULL);
2519
2520 if (mMaskedClearSavedState != NULL)
2521 {
2522 hr = mMaskedClearSavedState->Capture();
2523 ASSERT(SUCCEEDED(hr));
2524 }
2525
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2527 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2528 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2529 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2530 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2531 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2532 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2533 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2534
2535 if (flags & D3DCLEAR_TARGET)
2536 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002537 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2538 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2539 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2540 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 }
2542 else
2543 {
2544 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2545 }
2546
2547 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2548 {
2549 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2550 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2551 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2552 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002553 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002554 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2556 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002557 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 }
2559 else
2560 {
2561 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2562 }
2563
2564 device->SetPixelShader(NULL);
2565 device->SetVertexShader(NULL);
2566 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002567 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002568 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002570 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 {
2572 float x, y, z, w;
2573 D3DCOLOR diffuse;
2574 };
2575
2576 Vertex quad[4];
2577 quad[0].x = 0.0f;
2578 quad[0].y = (float)desc.Height;
2579 quad[0].z = 0.0f;
2580 quad[0].w = 1.0f;
2581 quad[0].diffuse = color;
2582
2583 quad[1].x = (float)desc.Width;
2584 quad[1].y = (float)desc.Height;
2585 quad[1].z = 0.0f;
2586 quad[1].w = 1.0f;
2587 quad[1].diffuse = color;
2588
2589 quad[2].x = 0.0f;
2590 quad[2].y = 0.0f;
2591 quad[2].z = 0.0f;
2592 quad[2].w = 1.0f;
2593 quad[2].diffuse = color;
2594
2595 quad[3].x = (float)desc.Width;
2596 quad[3].y = 0.0f;
2597 quad[3].z = 0.0f;
2598 quad[3].w = 1.0f;
2599 quad[3].diffuse = color;
2600
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002601 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603
2604 if (flags & D3DCLEAR_ZBUFFER)
2605 {
2606 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2607 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2608 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2609 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002610
2611 if (mMaskedClearSavedState != NULL)
2612 {
2613 mMaskedClearSavedState->Apply();
2614 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002616 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 {
2618 device->Clear(0, NULL, flags, color, depth, stencil);
2619 }
2620}
2621
2622void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2623{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002624 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625 {
2626 return error(GL_INVALID_OPERATION);
2627 }
2628
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002629 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630 IDirect3DDevice9 *device = getDevice();
2631 D3DPRIMITIVETYPE primitiveType;
2632 int primitiveCount;
2633
2634 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2635 return error(GL_INVALID_ENUM);
2636
2637 if (primitiveCount <= 0)
2638 {
2639 return;
2640 }
2641
2642 if (!applyRenderTarget(false))
2643 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002644 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002645 }
2646
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002647 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002648
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002649 TranslatedIndexData indexInfo;
2650 bool useIndexing;
2651 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
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.com81655a72010-05-20 19:18:17 +00002668 if (useIndexing)
2669 {
2670 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2671 }
2672 else
2673 {
2674 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2675 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002676 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677}
2678
2679void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2680{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002681 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682 {
2683 return error(GL_INVALID_OPERATION);
2684 }
2685
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002686 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 {
2688 return error(GL_INVALID_OPERATION);
2689 }
2690
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002691 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 IDirect3DDevice9 *device = getDevice();
2693 D3DPRIMITIVETYPE primitiveType;
2694 int primitiveCount;
2695
2696 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2697 return error(GL_INVALID_ENUM);
2698
2699 if (primitiveCount <= 0)
2700 {
2701 return;
2702 }
2703
2704 if (!applyRenderTarget(false))
2705 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002706 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 }
2708
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002709 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002710
2711 TranslatedIndexData indexInfo;
2712 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2713 if (err != GL_NO_ERROR)
2714 {
2715 return error(err);
2716 }
2717
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002718 err = applyVertexBuffer(indexInfo);
2719 if (err != GL_NO_ERROR)
2720 {
2721 return error(err);
2722 }
2723
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 applyShaders();
2725 applyTextures();
2726
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002727 if (!getCurrentProgram()->validateSamplers())
2728 {
2729 return error(GL_INVALID_OPERATION);
2730 }
2731
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002732 if (!cullSkipsDraw(mode))
2733 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002734 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002735 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737}
2738
2739void Context::finish()
2740{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002741 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 IDirect3DDevice9 *device = getDevice();
2743 IDirect3DQuery9 *occlusionQuery = NULL;
2744
2745 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2746
2747 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2748 {
2749 return error(GL_OUT_OF_MEMORY);
2750 }
2751
2752 ASSERT(SUCCEEDED(result));
2753
2754 if (occlusionQuery)
2755 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002756 IDirect3DStateBlock9 *savedState = NULL;
2757 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2758
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002759 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2760 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761
2762 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002763 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 device->SetPixelShader(NULL);
2765 device->SetVertexShader(NULL);
2766 device->SetFVF(D3DFVF_XYZRHW);
2767 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002768 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002771 result = occlusionQuery->Issue(D3DISSUE_END);
2772 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773
2774 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2775 {
2776 // Keep polling, but allow other threads to do something useful first
2777 Sleep(0);
2778 }
2779
2780 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002781
2782 if (savedState)
2783 {
2784 savedState->Apply();
2785 savedState->Release();
2786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787 }
2788}
2789
2790void Context::flush()
2791{
2792 IDirect3DDevice9 *device = getDevice();
2793 IDirect3DQuery9 *eventQuery = NULL;
2794
2795 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2796
2797 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2798 {
2799 return error(GL_OUT_OF_MEMORY);
2800 }
2801
2802 ASSERT(SUCCEEDED(result));
2803
2804 if (eventQuery)
2805 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002806 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2807 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002809 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002811
2812 if (result == D3DERR_DEVICELOST)
2813 {
2814 error(GL_OUT_OF_MEMORY);
2815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 }
2817}
2818
2819void Context::recordInvalidEnum()
2820{
2821 mInvalidEnum = true;
2822}
2823
2824void Context::recordInvalidValue()
2825{
2826 mInvalidValue = true;
2827}
2828
2829void Context::recordInvalidOperation()
2830{
2831 mInvalidOperation = true;
2832}
2833
2834void Context::recordOutOfMemory()
2835{
2836 mOutOfMemory = true;
2837}
2838
2839void Context::recordInvalidFramebufferOperation()
2840{
2841 mInvalidFramebufferOperation = true;
2842}
2843
2844// Get one of the recorded errors and clear its flag, if any.
2845// [OpenGL ES 2.0.24] section 2.5 page 13.
2846GLenum Context::getError()
2847{
2848 if (mInvalidEnum)
2849 {
2850 mInvalidEnum = false;
2851
2852 return GL_INVALID_ENUM;
2853 }
2854
2855 if (mInvalidValue)
2856 {
2857 mInvalidValue = false;
2858
2859 return GL_INVALID_VALUE;
2860 }
2861
2862 if (mInvalidOperation)
2863 {
2864 mInvalidOperation = false;
2865
2866 return GL_INVALID_OPERATION;
2867 }
2868
2869 if (mOutOfMemory)
2870 {
2871 mOutOfMemory = false;
2872
2873 return GL_OUT_OF_MEMORY;
2874 }
2875
2876 if (mInvalidFramebufferOperation)
2877 {
2878 mInvalidFramebufferOperation = false;
2879
2880 return GL_INVALID_FRAMEBUFFER_OPERATION;
2881 }
2882
2883 return GL_NO_ERROR;
2884}
2885
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002886bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002887{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002888 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002889}
2890
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002891int Context::getMaxSupportedSamples() const
2892{
2893 return mMaxSupportedSamples;
2894}
2895
2896int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2897{
2898 if (requested == 0)
2899 {
2900 return requested;
2901 }
2902
2903 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2904 if (itr == mMultiSampleSupport.end())
2905 {
2906 return -1;
2907 }
2908
2909 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2910 {
2911 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2912 {
2913 return i;
2914 }
2915 }
2916
2917 return -1;
2918}
2919
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002920bool Context::supportsEventQueries() const
2921{
2922 return mSupportsEventQueries;
2923}
2924
daniel@transgaming.com01868132010-08-24 19:21:17 +00002925bool Context::supportsCompressedTextures() const
2926{
2927 return mSupportsCompressedTextures;
2928}
2929
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002930bool Context::supportsFloatTextures() const
2931{
2932 return mSupportsFloatTextures;
2933}
2934
2935bool Context::supportsFloatLinearFilter() const
2936{
2937 return mSupportsFloatLinearFilter;
2938}
2939
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002940bool Context::supportsFloatRenderableTextures() const
2941{
2942 return mSupportsFloatRenderableTextures;
2943}
2944
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002945bool Context::supportsHalfFloatTextures() const
2946{
2947 return mSupportsHalfFloatTextures;
2948}
2949
2950bool Context::supportsHalfFloatLinearFilter() const
2951{
2952 return mSupportsHalfFloatLinearFilter;
2953}
2954
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002955bool Context::supportsHalfFloatRenderableTextures() const
2956{
2957 return mSupportsHalfFloatRenderableTextures;
2958}
2959
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002960int Context::getMaximumRenderbufferDimension() const
2961{
2962 return mMaxRenderbufferDimension;
2963}
2964
2965int Context::getMaximumTextureDimension() const
2966{
2967 return mMaxTextureDimension;
2968}
2969
2970int Context::getMaximumCubeTextureDimension() const
2971{
2972 return mMaxCubeTextureDimension;
2973}
2974
2975int Context::getMaximumTextureLevel() const
2976{
2977 return mMaxTextureLevel;
2978}
2979
daniel@transgaming.comed828e52010-10-15 17:57:30 +00002980bool Context::supportsLuminanceTextures() const
2981{
2982 return mSupportsLuminanceTextures;
2983}
2984
2985bool Context::supportsLuminanceAlphaTextures() const
2986{
2987 return mSupportsLuminanceAlphaTextures;
2988}
2989
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002990void Context::detachBuffer(GLuint buffer)
2991{
2992 // [OpenGL ES 2.0.24] section 2.9 page 22:
2993 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2994 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2995
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002996 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002998 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002999 }
3000
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003001 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003002 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003003 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 }
3005
3006 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3007 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003008 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003009 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003010 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011 }
3012 }
3013}
3014
3015void Context::detachTexture(GLuint texture)
3016{
3017 // [OpenGL ES 2.0.24] section 3.8 page 84:
3018 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3019 // rebound to texture object zero
3020
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003021 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003022 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003023 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003024 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003025 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003026 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003027 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 }
3030 }
3031
3032 // [OpenGL ES 2.0.24] section 4.4 page 112:
3033 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3034 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3035 // image was attached in the currently bound framebuffer.
3036
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003037 Framebuffer *readFramebuffer = getReadFramebuffer();
3038 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003040 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003041 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003042 readFramebuffer->detachTexture(texture);
3043 }
3044
3045 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3046 {
3047 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 }
3049}
3050
3051void Context::detachFramebuffer(GLuint framebuffer)
3052{
3053 // [OpenGL ES 2.0.24] section 4.4 page 107:
3054 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3055 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3056
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003057 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003058 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003059 bindReadFramebuffer(0);
3060 }
3061
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003062 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003063 {
3064 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065 }
3066}
3067
3068void Context::detachRenderbuffer(GLuint renderbuffer)
3069{
3070 // [OpenGL ES 2.0.24] section 4.4 page 109:
3071 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3072 // had been executed with the target RENDERBUFFER and name of zero.
3073
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003074 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003075 {
3076 bindRenderbuffer(0);
3077 }
3078
3079 // [OpenGL ES 2.0.24] section 4.4 page 111:
3080 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3081 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3082 // point to which this image was attached in the currently bound framebuffer.
3083
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003084 Framebuffer *readFramebuffer = getReadFramebuffer();
3085 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003086
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003087 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003088 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003089 readFramebuffer->detachRenderbuffer(renderbuffer);
3090 }
3091
3092 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3093 {
3094 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003095 }
3096}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003097
3098Texture *Context::getIncompleteTexture(SamplerType type)
3099{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003100 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003101
3102 if (t == NULL)
3103 {
3104 static const GLubyte color[] = { 0, 0, 0, 255 };
3105
3106 switch (type)
3107 {
3108 default:
3109 UNREACHABLE();
3110 // default falls through to SAMPLER_2D
3111
3112 case SAMPLER_2D:
3113 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003114 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003115 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003116 t = incomplete2d;
3117 }
3118 break;
3119
3120 case SAMPLER_CUBE:
3121 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003122 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003123
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003124 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3125 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3126 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3127 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3128 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3129 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003130
3131 t = incompleteCube;
3132 }
3133 break;
3134 }
3135
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003136 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003137 }
3138
3139 return t;
3140}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003141
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003142bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003143{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003144 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003145}
3146
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003147bool Context::isTriangleMode(GLenum drawMode)
3148{
3149 switch (drawMode)
3150 {
3151 case GL_TRIANGLES:
3152 case GL_TRIANGLE_FAN:
3153 case GL_TRIANGLE_STRIP:
3154 return true;
3155 case GL_POINTS:
3156 case GL_LINES:
3157 case GL_LINE_LOOP:
3158 case GL_LINE_STRIP:
3159 return false;
3160 default: UNREACHABLE();
3161 }
3162
3163 return false;
3164}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003165
3166void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3167{
3168 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3169
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003170 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3171 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3172 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3173 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003174
3175 mVertexDataManager->dirtyCurrentValues();
3176}
3177
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003178void Context::initExtensionString()
3179{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003180 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003181 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3182 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003183 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003184 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003185 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003186
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003187 if (supportsEventQueries())
3188 {
3189 mExtensionString += "GL_NV_fence ";
3190 }
3191
daniel@transgaming.com01868132010-08-24 19:21:17 +00003192 if (supportsCompressedTextures())
3193 {
3194 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3195 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003196
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003197 if (supportsFloatTextures())
3198 {
3199 mExtensionString += "GL_OES_texture_float ";
3200 }
3201
3202 if (supportsHalfFloatTextures())
3203 {
3204 mExtensionString += "GL_OES_texture_half_float ";
3205 }
3206
3207 if (supportsFloatLinearFilter())
3208 {
3209 mExtensionString += "GL_OES_texture_float_linear ";
3210 }
3211
3212 if (supportsHalfFloatLinearFilter())
3213 {
3214 mExtensionString += "GL_OES_texture_half_float_linear ";
3215 }
3216
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003217 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003218 {
3219 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3220 }
3221
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003222 if (mBufferBackEnd->supportIntIndices())
3223 {
3224 mExtensionString += "GL_OES_element_index_uint ";
3225 }
3226
3227 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3228 if (end != std::string::npos)
3229 {
3230 mExtensionString.resize(end+1);
3231 }
3232}
3233
3234const char *Context::getExtensionString() const
3235{
3236 return mExtensionString.c_str();
3237}
3238
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003239void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3240 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3241 GLbitfield mask)
3242{
3243 IDirect3DDevice9 *device = getDevice();
3244
3245 Framebuffer *readFramebuffer = getReadFramebuffer();
3246 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3247
3248 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3249 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3250 {
3251 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3252 }
3253
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003254 if (drawFramebuffer->getSamples() != 0)
3255 {
3256 return error(GL_INVALID_OPERATION);
3257 }
3258
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003259 RECT sourceRect;
3260 RECT destRect;
3261
3262 if (srcX0 < srcX1)
3263 {
3264 sourceRect.left = srcX0;
3265 sourceRect.right = srcX1;
3266 destRect.left = dstX0;
3267 destRect.right = dstX1;
3268 }
3269 else
3270 {
3271 sourceRect.left = srcX1;
3272 destRect.left = dstX1;
3273 sourceRect.right = srcX0;
3274 destRect.right = dstX0;
3275 }
3276
3277 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3278 // flip our Y-values here
3279 if (srcY0 < srcY1)
3280 {
3281 sourceRect.bottom = srcY1;
3282 destRect.bottom = dstY1;
3283 sourceRect.top = srcY0;
3284 destRect.top = dstY0;
3285 }
3286 else
3287 {
3288 sourceRect.bottom = srcY0;
3289 destRect.bottom = dstY0;
3290 sourceRect.top = srcY1;
3291 destRect.top = dstY1;
3292 }
3293
3294 RECT sourceScissoredRect = sourceRect;
3295 RECT destScissoredRect = destRect;
3296
3297 if (mState.scissorTest)
3298 {
3299 // Only write to parts of the destination framebuffer which pass the scissor test
3300 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3301 // rect will be checked against scissorY, rather than the bottom.
3302 if (destRect.left < mState.scissorX)
3303 {
3304 int xDiff = mState.scissorX - destRect.left;
3305 destScissoredRect.left = mState.scissorX;
3306 sourceScissoredRect.left += xDiff;
3307 }
3308
3309 if (destRect.right > mState.scissorX + mState.scissorWidth)
3310 {
3311 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3312 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3313 sourceScissoredRect.right -= xDiff;
3314 }
3315
3316 if (destRect.top < mState.scissorY)
3317 {
3318 int yDiff = mState.scissorY - destRect.top;
3319 destScissoredRect.top = mState.scissorY;
3320 sourceScissoredRect.top += yDiff;
3321 }
3322
3323 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3324 {
3325 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3326 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3327 sourceScissoredRect.bottom -= yDiff;
3328 }
3329 }
3330
3331 bool blitRenderTarget = false;
3332 bool blitDepthStencil = false;
3333
3334 RECT sourceTrimmedRect = sourceScissoredRect;
3335 RECT destTrimmedRect = destScissoredRect;
3336
3337 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3338 // the actual draw and read surfaces.
3339 if (sourceTrimmedRect.left < 0)
3340 {
3341 int xDiff = 0 - sourceTrimmedRect.left;
3342 sourceTrimmedRect.left = 0;
3343 destTrimmedRect.left += xDiff;
3344 }
3345
3346 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3347 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3348 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3349 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3350
3351 if (sourceTrimmedRect.right > readBufferWidth)
3352 {
3353 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3354 sourceTrimmedRect.right = readBufferWidth;
3355 destTrimmedRect.right -= xDiff;
3356 }
3357
3358 if (sourceTrimmedRect.top < 0)
3359 {
3360 int yDiff = 0 - sourceTrimmedRect.top;
3361 sourceTrimmedRect.top = 0;
3362 destTrimmedRect.top += yDiff;
3363 }
3364
3365 if (sourceTrimmedRect.bottom > readBufferHeight)
3366 {
3367 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3368 sourceTrimmedRect.bottom = readBufferHeight;
3369 destTrimmedRect.bottom -= yDiff;
3370 }
3371
3372 if (destTrimmedRect.left < 0)
3373 {
3374 int xDiff = 0 - destTrimmedRect.left;
3375 destTrimmedRect.left = 0;
3376 sourceTrimmedRect.left += xDiff;
3377 }
3378
3379 if (destTrimmedRect.right > drawBufferWidth)
3380 {
3381 int xDiff = destTrimmedRect.right - drawBufferWidth;
3382 destTrimmedRect.right = drawBufferWidth;
3383 sourceTrimmedRect.right -= xDiff;
3384 }
3385
3386 if (destTrimmedRect.top < 0)
3387 {
3388 int yDiff = 0 - destTrimmedRect.top;
3389 destTrimmedRect.top = 0;
3390 sourceTrimmedRect.top += yDiff;
3391 }
3392
3393 if (destTrimmedRect.bottom > drawBufferHeight)
3394 {
3395 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3396 destTrimmedRect.bottom = drawBufferHeight;
3397 sourceTrimmedRect.bottom -= yDiff;
3398 }
3399
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003400 bool partialBufferCopy = false;
3401 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3402 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3403 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3404 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3405 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3406 {
3407 partialBufferCopy = true;
3408 }
3409
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003410 if (mask & GL_COLOR_BUFFER_BIT)
3411 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003412 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3413 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3414 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3415 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3416 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003417 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3418 {
3419 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3420 return error(GL_INVALID_OPERATION);
3421 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003422
3423 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3424 {
3425 return error(GL_INVALID_OPERATION);
3426 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003427
3428 blitRenderTarget = true;
3429
3430 }
3431
3432 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3433 {
3434 DepthStencilbuffer *readDSBuffer = NULL;
3435 DepthStencilbuffer *drawDSBuffer = NULL;
3436
3437 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3438 // both a depth and stencil buffer, it will be the same buffer.
3439
3440 if (mask & GL_DEPTH_BUFFER_BIT)
3441 {
3442 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3443 {
3444 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3445 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3446 {
3447 return error(GL_INVALID_OPERATION);
3448 }
3449
3450 blitDepthStencil = true;
3451 readDSBuffer = readFramebuffer->getDepthbuffer();
3452 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3453 }
3454 }
3455
3456 if (mask & GL_STENCIL_BUFFER_BIT)
3457 {
3458 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3459 {
3460 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3461 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3462 {
3463 return error(GL_INVALID_OPERATION);
3464 }
3465
3466 blitDepthStencil = true;
3467 readDSBuffer = readFramebuffer->getStencilbuffer();
3468 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3469 }
3470 }
3471
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003472 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003473 {
3474 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3475 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3476 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003477
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003478 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3479 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003480 {
3481 return error(GL_INVALID_OPERATION);
3482 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003483 }
3484
3485 if (blitRenderTarget || blitDepthStencil)
3486 {
3487 egl::Display *display = getDisplay();
3488 display->endScene();
3489
3490 if (blitRenderTarget)
3491 {
3492 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3493 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3494
3495 if (FAILED(result))
3496 {
3497 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3498 return;
3499 }
3500 }
3501
3502 if (blitDepthStencil)
3503 {
3504 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3505
3506 if (FAILED(result))
3507 {
3508 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3509 return;
3510 }
3511 }
3512 }
3513}
3514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003515}
3516
3517extern "C"
3518{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003519gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003520{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003521 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003522}
3523
3524void glDestroyContext(gl::Context *context)
3525{
3526 delete context;
3527
3528 if (context == gl::getContext())
3529 {
3530 gl::makeCurrent(NULL, NULL, NULL);
3531 }
3532}
3533
3534void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3535{
3536 gl::makeCurrent(context, display, surface);
3537}
3538
3539gl::Context *glGetCurrentContext()
3540{
3541 return gl::getContext();
3542}
3543}