blob: c1b53c8a66136f6fe38faea7d3aa82a97e39a806 [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.com428d1582010-05-04 03:35:25 +00001139 case GL_CULL_FACE: *params = mState.cullFace;
1140 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1141 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1142 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1143 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1144 case GL_STENCIL_TEST: *params = mState.stencilTest;
1145 case GL_DEPTH_TEST: *params = mState.depthTest;
1146 case GL_BLEND: *params = mState.blend;
1147 case GL_DITHER: *params = mState.dither;
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.coma27ff1e2010-08-24 19:20:11 +00001220 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001221 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
1661 if (ignoreViewport)
1662 {
1663 viewport.X = 0;
1664 viewport.Y = 0;
1665 viewport.Width = desc.Width;
1666 viewport.Height = desc.Height;
1667 viewport.MinZ = 0.0f;
1668 viewport.MaxZ = 1.0f;
1669 }
1670 else
1671 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001672 viewport.X = std::max(mState.viewportX, 0);
1673 viewport.Y = std::max(mState.viewportY, 0);
1674 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1675 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1676 viewport.MinZ = clamp01(mState.zNear);
1677 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678 }
1679
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001680 if (viewport.Width <= 0 || viewport.Height <= 0)
1681 {
1682 return false; // Nothing to render
1683 }
1684
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 device->SetViewport(&viewport);
1686
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001687 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001689 if (mState.scissorTest)
1690 {
1691 RECT rect = {mState.scissorX,
1692 mState.scissorY,
1693 mState.scissorX + mState.scissorWidth,
1694 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001695 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1696 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001697 device->SetScissorRect(&rect);
1698 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1699 }
1700 else
1701 {
1702 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1703 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001704
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001705 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 }
1707
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001708 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001710 Program *programObject = getCurrentProgram();
1711
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001712 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001713 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001715
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001716 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001717 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1718 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1719 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001720 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001723 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001724 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1725
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001726 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001727 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001728
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001729 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001730 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001733 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001734 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
1736
1737 return true;
1738}
1739
1740// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001741void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742{
1743 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001744 Program *programObject = getCurrentProgram();
1745
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001746 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001747 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001748 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001750 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001751 GLint alwaysFront = !isTriangleMode(drawMode);
1752 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1753
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001754 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001755
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 }
1762 else
1763 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001764 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 }
1766
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 mCullStateDirty = false;
1768 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001770 if (mDepthStateDirty)
1771 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001772 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001774 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1775 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 }
1777 else
1778 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001779 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001781
1782 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 }
1784
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001785 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001787 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001788 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001789 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1790
1791 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1792 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1793 {
1794 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1795 }
1796 else
1797 {
1798 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1799 unorm<8>(mState.blendColor.alpha),
1800 unorm<8>(mState.blendColor.alpha),
1801 unorm<8>(mState.blendColor.alpha)));
1802 }
1803
1804 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1805 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1806 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1807
1808 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1809 mState.destBlendRGB != mState.destBlendAlpha ||
1810 mState.blendEquationRGB != mState.blendEquationAlpha)
1811 {
1812 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1813
1814 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1815 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1816 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1817
1818 }
1819 else
1820 {
1821 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1822 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001823 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001825 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001827 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001828
1829 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 }
1831
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001832 if (mStencilStateDirty || mFrontFaceDirty)
1833 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001834 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001835 {
1836 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1837 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1838
1839 // FIXME: Unsupported by D3D9
1840 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1841 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1842 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1843 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1844 mState.stencilRef != mState.stencilBackRef ||
1845 mState.stencilMask != mState.stencilBackMask)
1846 {
1847 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1848 return error(GL_INVALID_OPERATION);
1849 }
1850
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001851 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001852 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001853 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1854
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001855 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1856 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1857 es2dx::ConvertComparison(mState.stencilFunc));
1858
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001859 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 +00001860 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1861
1862 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1863 es2dx::ConvertStencilOp(mState.stencilFail));
1864 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1865 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1866 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1867 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1868
1869 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1870 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1871 es2dx::ConvertComparison(mState.stencilBackFunc));
1872
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001873 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 +00001874 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1875
1876 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1877 es2dx::ConvertStencilOp(mState.stencilBackFail));
1878 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1879 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1880 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1881 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1882 }
1883 else
1884 {
1885 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1886 }
1887
1888 mStencilStateDirty = false;
1889 }
1890
1891 if (mMaskStateDirty)
1892 {
1893 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1894 mState.colorMaskBlue, mState.colorMaskAlpha));
1895 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1896
1897 mMaskStateDirty = false;
1898 }
1899
1900 if (mPolygonOffsetStateDirty)
1901 {
1902 if (mState.polygonOffsetFill)
1903 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001904 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001905 if (depthbuffer)
1906 {
1907 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1908 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1909 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1910 }
1911 }
1912 else
1913 {
1914 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1915 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1916 }
1917
1918 mPolygonOffsetStateDirty = false;
1919 }
1920
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001921 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001923 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001924 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001925 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001926 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001927 FIXME("Sample alpha to coverage is unimplemented.");
1928 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1931 if (mState.sampleCoverage)
1932 {
1933 unsigned int mask = 0;
1934 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001935 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001936 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001938 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001939 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001940 mask <<= 1;
1941
1942 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1943 {
1944 threshold += 1.0f;
1945 mask |= 1;
1946 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001947 }
1948 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001949
1950 if (mState.sampleCoverageInvert)
1951 {
1952 mask = ~mask;
1953 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001954
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001955 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1956 }
1957 else
1958 {
1959 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1960 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001961 }
1962 else
1963 {
1964 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001965 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001966
1967 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 }
1969
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001970 if (mDitherStateDirty)
1971 {
1972 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1973
1974 mDitherStateDirty = false;
1975 }
1976
1977 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978}
1979
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001980// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001981void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001985 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001987 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988 }
1989 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001990}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001992GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001993{
1994 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1995
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001996 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001997 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001998 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001999 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002000 }
2001
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002002 lookupAttributeMapping(translated);
2003
2004 mBufferBackEnd->setupAttributesPreDraw(translated);
2005
2006 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2007 {
2008 if (translated[i].enabled && translated[i].nonArray)
2009 {
2010 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
2011 if (err != GL_NO_ERROR)
2012 {
2013 return err;
2014 }
2015
2016 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2017
2018 *useIndexing = true;
2019 return GL_NO_ERROR;
2020 }
2021 }
2022
2023 *useIndexing = false;
2024 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002025}
2026
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002027GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002028{
2029 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2030
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002031 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002032
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002033 if (err == GL_NO_ERROR)
2034 {
2035 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002036
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002037 mBufferBackEnd->setupAttributesPreDraw(translated);
2038 }
2039
2040 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002041}
2042
2043// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002044GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002045{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002046 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002047
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002048 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002049 {
2050 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2051 }
2052
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002053 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054}
2055
2056// Applies the shaders and shader constants to the Direct3D 9 device
2057void Context::applyShaders()
2058{
2059 IDirect3DDevice9 *device = getDevice();
2060 Program *programObject = getCurrentProgram();
2061 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2062 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2063
2064 device->SetVertexShader(vertexShader);
2065 device->SetPixelShader(pixelShader);
2066
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002067 if (programObject->getSerial() != mAppliedProgram)
2068 {
2069 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002070 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002071 mAppliedProgram = programObject->getSerial();
2072 }
2073
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 programObject->applyUniforms();
2075}
2076
2077// Applies the textures and sampler states to the Direct3D 9 device
2078void Context::applyTextures()
2079{
2080 IDirect3DDevice9 *device = getDevice();
2081 Program *programObject = getCurrentProgram();
2082
2083 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2084 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002085 int textureUnit = programObject->getSamplerMapping(sampler);
2086 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002088 SamplerType textureType = programObject->getSamplerType(sampler);
2089
2090 Texture *texture = getSamplerTexture(textureUnit, textureType);
2091
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002092 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002093 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002094 if (texture->isComplete())
2095 {
2096 GLenum wrapS = texture->getWrapS();
2097 GLenum wrapT = texture->getWrapT();
2098 GLenum minFilter = texture->getMinFilter();
2099 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002101 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2102 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002104 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2105 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2106 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2107 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2108 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002110 device->SetTexture(sampler, texture->getTexture());
2111 }
2112 else
2113 {
2114 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2115 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002116 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002117
2118 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002120 else
2121 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002122 if (programObject->isSamplerDirty(sampler))
2123 {
2124 device->SetTexture(sampler, NULL);
2125 programObject->setSamplerDirty(sampler, false);
2126 }
2127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 }
2129}
2130
2131void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2132{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002133 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002134
2135 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2136 {
2137 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2138 }
2139
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002140 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2141 {
2142 return error(GL_INVALID_OPERATION);
2143 }
2144
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002146
2147 if (!renderTarget)
2148 {
2149 return; // Context must be lost, return silently
2150 }
2151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 IDirect3DDevice9 *device = getDevice();
2153
2154 D3DSURFACE_DESC desc;
2155 renderTarget->GetDesc(&desc);
2156
2157 IDirect3DSurface9 *systemSurface;
2158 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2159
2160 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2161 {
2162 return error(GL_OUT_OF_MEMORY);
2163 }
2164
2165 ASSERT(SUCCEEDED(result));
2166
2167 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2168 {
2169 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2170 }
2171
2172 result = device->GetRenderTargetData(renderTarget, systemSurface);
2173
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 if (FAILED(result))
2175 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 systemSurface->Release();
2177
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002178 switch (result)
2179 {
2180 case D3DERR_DRIVERINTERNALERROR:
2181 case D3DERR_DEVICELOST:
2182 return error(GL_OUT_OF_MEMORY);
2183 default:
2184 UNREACHABLE();
2185 return; // No sensible error to generate
2186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187 }
2188
2189 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002190 RECT rect = {std::max(x, 0),
2191 std::max(y, 0),
2192 std::min(x + width, (int)desc.Width),
2193 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194
2195 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2196
2197 if (FAILED(result))
2198 {
2199 UNREACHABLE();
2200 systemSurface->Release();
2201
2202 return; // No sensible error to generate
2203 }
2204
2205 unsigned char *source = (unsigned char*)lock.pBits;
2206 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002207 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002209 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 for (int j = 0; j < rect.bottom - rect.top; j++)
2212 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002213 if (desc.Format == D3DFMT_A8R8G8B8 &&
2214 format == GL_BGRA_EXT &&
2215 type == GL_UNSIGNED_BYTE)
2216 {
2217 // Fast path for EXT_read_format_bgra, given
2218 // an RGBA source buffer. Note that buffers with no
2219 // alpha go through the slow path below.
2220 memcpy(dest + j * outputPitch,
2221 source + j * lock.Pitch,
2222 (rect.right - rect.left) * 4);
2223 continue;
2224 }
2225
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226 for (int i = 0; i < rect.right - rect.left; i++)
2227 {
2228 float r;
2229 float g;
2230 float b;
2231 float a;
2232
2233 switch (desc.Format)
2234 {
2235 case D3DFMT_R5G6B5:
2236 {
2237 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2238
2239 a = 1.0f;
2240 b = (rgb & 0x001F) * (1.0f / 0x001F);
2241 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2242 r = (rgb & 0xF800) * (1.0f / 0xF800);
2243 }
2244 break;
2245 case D3DFMT_X1R5G5B5:
2246 {
2247 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2248
2249 a = 1.0f;
2250 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2251 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2252 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2253 }
2254 break;
2255 case D3DFMT_A1R5G5B5:
2256 {
2257 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2258
2259 a = (argb & 0x8000) ? 1.0f : 0.0f;
2260 b = (argb & 0x001F) * (1.0f / 0x001F);
2261 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2262 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2263 }
2264 break;
2265 case D3DFMT_A8R8G8B8:
2266 {
2267 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2268
2269 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2270 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2271 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2272 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2273 }
2274 break;
2275 case D3DFMT_X8R8G8B8:
2276 {
2277 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2278
2279 a = 1.0f;
2280 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2281 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2282 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2283 }
2284 break;
2285 case D3DFMT_A2R10G10B10:
2286 {
2287 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2288
2289 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2290 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2291 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2292 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2293 }
2294 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002295 case D3DFMT_A32B32G32R32F:
2296 {
2297 // float formats in D3D are stored rgba, rather than the other way round
2298 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2299 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2300 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2301 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2302 }
2303 break;
2304 case D3DFMT_A16B16G16R16F:
2305 {
2306 // float formats in D3D are stored rgba, rather than the other way round
2307 float abgr[4];
2308
2309 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2310
2311 a = abgr[3];
2312 b = abgr[2];
2313 g = abgr[1];
2314 r = abgr[0];
2315 }
2316 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 default:
2318 UNIMPLEMENTED(); // FIXME
2319 UNREACHABLE();
2320 }
2321
2322 switch (format)
2323 {
2324 case GL_RGBA:
2325 switch (type)
2326 {
2327 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002328 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2329 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2330 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2331 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 break;
2333 default: UNREACHABLE();
2334 }
2335 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002336 case GL_BGRA_EXT:
2337 switch (type)
2338 {
2339 case GL_UNSIGNED_BYTE:
2340 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2341 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2342 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2343 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2344 break;
2345 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2346 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2347 // this type is packed as follows:
2348 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2349 // --------------------------------------------------------------------------------
2350 // | 4th | 3rd | 2nd | 1st component |
2351 // --------------------------------------------------------------------------------
2352 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2353 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2354 ((unsigned short)(15 * a + 0.5f) << 12)|
2355 ((unsigned short)(15 * r + 0.5f) << 8) |
2356 ((unsigned short)(15 * g + 0.5f) << 4) |
2357 ((unsigned short)(15 * b + 0.5f) << 0);
2358 break;
2359 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2360 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2361 // this type is packed as follows:
2362 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2363 // --------------------------------------------------------------------------------
2364 // | 4th | 3rd | 2nd | 1st component |
2365 // --------------------------------------------------------------------------------
2366 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2367 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2368 ((unsigned short)( a + 0.5f) << 15) |
2369 ((unsigned short)(31 * r + 0.5f) << 10) |
2370 ((unsigned short)(31 * g + 0.5f) << 5) |
2371 ((unsigned short)(31 * b + 0.5f) << 0);
2372 break;
2373 default: UNREACHABLE();
2374 }
2375 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002376 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 switch (type)
2378 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002379 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002380 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2381 ((unsigned short)(31 * b + 0.5f) << 0) |
2382 ((unsigned short)(63 * g + 0.5f) << 5) |
2383 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384 break;
2385 default: UNREACHABLE();
2386 }
2387 break;
2388 default: UNREACHABLE();
2389 }
2390 }
2391 }
2392
2393 systemSurface->UnlockRect();
2394
2395 systemSurface->Release();
2396}
2397
2398void Context::clear(GLbitfield mask)
2399{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002400 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002401
2402 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2403 {
2404 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2405
2406 return;
2407 }
2408
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002409 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 IDirect3DDevice9 *device = getDevice();
2411 DWORD flags = 0;
2412
2413 if (mask & GL_COLOR_BUFFER_BIT)
2414 {
2415 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002416
2417 if (framebufferObject->getColorbufferType() != GL_NONE)
2418 {
2419 flags |= D3DCLEAR_TARGET;
2420 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422
2423 if (mask & GL_DEPTH_BUFFER_BIT)
2424 {
2425 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002426 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 {
2428 flags |= D3DCLEAR_ZBUFFER;
2429 }
2430 }
2431
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 GLuint stencilUnmasked = 0x0;
2433
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002434 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002437 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002439 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002440 if (!depthStencil)
2441 {
2442 ERR("Depth stencil pointer unexpectedly null.");
2443 return;
2444 }
2445
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002446 D3DSURFACE_DESC desc;
2447 depthStencil->GetDesc(&desc);
2448
2449 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2450 stencilUnmasked = (0x1 << stencilSize) - 1;
2451
2452 if (stencilUnmasked != 0x0)
2453 {
2454 flags |= D3DCLEAR_STENCIL;
2455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 }
2457 }
2458
2459 if (mask != 0)
2460 {
2461 return error(GL_INVALID_VALUE);
2462 }
2463
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002464 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2465 {
2466 return;
2467 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002469 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2470 unorm<8>(mState.colorClearValue.red),
2471 unorm<8>(mState.colorClearValue.green),
2472 unorm<8>(mState.colorClearValue.blue));
2473 float depth = clamp01(mState.depthClearValue);
2474 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475
2476 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2477
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002478 if (!renderTarget)
2479 {
2480 return; // Context must be lost, return silently
2481 }
2482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 D3DSURFACE_DESC desc;
2484 renderTarget->GetDesc(&desc);
2485
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002486 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487
2488 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002489 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002491 !(mState.colorMaskRed && mState.colorMaskGreen &&
2492 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493
2494 if (needMaskedColorClear || needMaskedStencilClear)
2495 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002496 // State which is altered in all paths from this point to the clear call is saved.
2497 // State which is altered in only some paths will be flagged dirty in the case that
2498 // that path is taken.
2499 HRESULT hr;
2500 if (mMaskedClearSavedState == NULL)
2501 {
2502 hr = device->BeginStateBlock();
2503 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2504
2505 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2506 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2507 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2508 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2509 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2510 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2511 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2512 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2513 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2514 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2515 device->SetPixelShader(NULL);
2516 device->SetVertexShader(NULL);
2517 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2518 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002519 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002520
2521 hr = device->EndStateBlock(&mMaskedClearSavedState);
2522 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2523 }
2524
2525 ASSERT(mMaskedClearSavedState != NULL);
2526
2527 if (mMaskedClearSavedState != NULL)
2528 {
2529 hr = mMaskedClearSavedState->Capture();
2530 ASSERT(SUCCEEDED(hr));
2531 }
2532
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2534 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2535 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2536 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2537 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2538 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2539 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2540 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2541
2542 if (flags & D3DCLEAR_TARGET)
2543 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002544 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2545 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2546 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2547 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548 }
2549 else
2550 {
2551 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2552 }
2553
2554 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2555 {
2556 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2557 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2558 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2559 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002560 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002561 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2563 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002564 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 }
2566 else
2567 {
2568 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2569 }
2570
2571 device->SetPixelShader(NULL);
2572 device->SetVertexShader(NULL);
2573 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002574 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002575 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002577 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578 {
2579 float x, y, z, w;
2580 D3DCOLOR diffuse;
2581 };
2582
2583 Vertex quad[4];
2584 quad[0].x = 0.0f;
2585 quad[0].y = (float)desc.Height;
2586 quad[0].z = 0.0f;
2587 quad[0].w = 1.0f;
2588 quad[0].diffuse = color;
2589
2590 quad[1].x = (float)desc.Width;
2591 quad[1].y = (float)desc.Height;
2592 quad[1].z = 0.0f;
2593 quad[1].w = 1.0f;
2594 quad[1].diffuse = color;
2595
2596 quad[2].x = 0.0f;
2597 quad[2].y = 0.0f;
2598 quad[2].z = 0.0f;
2599 quad[2].w = 1.0f;
2600 quad[2].diffuse = color;
2601
2602 quad[3].x = (float)desc.Width;
2603 quad[3].y = 0.0f;
2604 quad[3].z = 0.0f;
2605 quad[3].w = 1.0f;
2606 quad[3].diffuse = color;
2607
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002608 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610
2611 if (flags & D3DCLEAR_ZBUFFER)
2612 {
2613 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2614 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2615 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2616 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002617
2618 if (mMaskedClearSavedState != NULL)
2619 {
2620 mMaskedClearSavedState->Apply();
2621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002623 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
2625 device->Clear(0, NULL, flags, color, depth, stencil);
2626 }
2627}
2628
2629void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2630{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002631 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 {
2633 return error(GL_INVALID_OPERATION);
2634 }
2635
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002636 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 IDirect3DDevice9 *device = getDevice();
2638 D3DPRIMITIVETYPE primitiveType;
2639 int primitiveCount;
2640
2641 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2642 return error(GL_INVALID_ENUM);
2643
2644 if (primitiveCount <= 0)
2645 {
2646 return;
2647 }
2648
2649 if (!applyRenderTarget(false))
2650 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002651 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652 }
2653
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002654 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002655
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002656 TranslatedIndexData indexInfo;
2657 bool useIndexing;
2658 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002659 if (err != GL_NO_ERROR)
2660 {
2661 return error(err);
2662 }
2663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664 applyShaders();
2665 applyTextures();
2666
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002667 if (!getCurrentProgram()->validateSamplers())
2668 {
2669 return error(GL_INVALID_OPERATION);
2670 }
2671
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002672 if (!cullSkipsDraw(mode))
2673 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002674 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002675 if (useIndexing)
2676 {
2677 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2678 }
2679 else
2680 {
2681 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2682 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002683 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684}
2685
2686void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2687{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002688 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002689 {
2690 return error(GL_INVALID_OPERATION);
2691 }
2692
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002693 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 {
2695 return error(GL_INVALID_OPERATION);
2696 }
2697
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002698 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 IDirect3DDevice9 *device = getDevice();
2700 D3DPRIMITIVETYPE primitiveType;
2701 int primitiveCount;
2702
2703 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2704 return error(GL_INVALID_ENUM);
2705
2706 if (primitiveCount <= 0)
2707 {
2708 return;
2709 }
2710
2711 if (!applyRenderTarget(false))
2712 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002713 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714 }
2715
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002716 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002717
2718 TranslatedIndexData indexInfo;
2719 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2720 if (err != GL_NO_ERROR)
2721 {
2722 return error(err);
2723 }
2724
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002725 err = applyVertexBuffer(indexInfo);
2726 if (err != GL_NO_ERROR)
2727 {
2728 return error(err);
2729 }
2730
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 applyShaders();
2732 applyTextures();
2733
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002734 if (!getCurrentProgram()->validateSamplers())
2735 {
2736 return error(GL_INVALID_OPERATION);
2737 }
2738
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002739 if (!cullSkipsDraw(mode))
2740 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002741 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002742 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 +00002743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744}
2745
2746void Context::finish()
2747{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002748 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 IDirect3DDevice9 *device = getDevice();
2750 IDirect3DQuery9 *occlusionQuery = NULL;
2751
2752 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2753
2754 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2755 {
2756 return error(GL_OUT_OF_MEMORY);
2757 }
2758
2759 ASSERT(SUCCEEDED(result));
2760
2761 if (occlusionQuery)
2762 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002763 IDirect3DStateBlock9 *savedState = NULL;
2764 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2765
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002766 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2767 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768
2769 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002770 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 device->SetPixelShader(NULL);
2772 device->SetVertexShader(NULL);
2773 device->SetFVF(D3DFVF_XYZRHW);
2774 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002775 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002778 result = occlusionQuery->Issue(D3DISSUE_END);
2779 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780
2781 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2782 {
2783 // Keep polling, but allow other threads to do something useful first
2784 Sleep(0);
2785 }
2786
2787 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002788
2789 if (savedState)
2790 {
2791 savedState->Apply();
2792 savedState->Release();
2793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 }
2795}
2796
2797void Context::flush()
2798{
2799 IDirect3DDevice9 *device = getDevice();
2800 IDirect3DQuery9 *eventQuery = NULL;
2801
2802 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2803
2804 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2805 {
2806 return error(GL_OUT_OF_MEMORY);
2807 }
2808
2809 ASSERT(SUCCEEDED(result));
2810
2811 if (eventQuery)
2812 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002813 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2814 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002816 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002818
2819 if (result == D3DERR_DEVICELOST)
2820 {
2821 error(GL_OUT_OF_MEMORY);
2822 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823 }
2824}
2825
2826void Context::recordInvalidEnum()
2827{
2828 mInvalidEnum = true;
2829}
2830
2831void Context::recordInvalidValue()
2832{
2833 mInvalidValue = true;
2834}
2835
2836void Context::recordInvalidOperation()
2837{
2838 mInvalidOperation = true;
2839}
2840
2841void Context::recordOutOfMemory()
2842{
2843 mOutOfMemory = true;
2844}
2845
2846void Context::recordInvalidFramebufferOperation()
2847{
2848 mInvalidFramebufferOperation = true;
2849}
2850
2851// Get one of the recorded errors and clear its flag, if any.
2852// [OpenGL ES 2.0.24] section 2.5 page 13.
2853GLenum Context::getError()
2854{
2855 if (mInvalidEnum)
2856 {
2857 mInvalidEnum = false;
2858
2859 return GL_INVALID_ENUM;
2860 }
2861
2862 if (mInvalidValue)
2863 {
2864 mInvalidValue = false;
2865
2866 return GL_INVALID_VALUE;
2867 }
2868
2869 if (mInvalidOperation)
2870 {
2871 mInvalidOperation = false;
2872
2873 return GL_INVALID_OPERATION;
2874 }
2875
2876 if (mOutOfMemory)
2877 {
2878 mOutOfMemory = false;
2879
2880 return GL_OUT_OF_MEMORY;
2881 }
2882
2883 if (mInvalidFramebufferOperation)
2884 {
2885 mInvalidFramebufferOperation = false;
2886
2887 return GL_INVALID_FRAMEBUFFER_OPERATION;
2888 }
2889
2890 return GL_NO_ERROR;
2891}
2892
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002893bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002894{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002895 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002896}
2897
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002898int Context::getMaxSupportedSamples() const
2899{
2900 return mMaxSupportedSamples;
2901}
2902
2903int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2904{
2905 if (requested == 0)
2906 {
2907 return requested;
2908 }
2909
2910 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2911 if (itr == mMultiSampleSupport.end())
2912 {
2913 return -1;
2914 }
2915
2916 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2917 {
2918 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2919 {
2920 return i;
2921 }
2922 }
2923
2924 return -1;
2925}
2926
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002927bool Context::supportsEventQueries() const
2928{
2929 return mSupportsEventQueries;
2930}
2931
daniel@transgaming.com01868132010-08-24 19:21:17 +00002932bool Context::supportsCompressedTextures() const
2933{
2934 return mSupportsCompressedTextures;
2935}
2936
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002937bool Context::supportsFloatTextures() const
2938{
2939 return mSupportsFloatTextures;
2940}
2941
2942bool Context::supportsFloatLinearFilter() const
2943{
2944 return mSupportsFloatLinearFilter;
2945}
2946
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002947bool Context::supportsFloatRenderableTextures() const
2948{
2949 return mSupportsFloatRenderableTextures;
2950}
2951
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002952bool Context::supportsHalfFloatTextures() const
2953{
2954 return mSupportsHalfFloatTextures;
2955}
2956
2957bool Context::supportsHalfFloatLinearFilter() const
2958{
2959 return mSupportsHalfFloatLinearFilter;
2960}
2961
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002962bool Context::supportsHalfFloatRenderableTextures() const
2963{
2964 return mSupportsHalfFloatRenderableTextures;
2965}
2966
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002967int Context::getMaximumRenderbufferDimension() const
2968{
2969 return mMaxRenderbufferDimension;
2970}
2971
2972int Context::getMaximumTextureDimension() const
2973{
2974 return mMaxTextureDimension;
2975}
2976
2977int Context::getMaximumCubeTextureDimension() const
2978{
2979 return mMaxCubeTextureDimension;
2980}
2981
2982int Context::getMaximumTextureLevel() const
2983{
2984 return mMaxTextureLevel;
2985}
2986
daniel@transgaming.comed828e52010-10-15 17:57:30 +00002987bool Context::supportsLuminanceTextures() const
2988{
2989 return mSupportsLuminanceTextures;
2990}
2991
2992bool Context::supportsLuminanceAlphaTextures() const
2993{
2994 return mSupportsLuminanceAlphaTextures;
2995}
2996
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997void Context::detachBuffer(GLuint buffer)
2998{
2999 // [OpenGL ES 2.0.24] section 2.9 page 22:
3000 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3001 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3002
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003003 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003005 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003006 }
3007
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003008 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003009 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003010 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011 }
3012
3013 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3014 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003015 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003016 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003017 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003018 }
3019 }
3020}
3021
3022void Context::detachTexture(GLuint texture)
3023{
3024 // [OpenGL ES 2.0.24] section 3.8 page 84:
3025 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3026 // rebound to texture object zero
3027
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003028 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003030 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003031 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003032 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003033 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003034 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 }
3037 }
3038
3039 // [OpenGL ES 2.0.24] section 4.4 page 112:
3040 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3041 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3042 // image was attached in the currently bound framebuffer.
3043
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003044 Framebuffer *readFramebuffer = getReadFramebuffer();
3045 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003047 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003049 readFramebuffer->detachTexture(texture);
3050 }
3051
3052 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3053 {
3054 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003055 }
3056}
3057
3058void Context::detachFramebuffer(GLuint framebuffer)
3059{
3060 // [OpenGL ES 2.0.24] section 4.4 page 107:
3061 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3062 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3063
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003064 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003066 bindReadFramebuffer(0);
3067 }
3068
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003069 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003070 {
3071 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003072 }
3073}
3074
3075void Context::detachRenderbuffer(GLuint renderbuffer)
3076{
3077 // [OpenGL ES 2.0.24] section 4.4 page 109:
3078 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3079 // had been executed with the target RENDERBUFFER and name of zero.
3080
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003081 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003082 {
3083 bindRenderbuffer(0);
3084 }
3085
3086 // [OpenGL ES 2.0.24] section 4.4 page 111:
3087 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3088 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3089 // point to which this image was attached in the currently bound framebuffer.
3090
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003091 Framebuffer *readFramebuffer = getReadFramebuffer();
3092 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003093
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003094 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003095 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003096 readFramebuffer->detachRenderbuffer(renderbuffer);
3097 }
3098
3099 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3100 {
3101 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003102 }
3103}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003104
3105Texture *Context::getIncompleteTexture(SamplerType type)
3106{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003107 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003108
3109 if (t == NULL)
3110 {
3111 static const GLubyte color[] = { 0, 0, 0, 255 };
3112
3113 switch (type)
3114 {
3115 default:
3116 UNREACHABLE();
3117 // default falls through to SAMPLER_2D
3118
3119 case SAMPLER_2D:
3120 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003121 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003122 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003123 t = incomplete2d;
3124 }
3125 break;
3126
3127 case SAMPLER_CUBE:
3128 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003129 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003130
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003131 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3132 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3133 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3134 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3135 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3136 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003137
3138 t = incompleteCube;
3139 }
3140 break;
3141 }
3142
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003143 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003144 }
3145
3146 return t;
3147}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003148
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003149bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003150{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003151 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003152}
3153
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003154bool Context::isTriangleMode(GLenum drawMode)
3155{
3156 switch (drawMode)
3157 {
3158 case GL_TRIANGLES:
3159 case GL_TRIANGLE_FAN:
3160 case GL_TRIANGLE_STRIP:
3161 return true;
3162 case GL_POINTS:
3163 case GL_LINES:
3164 case GL_LINE_LOOP:
3165 case GL_LINE_STRIP:
3166 return false;
3167 default: UNREACHABLE();
3168 }
3169
3170 return false;
3171}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003172
3173void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3174{
3175 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3176
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003177 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3178 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3179 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3180 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003181
3182 mVertexDataManager->dirtyCurrentValues();
3183}
3184
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003185void Context::initExtensionString()
3186{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003187 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003188 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3189 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003190 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003191 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003192 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003193
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003194 if (supportsEventQueries())
3195 {
3196 mExtensionString += "GL_NV_fence ";
3197 }
3198
daniel@transgaming.com01868132010-08-24 19:21:17 +00003199 if (supportsCompressedTextures())
3200 {
3201 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3202 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003203
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003204 if (supportsFloatTextures())
3205 {
3206 mExtensionString += "GL_OES_texture_float ";
3207 }
3208
3209 if (supportsHalfFloatTextures())
3210 {
3211 mExtensionString += "GL_OES_texture_half_float ";
3212 }
3213
3214 if (supportsFloatLinearFilter())
3215 {
3216 mExtensionString += "GL_OES_texture_float_linear ";
3217 }
3218
3219 if (supportsHalfFloatLinearFilter())
3220 {
3221 mExtensionString += "GL_OES_texture_half_float_linear ";
3222 }
3223
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003224 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003225 {
3226 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3227 }
3228
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003229 if (mBufferBackEnd->supportIntIndices())
3230 {
3231 mExtensionString += "GL_OES_element_index_uint ";
3232 }
3233
3234 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3235 if (end != std::string::npos)
3236 {
3237 mExtensionString.resize(end+1);
3238 }
3239}
3240
3241const char *Context::getExtensionString() const
3242{
3243 return mExtensionString.c_str();
3244}
3245
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003246void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3247 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3248 GLbitfield mask)
3249{
3250 IDirect3DDevice9 *device = getDevice();
3251
3252 Framebuffer *readFramebuffer = getReadFramebuffer();
3253 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3254
3255 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3256 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3257 {
3258 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3259 }
3260
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003261 if (drawFramebuffer->getSamples() != 0)
3262 {
3263 return error(GL_INVALID_OPERATION);
3264 }
3265
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003266 RECT sourceRect;
3267 RECT destRect;
3268
3269 if (srcX0 < srcX1)
3270 {
3271 sourceRect.left = srcX0;
3272 sourceRect.right = srcX1;
3273 destRect.left = dstX0;
3274 destRect.right = dstX1;
3275 }
3276 else
3277 {
3278 sourceRect.left = srcX1;
3279 destRect.left = dstX1;
3280 sourceRect.right = srcX0;
3281 destRect.right = dstX0;
3282 }
3283
3284 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3285 // flip our Y-values here
3286 if (srcY0 < srcY1)
3287 {
3288 sourceRect.bottom = srcY1;
3289 destRect.bottom = dstY1;
3290 sourceRect.top = srcY0;
3291 destRect.top = dstY0;
3292 }
3293 else
3294 {
3295 sourceRect.bottom = srcY0;
3296 destRect.bottom = dstY0;
3297 sourceRect.top = srcY1;
3298 destRect.top = dstY1;
3299 }
3300
3301 RECT sourceScissoredRect = sourceRect;
3302 RECT destScissoredRect = destRect;
3303
3304 if (mState.scissorTest)
3305 {
3306 // Only write to parts of the destination framebuffer which pass the scissor test
3307 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3308 // rect will be checked against scissorY, rather than the bottom.
3309 if (destRect.left < mState.scissorX)
3310 {
3311 int xDiff = mState.scissorX - destRect.left;
3312 destScissoredRect.left = mState.scissorX;
3313 sourceScissoredRect.left += xDiff;
3314 }
3315
3316 if (destRect.right > mState.scissorX + mState.scissorWidth)
3317 {
3318 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3319 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3320 sourceScissoredRect.right -= xDiff;
3321 }
3322
3323 if (destRect.top < mState.scissorY)
3324 {
3325 int yDiff = mState.scissorY - destRect.top;
3326 destScissoredRect.top = mState.scissorY;
3327 sourceScissoredRect.top += yDiff;
3328 }
3329
3330 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3331 {
3332 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3333 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3334 sourceScissoredRect.bottom -= yDiff;
3335 }
3336 }
3337
3338 bool blitRenderTarget = false;
3339 bool blitDepthStencil = false;
3340
3341 RECT sourceTrimmedRect = sourceScissoredRect;
3342 RECT destTrimmedRect = destScissoredRect;
3343
3344 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3345 // the actual draw and read surfaces.
3346 if (sourceTrimmedRect.left < 0)
3347 {
3348 int xDiff = 0 - sourceTrimmedRect.left;
3349 sourceTrimmedRect.left = 0;
3350 destTrimmedRect.left += xDiff;
3351 }
3352
3353 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3354 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3355 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3356 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3357
3358 if (sourceTrimmedRect.right > readBufferWidth)
3359 {
3360 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3361 sourceTrimmedRect.right = readBufferWidth;
3362 destTrimmedRect.right -= xDiff;
3363 }
3364
3365 if (sourceTrimmedRect.top < 0)
3366 {
3367 int yDiff = 0 - sourceTrimmedRect.top;
3368 sourceTrimmedRect.top = 0;
3369 destTrimmedRect.top += yDiff;
3370 }
3371
3372 if (sourceTrimmedRect.bottom > readBufferHeight)
3373 {
3374 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3375 sourceTrimmedRect.bottom = readBufferHeight;
3376 destTrimmedRect.bottom -= yDiff;
3377 }
3378
3379 if (destTrimmedRect.left < 0)
3380 {
3381 int xDiff = 0 - destTrimmedRect.left;
3382 destTrimmedRect.left = 0;
3383 sourceTrimmedRect.left += xDiff;
3384 }
3385
3386 if (destTrimmedRect.right > drawBufferWidth)
3387 {
3388 int xDiff = destTrimmedRect.right - drawBufferWidth;
3389 destTrimmedRect.right = drawBufferWidth;
3390 sourceTrimmedRect.right -= xDiff;
3391 }
3392
3393 if (destTrimmedRect.top < 0)
3394 {
3395 int yDiff = 0 - destTrimmedRect.top;
3396 destTrimmedRect.top = 0;
3397 sourceTrimmedRect.top += yDiff;
3398 }
3399
3400 if (destTrimmedRect.bottom > drawBufferHeight)
3401 {
3402 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3403 destTrimmedRect.bottom = drawBufferHeight;
3404 sourceTrimmedRect.bottom -= yDiff;
3405 }
3406
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003407 bool partialBufferCopy = false;
3408 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3409 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3410 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3411 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3412 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3413 {
3414 partialBufferCopy = true;
3415 }
3416
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003417 if (mask & GL_COLOR_BUFFER_BIT)
3418 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003419 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3420 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3421 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3422 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3423 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003424 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3425 {
3426 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3427 return error(GL_INVALID_OPERATION);
3428 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003429
3430 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3431 {
3432 return error(GL_INVALID_OPERATION);
3433 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003434
3435 blitRenderTarget = true;
3436
3437 }
3438
3439 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3440 {
3441 DepthStencilbuffer *readDSBuffer = NULL;
3442 DepthStencilbuffer *drawDSBuffer = NULL;
3443
3444 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3445 // both a depth and stencil buffer, it will be the same buffer.
3446
3447 if (mask & GL_DEPTH_BUFFER_BIT)
3448 {
3449 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3450 {
3451 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3452 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3453 {
3454 return error(GL_INVALID_OPERATION);
3455 }
3456
3457 blitDepthStencil = true;
3458 readDSBuffer = readFramebuffer->getDepthbuffer();
3459 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3460 }
3461 }
3462
3463 if (mask & GL_STENCIL_BUFFER_BIT)
3464 {
3465 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3466 {
3467 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3468 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3469 {
3470 return error(GL_INVALID_OPERATION);
3471 }
3472
3473 blitDepthStencil = true;
3474 readDSBuffer = readFramebuffer->getStencilbuffer();
3475 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3476 }
3477 }
3478
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003479 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003480 {
3481 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3482 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3483 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003484
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003485 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3486 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003487 {
3488 return error(GL_INVALID_OPERATION);
3489 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003490 }
3491
3492 if (blitRenderTarget || blitDepthStencil)
3493 {
3494 egl::Display *display = getDisplay();
3495 display->endScene();
3496
3497 if (blitRenderTarget)
3498 {
3499 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3500 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3501
3502 if (FAILED(result))
3503 {
3504 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3505 return;
3506 }
3507 }
3508
3509 if (blitDepthStencil)
3510 {
3511 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3512
3513 if (FAILED(result))
3514 {
3515 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3516 return;
3517 }
3518 }
3519 }
3520}
3521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003522}
3523
3524extern "C"
3525{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003526gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003527{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003528 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003529}
3530
3531void glDestroyContext(gl::Context *context)
3532{
3533 delete context;
3534
3535 if (context == gl::getContext())
3536 {
3537 gl::makeCurrent(NULL, NULL, NULL);
3538 }
3539}
3540
3541void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3542{
3543 gl::makeCurrent(context, display, surface);
3544}
3545
3546gl::Context *glGetCurrentContext()
3547{
3548 return gl::getContext();
3549}
3550}