blob: 71e8f9bf508665d344cc00f336a9a497e32562bb [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000087 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000090
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.viewportX = 0;
92 mState.viewportY = 0;
93 mState.viewportWidth = config->mDisplayMode.Width;
94 mState.viewportHeight = config->mDisplayMode.Height;
95 mState.zNear = 0.0f;
96 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.scissorX = 0;
99 mState.scissorY = 0;
100 mState.scissorWidth = config->mDisplayMode.Width;
101 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.colorMaskRed = true;
104 mState.colorMaskGreen = true;
105 mState.colorMaskBlue = true;
106 mState.colorMaskAlpha = true;
107 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000109 if (shareContext != NULL)
110 {
111 mResourceManager = shareContext->mResourceManager;
112 mResourceManager->addRef();
113 }
114 else
115 {
116 mResourceManager = new ResourceManager();
117 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 // [OpenGL ES 2.0.24] section 3.7 page 83:
120 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
121 // and cube map texture state vectors respectively associated with them.
122 // In order that access to these initial textures not be lost, they are treated as texture
123 // objects all of whose names are 0.
124
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000125 mTexture2DZero.set(new Texture2D(0));
126 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000128 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000129 bindArrayBuffer(0);
130 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 bindTextureCubeMap(0);
132 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000133 bindReadFramebuffer(0);
134 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 bindRenderbuffer(0);
136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000139 mState.packAlignment = 4;
140 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000141
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 mBufferBackEnd = NULL;
143 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000144 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000145 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 mInvalidEnum = false;
148 mInvalidValue = false;
149 mInvalidOperation = false;
150 mOutOfMemory = false;
151 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000152
153 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000154
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000155 mSupportsCompressedTextures = false;
156 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000157 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000158 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000159 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162Context::~Context()
163{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000164 if (mState.currentProgram != 0)
165 {
166 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
167 if (programObject)
168 {
169 programObject->release();
170 }
171 mState.currentProgram = 0;
172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000174 while (!mFramebufferMap.empty())
175 {
176 deleteFramebuffer(mFramebufferMap.begin()->first);
177 }
178
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000179 while (!mFenceMap.empty())
180 {
181 deleteFence(mFenceMap.begin()->first);
182 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000183
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000184 while (!mMultiSampleSupport.empty())
185 {
186 delete [] mMultiSampleSupport.begin()->second;
187 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
188 }
189
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000190 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
191 {
192 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
193 {
194 mState.samplerTexture[type][sampler].set(NULL);
195 }
196 }
197
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000200 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000201 }
202
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
204 {
205 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
206 }
207
208 mState.arrayBuffer.set(NULL);
209 mState.elementArrayBuffer.set(NULL);
210 mState.texture2D.set(NULL);
211 mState.textureCubeMap.set(NULL);
212 mState.renderbuffer.set(NULL);
213
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000214 mTexture2DZero.set(NULL);
215 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000217 delete mBufferBackEnd;
218 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000219 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000220 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000221
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000222 if (mMaskedClearSavedState)
223 {
224 mMaskedClearSavedState->Release();
225 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000226
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000227 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228}
229
230void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
231{
232 IDirect3DDevice9 *device = display->getDevice();
233
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000234 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000235 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000236 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000237
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000238 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000243 const D3DFORMAT renderBufferFormats[] =
244 {
245 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000246 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000247 D3DFMT_R5G6B5,
248 D3DFMT_D24S8
249 };
250
251 int max = 0;
252 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
253 {
254 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
255 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
256 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
257
258 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
259 {
260 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
261 {
262 max = j;
263 }
264 }
265 }
266
267 mMaxSupportedSamples = max;
268
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000269 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000270 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000271 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
272 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000273
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000274 initExtensionString();
275
276 mState.viewportX = 0;
277 mState.viewportY = 0;
278 mState.viewportWidth = surface->getWidth();
279 mState.viewportHeight = surface->getHeight();
280
281 mState.scissorX = 0;
282 mState.scissorY = 0;
283 mState.scissorWidth = surface->getWidth();
284 mState.scissorHeight = surface->getHeight();
285
286 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000287 }
288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
290 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000291 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000294 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000295 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
297 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000299 if (defaultRenderTarget)
300 {
301 defaultRenderTarget->Release();
302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000304 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000306 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000308
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000309 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000310
311 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312}
313
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000314// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000315void Context::markAllStateDirty()
316{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000317 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000318 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000319 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000320 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000321 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000322
323 mClearStateDirty = true;
324 mCullStateDirty = true;
325 mDepthStateDirty = true;
326 mMaskStateDirty = true;
327 mBlendStateDirty = true;
328 mStencilStateDirty = true;
329 mPolygonOffsetStateDirty = true;
330 mScissorStateDirty = true;
331 mSampleStateDirty = true;
332 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000333 mFrontFaceDirty = true;
334
335 if (mBufferBackEnd != NULL)
336 {
337 mBufferBackEnd->invalidate();
338 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000339}
340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341void Context::setClearColor(float red, float green, float blue, float alpha)
342{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000343 mState.colorClearValue.red = red;
344 mState.colorClearValue.green = green;
345 mState.colorClearValue.blue = blue;
346 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
349void Context::setClearDepth(float depth)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearStencil(int stencil)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.stencilClearValue = stencil;
357}
358
359void Context::setCullFace(bool enabled)
360{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000361 if (mState.cullFace != enabled)
362 {
363 mState.cullFace = enabled;
364 mCullStateDirty = true;
365 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000366}
367
368bool Context::isCullFaceEnabled() const
369{
370 return mState.cullFace;
371}
372
373void Context::setCullMode(GLenum mode)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.cullMode != mode)
376 {
377 mState.cullMode = mode;
378 mCullStateDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382void Context::setFrontFace(GLenum front)
383{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000384 if (mState.frontFace != front)
385 {
386 mState.frontFace = front;
387 mFrontFaceDirty = true;
388 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389}
390
391void Context::setDepthTest(bool enabled)
392{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393 if (mState.depthTest != enabled)
394 {
395 mState.depthTest = enabled;
396 mDepthStateDirty = true;
397 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000398}
399
400bool Context::isDepthTestEnabled() const
401{
402 return mState.depthTest;
403}
404
405void Context::setDepthFunc(GLenum depthFunc)
406{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000407 if (mState.depthFunc != depthFunc)
408 {
409 mState.depthFunc = depthFunc;
410 mDepthStateDirty = true;
411 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412}
413
414void Context::setDepthRange(float zNear, float zFar)
415{
416 mState.zNear = zNear;
417 mState.zFar = zFar;
418}
419
420void Context::setBlend(bool enabled)
421{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000422 if (mState.blend != enabled)
423 {
424 mState.blend = enabled;
425 mBlendStateDirty = true;
426 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000427}
428
429bool Context::isBlendEnabled() const
430{
431 return mState.blend;
432}
433
434void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
435{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000436 if (mState.sourceBlendRGB != sourceRGB ||
437 mState.sourceBlendAlpha != sourceAlpha ||
438 mState.destBlendRGB != destRGB ||
439 mState.destBlendAlpha != destAlpha)
440 {
441 mState.sourceBlendRGB = sourceRGB;
442 mState.destBlendRGB = destRGB;
443 mState.sourceBlendAlpha = sourceAlpha;
444 mState.destBlendAlpha = destAlpha;
445 mBlendStateDirty = true;
446 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000447}
448
449void Context::setBlendColor(float red, float green, float blue, float alpha)
450{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000451 if (mState.blendColor.red != red ||
452 mState.blendColor.green != green ||
453 mState.blendColor.blue != blue ||
454 mState.blendColor.alpha != alpha)
455 {
456 mState.blendColor.red = red;
457 mState.blendColor.green = green;
458 mState.blendColor.blue = blue;
459 mState.blendColor.alpha = alpha;
460 mBlendStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
465{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 if (mState.blendEquationRGB != rgbEquation ||
467 mState.blendEquationAlpha != alphaEquation)
468 {
469 mState.blendEquationRGB = rgbEquation;
470 mState.blendEquationAlpha = alphaEquation;
471 mBlendStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475void Context::setStencilTest(bool enabled)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.stencilTest != enabled)
478 {
479 mState.stencilTest = enabled;
480 mStencilStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484bool Context::isStencilTestEnabled() const
485{
486 return mState.stencilTest;
487}
488
489void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.stencilFunc != stencilFunc ||
492 mState.stencilRef != stencilRef ||
493 mState.stencilMask != stencilMask)
494 {
495 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000496 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 mState.stencilMask = stencilMask;
498 mStencilStateDirty = true;
499 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000500}
501
502void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
503{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000504 if (mState.stencilBackFunc != stencilBackFunc ||
505 mState.stencilBackRef != stencilBackRef ||
506 mState.stencilBackMask != stencilBackMask)
507 {
508 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000509 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 mState.stencilBackMask = stencilBackMask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilWritemask(GLuint stencilWritemask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilWritemask != stencilWritemask)
518 {
519 mState.stencilWritemask = stencilWritemask;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
525{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000526 if (mState.stencilBackWritemask != stencilBackWritemask)
527 {
528 mState.stencilBackWritemask = stencilBackWritemask;
529 mStencilStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.stencilFail != stencilFail ||
536 mState.stencilPassDepthFail != stencilPassDepthFail ||
537 mState.stencilPassDepthPass != stencilPassDepthPass)
538 {
539 mState.stencilFail = stencilFail;
540 mState.stencilPassDepthFail = stencilPassDepthFail;
541 mState.stencilPassDepthPass = stencilPassDepthPass;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
547{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000548 if (mState.stencilBackFail != stencilBackFail ||
549 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
550 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
551 {
552 mState.stencilBackFail = stencilBackFail;
553 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
554 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
555 mStencilStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559void Context::setPolygonOffsetFill(bool enabled)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.polygonOffsetFill != enabled)
562 {
563 mState.polygonOffsetFill = enabled;
564 mPolygonOffsetStateDirty = true;
565 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000566}
567
568bool Context::isPolygonOffsetFillEnabled() const
569{
570 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000571
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.polygonOffsetFactor != factor ||
577 mState.polygonOffsetUnits != units)
578 {
579 mState.polygonOffsetFactor = factor;
580 mState.polygonOffsetUnits = units;
581 mPolygonOffsetStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585void Context::setSampleAlphaToCoverage(bool enabled)
586{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000587 if (mState.sampleAlphaToCoverage != enabled)
588 {
589 mState.sampleAlphaToCoverage = enabled;
590 mSampleStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594bool Context::isSampleAlphaToCoverageEnabled() const
595{
596 return mState.sampleAlphaToCoverage;
597}
598
599void Context::setSampleCoverage(bool enabled)
600{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000601 if (mState.sampleCoverage != enabled)
602 {
603 mState.sampleCoverage = enabled;
604 mSampleStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608bool Context::isSampleCoverageEnabled() const
609{
610 return mState.sampleCoverage;
611}
612
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000613void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.sampleCoverageValue != value ||
616 mState.sampleCoverageInvert != invert)
617 {
618 mState.sampleCoverageValue = value;
619 mState.sampleCoverageInvert = invert;
620 mSampleStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624void Context::setScissorTest(bool enabled)
625{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000626 if (mState.scissorTest != enabled)
627 {
628 mState.scissorTest = enabled;
629 mScissorStateDirty = true;
630 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000631}
632
633bool Context::isScissorTestEnabled() const
634{
635 return mState.scissorTest;
636}
637
638void Context::setDither(bool enabled)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.dither != enabled)
641 {
642 mState.dither = enabled;
643 mDitherStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647bool Context::isDitherEnabled() const
648{
649 return mState.dither;
650}
651
652void Context::setLineWidth(GLfloat width)
653{
654 mState.lineWidth = width;
655}
656
657void Context::setGenerateMipmapHint(GLenum hint)
658{
659 mState.generateMipmapHint = hint;
660}
661
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000662void Context::setFragmentShaderDerivativeHint(GLenum hint)
663{
664 mState.fragmentShaderDerivativeHint = hint;
665 // TODO: Propagate the hint to shader translator so we can write
666 // ddx, ddx_coarse, or ddx_fine depending on the hint.
667 // Ignore for now. It is valid for implementations to ignore hint.
668}
669
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
671{
672 mState.viewportX = x;
673 mState.viewportY = y;
674 mState.viewportWidth = width;
675 mState.viewportHeight = height;
676}
677
678void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000680 if (mState.scissorX != x || mState.scissorY != y ||
681 mState.scissorWidth != width || mState.scissorHeight != height)
682 {
683 mState.scissorX = x;
684 mState.scissorY = y;
685 mState.scissorWidth = width;
686 mState.scissorHeight = height;
687 mScissorStateDirty = true;
688 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000689}
690
691void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
694 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
695 {
696 mState.colorMaskRed = red;
697 mState.colorMaskGreen = green;
698 mState.colorMaskBlue = blue;
699 mState.colorMaskAlpha = alpha;
700 mMaskStateDirty = true;
701 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702}
703
704void Context::setDepthMask(bool mask)
705{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000706 if (mState.depthMask != mask)
707 {
708 mState.depthMask = mask;
709 mMaskStateDirty = true;
710 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713void Context::setActiveSampler(int active)
714{
715 mState.activeSampler = active;
716}
717
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000718GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000720 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000721}
722
723GLuint Context::getDrawFramebufferHandle() const
724{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000725 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000726}
727
728GLuint Context::getRenderbufferHandle() const
729{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000730 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733GLuint Context::getArrayBufferHandle() const
734{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000735 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000736}
737
738void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
739{
740 mState.vertexAttribute[attribNum].mEnabled = enabled;
741}
742
743const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
744{
745 return mState.vertexAttribute[attribNum];
746}
747
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749 GLsizei stride, const void *pointer)
750{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000751 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000752 mState.vertexAttribute[attribNum].mSize = size;
753 mState.vertexAttribute[attribNum].mType = type;
754 mState.vertexAttribute[attribNum].mNormalized = normalized;
755 mState.vertexAttribute[attribNum].mStride = stride;
756 mState.vertexAttribute[attribNum].mPointer = pointer;
757}
758
759const void *Context::getVertexAttribPointer(unsigned int attribNum) const
760{
761 return mState.vertexAttribute[attribNum].mPointer;
762}
763
764// returns entire set of attributes as a block
765const AttributeState *Context::getVertexAttribBlock()
766{
767 return mState.vertexAttribute;
768}
769
770void Context::setPackAlignment(GLint alignment)
771{
772 mState.packAlignment = alignment;
773}
774
775GLint Context::getPackAlignment() const
776{
777 return mState.packAlignment;
778}
779
780void Context::setUnpackAlignment(GLint alignment)
781{
782 mState.unpackAlignment = alignment;
783}
784
785GLint Context::getUnpackAlignment() const
786{
787 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790GLuint Context::createBuffer()
791{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000792 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795GLuint Context::createProgram()
796{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000797 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800GLuint Context::createShader(GLenum type)
801{
802 return mResourceManager->createShader(type);
803}
804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805GLuint Context::createTexture()
806{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000807 return mResourceManager->createTexture();
808}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000810GLuint Context::createRenderbuffer()
811{
812 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000815// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816GLuint Context::createFramebuffer()
817{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000818 unsigned int handle = 1;
819
820 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
821 {
822 handle++;
823 }
824
825 mFramebufferMap[handle] = NULL;
826
827 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828}
829
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000830GLuint Context::createFence()
831{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000832 unsigned int handle = 0;
833
834 while (mFenceMap.find(handle) != mFenceMap.end())
835 {
836 handle++;
837 }
838
839 mFenceMap[handle] = new Fence;
840
841 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000842}
843
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844void Context::deleteBuffer(GLuint buffer)
845{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000846 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847 {
848 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000850
851 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852}
853
854void Context::deleteShader(GLuint shader)
855{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
859void Context::deleteProgram(GLuint program)
860{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000861 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
864void Context::deleteTexture(GLuint texture)
865{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000866 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867 {
868 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870
871 mResourceManager->deleteTexture(texture);
872}
873
874void Context::deleteRenderbuffer(GLuint renderbuffer)
875{
876 if (mResourceManager->getRenderbuffer(renderbuffer))
877 {
878 detachRenderbuffer(renderbuffer);
879 }
880
881 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882}
883
884void Context::deleteFramebuffer(GLuint framebuffer)
885{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000886 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
887
888 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889 {
890 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000891
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000892 delete framebufferObject->second;
893 mFramebufferMap.erase(framebufferObject);
894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000896
897void Context::deleteFence(GLuint fence)
898{
899 FenceMap::iterator fenceObject = mFenceMap.find(fence);
900
901 if (fenceObject != mFenceMap.end())
902 {
903 delete fenceObject->second;
904 mFenceMap.erase(fenceObject);
905 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000906}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000908Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910 return mResourceManager->getBuffer(handle);
911}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913Shader *Context::getShader(GLuint handle)
914{
915 return mResourceManager->getShader(handle);
916}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918Program *Context::getProgram(GLuint handle)
919{
920 return mResourceManager->getProgram(handle);
921}
922
923Texture *Context::getTexture(GLuint handle)
924{
925 return mResourceManager->getTexture(handle);
926}
927
928Renderbuffer *Context::getRenderbuffer(GLuint handle)
929{
930 return mResourceManager->getRenderbuffer(handle);
931}
932
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000933Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000934{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935 return getFramebuffer(mState.readFramebuffer);
936}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000937
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938Framebuffer *Context::getDrawFramebuffer()
939{
940 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
943void Context::bindArrayBuffer(unsigned int buffer)
944{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000945 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000947 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
950void Context::bindElementArrayBuffer(unsigned int buffer)
951{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000952 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000954 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
956
957void Context::bindTexture2D(GLuint texture)
958{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000959 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000961 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000963 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
966void Context::bindTextureCubeMap(GLuint texture)
967{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000968 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000970 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000972 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000975void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000977 if (!getFramebuffer(framebuffer))
978 {
979 mFramebufferMap[framebuffer] = new Framebuffer();
980 }
981
982 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000983}
984
985void Context::bindDrawFramebuffer(GLuint framebuffer)
986{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000987 if (!getFramebuffer(framebuffer))
988 {
989 mFramebufferMap[framebuffer] = new Framebuffer();
990 }
991
992 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995void Context::bindRenderbuffer(GLuint renderbuffer)
996{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000997 mResourceManager->checkRenderbufferAllocation(renderbuffer);
998
999 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000}
1001
1002void Context::useProgram(GLuint program)
1003{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001004 GLuint priorProgram = mState.currentProgram;
1005 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001006
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001007 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001009 Program *newProgram = mResourceManager->getProgram(program);
1010 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1011
1012 if (newProgram)
1013 {
1014 newProgram->addRef();
1015 }
1016
1017 if (oldProgram)
1018 {
1019 oldProgram->release();
1020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022}
1023
1024void Context::setFramebufferZero(Framebuffer *buffer)
1025{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001026 delete mFramebufferMap[0];
1027 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001030void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1033 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001036Framebuffer *Context::getFramebuffer(unsigned int handle)
1037{
1038 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1039
1040 if (framebuffer == mFramebufferMap.end())
1041 {
1042 return NULL;
1043 }
1044 else
1045 {
1046 return framebuffer->second;
1047 }
1048}
1049
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001050Fence *Context::getFence(unsigned int handle)
1051{
1052 FenceMap::iterator fence = mFenceMap.find(handle);
1053
1054 if (fence == mFenceMap.end())
1055 {
1056 return NULL;
1057 }
1058 else
1059 {
1060 return fence->second;
1061 }
1062}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064Buffer *Context::getArrayBuffer()
1065{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001066 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
1069Buffer *Context::getElementArrayBuffer()
1070{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001071 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072}
1073
1074Program *Context::getCurrentProgram()
1075{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001076 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079Texture2D *Context::getTexture2D()
1080{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001081 if (mState.texture2D.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001083 return mTexture2DZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 }
1085
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001086 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
1089TextureCubeMap *Context::getTextureCubeMap()
1090{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 if (mState.textureCubeMap.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001093 return mTextureCubeMapZero.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094 }
1095
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001096 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001099Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001101 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001102
1103 if (texid == 0)
1104 {
1105 switch (type)
1106 {
1107 default: UNREACHABLE();
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00001108 case SAMPLER_2D: return mTexture2DZero.get();
1109 case SAMPLER_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001110 }
1111 }
1112
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001113 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001116bool Context::getBooleanv(GLenum pname, GLboolean *params)
1117{
1118 switch (pname)
1119 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001120 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1121 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1122 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001123 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001124 params[0] = mState.colorMaskRed;
1125 params[1] = mState.colorMaskGreen;
1126 params[2] = mState.colorMaskBlue;
1127 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001129 case GL_CULL_FACE: *params = mState.cullFace;
1130 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1131 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1132 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1133 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1134 case GL_STENCIL_TEST: *params = mState.stencilTest;
1135 case GL_DEPTH_TEST: *params = mState.depthTest;
1136 case GL_BLEND: *params = mState.blend;
1137 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001138 default:
1139 return false;
1140 }
1141
1142 return true;
1143}
1144
1145bool Context::getFloatv(GLenum pname, GLfloat *params)
1146{
1147 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1148 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1149 // GetIntegerv as its native query function. As it would require conversion in any
1150 // case, this should make no difference to the calling application.
1151 switch (pname)
1152 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001153 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1154 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1155 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1156 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1157 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001158 case GL_ALIASED_LINE_WIDTH_RANGE:
1159 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1160 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1161 break;
1162 case GL_ALIASED_POINT_SIZE_RANGE:
1163 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001164 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001165 break;
1166 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001167 params[0] = mState.zNear;
1168 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001169 break;
1170 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001171 params[0] = mState.colorClearValue.red;
1172 params[1] = mState.colorClearValue.green;
1173 params[2] = mState.colorClearValue.blue;
1174 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001176 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001177 params[0] = mState.blendColor.red;
1178 params[1] = mState.blendColor.green;
1179 params[2] = mState.blendColor.blue;
1180 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001181 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001182 default:
1183 return false;
1184 }
1185
1186 return true;
1187}
1188
1189bool Context::getIntegerv(GLenum pname, GLint *params)
1190{
1191 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1192 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1193 // GetIntegerv as its native query function. As it would require conversion in any
1194 // case, this should make no difference to the calling application. You may find it in
1195 // Context::getFloatv.
1196 switch (pname)
1197 {
1198 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1199 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1200 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1201 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1202 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1203 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1204 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1205 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001206 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001207 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001208 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1209 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001210 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001211 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1212 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001213 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001214 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1215 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1216 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1217 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001218 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001219 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1220 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1221 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1222 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1223 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1224 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1225 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1226 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1227 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1228 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1229 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1230 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1231 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1232 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1233 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1234 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1235 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1236 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1237 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1238 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1239 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1240 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1241 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1242 case GL_SUBPIXEL_BITS: *params = 4; break;
1243 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1244 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001245 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1246 {
1247 if (supportsCompressedTextures())
1248 {
1249 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1250 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1251 *params = 2;
1252 }
1253 else
1254 {
1255 *params = 0;
1256 }
1257 }
1258 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001259 case GL_MAX_SAMPLES_ANGLE:
1260 {
1261 GLsizei maxSamples = getMaxSupportedSamples();
1262 if (maxSamples != 0)
1263 {
1264 *params = maxSamples;
1265 }
1266 else
1267 {
1268 return false;
1269 }
1270
1271 break;
1272 }
1273 case GL_SAMPLE_BUFFERS:
1274 case GL_SAMPLES:
1275 {
1276 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1277 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1278 {
1279 switch (pname)
1280 {
1281 case GL_SAMPLE_BUFFERS:
1282 if (framebuffer->getSamples() != 0)
1283 {
1284 *params = 1;
1285 }
1286 else
1287 {
1288 *params = 0;
1289 }
1290 break;
1291 case GL_SAMPLES:
1292 *params = framebuffer->getSamples();
1293 break;
1294 }
1295 }
1296 else
1297 {
1298 *params = 0;
1299 }
1300 }
1301 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001302 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1303 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1304 case GL_MAX_VIEWPORT_DIMS:
1305 {
1306 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1307 params[0] = maxDimension;
1308 params[1] = maxDimension;
1309 }
1310 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001311 case GL_COMPRESSED_TEXTURE_FORMATS:
1312 {
1313 if (supportsCompressedTextures())
1314 {
1315 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1316 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1317 }
1318 }
1319 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 params[0] = mState.viewportX;
1322 params[1] = mState.viewportY;
1323 params[2] = mState.viewportWidth;
1324 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001325 break;
1326 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001327 params[0] = mState.scissorX;
1328 params[1] = mState.scissorY;
1329 params[2] = mState.scissorWidth;
1330 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001332 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1333 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 case GL_RED_BITS:
1335 case GL_GREEN_BITS:
1336 case GL_BLUE_BITS:
1337 case GL_ALPHA_BITS:
1338 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001339 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001340 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1341
1342 if (colorbuffer)
1343 {
1344 switch (pname)
1345 {
1346 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1347 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1348 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1349 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1350 }
1351 }
1352 else
1353 {
1354 *params = 0;
1355 }
1356 }
1357 break;
1358 case GL_DEPTH_BITS:
1359 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001360 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001361 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001362
1363 if (depthbuffer)
1364 {
1365 *params = depthbuffer->getDepthSize();
1366 }
1367 else
1368 {
1369 *params = 0;
1370 }
1371 }
1372 break;
1373 case GL_STENCIL_BITS:
1374 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001375 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001376 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001377
1378 if (stencilbuffer)
1379 {
1380 *params = stencilbuffer->getStencilSize();
1381 }
1382 else
1383 {
1384 *params = 0;
1385 }
1386 }
1387 break;
1388 case GL_TEXTURE_BINDING_2D:
1389 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001390 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 {
1392 error(GL_INVALID_OPERATION);
1393 return false;
1394 }
1395
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001396 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001397 }
1398 break;
1399 case GL_TEXTURE_BINDING_CUBE_MAP:
1400 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001401 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 {
1403 error(GL_INVALID_OPERATION);
1404 return false;
1405 }
1406
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001407 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001408 }
1409 break;
1410 default:
1411 return false;
1412 }
1413
1414 return true;
1415}
1416
1417bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1418{
1419 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1420 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1421 // to the fact that it is stored internally as a float, and so would require conversion
1422 // if returned from Context::getIntegerv. Since this conversion is already implemented
1423 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1424 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1425 // application.
1426 switch (pname)
1427 {
1428 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1429 case GL_SHADER_BINARY_FORMATS:
1430 {
1431 *type = GL_INT;
1432 *numParams = 0;
1433 }
1434 break;
1435 case GL_MAX_VERTEX_ATTRIBS:
1436 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1437 case GL_MAX_VARYING_VECTORS:
1438 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1439 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1440 case GL_MAX_TEXTURE_IMAGE_UNITS:
1441 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1442 case GL_MAX_RENDERBUFFER_SIZE:
1443 case GL_NUM_SHADER_BINARY_FORMATS:
1444 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1445 case GL_ARRAY_BUFFER_BINDING:
1446 case GL_FRAMEBUFFER_BINDING:
1447 case GL_RENDERBUFFER_BINDING:
1448 case GL_CURRENT_PROGRAM:
1449 case GL_PACK_ALIGNMENT:
1450 case GL_UNPACK_ALIGNMENT:
1451 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001452 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001453 case GL_RED_BITS:
1454 case GL_GREEN_BITS:
1455 case GL_BLUE_BITS:
1456 case GL_ALPHA_BITS:
1457 case GL_DEPTH_BITS:
1458 case GL_STENCIL_BITS:
1459 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1460 case GL_CULL_FACE_MODE:
1461 case GL_FRONT_FACE:
1462 case GL_ACTIVE_TEXTURE:
1463 case GL_STENCIL_FUNC:
1464 case GL_STENCIL_VALUE_MASK:
1465 case GL_STENCIL_REF:
1466 case GL_STENCIL_FAIL:
1467 case GL_STENCIL_PASS_DEPTH_FAIL:
1468 case GL_STENCIL_PASS_DEPTH_PASS:
1469 case GL_STENCIL_BACK_FUNC:
1470 case GL_STENCIL_BACK_VALUE_MASK:
1471 case GL_STENCIL_BACK_REF:
1472 case GL_STENCIL_BACK_FAIL:
1473 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1474 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1475 case GL_DEPTH_FUNC:
1476 case GL_BLEND_SRC_RGB:
1477 case GL_BLEND_SRC_ALPHA:
1478 case GL_BLEND_DST_RGB:
1479 case GL_BLEND_DST_ALPHA:
1480 case GL_BLEND_EQUATION_RGB:
1481 case GL_BLEND_EQUATION_ALPHA:
1482 case GL_STENCIL_WRITEMASK:
1483 case GL_STENCIL_BACK_WRITEMASK:
1484 case GL_STENCIL_CLEAR_VALUE:
1485 case GL_SUBPIXEL_BITS:
1486 case GL_MAX_TEXTURE_SIZE:
1487 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1488 case GL_SAMPLE_BUFFERS:
1489 case GL_SAMPLES:
1490 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1491 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1492 case GL_TEXTURE_BINDING_2D:
1493 case GL_TEXTURE_BINDING_CUBE_MAP:
1494 {
1495 *type = GL_INT;
1496 *numParams = 1;
1497 }
1498 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001499 case GL_MAX_SAMPLES_ANGLE:
1500 {
1501 if (getMaxSupportedSamples() != 0)
1502 {
1503 *type = GL_INT;
1504 *numParams = 1;
1505 }
1506 else
1507 {
1508 return false;
1509 }
1510 }
1511 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001512 case GL_MAX_VIEWPORT_DIMS:
1513 {
1514 *type = GL_INT;
1515 *numParams = 2;
1516 }
1517 break;
1518 case GL_VIEWPORT:
1519 case GL_SCISSOR_BOX:
1520 {
1521 *type = GL_INT;
1522 *numParams = 4;
1523 }
1524 break;
1525 case GL_SHADER_COMPILER:
1526 case GL_SAMPLE_COVERAGE_INVERT:
1527 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001528 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1529 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1530 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1531 case GL_SAMPLE_COVERAGE:
1532 case GL_SCISSOR_TEST:
1533 case GL_STENCIL_TEST:
1534 case GL_DEPTH_TEST:
1535 case GL_BLEND:
1536 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001537 {
1538 *type = GL_BOOL;
1539 *numParams = 1;
1540 }
1541 break;
1542 case GL_COLOR_WRITEMASK:
1543 {
1544 *type = GL_BOOL;
1545 *numParams = 4;
1546 }
1547 break;
1548 case GL_POLYGON_OFFSET_FACTOR:
1549 case GL_POLYGON_OFFSET_UNITS:
1550 case GL_SAMPLE_COVERAGE_VALUE:
1551 case GL_DEPTH_CLEAR_VALUE:
1552 case GL_LINE_WIDTH:
1553 {
1554 *type = GL_FLOAT;
1555 *numParams = 1;
1556 }
1557 break;
1558 case GL_ALIASED_LINE_WIDTH_RANGE:
1559 case GL_ALIASED_POINT_SIZE_RANGE:
1560 case GL_DEPTH_RANGE:
1561 {
1562 *type = GL_FLOAT;
1563 *numParams = 2;
1564 }
1565 break;
1566 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001567 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 {
1569 *type = GL_FLOAT;
1570 *numParams = 4;
1571 }
1572 break;
1573 default:
1574 return false;
1575 }
1576
1577 return true;
1578}
1579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580// Applies the render target surface, depth stencil surface, viewport rectangle and
1581// scissor rectangle to the Direct3D 9 device
1582bool Context::applyRenderTarget(bool ignoreViewport)
1583{
1584 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001585
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001586 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
1588 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1589 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001590 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1591
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 return false;
1593 }
1594
1595 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001596
1597 if (!renderTarget)
1598 {
1599 return false; // Context must be lost
1600 }
1601
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001602 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001604 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1605 if (renderTargetSerial != mAppliedRenderTargetSerial)
1606 {
1607 device->SetRenderTarget(0, renderTarget);
1608 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001609 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001610 }
1611
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001612 unsigned int depthbufferSerial = 0;
1613 unsigned int stencilbufferSerial = 0;
1614 if (framebufferObject->getDepthbufferType() != GL_NONE)
1615 {
1616 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1617 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1618 }
1619 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1620 {
1621 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1622 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1623 }
1624
1625 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001626 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001627 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001628 {
1629 device->SetDepthStencilSurface(depthStencil);
1630 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001631 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001632 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
1635 D3DVIEWPORT9 viewport;
1636 D3DSURFACE_DESC desc;
1637 renderTarget->GetDesc(&desc);
1638
1639 if (ignoreViewport)
1640 {
1641 viewport.X = 0;
1642 viewport.Y = 0;
1643 viewport.Width = desc.Width;
1644 viewport.Height = desc.Height;
1645 viewport.MinZ = 0.0f;
1646 viewport.MaxZ = 1.0f;
1647 }
1648 else
1649 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001650 viewport.X = std::max(mState.viewportX, 0);
1651 viewport.Y = std::max(mState.viewportY, 0);
1652 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1653 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1654 viewport.MinZ = clamp01(mState.zNear);
1655 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 }
1657
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001658 if (viewport.Width <= 0 || viewport.Height <= 0)
1659 {
1660 return false; // Nothing to render
1661 }
1662
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663 device->SetViewport(&viewport);
1664
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001665 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001667 if (mState.scissorTest)
1668 {
1669 RECT rect = {mState.scissorX,
1670 mState.scissorY,
1671 mState.scissorX + mState.scissorWidth,
1672 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001673 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1674 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 device->SetScissorRect(&rect);
1676 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1677 }
1678 else
1679 {
1680 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1681 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001683 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 }
1685
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001686 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 Program *programObject = getCurrentProgram();
1689
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001690 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001691 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001693
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001694 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001695 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1696 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1697 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001698 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001701 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001702 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1703
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001704 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001706
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001707 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001708 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001709
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001710 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001711 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001712 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713 }
1714
1715 return true;
1716}
1717
1718// 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 +00001719void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720{
1721 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001722 Program *programObject = getCurrentProgram();
1723
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001724 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001725 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001728 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001729 GLint alwaysFront = !isTriangleMode(drawMode);
1730 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1731
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001732 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001733
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001734 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001736 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740 else
1741 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 }
1744
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 mCullStateDirty = false;
1746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001748 if (mDepthStateDirty)
1749 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001750 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1753 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755 else
1756 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001757 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001759
1760 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 }
1762
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1768
1769 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1770 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1771 {
1772 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1773 }
1774 else
1775 {
1776 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1777 unorm<8>(mState.blendColor.alpha),
1778 unorm<8>(mState.blendColor.alpha),
1779 unorm<8>(mState.blendColor.alpha)));
1780 }
1781
1782 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1783 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1784 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1785
1786 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1787 mState.destBlendRGB != mState.destBlendAlpha ||
1788 mState.blendEquationRGB != mState.blendEquationAlpha)
1789 {
1790 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1791
1792 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1793 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1794 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1795
1796 }
1797 else
1798 {
1799 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1800 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001801 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001802 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001803 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001804 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001805 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806
1807 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808 }
1809
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810 if (mStencilStateDirty || mFrontFaceDirty)
1811 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001812 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001813 {
1814 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1815 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1816
1817 // FIXME: Unsupported by D3D9
1818 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1819 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1820 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1821 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1822 mState.stencilRef != mState.stencilBackRef ||
1823 mState.stencilMask != mState.stencilBackMask)
1824 {
1825 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1826 return error(GL_INVALID_OPERATION);
1827 }
1828
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001829 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001830 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001831 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1832
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001833 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1834 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1835 es2dx::ConvertComparison(mState.stencilFunc));
1836
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001837 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 +00001838 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1839
1840 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1841 es2dx::ConvertStencilOp(mState.stencilFail));
1842 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1843 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1844 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1845 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1846
1847 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1848 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1849 es2dx::ConvertComparison(mState.stencilBackFunc));
1850
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001851 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 +00001852 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1853
1854 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1855 es2dx::ConvertStencilOp(mState.stencilBackFail));
1856 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1857 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1858 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1859 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1860 }
1861 else
1862 {
1863 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1864 }
1865
1866 mStencilStateDirty = false;
1867 }
1868
1869 if (mMaskStateDirty)
1870 {
1871 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1872 mState.colorMaskBlue, mState.colorMaskAlpha));
1873 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1874
1875 mMaskStateDirty = false;
1876 }
1877
1878 if (mPolygonOffsetStateDirty)
1879 {
1880 if (mState.polygonOffsetFill)
1881 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001882 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001883 if (depthbuffer)
1884 {
1885 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1886 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1887 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1888 }
1889 }
1890 else
1891 {
1892 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1893 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1894 }
1895
1896 mPolygonOffsetStateDirty = false;
1897 }
1898
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001899 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001901 if (framebufferObject->isMultisample())
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001902 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001903 if (mState.sampleAlphaToCoverage)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001904 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001905 FIXME("Sample alpha to coverage is unimplemented.");
1906 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001907
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001908 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1909 if (mState.sampleCoverage)
1910 {
1911 unsigned int mask = 0;
1912 if (mState.sampleCoverageValue != 0)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001913 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001914 float threshold = 0.5f;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001915
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001916 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001917 {
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001918 mask <<= 1;
1919
1920 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1921 {
1922 threshold += 1.0f;
1923 mask |= 1;
1924 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001925 }
1926 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001927
1928 if (mState.sampleCoverageInvert)
1929 {
1930 mask = ~mask;
1931 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001932
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00001933 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1934 }
1935 else
1936 {
1937 device->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1938 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001939 }
1940 else
1941 {
1942 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001943 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001944
1945 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946 }
1947
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001948 if (mDitherStateDirty)
1949 {
1950 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1951
1952 mDitherStateDirty = false;
1953 }
1954
1955 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956}
1957
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001958// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001959void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001961 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001963 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001965 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966 }
1967 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001968}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001970GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001971{
1972 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1973
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001974 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001975 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001976 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001977 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001978 }
1979
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001980 lookupAttributeMapping(translated);
1981
1982 mBufferBackEnd->setupAttributesPreDraw(translated);
1983
1984 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1985 {
1986 if (translated[i].enabled && translated[i].nonArray)
1987 {
1988 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1989 if (err != GL_NO_ERROR)
1990 {
1991 return err;
1992 }
1993
1994 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1995
1996 *useIndexing = true;
1997 return GL_NO_ERROR;
1998 }
1999 }
2000
2001 *useIndexing = false;
2002 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002003}
2004
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002005GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002006{
2007 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2008
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002009 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002010
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002011 if (err == GL_NO_ERROR)
2012 {
2013 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002014
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002015 mBufferBackEnd->setupAttributesPreDraw(translated);
2016 }
2017
2018 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002019}
2020
2021// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002022GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002023{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002024 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002025
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002026 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002027 {
2028 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2029 }
2030
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002031 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032}
2033
2034// Applies the shaders and shader constants to the Direct3D 9 device
2035void Context::applyShaders()
2036{
2037 IDirect3DDevice9 *device = getDevice();
2038 Program *programObject = getCurrentProgram();
2039 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2040 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2041
2042 device->SetVertexShader(vertexShader);
2043 device->SetPixelShader(pixelShader);
2044
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002045 if (programObject->getSerial() != mAppliedProgram)
2046 {
2047 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002048 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002049 mAppliedProgram = programObject->getSerial();
2050 }
2051
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 programObject->applyUniforms();
2053}
2054
2055// Applies the textures and sampler states to the Direct3D 9 device
2056void Context::applyTextures()
2057{
2058 IDirect3DDevice9 *device = getDevice();
2059 Program *programObject = getCurrentProgram();
2060
2061 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2062 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002063 int textureUnit = programObject->getSamplerMapping(sampler);
2064 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002066 SamplerType textureType = programObject->getSamplerType(sampler);
2067
2068 Texture *texture = getSamplerTexture(textureUnit, textureType);
2069
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002070 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002071 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002072 if (texture->isComplete())
2073 {
2074 GLenum wrapS = texture->getWrapS();
2075 GLenum wrapT = texture->getWrapT();
2076 GLenum minFilter = texture->getMinFilter();
2077 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002079 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2080 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002082 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2083 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2084 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2085 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2086 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002088 device->SetTexture(sampler, texture->getTexture());
2089 }
2090 else
2091 {
2092 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2093 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002094 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002095
2096 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002098 else
2099 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002100 if (programObject->isSamplerDirty(sampler))
2101 {
2102 device->SetTexture(sampler, NULL);
2103 programObject->setSamplerDirty(sampler, false);
2104 }
2105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 }
2107}
2108
2109void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2110{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002111 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002112
2113 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2114 {
2115 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2116 }
2117
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002118 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2119 {
2120 return error(GL_INVALID_OPERATION);
2121 }
2122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002124
2125 if (!renderTarget)
2126 {
2127 return; // Context must be lost, return silently
2128 }
2129
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 IDirect3DDevice9 *device = getDevice();
2131
2132 D3DSURFACE_DESC desc;
2133 renderTarget->GetDesc(&desc);
2134
2135 IDirect3DSurface9 *systemSurface;
2136 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2137
2138 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2139 {
2140 return error(GL_OUT_OF_MEMORY);
2141 }
2142
2143 ASSERT(SUCCEEDED(result));
2144
2145 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2146 {
2147 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2148 }
2149
2150 result = device->GetRenderTargetData(renderTarget, systemSurface);
2151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 if (FAILED(result))
2153 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 systemSurface->Release();
2155
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002156 switch (result)
2157 {
2158 case D3DERR_DRIVERINTERNALERROR:
2159 case D3DERR_DEVICELOST:
2160 return error(GL_OUT_OF_MEMORY);
2161 default:
2162 UNREACHABLE();
2163 return; // No sensible error to generate
2164 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165 }
2166
2167 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002168 RECT rect = {std::max(x, 0),
2169 std::max(y, 0),
2170 std::min(x + width, (int)desc.Width),
2171 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172
2173 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2174
2175 if (FAILED(result))
2176 {
2177 UNREACHABLE();
2178 systemSurface->Release();
2179
2180 return; // No sensible error to generate
2181 }
2182
2183 unsigned char *source = (unsigned char*)lock.pBits;
2184 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002185 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002187 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189 for (int j = 0; j < rect.bottom - rect.top; j++)
2190 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002191 if (desc.Format == D3DFMT_A8R8G8B8 &&
2192 format == GL_BGRA_EXT &&
2193 type == GL_UNSIGNED_BYTE)
2194 {
2195 // Fast path for EXT_read_format_bgra, given
2196 // an RGBA source buffer. Note that buffers with no
2197 // alpha go through the slow path below.
2198 memcpy(dest + j * outputPitch,
2199 source + j * lock.Pitch,
2200 (rect.right - rect.left) * 4);
2201 continue;
2202 }
2203
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204 for (int i = 0; i < rect.right - rect.left; i++)
2205 {
2206 float r;
2207 float g;
2208 float b;
2209 float a;
2210
2211 switch (desc.Format)
2212 {
2213 case D3DFMT_R5G6B5:
2214 {
2215 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2216
2217 a = 1.0f;
2218 b = (rgb & 0x001F) * (1.0f / 0x001F);
2219 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2220 r = (rgb & 0xF800) * (1.0f / 0xF800);
2221 }
2222 break;
2223 case D3DFMT_X1R5G5B5:
2224 {
2225 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2226
2227 a = 1.0f;
2228 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2229 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2230 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2231 }
2232 break;
2233 case D3DFMT_A1R5G5B5:
2234 {
2235 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2236
2237 a = (argb & 0x8000) ? 1.0f : 0.0f;
2238 b = (argb & 0x001F) * (1.0f / 0x001F);
2239 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2240 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2241 }
2242 break;
2243 case D3DFMT_A8R8G8B8:
2244 {
2245 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2246
2247 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2248 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2249 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2250 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2251 }
2252 break;
2253 case D3DFMT_X8R8G8B8:
2254 {
2255 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2256
2257 a = 1.0f;
2258 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2259 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2260 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2261 }
2262 break;
2263 case D3DFMT_A2R10G10B10:
2264 {
2265 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2266
2267 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2268 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2269 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2270 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2271 }
2272 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002273 case D3DFMT_A32B32G32R32F:
2274 {
2275 // float formats in D3D are stored rgba, rather than the other way round
2276 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2277 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2278 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2279 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2280 }
2281 break;
2282 case D3DFMT_A16B16G16R16F:
2283 {
2284 // float formats in D3D are stored rgba, rather than the other way round
2285 float abgr[4];
2286
2287 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2288
2289 a = abgr[3];
2290 b = abgr[2];
2291 g = abgr[1];
2292 r = abgr[0];
2293 }
2294 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 default:
2296 UNIMPLEMENTED(); // FIXME
2297 UNREACHABLE();
2298 }
2299
2300 switch (format)
2301 {
2302 case GL_RGBA:
2303 switch (type)
2304 {
2305 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002306 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2307 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2308 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2309 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 break;
2311 default: UNREACHABLE();
2312 }
2313 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002314 case GL_BGRA_EXT:
2315 switch (type)
2316 {
2317 case GL_UNSIGNED_BYTE:
2318 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2319 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2320 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2321 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2322 break;
2323 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2324 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2325 // this type is packed as follows:
2326 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2327 // --------------------------------------------------------------------------------
2328 // | 4th | 3rd | 2nd | 1st component |
2329 // --------------------------------------------------------------------------------
2330 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2331 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2332 ((unsigned short)(15 * a + 0.5f) << 12)|
2333 ((unsigned short)(15 * r + 0.5f) << 8) |
2334 ((unsigned short)(15 * g + 0.5f) << 4) |
2335 ((unsigned short)(15 * b + 0.5f) << 0);
2336 break;
2337 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2338 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2339 // this type is packed as follows:
2340 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2341 // --------------------------------------------------------------------------------
2342 // | 4th | 3rd | 2nd | 1st component |
2343 // --------------------------------------------------------------------------------
2344 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2345 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2346 ((unsigned short)( a + 0.5f) << 15) |
2347 ((unsigned short)(31 * r + 0.5f) << 10) |
2348 ((unsigned short)(31 * g + 0.5f) << 5) |
2349 ((unsigned short)(31 * b + 0.5f) << 0);
2350 break;
2351 default: UNREACHABLE();
2352 }
2353 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002354 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355 switch (type)
2356 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002357 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002358 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2359 ((unsigned short)(31 * b + 0.5f) << 0) |
2360 ((unsigned short)(63 * g + 0.5f) << 5) |
2361 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 break;
2363 default: UNREACHABLE();
2364 }
2365 break;
2366 default: UNREACHABLE();
2367 }
2368 }
2369 }
2370
2371 systemSurface->UnlockRect();
2372
2373 systemSurface->Release();
2374}
2375
2376void Context::clear(GLbitfield mask)
2377{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002378 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002379
2380 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2381 {
2382 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2383
2384 return;
2385 }
2386
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002387 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 IDirect3DDevice9 *device = getDevice();
2389 DWORD flags = 0;
2390
2391 if (mask & GL_COLOR_BUFFER_BIT)
2392 {
2393 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002394
2395 if (framebufferObject->getColorbufferType() != GL_NONE)
2396 {
2397 flags |= D3DCLEAR_TARGET;
2398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399 }
2400
2401 if (mask & GL_DEPTH_BUFFER_BIT)
2402 {
2403 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002404 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 {
2406 flags |= D3DCLEAR_ZBUFFER;
2407 }
2408 }
2409
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 GLuint stencilUnmasked = 0x0;
2411
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002412 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002415 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002417 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2418 D3DSURFACE_DESC desc;
2419 depthStencil->GetDesc(&desc);
2420
2421 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2422 stencilUnmasked = (0x1 << stencilSize) - 1;
2423
2424 if (stencilUnmasked != 0x0)
2425 {
2426 flags |= D3DCLEAR_STENCIL;
2427 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 }
2429 }
2430
2431 if (mask != 0)
2432 {
2433 return error(GL_INVALID_VALUE);
2434 }
2435
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002436 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2437 {
2438 return;
2439 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002441 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2442 unorm<8>(mState.colorClearValue.red),
2443 unorm<8>(mState.colorClearValue.green),
2444 unorm<8>(mState.colorClearValue.blue));
2445 float depth = clamp01(mState.depthClearValue);
2446 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447
2448 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2449
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002450 if (!renderTarget)
2451 {
2452 return; // Context must be lost, return silently
2453 }
2454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455 D3DSURFACE_DESC desc;
2456 renderTarget->GetDesc(&desc);
2457
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002458 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459
2460 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002461 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002463 !(mState.colorMaskRed && mState.colorMaskGreen &&
2464 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465
2466 if (needMaskedColorClear || needMaskedStencilClear)
2467 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002468 // State which is altered in all paths from this point to the clear call is saved.
2469 // State which is altered in only some paths will be flagged dirty in the case that
2470 // that path is taken.
2471 HRESULT hr;
2472 if (mMaskedClearSavedState == NULL)
2473 {
2474 hr = device->BeginStateBlock();
2475 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2476
2477 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 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2486 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2487 device->SetPixelShader(NULL);
2488 device->SetVertexShader(NULL);
2489 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2490 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002491 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002492
2493 hr = device->EndStateBlock(&mMaskedClearSavedState);
2494 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2495 }
2496
2497 ASSERT(mMaskedClearSavedState != NULL);
2498
2499 if (mMaskedClearSavedState != NULL)
2500 {
2501 hr = mMaskedClearSavedState->Capture();
2502 ASSERT(SUCCEEDED(hr));
2503 }
2504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2506 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2507 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2508 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2509 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2510 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2511 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2512 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2513
2514 if (flags & D3DCLEAR_TARGET)
2515 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002516 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2517 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2518 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2519 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 }
2521 else
2522 {
2523 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2524 }
2525
2526 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2527 {
2528 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2529 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2530 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2531 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002532 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002533 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2535 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002536 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 }
2538 else
2539 {
2540 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2541 }
2542
2543 device->SetPixelShader(NULL);
2544 device->SetVertexShader(NULL);
2545 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002546 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.comd10f8692010-09-16 19:56:52 +00002547 device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002549 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 {
2551 float x, y, z, w;
2552 D3DCOLOR diffuse;
2553 };
2554
2555 Vertex quad[4];
2556 quad[0].x = 0.0f;
2557 quad[0].y = (float)desc.Height;
2558 quad[0].z = 0.0f;
2559 quad[0].w = 1.0f;
2560 quad[0].diffuse = color;
2561
2562 quad[1].x = (float)desc.Width;
2563 quad[1].y = (float)desc.Height;
2564 quad[1].z = 0.0f;
2565 quad[1].w = 1.0f;
2566 quad[1].diffuse = color;
2567
2568 quad[2].x = 0.0f;
2569 quad[2].y = 0.0f;
2570 quad[2].z = 0.0f;
2571 quad[2].w = 1.0f;
2572 quad[2].diffuse = color;
2573
2574 quad[3].x = (float)desc.Width;
2575 quad[3].y = 0.0f;
2576 quad[3].z = 0.0f;
2577 quad[3].w = 1.0f;
2578 quad[3].diffuse = color;
2579
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002580 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582
2583 if (flags & D3DCLEAR_ZBUFFER)
2584 {
2585 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2586 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2587 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2588 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002589
2590 if (mMaskedClearSavedState != NULL)
2591 {
2592 mMaskedClearSavedState->Apply();
2593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002595 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002596 {
2597 device->Clear(0, NULL, flags, color, depth, stencil);
2598 }
2599}
2600
2601void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2602{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002603 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 {
2605 return error(GL_INVALID_OPERATION);
2606 }
2607
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002608 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 IDirect3DDevice9 *device = getDevice();
2610 D3DPRIMITIVETYPE primitiveType;
2611 int primitiveCount;
2612
2613 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2614 return error(GL_INVALID_ENUM);
2615
2616 if (primitiveCount <= 0)
2617 {
2618 return;
2619 }
2620
2621 if (!applyRenderTarget(false))
2622 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002623 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 }
2625
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002626 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002627
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002628 TranslatedIndexData indexInfo;
2629 bool useIndexing;
2630 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002631 if (err != GL_NO_ERROR)
2632 {
2633 return error(err);
2634 }
2635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636 applyShaders();
2637 applyTextures();
2638
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002639 if (!getCurrentProgram()->validateSamplers())
2640 {
2641 return error(GL_INVALID_OPERATION);
2642 }
2643
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002644 if (!cullSkipsDraw(mode))
2645 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002646 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002647 if (useIndexing)
2648 {
2649 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2650 }
2651 else
2652 {
2653 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2654 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002655 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656}
2657
2658void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2659{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002660 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 {
2662 return error(GL_INVALID_OPERATION);
2663 }
2664
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002665 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 {
2667 return error(GL_INVALID_OPERATION);
2668 }
2669
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002670 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 IDirect3DDevice9 *device = getDevice();
2672 D3DPRIMITIVETYPE primitiveType;
2673 int primitiveCount;
2674
2675 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2676 return error(GL_INVALID_ENUM);
2677
2678 if (primitiveCount <= 0)
2679 {
2680 return;
2681 }
2682
2683 if (!applyRenderTarget(false))
2684 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002685 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 }
2687
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002688 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002689
2690 TranslatedIndexData indexInfo;
2691 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2692 if (err != GL_NO_ERROR)
2693 {
2694 return error(err);
2695 }
2696
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002697 err = applyVertexBuffer(indexInfo);
2698 if (err != GL_NO_ERROR)
2699 {
2700 return error(err);
2701 }
2702
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 applyShaders();
2704 applyTextures();
2705
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002706 if (!getCurrentProgram()->validateSamplers())
2707 {
2708 return error(GL_INVALID_OPERATION);
2709 }
2710
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002711 if (!cullSkipsDraw(mode))
2712 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002713 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002714 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 +00002715 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716}
2717
2718void Context::finish()
2719{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002720 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 IDirect3DDevice9 *device = getDevice();
2722 IDirect3DQuery9 *occlusionQuery = NULL;
2723
2724 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2725
2726 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2727 {
2728 return error(GL_OUT_OF_MEMORY);
2729 }
2730
2731 ASSERT(SUCCEEDED(result));
2732
2733 if (occlusionQuery)
2734 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002735 IDirect3DStateBlock9 *savedState = NULL;
2736 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2737
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002738 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2739 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740
2741 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002742 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743 device->SetPixelShader(NULL);
2744 device->SetVertexShader(NULL);
2745 device->SetFVF(D3DFVF_XYZRHW);
2746 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002747 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002750 result = occlusionQuery->Issue(D3DISSUE_END);
2751 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002752
2753 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2754 {
2755 // Keep polling, but allow other threads to do something useful first
2756 Sleep(0);
2757 }
2758
2759 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002760
2761 if (savedState)
2762 {
2763 savedState->Apply();
2764 savedState->Release();
2765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 }
2767}
2768
2769void Context::flush()
2770{
2771 IDirect3DDevice9 *device = getDevice();
2772 IDirect3DQuery9 *eventQuery = NULL;
2773
2774 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2775
2776 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2777 {
2778 return error(GL_OUT_OF_MEMORY);
2779 }
2780
2781 ASSERT(SUCCEEDED(result));
2782
2783 if (eventQuery)
2784 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002785 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2786 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002788 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002790
2791 if (result == D3DERR_DEVICELOST)
2792 {
2793 error(GL_OUT_OF_MEMORY);
2794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795 }
2796}
2797
2798void Context::recordInvalidEnum()
2799{
2800 mInvalidEnum = true;
2801}
2802
2803void Context::recordInvalidValue()
2804{
2805 mInvalidValue = true;
2806}
2807
2808void Context::recordInvalidOperation()
2809{
2810 mInvalidOperation = true;
2811}
2812
2813void Context::recordOutOfMemory()
2814{
2815 mOutOfMemory = true;
2816}
2817
2818void Context::recordInvalidFramebufferOperation()
2819{
2820 mInvalidFramebufferOperation = true;
2821}
2822
2823// Get one of the recorded errors and clear its flag, if any.
2824// [OpenGL ES 2.0.24] section 2.5 page 13.
2825GLenum Context::getError()
2826{
2827 if (mInvalidEnum)
2828 {
2829 mInvalidEnum = false;
2830
2831 return GL_INVALID_ENUM;
2832 }
2833
2834 if (mInvalidValue)
2835 {
2836 mInvalidValue = false;
2837
2838 return GL_INVALID_VALUE;
2839 }
2840
2841 if (mInvalidOperation)
2842 {
2843 mInvalidOperation = false;
2844
2845 return GL_INVALID_OPERATION;
2846 }
2847
2848 if (mOutOfMemory)
2849 {
2850 mOutOfMemory = false;
2851
2852 return GL_OUT_OF_MEMORY;
2853 }
2854
2855 if (mInvalidFramebufferOperation)
2856 {
2857 mInvalidFramebufferOperation = false;
2858
2859 return GL_INVALID_FRAMEBUFFER_OPERATION;
2860 }
2861
2862 return GL_NO_ERROR;
2863}
2864
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002865bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002866{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002867 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002868}
2869
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002870int Context::getMaxSupportedSamples() const
2871{
2872 return mMaxSupportedSamples;
2873}
2874
2875int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2876{
2877 if (requested == 0)
2878 {
2879 return requested;
2880 }
2881
2882 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2883 if (itr == mMultiSampleSupport.end())
2884 {
2885 return -1;
2886 }
2887
2888 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2889 {
2890 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2891 {
2892 return i;
2893 }
2894 }
2895
2896 return -1;
2897}
2898
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002899bool Context::supportsEventQueries() const
2900{
2901 return mSupportsEventQueries;
2902}
2903
daniel@transgaming.com01868132010-08-24 19:21:17 +00002904bool Context::supportsCompressedTextures() const
2905{
2906 return mSupportsCompressedTextures;
2907}
2908
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002909bool Context::supportsFloatTextures() const
2910{
2911 return mSupportsFloatTextures;
2912}
2913
2914bool Context::supportsFloatLinearFilter() const
2915{
2916 return mSupportsFloatLinearFilter;
2917}
2918
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002919bool Context::supportsFloatRenderableTextures() const
2920{
2921 return mSupportsFloatRenderableTextures;
2922}
2923
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002924bool Context::supportsHalfFloatTextures() const
2925{
2926 return mSupportsHalfFloatTextures;
2927}
2928
2929bool Context::supportsHalfFloatLinearFilter() const
2930{
2931 return mSupportsHalfFloatLinearFilter;
2932}
2933
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002934bool Context::supportsHalfFloatRenderableTextures() const
2935{
2936 return mSupportsHalfFloatRenderableTextures;
2937}
2938
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002939void Context::detachBuffer(GLuint buffer)
2940{
2941 // [OpenGL ES 2.0.24] section 2.9 page 22:
2942 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2943 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2944
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002945 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002947 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002948 }
2949
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002950 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002951 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002952 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002953 }
2954
2955 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2956 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002957 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002958 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002959 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002960 }
2961 }
2962}
2963
2964void Context::detachTexture(GLuint texture)
2965{
2966 // [OpenGL ES 2.0.24] section 3.8 page 84:
2967 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2968 // rebound to texture object zero
2969
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002970 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002971 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002972 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002973 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002974 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002975 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002976 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002977 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002978 }
2979 }
2980
2981 // [OpenGL ES 2.0.24] section 4.4 page 112:
2982 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2983 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2984 // image was attached in the currently bound framebuffer.
2985
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002986 Framebuffer *readFramebuffer = getReadFramebuffer();
2987 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002988
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002989 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002990 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002991 readFramebuffer->detachTexture(texture);
2992 }
2993
2994 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2995 {
2996 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997 }
2998}
2999
3000void Context::detachFramebuffer(GLuint framebuffer)
3001{
3002 // [OpenGL ES 2.0.24] section 4.4 page 107:
3003 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3004 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3005
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003006 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003007 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003008 bindReadFramebuffer(0);
3009 }
3010
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003011 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003012 {
3013 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003014 }
3015}
3016
3017void Context::detachRenderbuffer(GLuint renderbuffer)
3018{
3019 // [OpenGL ES 2.0.24] section 4.4 page 109:
3020 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3021 // had been executed with the target RENDERBUFFER and name of zero.
3022
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003023 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003024 {
3025 bindRenderbuffer(0);
3026 }
3027
3028 // [OpenGL ES 2.0.24] section 4.4 page 111:
3029 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3030 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3031 // point to which this image was attached in the currently bound framebuffer.
3032
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003033 Framebuffer *readFramebuffer = getReadFramebuffer();
3034 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003035
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003036 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003038 readFramebuffer->detachRenderbuffer(renderbuffer);
3039 }
3040
3041 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3042 {
3043 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003044 }
3045}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003046
3047Texture *Context::getIncompleteTexture(SamplerType type)
3048{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003049 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003050
3051 if (t == NULL)
3052 {
3053 static const GLubyte color[] = { 0, 0, 0, 255 };
3054
3055 switch (type)
3056 {
3057 default:
3058 UNREACHABLE();
3059 // default falls through to SAMPLER_2D
3060
3061 case SAMPLER_2D:
3062 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003063 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003064 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003065 t = incomplete2d;
3066 }
3067 break;
3068
3069 case SAMPLER_CUBE:
3070 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003071 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003072
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003073 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3074 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3075 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3076 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3077 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3078 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003079
3080 t = incompleteCube;
3081 }
3082 break;
3083 }
3084
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003085 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003086 }
3087
3088 return t;
3089}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003090
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003091bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003092{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003093 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003094}
3095
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003096bool Context::isTriangleMode(GLenum drawMode)
3097{
3098 switch (drawMode)
3099 {
3100 case GL_TRIANGLES:
3101 case GL_TRIANGLE_FAN:
3102 case GL_TRIANGLE_STRIP:
3103 return true;
3104 case GL_POINTS:
3105 case GL_LINES:
3106 case GL_LINE_LOOP:
3107 case GL_LINE_STRIP:
3108 return false;
3109 default: UNREACHABLE();
3110 }
3111
3112 return false;
3113}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003114
3115void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3116{
3117 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3118
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003119 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3120 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3121 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3122 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003123
3124 mVertexDataManager->dirtyCurrentValues();
3125}
3126
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003127void Context::initExtensionString()
3128{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003129 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003130 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3131 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003132 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003133 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003134 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003135
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003136 if (supportsEventQueries())
3137 {
3138 mExtensionString += "GL_NV_fence ";
3139 }
3140
daniel@transgaming.com01868132010-08-24 19:21:17 +00003141 if (supportsCompressedTextures())
3142 {
3143 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3144 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003145
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003146 if (supportsFloatTextures())
3147 {
3148 mExtensionString += "GL_OES_texture_float ";
3149 }
3150
3151 if (supportsHalfFloatTextures())
3152 {
3153 mExtensionString += "GL_OES_texture_half_float ";
3154 }
3155
3156 if (supportsFloatLinearFilter())
3157 {
3158 mExtensionString += "GL_OES_texture_float_linear ";
3159 }
3160
3161 if (supportsHalfFloatLinearFilter())
3162 {
3163 mExtensionString += "GL_OES_texture_half_float_linear ";
3164 }
3165
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003166 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003167 {
3168 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3169 }
3170
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003171 if (mBufferBackEnd->supportIntIndices())
3172 {
3173 mExtensionString += "GL_OES_element_index_uint ";
3174 }
3175
3176 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3177 if (end != std::string::npos)
3178 {
3179 mExtensionString.resize(end+1);
3180 }
3181}
3182
3183const char *Context::getExtensionString() const
3184{
3185 return mExtensionString.c_str();
3186}
3187
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003188void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3189 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3190 GLbitfield mask)
3191{
3192 IDirect3DDevice9 *device = getDevice();
3193
3194 Framebuffer *readFramebuffer = getReadFramebuffer();
3195 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3196
3197 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3198 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3199 {
3200 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3201 }
3202
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003203 if (drawFramebuffer->getSamples() != 0)
3204 {
3205 return error(GL_INVALID_OPERATION);
3206 }
3207
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003208 RECT sourceRect;
3209 RECT destRect;
3210
3211 if (srcX0 < srcX1)
3212 {
3213 sourceRect.left = srcX0;
3214 sourceRect.right = srcX1;
3215 destRect.left = dstX0;
3216 destRect.right = dstX1;
3217 }
3218 else
3219 {
3220 sourceRect.left = srcX1;
3221 destRect.left = dstX1;
3222 sourceRect.right = srcX0;
3223 destRect.right = dstX0;
3224 }
3225
3226 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3227 // flip our Y-values here
3228 if (srcY0 < srcY1)
3229 {
3230 sourceRect.bottom = srcY1;
3231 destRect.bottom = dstY1;
3232 sourceRect.top = srcY0;
3233 destRect.top = dstY0;
3234 }
3235 else
3236 {
3237 sourceRect.bottom = srcY0;
3238 destRect.bottom = dstY0;
3239 sourceRect.top = srcY1;
3240 destRect.top = dstY1;
3241 }
3242
3243 RECT sourceScissoredRect = sourceRect;
3244 RECT destScissoredRect = destRect;
3245
3246 if (mState.scissorTest)
3247 {
3248 // Only write to parts of the destination framebuffer which pass the scissor test
3249 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3250 // rect will be checked against scissorY, rather than the bottom.
3251 if (destRect.left < mState.scissorX)
3252 {
3253 int xDiff = mState.scissorX - destRect.left;
3254 destScissoredRect.left = mState.scissorX;
3255 sourceScissoredRect.left += xDiff;
3256 }
3257
3258 if (destRect.right > mState.scissorX + mState.scissorWidth)
3259 {
3260 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3261 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3262 sourceScissoredRect.right -= xDiff;
3263 }
3264
3265 if (destRect.top < mState.scissorY)
3266 {
3267 int yDiff = mState.scissorY - destRect.top;
3268 destScissoredRect.top = mState.scissorY;
3269 sourceScissoredRect.top += yDiff;
3270 }
3271
3272 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3273 {
3274 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3275 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3276 sourceScissoredRect.bottom -= yDiff;
3277 }
3278 }
3279
3280 bool blitRenderTarget = false;
3281 bool blitDepthStencil = false;
3282
3283 RECT sourceTrimmedRect = sourceScissoredRect;
3284 RECT destTrimmedRect = destScissoredRect;
3285
3286 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3287 // the actual draw and read surfaces.
3288 if (sourceTrimmedRect.left < 0)
3289 {
3290 int xDiff = 0 - sourceTrimmedRect.left;
3291 sourceTrimmedRect.left = 0;
3292 destTrimmedRect.left += xDiff;
3293 }
3294
3295 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3296 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3297 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3298 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3299
3300 if (sourceTrimmedRect.right > readBufferWidth)
3301 {
3302 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3303 sourceTrimmedRect.right = readBufferWidth;
3304 destTrimmedRect.right -= xDiff;
3305 }
3306
3307 if (sourceTrimmedRect.top < 0)
3308 {
3309 int yDiff = 0 - sourceTrimmedRect.top;
3310 sourceTrimmedRect.top = 0;
3311 destTrimmedRect.top += yDiff;
3312 }
3313
3314 if (sourceTrimmedRect.bottom > readBufferHeight)
3315 {
3316 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3317 sourceTrimmedRect.bottom = readBufferHeight;
3318 destTrimmedRect.bottom -= yDiff;
3319 }
3320
3321 if (destTrimmedRect.left < 0)
3322 {
3323 int xDiff = 0 - destTrimmedRect.left;
3324 destTrimmedRect.left = 0;
3325 sourceTrimmedRect.left += xDiff;
3326 }
3327
3328 if (destTrimmedRect.right > drawBufferWidth)
3329 {
3330 int xDiff = destTrimmedRect.right - drawBufferWidth;
3331 destTrimmedRect.right = drawBufferWidth;
3332 sourceTrimmedRect.right -= xDiff;
3333 }
3334
3335 if (destTrimmedRect.top < 0)
3336 {
3337 int yDiff = 0 - destTrimmedRect.top;
3338 destTrimmedRect.top = 0;
3339 sourceTrimmedRect.top += yDiff;
3340 }
3341
3342 if (destTrimmedRect.bottom > drawBufferHeight)
3343 {
3344 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3345 destTrimmedRect.bottom = drawBufferHeight;
3346 sourceTrimmedRect.bottom -= yDiff;
3347 }
3348
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003349 bool partialBufferCopy = false;
3350 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3351 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3352 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3353 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3354 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3355 {
3356 partialBufferCopy = true;
3357 }
3358
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003359 if (mask & GL_COLOR_BUFFER_BIT)
3360 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003361 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3362 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3363 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3364 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3365 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003366 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3367 {
3368 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3369 return error(GL_INVALID_OPERATION);
3370 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003371
3372 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3373 {
3374 return error(GL_INVALID_OPERATION);
3375 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003376
3377 blitRenderTarget = true;
3378
3379 }
3380
3381 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3382 {
3383 DepthStencilbuffer *readDSBuffer = NULL;
3384 DepthStencilbuffer *drawDSBuffer = NULL;
3385
3386 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3387 // both a depth and stencil buffer, it will be the same buffer.
3388
3389 if (mask & GL_DEPTH_BUFFER_BIT)
3390 {
3391 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3392 {
3393 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3394 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3395 {
3396 return error(GL_INVALID_OPERATION);
3397 }
3398
3399 blitDepthStencil = true;
3400 readDSBuffer = readFramebuffer->getDepthbuffer();
3401 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3402 }
3403 }
3404
3405 if (mask & GL_STENCIL_BUFFER_BIT)
3406 {
3407 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3408 {
3409 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3410 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3411 {
3412 return error(GL_INVALID_OPERATION);
3413 }
3414
3415 blitDepthStencil = true;
3416 readDSBuffer = readFramebuffer->getStencilbuffer();
3417 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3418 }
3419 }
3420
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003421 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003422 {
3423 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3424 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3425 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003426
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003427 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3428 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003429 {
3430 return error(GL_INVALID_OPERATION);
3431 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003432 }
3433
3434 if (blitRenderTarget || blitDepthStencil)
3435 {
3436 egl::Display *display = getDisplay();
3437 display->endScene();
3438
3439 if (blitRenderTarget)
3440 {
3441 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3442 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3443
3444 if (FAILED(result))
3445 {
3446 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3447 return;
3448 }
3449 }
3450
3451 if (blitDepthStencil)
3452 {
3453 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3454
3455 if (FAILED(result))
3456 {
3457 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3458 return;
3459 }
3460 }
3461 }
3462}
3463
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003464}
3465
3466extern "C"
3467{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003468gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003469{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003470 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003471}
3472
3473void glDestroyContext(gl::Context *context)
3474{
3475 delete context;
3476
3477 if (context == gl::getContext())
3478 {
3479 gl::makeCurrent(NULL, NULL, NULL);
3480 }
3481}
3482
3483void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3484{
3485 gl::makeCurrent(context, display, surface);
3486}
3487
3488gl::Context *glGetCurrentContext()
3489{
3490 return gl::getContext();
3491}
3492}