blob: b004a4ad3f79666237016103bfcf2e9806bd833d [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.org55255022010-09-11 02:12:47 +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
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000174 while (!mFenceMap.empty())
175 {
176 deleteFence(mFenceMap.begin()->first);
177 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000178
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000179 while (!mMultiSampleSupport.empty())
180 {
181 delete [] mMultiSampleSupport.begin()->second;
182 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
183 }
184
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000185 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
186 {
187 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
188 {
189 mState.samplerTexture[type][sampler].set(NULL);
190 }
191 }
192
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000193 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
194 {
apatrick@chromium.org55255022010-09-11 02:12:47 +0000195 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000196 }
197
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
199 {
200 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
201 }
202
203 mState.arrayBuffer.set(NULL);
204 mState.elementArrayBuffer.set(NULL);
205 mState.texture2D.set(NULL);
206 mState.textureCubeMap.set(NULL);
207 mState.renderbuffer.set(NULL);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000208 mState.readFramebuffer.set(NULL);
209 mState.drawFramebuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210
apatrick@chromium.org55255022010-09-11 02:12:47 +0000211 mTexture2DZero.set(NULL);
212 mTextureCubeMapZero.set(NULL);
213 mFramebufferZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000215 delete mBufferBackEnd;
216 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000217 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000218 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000219
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000220 if (mMaskedClearSavedState)
221 {
222 mMaskedClearSavedState->Release();
223 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000224
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000225 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
229{
230 IDirect3DDevice9 *device = display->getDevice();
231
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000232 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000233 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000234 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000235
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000236 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000237 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000238 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000239 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000240
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000241 const D3DFORMAT renderBufferFormats[] =
242 {
243 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000244 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000245 D3DFMT_R5G6B5,
246 D3DFMT_D24S8
247 };
248
249 int max = 0;
250 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
251 {
252 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
253 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
254 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
255
256 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
257 {
258 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
259 {
260 max = j;
261 }
262 }
263 }
264
265 mMaxSupportedSamples = max;
266
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000267 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000268 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000269 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
270 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000271
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000272 initExtensionString();
273
274 mState.viewportX = 0;
275 mState.viewportY = 0;
276 mState.viewportWidth = surface->getWidth();
277 mState.viewportHeight = surface->getHeight();
278
279 mState.scissorX = 0;
280 mState.scissorY = 0;
281 mState.scissorWidth = surface->getWidth();
282 mState.scissorHeight = surface->getHeight();
283
284 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000285 }
286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
288 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000289 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000292 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000293 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
295 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000297 if (defaultRenderTarget)
298 {
299 defaultRenderTarget->Release();
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000302 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000304 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000306
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000307 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000308
309 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310}
311
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000312// 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 +0000313void Context::markAllStateDirty()
314{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000315 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000316 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000317 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000318
319 mClearStateDirty = true;
320 mCullStateDirty = true;
321 mDepthStateDirty = true;
322 mMaskStateDirty = true;
323 mBlendStateDirty = true;
324 mStencilStateDirty = true;
325 mPolygonOffsetStateDirty = true;
326 mScissorStateDirty = true;
327 mSampleStateDirty = true;
328 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000329 mFrontFaceDirty = true;
330
331 if (mBufferBackEnd != NULL)
332 {
333 mBufferBackEnd->invalidate();
334 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000335}
336
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337void Context::setClearColor(float red, float green, float blue, float alpha)
338{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000339 mState.colorClearValue.red = red;
340 mState.colorClearValue.green = green;
341 mState.colorClearValue.blue = blue;
342 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343}
344
345void Context::setClearDepth(float depth)
346{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000347 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348}
349
350void Context::setClearStencil(int stencil)
351{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000352 mState.stencilClearValue = stencil;
353}
354
355void Context::setCullFace(bool enabled)
356{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000357 if (mState.cullFace != enabled)
358 {
359 mState.cullFace = enabled;
360 mCullStateDirty = true;
361 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000362}
363
364bool Context::isCullFaceEnabled() const
365{
366 return mState.cullFace;
367}
368
369void Context::setCullMode(GLenum mode)
370{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000371 if (mState.cullMode != mode)
372 {
373 mState.cullMode = mode;
374 mCullStateDirty = true;
375 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000376}
377
378void Context::setFrontFace(GLenum front)
379{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000380 if (mState.frontFace != front)
381 {
382 mState.frontFace = front;
383 mFrontFaceDirty = true;
384 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000385}
386
387void Context::setDepthTest(bool enabled)
388{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000389 if (mState.depthTest != enabled)
390 {
391 mState.depthTest = enabled;
392 mDepthStateDirty = true;
393 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000394}
395
396bool Context::isDepthTestEnabled() const
397{
398 return mState.depthTest;
399}
400
401void Context::setDepthFunc(GLenum depthFunc)
402{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000403 if (mState.depthFunc != depthFunc)
404 {
405 mState.depthFunc = depthFunc;
406 mDepthStateDirty = true;
407 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000408}
409
410void Context::setDepthRange(float zNear, float zFar)
411{
412 mState.zNear = zNear;
413 mState.zFar = zFar;
414}
415
416void Context::setBlend(bool enabled)
417{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000418 if (mState.blend != enabled)
419 {
420 mState.blend = enabled;
421 mBlendStateDirty = true;
422 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000423}
424
425bool Context::isBlendEnabled() const
426{
427 return mState.blend;
428}
429
430void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
431{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000432 if (mState.sourceBlendRGB != sourceRGB ||
433 mState.sourceBlendAlpha != sourceAlpha ||
434 mState.destBlendRGB != destRGB ||
435 mState.destBlendAlpha != destAlpha)
436 {
437 mState.sourceBlendRGB = sourceRGB;
438 mState.destBlendRGB = destRGB;
439 mState.sourceBlendAlpha = sourceAlpha;
440 mState.destBlendAlpha = destAlpha;
441 mBlendStateDirty = true;
442 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000443}
444
445void Context::setBlendColor(float red, float green, float blue, float alpha)
446{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000447 if (mState.blendColor.red != red ||
448 mState.blendColor.green != green ||
449 mState.blendColor.blue != blue ||
450 mState.blendColor.alpha != alpha)
451 {
452 mState.blendColor.red = red;
453 mState.blendColor.green = green;
454 mState.blendColor.blue = blue;
455 mState.blendColor.alpha = alpha;
456 mBlendStateDirty = true;
457 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000458}
459
460void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
461{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000462 if (mState.blendEquationRGB != rgbEquation ||
463 mState.blendEquationAlpha != alphaEquation)
464 {
465 mState.blendEquationRGB = rgbEquation;
466 mState.blendEquationAlpha = alphaEquation;
467 mBlendStateDirty = true;
468 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000469}
470
471void Context::setStencilTest(bool enabled)
472{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000473 if (mState.stencilTest != enabled)
474 {
475 mState.stencilTest = enabled;
476 mStencilStateDirty = true;
477 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000478}
479
480bool Context::isStencilTestEnabled() const
481{
482 return mState.stencilTest;
483}
484
485void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
486{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000487 if (mState.stencilFunc != stencilFunc ||
488 mState.stencilRef != stencilRef ||
489 mState.stencilMask != stencilMask)
490 {
491 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000492 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000493 mState.stencilMask = stencilMask;
494 mStencilStateDirty = true;
495 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000496}
497
498void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
499{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000500 if (mState.stencilBackFunc != stencilBackFunc ||
501 mState.stencilBackRef != stencilBackRef ||
502 mState.stencilBackMask != stencilBackMask)
503 {
504 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000505 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000506 mState.stencilBackMask = stencilBackMask;
507 mStencilStateDirty = true;
508 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000509}
510
511void Context::setStencilWritemask(GLuint stencilWritemask)
512{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000513 if (mState.stencilWritemask != stencilWritemask)
514 {
515 mState.stencilWritemask = stencilWritemask;
516 mStencilStateDirty = true;
517 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000518}
519
520void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
521{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000522 if (mState.stencilBackWritemask != stencilBackWritemask)
523 {
524 mState.stencilBackWritemask = stencilBackWritemask;
525 mStencilStateDirty = true;
526 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000527}
528
529void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
530{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000531 if (mState.stencilFail != stencilFail ||
532 mState.stencilPassDepthFail != stencilPassDepthFail ||
533 mState.stencilPassDepthPass != stencilPassDepthPass)
534 {
535 mState.stencilFail = stencilFail;
536 mState.stencilPassDepthFail = stencilPassDepthFail;
537 mState.stencilPassDepthPass = stencilPassDepthPass;
538 mStencilStateDirty = true;
539 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000540}
541
542void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
543{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000544 if (mState.stencilBackFail != stencilBackFail ||
545 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
546 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
547 {
548 mState.stencilBackFail = stencilBackFail;
549 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
550 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
551 mStencilStateDirty = true;
552 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000553}
554
555void Context::setPolygonOffsetFill(bool enabled)
556{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000557 if (mState.polygonOffsetFill != enabled)
558 {
559 mState.polygonOffsetFill = enabled;
560 mPolygonOffsetStateDirty = true;
561 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000562}
563
564bool Context::isPolygonOffsetFillEnabled() const
565{
566 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000568}
569
570void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
571{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000572 if (mState.polygonOffsetFactor != factor ||
573 mState.polygonOffsetUnits != units)
574 {
575 mState.polygonOffsetFactor = factor;
576 mState.polygonOffsetUnits = units;
577 mPolygonOffsetStateDirty = true;
578 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000579}
580
581void Context::setSampleAlphaToCoverage(bool enabled)
582{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000583 if (mState.sampleAlphaToCoverage != enabled)
584 {
585 mState.sampleAlphaToCoverage = enabled;
586 mSampleStateDirty = true;
587 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000588}
589
590bool Context::isSampleAlphaToCoverageEnabled() const
591{
592 return mState.sampleAlphaToCoverage;
593}
594
595void Context::setSampleCoverage(bool enabled)
596{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000597 if (mState.sampleCoverage != enabled)
598 {
599 mState.sampleCoverage = enabled;
600 mSampleStateDirty = true;
601 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000602}
603
604bool Context::isSampleCoverageEnabled() const
605{
606 return mState.sampleCoverage;
607}
608
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000609void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000610{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000611 if (mState.sampleCoverageValue != value ||
612 mState.sampleCoverageInvert != invert)
613 {
614 mState.sampleCoverageValue = value;
615 mState.sampleCoverageInvert = invert;
616 mSampleStateDirty = true;
617 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000618}
619
620void Context::setScissorTest(bool enabled)
621{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000622 if (mState.scissorTest != enabled)
623 {
624 mState.scissorTest = enabled;
625 mScissorStateDirty = true;
626 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000627}
628
629bool Context::isScissorTestEnabled() const
630{
631 return mState.scissorTest;
632}
633
634void Context::setDither(bool enabled)
635{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000636 if (mState.dither != enabled)
637 {
638 mState.dither = enabled;
639 mDitherStateDirty = true;
640 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643bool Context::isDitherEnabled() const
644{
645 return mState.dither;
646}
647
648void Context::setLineWidth(GLfloat width)
649{
650 mState.lineWidth = width;
651}
652
653void Context::setGenerateMipmapHint(GLenum hint)
654{
655 mState.generateMipmapHint = hint;
656}
657
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000658void Context::setFragmentShaderDerivativeHint(GLenum hint)
659{
660 mState.fragmentShaderDerivativeHint = hint;
661 // TODO: Propagate the hint to shader translator so we can write
662 // ddx, ddx_coarse, or ddx_fine depending on the hint.
663 // Ignore for now. It is valid for implementations to ignore hint.
664}
665
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000666void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
667{
668 mState.viewportX = x;
669 mState.viewportY = y;
670 mState.viewportWidth = width;
671 mState.viewportHeight = height;
672}
673
674void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
675{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000676 if (mState.scissorX != x || mState.scissorY != y ||
677 mState.scissorWidth != width || mState.scissorHeight != height)
678 {
679 mState.scissorX = x;
680 mState.scissorY = y;
681 mState.scissorWidth = width;
682 mState.scissorHeight = height;
683 mScissorStateDirty = true;
684 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000685}
686
687void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
688{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000689 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
690 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
691 {
692 mState.colorMaskRed = red;
693 mState.colorMaskGreen = green;
694 mState.colorMaskBlue = blue;
695 mState.colorMaskAlpha = alpha;
696 mMaskStateDirty = true;
697 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000698}
699
700void Context::setDepthMask(bool mask)
701{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000702 if (mState.depthMask != mask)
703 {
704 mState.depthMask = mask;
705 mMaskStateDirty = true;
706 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709void Context::setActiveSampler(int active)
710{
711 mState.activeSampler = active;
712}
713
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000714GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000715{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000716 return mState.readFramebuffer.id();
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000717}
718
719GLuint Context::getDrawFramebufferHandle() const
720{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000721 return mState.drawFramebuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000722}
723
724GLuint Context::getRenderbufferHandle() const
725{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000726 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000727}
728
729GLuint Context::getArrayBufferHandle() const
730{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000731 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732}
733
734void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
735{
736 mState.vertexAttribute[attribNum].mEnabled = enabled;
737}
738
739const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
740{
741 return mState.vertexAttribute[attribNum];
742}
743
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000744void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000745 GLsizei stride, const void *pointer)
746{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000747 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000748 mState.vertexAttribute[attribNum].mSize = size;
749 mState.vertexAttribute[attribNum].mType = type;
750 mState.vertexAttribute[attribNum].mNormalized = normalized;
751 mState.vertexAttribute[attribNum].mStride = stride;
752 mState.vertexAttribute[attribNum].mPointer = pointer;
753}
754
755const void *Context::getVertexAttribPointer(unsigned int attribNum) const
756{
757 return mState.vertexAttribute[attribNum].mPointer;
758}
759
760// returns entire set of attributes as a block
761const AttributeState *Context::getVertexAttribBlock()
762{
763 return mState.vertexAttribute;
764}
765
766void Context::setPackAlignment(GLint alignment)
767{
768 mState.packAlignment = alignment;
769}
770
771GLint Context::getPackAlignment() const
772{
773 return mState.packAlignment;
774}
775
776void Context::setUnpackAlignment(GLint alignment)
777{
778 mState.unpackAlignment = alignment;
779}
780
781GLint Context::getUnpackAlignment() const
782{
783 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784}
785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786GLuint Context::createBuffer()
787{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000788 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789}
790
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791GLuint Context::createProgram()
792{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000793 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794}
795
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000796GLuint Context::createShader(GLenum type)
797{
798 return mResourceManager->createShader(type);
799}
800
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801GLuint Context::createTexture()
802{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000803 return mResourceManager->createTexture();
804}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000806GLuint Context::createRenderbuffer()
807{
808 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811GLuint Context::createFramebuffer()
812{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000813 return mResourceManager->createFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814}
815
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000816GLuint Context::createFence()
817{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000818 unsigned int handle = 0;
819
820 while (mFenceMap.find(handle) != mFenceMap.end())
821 {
822 handle++;
823 }
824
825 mFenceMap[handle] = new Fence;
826
827 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000828}
829
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830void Context::deleteBuffer(GLuint buffer)
831{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000832 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 {
834 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000836
837 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
840void Context::deleteShader(GLuint shader)
841{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000842 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
845void Context::deleteProgram(GLuint program)
846{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000847 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
850void Context::deleteTexture(GLuint texture)
851{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000852 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853 {
854 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856
857 mResourceManager->deleteTexture(texture);
858}
859
860void Context::deleteRenderbuffer(GLuint renderbuffer)
861{
862 if (mResourceManager->getRenderbuffer(renderbuffer))
863 {
864 detachRenderbuffer(renderbuffer);
865 }
866
867 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
870void Context::deleteFramebuffer(GLuint framebuffer)
871{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000872 if (mResourceManager->getFramebuffer(framebuffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 {
874 detachFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 }
apatrick@chromium.org55255022010-09-11 02:12:47 +0000876
877 mResourceManager->deleteFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000879
880void Context::deleteFence(GLuint fence)
881{
882 FenceMap::iterator fenceObject = mFenceMap.find(fence);
883
884 if (fenceObject != mFenceMap.end())
885 {
886 delete fenceObject->second;
887 mFenceMap.erase(fenceObject);
888 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000889}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000891Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000893 return mResourceManager->getBuffer(handle);
894}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000896Shader *Context::getShader(GLuint handle)
897{
898 return mResourceManager->getShader(handle);
899}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000901Program *Context::getProgram(GLuint handle)
902{
903 return mResourceManager->getProgram(handle);
904}
905
906Texture *Context::getTexture(GLuint handle)
907{
908 return mResourceManager->getTexture(handle);
909}
910
911Renderbuffer *Context::getRenderbuffer(GLuint handle)
912{
913 return mResourceManager->getRenderbuffer(handle);
914}
915
apatrick@chromium.org55255022010-09-11 02:12:47 +0000916Framebuffer *Context::getFramebuffer(GLuint handle)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000918 if (handle == 0)
919 {
920 return mFramebufferZero.get();
921 }
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000922
apatrick@chromium.org55255022010-09-11 02:12:47 +0000923 return mResourceManager->getFramebuffer(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924}
925
926void Context::bindArrayBuffer(unsigned int buffer)
927{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000930 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931}
932
933void Context::bindElementArrayBuffer(unsigned int buffer)
934{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000935 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000937 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938}
939
940void Context::bindTexture2D(GLuint texture)
941{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000944 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000946 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947}
948
949void Context::bindTextureCubeMap(GLuint texture)
950{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000953 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000958void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000960 mResourceManager->checkFramebufferAllocation(framebuffer);
961
962 mState.readFramebuffer.set(getFramebuffer(framebuffer));
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000963}
964
965void Context::bindDrawFramebuffer(GLuint framebuffer)
966{
apatrick@chromium.org55255022010-09-11 02:12:47 +0000967 mResourceManager->checkFramebufferAllocation(framebuffer);
968
969 mState.drawFramebuffer.set(getFramebuffer(framebuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
972void Context::bindRenderbuffer(GLuint renderbuffer)
973{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000974 mResourceManager->checkRenderbufferAllocation(renderbuffer);
975
976 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::useProgram(GLuint program)
980{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000981 GLuint priorProgram = mState.currentProgram;
982 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000983
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000984 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000986 Program *newProgram = mResourceManager->getProgram(program);
987 Program *oldProgram = mResourceManager->getProgram(priorProgram);
988
989 if (newProgram)
990 {
991 newProgram->addRef();
992 }
993
994 if (oldProgram)
995 {
996 oldProgram->release();
997 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
1001void Context::setFramebufferZero(Framebuffer *buffer)
1002{
apatrick@chromium.org55255022010-09-11 02:12:47 +00001003 if (mState.readFramebuffer.get() == mFramebufferZero.get())
1004 {
1005 mState.readFramebuffer.set(buffer);
1006 }
1007
1008 if (mState.drawFramebuffer.get() == mFramebufferZero.get())
1009 {
1010 mState.drawFramebuffer.set(buffer);
1011 }
1012
1013 mFramebufferZero.set(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014}
1015
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001016void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001018 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1019 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020}
1021
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001022Fence *Context::getFence(unsigned int handle)
1023{
1024 FenceMap::iterator fence = mFenceMap.find(handle);
1025
1026 if (fence == mFenceMap.end())
1027 {
1028 return NULL;
1029 }
1030 else
1031 {
1032 return fence->second;
1033 }
1034}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001035
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036Buffer *Context::getArrayBuffer()
1037{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039}
1040
1041Buffer *Context::getElementArrayBuffer()
1042{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001043 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044}
1045
1046Program *Context::getCurrentProgram()
1047{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001048 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049}
1050
1051Texture2D *Context::getTexture2D()
1052{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001053 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 +00001054 {
apatrick@chromium.org55255022010-09-11 02:12:47 +00001055 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056 }
1057
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001058 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059}
1060
1061TextureCubeMap *Context::getTextureCubeMap()
1062{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001063 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 +00001064 {
apatrick@chromium.org55255022010-09-11 02:12:47 +00001065 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066 }
1067
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001068 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001071Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001073 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001074
1075 if (texid == 0)
1076 {
1077 switch (type)
1078 {
1079 default: UNREACHABLE();
apatrick@chromium.org55255022010-09-11 02:12:47 +00001080 case SAMPLER_2D: return mTexture2DZero.get();
1081 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001082 }
1083 }
1084
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001085 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
apatrick@chromium.org55255022010-09-11 02:12:47 +00001088Framebuffer *Context::getReadFramebuffer()
1089{
1090 return mState.readFramebuffer.get();
1091}
1092
1093Framebuffer *Context::getDrawFramebuffer()
1094{
1095 return mState.drawFramebuffer.get();
1096}
1097
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001098bool Context::getBooleanv(GLenum pname, GLboolean *params)
1099{
1100 switch (pname)
1101 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001102 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1103 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1104 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001105 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001106 params[0] = mState.colorMaskRed;
1107 params[1] = mState.colorMaskGreen;
1108 params[2] = mState.colorMaskBlue;
1109 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001110 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001111 case GL_CULL_FACE: *params = mState.cullFace;
1112 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1113 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1114 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1115 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1116 case GL_STENCIL_TEST: *params = mState.stencilTest;
1117 case GL_DEPTH_TEST: *params = mState.depthTest;
1118 case GL_BLEND: *params = mState.blend;
1119 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001120 default:
1121 return false;
1122 }
1123
1124 return true;
1125}
1126
1127bool Context::getFloatv(GLenum pname, GLfloat *params)
1128{
1129 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1130 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1131 // GetIntegerv as its native query function. As it would require conversion in any
1132 // case, this should make no difference to the calling application.
1133 switch (pname)
1134 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001135 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1136 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1137 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1138 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1139 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001140 case GL_ALIASED_LINE_WIDTH_RANGE:
1141 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1142 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1143 break;
1144 case GL_ALIASED_POINT_SIZE_RANGE:
1145 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001146 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 +00001147 break;
1148 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001149 params[0] = mState.zNear;
1150 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001151 break;
1152 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001153 params[0] = mState.colorClearValue.red;
1154 params[1] = mState.colorClearValue.green;
1155 params[2] = mState.colorClearValue.blue;
1156 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001157 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001158 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001159 params[0] = mState.blendColor.red;
1160 params[1] = mState.blendColor.green;
1161 params[2] = mState.blendColor.blue;
1162 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001163 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001164 default:
1165 return false;
1166 }
1167
1168 return true;
1169}
1170
1171bool Context::getIntegerv(GLenum pname, GLint *params)
1172{
1173 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1174 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1175 // GetIntegerv as its native query function. As it would require conversion in any
1176 // case, this should make no difference to the calling application. You may find it in
1177 // Context::getFloatv.
1178 switch (pname)
1179 {
1180 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1181 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1182 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1183 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1184 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1185 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1186 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1187 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001188 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001189 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001190 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1191 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001192 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.org55255022010-09-11 02:12:47 +00001193 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer.id(); break;
1194 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer.id(); break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001195 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001196 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1197 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1198 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1199 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001200 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001201 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1202 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1203 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1204 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1205 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1206 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1207 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1208 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1209 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1210 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1211 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1212 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1213 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1214 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1215 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1216 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1217 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1218 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1219 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1220 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1221 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1222 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1223 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1224 case GL_SUBPIXEL_BITS: *params = 4; break;
1225 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1226 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001227 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1228 {
1229 if (supportsCompressedTextures())
1230 {
1231 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1232 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1233 *params = 2;
1234 }
1235 else
1236 {
1237 *params = 0;
1238 }
1239 }
1240 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001241 case GL_MAX_SAMPLES_ANGLE:
1242 {
1243 GLsizei maxSamples = getMaxSupportedSamples();
1244 if (maxSamples != 0)
1245 {
1246 *params = maxSamples;
1247 }
1248 else
1249 {
1250 return false;
1251 }
1252
1253 break;
1254 }
1255 case GL_SAMPLE_BUFFERS:
1256 case GL_SAMPLES:
1257 {
1258 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1259 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1260 {
1261 switch (pname)
1262 {
1263 case GL_SAMPLE_BUFFERS:
1264 if (framebuffer->getSamples() != 0)
1265 {
1266 *params = 1;
1267 }
1268 else
1269 {
1270 *params = 0;
1271 }
1272 break;
1273 case GL_SAMPLES:
1274 *params = framebuffer->getSamples();
1275 break;
1276 }
1277 }
1278 else
1279 {
1280 *params = 0;
1281 }
1282 }
1283 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001284 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1285 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1286 case GL_MAX_VIEWPORT_DIMS:
1287 {
1288 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1289 params[0] = maxDimension;
1290 params[1] = maxDimension;
1291 }
1292 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001293 case GL_COMPRESSED_TEXTURE_FORMATS:
1294 {
1295 if (supportsCompressedTextures())
1296 {
1297 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1298 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1299 }
1300 }
1301 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001302 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001303 params[0] = mState.viewportX;
1304 params[1] = mState.viewportY;
1305 params[2] = mState.viewportWidth;
1306 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001307 break;
1308 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001309 params[0] = mState.scissorX;
1310 params[1] = mState.scissorY;
1311 params[2] = mState.scissorWidth;
1312 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001313 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001314 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1315 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 case GL_RED_BITS:
1317 case GL_GREEN_BITS:
1318 case GL_BLUE_BITS:
1319 case GL_ALPHA_BITS:
1320 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001321 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1323
1324 if (colorbuffer)
1325 {
1326 switch (pname)
1327 {
1328 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1329 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1330 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1331 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1332 }
1333 }
1334 else
1335 {
1336 *params = 0;
1337 }
1338 }
1339 break;
1340 case GL_DEPTH_BITS:
1341 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001342 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001343 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001344
1345 if (depthbuffer)
1346 {
1347 *params = depthbuffer->getDepthSize();
1348 }
1349 else
1350 {
1351 *params = 0;
1352 }
1353 }
1354 break;
1355 case GL_STENCIL_BITS:
1356 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001357 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001358 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359
1360 if (stencilbuffer)
1361 {
1362 *params = stencilbuffer->getStencilSize();
1363 }
1364 else
1365 {
1366 *params = 0;
1367 }
1368 }
1369 break;
1370 case GL_TEXTURE_BINDING_2D:
1371 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001372 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001373 {
1374 error(GL_INVALID_OPERATION);
1375 return false;
1376 }
1377
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001378 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001379 }
1380 break;
1381 case GL_TEXTURE_BINDING_CUBE_MAP:
1382 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001383 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001384 {
1385 error(GL_INVALID_OPERATION);
1386 return false;
1387 }
1388
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001389 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001390 }
1391 break;
1392 default:
1393 return false;
1394 }
1395
1396 return true;
1397}
1398
1399bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1400{
1401 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1402 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1403 // to the fact that it is stored internally as a float, and so would require conversion
1404 // if returned from Context::getIntegerv. Since this conversion is already implemented
1405 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1406 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1407 // application.
1408 switch (pname)
1409 {
1410 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1411 case GL_SHADER_BINARY_FORMATS:
1412 {
1413 *type = GL_INT;
1414 *numParams = 0;
1415 }
1416 break;
1417 case GL_MAX_VERTEX_ATTRIBS:
1418 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1419 case GL_MAX_VARYING_VECTORS:
1420 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1421 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1422 case GL_MAX_TEXTURE_IMAGE_UNITS:
1423 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1424 case GL_MAX_RENDERBUFFER_SIZE:
1425 case GL_NUM_SHADER_BINARY_FORMATS:
1426 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1427 case GL_ARRAY_BUFFER_BINDING:
1428 case GL_FRAMEBUFFER_BINDING:
1429 case GL_RENDERBUFFER_BINDING:
1430 case GL_CURRENT_PROGRAM:
1431 case GL_PACK_ALIGNMENT:
1432 case GL_UNPACK_ALIGNMENT:
1433 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001434 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001435 case GL_RED_BITS:
1436 case GL_GREEN_BITS:
1437 case GL_BLUE_BITS:
1438 case GL_ALPHA_BITS:
1439 case GL_DEPTH_BITS:
1440 case GL_STENCIL_BITS:
1441 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1442 case GL_CULL_FACE_MODE:
1443 case GL_FRONT_FACE:
1444 case GL_ACTIVE_TEXTURE:
1445 case GL_STENCIL_FUNC:
1446 case GL_STENCIL_VALUE_MASK:
1447 case GL_STENCIL_REF:
1448 case GL_STENCIL_FAIL:
1449 case GL_STENCIL_PASS_DEPTH_FAIL:
1450 case GL_STENCIL_PASS_DEPTH_PASS:
1451 case GL_STENCIL_BACK_FUNC:
1452 case GL_STENCIL_BACK_VALUE_MASK:
1453 case GL_STENCIL_BACK_REF:
1454 case GL_STENCIL_BACK_FAIL:
1455 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1456 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1457 case GL_DEPTH_FUNC:
1458 case GL_BLEND_SRC_RGB:
1459 case GL_BLEND_SRC_ALPHA:
1460 case GL_BLEND_DST_RGB:
1461 case GL_BLEND_DST_ALPHA:
1462 case GL_BLEND_EQUATION_RGB:
1463 case GL_BLEND_EQUATION_ALPHA:
1464 case GL_STENCIL_WRITEMASK:
1465 case GL_STENCIL_BACK_WRITEMASK:
1466 case GL_STENCIL_CLEAR_VALUE:
1467 case GL_SUBPIXEL_BITS:
1468 case GL_MAX_TEXTURE_SIZE:
1469 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1470 case GL_SAMPLE_BUFFERS:
1471 case GL_SAMPLES:
1472 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1473 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1474 case GL_TEXTURE_BINDING_2D:
1475 case GL_TEXTURE_BINDING_CUBE_MAP:
1476 {
1477 *type = GL_INT;
1478 *numParams = 1;
1479 }
1480 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001481 case GL_MAX_SAMPLES_ANGLE:
1482 {
1483 if (getMaxSupportedSamples() != 0)
1484 {
1485 *type = GL_INT;
1486 *numParams = 1;
1487 }
1488 else
1489 {
1490 return false;
1491 }
1492 }
1493 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001494 case GL_MAX_VIEWPORT_DIMS:
1495 {
1496 *type = GL_INT;
1497 *numParams = 2;
1498 }
1499 break;
1500 case GL_VIEWPORT:
1501 case GL_SCISSOR_BOX:
1502 {
1503 *type = GL_INT;
1504 *numParams = 4;
1505 }
1506 break;
1507 case GL_SHADER_COMPILER:
1508 case GL_SAMPLE_COVERAGE_INVERT:
1509 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001510 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1511 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1512 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1513 case GL_SAMPLE_COVERAGE:
1514 case GL_SCISSOR_TEST:
1515 case GL_STENCIL_TEST:
1516 case GL_DEPTH_TEST:
1517 case GL_BLEND:
1518 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001519 {
1520 *type = GL_BOOL;
1521 *numParams = 1;
1522 }
1523 break;
1524 case GL_COLOR_WRITEMASK:
1525 {
1526 *type = GL_BOOL;
1527 *numParams = 4;
1528 }
1529 break;
1530 case GL_POLYGON_OFFSET_FACTOR:
1531 case GL_POLYGON_OFFSET_UNITS:
1532 case GL_SAMPLE_COVERAGE_VALUE:
1533 case GL_DEPTH_CLEAR_VALUE:
1534 case GL_LINE_WIDTH:
1535 {
1536 *type = GL_FLOAT;
1537 *numParams = 1;
1538 }
1539 break;
1540 case GL_ALIASED_LINE_WIDTH_RANGE:
1541 case GL_ALIASED_POINT_SIZE_RANGE:
1542 case GL_DEPTH_RANGE:
1543 {
1544 *type = GL_FLOAT;
1545 *numParams = 2;
1546 }
1547 break;
1548 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001549 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001550 {
1551 *type = GL_FLOAT;
1552 *numParams = 4;
1553 }
1554 break;
1555 default:
1556 return false;
1557 }
1558
1559 return true;
1560}
1561
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562// Applies the render target surface, depth stencil surface, viewport rectangle and
1563// scissor rectangle to the Direct3D 9 device
1564bool Context::applyRenderTarget(bool ignoreViewport)
1565{
1566 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001567
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001568 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569
1570 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1571 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001572 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1573
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574 return false;
1575 }
1576
1577 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001578
1579 if (!renderTarget)
1580 {
1581 return false; // Context must be lost
1582 }
1583
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001584 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001586 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1587 if (renderTargetSerial != mAppliedRenderTargetSerial)
1588 {
1589 device->SetRenderTarget(0, renderTarget);
1590 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001591 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 +00001592 }
1593
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001594 unsigned int depthbufferSerial = 0;
1595 unsigned int stencilbufferSerial = 0;
1596 if (framebufferObject->getDepthbufferType() != GL_NONE)
1597 {
1598 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1599 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1600 }
1601 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1602 {
1603 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1604 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1605 }
1606
1607 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1608 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001609 {
1610 device->SetDepthStencilSurface(depthStencil);
1611 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001612 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614
1615 D3DVIEWPORT9 viewport;
1616 D3DSURFACE_DESC desc;
1617 renderTarget->GetDesc(&desc);
1618
1619 if (ignoreViewport)
1620 {
1621 viewport.X = 0;
1622 viewport.Y = 0;
1623 viewport.Width = desc.Width;
1624 viewport.Height = desc.Height;
1625 viewport.MinZ = 0.0f;
1626 viewport.MaxZ = 1.0f;
1627 }
1628 else
1629 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001630 viewport.X = std::max(mState.viewportX, 0);
1631 viewport.Y = std::max(mState.viewportY, 0);
1632 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1633 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1634 viewport.MinZ = clamp01(mState.zNear);
1635 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636 }
1637
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001638 if (viewport.Width <= 0 || viewport.Height <= 0)
1639 {
1640 return false; // Nothing to render
1641 }
1642
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643 device->SetViewport(&viewport);
1644
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001645 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001647 if (mState.scissorTest)
1648 {
1649 RECT rect = {mState.scissorX,
1650 mState.scissorY,
1651 mState.scissorX + mState.scissorWidth,
1652 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001653 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1654 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001655 device->SetScissorRect(&rect);
1656 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1657 }
1658 else
1659 {
1660 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1661 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001662
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001663 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 }
1665
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001666 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 Program *programObject = getCurrentProgram();
1669
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001670 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001671 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001673
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001674 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001675 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1676 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1677 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001678 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1679
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001680 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001681 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001682 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1683
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001684 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001685 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001686
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001687 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001688 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001689
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001690 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001691 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001692 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693 }
1694
1695 return true;
1696}
1697
1698// 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 +00001699void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700{
1701 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001702 Program *programObject = getCurrentProgram();
1703
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001704 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001706 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001708 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001709 GLint alwaysFront = !isTriangleMode(drawMode);
1710 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1711
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001712 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001713
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001714 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001716 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001718 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 }
1720 else
1721 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001722 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 }
1724
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001725 mCullStateDirty = false;
1726 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001728 if (mDepthStateDirty)
1729 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001730 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001732 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1733 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 }
1735 else
1736 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001737 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001739
1740 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 }
1742
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001746 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1748
1749 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1750 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1751 {
1752 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1753 }
1754 else
1755 {
1756 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1757 unorm<8>(mState.blendColor.alpha),
1758 unorm<8>(mState.blendColor.alpha),
1759 unorm<8>(mState.blendColor.alpha)));
1760 }
1761
1762 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1763 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1764 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1765
1766 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1767 mState.destBlendRGB != mState.destBlendAlpha ||
1768 mState.blendEquationRGB != mState.blendEquationAlpha)
1769 {
1770 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1771
1772 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1773 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1774 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1775
1776 }
1777 else
1778 {
1779 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1780 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001781 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001782 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001783 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001784 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001785 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001786
1787 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788 }
1789
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001790 if (mStencilStateDirty || mFrontFaceDirty)
1791 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001792 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001793 {
1794 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1795 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1796
1797 // FIXME: Unsupported by D3D9
1798 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1799 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1800 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1801 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1802 mState.stencilRef != mState.stencilBackRef ||
1803 mState.stencilMask != mState.stencilBackMask)
1804 {
1805 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1806 return error(GL_INVALID_OPERATION);
1807 }
1808
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001809 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001810 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001811 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1812
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1814 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1815 es2dx::ConvertComparison(mState.stencilFunc));
1816
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001817 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 +00001818 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1819
1820 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1821 es2dx::ConvertStencilOp(mState.stencilFail));
1822 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1823 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1824 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1825 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1826
1827 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1828 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1829 es2dx::ConvertComparison(mState.stencilBackFunc));
1830
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001831 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 +00001832 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1833
1834 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1835 es2dx::ConvertStencilOp(mState.stencilBackFail));
1836 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1837 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1838 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1839 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1840 }
1841 else
1842 {
1843 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1844 }
1845
1846 mStencilStateDirty = false;
1847 }
1848
1849 if (mMaskStateDirty)
1850 {
1851 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1852 mState.colorMaskBlue, mState.colorMaskAlpha));
1853 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1854
1855 mMaskStateDirty = false;
1856 }
1857
1858 if (mPolygonOffsetStateDirty)
1859 {
1860 if (mState.polygonOffsetFill)
1861 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001862 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 if (depthbuffer)
1864 {
1865 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1866 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1867 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1868 }
1869 }
1870 else
1871 {
1872 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1873 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1874 }
1875
1876 mPolygonOffsetStateDirty = false;
1877 }
1878
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001879 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001881 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001882 {
1883 FIXME("Sample alpha to coverage is unimplemented.");
1884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001886 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001887 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001888 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1889 unsigned int mask = 0;
1890 if (mState.sampleCoverageValue != 0)
1891 {
1892 float threshold = 0.5f;
1893
1894 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1895 {
1896 mask <<= 1;
1897
1898 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1899 {
1900 threshold += 1.0f;
1901 mask |= 1;
1902 }
1903 }
1904 }
1905
1906 if (mState.sampleCoverageInvert)
1907 {
1908 mask = ~mask;
1909 }
1910
1911 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1912 }
1913 else
1914 {
1915 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001916 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001917
1918 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 }
1920
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921 if (mDitherStateDirty)
1922 {
1923 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1924
1925 mDitherStateDirty = false;
1926 }
1927
1928 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929}
1930
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001931// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001932void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001934 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001936 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001938 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 }
1940 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001941}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001943GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001944{
1945 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1946
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001947 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001948 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001949 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001950 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001951 }
1952
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001953 lookupAttributeMapping(translated);
1954
1955 mBufferBackEnd->setupAttributesPreDraw(translated);
1956
1957 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1958 {
1959 if (translated[i].enabled && translated[i].nonArray)
1960 {
1961 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1962 if (err != GL_NO_ERROR)
1963 {
1964 return err;
1965 }
1966
1967 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1968
1969 *useIndexing = true;
1970 return GL_NO_ERROR;
1971 }
1972 }
1973
1974 *useIndexing = false;
1975 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001976}
1977
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001978GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001979{
1980 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1981
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001982 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001983
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001984 if (err == GL_NO_ERROR)
1985 {
1986 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001987
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001988 mBufferBackEnd->setupAttributesPreDraw(translated);
1989 }
1990
1991 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001992}
1993
1994// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001995GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001996{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001997 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001998
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001999 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002000 {
2001 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2002 }
2003
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002004 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005}
2006
2007// Applies the shaders and shader constants to the Direct3D 9 device
2008void Context::applyShaders()
2009{
2010 IDirect3DDevice9 *device = getDevice();
2011 Program *programObject = getCurrentProgram();
2012 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2013 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2014
2015 device->SetVertexShader(vertexShader);
2016 device->SetPixelShader(pixelShader);
2017
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002018 if (programObject->getSerial() != mAppliedProgram)
2019 {
2020 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002021 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002022 mAppliedProgram = programObject->getSerial();
2023 }
2024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 programObject->applyUniforms();
2026}
2027
2028// Applies the textures and sampler states to the Direct3D 9 device
2029void Context::applyTextures()
2030{
2031 IDirect3DDevice9 *device = getDevice();
2032 Program *programObject = getCurrentProgram();
2033
2034 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2035 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002036 int textureUnit = programObject->getSamplerMapping(sampler);
2037 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002039 SamplerType textureType = programObject->getSamplerType(sampler);
2040
2041 Texture *texture = getSamplerTexture(textureUnit, textureType);
2042
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002043 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002044 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002045 if (texture->isComplete())
2046 {
2047 GLenum wrapS = texture->getWrapS();
2048 GLenum wrapT = texture->getWrapT();
2049 GLenum minFilter = texture->getMinFilter();
2050 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002052 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2053 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002055 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2056 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2057 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2058 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2059 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002061 device->SetTexture(sampler, texture->getTexture());
2062 }
2063 else
2064 {
2065 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2066 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002067 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002068
2069 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002071 else
2072 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002073 if (programObject->isSamplerDirty(sampler))
2074 {
2075 device->SetTexture(sampler, NULL);
2076 programObject->setSamplerDirty(sampler, false);
2077 }
2078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080}
2081
2082void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2083{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002084 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002085
2086 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2087 {
2088 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2089 }
2090
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002091 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2092 {
2093 return error(GL_INVALID_OPERATION);
2094 }
2095
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002097
2098 if (!renderTarget)
2099 {
2100 return; // Context must be lost, return silently
2101 }
2102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 IDirect3DDevice9 *device = getDevice();
2104
2105 D3DSURFACE_DESC desc;
2106 renderTarget->GetDesc(&desc);
2107
2108 IDirect3DSurface9 *systemSurface;
2109 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2110
2111 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2112 {
2113 return error(GL_OUT_OF_MEMORY);
2114 }
2115
2116 ASSERT(SUCCEEDED(result));
2117
2118 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2119 {
2120 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2121 }
2122
2123 result = device->GetRenderTargetData(renderTarget, systemSurface);
2124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 if (FAILED(result))
2126 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 systemSurface->Release();
2128
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002129 switch (result)
2130 {
2131 case D3DERR_DRIVERINTERNALERROR:
2132 case D3DERR_DEVICELOST:
2133 return error(GL_OUT_OF_MEMORY);
2134 default:
2135 UNREACHABLE();
2136 return; // No sensible error to generate
2137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138 }
2139
2140 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002141 RECT rect = {std::max(x, 0),
2142 std::max(y, 0),
2143 std::min(x + width, (int)desc.Width),
2144 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145
2146 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2147
2148 if (FAILED(result))
2149 {
2150 UNREACHABLE();
2151 systemSurface->Release();
2152
2153 return; // No sensible error to generate
2154 }
2155
2156 unsigned char *source = (unsigned char*)lock.pBits;
2157 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002158 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002160 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002161
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 for (int j = 0; j < rect.bottom - rect.top; j++)
2163 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002164 if (desc.Format == D3DFMT_A8R8G8B8 &&
2165 format == GL_BGRA_EXT &&
2166 type == GL_UNSIGNED_BYTE)
2167 {
2168 // Fast path for EXT_read_format_bgra, given
2169 // an RGBA source buffer. Note that buffers with no
2170 // alpha go through the slow path below.
2171 memcpy(dest + j * outputPitch,
2172 source + j * lock.Pitch,
2173 (rect.right - rect.left) * 4);
2174 continue;
2175 }
2176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 for (int i = 0; i < rect.right - rect.left; i++)
2178 {
2179 float r;
2180 float g;
2181 float b;
2182 float a;
2183
2184 switch (desc.Format)
2185 {
2186 case D3DFMT_R5G6B5:
2187 {
2188 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2189
2190 a = 1.0f;
2191 b = (rgb & 0x001F) * (1.0f / 0x001F);
2192 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2193 r = (rgb & 0xF800) * (1.0f / 0xF800);
2194 }
2195 break;
2196 case D3DFMT_X1R5G5B5:
2197 {
2198 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2199
2200 a = 1.0f;
2201 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2202 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2203 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2204 }
2205 break;
2206 case D3DFMT_A1R5G5B5:
2207 {
2208 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2209
2210 a = (argb & 0x8000) ? 1.0f : 0.0f;
2211 b = (argb & 0x001F) * (1.0f / 0x001F);
2212 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2213 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2214 }
2215 break;
2216 case D3DFMT_A8R8G8B8:
2217 {
2218 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2219
2220 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2221 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2222 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2223 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2224 }
2225 break;
2226 case D3DFMT_X8R8G8B8:
2227 {
2228 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2229
2230 a = 1.0f;
2231 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2232 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2233 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2234 }
2235 break;
2236 case D3DFMT_A2R10G10B10:
2237 {
2238 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2239
2240 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2241 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2242 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2243 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2244 }
2245 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002246 case D3DFMT_A32B32G32R32F:
2247 {
2248 // float formats in D3D are stored rgba, rather than the other way round
2249 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2250 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2251 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2252 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2253 }
2254 break;
2255 case D3DFMT_A16B16G16R16F:
2256 {
2257 // float formats in D3D are stored rgba, rather than the other way round
2258 float abgr[4];
2259
2260 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2261
2262 a = abgr[3];
2263 b = abgr[2];
2264 g = abgr[1];
2265 r = abgr[0];
2266 }
2267 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268 default:
2269 UNIMPLEMENTED(); // FIXME
2270 UNREACHABLE();
2271 }
2272
2273 switch (format)
2274 {
2275 case GL_RGBA:
2276 switch (type)
2277 {
2278 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002279 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2280 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2281 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2282 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283 break;
2284 default: UNREACHABLE();
2285 }
2286 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002287 case GL_BGRA_EXT:
2288 switch (type)
2289 {
2290 case GL_UNSIGNED_BYTE:
2291 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2292 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2293 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2294 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2295 break;
2296 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2297 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2298 // this type is packed as follows:
2299 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2300 // --------------------------------------------------------------------------------
2301 // | 4th | 3rd | 2nd | 1st component |
2302 // --------------------------------------------------------------------------------
2303 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2304 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2305 ((unsigned short)(15 * a + 0.5f) << 12)|
2306 ((unsigned short)(15 * r + 0.5f) << 8) |
2307 ((unsigned short)(15 * g + 0.5f) << 4) |
2308 ((unsigned short)(15 * b + 0.5f) << 0);
2309 break;
2310 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2311 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2312 // this type is packed as follows:
2313 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2314 // --------------------------------------------------------------------------------
2315 // | 4th | 3rd | 2nd | 1st component |
2316 // --------------------------------------------------------------------------------
2317 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2318 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2319 ((unsigned short)( a + 0.5f) << 15) |
2320 ((unsigned short)(31 * r + 0.5f) << 10) |
2321 ((unsigned short)(31 * g + 0.5f) << 5) |
2322 ((unsigned short)(31 * b + 0.5f) << 0);
2323 break;
2324 default: UNREACHABLE();
2325 }
2326 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002327 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328 switch (type)
2329 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002330 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002331 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2332 ((unsigned short)(31 * b + 0.5f) << 0) |
2333 ((unsigned short)(63 * g + 0.5f) << 5) |
2334 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 break;
2336 default: UNREACHABLE();
2337 }
2338 break;
2339 default: UNREACHABLE();
2340 }
2341 }
2342 }
2343
2344 systemSurface->UnlockRect();
2345
2346 systemSurface->Release();
2347}
2348
2349void Context::clear(GLbitfield mask)
2350{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002351 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002352
2353 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2354 {
2355 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2356
2357 return;
2358 }
2359
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002360 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361 IDirect3DDevice9 *device = getDevice();
2362 DWORD flags = 0;
2363
2364 if (mask & GL_COLOR_BUFFER_BIT)
2365 {
2366 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002367
2368 if (framebufferObject->getColorbufferType() != GL_NONE)
2369 {
2370 flags |= D3DCLEAR_TARGET;
2371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372 }
2373
2374 if (mask & GL_DEPTH_BUFFER_BIT)
2375 {
2376 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002377 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 {
2379 flags |= D3DCLEAR_ZBUFFER;
2380 }
2381 }
2382
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 GLuint stencilUnmasked = 0x0;
2384
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002385 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002388 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002390 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2391 D3DSURFACE_DESC desc;
2392 depthStencil->GetDesc(&desc);
2393
2394 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2395 stencilUnmasked = (0x1 << stencilSize) - 1;
2396
2397 if (stencilUnmasked != 0x0)
2398 {
2399 flags |= D3DCLEAR_STENCIL;
2400 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 }
2402 }
2403
2404 if (mask != 0)
2405 {
2406 return error(GL_INVALID_VALUE);
2407 }
2408
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002409 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2410 {
2411 return;
2412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002414 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2415 unorm<8>(mState.colorClearValue.red),
2416 unorm<8>(mState.colorClearValue.green),
2417 unorm<8>(mState.colorClearValue.blue));
2418 float depth = clamp01(mState.depthClearValue);
2419 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420
2421 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2422
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002423 if (!renderTarget)
2424 {
2425 return; // Context must be lost, return silently
2426 }
2427
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 D3DSURFACE_DESC desc;
2429 renderTarget->GetDesc(&desc);
2430
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002431 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432
2433 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002434 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002436 !(mState.colorMaskRed && mState.colorMaskGreen &&
2437 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
2439 if (needMaskedColorClear || needMaskedStencilClear)
2440 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002441 // State which is altered in all paths from this point to the clear call is saved.
2442 // State which is altered in only some paths will be flagged dirty in the case that
2443 // that path is taken.
2444 HRESULT hr;
2445 if (mMaskedClearSavedState == NULL)
2446 {
2447 hr = device->BeginStateBlock();
2448 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2449
2450 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2451 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2452 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2453 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2454 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2455 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2456 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2457 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2458 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2459 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2460 device->SetPixelShader(NULL);
2461 device->SetVertexShader(NULL);
2462 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2463 device->SetStreamSourceFreq(0, 1);
2464
2465 hr = device->EndStateBlock(&mMaskedClearSavedState);
2466 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2467 }
2468
2469 ASSERT(mMaskedClearSavedState != NULL);
2470
2471 if (mMaskedClearSavedState != NULL)
2472 {
2473 hr = mMaskedClearSavedState->Capture();
2474 ASSERT(SUCCEEDED(hr));
2475 }
2476
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2478 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2479 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2480 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2481 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2482 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2483 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2484 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2485
2486 if (flags & D3DCLEAR_TARGET)
2487 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002488 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2489 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2490 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2491 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 }
2493 else
2494 {
2495 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2496 }
2497
2498 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2499 {
2500 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2501 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2502 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2503 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002504 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002505 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2507 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002508 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 }
2510 else
2511 {
2512 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2513 }
2514
2515 device->SetPixelShader(NULL);
2516 device->SetVertexShader(NULL);
2517 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002518 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002520 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521 {
2522 float x, y, z, w;
2523 D3DCOLOR diffuse;
2524 };
2525
2526 Vertex quad[4];
2527 quad[0].x = 0.0f;
2528 quad[0].y = (float)desc.Height;
2529 quad[0].z = 0.0f;
2530 quad[0].w = 1.0f;
2531 quad[0].diffuse = color;
2532
2533 quad[1].x = (float)desc.Width;
2534 quad[1].y = (float)desc.Height;
2535 quad[1].z = 0.0f;
2536 quad[1].w = 1.0f;
2537 quad[1].diffuse = color;
2538
2539 quad[2].x = 0.0f;
2540 quad[2].y = 0.0f;
2541 quad[2].z = 0.0f;
2542 quad[2].w = 1.0f;
2543 quad[2].diffuse = color;
2544
2545 quad[3].x = (float)desc.Width;
2546 quad[3].y = 0.0f;
2547 quad[3].z = 0.0f;
2548 quad[3].w = 1.0f;
2549 quad[3].diffuse = color;
2550
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002551 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
2554 if (flags & D3DCLEAR_ZBUFFER)
2555 {
2556 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2557 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2558 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2559 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002560
2561 if (mMaskedClearSavedState != NULL)
2562 {
2563 mMaskedClearSavedState->Apply();
2564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002566 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 {
2568 device->Clear(0, NULL, flags, color, depth, stencil);
2569 }
2570}
2571
2572void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2573{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002574 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002575 {
2576 return error(GL_INVALID_OPERATION);
2577 }
2578
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002579 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 IDirect3DDevice9 *device = getDevice();
2581 D3DPRIMITIVETYPE primitiveType;
2582 int primitiveCount;
2583
2584 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2585 return error(GL_INVALID_ENUM);
2586
2587 if (primitiveCount <= 0)
2588 {
2589 return;
2590 }
2591
2592 if (!applyRenderTarget(false))
2593 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002594 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 }
2596
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002597 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002598
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002599 TranslatedIndexData indexInfo;
2600 bool useIndexing;
2601 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002602 if (err != GL_NO_ERROR)
2603 {
2604 return error(err);
2605 }
2606
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607 applyShaders();
2608 applyTextures();
2609
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002610 if (!getCurrentProgram()->validateSamplers())
2611 {
2612 return error(GL_INVALID_OPERATION);
2613 }
2614
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002615 if (!cullSkipsDraw(mode))
2616 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002617 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002618 if (useIndexing)
2619 {
2620 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2621 }
2622 else
2623 {
2624 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2625 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627}
2628
2629void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2630{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002631 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 {
2633 return error(GL_INVALID_OPERATION);
2634 }
2635
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002636 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 {
2638 return error(GL_INVALID_OPERATION);
2639 }
2640
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002641 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 IDirect3DDevice9 *device = getDevice();
2643 D3DPRIMITIVETYPE primitiveType;
2644 int primitiveCount;
2645
2646 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2647 return error(GL_INVALID_ENUM);
2648
2649 if (primitiveCount <= 0)
2650 {
2651 return;
2652 }
2653
2654 if (!applyRenderTarget(false))
2655 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002656 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 }
2658
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002659 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002660
2661 TranslatedIndexData indexInfo;
2662 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2663 if (err != GL_NO_ERROR)
2664 {
2665 return error(err);
2666 }
2667
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002668 err = applyVertexBuffer(indexInfo);
2669 if (err != GL_NO_ERROR)
2670 {
2671 return error(err);
2672 }
2673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 applyShaders();
2675 applyTextures();
2676
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002677 if (!getCurrentProgram()->validateSamplers())
2678 {
2679 return error(GL_INVALID_OPERATION);
2680 }
2681
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002682 if (!cullSkipsDraw(mode))
2683 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002684 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002685 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 +00002686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687}
2688
2689void Context::finish()
2690{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002691 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 IDirect3DDevice9 *device = getDevice();
2693 IDirect3DQuery9 *occlusionQuery = NULL;
2694
2695 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2696
2697 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2698 {
2699 return error(GL_OUT_OF_MEMORY);
2700 }
2701
2702 ASSERT(SUCCEEDED(result));
2703
2704 if (occlusionQuery)
2705 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002706 IDirect3DStateBlock9 *savedState = NULL;
2707 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2708
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002709 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2710 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711
2712 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002713 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714 device->SetPixelShader(NULL);
2715 device->SetVertexShader(NULL);
2716 device->SetFVF(D3DFVF_XYZRHW);
2717 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002718 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002721 result = occlusionQuery->Issue(D3DISSUE_END);
2722 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723
2724 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2725 {
2726 // Keep polling, but allow other threads to do something useful first
2727 Sleep(0);
2728 }
2729
2730 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002731
2732 if (savedState)
2733 {
2734 savedState->Apply();
2735 savedState->Release();
2736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 }
2738}
2739
2740void Context::flush()
2741{
2742 IDirect3DDevice9 *device = getDevice();
2743 IDirect3DQuery9 *eventQuery = NULL;
2744
2745 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2746
2747 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2748 {
2749 return error(GL_OUT_OF_MEMORY);
2750 }
2751
2752 ASSERT(SUCCEEDED(result));
2753
2754 if (eventQuery)
2755 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002756 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2757 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002759 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002761
2762 if (result == D3DERR_DEVICELOST)
2763 {
2764 error(GL_OUT_OF_MEMORY);
2765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 }
2767}
2768
2769void Context::recordInvalidEnum()
2770{
2771 mInvalidEnum = true;
2772}
2773
2774void Context::recordInvalidValue()
2775{
2776 mInvalidValue = true;
2777}
2778
2779void Context::recordInvalidOperation()
2780{
2781 mInvalidOperation = true;
2782}
2783
2784void Context::recordOutOfMemory()
2785{
2786 mOutOfMemory = true;
2787}
2788
2789void Context::recordInvalidFramebufferOperation()
2790{
2791 mInvalidFramebufferOperation = true;
2792}
2793
2794// Get one of the recorded errors and clear its flag, if any.
2795// [OpenGL ES 2.0.24] section 2.5 page 13.
2796GLenum Context::getError()
2797{
2798 if (mInvalidEnum)
2799 {
2800 mInvalidEnum = false;
2801
2802 return GL_INVALID_ENUM;
2803 }
2804
2805 if (mInvalidValue)
2806 {
2807 mInvalidValue = false;
2808
2809 return GL_INVALID_VALUE;
2810 }
2811
2812 if (mInvalidOperation)
2813 {
2814 mInvalidOperation = false;
2815
2816 return GL_INVALID_OPERATION;
2817 }
2818
2819 if (mOutOfMemory)
2820 {
2821 mOutOfMemory = false;
2822
2823 return GL_OUT_OF_MEMORY;
2824 }
2825
2826 if (mInvalidFramebufferOperation)
2827 {
2828 mInvalidFramebufferOperation = false;
2829
2830 return GL_INVALID_FRAMEBUFFER_OPERATION;
2831 }
2832
2833 return GL_NO_ERROR;
2834}
2835
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002836bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002837{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002838 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002839}
2840
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002841int Context::getMaxSupportedSamples() const
2842{
2843 return mMaxSupportedSamples;
2844}
2845
2846int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2847{
2848 if (requested == 0)
2849 {
2850 return requested;
2851 }
2852
2853 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2854 if (itr == mMultiSampleSupport.end())
2855 {
2856 return -1;
2857 }
2858
2859 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2860 {
2861 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2862 {
2863 return i;
2864 }
2865 }
2866
2867 return -1;
2868}
2869
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002870bool Context::supportsEventQueries() const
2871{
2872 return mSupportsEventQueries;
2873}
2874
daniel@transgaming.com01868132010-08-24 19:21:17 +00002875bool Context::supportsCompressedTextures() const
2876{
2877 return mSupportsCompressedTextures;
2878}
2879
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002880bool Context::supportsFloatTextures() const
2881{
2882 return mSupportsFloatTextures;
2883}
2884
2885bool Context::supportsFloatLinearFilter() const
2886{
2887 return mSupportsFloatLinearFilter;
2888}
2889
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002890bool Context::supportsFloatRenderableTextures() const
2891{
2892 return mSupportsFloatRenderableTextures;
2893}
2894
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002895bool Context::supportsHalfFloatTextures() const
2896{
2897 return mSupportsHalfFloatTextures;
2898}
2899
2900bool Context::supportsHalfFloatLinearFilter() const
2901{
2902 return mSupportsHalfFloatLinearFilter;
2903}
2904
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002905bool Context::supportsHalfFloatRenderableTextures() const
2906{
2907 return mSupportsHalfFloatRenderableTextures;
2908}
2909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002910void Context::detachBuffer(GLuint buffer)
2911{
2912 // [OpenGL ES 2.0.24] section 2.9 page 22:
2913 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2914 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2915
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002916 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002918 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002919 }
2920
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002921 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002923 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002924 }
2925
2926 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2927 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002928 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002929 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002930 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002931 }
2932 }
2933}
2934
2935void Context::detachTexture(GLuint texture)
2936{
2937 // [OpenGL ES 2.0.24] section 3.8 page 84:
2938 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2939 // rebound to texture object zero
2940
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002941 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002942 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002943 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002944 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002945 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002946 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002947 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002948 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002949 }
2950 }
2951
2952 // [OpenGL ES 2.0.24] section 4.4 page 112:
2953 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2954 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2955 // image was attached in the currently bound framebuffer.
2956
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002957 Framebuffer *readFramebuffer = getReadFramebuffer();
2958 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002959
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002960 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002961 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002962 readFramebuffer->detachTexture(texture);
2963 }
2964
2965 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2966 {
2967 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002968 }
2969}
2970
2971void Context::detachFramebuffer(GLuint framebuffer)
2972{
2973 // [OpenGL ES 2.0.24] section 4.4 page 107:
2974 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2975 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2976
apatrick@chromium.org55255022010-09-11 02:12:47 +00002977 if (mState.readFramebuffer.id() == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002978 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002979 bindReadFramebuffer(0);
2980 }
2981
apatrick@chromium.org55255022010-09-11 02:12:47 +00002982 if (mState.drawFramebuffer.id() == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002983 {
2984 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002985 }
2986}
2987
2988void Context::detachRenderbuffer(GLuint renderbuffer)
2989{
2990 // [OpenGL ES 2.0.24] section 4.4 page 109:
2991 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2992 // had been executed with the target RENDERBUFFER and name of zero.
2993
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002994 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002995 {
2996 bindRenderbuffer(0);
2997 }
2998
2999 // [OpenGL ES 2.0.24] section 4.4 page 111:
3000 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3001 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3002 // point to which this 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->detachRenderbuffer(renderbuffer);
3010 }
3011
3012 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3013 {
3014 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015 }
3016}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003017
3018Texture *Context::getIncompleteTexture(SamplerType type)
3019{
apatrick@chromium.org55255022010-09-11 02:12:47 +00003020 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003021
3022 if (t == NULL)
3023 {
3024 static const GLubyte color[] = { 0, 0, 0, 255 };
3025
3026 switch (type)
3027 {
3028 default:
3029 UNREACHABLE();
3030 // default falls through to SAMPLER_2D
3031
3032 case SAMPLER_2D:
3033 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003034 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003035 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003036 t = incomplete2d;
3037 }
3038 break;
3039
3040 case SAMPLER_CUBE:
3041 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003042 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003043
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003044 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3045 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3046 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3047 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3048 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3049 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003050
3051 t = incompleteCube;
3052 }
3053 break;
3054 }
3055
apatrick@chromium.org55255022010-09-11 02:12:47 +00003056 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003057 }
3058
3059 return t;
3060}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003061
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003062bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003063{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003064 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003065}
3066
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003067bool Context::isTriangleMode(GLenum drawMode)
3068{
3069 switch (drawMode)
3070 {
3071 case GL_TRIANGLES:
3072 case GL_TRIANGLE_FAN:
3073 case GL_TRIANGLE_STRIP:
3074 return true;
3075 case GL_POINTS:
3076 case GL_LINES:
3077 case GL_LINE_LOOP:
3078 case GL_LINE_STRIP:
3079 return false;
3080 default: UNREACHABLE();
3081 }
3082
3083 return false;
3084}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003085
3086void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3087{
3088 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3089
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003090 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3091 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3092 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3093 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003094
3095 mVertexDataManager->dirtyCurrentValues();
3096}
3097
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003098void Context::initExtensionString()
3099{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003100 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003101 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3102 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003103 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003104 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003105 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003106
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003107 if (supportsEventQueries())
3108 {
3109 mExtensionString += "GL_NV_fence ";
3110 }
3111
daniel@transgaming.com01868132010-08-24 19:21:17 +00003112 if (supportsCompressedTextures())
3113 {
3114 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3115 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003116
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003117 if (supportsFloatTextures())
3118 {
3119 mExtensionString += "GL_OES_texture_float ";
3120 }
3121
3122 if (supportsHalfFloatTextures())
3123 {
3124 mExtensionString += "GL_OES_texture_half_float ";
3125 }
3126
3127 if (supportsFloatLinearFilter())
3128 {
3129 mExtensionString += "GL_OES_texture_float_linear ";
3130 }
3131
3132 if (supportsHalfFloatLinearFilter())
3133 {
3134 mExtensionString += "GL_OES_texture_half_float_linear ";
3135 }
3136
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003137 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003138 {
3139 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3140 }
3141
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003142 if (mBufferBackEnd->supportIntIndices())
3143 {
3144 mExtensionString += "GL_OES_element_index_uint ";
3145 }
3146
3147 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3148 if (end != std::string::npos)
3149 {
3150 mExtensionString.resize(end+1);
3151 }
3152}
3153
3154const char *Context::getExtensionString() const
3155{
3156 return mExtensionString.c_str();
3157}
3158
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003159void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3160 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3161 GLbitfield mask)
3162{
3163 IDirect3DDevice9 *device = getDevice();
3164
3165 Framebuffer *readFramebuffer = getReadFramebuffer();
3166 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3167
3168 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3169 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3170 {
3171 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3172 }
3173
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003174 if (drawFramebuffer->getSamples() != 0)
3175 {
3176 return error(GL_INVALID_OPERATION);
3177 }
3178
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003179 RECT sourceRect;
3180 RECT destRect;
3181
3182 if (srcX0 < srcX1)
3183 {
3184 sourceRect.left = srcX0;
3185 sourceRect.right = srcX1;
3186 destRect.left = dstX0;
3187 destRect.right = dstX1;
3188 }
3189 else
3190 {
3191 sourceRect.left = srcX1;
3192 destRect.left = dstX1;
3193 sourceRect.right = srcX0;
3194 destRect.right = dstX0;
3195 }
3196
3197 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3198 // flip our Y-values here
3199 if (srcY0 < srcY1)
3200 {
3201 sourceRect.bottom = srcY1;
3202 destRect.bottom = dstY1;
3203 sourceRect.top = srcY0;
3204 destRect.top = dstY0;
3205 }
3206 else
3207 {
3208 sourceRect.bottom = srcY0;
3209 destRect.bottom = dstY0;
3210 sourceRect.top = srcY1;
3211 destRect.top = dstY1;
3212 }
3213
3214 RECT sourceScissoredRect = sourceRect;
3215 RECT destScissoredRect = destRect;
3216
3217 if (mState.scissorTest)
3218 {
3219 // Only write to parts of the destination framebuffer which pass the scissor test
3220 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3221 // rect will be checked against scissorY, rather than the bottom.
3222 if (destRect.left < mState.scissorX)
3223 {
3224 int xDiff = mState.scissorX - destRect.left;
3225 destScissoredRect.left = mState.scissorX;
3226 sourceScissoredRect.left += xDiff;
3227 }
3228
3229 if (destRect.right > mState.scissorX + mState.scissorWidth)
3230 {
3231 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3232 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3233 sourceScissoredRect.right -= xDiff;
3234 }
3235
3236 if (destRect.top < mState.scissorY)
3237 {
3238 int yDiff = mState.scissorY - destRect.top;
3239 destScissoredRect.top = mState.scissorY;
3240 sourceScissoredRect.top += yDiff;
3241 }
3242
3243 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3244 {
3245 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3246 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3247 sourceScissoredRect.bottom -= yDiff;
3248 }
3249 }
3250
3251 bool blitRenderTarget = false;
3252 bool blitDepthStencil = false;
3253
3254 RECT sourceTrimmedRect = sourceScissoredRect;
3255 RECT destTrimmedRect = destScissoredRect;
3256
3257 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3258 // the actual draw and read surfaces.
3259 if (sourceTrimmedRect.left < 0)
3260 {
3261 int xDiff = 0 - sourceTrimmedRect.left;
3262 sourceTrimmedRect.left = 0;
3263 destTrimmedRect.left += xDiff;
3264 }
3265
3266 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3267 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3268 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3269 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3270
3271 if (sourceTrimmedRect.right > readBufferWidth)
3272 {
3273 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3274 sourceTrimmedRect.right = readBufferWidth;
3275 destTrimmedRect.right -= xDiff;
3276 }
3277
3278 if (sourceTrimmedRect.top < 0)
3279 {
3280 int yDiff = 0 - sourceTrimmedRect.top;
3281 sourceTrimmedRect.top = 0;
3282 destTrimmedRect.top += yDiff;
3283 }
3284
3285 if (sourceTrimmedRect.bottom > readBufferHeight)
3286 {
3287 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3288 sourceTrimmedRect.bottom = readBufferHeight;
3289 destTrimmedRect.bottom -= yDiff;
3290 }
3291
3292 if (destTrimmedRect.left < 0)
3293 {
3294 int xDiff = 0 - destTrimmedRect.left;
3295 destTrimmedRect.left = 0;
3296 sourceTrimmedRect.left += xDiff;
3297 }
3298
3299 if (destTrimmedRect.right > drawBufferWidth)
3300 {
3301 int xDiff = destTrimmedRect.right - drawBufferWidth;
3302 destTrimmedRect.right = drawBufferWidth;
3303 sourceTrimmedRect.right -= xDiff;
3304 }
3305
3306 if (destTrimmedRect.top < 0)
3307 {
3308 int yDiff = 0 - destTrimmedRect.top;
3309 destTrimmedRect.top = 0;
3310 sourceTrimmedRect.top += yDiff;
3311 }
3312
3313 if (destTrimmedRect.bottom > drawBufferHeight)
3314 {
3315 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3316 destTrimmedRect.bottom = drawBufferHeight;
3317 sourceTrimmedRect.bottom -= yDiff;
3318 }
3319
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003320 bool partialBufferCopy = false;
3321 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3322 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3323 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3324 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3325 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3326 {
3327 partialBufferCopy = true;
3328 }
3329
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003330 if (mask & GL_COLOR_BUFFER_BIT)
3331 {
3332 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3333 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3334 {
3335 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3336 return error(GL_INVALID_OPERATION);
3337 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003338
3339 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3340 {
3341 return error(GL_INVALID_OPERATION);
3342 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003343
3344 blitRenderTarget = true;
3345
3346 }
3347
3348 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3349 {
3350 DepthStencilbuffer *readDSBuffer = NULL;
3351 DepthStencilbuffer *drawDSBuffer = NULL;
3352
3353 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3354 // both a depth and stencil buffer, it will be the same buffer.
3355
3356 if (mask & GL_DEPTH_BUFFER_BIT)
3357 {
3358 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3359 {
3360 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3361 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3362 {
3363 return error(GL_INVALID_OPERATION);
3364 }
3365
3366 blitDepthStencil = true;
3367 readDSBuffer = readFramebuffer->getDepthbuffer();
3368 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3369 }
3370 }
3371
3372 if (mask & GL_STENCIL_BUFFER_BIT)
3373 {
3374 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3375 {
3376 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3377 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3378 {
3379 return error(GL_INVALID_OPERATION);
3380 }
3381
3382 blitDepthStencil = true;
3383 readDSBuffer = readFramebuffer->getStencilbuffer();
3384 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3385 }
3386 }
3387
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003388 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003389 {
3390 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3391 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3392 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003393
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003394 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3395 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003396 {
3397 return error(GL_INVALID_OPERATION);
3398 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003399 }
3400
3401 if (blitRenderTarget || blitDepthStencil)
3402 {
3403 egl::Display *display = getDisplay();
3404 display->endScene();
3405
3406 if (blitRenderTarget)
3407 {
3408 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3409 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3410
3411 if (FAILED(result))
3412 {
3413 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3414 return;
3415 }
3416 }
3417
3418 if (blitDepthStencil)
3419 {
3420 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3421
3422 if (FAILED(result))
3423 {
3424 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3425 return;
3426 }
3427 }
3428 }
3429}
3430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003431}
3432
3433extern "C"
3434{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003435gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003436{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003437 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003438}
3439
3440void glDestroyContext(gl::Context *context)
3441{
3442 delete context;
3443
3444 if (context == gl::getContext())
3445 {
3446 gl::makeCurrent(NULL, NULL, NULL);
3447 }
3448}
3449
3450void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3451{
3452 gl::makeCurrent(context, display, surface);
3453}
3454
3455gl::Context *glGetCurrentContext()
3456{
3457 return gl::getContext();
3458}
3459}