blob: 55cdae32b0f1ffa0990b1feeebe27d0f1915fe26 [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.com1f135d82010-08-24 19:20:36 +0000243 const D3DFORMAT renderBufferFormats[] =
244 {
245 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000246 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000247 D3DFMT_R5G6B5,
248 D3DFMT_D24S8
249 };
250
251 int max = 0;
252 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
253 {
254 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
255 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
256 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
257
258 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
259 {
260 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
261 {
262 max = j;
263 }
264 }
265 }
266
267 mMaxSupportedSamples = max;
268
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000269 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000270 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000271 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
272 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000273
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000274 initExtensionString();
275
276 mState.viewportX = 0;
277 mState.viewportY = 0;
278 mState.viewportWidth = surface->getWidth();
279 mState.viewportHeight = surface->getHeight();
280
281 mState.scissorX = 0;
282 mState.scissorY = 0;
283 mState.scissorWidth = surface->getWidth();
284 mState.scissorHeight = surface->getHeight();
285
286 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000287 }
288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
290 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000291 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000294 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000295 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
297 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000299 if (defaultRenderTarget)
300 {
301 defaultRenderTarget->Release();
302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000304 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000306 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000308
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000309 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000310
311 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312}
313
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000314// 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 +0000315void Context::markAllStateDirty()
316{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000317 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000318 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000319 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000320 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000321 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000322
323 mClearStateDirty = true;
324 mCullStateDirty = true;
325 mDepthStateDirty = true;
326 mMaskStateDirty = true;
327 mBlendStateDirty = true;
328 mStencilStateDirty = true;
329 mPolygonOffsetStateDirty = true;
330 mScissorStateDirty = true;
331 mSampleStateDirty = true;
332 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000333 mFrontFaceDirty = true;
334
335 if (mBufferBackEnd != NULL)
336 {
337 mBufferBackEnd->invalidate();
338 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000339}
340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341void Context::setClearColor(float red, float green, float blue, float alpha)
342{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000343 mState.colorClearValue.red = red;
344 mState.colorClearValue.green = green;
345 mState.colorClearValue.blue = blue;
346 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
349void Context::setClearDepth(float depth)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearStencil(int stencil)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.stencilClearValue = stencil;
357}
358
359void Context::setCullFace(bool enabled)
360{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000361 if (mState.cullFace != enabled)
362 {
363 mState.cullFace = enabled;
364 mCullStateDirty = true;
365 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000366}
367
368bool Context::isCullFaceEnabled() const
369{
370 return mState.cullFace;
371}
372
373void Context::setCullMode(GLenum mode)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.cullMode != mode)
376 {
377 mState.cullMode = mode;
378 mCullStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382void Context::setFrontFace(GLenum front)
383{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000384 if (mState.frontFace != front)
385 {
386 mState.frontFace = front;
387 mFrontFaceDirty = true;
388 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389}
390
391void Context::setDepthTest(bool enabled)
392{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393 if (mState.depthTest != enabled)
394 {
395 mState.depthTest = enabled;
396 mDepthStateDirty = true;
397 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398}
399
400bool Context::isDepthTestEnabled() const
401{
402 return mState.depthTest;
403}
404
405void Context::setDepthFunc(GLenum depthFunc)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.depthFunc != depthFunc)
408 {
409 mState.depthFunc = depthFunc;
410 mDepthStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414void Context::setDepthRange(float zNear, float zFar)
415{
416 mState.zNear = zNear;
417 mState.zFar = zFar;
418}
419
420void Context::setBlend(bool enabled)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.blend != enabled)
423 {
424 mState.blend = enabled;
425 mBlendStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429bool Context::isBlendEnabled() const
430{
431 return mState.blend;
432}
433
434void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
435{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000436 if (mState.sourceBlendRGB != sourceRGB ||
437 mState.sourceBlendAlpha != sourceAlpha ||
438 mState.destBlendRGB != destRGB ||
439 mState.destBlendAlpha != destAlpha)
440 {
441 mState.sourceBlendRGB = sourceRGB;
442 mState.destBlendRGB = destRGB;
443 mState.sourceBlendAlpha = sourceAlpha;
444 mState.destBlendAlpha = destAlpha;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449void Context::setBlendColor(float red, float green, float blue, float alpha)
450{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000451 if (mState.blendColor.red != red ||
452 mState.blendColor.green != green ||
453 mState.blendColor.blue != blue ||
454 mState.blendColor.alpha != alpha)
455 {
456 mState.blendColor.red = red;
457 mState.blendColor.green = green;
458 mState.blendColor.blue = blue;
459 mState.blendColor.alpha = alpha;
460 mBlendStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
465{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 if (mState.blendEquationRGB != rgbEquation ||
467 mState.blendEquationAlpha != alphaEquation)
468 {
469 mState.blendEquationRGB = rgbEquation;
470 mState.blendEquationAlpha = alphaEquation;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475void Context::setStencilTest(bool enabled)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.stencilTest != enabled)
478 {
479 mState.stencilTest = enabled;
480 mStencilStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484bool Context::isStencilTestEnabled() const
485{
486 return mState.stencilTest;
487}
488
489void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilFunc != stencilFunc ||
492 mState.stencilRef != stencilRef ||
493 mState.stencilMask != stencilMask)
494 {
495 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000496 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 mState.stencilMask = stencilMask;
498 mStencilStateDirty = true;
499 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000500}
501
502void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilBackFunc != stencilBackFunc ||
505 mState.stencilBackRef != stencilBackRef ||
506 mState.stencilBackMask != stencilBackMask)
507 {
508 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000509 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 mState.stencilBackMask = stencilBackMask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilWritemask(GLuint stencilWritemask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilWritemask != stencilWritemask)
518 {
519 mState.stencilWritemask = stencilWritemask;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
525{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000526 if (mState.stencilBackWritemask != stencilBackWritemask)
527 {
528 mState.stencilBackWritemask = stencilBackWritemask;
529 mStencilStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.stencilFail != stencilFail ||
536 mState.stencilPassDepthFail != stencilPassDepthFail ||
537 mState.stencilPassDepthPass != stencilPassDepthPass)
538 {
539 mState.stencilFail = stencilFail;
540 mState.stencilPassDepthFail = stencilPassDepthFail;
541 mState.stencilPassDepthPass = stencilPassDepthPass;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
547{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 if (mState.stencilBackFail != stencilBackFail ||
549 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
550 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
551 {
552 mState.stencilBackFail = stencilBackFail;
553 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
554 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
555 mStencilStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setPolygonOffsetFill(bool enabled)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.polygonOffsetFill != enabled)
562 {
563 mState.polygonOffsetFill = enabled;
564 mPolygonOffsetStateDirty = true;
565 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000566}
567
568bool Context::isPolygonOffsetFillEnabled() const
569{
570 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.polygonOffsetFactor != factor ||
577 mState.polygonOffsetUnits != units)
578 {
579 mState.polygonOffsetFactor = factor;
580 mState.polygonOffsetUnits = units;
581 mPolygonOffsetStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585void Context::setSampleAlphaToCoverage(bool enabled)
586{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000587 if (mState.sampleAlphaToCoverage != enabled)
588 {
589 mState.sampleAlphaToCoverage = enabled;
590 mSampleStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594bool Context::isSampleAlphaToCoverageEnabled() const
595{
596 return mState.sampleAlphaToCoverage;
597}
598
599void Context::setSampleCoverage(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.sampleCoverage != enabled)
602 {
603 mState.sampleCoverage = enabled;
604 mSampleStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isSampleCoverageEnabled() const
609{
610 return mState.sampleCoverage;
611}
612
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000613void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.sampleCoverageValue != value ||
616 mState.sampleCoverageInvert != invert)
617 {
618 mState.sampleCoverageValue = value;
619 mState.sampleCoverageInvert = invert;
620 mSampleStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624void Context::setScissorTest(bool enabled)
625{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000626 if (mState.scissorTest != enabled)
627 {
628 mState.scissorTest = enabled;
629 mScissorStateDirty = true;
630 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000631}
632
633bool Context::isScissorTestEnabled() const
634{
635 return mState.scissorTest;
636}
637
638void Context::setDither(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.dither != enabled)
641 {
642 mState.dither = enabled;
643 mDitherStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isDitherEnabled() const
648{
649 return mState.dither;
650}
651
652void Context::setLineWidth(GLfloat width)
653{
654 mState.lineWidth = width;
655}
656
657void Context::setGenerateMipmapHint(GLenum hint)
658{
659 mState.generateMipmapHint = hint;
660}
661
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000662void Context::setFragmentShaderDerivativeHint(GLenum hint)
663{
664 mState.fragmentShaderDerivativeHint = hint;
665 // TODO: Propagate the hint to shader translator so we can write
666 // ddx, ddx_coarse, or ddx_fine depending on the hint.
667 // Ignore for now. It is valid for implementations to ignore hint.
668}
669
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
671{
672 mState.viewportX = x;
673 mState.viewportY = y;
674 mState.viewportWidth = width;
675 mState.viewportHeight = height;
676}
677
678void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.scissorX != x || mState.scissorY != y ||
681 mState.scissorWidth != width || mState.scissorHeight != height)
682 {
683 mState.scissorX = x;
684 mState.scissorY = y;
685 mState.scissorWidth = width;
686 mState.scissorHeight = height;
687 mScissorStateDirty = true;
688 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689}
690
691void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
694 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
695 {
696 mState.colorMaskRed = red;
697 mState.colorMaskGreen = green;
698 mState.colorMaskBlue = blue;
699 mState.colorMaskAlpha = alpha;
700 mMaskStateDirty = true;
701 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702}
703
704void Context::setDepthMask(bool mask)
705{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000706 if (mState.depthMask != mask)
707 {
708 mState.depthMask = mask;
709 mMaskStateDirty = true;
710 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713void Context::setActiveSampler(int active)
714{
715 mState.activeSampler = active;
716}
717
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000718GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000720 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000721}
722
723GLuint Context::getDrawFramebufferHandle() const
724{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000725 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000726}
727
728GLuint Context::getRenderbufferHandle() const
729{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000730 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733GLuint Context::getArrayBufferHandle() const
734{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000735 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
739{
740 mState.vertexAttribute[attribNum].mEnabled = enabled;
741}
742
743const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
744{
745 return mState.vertexAttribute[attribNum];
746}
747
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749 GLsizei stride, const void *pointer)
750{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000751 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000752 mState.vertexAttribute[attribNum].mSize = size;
753 mState.vertexAttribute[attribNum].mType = type;
754 mState.vertexAttribute[attribNum].mNormalized = normalized;
755 mState.vertexAttribute[attribNum].mStride = stride;
756 mState.vertexAttribute[attribNum].mPointer = pointer;
757}
758
759const void *Context::getVertexAttribPointer(unsigned int attribNum) const
760{
761 return mState.vertexAttribute[attribNum].mPointer;
762}
763
764// returns entire set of attributes as a block
765const AttributeState *Context::getVertexAttribBlock()
766{
767 return mState.vertexAttribute;
768}
769
770void Context::setPackAlignment(GLint alignment)
771{
772 mState.packAlignment = alignment;
773}
774
775GLint Context::getPackAlignment() const
776{
777 return mState.packAlignment;
778}
779
780void Context::setUnpackAlignment(GLint alignment)
781{
782 mState.unpackAlignment = alignment;
783}
784
785GLint Context::getUnpackAlignment() const
786{
787 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790GLuint Context::createBuffer()
791{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000792 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795GLuint Context::createProgram()
796{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000797 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800GLuint Context::createShader(GLenum type)
801{
802 return mResourceManager->createShader(type);
803}
804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805GLuint Context::createTexture()
806{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000807 return mResourceManager->createTexture();
808}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810GLuint Context::createRenderbuffer()
811{
812 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000815// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816GLuint Context::createFramebuffer()
817{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000818 unsigned int handle = 1;
819
820 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
821 {
822 handle++;
823 }
824
825 mFramebufferMap[handle] = NULL;
826
827 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828}
829
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000830GLuint Context::createFence()
831{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000832 unsigned int handle = 0;
833
834 while (mFenceMap.find(handle) != mFenceMap.end())
835 {
836 handle++;
837 }
838
839 mFenceMap[handle] = new Fence;
840
841 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000842}
843
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844void Context::deleteBuffer(GLuint buffer)
845{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000846 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847 {
848 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000850
851 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852}
853
854void Context::deleteShader(GLuint shader)
855{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
859void Context::deleteProgram(GLuint program)
860{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000861 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
864void Context::deleteTexture(GLuint texture)
865{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000866 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867 {
868 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870
871 mResourceManager->deleteTexture(texture);
872}
873
874void Context::deleteRenderbuffer(GLuint renderbuffer)
875{
876 if (mResourceManager->getRenderbuffer(renderbuffer))
877 {
878 detachRenderbuffer(renderbuffer);
879 }
880
881 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882}
883
884void Context::deleteFramebuffer(GLuint framebuffer)
885{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000886 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
887
888 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889 {
890 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000891
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000892 delete framebufferObject->second;
893 mFramebufferMap.erase(framebufferObject);
894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000896
897void Context::deleteFence(GLuint fence)
898{
899 FenceMap::iterator fenceObject = mFenceMap.find(fence);
900
901 if (fenceObject != mFenceMap.end())
902 {
903 delete fenceObject->second;
904 mFenceMap.erase(fenceObject);
905 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000906}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000908Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910 return mResourceManager->getBuffer(handle);
911}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913Shader *Context::getShader(GLuint handle)
914{
915 return mResourceManager->getShader(handle);
916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Program *Context::getProgram(GLuint handle)
919{
920 return mResourceManager->getProgram(handle);
921}
922
923Texture *Context::getTexture(GLuint handle)
924{
925 return mResourceManager->getTexture(handle);
926}
927
928Renderbuffer *Context::getRenderbuffer(GLuint handle)
929{
930 return mResourceManager->getRenderbuffer(handle);
931}
932
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000933Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000934{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935 return getFramebuffer(mState.readFramebuffer);
936}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000937
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938Framebuffer *Context::getDrawFramebuffer()
939{
940 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
943void Context::bindArrayBuffer(unsigned int buffer)
944{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000945 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000947 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
950void Context::bindElementArrayBuffer(unsigned int buffer)
951{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000952 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000954 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
956
957void Context::bindTexture2D(GLuint texture)
958{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000959 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000961 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000963 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
966void Context::bindTextureCubeMap(GLuint texture)
967{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000968 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000970 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000972 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000977 if (!getFramebuffer(framebuffer))
978 {
979 mFramebufferMap[framebuffer] = new Framebuffer();
980 }
981
982 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000983}
984
985void Context::bindDrawFramebuffer(GLuint framebuffer)
986{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000987 if (!getFramebuffer(framebuffer))
988 {
989 mFramebufferMap[framebuffer] = new Framebuffer();
990 }
991
992 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindRenderbuffer(GLuint renderbuffer)
996{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000997 mResourceManager->checkRenderbufferAllocation(renderbuffer);
998
999 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::useProgram(GLuint program)
1003{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001004 GLuint priorProgram = mState.currentProgram;
1005 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001006
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001007 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001009 Program *newProgram = mResourceManager->getProgram(program);
1010 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1011
1012 if (newProgram)
1013 {
1014 newProgram->addRef();
1015 }
1016
1017 if (oldProgram)
1018 {
1019 oldProgram->release();
1020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022}
1023
1024void Context::setFramebufferZero(Framebuffer *buffer)
1025{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001026 delete mFramebufferMap[0];
1027 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001030void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1033 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001036Framebuffer *Context::getFramebuffer(unsigned int handle)
1037{
1038 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1039
1040 if (framebuffer == mFramebufferMap.end())
1041 {
1042 return NULL;
1043 }
1044 else
1045 {
1046 return framebuffer->second;
1047 }
1048}
1049
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001050Fence *Context::getFence(unsigned int handle)
1051{
1052 FenceMap::iterator fence = mFenceMap.find(handle);
1053
1054 if (fence == mFenceMap.end())
1055 {
1056 return NULL;
1057 }
1058 else
1059 {
1060 return fence->second;
1061 }
1062}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064Buffer *Context::getArrayBuffer()
1065{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001066 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
1069Buffer *Context::getElementArrayBuffer()
1070{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001071 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074Program *Context::getCurrentProgram()
1075{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001076 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079Texture2D *Context::getTexture2D()
1080{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001081 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 +00001082 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001083 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 }
1085
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001086 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
1089TextureCubeMap *Context::getTextureCubeMap()
1090{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 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 +00001092 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001093 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094 }
1095
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001099Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001102
1103 if (texid == 0)
1104 {
1105 switch (type)
1106 {
1107 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001108 case SAMPLER_2D: return mTexture2DZero.get();
1109 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001110 }
1111 }
1112
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001113 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001116bool Context::getBooleanv(GLenum pname, GLboolean *params)
1117{
1118 switch (pname)
1119 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001120 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1121 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1122 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001123 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001124 params[0] = mState.colorMaskRed;
1125 params[1] = mState.colorMaskGreen;
1126 params[2] = mState.colorMaskBlue;
1127 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001129 case GL_CULL_FACE: *params = mState.cullFace;
1130 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1131 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1132 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1133 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1134 case GL_STENCIL_TEST: *params = mState.stencilTest;
1135 case GL_DEPTH_TEST: *params = mState.depthTest;
1136 case GL_BLEND: *params = mState.blend;
1137 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001138 default:
1139 return false;
1140 }
1141
1142 return true;
1143}
1144
1145bool Context::getFloatv(GLenum pname, GLfloat *params)
1146{
1147 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1148 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1149 // GetIntegerv as its native query function. As it would require conversion in any
1150 // case, this should make no difference to the calling application.
1151 switch (pname)
1152 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001153 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1154 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1155 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1156 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1157 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001158 case GL_ALIASED_LINE_WIDTH_RANGE:
1159 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1160 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1161 break;
1162 case GL_ALIASED_POINT_SIZE_RANGE:
1163 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001164 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 +00001165 break;
1166 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.zNear;
1168 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
1170 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.colorClearValue.red;
1172 params[1] = mState.colorClearValue.green;
1173 params[2] = mState.colorClearValue.blue;
1174 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001176 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001177 params[0] = mState.blendColor.red;
1178 params[1] = mState.blendColor.green;
1179 params[2] = mState.blendColor.blue;
1180 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001181 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001182 default:
1183 return false;
1184 }
1185
1186 return true;
1187}
1188
1189bool Context::getIntegerv(GLenum pname, GLint *params)
1190{
1191 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1192 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1193 // GetIntegerv as its native query function. As it would require conversion in any
1194 // case, this should make no difference to the calling application. You may find it in
1195 // Context::getFloatv.
1196 switch (pname)
1197 {
1198 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1199 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1200 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1201 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1202 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1203 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1204 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1205 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001206 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001207 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001208 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1209 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001210 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001211 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1212 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001213 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001214 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1215 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1216 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1217 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001218 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001219 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1220 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1221 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1222 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1223 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1224 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1225 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1226 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1227 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1228 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1229 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1230 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1231 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1232 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1233 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1234 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1235 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1236 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1237 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1238 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1239 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1240 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1241 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1242 case GL_SUBPIXEL_BITS: *params = 4; break;
1243 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1244 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001245 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1246 {
1247 if (supportsCompressedTextures())
1248 {
1249 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1250 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1251 *params = 2;
1252 }
1253 else
1254 {
1255 *params = 0;
1256 }
1257 }
1258 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001259 case GL_MAX_SAMPLES_ANGLE:
1260 {
1261 GLsizei maxSamples = getMaxSupportedSamples();
1262 if (maxSamples != 0)
1263 {
1264 *params = maxSamples;
1265 }
1266 else
1267 {
1268 return false;
1269 }
1270
1271 break;
1272 }
1273 case GL_SAMPLE_BUFFERS:
1274 case GL_SAMPLES:
1275 {
1276 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1277 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1278 {
1279 switch (pname)
1280 {
1281 case GL_SAMPLE_BUFFERS:
1282 if (framebuffer->getSamples() != 0)
1283 {
1284 *params = 1;
1285 }
1286 else
1287 {
1288 *params = 0;
1289 }
1290 break;
1291 case GL_SAMPLES:
1292 *params = framebuffer->getSamples();
1293 break;
1294 }
1295 }
1296 else
1297 {
1298 *params = 0;
1299 }
1300 }
1301 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001302 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1303 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1304 case GL_MAX_VIEWPORT_DIMS:
1305 {
1306 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1307 params[0] = maxDimension;
1308 params[1] = maxDimension;
1309 }
1310 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001311 case GL_COMPRESSED_TEXTURE_FORMATS:
1312 {
1313 if (supportsCompressedTextures())
1314 {
1315 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1316 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1317 }
1318 }
1319 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 params[0] = mState.viewportX;
1322 params[1] = mState.viewportY;
1323 params[2] = mState.viewportWidth;
1324 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 break;
1326 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001327 params[0] = mState.scissorX;
1328 params[1] = mState.scissorY;
1329 params[2] = mState.scissorWidth;
1330 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001332 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1333 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 case GL_RED_BITS:
1335 case GL_GREEN_BITS:
1336 case GL_BLUE_BITS:
1337 case GL_ALPHA_BITS:
1338 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001339 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001340 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1341
1342 if (colorbuffer)
1343 {
1344 switch (pname)
1345 {
1346 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1347 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1348 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1349 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1350 }
1351 }
1352 else
1353 {
1354 *params = 0;
1355 }
1356 }
1357 break;
1358 case GL_DEPTH_BITS:
1359 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001360 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001361 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001362
1363 if (depthbuffer)
1364 {
1365 *params = depthbuffer->getDepthSize();
1366 }
1367 else
1368 {
1369 *params = 0;
1370 }
1371 }
1372 break;
1373 case GL_STENCIL_BITS:
1374 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001375 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001376 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001377
1378 if (stencilbuffer)
1379 {
1380 *params = stencilbuffer->getStencilSize();
1381 }
1382 else
1383 {
1384 *params = 0;
1385 }
1386 }
1387 break;
1388 case GL_TEXTURE_BINDING_2D:
1389 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001390 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 {
1392 error(GL_INVALID_OPERATION);
1393 return false;
1394 }
1395
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001396 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001397 }
1398 break;
1399 case GL_TEXTURE_BINDING_CUBE_MAP:
1400 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001401 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 {
1403 error(GL_INVALID_OPERATION);
1404 return false;
1405 }
1406
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001407 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001408 }
1409 break;
1410 default:
1411 return false;
1412 }
1413
1414 return true;
1415}
1416
1417bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1418{
1419 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1420 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1421 // to the fact that it is stored internally as a float, and so would require conversion
1422 // if returned from Context::getIntegerv. Since this conversion is already implemented
1423 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1424 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1425 // application.
1426 switch (pname)
1427 {
1428 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1429 case GL_SHADER_BINARY_FORMATS:
1430 {
1431 *type = GL_INT;
1432 *numParams = 0;
1433 }
1434 break;
1435 case GL_MAX_VERTEX_ATTRIBS:
1436 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1437 case GL_MAX_VARYING_VECTORS:
1438 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1439 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1440 case GL_MAX_TEXTURE_IMAGE_UNITS:
1441 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1442 case GL_MAX_RENDERBUFFER_SIZE:
1443 case GL_NUM_SHADER_BINARY_FORMATS:
1444 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1445 case GL_ARRAY_BUFFER_BINDING:
1446 case GL_FRAMEBUFFER_BINDING:
1447 case GL_RENDERBUFFER_BINDING:
1448 case GL_CURRENT_PROGRAM:
1449 case GL_PACK_ALIGNMENT:
1450 case GL_UNPACK_ALIGNMENT:
1451 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001452 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001453 case GL_RED_BITS:
1454 case GL_GREEN_BITS:
1455 case GL_BLUE_BITS:
1456 case GL_ALPHA_BITS:
1457 case GL_DEPTH_BITS:
1458 case GL_STENCIL_BITS:
1459 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1460 case GL_CULL_FACE_MODE:
1461 case GL_FRONT_FACE:
1462 case GL_ACTIVE_TEXTURE:
1463 case GL_STENCIL_FUNC:
1464 case GL_STENCIL_VALUE_MASK:
1465 case GL_STENCIL_REF:
1466 case GL_STENCIL_FAIL:
1467 case GL_STENCIL_PASS_DEPTH_FAIL:
1468 case GL_STENCIL_PASS_DEPTH_PASS:
1469 case GL_STENCIL_BACK_FUNC:
1470 case GL_STENCIL_BACK_VALUE_MASK:
1471 case GL_STENCIL_BACK_REF:
1472 case GL_STENCIL_BACK_FAIL:
1473 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1474 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1475 case GL_DEPTH_FUNC:
1476 case GL_BLEND_SRC_RGB:
1477 case GL_BLEND_SRC_ALPHA:
1478 case GL_BLEND_DST_RGB:
1479 case GL_BLEND_DST_ALPHA:
1480 case GL_BLEND_EQUATION_RGB:
1481 case GL_BLEND_EQUATION_ALPHA:
1482 case GL_STENCIL_WRITEMASK:
1483 case GL_STENCIL_BACK_WRITEMASK:
1484 case GL_STENCIL_CLEAR_VALUE:
1485 case GL_SUBPIXEL_BITS:
1486 case GL_MAX_TEXTURE_SIZE:
1487 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1488 case GL_SAMPLE_BUFFERS:
1489 case GL_SAMPLES:
1490 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1491 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1492 case GL_TEXTURE_BINDING_2D:
1493 case GL_TEXTURE_BINDING_CUBE_MAP:
1494 {
1495 *type = GL_INT;
1496 *numParams = 1;
1497 }
1498 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001499 case GL_MAX_SAMPLES_ANGLE:
1500 {
1501 if (getMaxSupportedSamples() != 0)
1502 {
1503 *type = GL_INT;
1504 *numParams = 1;
1505 }
1506 else
1507 {
1508 return false;
1509 }
1510 }
1511 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001512 case GL_MAX_VIEWPORT_DIMS:
1513 {
1514 *type = GL_INT;
1515 *numParams = 2;
1516 }
1517 break;
1518 case GL_VIEWPORT:
1519 case GL_SCISSOR_BOX:
1520 {
1521 *type = GL_INT;
1522 *numParams = 4;
1523 }
1524 break;
1525 case GL_SHADER_COMPILER:
1526 case GL_SAMPLE_COVERAGE_INVERT:
1527 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001528 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1529 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1530 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1531 case GL_SAMPLE_COVERAGE:
1532 case GL_SCISSOR_TEST:
1533 case GL_STENCIL_TEST:
1534 case GL_DEPTH_TEST:
1535 case GL_BLEND:
1536 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001537 {
1538 *type = GL_BOOL;
1539 *numParams = 1;
1540 }
1541 break;
1542 case GL_COLOR_WRITEMASK:
1543 {
1544 *type = GL_BOOL;
1545 *numParams = 4;
1546 }
1547 break;
1548 case GL_POLYGON_OFFSET_FACTOR:
1549 case GL_POLYGON_OFFSET_UNITS:
1550 case GL_SAMPLE_COVERAGE_VALUE:
1551 case GL_DEPTH_CLEAR_VALUE:
1552 case GL_LINE_WIDTH:
1553 {
1554 *type = GL_FLOAT;
1555 *numParams = 1;
1556 }
1557 break;
1558 case GL_ALIASED_LINE_WIDTH_RANGE:
1559 case GL_ALIASED_POINT_SIZE_RANGE:
1560 case GL_DEPTH_RANGE:
1561 {
1562 *type = GL_FLOAT;
1563 *numParams = 2;
1564 }
1565 break;
1566 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001567 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 {
1569 *type = GL_FLOAT;
1570 *numParams = 4;
1571 }
1572 break;
1573 default:
1574 return false;
1575 }
1576
1577 return true;
1578}
1579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580// Applies the render target surface, depth stencil surface, viewport rectangle and
1581// scissor rectangle to the Direct3D 9 device
1582bool Context::applyRenderTarget(bool ignoreViewport)
1583{
1584 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001585
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001586 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
1588 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1589 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001590 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1591
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 return false;
1593 }
1594
1595 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001596
1597 if (!renderTarget)
1598 {
1599 return false; // Context must be lost
1600 }
1601
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001602 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001604 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1605 if (renderTargetSerial != mAppliedRenderTargetSerial)
1606 {
1607 device->SetRenderTarget(0, renderTarget);
1608 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001609 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 +00001610 }
1611
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001612 unsigned int depthbufferSerial = 0;
1613 unsigned int stencilbufferSerial = 0;
1614 if (framebufferObject->getDepthbufferType() != GL_NONE)
1615 {
1616 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001617 if (!depthStencil)
1618 {
1619 ERR("Depth stencil pointer unexpectedly null.");
1620 return false;
1621 }
1622
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001623 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1624 }
1625 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1626 {
1627 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001628 if (!depthStencil)
1629 {
1630 ERR("Depth stencil pointer unexpectedly null.");
1631 return false;
1632 }
1633
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001634 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1635 }
1636
1637 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001638 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001639 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001640 {
1641 device->SetDepthStencilSurface(depthStencil);
1642 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001643 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001644 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001645 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
1647 D3DVIEWPORT9 viewport;
1648 D3DSURFACE_DESC desc;
1649 renderTarget->GetDesc(&desc);
1650
1651 if (ignoreViewport)
1652 {
1653 viewport.X = 0;
1654 viewport.Y = 0;
1655 viewport.Width = desc.Width;
1656 viewport.Height = desc.Height;
1657 viewport.MinZ = 0.0f;
1658 viewport.MaxZ = 1.0f;
1659 }
1660 else
1661 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001662 viewport.X = std::max(mState.viewportX, 0);
1663 viewport.Y = std::max(mState.viewportY, 0);
1664 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1665 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1666 viewport.MinZ = clamp01(mState.zNear);
1667 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 }
1669
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001670 if (viewport.Width <= 0 || viewport.Height <= 0)
1671 {
1672 return false; // Nothing to render
1673 }
1674
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 device->SetViewport(&viewport);
1676
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001677 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001679 if (mState.scissorTest)
1680 {
1681 RECT rect = {mState.scissorX,
1682 mState.scissorY,
1683 mState.scissorX + mState.scissorWidth,
1684 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001685 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1686 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001687 device->SetScissorRect(&rect);
1688 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1689 }
1690 else
1691 {
1692 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1693 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001694
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001695 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 }
1697
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001698 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 Program *programObject = getCurrentProgram();
1701
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001702 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001703 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001705
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001706 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001707 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1708 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1709 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001710 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1711
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001712 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001713 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001714 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1715
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001716 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001717 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001718
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001719 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001720 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001721
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001722 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001723 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001724 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725 }
1726
1727 return true;
1728}
1729
1730// 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 +00001731void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732{
1733 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001734 Program *programObject = getCurrentProgram();
1735
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001736 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001737 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001738 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001740 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001741 GLint alwaysFront = !isTriangleMode(drawMode);
1742 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1743
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001744 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001745
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001746 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 }
1752 else
1753 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 }
1756
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 mCullStateDirty = false;
1758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 if (mDepthStateDirty)
1761 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001762 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001764 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1765 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 }
1767 else
1768 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771
1772 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 }
1774
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001775 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001777 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001778 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001779 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1780
1781 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1782 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1783 {
1784 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1785 }
1786 else
1787 {
1788 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1789 unorm<8>(mState.blendColor.alpha),
1790 unorm<8>(mState.blendColor.alpha),
1791 unorm<8>(mState.blendColor.alpha)));
1792 }
1793
1794 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1795 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1796 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1797
1798 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1799 mState.destBlendRGB != mState.destBlendAlpha ||
1800 mState.blendEquationRGB != mState.blendEquationAlpha)
1801 {
1802 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1803
1804 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1805 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1806 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1807
1808 }
1809 else
1810 {
1811 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1812 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001813 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001815 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001816 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001817 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001818
1819 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 }
1821
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001822 if (mStencilStateDirty || mFrontFaceDirty)
1823 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001824 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001825 {
1826 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1827 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1828
1829 // FIXME: Unsupported by D3D9
1830 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1831 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1832 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1833 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1834 mState.stencilRef != mState.stencilBackRef ||
1835 mState.stencilMask != mState.stencilBackMask)
1836 {
1837 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1838 return error(GL_INVALID_OPERATION);
1839 }
1840
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001841 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001842 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001843 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1844
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001845 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1846 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1847 es2dx::ConvertComparison(mState.stencilFunc));
1848
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001849 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 +00001850 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1851
1852 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1853 es2dx::ConvertStencilOp(mState.stencilFail));
1854 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1855 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1856 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1857 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1858
1859 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1860 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1861 es2dx::ConvertComparison(mState.stencilBackFunc));
1862
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001863 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 +00001864 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1865
1866 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1867 es2dx::ConvertStencilOp(mState.stencilBackFail));
1868 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1869 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1870 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1871 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1872 }
1873 else
1874 {
1875 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1876 }
1877
1878 mStencilStateDirty = false;
1879 }
1880
1881 if (mMaskStateDirty)
1882 {
1883 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1884 mState.colorMaskBlue, mState.colorMaskAlpha));
1885 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1886
1887 mMaskStateDirty = false;
1888 }
1889
1890 if (mPolygonOffsetStateDirty)
1891 {
1892 if (mState.polygonOffsetFill)
1893 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001894 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001895 if (depthbuffer)
1896 {
1897 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1898 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1899 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1900 }
1901 }
1902 else
1903 {
1904 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1905 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1906 }
1907
1908 mPolygonOffsetStateDirty = false;
1909 }
1910
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001911 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001913 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001914 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001915 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001917 FIXME("Sample alpha to coverage is unimplemented.");
1918 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001919
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001920 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1921 if (mState.sampleCoverage)
1922 {
1923 unsigned int mask = 0;
1924 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001925 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001926 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001927
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001928 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001929 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001930 mask <<= 1;
1931
1932 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1933 {
1934 threshold += 1.0f;
1935 mask |= 1;
1936 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001937 }
1938 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001939
1940 if (mState.sampleCoverageInvert)
1941 {
1942 mask = ~mask;
1943 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001944
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001945 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1946 }
1947 else
1948 {
1949 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1950 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001951 }
1952 else
1953 {
1954 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001955 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956
1957 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958 }
1959
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001960 if (mDitherStateDirty)
1961 {
1962 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1963
1964 mDitherStateDirty = false;
1965 }
1966
1967 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968}
1969
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001970// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001971void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001973 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001975 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001977 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978 }
1979 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001980}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001982GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983{
1984 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1985
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001986 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001987 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001988 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001989 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001990 }
1991
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001992 lookupAttributeMapping(translated);
1993
1994 mBufferBackEnd->setupAttributesPreDraw(translated);
1995
1996 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1997 {
1998 if (translated[i].enabled && translated[i].nonArray)
1999 {
2000 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
2001 if (err != GL_NO_ERROR)
2002 {
2003 return err;
2004 }
2005
2006 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2007
2008 *useIndexing = true;
2009 return GL_NO_ERROR;
2010 }
2011 }
2012
2013 *useIndexing = false;
2014 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002015}
2016
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002017GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002018{
2019 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2020
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002021 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002022
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002023 if (err == GL_NO_ERROR)
2024 {
2025 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002026
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002027 mBufferBackEnd->setupAttributesPreDraw(translated);
2028 }
2029
2030 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002031}
2032
2033// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002034GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002035{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002036 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002037
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002038 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002039 {
2040 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2041 }
2042
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002043 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044}
2045
2046// Applies the shaders and shader constants to the Direct3D 9 device
2047void Context::applyShaders()
2048{
2049 IDirect3DDevice9 *device = getDevice();
2050 Program *programObject = getCurrentProgram();
2051 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2052 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2053
2054 device->SetVertexShader(vertexShader);
2055 device->SetPixelShader(pixelShader);
2056
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002057 if (programObject->getSerial() != mAppliedProgram)
2058 {
2059 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002060 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002061 mAppliedProgram = programObject->getSerial();
2062 }
2063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 programObject->applyUniforms();
2065}
2066
2067// Applies the textures and sampler states to the Direct3D 9 device
2068void Context::applyTextures()
2069{
2070 IDirect3DDevice9 *device = getDevice();
2071 Program *programObject = getCurrentProgram();
2072
2073 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2074 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002075 int textureUnit = programObject->getSamplerMapping(sampler);
2076 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002078 SamplerType textureType = programObject->getSamplerType(sampler);
2079
2080 Texture *texture = getSamplerTexture(textureUnit, textureType);
2081
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002082 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002083 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002084 if (texture->isComplete())
2085 {
2086 GLenum wrapS = texture->getWrapS();
2087 GLenum wrapT = texture->getWrapT();
2088 GLenum minFilter = texture->getMinFilter();
2089 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002091 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2092 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002094 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2095 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2096 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2097 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2098 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002100 device->SetTexture(sampler, texture->getTexture());
2101 }
2102 else
2103 {
2104 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2105 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002106 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002107
2108 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002110 else
2111 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002112 if (programObject->isSamplerDirty(sampler))
2113 {
2114 device->SetTexture(sampler, NULL);
2115 programObject->setSamplerDirty(sampler, false);
2116 }
2117 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118 }
2119}
2120
2121void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2122{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002123 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002124
2125 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2126 {
2127 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2128 }
2129
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002130 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2131 {
2132 return error(GL_INVALID_OPERATION);
2133 }
2134
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002136
2137 if (!renderTarget)
2138 {
2139 return; // Context must be lost, return silently
2140 }
2141
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 IDirect3DDevice9 *device = getDevice();
2143
2144 D3DSURFACE_DESC desc;
2145 renderTarget->GetDesc(&desc);
2146
2147 IDirect3DSurface9 *systemSurface;
2148 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2149
2150 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2151 {
2152 return error(GL_OUT_OF_MEMORY);
2153 }
2154
2155 ASSERT(SUCCEEDED(result));
2156
2157 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2158 {
2159 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2160 }
2161
2162 result = device->GetRenderTargetData(renderTarget, systemSurface);
2163
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002164 if (FAILED(result))
2165 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 systemSurface->Release();
2167
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002168 switch (result)
2169 {
2170 case D3DERR_DRIVERINTERNALERROR:
2171 case D3DERR_DEVICELOST:
2172 return error(GL_OUT_OF_MEMORY);
2173 default:
2174 UNREACHABLE();
2175 return; // No sensible error to generate
2176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 }
2178
2179 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002180 RECT rect = {std::max(x, 0),
2181 std::max(y, 0),
2182 std::min(x + width, (int)desc.Width),
2183 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184
2185 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2186
2187 if (FAILED(result))
2188 {
2189 UNREACHABLE();
2190 systemSurface->Release();
2191
2192 return; // No sensible error to generate
2193 }
2194
2195 unsigned char *source = (unsigned char*)lock.pBits;
2196 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002197 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002199 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 for (int j = 0; j < rect.bottom - rect.top; j++)
2202 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002203 if (desc.Format == D3DFMT_A8R8G8B8 &&
2204 format == GL_BGRA_EXT &&
2205 type == GL_UNSIGNED_BYTE)
2206 {
2207 // Fast path for EXT_read_format_bgra, given
2208 // an RGBA source buffer. Note that buffers with no
2209 // alpha go through the slow path below.
2210 memcpy(dest + j * outputPitch,
2211 source + j * lock.Pitch,
2212 (rect.right - rect.left) * 4);
2213 continue;
2214 }
2215
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 for (int i = 0; i < rect.right - rect.left; i++)
2217 {
2218 float r;
2219 float g;
2220 float b;
2221 float a;
2222
2223 switch (desc.Format)
2224 {
2225 case D3DFMT_R5G6B5:
2226 {
2227 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2228
2229 a = 1.0f;
2230 b = (rgb & 0x001F) * (1.0f / 0x001F);
2231 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2232 r = (rgb & 0xF800) * (1.0f / 0xF800);
2233 }
2234 break;
2235 case D3DFMT_X1R5G5B5:
2236 {
2237 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2238
2239 a = 1.0f;
2240 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2241 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2242 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2243 }
2244 break;
2245 case D3DFMT_A1R5G5B5:
2246 {
2247 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2248
2249 a = (argb & 0x8000) ? 1.0f : 0.0f;
2250 b = (argb & 0x001F) * (1.0f / 0x001F);
2251 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2252 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2253 }
2254 break;
2255 case D3DFMT_A8R8G8B8:
2256 {
2257 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2258
2259 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2260 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2261 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2262 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2263 }
2264 break;
2265 case D3DFMT_X8R8G8B8:
2266 {
2267 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2268
2269 a = 1.0f;
2270 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2271 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2272 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2273 }
2274 break;
2275 case D3DFMT_A2R10G10B10:
2276 {
2277 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2278
2279 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2280 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2281 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2282 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2283 }
2284 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002285 case D3DFMT_A32B32G32R32F:
2286 {
2287 // float formats in D3D are stored rgba, rather than the other way round
2288 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2289 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2290 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2291 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2292 }
2293 break;
2294 case D3DFMT_A16B16G16R16F:
2295 {
2296 // float formats in D3D are stored rgba, rather than the other way round
2297 float abgr[4];
2298
2299 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2300
2301 a = abgr[3];
2302 b = abgr[2];
2303 g = abgr[1];
2304 r = abgr[0];
2305 }
2306 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 default:
2308 UNIMPLEMENTED(); // FIXME
2309 UNREACHABLE();
2310 }
2311
2312 switch (format)
2313 {
2314 case GL_RGBA:
2315 switch (type)
2316 {
2317 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002318 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2319 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2320 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2321 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 break;
2323 default: UNREACHABLE();
2324 }
2325 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002326 case GL_BGRA_EXT:
2327 switch (type)
2328 {
2329 case GL_UNSIGNED_BYTE:
2330 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2331 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2332 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2333 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2334 break;
2335 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2336 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2337 // this type is packed as follows:
2338 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2339 // --------------------------------------------------------------------------------
2340 // | 4th | 3rd | 2nd | 1st component |
2341 // --------------------------------------------------------------------------------
2342 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2343 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2344 ((unsigned short)(15 * a + 0.5f) << 12)|
2345 ((unsigned short)(15 * r + 0.5f) << 8) |
2346 ((unsigned short)(15 * g + 0.5f) << 4) |
2347 ((unsigned short)(15 * b + 0.5f) << 0);
2348 break;
2349 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2350 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2351 // this type is packed as follows:
2352 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2353 // --------------------------------------------------------------------------------
2354 // | 4th | 3rd | 2nd | 1st component |
2355 // --------------------------------------------------------------------------------
2356 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2357 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2358 ((unsigned short)( a + 0.5f) << 15) |
2359 ((unsigned short)(31 * r + 0.5f) << 10) |
2360 ((unsigned short)(31 * g + 0.5f) << 5) |
2361 ((unsigned short)(31 * b + 0.5f) << 0);
2362 break;
2363 default: UNREACHABLE();
2364 }
2365 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002366 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367 switch (type)
2368 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002369 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002370 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2371 ((unsigned short)(31 * b + 0.5f) << 0) |
2372 ((unsigned short)(63 * g + 0.5f) << 5) |
2373 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 break;
2375 default: UNREACHABLE();
2376 }
2377 break;
2378 default: UNREACHABLE();
2379 }
2380 }
2381 }
2382
2383 systemSurface->UnlockRect();
2384
2385 systemSurface->Release();
2386}
2387
2388void Context::clear(GLbitfield mask)
2389{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002390 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002391
2392 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2393 {
2394 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2395
2396 return;
2397 }
2398
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002399 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 IDirect3DDevice9 *device = getDevice();
2401 DWORD flags = 0;
2402
2403 if (mask & GL_COLOR_BUFFER_BIT)
2404 {
2405 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002406
2407 if (framebufferObject->getColorbufferType() != GL_NONE)
2408 {
2409 flags |= D3DCLEAR_TARGET;
2410 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 }
2412
2413 if (mask & GL_DEPTH_BUFFER_BIT)
2414 {
2415 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002416 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 {
2418 flags |= D3DCLEAR_ZBUFFER;
2419 }
2420 }
2421
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 GLuint stencilUnmasked = 0x0;
2423
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002424 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002427 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002429 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002430 if (!depthStencil)
2431 {
2432 ERR("Depth stencil pointer unexpectedly null.");
2433 return;
2434 }
2435
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002436 D3DSURFACE_DESC desc;
2437 depthStencil->GetDesc(&desc);
2438
2439 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2440 stencilUnmasked = (0x1 << stencilSize) - 1;
2441
2442 if (stencilUnmasked != 0x0)
2443 {
2444 flags |= D3DCLEAR_STENCIL;
2445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 }
2447 }
2448
2449 if (mask != 0)
2450 {
2451 return error(GL_INVALID_VALUE);
2452 }
2453
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002454 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2455 {
2456 return;
2457 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002459 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2460 unorm<8>(mState.colorClearValue.red),
2461 unorm<8>(mState.colorClearValue.green),
2462 unorm<8>(mState.colorClearValue.blue));
2463 float depth = clamp01(mState.depthClearValue);
2464 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465
2466 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2467
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002468 if (!renderTarget)
2469 {
2470 return; // Context must be lost, return silently
2471 }
2472
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 D3DSURFACE_DESC desc;
2474 renderTarget->GetDesc(&desc);
2475
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002476 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477
2478 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002479 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002481 !(mState.colorMaskRed && mState.colorMaskGreen &&
2482 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483
2484 if (needMaskedColorClear || needMaskedStencilClear)
2485 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002486 // State which is altered in all paths from this point to the clear call is saved.
2487 // State which is altered in only some paths will be flagged dirty in the case that
2488 // that path is taken.
2489 HRESULT hr;
2490 if (mMaskedClearSavedState == NULL)
2491 {
2492 hr = device->BeginStateBlock();
2493 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2494
2495 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2496 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2497 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2498 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2499 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2500 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2501 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2502 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2503 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2504 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2505 device->SetPixelShader(NULL);
2506 device->SetVertexShader(NULL);
2507 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2508 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002509 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002510
2511 hr = device->EndStateBlock(&mMaskedClearSavedState);
2512 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2513 }
2514
2515 ASSERT(mMaskedClearSavedState != NULL);
2516
2517 if (mMaskedClearSavedState != NULL)
2518 {
2519 hr = mMaskedClearSavedState->Capture();
2520 ASSERT(SUCCEEDED(hr));
2521 }
2522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2524 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2525 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2526 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2527 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2528 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2529 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2530 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2531
2532 if (flags & D3DCLEAR_TARGET)
2533 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002534 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2535 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2536 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2537 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 }
2539 else
2540 {
2541 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2542 }
2543
2544 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2545 {
2546 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2547 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2548 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2549 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002550 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002551 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2553 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002554 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 }
2556 else
2557 {
2558 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2559 }
2560
2561 device->SetPixelShader(NULL);
2562 device->SetVertexShader(NULL);
2563 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002564 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002565 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002567 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568 {
2569 float x, y, z, w;
2570 D3DCOLOR diffuse;
2571 };
2572
2573 Vertex quad[4];
2574 quad[0].x = 0.0f;
2575 quad[0].y = (float)desc.Height;
2576 quad[0].z = 0.0f;
2577 quad[0].w = 1.0f;
2578 quad[0].diffuse = color;
2579
2580 quad[1].x = (float)desc.Width;
2581 quad[1].y = (float)desc.Height;
2582 quad[1].z = 0.0f;
2583 quad[1].w = 1.0f;
2584 quad[1].diffuse = color;
2585
2586 quad[2].x = 0.0f;
2587 quad[2].y = 0.0f;
2588 quad[2].z = 0.0f;
2589 quad[2].w = 1.0f;
2590 quad[2].diffuse = color;
2591
2592 quad[3].x = (float)desc.Width;
2593 quad[3].y = 0.0f;
2594 quad[3].z = 0.0f;
2595 quad[3].w = 1.0f;
2596 quad[3].diffuse = color;
2597
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002598 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600
2601 if (flags & D3DCLEAR_ZBUFFER)
2602 {
2603 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2604 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2605 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2606 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002607
2608 if (mMaskedClearSavedState != NULL)
2609 {
2610 mMaskedClearSavedState->Apply();
2611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002613 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 {
2615 device->Clear(0, NULL, flags, color, depth, stencil);
2616 }
2617}
2618
2619void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2620{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002621 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622 {
2623 return error(GL_INVALID_OPERATION);
2624 }
2625
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002626 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 IDirect3DDevice9 *device = getDevice();
2628 D3DPRIMITIVETYPE primitiveType;
2629 int primitiveCount;
2630
2631 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2632 return error(GL_INVALID_ENUM);
2633
2634 if (primitiveCount <= 0)
2635 {
2636 return;
2637 }
2638
2639 if (!applyRenderTarget(false))
2640 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002641 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 }
2643
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002644 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002645
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002646 TranslatedIndexData indexInfo;
2647 bool useIndexing;
2648 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002649 if (err != GL_NO_ERROR)
2650 {
2651 return error(err);
2652 }
2653
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002654 applyShaders();
2655 applyTextures();
2656
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002657 if (!getCurrentProgram()->validateSamplers())
2658 {
2659 return error(GL_INVALID_OPERATION);
2660 }
2661
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002662 if (!cullSkipsDraw(mode))
2663 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002664 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002665 if (useIndexing)
2666 {
2667 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2668 }
2669 else
2670 {
2671 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2672 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002673 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674}
2675
2676void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2677{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002678 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 {
2680 return error(GL_INVALID_OPERATION);
2681 }
2682
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002683 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684 {
2685 return error(GL_INVALID_OPERATION);
2686 }
2687
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002688 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002689 IDirect3DDevice9 *device = getDevice();
2690 D3DPRIMITIVETYPE primitiveType;
2691 int primitiveCount;
2692
2693 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2694 return error(GL_INVALID_ENUM);
2695
2696 if (primitiveCount <= 0)
2697 {
2698 return;
2699 }
2700
2701 if (!applyRenderTarget(false))
2702 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002703 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002704 }
2705
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002706 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002707
2708 TranslatedIndexData indexInfo;
2709 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2710 if (err != GL_NO_ERROR)
2711 {
2712 return error(err);
2713 }
2714
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002715 err = applyVertexBuffer(indexInfo);
2716 if (err != GL_NO_ERROR)
2717 {
2718 return error(err);
2719 }
2720
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 applyShaders();
2722 applyTextures();
2723
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002724 if (!getCurrentProgram()->validateSamplers())
2725 {
2726 return error(GL_INVALID_OPERATION);
2727 }
2728
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002729 if (!cullSkipsDraw(mode))
2730 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002731 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002732 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 +00002733 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734}
2735
2736void Context::finish()
2737{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002738 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739 IDirect3DDevice9 *device = getDevice();
2740 IDirect3DQuery9 *occlusionQuery = NULL;
2741
2742 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2743
2744 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2745 {
2746 return error(GL_OUT_OF_MEMORY);
2747 }
2748
2749 ASSERT(SUCCEEDED(result));
2750
2751 if (occlusionQuery)
2752 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002753 IDirect3DStateBlock9 *savedState = NULL;
2754 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2755
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002756 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2757 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758
2759 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002760 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761 device->SetPixelShader(NULL);
2762 device->SetVertexShader(NULL);
2763 device->SetFVF(D3DFVF_XYZRHW);
2764 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002765 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002768 result = occlusionQuery->Issue(D3DISSUE_END);
2769 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770
2771 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2772 {
2773 // Keep polling, but allow other threads to do something useful first
2774 Sleep(0);
2775 }
2776
2777 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002778
2779 if (savedState)
2780 {
2781 savedState->Apply();
2782 savedState->Release();
2783 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 }
2785}
2786
2787void Context::flush()
2788{
2789 IDirect3DDevice9 *device = getDevice();
2790 IDirect3DQuery9 *eventQuery = NULL;
2791
2792 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2793
2794 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2795 {
2796 return error(GL_OUT_OF_MEMORY);
2797 }
2798
2799 ASSERT(SUCCEEDED(result));
2800
2801 if (eventQuery)
2802 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002803 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2804 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002806 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002808
2809 if (result == D3DERR_DEVICELOST)
2810 {
2811 error(GL_OUT_OF_MEMORY);
2812 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 }
2814}
2815
2816void Context::recordInvalidEnum()
2817{
2818 mInvalidEnum = true;
2819}
2820
2821void Context::recordInvalidValue()
2822{
2823 mInvalidValue = true;
2824}
2825
2826void Context::recordInvalidOperation()
2827{
2828 mInvalidOperation = true;
2829}
2830
2831void Context::recordOutOfMemory()
2832{
2833 mOutOfMemory = true;
2834}
2835
2836void Context::recordInvalidFramebufferOperation()
2837{
2838 mInvalidFramebufferOperation = true;
2839}
2840
2841// Get one of the recorded errors and clear its flag, if any.
2842// [OpenGL ES 2.0.24] section 2.5 page 13.
2843GLenum Context::getError()
2844{
2845 if (mInvalidEnum)
2846 {
2847 mInvalidEnum = false;
2848
2849 return GL_INVALID_ENUM;
2850 }
2851
2852 if (mInvalidValue)
2853 {
2854 mInvalidValue = false;
2855
2856 return GL_INVALID_VALUE;
2857 }
2858
2859 if (mInvalidOperation)
2860 {
2861 mInvalidOperation = false;
2862
2863 return GL_INVALID_OPERATION;
2864 }
2865
2866 if (mOutOfMemory)
2867 {
2868 mOutOfMemory = false;
2869
2870 return GL_OUT_OF_MEMORY;
2871 }
2872
2873 if (mInvalidFramebufferOperation)
2874 {
2875 mInvalidFramebufferOperation = false;
2876
2877 return GL_INVALID_FRAMEBUFFER_OPERATION;
2878 }
2879
2880 return GL_NO_ERROR;
2881}
2882
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002883bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002884{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002885 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002886}
2887
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002888int Context::getMaxSupportedSamples() const
2889{
2890 return mMaxSupportedSamples;
2891}
2892
2893int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2894{
2895 if (requested == 0)
2896 {
2897 return requested;
2898 }
2899
2900 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2901 if (itr == mMultiSampleSupport.end())
2902 {
2903 return -1;
2904 }
2905
2906 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2907 {
2908 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2909 {
2910 return i;
2911 }
2912 }
2913
2914 return -1;
2915}
2916
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002917bool Context::supportsEventQueries() const
2918{
2919 return mSupportsEventQueries;
2920}
2921
daniel@transgaming.com01868132010-08-24 19:21:17 +00002922bool Context::supportsCompressedTextures() const
2923{
2924 return mSupportsCompressedTextures;
2925}
2926
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002927bool Context::supportsFloatTextures() const
2928{
2929 return mSupportsFloatTextures;
2930}
2931
2932bool Context::supportsFloatLinearFilter() const
2933{
2934 return mSupportsFloatLinearFilter;
2935}
2936
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002937bool Context::supportsFloatRenderableTextures() const
2938{
2939 return mSupportsFloatRenderableTextures;
2940}
2941
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002942bool Context::supportsHalfFloatTextures() const
2943{
2944 return mSupportsHalfFloatTextures;
2945}
2946
2947bool Context::supportsHalfFloatLinearFilter() const
2948{
2949 return mSupportsHalfFloatLinearFilter;
2950}
2951
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002952bool Context::supportsHalfFloatRenderableTextures() const
2953{
2954 return mSupportsHalfFloatRenderableTextures;
2955}
2956
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002957void Context::detachBuffer(GLuint buffer)
2958{
2959 // [OpenGL ES 2.0.24] section 2.9 page 22:
2960 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2961 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2962
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002963 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002964 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002965 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002966 }
2967
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002968 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002969 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002970 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002971 }
2972
2973 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2974 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002975 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002976 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002977 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002978 }
2979 }
2980}
2981
2982void Context::detachTexture(GLuint texture)
2983{
2984 // [OpenGL ES 2.0.24] section 3.8 page 84:
2985 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2986 // rebound to texture object zero
2987
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002988 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002990 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002992 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002993 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002994 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002995 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002996 }
2997 }
2998
2999 // [OpenGL ES 2.0.24] section 4.4 page 112:
3000 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3001 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3002 // image was attached in the currently bound framebuffer.
3003
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003004 Framebuffer *readFramebuffer = getReadFramebuffer();
3005 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003006
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003007 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003008 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003009 readFramebuffer->detachTexture(texture);
3010 }
3011
3012 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3013 {
3014 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015 }
3016}
3017
3018void Context::detachFramebuffer(GLuint framebuffer)
3019{
3020 // [OpenGL ES 2.0.24] section 4.4 page 107:
3021 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3022 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3023
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003024 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003025 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003026 bindReadFramebuffer(0);
3027 }
3028
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003029 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003030 {
3031 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003032 }
3033}
3034
3035void Context::detachRenderbuffer(GLuint renderbuffer)
3036{
3037 // [OpenGL ES 2.0.24] section 4.4 page 109:
3038 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3039 // had been executed with the target RENDERBUFFER and name of zero.
3040
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003041 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003042 {
3043 bindRenderbuffer(0);
3044 }
3045
3046 // [OpenGL ES 2.0.24] section 4.4 page 111:
3047 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3048 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3049 // point to which this image was attached in the currently bound framebuffer.
3050
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003051 Framebuffer *readFramebuffer = getReadFramebuffer();
3052 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003054 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003055 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003056 readFramebuffer->detachRenderbuffer(renderbuffer);
3057 }
3058
3059 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3060 {
3061 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062 }
3063}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003064
3065Texture *Context::getIncompleteTexture(SamplerType type)
3066{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003067 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003068
3069 if (t == NULL)
3070 {
3071 static const GLubyte color[] = { 0, 0, 0, 255 };
3072
3073 switch (type)
3074 {
3075 default:
3076 UNREACHABLE();
3077 // default falls through to SAMPLER_2D
3078
3079 case SAMPLER_2D:
3080 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003081 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003082 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003083 t = incomplete2d;
3084 }
3085 break;
3086
3087 case SAMPLER_CUBE:
3088 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003089 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003090
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003091 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3092 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3093 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3094 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3095 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3096 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003097
3098 t = incompleteCube;
3099 }
3100 break;
3101 }
3102
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003103 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003104 }
3105
3106 return t;
3107}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003108
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003109bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003110{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003111 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003112}
3113
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003114bool Context::isTriangleMode(GLenum drawMode)
3115{
3116 switch (drawMode)
3117 {
3118 case GL_TRIANGLES:
3119 case GL_TRIANGLE_FAN:
3120 case GL_TRIANGLE_STRIP:
3121 return true;
3122 case GL_POINTS:
3123 case GL_LINES:
3124 case GL_LINE_LOOP:
3125 case GL_LINE_STRIP:
3126 return false;
3127 default: UNREACHABLE();
3128 }
3129
3130 return false;
3131}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003132
3133void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3134{
3135 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3136
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003137 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3138 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3139 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3140 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003141
3142 mVertexDataManager->dirtyCurrentValues();
3143}
3144
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003145void Context::initExtensionString()
3146{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003147 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003148 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3149 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003150 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003151 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003152 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003153
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003154 if (supportsEventQueries())
3155 {
3156 mExtensionString += "GL_NV_fence ";
3157 }
3158
daniel@transgaming.com01868132010-08-24 19:21:17 +00003159 if (supportsCompressedTextures())
3160 {
3161 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3162 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003163
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003164 if (supportsFloatTextures())
3165 {
3166 mExtensionString += "GL_OES_texture_float ";
3167 }
3168
3169 if (supportsHalfFloatTextures())
3170 {
3171 mExtensionString += "GL_OES_texture_half_float ";
3172 }
3173
3174 if (supportsFloatLinearFilter())
3175 {
3176 mExtensionString += "GL_OES_texture_float_linear ";
3177 }
3178
3179 if (supportsHalfFloatLinearFilter())
3180 {
3181 mExtensionString += "GL_OES_texture_half_float_linear ";
3182 }
3183
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003184 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003185 {
3186 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3187 }
3188
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003189 if (mBufferBackEnd->supportIntIndices())
3190 {
3191 mExtensionString += "GL_OES_element_index_uint ";
3192 }
3193
3194 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3195 if (end != std::string::npos)
3196 {
3197 mExtensionString.resize(end+1);
3198 }
3199}
3200
3201const char *Context::getExtensionString() const
3202{
3203 return mExtensionString.c_str();
3204}
3205
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003206void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3207 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3208 GLbitfield mask)
3209{
3210 IDirect3DDevice9 *device = getDevice();
3211
3212 Framebuffer *readFramebuffer = getReadFramebuffer();
3213 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3214
3215 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3216 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3217 {
3218 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3219 }
3220
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003221 if (drawFramebuffer->getSamples() != 0)
3222 {
3223 return error(GL_INVALID_OPERATION);
3224 }
3225
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003226 RECT sourceRect;
3227 RECT destRect;
3228
3229 if (srcX0 < srcX1)
3230 {
3231 sourceRect.left = srcX0;
3232 sourceRect.right = srcX1;
3233 destRect.left = dstX0;
3234 destRect.right = dstX1;
3235 }
3236 else
3237 {
3238 sourceRect.left = srcX1;
3239 destRect.left = dstX1;
3240 sourceRect.right = srcX0;
3241 destRect.right = dstX0;
3242 }
3243
3244 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3245 // flip our Y-values here
3246 if (srcY0 < srcY1)
3247 {
3248 sourceRect.bottom = srcY1;
3249 destRect.bottom = dstY1;
3250 sourceRect.top = srcY0;
3251 destRect.top = dstY0;
3252 }
3253 else
3254 {
3255 sourceRect.bottom = srcY0;
3256 destRect.bottom = dstY0;
3257 sourceRect.top = srcY1;
3258 destRect.top = dstY1;
3259 }
3260
3261 RECT sourceScissoredRect = sourceRect;
3262 RECT destScissoredRect = destRect;
3263
3264 if (mState.scissorTest)
3265 {
3266 // Only write to parts of the destination framebuffer which pass the scissor test
3267 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3268 // rect will be checked against scissorY, rather than the bottom.
3269 if (destRect.left < mState.scissorX)
3270 {
3271 int xDiff = mState.scissorX - destRect.left;
3272 destScissoredRect.left = mState.scissorX;
3273 sourceScissoredRect.left += xDiff;
3274 }
3275
3276 if (destRect.right > mState.scissorX + mState.scissorWidth)
3277 {
3278 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3279 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3280 sourceScissoredRect.right -= xDiff;
3281 }
3282
3283 if (destRect.top < mState.scissorY)
3284 {
3285 int yDiff = mState.scissorY - destRect.top;
3286 destScissoredRect.top = mState.scissorY;
3287 sourceScissoredRect.top += yDiff;
3288 }
3289
3290 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3291 {
3292 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3293 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3294 sourceScissoredRect.bottom -= yDiff;
3295 }
3296 }
3297
3298 bool blitRenderTarget = false;
3299 bool blitDepthStencil = false;
3300
3301 RECT sourceTrimmedRect = sourceScissoredRect;
3302 RECT destTrimmedRect = destScissoredRect;
3303
3304 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3305 // the actual draw and read surfaces.
3306 if (sourceTrimmedRect.left < 0)
3307 {
3308 int xDiff = 0 - sourceTrimmedRect.left;
3309 sourceTrimmedRect.left = 0;
3310 destTrimmedRect.left += xDiff;
3311 }
3312
3313 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3314 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3315 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3316 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3317
3318 if (sourceTrimmedRect.right > readBufferWidth)
3319 {
3320 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3321 sourceTrimmedRect.right = readBufferWidth;
3322 destTrimmedRect.right -= xDiff;
3323 }
3324
3325 if (sourceTrimmedRect.top < 0)
3326 {
3327 int yDiff = 0 - sourceTrimmedRect.top;
3328 sourceTrimmedRect.top = 0;
3329 destTrimmedRect.top += yDiff;
3330 }
3331
3332 if (sourceTrimmedRect.bottom > readBufferHeight)
3333 {
3334 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3335 sourceTrimmedRect.bottom = readBufferHeight;
3336 destTrimmedRect.bottom -= yDiff;
3337 }
3338
3339 if (destTrimmedRect.left < 0)
3340 {
3341 int xDiff = 0 - destTrimmedRect.left;
3342 destTrimmedRect.left = 0;
3343 sourceTrimmedRect.left += xDiff;
3344 }
3345
3346 if (destTrimmedRect.right > drawBufferWidth)
3347 {
3348 int xDiff = destTrimmedRect.right - drawBufferWidth;
3349 destTrimmedRect.right = drawBufferWidth;
3350 sourceTrimmedRect.right -= xDiff;
3351 }
3352
3353 if (destTrimmedRect.top < 0)
3354 {
3355 int yDiff = 0 - destTrimmedRect.top;
3356 destTrimmedRect.top = 0;
3357 sourceTrimmedRect.top += yDiff;
3358 }
3359
3360 if (destTrimmedRect.bottom > drawBufferHeight)
3361 {
3362 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3363 destTrimmedRect.bottom = drawBufferHeight;
3364 sourceTrimmedRect.bottom -= yDiff;
3365 }
3366
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003367 bool partialBufferCopy = false;
3368 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3369 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3370 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3371 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3372 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3373 {
3374 partialBufferCopy = true;
3375 }
3376
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003377 if (mask & GL_COLOR_BUFFER_BIT)
3378 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003379 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3380 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3381 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3382 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3383 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003384 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3385 {
3386 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3387 return error(GL_INVALID_OPERATION);
3388 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003389
3390 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3391 {
3392 return error(GL_INVALID_OPERATION);
3393 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003394
3395 blitRenderTarget = true;
3396
3397 }
3398
3399 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3400 {
3401 DepthStencilbuffer *readDSBuffer = NULL;
3402 DepthStencilbuffer *drawDSBuffer = NULL;
3403
3404 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3405 // both a depth and stencil buffer, it will be the same buffer.
3406
3407 if (mask & GL_DEPTH_BUFFER_BIT)
3408 {
3409 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3410 {
3411 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3412 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3413 {
3414 return error(GL_INVALID_OPERATION);
3415 }
3416
3417 blitDepthStencil = true;
3418 readDSBuffer = readFramebuffer->getDepthbuffer();
3419 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3420 }
3421 }
3422
3423 if (mask & GL_STENCIL_BUFFER_BIT)
3424 {
3425 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3426 {
3427 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3428 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3429 {
3430 return error(GL_INVALID_OPERATION);
3431 }
3432
3433 blitDepthStencil = true;
3434 readDSBuffer = readFramebuffer->getStencilbuffer();
3435 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3436 }
3437 }
3438
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003439 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003440 {
3441 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3442 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3443 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003444
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003445 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3446 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003447 {
3448 return error(GL_INVALID_OPERATION);
3449 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003450 }
3451
3452 if (blitRenderTarget || blitDepthStencil)
3453 {
3454 egl::Display *display = getDisplay();
3455 display->endScene();
3456
3457 if (blitRenderTarget)
3458 {
3459 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3460 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3461
3462 if (FAILED(result))
3463 {
3464 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3465 return;
3466 }
3467 }
3468
3469 if (blitDepthStencil)
3470 {
3471 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3472
3473 if (FAILED(result))
3474 {
3475 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3476 return;
3477 }
3478 }
3479 }
3480}
3481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003482}
3483
3484extern "C"
3485{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003486gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003487{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003488 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003489}
3490
3491void glDestroyContext(gl::Context *context)
3492{
3493 delete context;
3494
3495 if (context == gl::getContext())
3496 {
3497 gl::makeCurrent(NULL, NULL, NULL);
3498 }
3499}
3500
3501void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3502{
3503 gl::makeCurrent(context, display, surface);
3504}
3505
3506gl::Context *glGetCurrentContext()
3507{
3508 return gl::getContext();
3509}
3510}