blob: 9c1ada4246c79457a5c571ba4c17a4dec014e252 [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;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087
daniel@transgaming.com428d1582010-05-04 03:35:25 +000088 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000089
daniel@transgaming.com428d1582010-05-04 03:35:25 +000090 mState.viewportX = 0;
91 mState.viewportY = 0;
92 mState.viewportWidth = config->mDisplayMode.Width;
93 mState.viewportHeight = config->mDisplayMode.Height;
94 mState.zNear = 0.0f;
95 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
daniel@transgaming.com428d1582010-05-04 03:35:25 +000097 mState.scissorX = 0;
98 mState.scissorY = 0;
99 mState.scissorWidth = config->mDisplayMode.Width;
100 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000102 mState.colorMaskRed = true;
103 mState.colorMaskGreen = true;
104 mState.colorMaskBlue = true;
105 mState.colorMaskAlpha = true;
106 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000108 if (shareContext != NULL)
109 {
110 mResourceManager = shareContext->mResourceManager;
111 mResourceManager->addRef();
112 }
113 else
114 {
115 mResourceManager = new ResourceManager();
116 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000117
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118 // [OpenGL ES 2.0.24] section 3.7 page 83:
119 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
120 // and cube map texture state vectors respectively associated with them.
121 // In order that access to these initial textures not be lost, they are treated as texture
122 // objects all of whose names are 0.
123
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000124 mTexture2DZero = new Texture2D(0);
125 mTextureCubeMapZero = new TextureCubeMap(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126
127 mColorbufferZero = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000128 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000130 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000131 bindArrayBuffer(0);
132 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 bindTextureCubeMap(0);
134 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000135 bindReadFramebuffer(0);
136 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 bindRenderbuffer(0);
138
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000139 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 {
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000141 mIncompleteTextures[type] = NULL;
142 }
143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000146 mState.packAlignment = 4;
147 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000148
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149 mBufferBackEnd = NULL;
150 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000151 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000152 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 mInvalidEnum = false;
155 mInvalidValue = false;
156 mInvalidOperation = false;
157 mOutOfMemory = false;
158 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000159
160 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000161
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000162 mSupportsCompressedTextures = false;
163 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000164 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000165 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000166 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
169Context::~Context()
170{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000171 if (mState.currentProgram != 0)
172 {
173 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
174 if (programObject)
175 {
176 programObject->release();
177 }
178 mState.currentProgram = 0;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000181 while (!mFramebufferMap.empty())
182 {
183 deleteFramebuffer(mFramebufferMap.begin()->first);
184 }
185
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000186 while (!mFenceMap.empty())
187 {
188 deleteFence(mFenceMap.begin()->first);
189 }
190
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000191 while (!mMultiSampleSupport.empty())
192 {
193 delete [] mMultiSampleSupport.begin()->second;
194 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
195 }
196
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000197 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
198 {
199 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
200 {
201 mState.samplerTexture[type][sampler].set(NULL);
202 }
203 }
204
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000205 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
206 {
207 delete mIncompleteTextures[type];
208 }
209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
211 {
212 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
213 }
214
215 mState.arrayBuffer.set(NULL);
216 mState.elementArrayBuffer.set(NULL);
217 mState.texture2D.set(NULL);
218 mState.textureCubeMap.set(NULL);
219 mState.renderbuffer.set(NULL);
220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221 delete mTexture2DZero;
222 delete mTextureCubeMapZero;
223
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000224 delete mBufferBackEnd;
225 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000226 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000227 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000228
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000229 if (mMaskedClearSavedState)
230 {
231 mMaskedClearSavedState->Release();
232 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000233
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000234 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
237void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
238{
239 IDirect3DDevice9 *device = display->getDevice();
240
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000241 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000242 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000243 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000244
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000245 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000246 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000247 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000248 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000249
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000250 const D3DFORMAT renderBufferFormats[] =
251 {
252 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000253 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000254 D3DFMT_R5G6B5,
255 D3DFMT_D24S8
256 };
257
258 int max = 0;
259 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
260 {
261 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
262 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
263 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
264
265 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
266 {
267 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
268 {
269 max = j;
270 }
271 }
272 }
273
274 mMaxSupportedSamples = max;
275
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000276 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000277 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com0a337e92010-08-28 17:38:27 +0000278 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter);
279 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000280
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000281 initExtensionString();
282
283 mState.viewportX = 0;
284 mState.viewportY = 0;
285 mState.viewportWidth = surface->getWidth();
286 mState.viewportHeight = surface->getHeight();
287
288 mState.scissorX = 0;
289 mState.scissorY = 0;
290 mState.scissorWidth = surface->getWidth();
291 mState.scissorHeight = surface->getHeight();
292
293 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000294 }
295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
297 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000298 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000301 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000302 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
304 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000306 if (defaultRenderTarget)
307 {
308 defaultRenderTarget->Release();
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000311 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000313 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000315
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000316 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000317
318 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319}
320
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000321// 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 +0000322void Context::markAllStateDirty()
323{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000324 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000325 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000326 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000327
328 mClearStateDirty = true;
329 mCullStateDirty = true;
330 mDepthStateDirty = true;
331 mMaskStateDirty = true;
332 mBlendStateDirty = true;
333 mStencilStateDirty = true;
334 mPolygonOffsetStateDirty = true;
335 mScissorStateDirty = true;
336 mSampleStateDirty = true;
337 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000338 mFrontFaceDirty = true;
339
340 if (mBufferBackEnd != NULL)
341 {
342 mBufferBackEnd->invalidate();
343 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000344}
345
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346void Context::setClearColor(float red, float green, float blue, float alpha)
347{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000348 mState.colorClearValue.red = red;
349 mState.colorClearValue.green = green;
350 mState.colorClearValue.blue = blue;
351 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
354void Context::setClearDepth(float depth)
355{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000356 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357}
358
359void Context::setClearStencil(int stencil)
360{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000361 mState.stencilClearValue = stencil;
362}
363
364void Context::setCullFace(bool enabled)
365{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000366 if (mState.cullFace != enabled)
367 {
368 mState.cullFace = enabled;
369 mCullStateDirty = true;
370 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000371}
372
373bool Context::isCullFaceEnabled() const
374{
375 return mState.cullFace;
376}
377
378void Context::setCullMode(GLenum mode)
379{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000380 if (mState.cullMode != mode)
381 {
382 mState.cullMode = mode;
383 mCullStateDirty = true;
384 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000385}
386
387void Context::setFrontFace(GLenum front)
388{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000389 if (mState.frontFace != front)
390 {
391 mState.frontFace = front;
392 mFrontFaceDirty = true;
393 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000394}
395
396void Context::setDepthTest(bool enabled)
397{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000398 if (mState.depthTest != enabled)
399 {
400 mState.depthTest = enabled;
401 mDepthStateDirty = true;
402 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403}
404
405bool Context::isDepthTestEnabled() const
406{
407 return mState.depthTest;
408}
409
410void Context::setDepthFunc(GLenum depthFunc)
411{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000412 if (mState.depthFunc != depthFunc)
413 {
414 mState.depthFunc = depthFunc;
415 mDepthStateDirty = true;
416 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000417}
418
419void Context::setDepthRange(float zNear, float zFar)
420{
421 mState.zNear = zNear;
422 mState.zFar = zFar;
423}
424
425void Context::setBlend(bool enabled)
426{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000427 if (mState.blend != enabled)
428 {
429 mState.blend = enabled;
430 mBlendStateDirty = true;
431 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000432}
433
434bool Context::isBlendEnabled() const
435{
436 return mState.blend;
437}
438
439void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
440{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000441 if (mState.sourceBlendRGB != sourceRGB ||
442 mState.sourceBlendAlpha != sourceAlpha ||
443 mState.destBlendRGB != destRGB ||
444 mState.destBlendAlpha != destAlpha)
445 {
446 mState.sourceBlendRGB = sourceRGB;
447 mState.destBlendRGB = destRGB;
448 mState.sourceBlendAlpha = sourceAlpha;
449 mState.destBlendAlpha = destAlpha;
450 mBlendStateDirty = true;
451 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000452}
453
454void Context::setBlendColor(float red, float green, float blue, float alpha)
455{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000456 if (mState.blendColor.red != red ||
457 mState.blendColor.green != green ||
458 mState.blendColor.blue != blue ||
459 mState.blendColor.alpha != alpha)
460 {
461 mState.blendColor.red = red;
462 mState.blendColor.green = green;
463 mState.blendColor.blue = blue;
464 mState.blendColor.alpha = alpha;
465 mBlendStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.blendEquationRGB != rgbEquation ||
472 mState.blendEquationAlpha != alphaEquation)
473 {
474 mState.blendEquationRGB = rgbEquation;
475 mState.blendEquationAlpha = alphaEquation;
476 mBlendStateDirty = true;
477 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000478}
479
480void Context::setStencilTest(bool enabled)
481{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000482 if (mState.stencilTest != enabled)
483 {
484 mState.stencilTest = enabled;
485 mStencilStateDirty = true;
486 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000487}
488
489bool Context::isStencilTestEnabled() const
490{
491 return mState.stencilTest;
492}
493
494void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
495{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000496 if (mState.stencilFunc != stencilFunc ||
497 mState.stencilRef != stencilRef ||
498 mState.stencilMask != stencilMask)
499 {
500 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000501 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000502 mState.stencilMask = stencilMask;
503 mStencilStateDirty = true;
504 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000505}
506
507void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
508{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000509 if (mState.stencilBackFunc != stencilBackFunc ||
510 mState.stencilBackRef != stencilBackRef ||
511 mState.stencilBackMask != stencilBackMask)
512 {
513 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000514 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 mState.stencilBackMask = stencilBackMask;
516 mStencilStateDirty = true;
517 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000518}
519
520void Context::setStencilWritemask(GLuint stencilWritemask)
521{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000522 if (mState.stencilWritemask != stencilWritemask)
523 {
524 mState.stencilWritemask = stencilWritemask;
525 mStencilStateDirty = true;
526 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000527}
528
529void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
530{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000531 if (mState.stencilBackWritemask != stencilBackWritemask)
532 {
533 mState.stencilBackWritemask = stencilBackWritemask;
534 mStencilStateDirty = true;
535 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000536}
537
538void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
539{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000540 if (mState.stencilFail != stencilFail ||
541 mState.stencilPassDepthFail != stencilPassDepthFail ||
542 mState.stencilPassDepthPass != stencilPassDepthPass)
543 {
544 mState.stencilFail = stencilFail;
545 mState.stencilPassDepthFail = stencilPassDepthFail;
546 mState.stencilPassDepthPass = stencilPassDepthPass;
547 mStencilStateDirty = true;
548 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000549}
550
551void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
552{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000553 if (mState.stencilBackFail != stencilBackFail ||
554 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
555 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
556 {
557 mState.stencilBackFail = stencilBackFail;
558 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
559 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
560 mStencilStateDirty = true;
561 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000562}
563
564void Context::setPolygonOffsetFill(bool enabled)
565{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000566 if (mState.polygonOffsetFill != enabled)
567 {
568 mState.polygonOffsetFill = enabled;
569 mPolygonOffsetStateDirty = true;
570 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000571}
572
573bool Context::isPolygonOffsetFillEnabled() const
574{
575 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.polygonOffsetFactor != factor ||
582 mState.polygonOffsetUnits != units)
583 {
584 mState.polygonOffsetFactor = factor;
585 mState.polygonOffsetUnits = units;
586 mPolygonOffsetStateDirty = true;
587 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000588}
589
590void Context::setSampleAlphaToCoverage(bool enabled)
591{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000592 if (mState.sampleAlphaToCoverage != enabled)
593 {
594 mState.sampleAlphaToCoverage = enabled;
595 mSampleStateDirty = true;
596 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000597}
598
599bool Context::isSampleAlphaToCoverageEnabled() const
600{
601 return mState.sampleAlphaToCoverage;
602}
603
604void Context::setSampleCoverage(bool enabled)
605{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000606 if (mState.sampleCoverage != enabled)
607 {
608 mState.sampleCoverage = enabled;
609 mSampleStateDirty = true;
610 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000611}
612
613bool Context::isSampleCoverageEnabled() const
614{
615 return mState.sampleCoverage;
616}
617
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000618void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.sampleCoverageValue != value ||
621 mState.sampleCoverageInvert != invert)
622 {
623 mState.sampleCoverageValue = value;
624 mState.sampleCoverageInvert = invert;
625 mSampleStateDirty = true;
626 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000627}
628
629void Context::setScissorTest(bool enabled)
630{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000631 if (mState.scissorTest != enabled)
632 {
633 mState.scissorTest = enabled;
634 mScissorStateDirty = true;
635 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638bool Context::isScissorTestEnabled() const
639{
640 return mState.scissorTest;
641}
642
643void Context::setDither(bool enabled)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.dither != enabled)
646 {
647 mState.dither = enabled;
648 mDitherStateDirty = true;
649 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652bool Context::isDitherEnabled() const
653{
654 return mState.dither;
655}
656
657void Context::setLineWidth(GLfloat width)
658{
659 mState.lineWidth = width;
660}
661
662void Context::setGenerateMipmapHint(GLenum hint)
663{
664 mState.generateMipmapHint = hint;
665}
666
667void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
668{
669 mState.viewportX = x;
670 mState.viewportY = y;
671 mState.viewportWidth = width;
672 mState.viewportHeight = height;
673}
674
675void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
676{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000677 if (mState.scissorX != x || mState.scissorY != y ||
678 mState.scissorWidth != width || mState.scissorHeight != height)
679 {
680 mState.scissorX = x;
681 mState.scissorY = y;
682 mState.scissorWidth = width;
683 mState.scissorHeight = height;
684 mScissorStateDirty = true;
685 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000686}
687
688void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
689{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000690 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
691 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
692 {
693 mState.colorMaskRed = red;
694 mState.colorMaskGreen = green;
695 mState.colorMaskBlue = blue;
696 mState.colorMaskAlpha = alpha;
697 mMaskStateDirty = true;
698 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000699}
700
701void Context::setDepthMask(bool mask)
702{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000703 if (mState.depthMask != mask)
704 {
705 mState.depthMask = mask;
706 mMaskStateDirty = true;
707 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000708}
709
710void Context::setActiveSampler(int active)
711{
712 mState.activeSampler = active;
713}
714
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000715GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000717 return mState.readFramebuffer;
718}
719
720GLuint Context::getDrawFramebufferHandle() const
721{
722 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000723}
724
725GLuint Context::getRenderbufferHandle() const
726{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000727 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000728}
729
730GLuint Context::getArrayBufferHandle() const
731{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000732 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000733}
734
735void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
736{
737 mState.vertexAttribute[attribNum].mEnabled = enabled;
738}
739
740const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
741{
742 return mState.vertexAttribute[attribNum];
743}
744
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000745void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000746 GLsizei stride, const void *pointer)
747{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000748 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000749 mState.vertexAttribute[attribNum].mSize = size;
750 mState.vertexAttribute[attribNum].mType = type;
751 mState.vertexAttribute[attribNum].mNormalized = normalized;
752 mState.vertexAttribute[attribNum].mStride = stride;
753 mState.vertexAttribute[attribNum].mPointer = pointer;
754}
755
756const void *Context::getVertexAttribPointer(unsigned int attribNum) const
757{
758 return mState.vertexAttribute[attribNum].mPointer;
759}
760
761// returns entire set of attributes as a block
762const AttributeState *Context::getVertexAttribBlock()
763{
764 return mState.vertexAttribute;
765}
766
767void Context::setPackAlignment(GLint alignment)
768{
769 mState.packAlignment = alignment;
770}
771
772GLint Context::getPackAlignment() const
773{
774 return mState.packAlignment;
775}
776
777void Context::setUnpackAlignment(GLint alignment)
778{
779 mState.unpackAlignment = alignment;
780}
781
782GLint Context::getUnpackAlignment() const
783{
784 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785}
786
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787GLuint Context::createBuffer()
788{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000789 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790}
791
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792GLuint Context::createProgram()
793{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000794 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795}
796
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000797GLuint Context::createShader(GLenum type)
798{
799 return mResourceManager->createShader(type);
800}
801
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802GLuint Context::createTexture()
803{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000804 return mResourceManager->createTexture();
805}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000807GLuint Context::createRenderbuffer()
808{
809 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810}
811
812// Returns an unused framebuffer name
813GLuint Context::createFramebuffer()
814{
815 unsigned int handle = 1;
816
817 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
818 {
819 handle++;
820 }
821
822 mFramebufferMap[handle] = NULL;
823
824 return handle;
825}
826
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000827GLuint Context::createFence()
828{
829 unsigned int handle = 0;
830
831 while (mFenceMap.find(handle) != mFenceMap.end())
832 {
833 handle++;
834 }
835
836 mFenceMap[handle] = new Fence;
837
838 return handle;
839}
840
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841void Context::deleteBuffer(GLuint buffer)
842{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000843 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844 {
845 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000847
848 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849}
850
851void Context::deleteShader(GLuint shader)
852{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000853 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
856void Context::deleteProgram(GLuint program)
857{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
861void Context::deleteTexture(GLuint texture)
862{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000863 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864 {
865 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000867
868 mResourceManager->deleteTexture(texture);
869}
870
871void Context::deleteRenderbuffer(GLuint renderbuffer)
872{
873 if (mResourceManager->getRenderbuffer(renderbuffer))
874 {
875 detachRenderbuffer(renderbuffer);
876 }
877
878 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
881void Context::deleteFramebuffer(GLuint framebuffer)
882{
883 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
884
885 if (framebufferObject != mFramebufferMap.end())
886 {
887 detachFramebuffer(framebuffer);
888
889 delete framebufferObject->second;
890 mFramebufferMap.erase(framebufferObject);
891 }
892}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000893
894void Context::deleteFence(GLuint fence)
895{
896 FenceMap::iterator fenceObject = mFenceMap.find(fence);
897
898 if (fenceObject != mFenceMap.end())
899 {
900 delete fenceObject->second;
901 mFenceMap.erase(fenceObject);
902 }
903}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000905Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907 return mResourceManager->getBuffer(handle);
908}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910Shader *Context::getShader(GLuint handle)
911{
912 return mResourceManager->getShader(handle);
913}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000915Program *Context::getProgram(GLuint handle)
916{
917 return mResourceManager->getProgram(handle);
918}
919
920Texture *Context::getTexture(GLuint handle)
921{
922 return mResourceManager->getTexture(handle);
923}
924
925Renderbuffer *Context::getRenderbuffer(GLuint handle)
926{
927 return mResourceManager->getRenderbuffer(handle);
928}
929
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000930Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000931{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000932 return getFramebuffer(mState.readFramebuffer);
933}
934
935Framebuffer *Context::getDrawFramebuffer()
936{
937 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938}
939
940void Context::bindArrayBuffer(unsigned int buffer)
941{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000944 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945}
946
947void Context::bindElementArrayBuffer(unsigned int buffer)
948{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000949 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000951 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::bindTexture2D(GLuint texture)
955{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000956 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000958 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000960 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961}
962
963void Context::bindTextureCubeMap(GLuint texture)
964{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000967 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000969 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000972void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973{
974 if (!getFramebuffer(framebuffer))
975 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000976 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977 }
978
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000979 mState.readFramebuffer = framebuffer;
980}
981
982void Context::bindDrawFramebuffer(GLuint framebuffer)
983{
984 if (!getFramebuffer(framebuffer))
985 {
986 mFramebufferMap[framebuffer] = new Framebuffer();
987 }
988
989 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990}
991
992void Context::bindRenderbuffer(GLuint renderbuffer)
993{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000994 mResourceManager->checkRenderbufferAllocation(renderbuffer);
995
996 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
999void Context::useProgram(GLuint program)
1000{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001001 GLuint priorProgram = mState.currentProgram;
1002 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001003
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001004 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006 Program *newProgram = mResourceManager->getProgram(program);
1007 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1008
1009 if (newProgram)
1010 {
1011 newProgram->addRef();
1012 }
1013
1014 if (oldProgram)
1015 {
1016 oldProgram->release();
1017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019}
1020
1021void Context::setFramebufferZero(Framebuffer *buffer)
1022{
1023 delete mFramebufferMap[0];
1024 mFramebufferMap[0] = buffer;
1025}
1026
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001027void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001029 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1030 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031}
1032
1033Framebuffer *Context::getFramebuffer(unsigned int handle)
1034{
1035 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001036
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037 if (framebuffer == mFramebufferMap.end())
1038 {
1039 return NULL;
1040 }
1041 else
1042 {
1043 return framebuffer->second;
1044 }
1045}
1046
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001047Fence *Context::getFence(unsigned int handle)
1048{
1049 FenceMap::iterator fence = mFenceMap.find(handle);
1050
1051 if (fence == mFenceMap.end())
1052 {
1053 return NULL;
1054 }
1055 else
1056 {
1057 return fence->second;
1058 }
1059}
1060
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061Buffer *Context::getArrayBuffer()
1062{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001063 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064}
1065
1066Buffer *Context::getElementArrayBuffer()
1067{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001068 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
1071Program *Context::getCurrentProgram()
1072{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001073 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074}
1075
1076Texture2D *Context::getTexture2D()
1077{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001078 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 +00001079 {
1080 return mTexture2DZero;
1081 }
1082
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001083 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
1086TextureCubeMap *Context::getTextureCubeMap()
1087{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001088 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 +00001089 {
1090 return mTextureCubeMapZero;
1091 }
1092
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001093 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001096Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001098 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001099
1100 if (texid == 0)
1101 {
1102 switch (type)
1103 {
1104 default: UNREACHABLE();
1105 case SAMPLER_2D: return mTexture2DZero;
1106 case SAMPLER_CUBE: return mTextureCubeMapZero;
1107 }
1108 }
1109
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001110 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111}
1112
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113bool Context::getBooleanv(GLenum pname, GLboolean *params)
1114{
1115 switch (pname)
1116 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001117 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1118 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1119 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001120 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001121 params[0] = mState.colorMaskRed;
1122 params[1] = mState.colorMaskGreen;
1123 params[2] = mState.colorMaskBlue;
1124 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001125 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001126 case GL_CULL_FACE: *params = mState.cullFace;
1127 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1128 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1129 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1130 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1131 case GL_STENCIL_TEST: *params = mState.stencilTest;
1132 case GL_DEPTH_TEST: *params = mState.depthTest;
1133 case GL_BLEND: *params = mState.blend;
1134 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001135 default:
1136 return false;
1137 }
1138
1139 return true;
1140}
1141
1142bool Context::getFloatv(GLenum pname, GLfloat *params)
1143{
1144 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1145 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1146 // GetIntegerv as its native query function. As it would require conversion in any
1147 // case, this should make no difference to the calling application.
1148 switch (pname)
1149 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001150 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1151 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1152 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1153 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1154 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001155 case GL_ALIASED_LINE_WIDTH_RANGE:
1156 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1157 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1158 break;
1159 case GL_ALIASED_POINT_SIZE_RANGE:
1160 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001161 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 +00001162 break;
1163 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001164 params[0] = mState.zNear;
1165 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 break;
1167 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001168 params[0] = mState.colorClearValue.red;
1169 params[1] = mState.colorClearValue.green;
1170 params[2] = mState.colorClearValue.blue;
1171 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001172 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001173 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001174 params[0] = mState.blendColor.red;
1175 params[1] = mState.blendColor.green;
1176 params[2] = mState.blendColor.blue;
1177 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001178 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001179 default:
1180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186bool Context::getIntegerv(GLenum pname, GLint *params)
1187{
1188 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1189 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1190 // GetIntegerv as its native query function. As it would require conversion in any
1191 // case, this should make no difference to the calling application. You may find it in
1192 // Context::getFloatv.
1193 switch (pname)
1194 {
1195 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1196 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1197 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1198 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1199 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1200 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1201 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1202 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001203 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001204 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001205 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1206 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001207 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1208 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1209 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001210 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001211 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1212 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1213 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1214 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1215 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1216 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1217 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1218 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1219 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1220 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1221 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1222 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1223 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1224 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1225 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1226 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1228 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1229 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1230 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1231 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1232 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1233 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1234 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1235 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1236 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1237 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1238 case GL_SUBPIXEL_BITS: *params = 4; break;
1239 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1240 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001241 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1242 {
1243 if (supportsCompressedTextures())
1244 {
1245 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1246 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1247 *params = 2;
1248 }
1249 else
1250 {
1251 *params = 0;
1252 }
1253 }
1254 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001255 case GL_MAX_SAMPLES_ANGLE:
1256 {
1257 GLsizei maxSamples = getMaxSupportedSamples();
1258 if (maxSamples != 0)
1259 {
1260 *params = maxSamples;
1261 }
1262 else
1263 {
1264 return false;
1265 }
1266
1267 break;
1268 }
1269 case GL_SAMPLE_BUFFERS:
1270 case GL_SAMPLES:
1271 {
1272 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1273 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1274 {
1275 switch (pname)
1276 {
1277 case GL_SAMPLE_BUFFERS:
1278 if (framebuffer->getSamples() != 0)
1279 {
1280 *params = 1;
1281 }
1282 else
1283 {
1284 *params = 0;
1285 }
1286 break;
1287 case GL_SAMPLES:
1288 *params = framebuffer->getSamples();
1289 break;
1290 }
1291 }
1292 else
1293 {
1294 *params = 0;
1295 }
1296 }
1297 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001298 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1299 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1300 case GL_MAX_VIEWPORT_DIMS:
1301 {
1302 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1303 params[0] = maxDimension;
1304 params[1] = maxDimension;
1305 }
1306 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001307 case GL_COMPRESSED_TEXTURE_FORMATS:
1308 {
1309 if (supportsCompressedTextures())
1310 {
1311 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1312 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1313 }
1314 }
1315 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001317 params[0] = mState.viewportX;
1318 params[1] = mState.viewportY;
1319 params[2] = mState.viewportWidth;
1320 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001321 break;
1322 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001323 params[0] = mState.scissorX;
1324 params[1] = mState.scissorY;
1325 params[2] = mState.scissorWidth;
1326 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001327 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001328 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1329 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001330 case GL_RED_BITS:
1331 case GL_GREEN_BITS:
1332 case GL_BLUE_BITS:
1333 case GL_ALPHA_BITS:
1334 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001335 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001336 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1337
1338 if (colorbuffer)
1339 {
1340 switch (pname)
1341 {
1342 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1343 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1344 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1345 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1346 }
1347 }
1348 else
1349 {
1350 *params = 0;
1351 }
1352 }
1353 break;
1354 case GL_DEPTH_BITS:
1355 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001356 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001357 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001358
1359 if (depthbuffer)
1360 {
1361 *params = depthbuffer->getDepthSize();
1362 }
1363 else
1364 {
1365 *params = 0;
1366 }
1367 }
1368 break;
1369 case GL_STENCIL_BITS:
1370 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001371 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001372 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001373
1374 if (stencilbuffer)
1375 {
1376 *params = stencilbuffer->getStencilSize();
1377 }
1378 else
1379 {
1380 *params = 0;
1381 }
1382 }
1383 break;
1384 case GL_TEXTURE_BINDING_2D:
1385 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001386 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001387 {
1388 error(GL_INVALID_OPERATION);
1389 return false;
1390 }
1391
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001392 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001393 }
1394 break;
1395 case GL_TEXTURE_BINDING_CUBE_MAP:
1396 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001397 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398 {
1399 error(GL_INVALID_OPERATION);
1400 return false;
1401 }
1402
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001403 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001404 }
1405 break;
1406 default:
1407 return false;
1408 }
1409
1410 return true;
1411}
1412
1413bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1414{
1415 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1416 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1417 // to the fact that it is stored internally as a float, and so would require conversion
1418 // if returned from Context::getIntegerv. Since this conversion is already implemented
1419 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1420 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1421 // application.
1422 switch (pname)
1423 {
1424 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1425 case GL_SHADER_BINARY_FORMATS:
1426 {
1427 *type = GL_INT;
1428 *numParams = 0;
1429 }
1430 break;
1431 case GL_MAX_VERTEX_ATTRIBS:
1432 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1433 case GL_MAX_VARYING_VECTORS:
1434 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1435 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1436 case GL_MAX_TEXTURE_IMAGE_UNITS:
1437 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1438 case GL_MAX_RENDERBUFFER_SIZE:
1439 case GL_NUM_SHADER_BINARY_FORMATS:
1440 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1441 case GL_ARRAY_BUFFER_BINDING:
1442 case GL_FRAMEBUFFER_BINDING:
1443 case GL_RENDERBUFFER_BINDING:
1444 case GL_CURRENT_PROGRAM:
1445 case GL_PACK_ALIGNMENT:
1446 case GL_UNPACK_ALIGNMENT:
1447 case GL_GENERATE_MIPMAP_HINT:
1448 case GL_RED_BITS:
1449 case GL_GREEN_BITS:
1450 case GL_BLUE_BITS:
1451 case GL_ALPHA_BITS:
1452 case GL_DEPTH_BITS:
1453 case GL_STENCIL_BITS:
1454 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1455 case GL_CULL_FACE_MODE:
1456 case GL_FRONT_FACE:
1457 case GL_ACTIVE_TEXTURE:
1458 case GL_STENCIL_FUNC:
1459 case GL_STENCIL_VALUE_MASK:
1460 case GL_STENCIL_REF:
1461 case GL_STENCIL_FAIL:
1462 case GL_STENCIL_PASS_DEPTH_FAIL:
1463 case GL_STENCIL_PASS_DEPTH_PASS:
1464 case GL_STENCIL_BACK_FUNC:
1465 case GL_STENCIL_BACK_VALUE_MASK:
1466 case GL_STENCIL_BACK_REF:
1467 case GL_STENCIL_BACK_FAIL:
1468 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1469 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1470 case GL_DEPTH_FUNC:
1471 case GL_BLEND_SRC_RGB:
1472 case GL_BLEND_SRC_ALPHA:
1473 case GL_BLEND_DST_RGB:
1474 case GL_BLEND_DST_ALPHA:
1475 case GL_BLEND_EQUATION_RGB:
1476 case GL_BLEND_EQUATION_ALPHA:
1477 case GL_STENCIL_WRITEMASK:
1478 case GL_STENCIL_BACK_WRITEMASK:
1479 case GL_STENCIL_CLEAR_VALUE:
1480 case GL_SUBPIXEL_BITS:
1481 case GL_MAX_TEXTURE_SIZE:
1482 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1483 case GL_SAMPLE_BUFFERS:
1484 case GL_SAMPLES:
1485 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1486 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1487 case GL_TEXTURE_BINDING_2D:
1488 case GL_TEXTURE_BINDING_CUBE_MAP:
1489 {
1490 *type = GL_INT;
1491 *numParams = 1;
1492 }
1493 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001494 case GL_MAX_SAMPLES_ANGLE:
1495 {
1496 if (getMaxSupportedSamples() != 0)
1497 {
1498 *type = GL_INT;
1499 *numParams = 1;
1500 }
1501 else
1502 {
1503 return false;
1504 }
1505 }
1506 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001507 case GL_MAX_VIEWPORT_DIMS:
1508 {
1509 *type = GL_INT;
1510 *numParams = 2;
1511 }
1512 break;
1513 case GL_VIEWPORT:
1514 case GL_SCISSOR_BOX:
1515 {
1516 *type = GL_INT;
1517 *numParams = 4;
1518 }
1519 break;
1520 case GL_SHADER_COMPILER:
1521 case GL_SAMPLE_COVERAGE_INVERT:
1522 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001523 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1524 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1525 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1526 case GL_SAMPLE_COVERAGE:
1527 case GL_SCISSOR_TEST:
1528 case GL_STENCIL_TEST:
1529 case GL_DEPTH_TEST:
1530 case GL_BLEND:
1531 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001532 {
1533 *type = GL_BOOL;
1534 *numParams = 1;
1535 }
1536 break;
1537 case GL_COLOR_WRITEMASK:
1538 {
1539 *type = GL_BOOL;
1540 *numParams = 4;
1541 }
1542 break;
1543 case GL_POLYGON_OFFSET_FACTOR:
1544 case GL_POLYGON_OFFSET_UNITS:
1545 case GL_SAMPLE_COVERAGE_VALUE:
1546 case GL_DEPTH_CLEAR_VALUE:
1547 case GL_LINE_WIDTH:
1548 {
1549 *type = GL_FLOAT;
1550 *numParams = 1;
1551 }
1552 break;
1553 case GL_ALIASED_LINE_WIDTH_RANGE:
1554 case GL_ALIASED_POINT_SIZE_RANGE:
1555 case GL_DEPTH_RANGE:
1556 {
1557 *type = GL_FLOAT;
1558 *numParams = 2;
1559 }
1560 break;
1561 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001562 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001563 {
1564 *type = GL_FLOAT;
1565 *numParams = 4;
1566 }
1567 break;
1568 default:
1569 return false;
1570 }
1571
1572 return true;
1573}
1574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001575// Applies the render target surface, depth stencil surface, viewport rectangle and
1576// scissor rectangle to the Direct3D 9 device
1577bool Context::applyRenderTarget(bool ignoreViewport)
1578{
1579 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001580
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001581 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582
1583 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1584 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001585 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1586
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587 return false;
1588 }
1589
1590 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001591
1592 if (!renderTarget)
1593 {
1594 return false; // Context must be lost
1595 }
1596
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001597 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001599 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1600 if (renderTargetSerial != mAppliedRenderTargetSerial)
1601 {
1602 device->SetRenderTarget(0, renderTarget);
1603 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001604 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 +00001605 }
1606
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001607 unsigned int depthbufferSerial = 0;
1608 unsigned int stencilbufferSerial = 0;
1609 if (framebufferObject->getDepthbufferType() != GL_NONE)
1610 {
1611 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1612 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1613 }
1614 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1615 {
1616 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1617 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1618 }
1619
1620 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1621 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001622 {
1623 device->SetDepthStencilSurface(depthStencil);
1624 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001625 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001627
1628 D3DVIEWPORT9 viewport;
1629 D3DSURFACE_DESC desc;
1630 renderTarget->GetDesc(&desc);
1631
1632 if (ignoreViewport)
1633 {
1634 viewport.X = 0;
1635 viewport.Y = 0;
1636 viewport.Width = desc.Width;
1637 viewport.Height = desc.Height;
1638 viewport.MinZ = 0.0f;
1639 viewport.MaxZ = 1.0f;
1640 }
1641 else
1642 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001643 viewport.X = std::max(mState.viewportX, 0);
1644 viewport.Y = std::max(mState.viewportY, 0);
1645 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1646 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1647 viewport.MinZ = clamp01(mState.zNear);
1648 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 }
1650
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001651 if (viewport.Width <= 0 || viewport.Height <= 0)
1652 {
1653 return false; // Nothing to render
1654 }
1655
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 device->SetViewport(&viewport);
1657
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001658 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001660 if (mState.scissorTest)
1661 {
1662 RECT rect = {mState.scissorX,
1663 mState.scissorY,
1664 mState.scissorX + mState.scissorWidth,
1665 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001666 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1667 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 device->SetScissorRect(&rect);
1669 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1670 }
1671 else
1672 {
1673 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1674 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001675
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 }
1678
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001679 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 Program *programObject = getCurrentProgram();
1682
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001683 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001684 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001686
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001687 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001688 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1689 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1690 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001691 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1692
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001693 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001694 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001695 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1696
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001697 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001698 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001699
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001700 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001701 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001702
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001703 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001704 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001705 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706 }
1707
1708 return true;
1709}
1710
1711// 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 +00001712void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713{
1714 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001715 Program *programObject = getCurrentProgram();
1716
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001717 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001718 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001719 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001721 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001722 GLint alwaysFront = !isTriangleMode(drawMode);
1723 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1724
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001725 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001726
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001727 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001729 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001731 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 }
1733 else
1734 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001735 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 }
1737
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 mCullStateDirty = false;
1739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001741 if (mDepthStateDirty)
1742 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001743 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001745 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1746 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 }
1748 else
1749 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752
1753 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001758 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001759 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1761
1762 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1763 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1764 {
1765 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1766 }
1767 else
1768 {
1769 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1770 unorm<8>(mState.blendColor.alpha),
1771 unorm<8>(mState.blendColor.alpha),
1772 unorm<8>(mState.blendColor.alpha)));
1773 }
1774
1775 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1776 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1777 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1778
1779 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1780 mState.destBlendRGB != mState.destBlendAlpha ||
1781 mState.blendEquationRGB != mState.blendEquationAlpha)
1782 {
1783 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1784
1785 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1786 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1787 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1788
1789 }
1790 else
1791 {
1792 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1793 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001794 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001795 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001796 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001797 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001798 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001799
1800 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801 }
1802
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001803 if (mStencilStateDirty || mFrontFaceDirty)
1804 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001805 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001806 {
1807 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1808 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1809
1810 // FIXME: Unsupported by D3D9
1811 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1812 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1813 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1814 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1815 mState.stencilRef != mState.stencilBackRef ||
1816 mState.stencilMask != mState.stencilBackMask)
1817 {
1818 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1819 return error(GL_INVALID_OPERATION);
1820 }
1821
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001822 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001823 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001824 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1825
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1827 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1828 es2dx::ConvertComparison(mState.stencilFunc));
1829
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001830 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 +00001831 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1832
1833 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1834 es2dx::ConvertStencilOp(mState.stencilFail));
1835 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1836 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1837 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1838 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1839
1840 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1841 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1842 es2dx::ConvertComparison(mState.stencilBackFunc));
1843
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001844 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 +00001845 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1846
1847 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1848 es2dx::ConvertStencilOp(mState.stencilBackFail));
1849 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1850 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1851 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1852 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1853 }
1854 else
1855 {
1856 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1857 }
1858
1859 mStencilStateDirty = false;
1860 }
1861
1862 if (mMaskStateDirty)
1863 {
1864 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1865 mState.colorMaskBlue, mState.colorMaskAlpha));
1866 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1867
1868 mMaskStateDirty = false;
1869 }
1870
1871 if (mPolygonOffsetStateDirty)
1872 {
1873 if (mState.polygonOffsetFill)
1874 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001875 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001876 if (depthbuffer)
1877 {
1878 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1879 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1880 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1881 }
1882 }
1883 else
1884 {
1885 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1886 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1887 }
1888
1889 mPolygonOffsetStateDirty = false;
1890 }
1891
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001892 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001894 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001895 {
1896 FIXME("Sample alpha to coverage is unimplemented.");
1897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001899 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001900 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001901 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1902 unsigned int mask = 0;
1903 if (mState.sampleCoverageValue != 0)
1904 {
1905 float threshold = 0.5f;
1906
1907 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1908 {
1909 mask <<= 1;
1910
1911 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1912 {
1913 threshold += 1.0f;
1914 mask |= 1;
1915 }
1916 }
1917 }
1918
1919 if (mState.sampleCoverageInvert)
1920 {
1921 mask = ~mask;
1922 }
1923
1924 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1925 }
1926 else
1927 {
1928 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001929 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001930
1931 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932 }
1933
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001934 if (mDitherStateDirty)
1935 {
1936 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1937
1938 mDitherStateDirty = false;
1939 }
1940
1941 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942}
1943
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001944// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001945void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001947 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001949 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001951 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952 }
1953 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001954}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001956GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001957{
1958 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1959
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001960 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001961 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001962 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001963 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001964 }
1965
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001966 lookupAttributeMapping(translated);
1967
1968 mBufferBackEnd->setupAttributesPreDraw(translated);
1969
1970 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1971 {
1972 if (translated[i].enabled && translated[i].nonArray)
1973 {
1974 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1975 if (err != GL_NO_ERROR)
1976 {
1977 return err;
1978 }
1979
1980 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1981
1982 *useIndexing = true;
1983 return GL_NO_ERROR;
1984 }
1985 }
1986
1987 *useIndexing = false;
1988 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001989}
1990
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001991GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001992{
1993 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1994
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001995 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001996
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001997 if (err == GL_NO_ERROR)
1998 {
1999 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002000
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002001 mBufferBackEnd->setupAttributesPreDraw(translated);
2002 }
2003
2004 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002005}
2006
2007// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002008GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002009{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002010 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002011
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002012 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002013 {
2014 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2015 }
2016
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002017 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018}
2019
2020// Applies the shaders and shader constants to the Direct3D 9 device
2021void Context::applyShaders()
2022{
2023 IDirect3DDevice9 *device = getDevice();
2024 Program *programObject = getCurrentProgram();
2025 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2026 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2027
2028 device->SetVertexShader(vertexShader);
2029 device->SetPixelShader(pixelShader);
2030
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002031 if (programObject->getSerial() != mAppliedProgram)
2032 {
2033 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002034 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002035 mAppliedProgram = programObject->getSerial();
2036 }
2037
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 programObject->applyUniforms();
2039}
2040
2041// Applies the textures and sampler states to the Direct3D 9 device
2042void Context::applyTextures()
2043{
2044 IDirect3DDevice9 *device = getDevice();
2045 Program *programObject = getCurrentProgram();
2046
2047 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2048 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002049 int textureUnit = programObject->getSamplerMapping(sampler);
2050 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002052 SamplerType textureType = programObject->getSamplerType(sampler);
2053
2054 Texture *texture = getSamplerTexture(textureUnit, textureType);
2055
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002056 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002057 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002058 if (texture->isComplete())
2059 {
2060 GLenum wrapS = texture->getWrapS();
2061 GLenum wrapT = texture->getWrapT();
2062 GLenum minFilter = texture->getMinFilter();
2063 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002065 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2066 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002068 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2069 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2070 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2071 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2072 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002074 device->SetTexture(sampler, texture->getTexture());
2075 }
2076 else
2077 {
2078 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2079 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002080 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002081
2082 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002084 else
2085 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002086 if (programObject->isSamplerDirty(sampler))
2087 {
2088 device->SetTexture(sampler, NULL);
2089 programObject->setSamplerDirty(sampler, false);
2090 }
2091 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 }
2093}
2094
2095void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2096{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002097 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002098
2099 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2100 {
2101 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2102 }
2103
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002104 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2105 {
2106 return error(GL_INVALID_OPERATION);
2107 }
2108
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002110
2111 if (!renderTarget)
2112 {
2113 return; // Context must be lost, return silently
2114 }
2115
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 IDirect3DDevice9 *device = getDevice();
2117
2118 D3DSURFACE_DESC desc;
2119 renderTarget->GetDesc(&desc);
2120
2121 IDirect3DSurface9 *systemSurface;
2122 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2123
2124 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2125 {
2126 return error(GL_OUT_OF_MEMORY);
2127 }
2128
2129 ASSERT(SUCCEEDED(result));
2130
2131 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2132 {
2133 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2134 }
2135
2136 result = device->GetRenderTargetData(renderTarget, systemSurface);
2137
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138 if (FAILED(result))
2139 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140 systemSurface->Release();
2141
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002142 switch (result)
2143 {
2144 case D3DERR_DRIVERINTERNALERROR:
2145 case D3DERR_DEVICELOST:
2146 return error(GL_OUT_OF_MEMORY);
2147 default:
2148 UNREACHABLE();
2149 return; // No sensible error to generate
2150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 }
2152
2153 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002154 RECT rect = {std::max(x, 0),
2155 std::max(y, 0),
2156 std::min(x + width, (int)desc.Width),
2157 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158
2159 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2160
2161 if (FAILED(result))
2162 {
2163 UNREACHABLE();
2164 systemSurface->Release();
2165
2166 return; // No sensible error to generate
2167 }
2168
2169 unsigned char *source = (unsigned char*)lock.pBits;
2170 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002171 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002173 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 for (int j = 0; j < rect.bottom - rect.top; j++)
2176 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002177 if (desc.Format == D3DFMT_A8R8G8B8 &&
2178 format == GL_BGRA_EXT &&
2179 type == GL_UNSIGNED_BYTE)
2180 {
2181 // Fast path for EXT_read_format_bgra, given
2182 // an RGBA source buffer. Note that buffers with no
2183 // alpha go through the slow path below.
2184 memcpy(dest + j * outputPitch,
2185 source + j * lock.Pitch,
2186 (rect.right - rect.left) * 4);
2187 continue;
2188 }
2189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 for (int i = 0; i < rect.right - rect.left; i++)
2191 {
2192 float r;
2193 float g;
2194 float b;
2195 float a;
2196
2197 switch (desc.Format)
2198 {
2199 case D3DFMT_R5G6B5:
2200 {
2201 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2202
2203 a = 1.0f;
2204 b = (rgb & 0x001F) * (1.0f / 0x001F);
2205 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2206 r = (rgb & 0xF800) * (1.0f / 0xF800);
2207 }
2208 break;
2209 case D3DFMT_X1R5G5B5:
2210 {
2211 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2212
2213 a = 1.0f;
2214 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2215 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2216 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2217 }
2218 break;
2219 case D3DFMT_A1R5G5B5:
2220 {
2221 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2222
2223 a = (argb & 0x8000) ? 1.0f : 0.0f;
2224 b = (argb & 0x001F) * (1.0f / 0x001F);
2225 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2226 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2227 }
2228 break;
2229 case D3DFMT_A8R8G8B8:
2230 {
2231 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2232
2233 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2234 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2235 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2236 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2237 }
2238 break;
2239 case D3DFMT_X8R8G8B8:
2240 {
2241 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2242
2243 a = 1.0f;
2244 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2245 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2246 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2247 }
2248 break;
2249 case D3DFMT_A2R10G10B10:
2250 {
2251 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2252
2253 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2254 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2255 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2256 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2257 }
2258 break;
2259 default:
2260 UNIMPLEMENTED(); // FIXME
2261 UNREACHABLE();
2262 }
2263
2264 switch (format)
2265 {
2266 case GL_RGBA:
2267 switch (type)
2268 {
2269 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002270 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2271 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2272 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2273 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 break;
2275 default: UNREACHABLE();
2276 }
2277 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002278 case GL_BGRA_EXT:
2279 switch (type)
2280 {
2281 case GL_UNSIGNED_BYTE:
2282 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2283 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2284 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2285 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2286 break;
2287 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2288 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2289 // this type is packed as follows:
2290 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2291 // --------------------------------------------------------------------------------
2292 // | 4th | 3rd | 2nd | 1st component |
2293 // --------------------------------------------------------------------------------
2294 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2295 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2296 ((unsigned short)(15 * a + 0.5f) << 12)|
2297 ((unsigned short)(15 * r + 0.5f) << 8) |
2298 ((unsigned short)(15 * g + 0.5f) << 4) |
2299 ((unsigned short)(15 * b + 0.5f) << 0);
2300 break;
2301 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2302 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2303 // this type is packed as follows:
2304 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2305 // --------------------------------------------------------------------------------
2306 // | 4th | 3rd | 2nd | 1st component |
2307 // --------------------------------------------------------------------------------
2308 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2309 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2310 ((unsigned short)( a + 0.5f) << 15) |
2311 ((unsigned short)(31 * r + 0.5f) << 10) |
2312 ((unsigned short)(31 * g + 0.5f) << 5) |
2313 ((unsigned short)(31 * b + 0.5f) << 0);
2314 break;
2315 default: UNREACHABLE();
2316 }
2317 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002318 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 switch (type)
2320 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002321 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002322 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2323 ((unsigned short)(31 * b + 0.5f) << 0) |
2324 ((unsigned short)(63 * g + 0.5f) << 5) |
2325 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326 break;
2327 default: UNREACHABLE();
2328 }
2329 break;
2330 default: UNREACHABLE();
2331 }
2332 }
2333 }
2334
2335 systemSurface->UnlockRect();
2336
2337 systemSurface->Release();
2338}
2339
2340void Context::clear(GLbitfield mask)
2341{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002342 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002343
2344 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2345 {
2346 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2347
2348 return;
2349 }
2350
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002351 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 IDirect3DDevice9 *device = getDevice();
2353 DWORD flags = 0;
2354
2355 if (mask & GL_COLOR_BUFFER_BIT)
2356 {
2357 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002358
2359 if (framebufferObject->getColorbufferType() != GL_NONE)
2360 {
2361 flags |= D3DCLEAR_TARGET;
2362 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 }
2364
2365 if (mask & GL_DEPTH_BUFFER_BIT)
2366 {
2367 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002368 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369 {
2370 flags |= D3DCLEAR_ZBUFFER;
2371 }
2372 }
2373
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 GLuint stencilUnmasked = 0x0;
2375
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002376 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002379 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002381 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2382 D3DSURFACE_DESC desc;
2383 depthStencil->GetDesc(&desc);
2384
2385 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2386 stencilUnmasked = (0x1 << stencilSize) - 1;
2387
2388 if (stencilUnmasked != 0x0)
2389 {
2390 flags |= D3DCLEAR_STENCIL;
2391 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392 }
2393 }
2394
2395 if (mask != 0)
2396 {
2397 return error(GL_INVALID_VALUE);
2398 }
2399
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002400 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2401 {
2402 return;
2403 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002405 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2406 unorm<8>(mState.colorClearValue.red),
2407 unorm<8>(mState.colorClearValue.green),
2408 unorm<8>(mState.colorClearValue.blue));
2409 float depth = clamp01(mState.depthClearValue);
2410 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411
2412 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2413
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002414 if (!renderTarget)
2415 {
2416 return; // Context must be lost, return silently
2417 }
2418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 D3DSURFACE_DESC desc;
2420 renderTarget->GetDesc(&desc);
2421
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002422 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
2424 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002425 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002427 !(mState.colorMaskRed && mState.colorMaskGreen &&
2428 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429
2430 if (needMaskedColorClear || needMaskedStencilClear)
2431 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002432 // State which is altered in all paths from this point to the clear call is saved.
2433 // State which is altered in only some paths will be flagged dirty in the case that
2434 // that path is taken.
2435 HRESULT hr;
2436 if (mMaskedClearSavedState == NULL)
2437 {
2438 hr = device->BeginStateBlock();
2439 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2440
2441 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2442 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2443 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2444 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2445 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2446 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2447 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2448 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2449 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2450 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2451 device->SetPixelShader(NULL);
2452 device->SetVertexShader(NULL);
2453 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2454 device->SetStreamSourceFreq(0, 1);
2455
2456 hr = device->EndStateBlock(&mMaskedClearSavedState);
2457 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2458 }
2459
2460 ASSERT(mMaskedClearSavedState != NULL);
2461
2462 if (mMaskedClearSavedState != NULL)
2463 {
2464 hr = mMaskedClearSavedState->Capture();
2465 ASSERT(SUCCEEDED(hr));
2466 }
2467
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2469 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2470 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2471 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2472 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2473 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2474 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2475 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2476
2477 if (flags & D3DCLEAR_TARGET)
2478 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002479 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2480 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2481 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2482 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 }
2484 else
2485 {
2486 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2487 }
2488
2489 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2490 {
2491 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2492 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2493 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2494 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002495 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002496 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2498 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002499 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500 }
2501 else
2502 {
2503 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2504 }
2505
2506 device->SetPixelShader(NULL);
2507 device->SetVertexShader(NULL);
2508 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002509 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002511 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 {
2513 float x, y, z, w;
2514 D3DCOLOR diffuse;
2515 };
2516
2517 Vertex quad[4];
2518 quad[0].x = 0.0f;
2519 quad[0].y = (float)desc.Height;
2520 quad[0].z = 0.0f;
2521 quad[0].w = 1.0f;
2522 quad[0].diffuse = color;
2523
2524 quad[1].x = (float)desc.Width;
2525 quad[1].y = (float)desc.Height;
2526 quad[1].z = 0.0f;
2527 quad[1].w = 1.0f;
2528 quad[1].diffuse = color;
2529
2530 quad[2].x = 0.0f;
2531 quad[2].y = 0.0f;
2532 quad[2].z = 0.0f;
2533 quad[2].w = 1.0f;
2534 quad[2].diffuse = color;
2535
2536 quad[3].x = (float)desc.Width;
2537 quad[3].y = 0.0f;
2538 quad[3].z = 0.0f;
2539 quad[3].w = 1.0f;
2540 quad[3].diffuse = color;
2541
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002542 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
2545 if (flags & D3DCLEAR_ZBUFFER)
2546 {
2547 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2548 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2549 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2550 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002551
2552 if (mMaskedClearSavedState != NULL)
2553 {
2554 mMaskedClearSavedState->Apply();
2555 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002557 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
2559 device->Clear(0, NULL, flags, color, depth, stencil);
2560 }
2561}
2562
2563void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2564{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002565 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 {
2567 return error(GL_INVALID_OPERATION);
2568 }
2569
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002570 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 IDirect3DDevice9 *device = getDevice();
2572 D3DPRIMITIVETYPE primitiveType;
2573 int primitiveCount;
2574
2575 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2576 return error(GL_INVALID_ENUM);
2577
2578 if (primitiveCount <= 0)
2579 {
2580 return;
2581 }
2582
2583 if (!applyRenderTarget(false))
2584 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002585 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586 }
2587
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002588 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002589
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002590 TranslatedIndexData indexInfo;
2591 bool useIndexing;
2592 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002593 if (err != GL_NO_ERROR)
2594 {
2595 return error(err);
2596 }
2597
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598 applyShaders();
2599 applyTextures();
2600
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002601 if (!getCurrentProgram()->validateSamplers())
2602 {
2603 return error(GL_INVALID_OPERATION);
2604 }
2605
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002606 if (!cullSkipsDraw(mode))
2607 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002608 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002609 if (useIndexing)
2610 {
2611 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2612 }
2613 else
2614 {
2615 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2616 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002618}
2619
2620void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2621{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002622 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 {
2624 return error(GL_INVALID_OPERATION);
2625 }
2626
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002627 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628 {
2629 return error(GL_INVALID_OPERATION);
2630 }
2631
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002632 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 IDirect3DDevice9 *device = getDevice();
2634 D3DPRIMITIVETYPE primitiveType;
2635 int primitiveCount;
2636
2637 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2638 return error(GL_INVALID_ENUM);
2639
2640 if (primitiveCount <= 0)
2641 {
2642 return;
2643 }
2644
2645 if (!applyRenderTarget(false))
2646 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002647 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002648 }
2649
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002650 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002651
2652 TranslatedIndexData indexInfo;
2653 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2654 if (err != GL_NO_ERROR)
2655 {
2656 return error(err);
2657 }
2658
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002659 err = applyVertexBuffer(indexInfo);
2660 if (err != GL_NO_ERROR)
2661 {
2662 return error(err);
2663 }
2664
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002665 applyShaders();
2666 applyTextures();
2667
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002668 if (!getCurrentProgram()->validateSamplers())
2669 {
2670 return error(GL_INVALID_OPERATION);
2671 }
2672
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002673 if (!cullSkipsDraw(mode))
2674 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002675 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002676 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 +00002677 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002678}
2679
2680void Context::finish()
2681{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002682 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683 IDirect3DDevice9 *device = getDevice();
2684 IDirect3DQuery9 *occlusionQuery = NULL;
2685
2686 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2687
2688 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2689 {
2690 return error(GL_OUT_OF_MEMORY);
2691 }
2692
2693 ASSERT(SUCCEEDED(result));
2694
2695 if (occlusionQuery)
2696 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002697 IDirect3DStateBlock9 *savedState = NULL;
2698 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2699
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002700 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2701 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702
2703 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002704 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 device->SetPixelShader(NULL);
2706 device->SetVertexShader(NULL);
2707 device->SetFVF(D3DFVF_XYZRHW);
2708 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002709 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002712 result = occlusionQuery->Issue(D3DISSUE_END);
2713 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714
2715 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2716 {
2717 // Keep polling, but allow other threads to do something useful first
2718 Sleep(0);
2719 }
2720
2721 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002722
2723 if (savedState)
2724 {
2725 savedState->Apply();
2726 savedState->Release();
2727 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728 }
2729}
2730
2731void Context::flush()
2732{
2733 IDirect3DDevice9 *device = getDevice();
2734 IDirect3DQuery9 *eventQuery = NULL;
2735
2736 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2737
2738 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2739 {
2740 return error(GL_OUT_OF_MEMORY);
2741 }
2742
2743 ASSERT(SUCCEEDED(result));
2744
2745 if (eventQuery)
2746 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002747 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2748 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002750 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002752
2753 if (result == D3DERR_DEVICELOST)
2754 {
2755 error(GL_OUT_OF_MEMORY);
2756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757 }
2758}
2759
2760void Context::recordInvalidEnum()
2761{
2762 mInvalidEnum = true;
2763}
2764
2765void Context::recordInvalidValue()
2766{
2767 mInvalidValue = true;
2768}
2769
2770void Context::recordInvalidOperation()
2771{
2772 mInvalidOperation = true;
2773}
2774
2775void Context::recordOutOfMemory()
2776{
2777 mOutOfMemory = true;
2778}
2779
2780void Context::recordInvalidFramebufferOperation()
2781{
2782 mInvalidFramebufferOperation = true;
2783}
2784
2785// Get one of the recorded errors and clear its flag, if any.
2786// [OpenGL ES 2.0.24] section 2.5 page 13.
2787GLenum Context::getError()
2788{
2789 if (mInvalidEnum)
2790 {
2791 mInvalidEnum = false;
2792
2793 return GL_INVALID_ENUM;
2794 }
2795
2796 if (mInvalidValue)
2797 {
2798 mInvalidValue = false;
2799
2800 return GL_INVALID_VALUE;
2801 }
2802
2803 if (mInvalidOperation)
2804 {
2805 mInvalidOperation = false;
2806
2807 return GL_INVALID_OPERATION;
2808 }
2809
2810 if (mOutOfMemory)
2811 {
2812 mOutOfMemory = false;
2813
2814 return GL_OUT_OF_MEMORY;
2815 }
2816
2817 if (mInvalidFramebufferOperation)
2818 {
2819 mInvalidFramebufferOperation = false;
2820
2821 return GL_INVALID_FRAMEBUFFER_OPERATION;
2822 }
2823
2824 return GL_NO_ERROR;
2825}
2826
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002827bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002828{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002829 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002830}
2831
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002832int Context::getMaxSupportedSamples() const
2833{
2834 return mMaxSupportedSamples;
2835}
2836
2837int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2838{
2839 if (requested == 0)
2840 {
2841 return requested;
2842 }
2843
2844 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2845 if (itr == mMultiSampleSupport.end())
2846 {
2847 return -1;
2848 }
2849
2850 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2851 {
2852 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2853 {
2854 return i;
2855 }
2856 }
2857
2858 return -1;
2859}
2860
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002861bool Context::supportsEventQueries() const
2862{
2863 return mSupportsEventQueries;
2864}
2865
daniel@transgaming.com01868132010-08-24 19:21:17 +00002866bool Context::supportsCompressedTextures() const
2867{
2868 return mSupportsCompressedTextures;
2869}
2870
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002871bool Context::supportsFloatTextures() const
2872{
2873 return mSupportsFloatTextures;
2874}
2875
2876bool Context::supportsFloatLinearFilter() const
2877{
2878 return mSupportsFloatLinearFilter;
2879}
2880
2881bool Context::supportsHalfFloatTextures() const
2882{
2883 return mSupportsHalfFloatTextures;
2884}
2885
2886bool Context::supportsHalfFloatLinearFilter() const
2887{
2888 return mSupportsHalfFloatLinearFilter;
2889}
2890
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891void Context::detachBuffer(GLuint buffer)
2892{
2893 // [OpenGL ES 2.0.24] section 2.9 page 22:
2894 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2895 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2896
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002897 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002898 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002899 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900 }
2901
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002902 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002904 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002905 }
2906
2907 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2908 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002909 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002910 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002911 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912 }
2913 }
2914}
2915
2916void Context::detachTexture(GLuint texture)
2917{
2918 // [OpenGL ES 2.0.24] section 3.8 page 84:
2919 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2920 // rebound to texture object zero
2921
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002922 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002924 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002925 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002926 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002927 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002928 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930 }
2931 }
2932
2933 // [OpenGL ES 2.0.24] section 4.4 page 112:
2934 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2935 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2936 // image was attached in the currently bound framebuffer.
2937
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002938 Framebuffer *readFramebuffer = getReadFramebuffer();
2939 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002940
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002941 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002942 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002943 readFramebuffer->detachTexture(texture);
2944 }
2945
2946 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2947 {
2948 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002949 }
2950}
2951
2952void Context::detachFramebuffer(GLuint framebuffer)
2953{
2954 // [OpenGL ES 2.0.24] section 4.4 page 107:
2955 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2956 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2957
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002958 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002959 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002960 bindReadFramebuffer(0);
2961 }
2962
2963 if (mState.drawFramebuffer == framebuffer)
2964 {
2965 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002966 }
2967}
2968
2969void Context::detachRenderbuffer(GLuint renderbuffer)
2970{
2971 // [OpenGL ES 2.0.24] section 4.4 page 109:
2972 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2973 // had been executed with the target RENDERBUFFER and name of zero.
2974
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002975 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002976 {
2977 bindRenderbuffer(0);
2978 }
2979
2980 // [OpenGL ES 2.0.24] section 4.4 page 111:
2981 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2982 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2983 // point to which this image was attached in the currently bound framebuffer.
2984
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002985 Framebuffer *readFramebuffer = getReadFramebuffer();
2986 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002987
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002988 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002990 readFramebuffer->detachRenderbuffer(renderbuffer);
2991 }
2992
2993 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2994 {
2995 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002996 }
2997}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002998
2999Texture *Context::getIncompleteTexture(SamplerType type)
3000{
3001 Texture *t = mIncompleteTextures[type];
3002
3003 if (t == NULL)
3004 {
3005 static const GLubyte color[] = { 0, 0, 0, 255 };
3006
3007 switch (type)
3008 {
3009 default:
3010 UNREACHABLE();
3011 // default falls through to SAMPLER_2D
3012
3013 case SAMPLER_2D:
3014 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003015 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003016 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003017 t = incomplete2d;
3018 }
3019 break;
3020
3021 case SAMPLER_CUBE:
3022 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003023 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003024
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003025 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3026 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3027 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3028 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3029 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3030 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003031
3032 t = incompleteCube;
3033 }
3034 break;
3035 }
3036
3037 mIncompleteTextures[type] = t;
3038 }
3039
3040 return t;
3041}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003042
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003043bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003044{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003045 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003046}
3047
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003048bool Context::isTriangleMode(GLenum drawMode)
3049{
3050 switch (drawMode)
3051 {
3052 case GL_TRIANGLES:
3053 case GL_TRIANGLE_FAN:
3054 case GL_TRIANGLE_STRIP:
3055 return true;
3056 case GL_POINTS:
3057 case GL_LINES:
3058 case GL_LINE_LOOP:
3059 case GL_LINE_STRIP:
3060 return false;
3061 default: UNREACHABLE();
3062 }
3063
3064 return false;
3065}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003066
3067void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3068{
3069 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3070
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003071 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3072 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3073 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3074 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003075
3076 mVertexDataManager->dirtyCurrentValues();
3077}
3078
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003079void Context::initExtensionString()
3080{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003081 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003082 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3083 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003084 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003085 mExtensionString += "GL_OES_rgb8_rgba8 ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003086
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003087 if (supportsEventQueries())
3088 {
3089 mExtensionString += "GL_NV_fence ";
3090 }
3091
daniel@transgaming.com01868132010-08-24 19:21:17 +00003092 if (supportsCompressedTextures())
3093 {
3094 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3095 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003096
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003097 if (supportsFloatTextures())
3098 {
3099 mExtensionString += "GL_OES_texture_float ";
3100 }
3101
3102 if (supportsHalfFloatTextures())
3103 {
3104 mExtensionString += "GL_OES_texture_half_float ";
3105 }
3106
3107 if (supportsFloatLinearFilter())
3108 {
3109 mExtensionString += "GL_OES_texture_float_linear ";
3110 }
3111
3112 if (supportsHalfFloatLinearFilter())
3113 {
3114 mExtensionString += "GL_OES_texture_half_float_linear ";
3115 }
3116
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003117 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003118 {
3119 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3120 }
3121
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003122 if (mBufferBackEnd->supportIntIndices())
3123 {
3124 mExtensionString += "GL_OES_element_index_uint ";
3125 }
3126
3127 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3128 if (end != std::string::npos)
3129 {
3130 mExtensionString.resize(end+1);
3131 }
3132}
3133
3134const char *Context::getExtensionString() const
3135{
3136 return mExtensionString.c_str();
3137}
3138
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003139void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3140 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3141 GLbitfield mask)
3142{
3143 IDirect3DDevice9 *device = getDevice();
3144
3145 Framebuffer *readFramebuffer = getReadFramebuffer();
3146 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3147
3148 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3149 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3150 {
3151 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3152 }
3153
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003154 if (drawFramebuffer->getSamples() != 0)
3155 {
3156 return error(GL_INVALID_OPERATION);
3157 }
3158
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003159 RECT sourceRect;
3160 RECT destRect;
3161
3162 if (srcX0 < srcX1)
3163 {
3164 sourceRect.left = srcX0;
3165 sourceRect.right = srcX1;
3166 destRect.left = dstX0;
3167 destRect.right = dstX1;
3168 }
3169 else
3170 {
3171 sourceRect.left = srcX1;
3172 destRect.left = dstX1;
3173 sourceRect.right = srcX0;
3174 destRect.right = dstX0;
3175 }
3176
3177 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3178 // flip our Y-values here
3179 if (srcY0 < srcY1)
3180 {
3181 sourceRect.bottom = srcY1;
3182 destRect.bottom = dstY1;
3183 sourceRect.top = srcY0;
3184 destRect.top = dstY0;
3185 }
3186 else
3187 {
3188 sourceRect.bottom = srcY0;
3189 destRect.bottom = dstY0;
3190 sourceRect.top = srcY1;
3191 destRect.top = dstY1;
3192 }
3193
3194 RECT sourceScissoredRect = sourceRect;
3195 RECT destScissoredRect = destRect;
3196
3197 if (mState.scissorTest)
3198 {
3199 // Only write to parts of the destination framebuffer which pass the scissor test
3200 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3201 // rect will be checked against scissorY, rather than the bottom.
3202 if (destRect.left < mState.scissorX)
3203 {
3204 int xDiff = mState.scissorX - destRect.left;
3205 destScissoredRect.left = mState.scissorX;
3206 sourceScissoredRect.left += xDiff;
3207 }
3208
3209 if (destRect.right > mState.scissorX + mState.scissorWidth)
3210 {
3211 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3212 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3213 sourceScissoredRect.right -= xDiff;
3214 }
3215
3216 if (destRect.top < mState.scissorY)
3217 {
3218 int yDiff = mState.scissorY - destRect.top;
3219 destScissoredRect.top = mState.scissorY;
3220 sourceScissoredRect.top += yDiff;
3221 }
3222
3223 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3224 {
3225 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3226 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3227 sourceScissoredRect.bottom -= yDiff;
3228 }
3229 }
3230
3231 bool blitRenderTarget = false;
3232 bool blitDepthStencil = false;
3233
3234 RECT sourceTrimmedRect = sourceScissoredRect;
3235 RECT destTrimmedRect = destScissoredRect;
3236
3237 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3238 // the actual draw and read surfaces.
3239 if (sourceTrimmedRect.left < 0)
3240 {
3241 int xDiff = 0 - sourceTrimmedRect.left;
3242 sourceTrimmedRect.left = 0;
3243 destTrimmedRect.left += xDiff;
3244 }
3245
3246 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3247 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3248 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3249 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3250
3251 if (sourceTrimmedRect.right > readBufferWidth)
3252 {
3253 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3254 sourceTrimmedRect.right = readBufferWidth;
3255 destTrimmedRect.right -= xDiff;
3256 }
3257
3258 if (sourceTrimmedRect.top < 0)
3259 {
3260 int yDiff = 0 - sourceTrimmedRect.top;
3261 sourceTrimmedRect.top = 0;
3262 destTrimmedRect.top += yDiff;
3263 }
3264
3265 if (sourceTrimmedRect.bottom > readBufferHeight)
3266 {
3267 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3268 sourceTrimmedRect.bottom = readBufferHeight;
3269 destTrimmedRect.bottom -= yDiff;
3270 }
3271
3272 if (destTrimmedRect.left < 0)
3273 {
3274 int xDiff = 0 - destTrimmedRect.left;
3275 destTrimmedRect.left = 0;
3276 sourceTrimmedRect.left += xDiff;
3277 }
3278
3279 if (destTrimmedRect.right > drawBufferWidth)
3280 {
3281 int xDiff = destTrimmedRect.right - drawBufferWidth;
3282 destTrimmedRect.right = drawBufferWidth;
3283 sourceTrimmedRect.right -= xDiff;
3284 }
3285
3286 if (destTrimmedRect.top < 0)
3287 {
3288 int yDiff = 0 - destTrimmedRect.top;
3289 destTrimmedRect.top = 0;
3290 sourceTrimmedRect.top += yDiff;
3291 }
3292
3293 if (destTrimmedRect.bottom > drawBufferHeight)
3294 {
3295 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3296 destTrimmedRect.bottom = drawBufferHeight;
3297 sourceTrimmedRect.bottom -= yDiff;
3298 }
3299
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003300 bool partialBufferCopy = false;
3301 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3302 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3303 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3304 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3305 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3306 {
3307 partialBufferCopy = true;
3308 }
3309
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003310 if (mask & GL_COLOR_BUFFER_BIT)
3311 {
3312 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3313 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3314 {
3315 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3316 return error(GL_INVALID_OPERATION);
3317 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003318
3319 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3320 {
3321 return error(GL_INVALID_OPERATION);
3322 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003323
3324 blitRenderTarget = true;
3325
3326 }
3327
3328 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3329 {
3330 DepthStencilbuffer *readDSBuffer = NULL;
3331 DepthStencilbuffer *drawDSBuffer = NULL;
3332
3333 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3334 // both a depth and stencil buffer, it will be the same buffer.
3335
3336 if (mask & GL_DEPTH_BUFFER_BIT)
3337 {
3338 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3339 {
3340 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3341 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3342 {
3343 return error(GL_INVALID_OPERATION);
3344 }
3345
3346 blitDepthStencil = true;
3347 readDSBuffer = readFramebuffer->getDepthbuffer();
3348 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3349 }
3350 }
3351
3352 if (mask & GL_STENCIL_BUFFER_BIT)
3353 {
3354 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3355 {
3356 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3357 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3358 {
3359 return error(GL_INVALID_OPERATION);
3360 }
3361
3362 blitDepthStencil = true;
3363 readDSBuffer = readFramebuffer->getStencilbuffer();
3364 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3365 }
3366 }
3367
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003368 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003369 {
3370 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3371 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3372 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003373
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003374 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3375 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003376 {
3377 return error(GL_INVALID_OPERATION);
3378 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003379 }
3380
3381 if (blitRenderTarget || blitDepthStencil)
3382 {
3383 egl::Display *display = getDisplay();
3384 display->endScene();
3385
3386 if (blitRenderTarget)
3387 {
3388 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3389 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3390
3391 if (FAILED(result))
3392 {
3393 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3394 return;
3395 }
3396 }
3397
3398 if (blitDepthStencil)
3399 {
3400 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3401
3402 if (FAILED(result))
3403 {
3404 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3405 return;
3406 }
3407 }
3408 }
3409}
3410
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003411}
3412
3413extern "C"
3414{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003415gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003416{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003417 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003418}
3419
3420void glDestroyContext(gl::Context *context)
3421{
3422 delete context;
3423
3424 if (context == gl::getContext())
3425 {
3426 gl::makeCurrent(NULL, NULL, NULL);
3427 }
3428}
3429
3430void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3431{
3432 gl::makeCurrent(context, display, surface);
3433}
3434
3435gl::Context *glGetCurrentContext()
3436{
3437 return gl::getContext();
3438}
3439}