blob: 7b692a2fc85b803fb6abde98dad22e966d5bb381 [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
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000125 mTexture2DZero = new Texture2D(0);
126 mTextureCubeMapZero = new TextureCubeMap(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
128 mColorbufferZero = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000129 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000131 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000132 bindArrayBuffer(0);
133 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134 bindTextureCubeMap(0);
135 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000136 bindReadFramebuffer(0);
137 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 bindRenderbuffer(0);
139
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000140 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 {
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000142 mIncompleteTextures[type] = NULL;
143 }
144
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000145 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000147 mState.packAlignment = 4;
148 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000149
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150 mBufferBackEnd = NULL;
151 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000152 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000153 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155 mInvalidEnum = false;
156 mInvalidValue = false;
157 mInvalidOperation = false;
158 mOutOfMemory = false;
159 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000160
161 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000162
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000163 mSupportsCompressedTextures = false;
164 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000165 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000166 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000167 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168}
169
170Context::~Context()
171{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000172 if (mState.currentProgram != 0)
173 {
174 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
175 if (programObject)
176 {
177 programObject->release();
178 }
179 mState.currentProgram = 0;
180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000182 while (!mFramebufferMap.empty())
183 {
184 deleteFramebuffer(mFramebufferMap.begin()->first);
185 }
186
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000187 while (!mFenceMap.empty())
188 {
189 deleteFence(mFenceMap.begin()->first);
190 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000191
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000192 while (!mMultiSampleSupport.empty())
193 {
194 delete [] mMultiSampleSupport.begin()->second;
195 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
196 }
197
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
200 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
201 {
202 mState.samplerTexture[type][sampler].set(NULL);
203 }
204 }
205
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000206 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
207 {
208 delete mIncompleteTextures[type];
209 }
210
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000211 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
212 {
213 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
214 }
215
216 mState.arrayBuffer.set(NULL);
217 mState.elementArrayBuffer.set(NULL);
218 mState.texture2D.set(NULL);
219 mState.textureCubeMap.set(NULL);
220 mState.renderbuffer.set(NULL);
221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222 delete mTexture2DZero;
223 delete mTextureCubeMapZero;
224
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000225 delete mBufferBackEnd;
226 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000227 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000228 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000229
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000230 if (mMaskedClearSavedState)
231 {
232 mMaskedClearSavedState->Release();
233 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000234
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000235 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
238void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
239{
240 IDirect3DDevice9 *device = display->getDevice();
241
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000244 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000245
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000246 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000247 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000248 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000249 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000250
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000251 const D3DFORMAT renderBufferFormats[] =
252 {
253 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000254 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000255 D3DFMT_R5G6B5,
256 D3DFMT_D24S8
257 };
258
259 int max = 0;
260 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
261 {
262 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
263 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
264 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
265
266 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
267 {
268 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
269 {
270 max = j;
271 }
272 }
273 }
274
275 mMaxSupportedSamples = max;
276
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000277 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000278 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000279 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
280 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000281
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000282 initExtensionString();
283
284 mState.viewportX = 0;
285 mState.viewportY = 0;
286 mState.viewportWidth = surface->getWidth();
287 mState.viewportHeight = surface->getHeight();
288
289 mState.scissorX = 0;
290 mState.scissorY = 0;
291 mState.scissorWidth = surface->getWidth();
292 mState.scissorHeight = surface->getHeight();
293
294 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000295 }
296
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
298 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000299 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000302 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000303 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
305 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000307 if (defaultRenderTarget)
308 {
309 defaultRenderTarget->Release();
310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000312 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000314 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000316
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000317 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000318
319 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320}
321
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000322// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000323void Context::markAllStateDirty()
324{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000326 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000327 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000328
329 mClearStateDirty = true;
330 mCullStateDirty = true;
331 mDepthStateDirty = true;
332 mMaskStateDirty = true;
333 mBlendStateDirty = true;
334 mStencilStateDirty = true;
335 mPolygonOffsetStateDirty = true;
336 mScissorStateDirty = true;
337 mSampleStateDirty = true;
338 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000339 mFrontFaceDirty = true;
340
341 if (mBufferBackEnd != NULL)
342 {
343 mBufferBackEnd->invalidate();
344 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000345}
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347void Context::setClearColor(float red, float green, float blue, float alpha)
348{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000349 mState.colorClearValue.red = red;
350 mState.colorClearValue.green = green;
351 mState.colorClearValue.blue = blue;
352 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353}
354
355void Context::setClearDepth(float depth)
356{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000357 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358}
359
360void Context::setClearStencil(int stencil)
361{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000362 mState.stencilClearValue = stencil;
363}
364
365void Context::setCullFace(bool enabled)
366{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000367 if (mState.cullFace != enabled)
368 {
369 mState.cullFace = enabled;
370 mCullStateDirty = true;
371 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000372}
373
374bool Context::isCullFaceEnabled() const
375{
376 return mState.cullFace;
377}
378
379void Context::setCullMode(GLenum mode)
380{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000381 if (mState.cullMode != mode)
382 {
383 mState.cullMode = mode;
384 mCullStateDirty = true;
385 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000386}
387
388void Context::setFrontFace(GLenum front)
389{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000390 if (mState.frontFace != front)
391 {
392 mState.frontFace = front;
393 mFrontFaceDirty = true;
394 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000395}
396
397void Context::setDepthTest(bool enabled)
398{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000399 if (mState.depthTest != enabled)
400 {
401 mState.depthTest = enabled;
402 mDepthStateDirty = true;
403 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000404}
405
406bool Context::isDepthTestEnabled() const
407{
408 return mState.depthTest;
409}
410
411void Context::setDepthFunc(GLenum depthFunc)
412{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000413 if (mState.depthFunc != depthFunc)
414 {
415 mState.depthFunc = depthFunc;
416 mDepthStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420void Context::setDepthRange(float zNear, float zFar)
421{
422 mState.zNear = zNear;
423 mState.zFar = zFar;
424}
425
426void Context::setBlend(bool enabled)
427{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000428 if (mState.blend != enabled)
429 {
430 mState.blend = enabled;
431 mBlendStateDirty = true;
432 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000433}
434
435bool Context::isBlendEnabled() const
436{
437 return mState.blend;
438}
439
440void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
441{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000442 if (mState.sourceBlendRGB != sourceRGB ||
443 mState.sourceBlendAlpha != sourceAlpha ||
444 mState.destBlendRGB != destRGB ||
445 mState.destBlendAlpha != destAlpha)
446 {
447 mState.sourceBlendRGB = sourceRGB;
448 mState.destBlendRGB = destRGB;
449 mState.sourceBlendAlpha = sourceAlpha;
450 mState.destBlendAlpha = destAlpha;
451 mBlendStateDirty = true;
452 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000453}
454
455void Context::setBlendColor(float red, float green, float blue, float alpha)
456{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000457 if (mState.blendColor.red != red ||
458 mState.blendColor.green != green ||
459 mState.blendColor.blue != blue ||
460 mState.blendColor.alpha != alpha)
461 {
462 mState.blendColor.red = red;
463 mState.blendColor.green = green;
464 mState.blendColor.blue = blue;
465 mState.blendColor.alpha = alpha;
466 mBlendStateDirty = true;
467 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000468}
469
470void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
471{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000472 if (mState.blendEquationRGB != rgbEquation ||
473 mState.blendEquationAlpha != alphaEquation)
474 {
475 mState.blendEquationRGB = rgbEquation;
476 mState.blendEquationAlpha = alphaEquation;
477 mBlendStateDirty = true;
478 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000479}
480
481void Context::setStencilTest(bool enabled)
482{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000483 if (mState.stencilTest != enabled)
484 {
485 mState.stencilTest = enabled;
486 mStencilStateDirty = true;
487 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000488}
489
490bool Context::isStencilTestEnabled() const
491{
492 return mState.stencilTest;
493}
494
495void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.stencilFunc != stencilFunc ||
498 mState.stencilRef != stencilRef ||
499 mState.stencilMask != stencilMask)
500 {
501 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000502 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000503 mState.stencilMask = stencilMask;
504 mStencilStateDirty = true;
505 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000506}
507
508void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
509{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 if (mState.stencilBackFunc != stencilBackFunc ||
511 mState.stencilBackRef != stencilBackRef ||
512 mState.stencilBackMask != stencilBackMask)
513 {
514 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000515 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000516 mState.stencilBackMask = stencilBackMask;
517 mStencilStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilWritemask(GLuint stencilWritemask)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilWritemask != stencilWritemask)
524 {
525 mState.stencilWritemask = stencilWritemask;
526 mStencilStateDirty = true;
527 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000528}
529
530void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
531{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000532 if (mState.stencilBackWritemask != stencilBackWritemask)
533 {
534 mState.stencilBackWritemask = stencilBackWritemask;
535 mStencilStateDirty = true;
536 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000537}
538
539void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
540{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000541 if (mState.stencilFail != stencilFail ||
542 mState.stencilPassDepthFail != stencilPassDepthFail ||
543 mState.stencilPassDepthPass != stencilPassDepthPass)
544 {
545 mState.stencilFail = stencilFail;
546 mState.stencilPassDepthFail = stencilPassDepthFail;
547 mState.stencilPassDepthPass = stencilPassDepthPass;
548 mStencilStateDirty = true;
549 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000550}
551
552void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.stencilBackFail != stencilBackFail ||
555 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
556 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
557 {
558 mState.stencilBackFail = stencilBackFail;
559 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
560 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
561 mStencilStateDirty = true;
562 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000563}
564
565void Context::setPolygonOffsetFill(bool enabled)
566{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567 if (mState.polygonOffsetFill != enabled)
568 {
569 mState.polygonOffsetFill = enabled;
570 mPolygonOffsetStateDirty = true;
571 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574bool Context::isPolygonOffsetFillEnabled() const
575{
576 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000577
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578}
579
580void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.polygonOffsetFactor != factor ||
583 mState.polygonOffsetUnits != units)
584 {
585 mState.polygonOffsetFactor = factor;
586 mState.polygonOffsetUnits = units;
587 mPolygonOffsetStateDirty = true;
588 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000589}
590
591void Context::setSampleAlphaToCoverage(bool enabled)
592{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000593 if (mState.sampleAlphaToCoverage != enabled)
594 {
595 mState.sampleAlphaToCoverage = enabled;
596 mSampleStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600bool Context::isSampleAlphaToCoverageEnabled() const
601{
602 return mState.sampleAlphaToCoverage;
603}
604
605void Context::setSampleCoverage(bool enabled)
606{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000607 if (mState.sampleCoverage != enabled)
608 {
609 mState.sampleCoverage = enabled;
610 mSampleStateDirty = true;
611 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000612}
613
614bool Context::isSampleCoverageEnabled() const
615{
616 return mState.sampleCoverage;
617}
618
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000619void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.sampleCoverageValue != value ||
622 mState.sampleCoverageInvert != invert)
623 {
624 mState.sampleCoverageValue = value;
625 mState.sampleCoverageInvert = invert;
626 mSampleStateDirty = true;
627 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000628}
629
630void Context::setScissorTest(bool enabled)
631{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000632 if (mState.scissorTest != enabled)
633 {
634 mState.scissorTest = enabled;
635 mScissorStateDirty = true;
636 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000637}
638
639bool Context::isScissorTestEnabled() const
640{
641 return mState.scissorTest;
642}
643
644void Context::setDither(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.dither != enabled)
647 {
648 mState.dither = enabled;
649 mDitherStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isDitherEnabled() const
654{
655 return mState.dither;
656}
657
658void Context::setLineWidth(GLfloat width)
659{
660 mState.lineWidth = width;
661}
662
663void Context::setGenerateMipmapHint(GLenum hint)
664{
665 mState.generateMipmapHint = hint;
666}
667
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000668void Context::setFragmentShaderDerivativeHint(GLenum hint)
669{
670 mState.fragmentShaderDerivativeHint = hint;
671 // TODO: Propagate the hint to shader translator so we can write
672 // ddx, ddx_coarse, or ddx_fine depending on the hint.
673 // Ignore for now. It is valid for implementations to ignore hint.
674}
675
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000676void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
677{
678 mState.viewportX = x;
679 mState.viewportY = y;
680 mState.viewportWidth = width;
681 mState.viewportHeight = height;
682}
683
684void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.scissorX != x || mState.scissorY != y ||
687 mState.scissorWidth != width || mState.scissorHeight != height)
688 {
689 mState.scissorX = x;
690 mState.scissorY = y;
691 mState.scissorWidth = width;
692 mState.scissorHeight = height;
693 mScissorStateDirty = true;
694 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000695}
696
697void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
698{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000699 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
700 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
701 {
702 mState.colorMaskRed = red;
703 mState.colorMaskGreen = green;
704 mState.colorMaskBlue = blue;
705 mState.colorMaskAlpha = alpha;
706 mMaskStateDirty = true;
707 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000708}
709
710void Context::setDepthMask(bool mask)
711{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000712 if (mState.depthMask != mask)
713 {
714 mState.depthMask = mask;
715 mMaskStateDirty = true;
716 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717}
718
719void Context::setActiveSampler(int active)
720{
721 mState.activeSampler = active;
722}
723
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000724GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000725{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000726 return mState.readFramebuffer;
727}
728
729GLuint Context::getDrawFramebufferHandle() const
730{
731 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732}
733
734GLuint Context::getRenderbufferHandle() const
735{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000736 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000737}
738
739GLuint Context::getArrayBufferHandle() const
740{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000741 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000742}
743
744void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
745{
746 mState.vertexAttribute[attribNum].mEnabled = enabled;
747}
748
749const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
750{
751 return mState.vertexAttribute[attribNum];
752}
753
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000754void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755 GLsizei stride, const void *pointer)
756{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000757 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758 mState.vertexAttribute[attribNum].mSize = size;
759 mState.vertexAttribute[attribNum].mType = type;
760 mState.vertexAttribute[attribNum].mNormalized = normalized;
761 mState.vertexAttribute[attribNum].mStride = stride;
762 mState.vertexAttribute[attribNum].mPointer = pointer;
763}
764
765const void *Context::getVertexAttribPointer(unsigned int attribNum) const
766{
767 return mState.vertexAttribute[attribNum].mPointer;
768}
769
770// returns entire set of attributes as a block
771const AttributeState *Context::getVertexAttribBlock()
772{
773 return mState.vertexAttribute;
774}
775
776void Context::setPackAlignment(GLint alignment)
777{
778 mState.packAlignment = alignment;
779}
780
781GLint Context::getPackAlignment() const
782{
783 return mState.packAlignment;
784}
785
786void Context::setUnpackAlignment(GLint alignment)
787{
788 mState.unpackAlignment = alignment;
789}
790
791GLint Context::getUnpackAlignment() const
792{
793 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794}
795
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796GLuint Context::createBuffer()
797{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000798 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799}
800
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801GLuint Context::createProgram()
802{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000803 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804}
805
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000806GLuint Context::createShader(GLenum type)
807{
808 return mResourceManager->createShader(type);
809}
810
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811GLuint Context::createTexture()
812{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000813 return mResourceManager->createTexture();
814}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000816GLuint Context::createRenderbuffer()
817{
818 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819}
820
821// Returns an unused framebuffer name
822GLuint Context::createFramebuffer()
823{
824 unsigned int handle = 1;
825
826 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
827 {
828 handle++;
829 }
830
831 mFramebufferMap[handle] = NULL;
832
833 return handle;
834}
835
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000836GLuint Context::createFence()
837{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000838 unsigned int handle = 0;
839
840 while (mFenceMap.find(handle) != mFenceMap.end())
841 {
842 handle++;
843 }
844
845 mFenceMap[handle] = new Fence;
846
847 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000848}
849
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850void Context::deleteBuffer(GLuint buffer)
851{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000852 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853 {
854 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000856
857 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860void Context::deleteShader(GLuint shader)
861{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000862 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863}
864
865void Context::deleteProgram(GLuint program)
866{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000867 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
870void Context::deleteTexture(GLuint texture)
871{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000872 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 {
874 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000876
877 mResourceManager->deleteTexture(texture);
878}
879
880void Context::deleteRenderbuffer(GLuint renderbuffer)
881{
882 if (mResourceManager->getRenderbuffer(renderbuffer))
883 {
884 detachRenderbuffer(renderbuffer);
885 }
886
887 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
890void Context::deleteFramebuffer(GLuint framebuffer)
891{
892 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
893
894 if (framebufferObject != mFramebufferMap.end())
895 {
896 detachFramebuffer(framebuffer);
897
898 delete framebufferObject->second;
899 mFramebufferMap.erase(framebufferObject);
900 }
901}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000902
903void Context::deleteFence(GLuint fence)
904{
905 FenceMap::iterator fenceObject = mFenceMap.find(fence);
906
907 if (fenceObject != mFenceMap.end())
908 {
909 delete fenceObject->second;
910 mFenceMap.erase(fenceObject);
911 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000912}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916 return mResourceManager->getBuffer(handle);
917}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919Shader *Context::getShader(GLuint handle)
920{
921 return mResourceManager->getShader(handle);
922}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924Program *Context::getProgram(GLuint handle)
925{
926 return mResourceManager->getProgram(handle);
927}
928
929Texture *Context::getTexture(GLuint handle)
930{
931 return mResourceManager->getTexture(handle);
932}
933
934Renderbuffer *Context::getRenderbuffer(GLuint handle)
935{
936 return mResourceManager->getRenderbuffer(handle);
937}
938
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000939Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000940{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000941 return getFramebuffer(mState.readFramebuffer);
942}
943
944Framebuffer *Context::getDrawFramebuffer()
945{
946 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947}
948
949void Context::bindArrayBuffer(unsigned int buffer)
950{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000951 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000953 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
956void Context::bindElementArrayBuffer(unsigned int buffer)
957{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000958 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000960 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
963void Context::bindTexture2D(GLuint texture)
964{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000967 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000969 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
972void Context::bindTextureCubeMap(GLuint texture)
973{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000976 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000978 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979}
980
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000981void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982{
983 if (!getFramebuffer(framebuffer))
984 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000985 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986 }
987
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000988 mState.readFramebuffer = framebuffer;
989}
990
991void Context::bindDrawFramebuffer(GLuint framebuffer)
992{
993 if (!getFramebuffer(framebuffer))
994 {
995 mFramebufferMap[framebuffer] = new Framebuffer();
996 }
997
998 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999}
1000
1001void Context::bindRenderbuffer(GLuint renderbuffer)
1002{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001003 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1004
1005 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
1008void Context::useProgram(GLuint program)
1009{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001010 GLuint priorProgram = mState.currentProgram;
1011 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001012
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001015 Program *newProgram = mResourceManager->getProgram(program);
1016 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1017
1018 if (newProgram)
1019 {
1020 newProgram->addRef();
1021 }
1022
1023 if (oldProgram)
1024 {
1025 oldProgram->release();
1026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030void Context::setFramebufferZero(Framebuffer *buffer)
1031{
1032 delete mFramebufferMap[0];
1033 mFramebufferMap[0] = buffer;
1034}
1035
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001036void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1039 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040}
1041
1042Framebuffer *Context::getFramebuffer(unsigned int handle)
1043{
1044 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001045
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046 if (framebuffer == mFramebufferMap.end())
1047 {
1048 return NULL;
1049 }
1050 else
1051 {
1052 return framebuffer->second;
1053 }
1054}
1055
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001056Fence *Context::getFence(unsigned int handle)
1057{
1058 FenceMap::iterator fence = mFenceMap.find(handle);
1059
1060 if (fence == mFenceMap.end())
1061 {
1062 return NULL;
1063 }
1064 else
1065 {
1066 return fence->second;
1067 }
1068}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001069
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070Buffer *Context::getArrayBuffer()
1071{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001072 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
1075Buffer *Context::getElementArrayBuffer()
1076{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001077 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080Program *Context::getCurrentProgram()
1081{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001082 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083}
1084
1085Texture2D *Context::getTexture2D()
1086{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001087 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 +00001088 {
1089 return mTexture2DZero;
1090 }
1091
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001092 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
1095TextureCubeMap *Context::getTextureCubeMap()
1096{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001097 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 +00001098 {
1099 return mTextureCubeMapZero;
1100 }
1101
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001102 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103}
1104
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001105Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001107 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001108
1109 if (texid == 0)
1110 {
1111 switch (type)
1112 {
1113 default: UNREACHABLE();
1114 case SAMPLER_2D: return mTexture2DZero;
1115 case SAMPLER_CUBE: return mTextureCubeMapZero;
1116 }
1117 }
1118
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001119 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120}
1121
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001122bool Context::getBooleanv(GLenum pname, GLboolean *params)
1123{
1124 switch (pname)
1125 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001126 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1127 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1128 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001129 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001130 params[0] = mState.colorMaskRed;
1131 params[1] = mState.colorMaskGreen;
1132 params[2] = mState.colorMaskBlue;
1133 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001134 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001135 case GL_CULL_FACE: *params = mState.cullFace;
1136 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1137 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1138 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1139 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1140 case GL_STENCIL_TEST: *params = mState.stencilTest;
1141 case GL_DEPTH_TEST: *params = mState.depthTest;
1142 case GL_BLEND: *params = mState.blend;
1143 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001144 default:
1145 return false;
1146 }
1147
1148 return true;
1149}
1150
1151bool Context::getFloatv(GLenum pname, GLfloat *params)
1152{
1153 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1154 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1155 // GetIntegerv as its native query function. As it would require conversion in any
1156 // case, this should make no difference to the calling application.
1157 switch (pname)
1158 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001159 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1160 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1161 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1162 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1163 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001164 case GL_ALIASED_LINE_WIDTH_RANGE:
1165 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1166 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1167 break;
1168 case GL_ALIASED_POINT_SIZE_RANGE:
1169 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001170 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 +00001171 break;
1172 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001173 params[0] = mState.zNear;
1174 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 break;
1176 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001177 params[0] = mState.colorClearValue.red;
1178 params[1] = mState.colorClearValue.green;
1179 params[2] = mState.colorClearValue.blue;
1180 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001181 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001182 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001183 params[0] = mState.blendColor.red;
1184 params[1] = mState.blendColor.green;
1185 params[2] = mState.blendColor.blue;
1186 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001187 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001188 default:
1189 return false;
1190 }
1191
1192 return true;
1193}
1194
1195bool Context::getIntegerv(GLenum pname, GLint *params)
1196{
1197 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1198 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1199 // GetIntegerv as its native query function. As it would require conversion in any
1200 // case, this should make no difference to the calling application. You may find it in
1201 // Context::getFloatv.
1202 switch (pname)
1203 {
1204 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1205 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1206 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1207 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1208 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1209 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1210 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1211 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001212 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001213 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001214 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1215 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001216 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1217 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1218 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001219 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001220 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1221 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1222 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1223 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001224 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001225 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1226 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1227 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1228 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1229 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1230 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1231 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1232 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1233 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1234 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1235 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1236 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1237 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1238 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1239 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1240 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1241 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1242 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1243 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1244 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1245 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1246 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1247 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1248 case GL_SUBPIXEL_BITS: *params = 4; break;
1249 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1250 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001251 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1252 {
1253 if (supportsCompressedTextures())
1254 {
1255 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1256 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1257 *params = 2;
1258 }
1259 else
1260 {
1261 *params = 0;
1262 }
1263 }
1264 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001265 case GL_MAX_SAMPLES_ANGLE:
1266 {
1267 GLsizei maxSamples = getMaxSupportedSamples();
1268 if (maxSamples != 0)
1269 {
1270 *params = maxSamples;
1271 }
1272 else
1273 {
1274 return false;
1275 }
1276
1277 break;
1278 }
1279 case GL_SAMPLE_BUFFERS:
1280 case GL_SAMPLES:
1281 {
1282 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1283 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1284 {
1285 switch (pname)
1286 {
1287 case GL_SAMPLE_BUFFERS:
1288 if (framebuffer->getSamples() != 0)
1289 {
1290 *params = 1;
1291 }
1292 else
1293 {
1294 *params = 0;
1295 }
1296 break;
1297 case GL_SAMPLES:
1298 *params = framebuffer->getSamples();
1299 break;
1300 }
1301 }
1302 else
1303 {
1304 *params = 0;
1305 }
1306 }
1307 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001308 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1309 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1310 case GL_MAX_VIEWPORT_DIMS:
1311 {
1312 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1313 params[0] = maxDimension;
1314 params[1] = maxDimension;
1315 }
1316 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001317 case GL_COMPRESSED_TEXTURE_FORMATS:
1318 {
1319 if (supportsCompressedTextures())
1320 {
1321 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1322 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1323 }
1324 }
1325 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001326 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001327 params[0] = mState.viewportX;
1328 params[1] = mState.viewportY;
1329 params[2] = mState.viewportWidth;
1330 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001331 break;
1332 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001333 params[0] = mState.scissorX;
1334 params[1] = mState.scissorY;
1335 params[2] = mState.scissorWidth;
1336 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001337 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001338 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1339 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001340 case GL_RED_BITS:
1341 case GL_GREEN_BITS:
1342 case GL_BLUE_BITS:
1343 case GL_ALPHA_BITS:
1344 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001345 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001346 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1347
1348 if (colorbuffer)
1349 {
1350 switch (pname)
1351 {
1352 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1353 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1354 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1355 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1356 }
1357 }
1358 else
1359 {
1360 *params = 0;
1361 }
1362 }
1363 break;
1364 case GL_DEPTH_BITS:
1365 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001366 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001367 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368
1369 if (depthbuffer)
1370 {
1371 *params = depthbuffer->getDepthSize();
1372 }
1373 else
1374 {
1375 *params = 0;
1376 }
1377 }
1378 break;
1379 case GL_STENCIL_BITS:
1380 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001381 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001382 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001383
1384 if (stencilbuffer)
1385 {
1386 *params = stencilbuffer->getStencilSize();
1387 }
1388 else
1389 {
1390 *params = 0;
1391 }
1392 }
1393 break;
1394 case GL_TEXTURE_BINDING_2D:
1395 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001396 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001397 {
1398 error(GL_INVALID_OPERATION);
1399 return false;
1400 }
1401
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001402 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001403 }
1404 break;
1405 case GL_TEXTURE_BINDING_CUBE_MAP:
1406 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001407 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001408 {
1409 error(GL_INVALID_OPERATION);
1410 return false;
1411 }
1412
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001413 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001414 }
1415 break;
1416 default:
1417 return false;
1418 }
1419
1420 return true;
1421}
1422
1423bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1424{
1425 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1426 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1427 // to the fact that it is stored internally as a float, and so would require conversion
1428 // if returned from Context::getIntegerv. Since this conversion is already implemented
1429 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1430 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1431 // application.
1432 switch (pname)
1433 {
1434 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1435 case GL_SHADER_BINARY_FORMATS:
1436 {
1437 *type = GL_INT;
1438 *numParams = 0;
1439 }
1440 break;
1441 case GL_MAX_VERTEX_ATTRIBS:
1442 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1443 case GL_MAX_VARYING_VECTORS:
1444 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1445 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1446 case GL_MAX_TEXTURE_IMAGE_UNITS:
1447 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1448 case GL_MAX_RENDERBUFFER_SIZE:
1449 case GL_NUM_SHADER_BINARY_FORMATS:
1450 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1451 case GL_ARRAY_BUFFER_BINDING:
1452 case GL_FRAMEBUFFER_BINDING:
1453 case GL_RENDERBUFFER_BINDING:
1454 case GL_CURRENT_PROGRAM:
1455 case GL_PACK_ALIGNMENT:
1456 case GL_UNPACK_ALIGNMENT:
1457 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001458 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001459 case GL_RED_BITS:
1460 case GL_GREEN_BITS:
1461 case GL_BLUE_BITS:
1462 case GL_ALPHA_BITS:
1463 case GL_DEPTH_BITS:
1464 case GL_STENCIL_BITS:
1465 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1466 case GL_CULL_FACE_MODE:
1467 case GL_FRONT_FACE:
1468 case GL_ACTIVE_TEXTURE:
1469 case GL_STENCIL_FUNC:
1470 case GL_STENCIL_VALUE_MASK:
1471 case GL_STENCIL_REF:
1472 case GL_STENCIL_FAIL:
1473 case GL_STENCIL_PASS_DEPTH_FAIL:
1474 case GL_STENCIL_PASS_DEPTH_PASS:
1475 case GL_STENCIL_BACK_FUNC:
1476 case GL_STENCIL_BACK_VALUE_MASK:
1477 case GL_STENCIL_BACK_REF:
1478 case GL_STENCIL_BACK_FAIL:
1479 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1480 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1481 case GL_DEPTH_FUNC:
1482 case GL_BLEND_SRC_RGB:
1483 case GL_BLEND_SRC_ALPHA:
1484 case GL_BLEND_DST_RGB:
1485 case GL_BLEND_DST_ALPHA:
1486 case GL_BLEND_EQUATION_RGB:
1487 case GL_BLEND_EQUATION_ALPHA:
1488 case GL_STENCIL_WRITEMASK:
1489 case GL_STENCIL_BACK_WRITEMASK:
1490 case GL_STENCIL_CLEAR_VALUE:
1491 case GL_SUBPIXEL_BITS:
1492 case GL_MAX_TEXTURE_SIZE:
1493 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1494 case GL_SAMPLE_BUFFERS:
1495 case GL_SAMPLES:
1496 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1497 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1498 case GL_TEXTURE_BINDING_2D:
1499 case GL_TEXTURE_BINDING_CUBE_MAP:
1500 {
1501 *type = GL_INT;
1502 *numParams = 1;
1503 }
1504 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001505 case GL_MAX_SAMPLES_ANGLE:
1506 {
1507 if (getMaxSupportedSamples() != 0)
1508 {
1509 *type = GL_INT;
1510 *numParams = 1;
1511 }
1512 else
1513 {
1514 return false;
1515 }
1516 }
1517 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001518 case GL_MAX_VIEWPORT_DIMS:
1519 {
1520 *type = GL_INT;
1521 *numParams = 2;
1522 }
1523 break;
1524 case GL_VIEWPORT:
1525 case GL_SCISSOR_BOX:
1526 {
1527 *type = GL_INT;
1528 *numParams = 4;
1529 }
1530 break;
1531 case GL_SHADER_COMPILER:
1532 case GL_SAMPLE_COVERAGE_INVERT:
1533 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001534 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1535 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1536 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1537 case GL_SAMPLE_COVERAGE:
1538 case GL_SCISSOR_TEST:
1539 case GL_STENCIL_TEST:
1540 case GL_DEPTH_TEST:
1541 case GL_BLEND:
1542 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001543 {
1544 *type = GL_BOOL;
1545 *numParams = 1;
1546 }
1547 break;
1548 case GL_COLOR_WRITEMASK:
1549 {
1550 *type = GL_BOOL;
1551 *numParams = 4;
1552 }
1553 break;
1554 case GL_POLYGON_OFFSET_FACTOR:
1555 case GL_POLYGON_OFFSET_UNITS:
1556 case GL_SAMPLE_COVERAGE_VALUE:
1557 case GL_DEPTH_CLEAR_VALUE:
1558 case GL_LINE_WIDTH:
1559 {
1560 *type = GL_FLOAT;
1561 *numParams = 1;
1562 }
1563 break;
1564 case GL_ALIASED_LINE_WIDTH_RANGE:
1565 case GL_ALIASED_POINT_SIZE_RANGE:
1566 case GL_DEPTH_RANGE:
1567 {
1568 *type = GL_FLOAT;
1569 *numParams = 2;
1570 }
1571 break;
1572 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001573 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001574 {
1575 *type = GL_FLOAT;
1576 *numParams = 4;
1577 }
1578 break;
1579 default:
1580 return false;
1581 }
1582
1583 return true;
1584}
1585
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586// Applies the render target surface, depth stencil surface, viewport rectangle and
1587// scissor rectangle to the Direct3D 9 device
1588bool Context::applyRenderTarget(bool ignoreViewport)
1589{
1590 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001591
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001592 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
1594 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1595 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001596 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1597
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598 return false;
1599 }
1600
1601 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001602
1603 if (!renderTarget)
1604 {
1605 return false; // Context must be lost
1606 }
1607
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001608 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001610 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1611 if (renderTargetSerial != mAppliedRenderTargetSerial)
1612 {
1613 device->SetRenderTarget(0, renderTarget);
1614 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001615 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 +00001616 }
1617
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001618 unsigned int depthbufferSerial = 0;
1619 unsigned int stencilbufferSerial = 0;
1620 if (framebufferObject->getDepthbufferType() != GL_NONE)
1621 {
1622 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1623 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1624 }
1625 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1626 {
1627 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1628 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1629 }
1630
1631 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1632 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001633 {
1634 device->SetDepthStencilSurface(depthStencil);
1635 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001636 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001637 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638
1639 D3DVIEWPORT9 viewport;
1640 D3DSURFACE_DESC desc;
1641 renderTarget->GetDesc(&desc);
1642
1643 if (ignoreViewport)
1644 {
1645 viewport.X = 0;
1646 viewport.Y = 0;
1647 viewport.Width = desc.Width;
1648 viewport.Height = desc.Height;
1649 viewport.MinZ = 0.0f;
1650 viewport.MaxZ = 1.0f;
1651 }
1652 else
1653 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001654 viewport.X = std::max(mState.viewportX, 0);
1655 viewport.Y = std::max(mState.viewportY, 0);
1656 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1657 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1658 viewport.MinZ = clamp01(mState.zNear);
1659 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660 }
1661
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001662 if (viewport.Width <= 0 || viewport.Height <= 0)
1663 {
1664 return false; // Nothing to render
1665 }
1666
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 device->SetViewport(&viewport);
1668
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001669 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001671 if (mState.scissorTest)
1672 {
1673 RECT rect = {mState.scissorX,
1674 mState.scissorY,
1675 mState.scissorX + mState.scissorWidth,
1676 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001677 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1678 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001679 device->SetScissorRect(&rect);
1680 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1681 }
1682 else
1683 {
1684 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1685 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001686
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001687 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 }
1689
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001690 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 Program *programObject = getCurrentProgram();
1693
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001694 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001695 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001697
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001698 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001699 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1700 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1701 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001702 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1703
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001704 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001705 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001706 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1707
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001708 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001709 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001710
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001711 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001712 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001713
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001714 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001715 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001716 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 }
1718
1719 return true;
1720}
1721
1722// 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 +00001723void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724{
1725 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001726 Program *programObject = getCurrentProgram();
1727
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001728 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001729 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001730 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001733 GLint alwaysFront = !isTriangleMode(drawMode);
1734 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1735
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001736 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001737
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 }
1744 else
1745 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001746 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 }
1748
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 mCullStateDirty = false;
1750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752 if (mDepthStateDirty)
1753 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001754 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1757 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
1759 else
1760 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001761 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763
1764 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 }
1766
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001770 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1772
1773 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1774 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1775 {
1776 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1777 }
1778 else
1779 {
1780 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1781 unorm<8>(mState.blendColor.alpha),
1782 unorm<8>(mState.blendColor.alpha),
1783 unorm<8>(mState.blendColor.alpha)));
1784 }
1785
1786 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1787 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1788 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1789
1790 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1791 mState.destBlendRGB != mState.destBlendAlpha ||
1792 mState.blendEquationRGB != mState.blendEquationAlpha)
1793 {
1794 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1795
1796 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1797 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1798 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1799
1800 }
1801 else
1802 {
1803 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1804 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001805 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001807 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001808 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001809 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810
1811 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 }
1813
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814 if (mStencilStateDirty || mFrontFaceDirty)
1815 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001816 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 {
1818 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1819 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1820
1821 // FIXME: Unsupported by D3D9
1822 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1823 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1824 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1825 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1826 mState.stencilRef != mState.stencilBackRef ||
1827 mState.stencilMask != mState.stencilBackMask)
1828 {
1829 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1830 return error(GL_INVALID_OPERATION);
1831 }
1832
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001833 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001834 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001835 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1836
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1838 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1839 es2dx::ConvertComparison(mState.stencilFunc));
1840
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001841 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 +00001842 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1843
1844 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1845 es2dx::ConvertStencilOp(mState.stencilFail));
1846 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1847 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1848 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1849 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1850
1851 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1852 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1853 es2dx::ConvertComparison(mState.stencilBackFunc));
1854
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001855 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 +00001856 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1857
1858 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1859 es2dx::ConvertStencilOp(mState.stencilBackFail));
1860 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1861 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1862 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1863 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1864 }
1865 else
1866 {
1867 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1868 }
1869
1870 mStencilStateDirty = false;
1871 }
1872
1873 if (mMaskStateDirty)
1874 {
1875 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1876 mState.colorMaskBlue, mState.colorMaskAlpha));
1877 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1878
1879 mMaskStateDirty = false;
1880 }
1881
1882 if (mPolygonOffsetStateDirty)
1883 {
1884 if (mState.polygonOffsetFill)
1885 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001886 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887 if (depthbuffer)
1888 {
1889 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1890 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1891 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1892 }
1893 }
1894 else
1895 {
1896 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1897 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1898 }
1899
1900 mPolygonOffsetStateDirty = false;
1901 }
1902
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001903 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001905 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001906 {
1907 FIXME("Sample alpha to coverage is unimplemented.");
1908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001910 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001911 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001912 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1913 unsigned int mask = 0;
1914 if (mState.sampleCoverageValue != 0)
1915 {
1916 float threshold = 0.5f;
1917
1918 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1919 {
1920 mask <<= 1;
1921
1922 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1923 {
1924 threshold += 1.0f;
1925 mask |= 1;
1926 }
1927 }
1928 }
1929
1930 if (mState.sampleCoverageInvert)
1931 {
1932 mask = ~mask;
1933 }
1934
1935 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1936 }
1937 else
1938 {
1939 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001940 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941
1942 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943 }
1944
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001945 if (mDitherStateDirty)
1946 {
1947 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1948
1949 mDitherStateDirty = false;
1950 }
1951
1952 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953}
1954
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001955// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001956void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001958 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001960 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001962 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963 }
1964 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001965}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001966
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001967GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001968{
1969 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1970
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001971 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001972 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001973 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001974 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001975 }
1976
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001977 lookupAttributeMapping(translated);
1978
1979 mBufferBackEnd->setupAttributesPreDraw(translated);
1980
1981 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1982 {
1983 if (translated[i].enabled && translated[i].nonArray)
1984 {
1985 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1986 if (err != GL_NO_ERROR)
1987 {
1988 return err;
1989 }
1990
1991 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1992
1993 *useIndexing = true;
1994 return GL_NO_ERROR;
1995 }
1996 }
1997
1998 *useIndexing = false;
1999 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002000}
2001
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002002GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002003{
2004 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2005
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002006 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002007
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002008 if (err == GL_NO_ERROR)
2009 {
2010 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002011
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002012 mBufferBackEnd->setupAttributesPreDraw(translated);
2013 }
2014
2015 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002016}
2017
2018// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002019GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002020{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002021 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002022
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002023 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002024 {
2025 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2026 }
2027
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002028 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029}
2030
2031// Applies the shaders and shader constants to the Direct3D 9 device
2032void Context::applyShaders()
2033{
2034 IDirect3DDevice9 *device = getDevice();
2035 Program *programObject = getCurrentProgram();
2036 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2037 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2038
2039 device->SetVertexShader(vertexShader);
2040 device->SetPixelShader(pixelShader);
2041
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002042 if (programObject->getSerial() != mAppliedProgram)
2043 {
2044 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002045 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002046 mAppliedProgram = programObject->getSerial();
2047 }
2048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 programObject->applyUniforms();
2050}
2051
2052// Applies the textures and sampler states to the Direct3D 9 device
2053void Context::applyTextures()
2054{
2055 IDirect3DDevice9 *device = getDevice();
2056 Program *programObject = getCurrentProgram();
2057
2058 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2059 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002060 int textureUnit = programObject->getSamplerMapping(sampler);
2061 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002063 SamplerType textureType = programObject->getSamplerType(sampler);
2064
2065 Texture *texture = getSamplerTexture(textureUnit, textureType);
2066
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002067 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002068 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002069 if (texture->isComplete())
2070 {
2071 GLenum wrapS = texture->getWrapS();
2072 GLenum wrapT = texture->getWrapT();
2073 GLenum minFilter = texture->getMinFilter();
2074 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002076 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2077 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002079 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2080 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2081 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2082 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2083 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002085 device->SetTexture(sampler, texture->getTexture());
2086 }
2087 else
2088 {
2089 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2090 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002091 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002092
2093 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002095 else
2096 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002097 if (programObject->isSamplerDirty(sampler))
2098 {
2099 device->SetTexture(sampler, NULL);
2100 programObject->setSamplerDirty(sampler, false);
2101 }
2102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
2104}
2105
2106void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2107{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002108 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002109
2110 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2111 {
2112 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2113 }
2114
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002115 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2116 {
2117 return error(GL_INVALID_OPERATION);
2118 }
2119
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002121
2122 if (!renderTarget)
2123 {
2124 return; // Context must be lost, return silently
2125 }
2126
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 IDirect3DDevice9 *device = getDevice();
2128
2129 D3DSURFACE_DESC desc;
2130 renderTarget->GetDesc(&desc);
2131
2132 IDirect3DSurface9 *systemSurface;
2133 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2134
2135 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2136 {
2137 return error(GL_OUT_OF_MEMORY);
2138 }
2139
2140 ASSERT(SUCCEEDED(result));
2141
2142 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2143 {
2144 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2145 }
2146
2147 result = device->GetRenderTargetData(renderTarget, systemSurface);
2148
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 if (FAILED(result))
2150 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 systemSurface->Release();
2152
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002153 switch (result)
2154 {
2155 case D3DERR_DRIVERINTERNALERROR:
2156 case D3DERR_DEVICELOST:
2157 return error(GL_OUT_OF_MEMORY);
2158 default:
2159 UNREACHABLE();
2160 return; // No sensible error to generate
2161 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 }
2163
2164 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002165 RECT rect = {std::max(x, 0),
2166 std::max(y, 0),
2167 std::min(x + width, (int)desc.Width),
2168 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169
2170 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2171
2172 if (FAILED(result))
2173 {
2174 UNREACHABLE();
2175 systemSurface->Release();
2176
2177 return; // No sensible error to generate
2178 }
2179
2180 unsigned char *source = (unsigned char*)lock.pBits;
2181 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002182 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002184 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002185
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 for (int j = 0; j < rect.bottom - rect.top; j++)
2187 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002188 if (desc.Format == D3DFMT_A8R8G8B8 &&
2189 format == GL_BGRA_EXT &&
2190 type == GL_UNSIGNED_BYTE)
2191 {
2192 // Fast path for EXT_read_format_bgra, given
2193 // an RGBA source buffer. Note that buffers with no
2194 // alpha go through the slow path below.
2195 memcpy(dest + j * outputPitch,
2196 source + j * lock.Pitch,
2197 (rect.right - rect.left) * 4);
2198 continue;
2199 }
2200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 for (int i = 0; i < rect.right - rect.left; i++)
2202 {
2203 float r;
2204 float g;
2205 float b;
2206 float a;
2207
2208 switch (desc.Format)
2209 {
2210 case D3DFMT_R5G6B5:
2211 {
2212 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2213
2214 a = 1.0f;
2215 b = (rgb & 0x001F) * (1.0f / 0x001F);
2216 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2217 r = (rgb & 0xF800) * (1.0f / 0xF800);
2218 }
2219 break;
2220 case D3DFMT_X1R5G5B5:
2221 {
2222 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2223
2224 a = 1.0f;
2225 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2226 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2227 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2228 }
2229 break;
2230 case D3DFMT_A1R5G5B5:
2231 {
2232 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2233
2234 a = (argb & 0x8000) ? 1.0f : 0.0f;
2235 b = (argb & 0x001F) * (1.0f / 0x001F);
2236 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2237 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2238 }
2239 break;
2240 case D3DFMT_A8R8G8B8:
2241 {
2242 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2243
2244 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2245 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2246 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2247 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2248 }
2249 break;
2250 case D3DFMT_X8R8G8B8:
2251 {
2252 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2253
2254 a = 1.0f;
2255 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2256 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2257 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2258 }
2259 break;
2260 case D3DFMT_A2R10G10B10:
2261 {
2262 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2263
2264 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2265 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2266 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2267 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2268 }
2269 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002270 case D3DFMT_A32B32G32R32F:
2271 {
2272 // float formats in D3D are stored rgba, rather than the other way round
2273 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2274 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2275 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2276 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2277 }
2278 break;
2279 case D3DFMT_A16B16G16R16F:
2280 {
2281 // float formats in D3D are stored rgba, rather than the other way round
2282 float abgr[4];
2283
2284 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2285
2286 a = abgr[3];
2287 b = abgr[2];
2288 g = abgr[1];
2289 r = abgr[0];
2290 }
2291 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 default:
2293 UNIMPLEMENTED(); // FIXME
2294 UNREACHABLE();
2295 }
2296
2297 switch (format)
2298 {
2299 case GL_RGBA:
2300 switch (type)
2301 {
2302 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002303 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2304 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2305 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2306 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 break;
2308 default: UNREACHABLE();
2309 }
2310 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002311 case GL_BGRA_EXT:
2312 switch (type)
2313 {
2314 case GL_UNSIGNED_BYTE:
2315 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2316 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2317 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2318 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2319 break;
2320 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2321 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2322 // this type is packed as follows:
2323 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2324 // --------------------------------------------------------------------------------
2325 // | 4th | 3rd | 2nd | 1st component |
2326 // --------------------------------------------------------------------------------
2327 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2328 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2329 ((unsigned short)(15 * a + 0.5f) << 12)|
2330 ((unsigned short)(15 * r + 0.5f) << 8) |
2331 ((unsigned short)(15 * g + 0.5f) << 4) |
2332 ((unsigned short)(15 * b + 0.5f) << 0);
2333 break;
2334 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2335 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2336 // this type is packed as follows:
2337 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2338 // --------------------------------------------------------------------------------
2339 // | 4th | 3rd | 2nd | 1st component |
2340 // --------------------------------------------------------------------------------
2341 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2342 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2343 ((unsigned short)( a + 0.5f) << 15) |
2344 ((unsigned short)(31 * r + 0.5f) << 10) |
2345 ((unsigned short)(31 * g + 0.5f) << 5) |
2346 ((unsigned short)(31 * b + 0.5f) << 0);
2347 break;
2348 default: UNREACHABLE();
2349 }
2350 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002351 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 switch (type)
2353 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002354 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002355 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2356 ((unsigned short)(31 * b + 0.5f) << 0) |
2357 ((unsigned short)(63 * g + 0.5f) << 5) |
2358 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 break;
2360 default: UNREACHABLE();
2361 }
2362 break;
2363 default: UNREACHABLE();
2364 }
2365 }
2366 }
2367
2368 systemSurface->UnlockRect();
2369
2370 systemSurface->Release();
2371}
2372
2373void Context::clear(GLbitfield mask)
2374{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002375 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002376
2377 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2378 {
2379 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2380
2381 return;
2382 }
2383
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002384 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 IDirect3DDevice9 *device = getDevice();
2386 DWORD flags = 0;
2387
2388 if (mask & GL_COLOR_BUFFER_BIT)
2389 {
2390 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002391
2392 if (framebufferObject->getColorbufferType() != GL_NONE)
2393 {
2394 flags |= D3DCLEAR_TARGET;
2395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
2397
2398 if (mask & GL_DEPTH_BUFFER_BIT)
2399 {
2400 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002401 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 {
2403 flags |= D3DCLEAR_ZBUFFER;
2404 }
2405 }
2406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 GLuint stencilUnmasked = 0x0;
2408
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002409 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002412 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002414 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2415 D3DSURFACE_DESC desc;
2416 depthStencil->GetDesc(&desc);
2417
2418 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2419 stencilUnmasked = (0x1 << stencilSize) - 1;
2420
2421 if (stencilUnmasked != 0x0)
2422 {
2423 flags |= D3DCLEAR_STENCIL;
2424 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 }
2426 }
2427
2428 if (mask != 0)
2429 {
2430 return error(GL_INVALID_VALUE);
2431 }
2432
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002433 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2434 {
2435 return;
2436 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002438 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2439 unorm<8>(mState.colorClearValue.red),
2440 unorm<8>(mState.colorClearValue.green),
2441 unorm<8>(mState.colorClearValue.blue));
2442 float depth = clamp01(mState.depthClearValue);
2443 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444
2445 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2446
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002447 if (!renderTarget)
2448 {
2449 return; // Context must be lost, return silently
2450 }
2451
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 D3DSURFACE_DESC desc;
2453 renderTarget->GetDesc(&desc);
2454
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002455 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456
2457 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002458 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002460 !(mState.colorMaskRed && mState.colorMaskGreen &&
2461 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462
2463 if (needMaskedColorClear || needMaskedStencilClear)
2464 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002465 // State which is altered in all paths from this point to the clear call is saved.
2466 // State which is altered in only some paths will be flagged dirty in the case that
2467 // that path is taken.
2468 HRESULT hr;
2469 if (mMaskedClearSavedState == NULL)
2470 {
2471 hr = device->BeginStateBlock();
2472 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2473
2474 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2475 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2476 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2477 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2478 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2479 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2480 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2481 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2482 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2483 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2484 device->SetPixelShader(NULL);
2485 device->SetVertexShader(NULL);
2486 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2487 device->SetStreamSourceFreq(0, 1);
2488
2489 hr = device->EndStateBlock(&mMaskedClearSavedState);
2490 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2491 }
2492
2493 ASSERT(mMaskedClearSavedState != NULL);
2494
2495 if (mMaskedClearSavedState != NULL)
2496 {
2497 hr = mMaskedClearSavedState->Capture();
2498 ASSERT(SUCCEEDED(hr));
2499 }
2500
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2502 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2503 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2504 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2505 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2506 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2507 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2508 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2509
2510 if (flags & D3DCLEAR_TARGET)
2511 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002512 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2513 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2514 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2515 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 }
2517 else
2518 {
2519 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2520 }
2521
2522 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2523 {
2524 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2525 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2526 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2527 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002528 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002529 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2531 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002532 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 }
2534 else
2535 {
2536 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2537 }
2538
2539 device->SetPixelShader(NULL);
2540 device->SetVertexShader(NULL);
2541 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002542 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002544 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 {
2546 float x, y, z, w;
2547 D3DCOLOR diffuse;
2548 };
2549
2550 Vertex quad[4];
2551 quad[0].x = 0.0f;
2552 quad[0].y = (float)desc.Height;
2553 quad[0].z = 0.0f;
2554 quad[0].w = 1.0f;
2555 quad[0].diffuse = color;
2556
2557 quad[1].x = (float)desc.Width;
2558 quad[1].y = (float)desc.Height;
2559 quad[1].z = 0.0f;
2560 quad[1].w = 1.0f;
2561 quad[1].diffuse = color;
2562
2563 quad[2].x = 0.0f;
2564 quad[2].y = 0.0f;
2565 quad[2].z = 0.0f;
2566 quad[2].w = 1.0f;
2567 quad[2].diffuse = color;
2568
2569 quad[3].x = (float)desc.Width;
2570 quad[3].y = 0.0f;
2571 quad[3].z = 0.0f;
2572 quad[3].w = 1.0f;
2573 quad[3].diffuse = color;
2574
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002575 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
2578 if (flags & D3DCLEAR_ZBUFFER)
2579 {
2580 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2581 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2582 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2583 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002584
2585 if (mMaskedClearSavedState != NULL)
2586 {
2587 mMaskedClearSavedState->Apply();
2588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002590 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591 {
2592 device->Clear(0, NULL, flags, color, depth, stencil);
2593 }
2594}
2595
2596void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2597{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002598 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 {
2600 return error(GL_INVALID_OPERATION);
2601 }
2602
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002603 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 IDirect3DDevice9 *device = getDevice();
2605 D3DPRIMITIVETYPE primitiveType;
2606 int primitiveCount;
2607
2608 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2609 return error(GL_INVALID_ENUM);
2610
2611 if (primitiveCount <= 0)
2612 {
2613 return;
2614 }
2615
2616 if (!applyRenderTarget(false))
2617 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002618 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 }
2620
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002621 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002622
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002623 TranslatedIndexData indexInfo;
2624 bool useIndexing;
2625 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002626 if (err != GL_NO_ERROR)
2627 {
2628 return error(err);
2629 }
2630
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631 applyShaders();
2632 applyTextures();
2633
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002634 if (!getCurrentProgram()->validateSamplers())
2635 {
2636 return error(GL_INVALID_OPERATION);
2637 }
2638
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002639 if (!cullSkipsDraw(mode))
2640 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002641 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002642 if (useIndexing)
2643 {
2644 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2645 }
2646 else
2647 {
2648 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2649 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651}
2652
2653void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2654{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002655 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656 {
2657 return error(GL_INVALID_OPERATION);
2658 }
2659
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002660 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 {
2662 return error(GL_INVALID_OPERATION);
2663 }
2664
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002665 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 IDirect3DDevice9 *device = getDevice();
2667 D3DPRIMITIVETYPE primitiveType;
2668 int primitiveCount;
2669
2670 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2671 return error(GL_INVALID_ENUM);
2672
2673 if (primitiveCount <= 0)
2674 {
2675 return;
2676 }
2677
2678 if (!applyRenderTarget(false))
2679 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002680 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 }
2682
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002683 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002684
2685 TranslatedIndexData indexInfo;
2686 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2687 if (err != GL_NO_ERROR)
2688 {
2689 return error(err);
2690 }
2691
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002692 err = applyVertexBuffer(indexInfo);
2693 if (err != GL_NO_ERROR)
2694 {
2695 return error(err);
2696 }
2697
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698 applyShaders();
2699 applyTextures();
2700
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002701 if (!getCurrentProgram()->validateSamplers())
2702 {
2703 return error(GL_INVALID_OPERATION);
2704 }
2705
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002706 if (!cullSkipsDraw(mode))
2707 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002708 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002709 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 +00002710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711}
2712
2713void Context::finish()
2714{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002715 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716 IDirect3DDevice9 *device = getDevice();
2717 IDirect3DQuery9 *occlusionQuery = NULL;
2718
2719 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2720
2721 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2722 {
2723 return error(GL_OUT_OF_MEMORY);
2724 }
2725
2726 ASSERT(SUCCEEDED(result));
2727
2728 if (occlusionQuery)
2729 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002730 IDirect3DStateBlock9 *savedState = NULL;
2731 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2732
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002733 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2734 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735
2736 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002737 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 device->SetPixelShader(NULL);
2739 device->SetVertexShader(NULL);
2740 device->SetFVF(D3DFVF_XYZRHW);
2741 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002742 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002745 result = occlusionQuery->Issue(D3DISSUE_END);
2746 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747
2748 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2749 {
2750 // Keep polling, but allow other threads to do something useful first
2751 Sleep(0);
2752 }
2753
2754 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002755
2756 if (savedState)
2757 {
2758 savedState->Apply();
2759 savedState->Release();
2760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761 }
2762}
2763
2764void Context::flush()
2765{
2766 IDirect3DDevice9 *device = getDevice();
2767 IDirect3DQuery9 *eventQuery = NULL;
2768
2769 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2770
2771 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2772 {
2773 return error(GL_OUT_OF_MEMORY);
2774 }
2775
2776 ASSERT(SUCCEEDED(result));
2777
2778 if (eventQuery)
2779 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002780 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2781 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002783 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002785
2786 if (result == D3DERR_DEVICELOST)
2787 {
2788 error(GL_OUT_OF_MEMORY);
2789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790 }
2791}
2792
2793void Context::recordInvalidEnum()
2794{
2795 mInvalidEnum = true;
2796}
2797
2798void Context::recordInvalidValue()
2799{
2800 mInvalidValue = true;
2801}
2802
2803void Context::recordInvalidOperation()
2804{
2805 mInvalidOperation = true;
2806}
2807
2808void Context::recordOutOfMemory()
2809{
2810 mOutOfMemory = true;
2811}
2812
2813void Context::recordInvalidFramebufferOperation()
2814{
2815 mInvalidFramebufferOperation = true;
2816}
2817
2818// Get one of the recorded errors and clear its flag, if any.
2819// [OpenGL ES 2.0.24] section 2.5 page 13.
2820GLenum Context::getError()
2821{
2822 if (mInvalidEnum)
2823 {
2824 mInvalidEnum = false;
2825
2826 return GL_INVALID_ENUM;
2827 }
2828
2829 if (mInvalidValue)
2830 {
2831 mInvalidValue = false;
2832
2833 return GL_INVALID_VALUE;
2834 }
2835
2836 if (mInvalidOperation)
2837 {
2838 mInvalidOperation = false;
2839
2840 return GL_INVALID_OPERATION;
2841 }
2842
2843 if (mOutOfMemory)
2844 {
2845 mOutOfMemory = false;
2846
2847 return GL_OUT_OF_MEMORY;
2848 }
2849
2850 if (mInvalidFramebufferOperation)
2851 {
2852 mInvalidFramebufferOperation = false;
2853
2854 return GL_INVALID_FRAMEBUFFER_OPERATION;
2855 }
2856
2857 return GL_NO_ERROR;
2858}
2859
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002860bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002861{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002862 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002863}
2864
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002865int Context::getMaxSupportedSamples() const
2866{
2867 return mMaxSupportedSamples;
2868}
2869
2870int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2871{
2872 if (requested == 0)
2873 {
2874 return requested;
2875 }
2876
2877 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2878 if (itr == mMultiSampleSupport.end())
2879 {
2880 return -1;
2881 }
2882
2883 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2884 {
2885 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2886 {
2887 return i;
2888 }
2889 }
2890
2891 return -1;
2892}
2893
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002894bool Context::supportsEventQueries() const
2895{
2896 return mSupportsEventQueries;
2897}
2898
daniel@transgaming.com01868132010-08-24 19:21:17 +00002899bool Context::supportsCompressedTextures() const
2900{
2901 return mSupportsCompressedTextures;
2902}
2903
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002904bool Context::supportsFloatTextures() const
2905{
2906 return mSupportsFloatTextures;
2907}
2908
2909bool Context::supportsFloatLinearFilter() const
2910{
2911 return mSupportsFloatLinearFilter;
2912}
2913
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002914bool Context::supportsFloatRenderableTextures() const
2915{
2916 return mSupportsFloatRenderableTextures;
2917}
2918
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002919bool Context::supportsHalfFloatTextures() const
2920{
2921 return mSupportsHalfFloatTextures;
2922}
2923
2924bool Context::supportsHalfFloatLinearFilter() const
2925{
2926 return mSupportsHalfFloatLinearFilter;
2927}
2928
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002929bool Context::supportsHalfFloatRenderableTextures() const
2930{
2931 return mSupportsHalfFloatRenderableTextures;
2932}
2933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002934void Context::detachBuffer(GLuint buffer)
2935{
2936 // [OpenGL ES 2.0.24] section 2.9 page 22:
2937 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2938 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2939
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002940 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002942 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002943 }
2944
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002945 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002947 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002948 }
2949
2950 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2951 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002952 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002953 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002954 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002955 }
2956 }
2957}
2958
2959void Context::detachTexture(GLuint texture)
2960{
2961 // [OpenGL ES 2.0.24] section 3.8 page 84:
2962 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2963 // rebound to texture object zero
2964
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002965 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002966 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002967 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002968 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002969 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002970 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002971 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002972 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002973 }
2974 }
2975
2976 // [OpenGL ES 2.0.24] section 4.4 page 112:
2977 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2978 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2979 // image was attached in the currently bound framebuffer.
2980
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002981 Framebuffer *readFramebuffer = getReadFramebuffer();
2982 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002983
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002984 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002985 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002986 readFramebuffer->detachTexture(texture);
2987 }
2988
2989 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2990 {
2991 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002992 }
2993}
2994
2995void Context::detachFramebuffer(GLuint framebuffer)
2996{
2997 // [OpenGL ES 2.0.24] section 4.4 page 107:
2998 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2999 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3000
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003001 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003002 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003003 bindReadFramebuffer(0);
3004 }
3005
3006 if (mState.drawFramebuffer == framebuffer)
3007 {
3008 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003009 }
3010}
3011
3012void Context::detachRenderbuffer(GLuint renderbuffer)
3013{
3014 // [OpenGL ES 2.0.24] section 4.4 page 109:
3015 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3016 // had been executed with the target RENDERBUFFER and name of zero.
3017
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003018 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 {
3020 bindRenderbuffer(0);
3021 }
3022
3023 // [OpenGL ES 2.0.24] section 4.4 page 111:
3024 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3025 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3026 // point to which this image was attached in the currently bound framebuffer.
3027
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003028 Framebuffer *readFramebuffer = getReadFramebuffer();
3029 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003031 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003032 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003033 readFramebuffer->detachRenderbuffer(renderbuffer);
3034 }
3035
3036 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3037 {
3038 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003039 }
3040}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003041
3042Texture *Context::getIncompleteTexture(SamplerType type)
3043{
3044 Texture *t = mIncompleteTextures[type];
3045
3046 if (t == NULL)
3047 {
3048 static const GLubyte color[] = { 0, 0, 0, 255 };
3049
3050 switch (type)
3051 {
3052 default:
3053 UNREACHABLE();
3054 // default falls through to SAMPLER_2D
3055
3056 case SAMPLER_2D:
3057 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003058 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003059 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003060 t = incomplete2d;
3061 }
3062 break;
3063
3064 case SAMPLER_CUBE:
3065 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003066 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003067
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003068 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3069 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3070 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3071 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3072 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3073 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003074
3075 t = incompleteCube;
3076 }
3077 break;
3078 }
3079
3080 mIncompleteTextures[type] = t;
3081 }
3082
3083 return t;
3084}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003085
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003086bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003087{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003088 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003089}
3090
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003091bool Context::isTriangleMode(GLenum drawMode)
3092{
3093 switch (drawMode)
3094 {
3095 case GL_TRIANGLES:
3096 case GL_TRIANGLE_FAN:
3097 case GL_TRIANGLE_STRIP:
3098 return true;
3099 case GL_POINTS:
3100 case GL_LINES:
3101 case GL_LINE_LOOP:
3102 case GL_LINE_STRIP:
3103 return false;
3104 default: UNREACHABLE();
3105 }
3106
3107 return false;
3108}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003109
3110void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3111{
3112 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3113
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003114 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3115 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3116 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3117 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003118
3119 mVertexDataManager->dirtyCurrentValues();
3120}
3121
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003122void Context::initExtensionString()
3123{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003124 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003125 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3126 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003127 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003128 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003129 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003130
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003131 if (supportsEventQueries())
3132 {
3133 mExtensionString += "GL_NV_fence ";
3134 }
3135
daniel@transgaming.com01868132010-08-24 19:21:17 +00003136 if (supportsCompressedTextures())
3137 {
3138 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3139 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003140
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003141 if (supportsFloatTextures())
3142 {
3143 mExtensionString += "GL_OES_texture_float ";
3144 }
3145
3146 if (supportsHalfFloatTextures())
3147 {
3148 mExtensionString += "GL_OES_texture_half_float ";
3149 }
3150
3151 if (supportsFloatLinearFilter())
3152 {
3153 mExtensionString += "GL_OES_texture_float_linear ";
3154 }
3155
3156 if (supportsHalfFloatLinearFilter())
3157 {
3158 mExtensionString += "GL_OES_texture_half_float_linear ";
3159 }
3160
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003161 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003162 {
3163 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3164 }
3165
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003166 if (mBufferBackEnd->supportIntIndices())
3167 {
3168 mExtensionString += "GL_OES_element_index_uint ";
3169 }
3170
3171 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3172 if (end != std::string::npos)
3173 {
3174 mExtensionString.resize(end+1);
3175 }
3176}
3177
3178const char *Context::getExtensionString() const
3179{
3180 return mExtensionString.c_str();
3181}
3182
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003183void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3184 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3185 GLbitfield mask)
3186{
3187 IDirect3DDevice9 *device = getDevice();
3188
3189 Framebuffer *readFramebuffer = getReadFramebuffer();
3190 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3191
3192 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3193 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3194 {
3195 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3196 }
3197
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003198 if (drawFramebuffer->getSamples() != 0)
3199 {
3200 return error(GL_INVALID_OPERATION);
3201 }
3202
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003203 RECT sourceRect;
3204 RECT destRect;
3205
3206 if (srcX0 < srcX1)
3207 {
3208 sourceRect.left = srcX0;
3209 sourceRect.right = srcX1;
3210 destRect.left = dstX0;
3211 destRect.right = dstX1;
3212 }
3213 else
3214 {
3215 sourceRect.left = srcX1;
3216 destRect.left = dstX1;
3217 sourceRect.right = srcX0;
3218 destRect.right = dstX0;
3219 }
3220
3221 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3222 // flip our Y-values here
3223 if (srcY0 < srcY1)
3224 {
3225 sourceRect.bottom = srcY1;
3226 destRect.bottom = dstY1;
3227 sourceRect.top = srcY0;
3228 destRect.top = dstY0;
3229 }
3230 else
3231 {
3232 sourceRect.bottom = srcY0;
3233 destRect.bottom = dstY0;
3234 sourceRect.top = srcY1;
3235 destRect.top = dstY1;
3236 }
3237
3238 RECT sourceScissoredRect = sourceRect;
3239 RECT destScissoredRect = destRect;
3240
3241 if (mState.scissorTest)
3242 {
3243 // Only write to parts of the destination framebuffer which pass the scissor test
3244 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3245 // rect will be checked against scissorY, rather than the bottom.
3246 if (destRect.left < mState.scissorX)
3247 {
3248 int xDiff = mState.scissorX - destRect.left;
3249 destScissoredRect.left = mState.scissorX;
3250 sourceScissoredRect.left += xDiff;
3251 }
3252
3253 if (destRect.right > mState.scissorX + mState.scissorWidth)
3254 {
3255 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3256 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3257 sourceScissoredRect.right -= xDiff;
3258 }
3259
3260 if (destRect.top < mState.scissorY)
3261 {
3262 int yDiff = mState.scissorY - destRect.top;
3263 destScissoredRect.top = mState.scissorY;
3264 sourceScissoredRect.top += yDiff;
3265 }
3266
3267 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3268 {
3269 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3270 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3271 sourceScissoredRect.bottom -= yDiff;
3272 }
3273 }
3274
3275 bool blitRenderTarget = false;
3276 bool blitDepthStencil = false;
3277
3278 RECT sourceTrimmedRect = sourceScissoredRect;
3279 RECT destTrimmedRect = destScissoredRect;
3280
3281 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3282 // the actual draw and read surfaces.
3283 if (sourceTrimmedRect.left < 0)
3284 {
3285 int xDiff = 0 - sourceTrimmedRect.left;
3286 sourceTrimmedRect.left = 0;
3287 destTrimmedRect.left += xDiff;
3288 }
3289
3290 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3291 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3292 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3293 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3294
3295 if (sourceTrimmedRect.right > readBufferWidth)
3296 {
3297 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3298 sourceTrimmedRect.right = readBufferWidth;
3299 destTrimmedRect.right -= xDiff;
3300 }
3301
3302 if (sourceTrimmedRect.top < 0)
3303 {
3304 int yDiff = 0 - sourceTrimmedRect.top;
3305 sourceTrimmedRect.top = 0;
3306 destTrimmedRect.top += yDiff;
3307 }
3308
3309 if (sourceTrimmedRect.bottom > readBufferHeight)
3310 {
3311 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3312 sourceTrimmedRect.bottom = readBufferHeight;
3313 destTrimmedRect.bottom -= yDiff;
3314 }
3315
3316 if (destTrimmedRect.left < 0)
3317 {
3318 int xDiff = 0 - destTrimmedRect.left;
3319 destTrimmedRect.left = 0;
3320 sourceTrimmedRect.left += xDiff;
3321 }
3322
3323 if (destTrimmedRect.right > drawBufferWidth)
3324 {
3325 int xDiff = destTrimmedRect.right - drawBufferWidth;
3326 destTrimmedRect.right = drawBufferWidth;
3327 sourceTrimmedRect.right -= xDiff;
3328 }
3329
3330 if (destTrimmedRect.top < 0)
3331 {
3332 int yDiff = 0 - destTrimmedRect.top;
3333 destTrimmedRect.top = 0;
3334 sourceTrimmedRect.top += yDiff;
3335 }
3336
3337 if (destTrimmedRect.bottom > drawBufferHeight)
3338 {
3339 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3340 destTrimmedRect.bottom = drawBufferHeight;
3341 sourceTrimmedRect.bottom -= yDiff;
3342 }
3343
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003344 bool partialBufferCopy = false;
3345 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3346 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3347 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3348 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3349 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3350 {
3351 partialBufferCopy = true;
3352 }
3353
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003354 if (mask & GL_COLOR_BUFFER_BIT)
3355 {
3356 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3357 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3358 {
3359 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3360 return error(GL_INVALID_OPERATION);
3361 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003362
3363 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3364 {
3365 return error(GL_INVALID_OPERATION);
3366 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003367
3368 blitRenderTarget = true;
3369
3370 }
3371
3372 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3373 {
3374 DepthStencilbuffer *readDSBuffer = NULL;
3375 DepthStencilbuffer *drawDSBuffer = NULL;
3376
3377 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3378 // both a depth and stencil buffer, it will be the same buffer.
3379
3380 if (mask & GL_DEPTH_BUFFER_BIT)
3381 {
3382 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3383 {
3384 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3385 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3386 {
3387 return error(GL_INVALID_OPERATION);
3388 }
3389
3390 blitDepthStencil = true;
3391 readDSBuffer = readFramebuffer->getDepthbuffer();
3392 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3393 }
3394 }
3395
3396 if (mask & GL_STENCIL_BUFFER_BIT)
3397 {
3398 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3399 {
3400 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3401 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3402 {
3403 return error(GL_INVALID_OPERATION);
3404 }
3405
3406 blitDepthStencil = true;
3407 readDSBuffer = readFramebuffer->getStencilbuffer();
3408 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3409 }
3410 }
3411
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003412 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003413 {
3414 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3415 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3416 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003417
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003418 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3419 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003420 {
3421 return error(GL_INVALID_OPERATION);
3422 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003423 }
3424
3425 if (blitRenderTarget || blitDepthStencil)
3426 {
3427 egl::Display *display = getDisplay();
3428 display->endScene();
3429
3430 if (blitRenderTarget)
3431 {
3432 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3433 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3434
3435 if (FAILED(result))
3436 {
3437 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3438 return;
3439 }
3440 }
3441
3442 if (blitDepthStencil)
3443 {
3444 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3445
3446 if (FAILED(result))
3447 {
3448 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3449 return;
3450 }
3451 }
3452 }
3453}
3454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003455}
3456
3457extern "C"
3458{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003459gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003460{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003461 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003462}
3463
3464void glDestroyContext(gl::Context *context)
3465{
3466 delete context;
3467
3468 if (context == gl::getContext())
3469 {
3470 gl::makeCurrent(NULL, NULL, NULL);
3471 }
3472}
3473
3474void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3475{
3476 gl::makeCurrent(context, display, surface);
3477}
3478
3479gl::Context *glGetCurrentContext()
3480{
3481 return gl::getContext();
3482}
3483}