blob: f50aa4defc41c59b4213322a792d7016862462f8 [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.com01868132010-08-24 19:21:17 +0000283
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000284 initExtensionString();
285
286 mState.viewportX = 0;
287 mState.viewportY = 0;
288 mState.viewportWidth = surface->getWidth();
289 mState.viewportHeight = surface->getHeight();
290
291 mState.scissorX = 0;
292 mState.scissorY = 0;
293 mState.scissorWidth = surface->getWidth();
294 mState.scissorHeight = surface->getHeight();
295
296 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000297 }
298
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
300 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000301 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000304 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000305 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
307 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000309 if (defaultRenderTarget)
310 {
311 defaultRenderTarget->Release();
312 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000314 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000316 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000318
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000319 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320}
321
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000322// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000323void Context::markAllStateDirty()
324{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000326 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000327 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000328 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000329 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000330
331 mClearStateDirty = true;
332 mCullStateDirty = true;
333 mDepthStateDirty = true;
334 mMaskStateDirty = true;
335 mBlendStateDirty = true;
336 mStencilStateDirty = true;
337 mPolygonOffsetStateDirty = true;
338 mScissorStateDirty = true;
339 mSampleStateDirty = true;
340 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000341 mFrontFaceDirty = true;
342
343 if (mBufferBackEnd != NULL)
344 {
345 mBufferBackEnd->invalidate();
346 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000347}
348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349void Context::setClearColor(float red, float green, float blue, float alpha)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.colorClearValue.red = red;
352 mState.colorClearValue.green = green;
353 mState.colorClearValue.blue = blue;
354 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355}
356
357void Context::setClearDepth(float depth)
358{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000359 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362void Context::setClearStencil(int stencil)
363{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000364 mState.stencilClearValue = stencil;
365}
366
367void Context::setCullFace(bool enabled)
368{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000369 if (mState.cullFace != enabled)
370 {
371 mState.cullFace = enabled;
372 mCullStateDirty = true;
373 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000374}
375
376bool Context::isCullFaceEnabled() const
377{
378 return mState.cullFace;
379}
380
381void Context::setCullMode(GLenum mode)
382{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000383 if (mState.cullMode != mode)
384 {
385 mState.cullMode = mode;
386 mCullStateDirty = true;
387 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000388}
389
390void Context::setFrontFace(GLenum front)
391{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000392 if (mState.frontFace != front)
393 {
394 mState.frontFace = front;
395 mFrontFaceDirty = true;
396 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000397}
398
399void Context::setDepthTest(bool enabled)
400{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000401 if (mState.depthTest != enabled)
402 {
403 mState.depthTest = enabled;
404 mDepthStateDirty = true;
405 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000406}
407
408bool Context::isDepthTestEnabled() const
409{
410 return mState.depthTest;
411}
412
413void Context::setDepthFunc(GLenum depthFunc)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.depthFunc != depthFunc)
416 {
417 mState.depthFunc = depthFunc;
418 mDepthStateDirty = true;
419 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420}
421
422void Context::setDepthRange(float zNear, float zFar)
423{
424 mState.zNear = zNear;
425 mState.zFar = zFar;
426}
427
428void Context::setBlend(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.blend != enabled)
431 {
432 mState.blend = enabled;
433 mBlendStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isBlendEnabled() const
438{
439 return mState.blend;
440}
441
442void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.sourceBlendRGB != sourceRGB ||
445 mState.sourceBlendAlpha != sourceAlpha ||
446 mState.destBlendRGB != destRGB ||
447 mState.destBlendAlpha != destAlpha)
448 {
449 mState.sourceBlendRGB = sourceRGB;
450 mState.destBlendRGB = destRGB;
451 mState.sourceBlendAlpha = sourceAlpha;
452 mState.destBlendAlpha = destAlpha;
453 mBlendStateDirty = true;
454 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000455}
456
457void Context::setBlendColor(float red, float green, float blue, float alpha)
458{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000459 if (mState.blendColor.red != red ||
460 mState.blendColor.green != green ||
461 mState.blendColor.blue != blue ||
462 mState.blendColor.alpha != alpha)
463 {
464 mState.blendColor.red = red;
465 mState.blendColor.green = green;
466 mState.blendColor.blue = blue;
467 mState.blendColor.alpha = alpha;
468 mBlendStateDirty = true;
469 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000470}
471
472void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
473{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000474 if (mState.blendEquationRGB != rgbEquation ||
475 mState.blendEquationAlpha != alphaEquation)
476 {
477 mState.blendEquationRGB = rgbEquation;
478 mState.blendEquationAlpha = alphaEquation;
479 mBlendStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setStencilTest(bool enabled)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.stencilTest != enabled)
486 {
487 mState.stencilTest = enabled;
488 mStencilStateDirty = true;
489 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000490}
491
492bool Context::isStencilTestEnabled() const
493{
494 return mState.stencilTest;
495}
496
497void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
498{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 if (mState.stencilFunc != stencilFunc ||
500 mState.stencilRef != stencilRef ||
501 mState.stencilMask != stencilMask)
502 {
503 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000504 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 mState.stencilMask = stencilMask;
506 mStencilStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.stencilBackFunc != stencilBackFunc ||
513 mState.stencilBackRef != stencilBackRef ||
514 mState.stencilBackMask != stencilBackMask)
515 {
516 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000517 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000518 mState.stencilBackMask = stencilBackMask;
519 mStencilStateDirty = true;
520 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000521}
522
523void Context::setStencilWritemask(GLuint stencilWritemask)
524{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000525 if (mState.stencilWritemask != stencilWritemask)
526 {
527 mState.stencilWritemask = stencilWritemask;
528 mStencilStateDirty = true;
529 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000530}
531
532void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
533{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000534 if (mState.stencilBackWritemask != stencilBackWritemask)
535 {
536 mState.stencilBackWritemask = stencilBackWritemask;
537 mStencilStateDirty = true;
538 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000539}
540
541void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
542{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 if (mState.stencilFail != stencilFail ||
544 mState.stencilPassDepthFail != stencilPassDepthFail ||
545 mState.stencilPassDepthPass != stencilPassDepthPass)
546 {
547 mState.stencilFail = stencilFail;
548 mState.stencilPassDepthFail = stencilPassDepthFail;
549 mState.stencilPassDepthPass = stencilPassDepthPass;
550 mStencilStateDirty = true;
551 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000552}
553
554void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
555{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 if (mState.stencilBackFail != stencilBackFail ||
557 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
558 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
559 {
560 mState.stencilBackFail = stencilBackFail;
561 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
562 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
563 mStencilStateDirty = true;
564 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000565}
566
567void Context::setPolygonOffsetFill(bool enabled)
568{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569 if (mState.polygonOffsetFill != enabled)
570 {
571 mState.polygonOffsetFill = enabled;
572 mPolygonOffsetStateDirty = true;
573 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000574}
575
576bool Context::isPolygonOffsetFillEnabled() const
577{
578 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000580}
581
582void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
583{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000584 if (mState.polygonOffsetFactor != factor ||
585 mState.polygonOffsetUnits != units)
586 {
587 mState.polygonOffsetFactor = factor;
588 mState.polygonOffsetUnits = units;
589 mPolygonOffsetStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setSampleAlphaToCoverage(bool enabled)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.sampleAlphaToCoverage != enabled)
596 {
597 mState.sampleAlphaToCoverage = enabled;
598 mSampleStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602bool Context::isSampleAlphaToCoverageEnabled() const
603{
604 return mState.sampleAlphaToCoverage;
605}
606
607void Context::setSampleCoverage(bool enabled)
608{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000609 if (mState.sampleCoverage != enabled)
610 {
611 mState.sampleCoverage = enabled;
612 mSampleStateDirty = true;
613 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614}
615
616bool Context::isSampleCoverageEnabled() const
617{
618 return mState.sampleCoverage;
619}
620
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000621void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.sampleCoverageValue != value ||
624 mState.sampleCoverageInvert != invert)
625 {
626 mState.sampleCoverageValue = value;
627 mState.sampleCoverageInvert = invert;
628 mSampleStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setScissorTest(bool enabled)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.scissorTest != enabled)
635 {
636 mState.scissorTest = enabled;
637 mScissorStateDirty = true;
638 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000639}
640
641bool Context::isScissorTestEnabled() const
642{
643 return mState.scissorTest;
644}
645
646void Context::setDither(bool enabled)
647{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000648 if (mState.dither != enabled)
649 {
650 mState.dither = enabled;
651 mDitherStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655bool Context::isDitherEnabled() const
656{
657 return mState.dither;
658}
659
660void Context::setLineWidth(GLfloat width)
661{
662 mState.lineWidth = width;
663}
664
665void Context::setGenerateMipmapHint(GLenum hint)
666{
667 mState.generateMipmapHint = hint;
668}
669
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000670void Context::setFragmentShaderDerivativeHint(GLenum hint)
671{
672 mState.fragmentShaderDerivativeHint = hint;
673 // TODO: Propagate the hint to shader translator so we can write
674 // ddx, ddx_coarse, or ddx_fine depending on the hint.
675 // Ignore for now. It is valid for implementations to ignore hint.
676}
677
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
680 mState.viewportX = x;
681 mState.viewportY = y;
682 mState.viewportWidth = width;
683 mState.viewportHeight = height;
684}
685
686void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
687{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000688 if (mState.scissorX != x || mState.scissorY != y ||
689 mState.scissorWidth != width || mState.scissorHeight != height)
690 {
691 mState.scissorX = x;
692 mState.scissorY = y;
693 mState.scissorWidth = width;
694 mState.scissorHeight = height;
695 mScissorStateDirty = true;
696 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000697}
698
699void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
700{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000701 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
702 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
703 {
704 mState.colorMaskRed = red;
705 mState.colorMaskGreen = green;
706 mState.colorMaskBlue = blue;
707 mState.colorMaskAlpha = alpha;
708 mMaskStateDirty = true;
709 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000710}
711
712void Context::setDepthMask(bool mask)
713{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000714 if (mState.depthMask != mask)
715 {
716 mState.depthMask = mask;
717 mMaskStateDirty = true;
718 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719}
720
721void Context::setActiveSampler(int active)
722{
723 mState.activeSampler = active;
724}
725
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000726GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000727{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000728 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000729}
730
731GLuint Context::getDrawFramebufferHandle() const
732{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000733 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734}
735
736GLuint Context::getRenderbufferHandle() const
737{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000738 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739}
740
741GLuint Context::getArrayBufferHandle() const
742{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000743 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744}
745
746void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
747{
748 mState.vertexAttribute[attribNum].mEnabled = enabled;
749}
750
751const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
752{
753 return mState.vertexAttribute[attribNum];
754}
755
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000756void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757 GLsizei stride, const void *pointer)
758{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000759 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760 mState.vertexAttribute[attribNum].mSize = size;
761 mState.vertexAttribute[attribNum].mType = type;
762 mState.vertexAttribute[attribNum].mNormalized = normalized;
763 mState.vertexAttribute[attribNum].mStride = stride;
764 mState.vertexAttribute[attribNum].mPointer = pointer;
765}
766
767const void *Context::getVertexAttribPointer(unsigned int attribNum) const
768{
769 return mState.vertexAttribute[attribNum].mPointer;
770}
771
772// returns entire set of attributes as a block
773const AttributeState *Context::getVertexAttribBlock()
774{
775 return mState.vertexAttribute;
776}
777
778void Context::setPackAlignment(GLint alignment)
779{
780 mState.packAlignment = alignment;
781}
782
783GLint Context::getPackAlignment() const
784{
785 return mState.packAlignment;
786}
787
788void Context::setUnpackAlignment(GLint alignment)
789{
790 mState.unpackAlignment = alignment;
791}
792
793GLint Context::getUnpackAlignment() const
794{
795 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798GLuint Context::createBuffer()
799{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803GLuint Context::createProgram()
804{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000808GLuint Context::createShader(GLenum type)
809{
810 return mResourceManager->createShader(type);
811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createTexture()
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 return mResourceManager->createTexture();
816}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000818GLuint Context::createRenderbuffer()
819{
820 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000823// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createFramebuffer()
825{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000826 unsigned int handle = 1;
827
828 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
829 {
830 handle++;
831 }
832
833 mFramebufferMap[handle] = NULL;
834
835 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000838GLuint Context::createFence()
839{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000840 unsigned int handle = 0;
841
842 while (mFenceMap.find(handle) != mFenceMap.end())
843 {
844 handle++;
845 }
846
847 mFenceMap[handle] = new Fence;
848
849 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000850}
851
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852void Context::deleteBuffer(GLuint buffer)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteShader(GLuint shader)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteProgram(GLuint program)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteTexture(GLuint texture)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878
879 mResourceManager->deleteTexture(texture);
880}
881
882void Context::deleteRenderbuffer(GLuint renderbuffer)
883{
884 if (mResourceManager->getRenderbuffer(renderbuffer))
885 {
886 detachRenderbuffer(renderbuffer);
887 }
888
889 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
892void Context::deleteFramebuffer(GLuint framebuffer)
893{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
895
896 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897 {
898 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000899
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000900 delete framebufferObject->second;
901 mFramebufferMap.erase(framebufferObject);
902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000904
905void Context::deleteFence(GLuint fence)
906{
907 FenceMap::iterator fenceObject = mFenceMap.find(fence);
908
909 if (fenceObject != mFenceMap.end())
910 {
911 delete fenceObject->second;
912 mFenceMap.erase(fenceObject);
913 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000914}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918 return mResourceManager->getBuffer(handle);
919}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921Shader *Context::getShader(GLuint handle)
922{
923 return mResourceManager->getShader(handle);
924}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926Program *Context::getProgram(GLuint handle)
927{
928 return mResourceManager->getProgram(handle);
929}
930
931Texture *Context::getTexture(GLuint handle)
932{
933 return mResourceManager->getTexture(handle);
934}
935
936Renderbuffer *Context::getRenderbuffer(GLuint handle)
937{
938 return mResourceManager->getRenderbuffer(handle);
939}
940
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000941Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943 return getFramebuffer(mState.readFramebuffer);
944}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000945
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946Framebuffer *Context::getDrawFramebuffer()
947{
948 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindArrayBuffer(unsigned int buffer)
952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
958void Context::bindElementArrayBuffer(unsigned int buffer)
959{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000960 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000962 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
965void Context::bindTexture2D(GLuint texture)
966{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000969 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000971 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::bindTextureCubeMap(GLuint texture)
975{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000976 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000978 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000980 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981}
982
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000983void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000985 if (!getFramebuffer(framebuffer))
986 {
987 mFramebufferMap[framebuffer] = new Framebuffer();
988 }
989
990 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000991}
992
993void Context::bindDrawFramebuffer(GLuint framebuffer)
994{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000995 if (!getFramebuffer(framebuffer))
996 {
997 mFramebufferMap[framebuffer] = new Framebuffer();
998 }
999
1000 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
1003void Context::bindRenderbuffer(GLuint renderbuffer)
1004{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001005 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1006
1007 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008}
1009
1010void Context::useProgram(GLuint program)
1011{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001012 GLuint priorProgram = mState.currentProgram;
1013 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001014
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001015 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001017 Program *newProgram = mResourceManager->getProgram(program);
1018 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1019
1020 if (newProgram)
1021 {
1022 newProgram->addRef();
1023 }
1024
1025 if (oldProgram)
1026 {
1027 oldProgram->release();
1028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
1032void Context::setFramebufferZero(Framebuffer *buffer)
1033{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001034 delete mFramebufferMap[0];
1035 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036}
1037
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001040 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1041 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042}
1043
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001044Framebuffer *Context::getFramebuffer(unsigned int handle)
1045{
1046 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1047
1048 if (framebuffer == mFramebufferMap.end())
1049 {
1050 return NULL;
1051 }
1052 else
1053 {
1054 return framebuffer->second;
1055 }
1056}
1057
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001058Fence *Context::getFence(unsigned int handle)
1059{
1060 FenceMap::iterator fence = mFenceMap.find(handle);
1061
1062 if (fence == mFenceMap.end())
1063 {
1064 return NULL;
1065 }
1066 else
1067 {
1068 return fence->second;
1069 }
1070}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001071
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072Buffer *Context::getArrayBuffer()
1073{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001074 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075}
1076
1077Buffer *Context::getElementArrayBuffer()
1078{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001079 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080}
1081
1082Program *Context::getCurrentProgram()
1083{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001084 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087Texture2D *Context::getTexture2D()
1088{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001089 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 +00001090 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001091 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092 }
1093
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001094 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
1097TextureCubeMap *Context::getTextureCubeMap()
1098{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001099 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 +00001100 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001101 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102 }
1103
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001104 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001107Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001110
1111 if (texid == 0)
1112 {
1113 switch (type)
1114 {
1115 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001116 case SAMPLER_2D: return mTexture2DZero.get();
1117 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001118 }
1119 }
1120
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001121 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122}
1123
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001124bool Context::getBooleanv(GLenum pname, GLboolean *params)
1125{
1126 switch (pname)
1127 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001128 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1129 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1130 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001131 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001132 params[0] = mState.colorMaskRed;
1133 params[1] = mState.colorMaskGreen;
1134 params[2] = mState.colorMaskBlue;
1135 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001136 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001137 case GL_CULL_FACE: *params = mState.cullFace;
1138 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1139 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1140 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1141 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1142 case GL_STENCIL_TEST: *params = mState.stencilTest;
1143 case GL_DEPTH_TEST: *params = mState.depthTest;
1144 case GL_BLEND: *params = mState.blend;
1145 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001146 default:
1147 return false;
1148 }
1149
1150 return true;
1151}
1152
1153bool Context::getFloatv(GLenum pname, GLfloat *params)
1154{
1155 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1156 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1157 // GetIntegerv as its native query function. As it would require conversion in any
1158 // case, this should make no difference to the calling application.
1159 switch (pname)
1160 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1162 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1163 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1164 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1165 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 case GL_ALIASED_LINE_WIDTH_RANGE:
1167 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1168 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1169 break;
1170 case GL_ALIASED_POINT_SIZE_RANGE:
1171 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001172 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 +00001173 break;
1174 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001175 params[0] = mState.zNear;
1176 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001177 break;
1178 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001179 params[0] = mState.colorClearValue.red;
1180 params[1] = mState.colorClearValue.green;
1181 params[2] = mState.colorClearValue.blue;
1182 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001183 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001184 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001185 params[0] = mState.blendColor.red;
1186 params[1] = mState.blendColor.green;
1187 params[2] = mState.blendColor.blue;
1188 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001189 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001190 default:
1191 return false;
1192 }
1193
1194 return true;
1195}
1196
1197bool Context::getIntegerv(GLenum pname, GLint *params)
1198{
1199 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1200 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1201 // GetIntegerv as its native query function. As it would require conversion in any
1202 // case, this should make no difference to the calling application. You may find it in
1203 // Context::getFloatv.
1204 switch (pname)
1205 {
1206 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1207 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1208 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1209 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1210 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1211 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1212 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001213 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001214 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001215 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001216 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1217 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001218 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001219 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1220 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001221 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001222 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1223 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1224 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1225 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001226 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001227 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1228 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1229 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1230 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1231 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1232 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1233 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1234 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1235 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1236 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1237 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1238 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1239 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1240 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1241 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1242 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1243 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1244 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1245 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1246 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1247 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1248 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1249 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1250 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001251 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1252 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001253 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1254 {
1255 if (supportsCompressedTextures())
1256 {
1257 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1258 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1259 *params = 2;
1260 }
1261 else
1262 {
1263 *params = 0;
1264 }
1265 }
1266 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001267 case GL_MAX_SAMPLES_ANGLE:
1268 {
1269 GLsizei maxSamples = getMaxSupportedSamples();
1270 if (maxSamples != 0)
1271 {
1272 *params = maxSamples;
1273 }
1274 else
1275 {
1276 return false;
1277 }
1278
1279 break;
1280 }
1281 case GL_SAMPLE_BUFFERS:
1282 case GL_SAMPLES:
1283 {
1284 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1285 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1286 {
1287 switch (pname)
1288 {
1289 case GL_SAMPLE_BUFFERS:
1290 if (framebuffer->getSamples() != 0)
1291 {
1292 *params = 1;
1293 }
1294 else
1295 {
1296 *params = 0;
1297 }
1298 break;
1299 case GL_SAMPLES:
1300 *params = framebuffer->getSamples();
1301 break;
1302 }
1303 }
1304 else
1305 {
1306 *params = 0;
1307 }
1308 }
1309 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1311 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1312 case GL_MAX_VIEWPORT_DIMS:
1313 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001314 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001315 params[0] = maxDimension;
1316 params[1] = maxDimension;
1317 }
1318 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001319 case GL_COMPRESSED_TEXTURE_FORMATS:
1320 {
1321 if (supportsCompressedTextures())
1322 {
1323 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1324 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1325 }
1326 }
1327 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001329 params[0] = mState.viewportX;
1330 params[1] = mState.viewportY;
1331 params[2] = mState.viewportWidth;
1332 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001333 break;
1334 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001335 params[0] = mState.scissorX;
1336 params[1] = mState.scissorY;
1337 params[2] = mState.scissorWidth;
1338 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001339 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001340 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1341 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001342 case GL_RED_BITS:
1343 case GL_GREEN_BITS:
1344 case GL_BLUE_BITS:
1345 case GL_ALPHA_BITS:
1346 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001347 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001348 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1349
1350 if (colorbuffer)
1351 {
1352 switch (pname)
1353 {
1354 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1355 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1356 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1357 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1358 }
1359 }
1360 else
1361 {
1362 *params = 0;
1363 }
1364 }
1365 break;
1366 case GL_DEPTH_BITS:
1367 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001368 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001369 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001370
1371 if (depthbuffer)
1372 {
1373 *params = depthbuffer->getDepthSize();
1374 }
1375 else
1376 {
1377 *params = 0;
1378 }
1379 }
1380 break;
1381 case GL_STENCIL_BITS:
1382 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001383 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001384 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385
1386 if (stencilbuffer)
1387 {
1388 *params = stencilbuffer->getStencilSize();
1389 }
1390 else
1391 {
1392 *params = 0;
1393 }
1394 }
1395 break;
1396 case GL_TEXTURE_BINDING_2D:
1397 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001398 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001399 {
1400 error(GL_INVALID_OPERATION);
1401 return false;
1402 }
1403
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001404 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405 }
1406 break;
1407 case GL_TEXTURE_BINDING_CUBE_MAP:
1408 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001409 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001410 {
1411 error(GL_INVALID_OPERATION);
1412 return false;
1413 }
1414
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001415 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001416 }
1417 break;
1418 default:
1419 return false;
1420 }
1421
1422 return true;
1423}
1424
1425bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1426{
1427 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1428 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1429 // to the fact that it is stored internally as a float, and so would require conversion
1430 // if returned from Context::getIntegerv. Since this conversion is already implemented
1431 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1432 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1433 // application.
1434 switch (pname)
1435 {
1436 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1437 case GL_SHADER_BINARY_FORMATS:
1438 {
1439 *type = GL_INT;
1440 *numParams = 0;
1441 }
1442 break;
1443 case GL_MAX_VERTEX_ATTRIBS:
1444 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1445 case GL_MAX_VARYING_VECTORS:
1446 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1447 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1448 case GL_MAX_TEXTURE_IMAGE_UNITS:
1449 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1450 case GL_MAX_RENDERBUFFER_SIZE:
1451 case GL_NUM_SHADER_BINARY_FORMATS:
1452 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1453 case GL_ARRAY_BUFFER_BINDING:
1454 case GL_FRAMEBUFFER_BINDING:
1455 case GL_RENDERBUFFER_BINDING:
1456 case GL_CURRENT_PROGRAM:
1457 case GL_PACK_ALIGNMENT:
1458 case GL_UNPACK_ALIGNMENT:
1459 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001460 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001461 case GL_RED_BITS:
1462 case GL_GREEN_BITS:
1463 case GL_BLUE_BITS:
1464 case GL_ALPHA_BITS:
1465 case GL_DEPTH_BITS:
1466 case GL_STENCIL_BITS:
1467 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1468 case GL_CULL_FACE_MODE:
1469 case GL_FRONT_FACE:
1470 case GL_ACTIVE_TEXTURE:
1471 case GL_STENCIL_FUNC:
1472 case GL_STENCIL_VALUE_MASK:
1473 case GL_STENCIL_REF:
1474 case GL_STENCIL_FAIL:
1475 case GL_STENCIL_PASS_DEPTH_FAIL:
1476 case GL_STENCIL_PASS_DEPTH_PASS:
1477 case GL_STENCIL_BACK_FUNC:
1478 case GL_STENCIL_BACK_VALUE_MASK:
1479 case GL_STENCIL_BACK_REF:
1480 case GL_STENCIL_BACK_FAIL:
1481 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1482 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1483 case GL_DEPTH_FUNC:
1484 case GL_BLEND_SRC_RGB:
1485 case GL_BLEND_SRC_ALPHA:
1486 case GL_BLEND_DST_RGB:
1487 case GL_BLEND_DST_ALPHA:
1488 case GL_BLEND_EQUATION_RGB:
1489 case GL_BLEND_EQUATION_ALPHA:
1490 case GL_STENCIL_WRITEMASK:
1491 case GL_STENCIL_BACK_WRITEMASK:
1492 case GL_STENCIL_CLEAR_VALUE:
1493 case GL_SUBPIXEL_BITS:
1494 case GL_MAX_TEXTURE_SIZE:
1495 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1496 case GL_SAMPLE_BUFFERS:
1497 case GL_SAMPLES:
1498 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1499 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1500 case GL_TEXTURE_BINDING_2D:
1501 case GL_TEXTURE_BINDING_CUBE_MAP:
1502 {
1503 *type = GL_INT;
1504 *numParams = 1;
1505 }
1506 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001507 case GL_MAX_SAMPLES_ANGLE:
1508 {
1509 if (getMaxSupportedSamples() != 0)
1510 {
1511 *type = GL_INT;
1512 *numParams = 1;
1513 }
1514 else
1515 {
1516 return false;
1517 }
1518 }
1519 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001520 case GL_MAX_VIEWPORT_DIMS:
1521 {
1522 *type = GL_INT;
1523 *numParams = 2;
1524 }
1525 break;
1526 case GL_VIEWPORT:
1527 case GL_SCISSOR_BOX:
1528 {
1529 *type = GL_INT;
1530 *numParams = 4;
1531 }
1532 break;
1533 case GL_SHADER_COMPILER:
1534 case GL_SAMPLE_COVERAGE_INVERT:
1535 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001536 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1537 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1538 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1539 case GL_SAMPLE_COVERAGE:
1540 case GL_SCISSOR_TEST:
1541 case GL_STENCIL_TEST:
1542 case GL_DEPTH_TEST:
1543 case GL_BLEND:
1544 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001545 {
1546 *type = GL_BOOL;
1547 *numParams = 1;
1548 }
1549 break;
1550 case GL_COLOR_WRITEMASK:
1551 {
1552 *type = GL_BOOL;
1553 *numParams = 4;
1554 }
1555 break;
1556 case GL_POLYGON_OFFSET_FACTOR:
1557 case GL_POLYGON_OFFSET_UNITS:
1558 case GL_SAMPLE_COVERAGE_VALUE:
1559 case GL_DEPTH_CLEAR_VALUE:
1560 case GL_LINE_WIDTH:
1561 {
1562 *type = GL_FLOAT;
1563 *numParams = 1;
1564 }
1565 break;
1566 case GL_ALIASED_LINE_WIDTH_RANGE:
1567 case GL_ALIASED_POINT_SIZE_RANGE:
1568 case GL_DEPTH_RANGE:
1569 {
1570 *type = GL_FLOAT;
1571 *numParams = 2;
1572 }
1573 break;
1574 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001575 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 {
1577 *type = GL_FLOAT;
1578 *numParams = 4;
1579 }
1580 break;
1581 default:
1582 return false;
1583 }
1584
1585 return true;
1586}
1587
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588// Applies the render target surface, depth stencil surface, viewport rectangle and
1589// scissor rectangle to the Direct3D 9 device
1590bool Context::applyRenderTarget(bool ignoreViewport)
1591{
1592 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001593
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001594 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595
1596 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1597 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001598 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1599
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 return false;
1601 }
1602
1603 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001604
1605 if (!renderTarget)
1606 {
1607 return false; // Context must be lost
1608 }
1609
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001610 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001612 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1613 if (renderTargetSerial != mAppliedRenderTargetSerial)
1614 {
1615 device->SetRenderTarget(0, renderTarget);
1616 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001617 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 +00001618 }
1619
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001620 unsigned int depthbufferSerial = 0;
1621 unsigned int stencilbufferSerial = 0;
1622 if (framebufferObject->getDepthbufferType() != GL_NONE)
1623 {
1624 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001625 if (!depthStencil)
1626 {
1627 ERR("Depth stencil pointer unexpectedly null.");
1628 return false;
1629 }
1630
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001631 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1632 }
1633 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1634 {
1635 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001636 if (!depthStencil)
1637 {
1638 ERR("Depth stencil pointer unexpectedly null.");
1639 return false;
1640 }
1641
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001642 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1643 }
1644
1645 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001646 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001647 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001648 {
1649 device->SetDepthStencilSurface(depthStencil);
1650 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001651 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001652 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001653 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654
1655 D3DVIEWPORT9 viewport;
1656 D3DSURFACE_DESC desc;
1657 renderTarget->GetDesc(&desc);
1658
1659 if (ignoreViewport)
1660 {
1661 viewport.X = 0;
1662 viewport.Y = 0;
1663 viewport.Width = desc.Width;
1664 viewport.Height = desc.Height;
1665 viewport.MinZ = 0.0f;
1666 viewport.MaxZ = 1.0f;
1667 }
1668 else
1669 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001670 viewport.X = std::max(mState.viewportX, 0);
1671 viewport.Y = std::max(mState.viewportY, 0);
1672 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1673 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1674 viewport.MinZ = clamp01(mState.zNear);
1675 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 }
1677
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001678 if (viewport.Width <= 0 || viewport.Height <= 0)
1679 {
1680 return false; // Nothing to render
1681 }
1682
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 device->SetViewport(&viewport);
1684
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001685 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001687 if (mState.scissorTest)
1688 {
1689 RECT rect = {mState.scissorX,
1690 mState.scissorY,
1691 mState.scissorX + mState.scissorWidth,
1692 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001693 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1694 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001695 device->SetScissorRect(&rect);
1696 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1697 }
1698 else
1699 {
1700 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1701 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001702
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001703 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 }
1705
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001706 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708 Program *programObject = getCurrentProgram();
1709
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001710 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001711 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001713
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001714 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001715 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1716 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1717 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001718 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1719
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001720 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001721 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001722 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1723
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001724 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001725 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001726
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001727 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001728 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001729
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001730 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001731 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001732 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733 }
1734
1735 return true;
1736}
1737
1738// 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 +00001739void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740{
1741 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001742 Program *programObject = getCurrentProgram();
1743
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001744 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001745 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001746 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001748 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001749 GLint alwaysFront = !isTriangleMode(drawMode);
1750 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1751
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001752 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001753
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
1760 else
1761 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001762 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
1764
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 mCullStateDirty = false;
1766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001768 if (mDepthStateDirty)
1769 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001770 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001772 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1773 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 }
1775 else
1776 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001777 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001779
1780 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781 }
1782
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001783 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001785 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001786 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001787 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1788
1789 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1790 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1791 {
1792 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1793 }
1794 else
1795 {
1796 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1797 unorm<8>(mState.blendColor.alpha),
1798 unorm<8>(mState.blendColor.alpha),
1799 unorm<8>(mState.blendColor.alpha)));
1800 }
1801
1802 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1803 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1804 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1805
1806 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1807 mState.destBlendRGB != mState.destBlendAlpha ||
1808 mState.blendEquationRGB != mState.blendEquationAlpha)
1809 {
1810 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1811
1812 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1813 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1814 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1815
1816 }
1817 else
1818 {
1819 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1820 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001821 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001823 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001825 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826
1827 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828 }
1829
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001830 if (mStencilStateDirty || mFrontFaceDirty)
1831 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001832 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 {
1834 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1835 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1836
1837 // FIXME: Unsupported by D3D9
1838 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1839 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1840 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1841 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1842 mState.stencilRef != mState.stencilBackRef ||
1843 mState.stencilMask != mState.stencilBackMask)
1844 {
1845 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1846 return error(GL_INVALID_OPERATION);
1847 }
1848
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001849 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001850 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001851 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1852
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001853 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1854 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1855 es2dx::ConvertComparison(mState.stencilFunc));
1856
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001857 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 +00001858 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1859
1860 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1861 es2dx::ConvertStencilOp(mState.stencilFail));
1862 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1863 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1864 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1865 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1866
1867 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1868 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1869 es2dx::ConvertComparison(mState.stencilBackFunc));
1870
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001871 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 +00001872 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1873
1874 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1875 es2dx::ConvertStencilOp(mState.stencilBackFail));
1876 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1877 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1878 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1879 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1880 }
1881 else
1882 {
1883 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1884 }
1885
1886 mStencilStateDirty = false;
1887 }
1888
1889 if (mMaskStateDirty)
1890 {
1891 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1892 mState.colorMaskBlue, mState.colorMaskAlpha));
1893 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1894
1895 mMaskStateDirty = false;
1896 }
1897
1898 if (mPolygonOffsetStateDirty)
1899 {
1900 if (mState.polygonOffsetFill)
1901 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001902 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001903 if (depthbuffer)
1904 {
1905 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1906 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1907 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1908 }
1909 }
1910 else
1911 {
1912 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1913 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1914 }
1915
1916 mPolygonOffsetStateDirty = false;
1917 }
1918
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001919 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001921 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001922 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001923 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001924 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001925 FIXME("Sample alpha to coverage is unimplemented.");
1926 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001927
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001928 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1929 if (mState.sampleCoverage)
1930 {
1931 unsigned int mask = 0;
1932 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001933 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001934 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001935
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001936 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001938 mask <<= 1;
1939
1940 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1941 {
1942 threshold += 1.0f;
1943 mask |= 1;
1944 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001945 }
1946 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001947
1948 if (mState.sampleCoverageInvert)
1949 {
1950 mask = ~mask;
1951 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001952
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001953 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1954 }
1955 else
1956 {
1957 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1958 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001959 }
1960 else
1961 {
1962 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001963 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001964
1965 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966 }
1967
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001968 if (mDitherStateDirty)
1969 {
1970 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1971
1972 mDitherStateDirty = false;
1973 }
1974
1975 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976}
1977
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001978// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001979void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001981 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001985 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 }
1987 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001988}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001990GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001991{
1992 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1993
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001994 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001995 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001996 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001997 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001998 }
1999
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002000 lookupAttributeMapping(translated);
2001
2002 mBufferBackEnd->setupAttributesPreDraw(translated);
2003
2004 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2005 {
2006 if (translated[i].enabled && translated[i].nonArray)
2007 {
2008 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
2009 if (err != GL_NO_ERROR)
2010 {
2011 return err;
2012 }
2013
2014 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2015
2016 *useIndexing = true;
2017 return GL_NO_ERROR;
2018 }
2019 }
2020
2021 *useIndexing = false;
2022 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002023}
2024
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002025GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002026{
2027 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2028
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002029 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002030
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002031 if (err == GL_NO_ERROR)
2032 {
2033 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002034
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002035 mBufferBackEnd->setupAttributesPreDraw(translated);
2036 }
2037
2038 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002039}
2040
2041// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002042GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002043{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002044 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002045
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002046 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002047 {
2048 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2049 }
2050
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002051 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052}
2053
2054// Applies the shaders and shader constants to the Direct3D 9 device
2055void Context::applyShaders()
2056{
2057 IDirect3DDevice9 *device = getDevice();
2058 Program *programObject = getCurrentProgram();
2059 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2060 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2061
2062 device->SetVertexShader(vertexShader);
2063 device->SetPixelShader(pixelShader);
2064
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002065 if (programObject->getSerial() != mAppliedProgram)
2066 {
2067 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002068 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002069 mAppliedProgram = programObject->getSerial();
2070 }
2071
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 programObject->applyUniforms();
2073}
2074
2075// Applies the textures and sampler states to the Direct3D 9 device
2076void Context::applyTextures()
2077{
2078 IDirect3DDevice9 *device = getDevice();
2079 Program *programObject = getCurrentProgram();
2080
2081 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2082 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002083 int textureUnit = programObject->getSamplerMapping(sampler);
2084 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002086 SamplerType textureType = programObject->getSamplerType(sampler);
2087
2088 Texture *texture = getSamplerTexture(textureUnit, textureType);
2089
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002090 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002091 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002092 if (texture->isComplete())
2093 {
2094 GLenum wrapS = texture->getWrapS();
2095 GLenum wrapT = texture->getWrapT();
2096 GLenum minFilter = texture->getMinFilter();
2097 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002099 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2100 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002102 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2103 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2104 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2105 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2106 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002108 device->SetTexture(sampler, texture->getTexture());
2109 }
2110 else
2111 {
2112 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2113 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002114 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002115
2116 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002118 else
2119 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002120 if (programObject->isSamplerDirty(sampler))
2121 {
2122 device->SetTexture(sampler, NULL);
2123 programObject->setSamplerDirty(sampler, false);
2124 }
2125 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 }
2127}
2128
2129void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2130{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002131 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002132
2133 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2134 {
2135 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2136 }
2137
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002138 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2139 {
2140 return error(GL_INVALID_OPERATION);
2141 }
2142
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002144
2145 if (!renderTarget)
2146 {
2147 return; // Context must be lost, return silently
2148 }
2149
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 IDirect3DDevice9 *device = getDevice();
2151
2152 D3DSURFACE_DESC desc;
2153 renderTarget->GetDesc(&desc);
2154
2155 IDirect3DSurface9 *systemSurface;
2156 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2157
2158 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2159 {
2160 return error(GL_OUT_OF_MEMORY);
2161 }
2162
2163 ASSERT(SUCCEEDED(result));
2164
2165 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2166 {
2167 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2168 }
2169
2170 result = device->GetRenderTargetData(renderTarget, systemSurface);
2171
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 if (FAILED(result))
2173 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 systemSurface->Release();
2175
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002176 switch (result)
2177 {
2178 case D3DERR_DRIVERINTERNALERROR:
2179 case D3DERR_DEVICELOST:
2180 return error(GL_OUT_OF_MEMORY);
2181 default:
2182 UNREACHABLE();
2183 return; // No sensible error to generate
2184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
2186
2187 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002188 RECT rect = {std::max(x, 0),
2189 std::max(y, 0),
2190 std::min(x + width, (int)desc.Width),
2191 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192
2193 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2194
2195 if (FAILED(result))
2196 {
2197 UNREACHABLE();
2198 systemSurface->Release();
2199
2200 return; // No sensible error to generate
2201 }
2202
2203 unsigned char *source = (unsigned char*)lock.pBits;
2204 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002205 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002207 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002208
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209 for (int j = 0; j < rect.bottom - rect.top; j++)
2210 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002211 if (desc.Format == D3DFMT_A8R8G8B8 &&
2212 format == GL_BGRA_EXT &&
2213 type == GL_UNSIGNED_BYTE)
2214 {
2215 // Fast path for EXT_read_format_bgra, given
2216 // an RGBA source buffer. Note that buffers with no
2217 // alpha go through the slow path below.
2218 memcpy(dest + j * outputPitch,
2219 source + j * lock.Pitch,
2220 (rect.right - rect.left) * 4);
2221 continue;
2222 }
2223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002224 for (int i = 0; i < rect.right - rect.left; i++)
2225 {
2226 float r;
2227 float g;
2228 float b;
2229 float a;
2230
2231 switch (desc.Format)
2232 {
2233 case D3DFMT_R5G6B5:
2234 {
2235 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2236
2237 a = 1.0f;
2238 b = (rgb & 0x001F) * (1.0f / 0x001F);
2239 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2240 r = (rgb & 0xF800) * (1.0f / 0xF800);
2241 }
2242 break;
2243 case D3DFMT_X1R5G5B5:
2244 {
2245 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2246
2247 a = 1.0f;
2248 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2249 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2250 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2251 }
2252 break;
2253 case D3DFMT_A1R5G5B5:
2254 {
2255 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2256
2257 a = (argb & 0x8000) ? 1.0f : 0.0f;
2258 b = (argb & 0x001F) * (1.0f / 0x001F);
2259 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2260 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2261 }
2262 break;
2263 case D3DFMT_A8R8G8B8:
2264 {
2265 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2266
2267 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2268 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2269 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2270 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2271 }
2272 break;
2273 case D3DFMT_X8R8G8B8:
2274 {
2275 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2276
2277 a = 1.0f;
2278 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2279 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2280 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2281 }
2282 break;
2283 case D3DFMT_A2R10G10B10:
2284 {
2285 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2286
2287 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2288 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2289 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2290 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2291 }
2292 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002293 case D3DFMT_A32B32G32R32F:
2294 {
2295 // float formats in D3D are stored rgba, rather than the other way round
2296 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2297 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2298 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2299 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2300 }
2301 break;
2302 case D3DFMT_A16B16G16R16F:
2303 {
2304 // float formats in D3D are stored rgba, rather than the other way round
2305 float abgr[4];
2306
2307 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2308
2309 a = abgr[3];
2310 b = abgr[2];
2311 g = abgr[1];
2312 r = abgr[0];
2313 }
2314 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 default:
2316 UNIMPLEMENTED(); // FIXME
2317 UNREACHABLE();
2318 }
2319
2320 switch (format)
2321 {
2322 case GL_RGBA:
2323 switch (type)
2324 {
2325 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002326 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2327 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2328 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2329 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330 break;
2331 default: UNREACHABLE();
2332 }
2333 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002334 case GL_BGRA_EXT:
2335 switch (type)
2336 {
2337 case GL_UNSIGNED_BYTE:
2338 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2339 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2340 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2341 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2342 break;
2343 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2344 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2345 // this type is packed as follows:
2346 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2347 // --------------------------------------------------------------------------------
2348 // | 4th | 3rd | 2nd | 1st component |
2349 // --------------------------------------------------------------------------------
2350 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2351 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2352 ((unsigned short)(15 * a + 0.5f) << 12)|
2353 ((unsigned short)(15 * r + 0.5f) << 8) |
2354 ((unsigned short)(15 * g + 0.5f) << 4) |
2355 ((unsigned short)(15 * b + 0.5f) << 0);
2356 break;
2357 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2358 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2359 // this type is packed as follows:
2360 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2361 // --------------------------------------------------------------------------------
2362 // | 4th | 3rd | 2nd | 1st component |
2363 // --------------------------------------------------------------------------------
2364 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2365 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2366 ((unsigned short)( a + 0.5f) << 15) |
2367 ((unsigned short)(31 * r + 0.5f) << 10) |
2368 ((unsigned short)(31 * g + 0.5f) << 5) |
2369 ((unsigned short)(31 * b + 0.5f) << 0);
2370 break;
2371 default: UNREACHABLE();
2372 }
2373 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002374 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 switch (type)
2376 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002377 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002378 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2379 ((unsigned short)(31 * b + 0.5f) << 0) |
2380 ((unsigned short)(63 * g + 0.5f) << 5) |
2381 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382 break;
2383 default: UNREACHABLE();
2384 }
2385 break;
2386 default: UNREACHABLE();
2387 }
2388 }
2389 }
2390
2391 systemSurface->UnlockRect();
2392
2393 systemSurface->Release();
2394}
2395
2396void Context::clear(GLbitfield mask)
2397{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002398 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002399
2400 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2401 {
2402 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2403
2404 return;
2405 }
2406
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002407 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 IDirect3DDevice9 *device = getDevice();
2409 DWORD flags = 0;
2410
2411 if (mask & GL_COLOR_BUFFER_BIT)
2412 {
2413 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002414
2415 if (framebufferObject->getColorbufferType() != GL_NONE)
2416 {
2417 flags |= D3DCLEAR_TARGET;
2418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 }
2420
2421 if (mask & GL_DEPTH_BUFFER_BIT)
2422 {
2423 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002424 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 {
2426 flags |= D3DCLEAR_ZBUFFER;
2427 }
2428 }
2429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 GLuint stencilUnmasked = 0x0;
2431
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002432 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002435 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002437 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002438 if (!depthStencil)
2439 {
2440 ERR("Depth stencil pointer unexpectedly null.");
2441 return;
2442 }
2443
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002444 D3DSURFACE_DESC desc;
2445 depthStencil->GetDesc(&desc);
2446
2447 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2448 stencilUnmasked = (0x1 << stencilSize) - 1;
2449
2450 if (stencilUnmasked != 0x0)
2451 {
2452 flags |= D3DCLEAR_STENCIL;
2453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
2455 }
2456
2457 if (mask != 0)
2458 {
2459 return error(GL_INVALID_VALUE);
2460 }
2461
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002462 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2463 {
2464 return;
2465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002467 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2468 unorm<8>(mState.colorClearValue.red),
2469 unorm<8>(mState.colorClearValue.green),
2470 unorm<8>(mState.colorClearValue.blue));
2471 float depth = clamp01(mState.depthClearValue);
2472 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473
2474 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2475
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002476 if (!renderTarget)
2477 {
2478 return; // Context must be lost, return silently
2479 }
2480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 D3DSURFACE_DESC desc;
2482 renderTarget->GetDesc(&desc);
2483
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002484 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485
2486 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002487 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002489 !(mState.colorMaskRed && mState.colorMaskGreen &&
2490 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491
2492 if (needMaskedColorClear || needMaskedStencilClear)
2493 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002494 // State which is altered in all paths from this point to the clear call is saved.
2495 // State which is altered in only some paths will be flagged dirty in the case that
2496 // that path is taken.
2497 HRESULT hr;
2498 if (mMaskedClearSavedState == NULL)
2499 {
2500 hr = device->BeginStateBlock();
2501 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2502
2503 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2504 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2505 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2506 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2507 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2508 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2509 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2510 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2511 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2512 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2513 device->SetPixelShader(NULL);
2514 device->SetVertexShader(NULL);
2515 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2516 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002517 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002518
2519 hr = device->EndStateBlock(&mMaskedClearSavedState);
2520 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2521 }
2522
2523 ASSERT(mMaskedClearSavedState != NULL);
2524
2525 if (mMaskedClearSavedState != NULL)
2526 {
2527 hr = mMaskedClearSavedState->Capture();
2528 ASSERT(SUCCEEDED(hr));
2529 }
2530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2532 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2533 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2534 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2535 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2536 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2537 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2538 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2539
2540 if (flags & D3DCLEAR_TARGET)
2541 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002542 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2543 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2544 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2545 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 }
2547 else
2548 {
2549 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2550 }
2551
2552 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2553 {
2554 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2555 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2556 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2557 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002558 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002559 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2561 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002562 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 }
2564 else
2565 {
2566 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2567 }
2568
2569 device->SetPixelShader(NULL);
2570 device->SetVertexShader(NULL);
2571 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002572 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002573 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002575 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576 {
2577 float x, y, z, w;
2578 D3DCOLOR diffuse;
2579 };
2580
2581 Vertex quad[4];
2582 quad[0].x = 0.0f;
2583 quad[0].y = (float)desc.Height;
2584 quad[0].z = 0.0f;
2585 quad[0].w = 1.0f;
2586 quad[0].diffuse = color;
2587
2588 quad[1].x = (float)desc.Width;
2589 quad[1].y = (float)desc.Height;
2590 quad[1].z = 0.0f;
2591 quad[1].w = 1.0f;
2592 quad[1].diffuse = color;
2593
2594 quad[2].x = 0.0f;
2595 quad[2].y = 0.0f;
2596 quad[2].z = 0.0f;
2597 quad[2].w = 1.0f;
2598 quad[2].diffuse = color;
2599
2600 quad[3].x = (float)desc.Width;
2601 quad[3].y = 0.0f;
2602 quad[3].z = 0.0f;
2603 quad[3].w = 1.0f;
2604 quad[3].diffuse = color;
2605
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002606 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608
2609 if (flags & D3DCLEAR_ZBUFFER)
2610 {
2611 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2612 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2613 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2614 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002615
2616 if (mMaskedClearSavedState != NULL)
2617 {
2618 mMaskedClearSavedState->Apply();
2619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002621 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622 {
2623 device->Clear(0, NULL, flags, color, depth, stencil);
2624 }
2625}
2626
2627void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2628{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002629 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630 {
2631 return error(GL_INVALID_OPERATION);
2632 }
2633
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002634 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 IDirect3DDevice9 *device = getDevice();
2636 D3DPRIMITIVETYPE primitiveType;
2637 int primitiveCount;
2638
2639 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2640 return error(GL_INVALID_ENUM);
2641
2642 if (primitiveCount <= 0)
2643 {
2644 return;
2645 }
2646
2647 if (!applyRenderTarget(false))
2648 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002649 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650 }
2651
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002652 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002653
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002654 TranslatedIndexData indexInfo;
2655 bool useIndexing;
2656 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002657 if (err != GL_NO_ERROR)
2658 {
2659 return error(err);
2660 }
2661
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002662 applyShaders();
2663 applyTextures();
2664
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002665 if (!getCurrentProgram()->validateSamplers())
2666 {
2667 return error(GL_INVALID_OPERATION);
2668 }
2669
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002670 if (!cullSkipsDraw(mode))
2671 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002672 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002673 if (useIndexing)
2674 {
2675 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2676 }
2677 else
2678 {
2679 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2680 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002681 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682}
2683
2684void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2685{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002686 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 {
2688 return error(GL_INVALID_OPERATION);
2689 }
2690
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002691 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 {
2693 return error(GL_INVALID_OPERATION);
2694 }
2695
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002696 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697 IDirect3DDevice9 *device = getDevice();
2698 D3DPRIMITIVETYPE primitiveType;
2699 int primitiveCount;
2700
2701 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2702 return error(GL_INVALID_ENUM);
2703
2704 if (primitiveCount <= 0)
2705 {
2706 return;
2707 }
2708
2709 if (!applyRenderTarget(false))
2710 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002711 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 }
2713
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002714 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002715
2716 TranslatedIndexData indexInfo;
2717 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2718 if (err != GL_NO_ERROR)
2719 {
2720 return error(err);
2721 }
2722
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002723 err = applyVertexBuffer(indexInfo);
2724 if (err != GL_NO_ERROR)
2725 {
2726 return error(err);
2727 }
2728
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729 applyShaders();
2730 applyTextures();
2731
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002732 if (!getCurrentProgram()->validateSamplers())
2733 {
2734 return error(GL_INVALID_OPERATION);
2735 }
2736
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002737 if (!cullSkipsDraw(mode))
2738 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002739 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002740 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 +00002741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742}
2743
2744void Context::finish()
2745{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002746 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 IDirect3DDevice9 *device = getDevice();
2748 IDirect3DQuery9 *occlusionQuery = NULL;
2749
2750 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2751
2752 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2753 {
2754 return error(GL_OUT_OF_MEMORY);
2755 }
2756
2757 ASSERT(SUCCEEDED(result));
2758
2759 if (occlusionQuery)
2760 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002761 IDirect3DStateBlock9 *savedState = NULL;
2762 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2763
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002764 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2765 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766
2767 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002768 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 device->SetPixelShader(NULL);
2770 device->SetVertexShader(NULL);
2771 device->SetFVF(D3DFVF_XYZRHW);
2772 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002773 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002776 result = occlusionQuery->Issue(D3DISSUE_END);
2777 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778
2779 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2780 {
2781 // Keep polling, but allow other threads to do something useful first
2782 Sleep(0);
2783 }
2784
2785 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002786
2787 if (savedState)
2788 {
2789 savedState->Apply();
2790 savedState->Release();
2791 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792 }
2793}
2794
2795void Context::flush()
2796{
2797 IDirect3DDevice9 *device = getDevice();
2798 IDirect3DQuery9 *eventQuery = NULL;
2799
2800 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2801
2802 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2803 {
2804 return error(GL_OUT_OF_MEMORY);
2805 }
2806
2807 ASSERT(SUCCEEDED(result));
2808
2809 if (eventQuery)
2810 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002811 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2812 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002814 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002816
2817 if (result == D3DERR_DEVICELOST)
2818 {
2819 error(GL_OUT_OF_MEMORY);
2820 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821 }
2822}
2823
2824void Context::recordInvalidEnum()
2825{
2826 mInvalidEnum = true;
2827}
2828
2829void Context::recordInvalidValue()
2830{
2831 mInvalidValue = true;
2832}
2833
2834void Context::recordInvalidOperation()
2835{
2836 mInvalidOperation = true;
2837}
2838
2839void Context::recordOutOfMemory()
2840{
2841 mOutOfMemory = true;
2842}
2843
2844void Context::recordInvalidFramebufferOperation()
2845{
2846 mInvalidFramebufferOperation = true;
2847}
2848
2849// Get one of the recorded errors and clear its flag, if any.
2850// [OpenGL ES 2.0.24] section 2.5 page 13.
2851GLenum Context::getError()
2852{
2853 if (mInvalidEnum)
2854 {
2855 mInvalidEnum = false;
2856
2857 return GL_INVALID_ENUM;
2858 }
2859
2860 if (mInvalidValue)
2861 {
2862 mInvalidValue = false;
2863
2864 return GL_INVALID_VALUE;
2865 }
2866
2867 if (mInvalidOperation)
2868 {
2869 mInvalidOperation = false;
2870
2871 return GL_INVALID_OPERATION;
2872 }
2873
2874 if (mOutOfMemory)
2875 {
2876 mOutOfMemory = false;
2877
2878 return GL_OUT_OF_MEMORY;
2879 }
2880
2881 if (mInvalidFramebufferOperation)
2882 {
2883 mInvalidFramebufferOperation = false;
2884
2885 return GL_INVALID_FRAMEBUFFER_OPERATION;
2886 }
2887
2888 return GL_NO_ERROR;
2889}
2890
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002891bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002892{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002893 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002894}
2895
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002896int Context::getMaxSupportedSamples() const
2897{
2898 return mMaxSupportedSamples;
2899}
2900
2901int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2902{
2903 if (requested == 0)
2904 {
2905 return requested;
2906 }
2907
2908 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2909 if (itr == mMultiSampleSupport.end())
2910 {
2911 return -1;
2912 }
2913
2914 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2915 {
2916 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2917 {
2918 return i;
2919 }
2920 }
2921
2922 return -1;
2923}
2924
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002925bool Context::supportsEventQueries() const
2926{
2927 return mSupportsEventQueries;
2928}
2929
daniel@transgaming.com01868132010-08-24 19:21:17 +00002930bool Context::supportsCompressedTextures() const
2931{
2932 return mSupportsCompressedTextures;
2933}
2934
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002935bool Context::supportsFloatTextures() const
2936{
2937 return mSupportsFloatTextures;
2938}
2939
2940bool Context::supportsFloatLinearFilter() const
2941{
2942 return mSupportsFloatLinearFilter;
2943}
2944
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002945bool Context::supportsFloatRenderableTextures() const
2946{
2947 return mSupportsFloatRenderableTextures;
2948}
2949
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002950bool Context::supportsHalfFloatTextures() const
2951{
2952 return mSupportsHalfFloatTextures;
2953}
2954
2955bool Context::supportsHalfFloatLinearFilter() const
2956{
2957 return mSupportsHalfFloatLinearFilter;
2958}
2959
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002960bool Context::supportsHalfFloatRenderableTextures() const
2961{
2962 return mSupportsHalfFloatRenderableTextures;
2963}
2964
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00002965int Context::getMaximumRenderbufferDimension() const
2966{
2967 return mMaxRenderbufferDimension;
2968}
2969
2970int Context::getMaximumTextureDimension() const
2971{
2972 return mMaxTextureDimension;
2973}
2974
2975int Context::getMaximumCubeTextureDimension() const
2976{
2977 return mMaxCubeTextureDimension;
2978}
2979
2980int Context::getMaximumTextureLevel() const
2981{
2982 return mMaxTextureLevel;
2983}
2984
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002985void Context::detachBuffer(GLuint buffer)
2986{
2987 // [OpenGL ES 2.0.24] section 2.9 page 22:
2988 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2989 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2990
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002991 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002992 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002993 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002994 }
2995
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002996 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002998 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002999 }
3000
3001 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3002 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003003 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003005 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003006 }
3007 }
3008}
3009
3010void Context::detachTexture(GLuint texture)
3011{
3012 // [OpenGL ES 2.0.24] section 3.8 page 84:
3013 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3014 // rebound to texture object zero
3015
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003016 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003017 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003018 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003020 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003021 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003022 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003024 }
3025 }
3026
3027 // [OpenGL ES 2.0.24] section 4.4 page 112:
3028 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3029 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3030 // image was attached in the currently bound framebuffer.
3031
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003032 Framebuffer *readFramebuffer = getReadFramebuffer();
3033 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003035 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003037 readFramebuffer->detachTexture(texture);
3038 }
3039
3040 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3041 {
3042 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003043 }
3044}
3045
3046void Context::detachFramebuffer(GLuint framebuffer)
3047{
3048 // [OpenGL ES 2.0.24] section 4.4 page 107:
3049 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3050 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3051
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003052 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003054 bindReadFramebuffer(0);
3055 }
3056
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003057 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003058 {
3059 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003060 }
3061}
3062
3063void Context::detachRenderbuffer(GLuint renderbuffer)
3064{
3065 // [OpenGL ES 2.0.24] section 4.4 page 109:
3066 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3067 // had been executed with the target RENDERBUFFER and name of zero.
3068
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003069 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003070 {
3071 bindRenderbuffer(0);
3072 }
3073
3074 // [OpenGL ES 2.0.24] section 4.4 page 111:
3075 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3076 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3077 // point to which this image was attached in the currently bound framebuffer.
3078
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003079 Framebuffer *readFramebuffer = getReadFramebuffer();
3080 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003082 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003084 readFramebuffer->detachRenderbuffer(renderbuffer);
3085 }
3086
3087 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3088 {
3089 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003090 }
3091}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003092
3093Texture *Context::getIncompleteTexture(SamplerType type)
3094{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003095 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003096
3097 if (t == NULL)
3098 {
3099 static const GLubyte color[] = { 0, 0, 0, 255 };
3100
3101 switch (type)
3102 {
3103 default:
3104 UNREACHABLE();
3105 // default falls through to SAMPLER_2D
3106
3107 case SAMPLER_2D:
3108 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003109 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003110 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003111 t = incomplete2d;
3112 }
3113 break;
3114
3115 case SAMPLER_CUBE:
3116 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003117 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003118
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003119 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3120 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3121 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3122 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3123 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3124 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003125
3126 t = incompleteCube;
3127 }
3128 break;
3129 }
3130
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003131 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003132 }
3133
3134 return t;
3135}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003136
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003137bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003138{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003139 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003140}
3141
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003142bool Context::isTriangleMode(GLenum drawMode)
3143{
3144 switch (drawMode)
3145 {
3146 case GL_TRIANGLES:
3147 case GL_TRIANGLE_FAN:
3148 case GL_TRIANGLE_STRIP:
3149 return true;
3150 case GL_POINTS:
3151 case GL_LINES:
3152 case GL_LINE_LOOP:
3153 case GL_LINE_STRIP:
3154 return false;
3155 default: UNREACHABLE();
3156 }
3157
3158 return false;
3159}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003160
3161void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3162{
3163 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3164
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003165 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3166 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3167 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3168 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003169
3170 mVertexDataManager->dirtyCurrentValues();
3171}
3172
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003173void Context::initExtensionString()
3174{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003175 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003176 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3177 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003178 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003179 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003180 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003181
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003182 if (supportsEventQueries())
3183 {
3184 mExtensionString += "GL_NV_fence ";
3185 }
3186
daniel@transgaming.com01868132010-08-24 19:21:17 +00003187 if (supportsCompressedTextures())
3188 {
3189 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3190 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003191
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003192 if (supportsFloatTextures())
3193 {
3194 mExtensionString += "GL_OES_texture_float ";
3195 }
3196
3197 if (supportsHalfFloatTextures())
3198 {
3199 mExtensionString += "GL_OES_texture_half_float ";
3200 }
3201
3202 if (supportsFloatLinearFilter())
3203 {
3204 mExtensionString += "GL_OES_texture_float_linear ";
3205 }
3206
3207 if (supportsHalfFloatLinearFilter())
3208 {
3209 mExtensionString += "GL_OES_texture_half_float_linear ";
3210 }
3211
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003212 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003213 {
3214 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3215 }
3216
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003217 if (mBufferBackEnd->supportIntIndices())
3218 {
3219 mExtensionString += "GL_OES_element_index_uint ";
3220 }
3221
3222 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3223 if (end != std::string::npos)
3224 {
3225 mExtensionString.resize(end+1);
3226 }
3227}
3228
3229const char *Context::getExtensionString() const
3230{
3231 return mExtensionString.c_str();
3232}
3233
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003234void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3235 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3236 GLbitfield mask)
3237{
3238 IDirect3DDevice9 *device = getDevice();
3239
3240 Framebuffer *readFramebuffer = getReadFramebuffer();
3241 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3242
3243 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3244 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3245 {
3246 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3247 }
3248
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003249 if (drawFramebuffer->getSamples() != 0)
3250 {
3251 return error(GL_INVALID_OPERATION);
3252 }
3253
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003254 RECT sourceRect;
3255 RECT destRect;
3256
3257 if (srcX0 < srcX1)
3258 {
3259 sourceRect.left = srcX0;
3260 sourceRect.right = srcX1;
3261 destRect.left = dstX0;
3262 destRect.right = dstX1;
3263 }
3264 else
3265 {
3266 sourceRect.left = srcX1;
3267 destRect.left = dstX1;
3268 sourceRect.right = srcX0;
3269 destRect.right = dstX0;
3270 }
3271
3272 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3273 // flip our Y-values here
3274 if (srcY0 < srcY1)
3275 {
3276 sourceRect.bottom = srcY1;
3277 destRect.bottom = dstY1;
3278 sourceRect.top = srcY0;
3279 destRect.top = dstY0;
3280 }
3281 else
3282 {
3283 sourceRect.bottom = srcY0;
3284 destRect.bottom = dstY0;
3285 sourceRect.top = srcY1;
3286 destRect.top = dstY1;
3287 }
3288
3289 RECT sourceScissoredRect = sourceRect;
3290 RECT destScissoredRect = destRect;
3291
3292 if (mState.scissorTest)
3293 {
3294 // Only write to parts of the destination framebuffer which pass the scissor test
3295 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3296 // rect will be checked against scissorY, rather than the bottom.
3297 if (destRect.left < mState.scissorX)
3298 {
3299 int xDiff = mState.scissorX - destRect.left;
3300 destScissoredRect.left = mState.scissorX;
3301 sourceScissoredRect.left += xDiff;
3302 }
3303
3304 if (destRect.right > mState.scissorX + mState.scissorWidth)
3305 {
3306 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3307 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3308 sourceScissoredRect.right -= xDiff;
3309 }
3310
3311 if (destRect.top < mState.scissorY)
3312 {
3313 int yDiff = mState.scissorY - destRect.top;
3314 destScissoredRect.top = mState.scissorY;
3315 sourceScissoredRect.top += yDiff;
3316 }
3317
3318 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3319 {
3320 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3321 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3322 sourceScissoredRect.bottom -= yDiff;
3323 }
3324 }
3325
3326 bool blitRenderTarget = false;
3327 bool blitDepthStencil = false;
3328
3329 RECT sourceTrimmedRect = sourceScissoredRect;
3330 RECT destTrimmedRect = destScissoredRect;
3331
3332 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3333 // the actual draw and read surfaces.
3334 if (sourceTrimmedRect.left < 0)
3335 {
3336 int xDiff = 0 - sourceTrimmedRect.left;
3337 sourceTrimmedRect.left = 0;
3338 destTrimmedRect.left += xDiff;
3339 }
3340
3341 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3342 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3343 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3344 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3345
3346 if (sourceTrimmedRect.right > readBufferWidth)
3347 {
3348 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3349 sourceTrimmedRect.right = readBufferWidth;
3350 destTrimmedRect.right -= xDiff;
3351 }
3352
3353 if (sourceTrimmedRect.top < 0)
3354 {
3355 int yDiff = 0 - sourceTrimmedRect.top;
3356 sourceTrimmedRect.top = 0;
3357 destTrimmedRect.top += yDiff;
3358 }
3359
3360 if (sourceTrimmedRect.bottom > readBufferHeight)
3361 {
3362 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3363 sourceTrimmedRect.bottom = readBufferHeight;
3364 destTrimmedRect.bottom -= yDiff;
3365 }
3366
3367 if (destTrimmedRect.left < 0)
3368 {
3369 int xDiff = 0 - destTrimmedRect.left;
3370 destTrimmedRect.left = 0;
3371 sourceTrimmedRect.left += xDiff;
3372 }
3373
3374 if (destTrimmedRect.right > drawBufferWidth)
3375 {
3376 int xDiff = destTrimmedRect.right - drawBufferWidth;
3377 destTrimmedRect.right = drawBufferWidth;
3378 sourceTrimmedRect.right -= xDiff;
3379 }
3380
3381 if (destTrimmedRect.top < 0)
3382 {
3383 int yDiff = 0 - destTrimmedRect.top;
3384 destTrimmedRect.top = 0;
3385 sourceTrimmedRect.top += yDiff;
3386 }
3387
3388 if (destTrimmedRect.bottom > drawBufferHeight)
3389 {
3390 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3391 destTrimmedRect.bottom = drawBufferHeight;
3392 sourceTrimmedRect.bottom -= yDiff;
3393 }
3394
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003395 bool partialBufferCopy = false;
3396 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3397 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3398 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3399 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3400 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3401 {
3402 partialBufferCopy = true;
3403 }
3404
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003405 if (mask & GL_COLOR_BUFFER_BIT)
3406 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003407 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3408 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3409 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3410 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3411 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003412 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3413 {
3414 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3415 return error(GL_INVALID_OPERATION);
3416 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003417
3418 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3419 {
3420 return error(GL_INVALID_OPERATION);
3421 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003422
3423 blitRenderTarget = true;
3424
3425 }
3426
3427 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3428 {
3429 DepthStencilbuffer *readDSBuffer = NULL;
3430 DepthStencilbuffer *drawDSBuffer = NULL;
3431
3432 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3433 // both a depth and stencil buffer, it will be the same buffer.
3434
3435 if (mask & GL_DEPTH_BUFFER_BIT)
3436 {
3437 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3438 {
3439 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3440 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3441 {
3442 return error(GL_INVALID_OPERATION);
3443 }
3444
3445 blitDepthStencil = true;
3446 readDSBuffer = readFramebuffer->getDepthbuffer();
3447 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3448 }
3449 }
3450
3451 if (mask & GL_STENCIL_BUFFER_BIT)
3452 {
3453 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3454 {
3455 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3456 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3457 {
3458 return error(GL_INVALID_OPERATION);
3459 }
3460
3461 blitDepthStencil = true;
3462 readDSBuffer = readFramebuffer->getStencilbuffer();
3463 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3464 }
3465 }
3466
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003467 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003468 {
3469 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3470 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3471 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003472
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003473 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3474 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003475 {
3476 return error(GL_INVALID_OPERATION);
3477 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003478 }
3479
3480 if (blitRenderTarget || blitDepthStencil)
3481 {
3482 egl::Display *display = getDisplay();
3483 display->endScene();
3484
3485 if (blitRenderTarget)
3486 {
3487 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3488 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3489
3490 if (FAILED(result))
3491 {
3492 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3493 return;
3494 }
3495 }
3496
3497 if (blitDepthStencil)
3498 {
3499 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3500
3501 if (FAILED(result))
3502 {
3503 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3504 return;
3505 }
3506 }
3507 }
3508}
3509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003510}
3511
3512extern "C"
3513{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003514gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003515{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003516 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003517}
3518
3519void glDestroyContext(gl::Context *context)
3520{
3521 delete context;
3522
3523 if (context == gl::getContext())
3524 {
3525 gl::makeCurrent(NULL, NULL, NULL);
3526 }
3527}
3528
3529void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3530{
3531 gl::makeCurrent(context, display, surface);
3532}
3533
3534gl::Context *glGetCurrentContext()
3535{
3536 return gl::getContext();
3537}
3538}