blob: dc1e0a46cc646561635d87b2ca26afeab76fa6a7 [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.comfab5a1a2010-03-11 19:22:30 +000010#include "Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014#include "main.h"
15#include "Display.h"
16#include "Buffer.h"
17#include "Shader.h"
18#include "Program.h"
19#include "Texture.h"
20#include "FrameBuffer.h"
21#include "RenderBuffer.h"
22#include "mathutil.h"
23#include "utilities.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000024#include "geometry/backend.h"
25#include "geometry/VertexDataManager.h"
26#include "geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027
daniel@transgaming.com86487c22010-03-11 19:41:43 +000028#undef near
29#undef far
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace gl
32{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000033Context::Context(const egl::Config *config)
34 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035{
36 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37 depthClearValue = 1.0f;
38 stencilClearValue = 0;
39
40 cullFace = false;
41 cullMode = GL_BACK;
42 frontFace = GL_CCW;
43 depthTest = false;
44 depthFunc = GL_LESS;
45 blend = false;
46 sourceBlendRGB = GL_ONE;
47 sourceBlendAlpha = GL_ONE;
48 destBlendRGB = GL_ZERO;
49 destBlendAlpha = GL_ZERO;
50 blendEquationRGB = GL_FUNC_ADD;
51 blendEquationAlpha = GL_FUNC_ADD;
52 blendColor.red = 0;
53 blendColor.green = 0;
54 blendColor.blue = 0;
55 blendColor.alpha = 0;
56 stencilTest = false;
57 stencilFunc = GL_ALWAYS;
58 stencilRef = 0;
59 stencilMask = -1;
60 stencilWritemask = -1;
61 stencilBackFunc = GL_ALWAYS;
62 stencilBackRef = 0;
63 stencilBackMask = - 1;
64 stencilBackWritemask = -1;
65 stencilFail = GL_KEEP;
66 stencilPassDepthFail = GL_KEEP;
67 stencilPassDepthPass = GL_KEEP;
68 stencilBackFail = GL_KEEP;
69 stencilBackPassDepthFail = GL_KEEP;
70 stencilBackPassDepthPass = GL_KEEP;
71 polygonOffsetFill = false;
72 sampleAlphaToCoverage = false;
73 sampleCoverage = false;
74 sampleCoverageValue = 1.0f;
75 sampleCoverageInvert = GL_FALSE;
76 scissorTest = false;
77 dither = true;
78
79 viewportX = 0;
80 viewportY = 0;
81 viewportWidth = config->mDisplayMode.Width;
82 viewportHeight = config->mDisplayMode.Height;
83 zNear = 0.0f;
84 zFar = 1.0f;
85
86 scissorX = 0;
87 scissorY = 0;
88 scissorWidth = config->mDisplayMode.Width;
89 scissorHeight = config->mDisplayMode.Height;
90
91 colorMaskRed = true;
92 colorMaskGreen = true;
93 colorMaskBlue = true;
94 colorMaskAlpha = true;
95 depthMask = true;
96
97 // [OpenGL ES 2.0.24] section 3.7 page 83:
98 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
99 // and cube map texture state vectors respectively associated with them.
100 // In order that access to these initial textures not be lost, they are treated as texture
101 // objects all of whose names are 0.
102
103 mTexture2DZero = new Texture2D();
104 mTextureCubeMapZero = new TextureCubeMap();
105
106 mColorbufferZero = NULL;
107 mDepthbufferZero = NULL;
108 mStencilbufferZero = NULL;
109
110 activeSampler = 0;
111 arrayBuffer = 0;
112 elementArrayBuffer = 0;
113 bindTextureCubeMap(0);
114 bindTexture2D(0);
115 bindFramebuffer(0);
116 bindRenderbuffer(0);
117
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000118 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000120 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
121 {
122 samplerTexture[type][sampler] = 0;
123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 }
125
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000126 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
127 {
128 mIncompleteTextures[type] = NULL;
129 }
130
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 currentProgram = 0;
132
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000133 mBufferBackEnd = NULL;
134 mVertexDataManager = NULL;
135
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 mInvalidEnum = false;
137 mInvalidValue = false;
138 mInvalidOperation = false;
139 mOutOfMemory = false;
140 mInvalidFramebufferOperation = false;
141}
142
143Context::~Context()
144{
145 currentProgram = 0;
146
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000147 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
148 {
149 delete mIncompleteTextures[type];
150 }
151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152 delete mTexture2DZero;
153 delete mTextureCubeMapZero;
154
155 delete mColorbufferZero;
156 delete mDepthbufferZero;
157 delete mStencilbufferZero;
158
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000159 delete mBufferBackEnd;
160 delete mVertexDataManager;
161
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162 while (!mBufferMap.empty())
163 {
164 deleteBuffer(mBufferMap.begin()->first);
165 }
166
167 while (!mProgramMap.empty())
168 {
169 deleteProgram(mProgramMap.begin()->first);
170 }
171
172 while (!mShaderMap.empty())
173 {
174 deleteShader(mShaderMap.begin()->first);
175 }
176
177 while (!mFramebufferMap.empty())
178 {
179 deleteFramebuffer(mFramebufferMap.begin()->first);
180 }
181
182 while (!mRenderbufferMap.empty())
183 {
184 deleteRenderbuffer(mRenderbufferMap.begin()->first);
185 }
186
187 while (!mTextureMap.empty())
188 {
189 deleteTexture(mTextureMap.begin()->first);
190 }
191}
192
193void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
194{
195 IDirect3DDevice9 *device = display->getDevice();
196
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000197 if (!mBufferBackEnd)
198 {
199 mBufferBackEnd = new Dx9BackEnd(device);
200 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
201 }
202
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
204 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000205 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206
207 Framebuffer *framebufferZero = new Framebuffer();
208 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000209 Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil);
210 Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211
212 setFramebufferZero(framebufferZero);
213 setColorbufferZero(colorbufferZero);
214 setDepthbufferZero(depthbufferZero);
215 setStencilbufferZero(stencilbufferZero);
216
217 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
218 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
219 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
220
221 defaultRenderTarget->Release();
222
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000223 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000225 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226 }
227}
228
229void Context::setClearColor(float red, float green, float blue, float alpha)
230{
231 colorClearValue.red = red;
232 colorClearValue.green = green;
233 colorClearValue.blue = blue;
234 colorClearValue.alpha = alpha;
235}
236
237void Context::setClearDepth(float depth)
238{
239 depthClearValue = depth;
240}
241
242void Context::setClearStencil(int stencil)
243{
244 stencilClearValue = stencil;
245}
246
247// Returns an unused buffer name
248GLuint Context::createBuffer()
249{
250 unsigned int handle = 1;
251
252 while (mBufferMap.find(handle) != mBufferMap.end())
253 {
254 handle++;
255 }
256
257 mBufferMap[handle] = NULL;
258
259 return handle;
260}
261
262// Returns an unused shader/program name
263GLuint Context::createShader(GLenum type)
264{
265 unsigned int handle = 1;
266
267 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
268 {
269 handle++;
270 }
271
272 if (type == GL_VERTEX_SHADER)
273 {
274 mShaderMap[handle] = new VertexShader();
275 }
276 else if (type == GL_FRAGMENT_SHADER)
277 {
278 mShaderMap[handle] = new FragmentShader();
279 }
280 else UNREACHABLE();
281
282 return handle;
283}
284
285// Returns an unused program/shader name
286GLuint Context::createProgram()
287{
288 unsigned int handle = 1;
289
290 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
291 {
292 handle++;
293 }
294
295 mProgramMap[handle] = new Program();
296
297 return handle;
298}
299
300// Returns an unused texture name
301GLuint Context::createTexture()
302{
303 unsigned int handle = 1;
304
305 while (mTextureMap.find(handle) != mTextureMap.end())
306 {
307 handle++;
308 }
309
310 mTextureMap[handle] = NULL;
311
312 return handle;
313}
314
315// Returns an unused framebuffer name
316GLuint Context::createFramebuffer()
317{
318 unsigned int handle = 1;
319
320 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
321 {
322 handle++;
323 }
324
325 mFramebufferMap[handle] = NULL;
326
327 return handle;
328}
329
330// Returns an unused renderbuffer name
331GLuint Context::createRenderbuffer()
332{
333 unsigned int handle = 1;
334
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000335 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 {
337 handle++;
338 }
339
340 mRenderbufferMap[handle] = NULL;
341
342 return handle;
343}
344
345void Context::deleteBuffer(GLuint buffer)
346{
347 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
348
349 if (bufferObject != mBufferMap.end())
350 {
351 detachBuffer(buffer);
352
353 delete bufferObject->second;
354 mBufferMap.erase(bufferObject);
355 }
356}
357
358void Context::deleteShader(GLuint shader)
359{
360 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
361
362 if (shaderObject != mShaderMap.end())
363 {
364 if (!shaderObject->second->isAttached())
365 {
366 delete shaderObject->second;
367 mShaderMap.erase(shaderObject);
368 }
369 else
370 {
371 shaderObject->second->flagForDeletion();
372 }
373 }
374}
375
376void Context::deleteProgram(GLuint program)
377{
378 ProgramMap::iterator programObject = mProgramMap.find(program);
379
380 if (programObject != mProgramMap.end())
381 {
382 if (program != currentProgram)
383 {
384 delete programObject->second;
385 mProgramMap.erase(programObject);
386 }
387 else
388 {
389 programObject->second->flagForDeletion();
390 }
391 }
392}
393
394void Context::deleteTexture(GLuint texture)
395{
396 TextureMap::iterator textureObject = mTextureMap.find(texture);
397
398 if (textureObject != mTextureMap.end())
399 {
400 detachTexture(texture);
401
402 if (texture != 0)
403 {
404 delete textureObject->second;
405 }
406
407 mTextureMap.erase(textureObject);
408 }
409}
410
411void Context::deleteFramebuffer(GLuint framebuffer)
412{
413 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
414
415 if (framebufferObject != mFramebufferMap.end())
416 {
417 detachFramebuffer(framebuffer);
418
419 delete framebufferObject->second;
420 mFramebufferMap.erase(framebufferObject);
421 }
422}
423
424void Context::deleteRenderbuffer(GLuint renderbuffer)
425{
426 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
427
428 if (renderbufferObject != mRenderbufferMap.end())
429 {
430 detachRenderbuffer(renderbuffer);
431
432 delete renderbufferObject->second;
433 mRenderbufferMap.erase(renderbufferObject);
434 }
435}
436
437void Context::bindArrayBuffer(unsigned int buffer)
438{
439 if (buffer != 0 && !getBuffer(buffer))
440 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000441 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442 }
443
444 arrayBuffer = buffer;
445}
446
447void Context::bindElementArrayBuffer(unsigned int buffer)
448{
449 if (buffer != 0 && !getBuffer(buffer))
450 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000451 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452 }
453
454 elementArrayBuffer = buffer;
455}
456
457void Context::bindTexture2D(GLuint texture)
458{
459 if (!getTexture(texture) || texture == 0)
460 {
461 if (texture != 0)
462 {
463 mTextureMap[texture] = new Texture2D();
464 }
465 else // Special case: 0 refers to different initial textures based on the target
466 {
467 mTextureMap[0] = mTexture2DZero;
468 }
469 }
470
471 texture2D = texture;
472
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000473 samplerTexture[SAMPLER_2D][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474}
475
476void Context::bindTextureCubeMap(GLuint texture)
477{
478 if (!getTexture(texture) || texture == 0)
479 {
480 if (texture != 0)
481 {
482 mTextureMap[texture] = new TextureCubeMap();
483 }
484 else // Special case: 0 refers to different initial textures based on the target
485 {
486 mTextureMap[0] = mTextureCubeMapZero;
487 }
488 }
489
490 textureCubeMap = texture;
491
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000492 samplerTexture[SAMPLER_CUBE][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493}
494
495void Context::bindFramebuffer(GLuint framebuffer)
496{
497 if (!getFramebuffer(framebuffer))
498 {
499 mFramebufferMap[framebuffer] = new Framebuffer();
500 }
501
502 this->framebuffer = framebuffer;
503}
504
505void Context::bindRenderbuffer(GLuint renderbuffer)
506{
507 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
508 {
509 mRenderbufferMap[renderbuffer] = new Renderbuffer();
510 }
511
512 this->renderbuffer = renderbuffer;
513}
514
515void Context::useProgram(GLuint program)
516{
517 Program *programObject = getCurrentProgram();
518
519 if (programObject && programObject->isFlaggedForDeletion())
520 {
521 deleteProgram(currentProgram);
522 }
523
524 currentProgram = program;
525}
526
527void Context::setFramebufferZero(Framebuffer *buffer)
528{
529 delete mFramebufferMap[0];
530 mFramebufferMap[0] = buffer;
531}
532
533void Context::setColorbufferZero(Colorbuffer *buffer)
534{
535 delete mColorbufferZero;
536 mColorbufferZero = buffer;
537}
538
539void Context::setDepthbufferZero(Depthbuffer *buffer)
540{
541 delete mDepthbufferZero;
542 mDepthbufferZero = buffer;
543}
544
545void Context::setStencilbufferZero(Stencilbuffer *buffer)
546{
547 delete mStencilbufferZero;
548 mStencilbufferZero = buffer;
549}
550
551void Context::setRenderbuffer(Renderbuffer *buffer)
552{
553 delete mRenderbufferMap[renderbuffer];
554 mRenderbufferMap[renderbuffer] = buffer;
555}
556
557Buffer *Context::getBuffer(unsigned int handle)
558{
559 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000560
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561 if (buffer == mBufferMap.end())
562 {
563 return NULL;
564 }
565 else
566 {
567 return buffer->second;
568 }
569}
570
571Shader *Context::getShader(unsigned int handle)
572{
573 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575 if (shader == mShaderMap.end())
576 {
577 return NULL;
578 }
579 else
580 {
581 return shader->second;
582 }
583}
584
585Program *Context::getProgram(unsigned int handle)
586{
587 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000588
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589 if (program == mProgramMap.end())
590 {
591 return NULL;
592 }
593 else
594 {
595 return program->second;
596 }
597}
598
599Texture *Context::getTexture(unsigned int handle)
600{
601 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000602
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603 if (texture == mTextureMap.end())
604 {
605 return NULL;
606 }
607 else
608 {
609 return texture->second;
610 }
611}
612
613Framebuffer *Context::getFramebuffer(unsigned int handle)
614{
615 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000616
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617 if (framebuffer == mFramebufferMap.end())
618 {
619 return NULL;
620 }
621 else
622 {
623 return framebuffer->second;
624 }
625}
626
627Renderbuffer *Context::getRenderbuffer(unsigned int handle)
628{
629 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000630
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631 if (renderbuffer == mRenderbufferMap.end())
632 {
633 return NULL;
634 }
635 else
636 {
637 return renderbuffer->second;
638 }
639}
640
641Colorbuffer *Context::getColorbuffer(GLuint handle)
642{
643 if (handle != 0)
644 {
645 Renderbuffer *renderbuffer = getRenderbuffer(handle);
646
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000647 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648 {
649 return static_cast<Colorbuffer*>(renderbuffer);
650 }
651 }
652 else // Special case: 0 refers to different initial render targets based on the attachment type
653 {
654 return mColorbufferZero;
655 }
656
657 return NULL;
658}
659
660Depthbuffer *Context::getDepthbuffer(GLuint handle)
661{
662 if (handle != 0)
663 {
664 Renderbuffer *renderbuffer = getRenderbuffer(handle);
665
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000666 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667 {
668 return static_cast<Depthbuffer*>(renderbuffer);
669 }
670 }
671 else // Special case: 0 refers to different initial render targets based on the attachment type
672 {
673 return mDepthbufferZero;
674 }
675
676 return NULL;
677}
678
679Stencilbuffer *Context::getStencilbuffer(GLuint handle)
680{
681 if (handle != 0)
682 {
683 Renderbuffer *renderbuffer = getRenderbuffer(handle);
684
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000685 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686 {
687 return static_cast<Stencilbuffer*>(renderbuffer);
688 }
689 }
690 else
691 {
692 return mStencilbufferZero;
693 }
694
695 return NULL;
696}
697
698Buffer *Context::getArrayBuffer()
699{
700 return getBuffer(arrayBuffer);
701}
702
703Buffer *Context::getElementArrayBuffer()
704{
705 return getBuffer(elementArrayBuffer);
706}
707
708Program *Context::getCurrentProgram()
709{
710 return getProgram(currentProgram);
711}
712
713Texture2D *Context::getTexture2D()
714{
715 if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target
716 {
717 return mTexture2DZero;
718 }
719
720 return (Texture2D*)getTexture(texture2D);
721}
722
723TextureCubeMap *Context::getTextureCubeMap()
724{
725 if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
726 {
727 return mTextureCubeMapZero;
728 }
729
730 return (TextureCubeMap*)getTexture(textureCubeMap);
731}
732
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000733Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000735 return getTexture(samplerTexture[type][sampler]);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736}
737
738Framebuffer *Context::getFramebuffer()
739{
740 return getFramebuffer(framebuffer);
741}
742
743// Applies the render target surface, depth stencil surface, viewport rectangle and
744// scissor rectangle to the Direct3D 9 device
745bool Context::applyRenderTarget(bool ignoreViewport)
746{
747 IDirect3DDevice9 *device = getDevice();
748 Framebuffer *framebufferObject = getFramebuffer();
749
750 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
751 {
752 return false;
753 }
754
755 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
756 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
757
758 device->SetRenderTarget(0, renderTarget);
759 device->SetDepthStencilSurface(depthStencil);
760
761 D3DVIEWPORT9 viewport;
762 D3DSURFACE_DESC desc;
763 renderTarget->GetDesc(&desc);
764
765 if (ignoreViewport)
766 {
767 viewport.X = 0;
768 viewport.Y = 0;
769 viewport.Width = desc.Width;
770 viewport.Height = desc.Height;
771 viewport.MinZ = 0.0f;
772 viewport.MaxZ = 1.0f;
773 }
774 else
775 {
daniel@transgaming.com16973022010-03-11 19:22:19 +0000776 viewport.X = std::max(viewportX, 0);
777 viewport.Y = std::max(viewportY, 0);
778 viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X);
779 viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780 viewport.MinZ = clamp01(zNear);
781 viewport.MaxZ = clamp01(zFar);
782 }
783
784 device->SetViewport(&viewport);
785
786 if (scissorTest)
787 {
788 RECT rect = {scissorX,
789 scissorY,
790 scissorX + scissorWidth,
791 scissorY + scissorHeight};
792
793 device->SetScissorRect(&rect);
794 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
795 }
796 else
797 {
798 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
799 }
800
801 if (currentProgram)
802 {
803 D3DSURFACE_DESC description;
804 renderTarget->GetDesc(&description);
805 Program *programObject = getCurrentProgram();
806
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000807 GLint halfPixelSize = programObject->getUniformLocation("gl_HalfPixelSize");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808 GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height};
809 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000810
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000811 GLint window = programObject->getUniformLocation("gl_Window");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000812 GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f};
813 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
814
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000815 GLint depth = programObject->getUniformLocation("gl_Depth");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000816 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
817 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
818
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000819 GLint near = programObject->getUniformLocation("gl_DepthRange.near");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000820 programObject->setUniform1fv(near, 1, &zNear);
821
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000822 GLint far = programObject->getUniformLocation("gl_DepthRange.far");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000823 programObject->setUniform1fv(far, 1, &zFar);
824
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000825 GLint diff = programObject->getUniformLocation("gl_DepthRange.diff");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000826 GLfloat zDiff = zFar - zNear;
827 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828 }
829
830 return true;
831}
832
833// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
834void Context::applyState()
835{
836 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000837 Program *programObject = getCurrentProgram();
838
839 GLint frontCCW = programObject->getUniformLocation("__frontCCW");
840 GLint ccw = (frontFace == GL_CCW);
841 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842
843 if (cullFace)
844 {
845 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace));
846 }
847 else
848 {
849 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
850 }
851
852 if (depthTest)
853 {
854 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
855 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc));
856 }
857 else
858 {
859 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
860 }
861
862 if (blend)
863 {
864 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
865
866 if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
867 destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
868 {
869 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor));
870 }
871 else
872 {
873 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha),
874 unorm<8>(blendColor.alpha),
875 unorm<8>(blendColor.alpha),
876 unorm<8>(blendColor.alpha)));
877 }
878
879 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB));
880 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB));
881 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB));
882
883 if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha)
884 {
885 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
886
887 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha));
888 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha));
889 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha));
890
891 }
892 else
893 {
894 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
895 }
896 }
897 else
898 {
899 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
900 }
901
902 if (stencilTest)
903 {
904 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
905 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
906
907 // FIXME: Unsupported by D3D9
908 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
909 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
910 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
daniel@transgaming.com1436e262010-03-17 03:58:56 +0000911 if(stencilWritemask != stencilBackWritemask || stencilRef != stencilBackRef || stencilMask != stencilBackMask)
912 {
913 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
914 return error(GL_INVALID_OPERATION);
915 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
917 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask);
918 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc));
919
920 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range
921 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask);
922
923 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail));
924 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail));
925 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass));
926
927 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask);
928 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc));
929
930 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range
931 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask);
932
933 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail));
934 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail));
935 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass));
936 }
937 else
938 {
939 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
940 }
941
942 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha));
943 device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE);
944
945 if (polygonOffsetFill)
946 {
947 UNIMPLEMENTED(); // FIXME
948 }
949
950 if (sampleAlphaToCoverage)
951 {
952 UNIMPLEMENTED(); // FIXME
953 }
954
955 if (sampleCoverage)
956 {
957 UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one
958 }
959
960 device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE);
961}
962
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000963// Fill in the programAttribute field of the array of TranslatedAttributes based on the active GLSL program.
964void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000966 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000968 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000970 attributes[i].programAttribute = getCurrentProgram()->getInputMapping(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971 }
972 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000973}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000975// The indices parameter to glDrawElements can have two interpretations:
976// - as a pointer into client memory
977// - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer
978// Handle these cases here and return a pointer to the index data.
979const Index *Context::adjustIndexPointer(const void *indices)
980{
981 if (elementArrayBuffer)
982 {
983 Buffer *buffer = getBuffer(elementArrayBuffer);
984 return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices));
985 }
986 else
987 {
988 return static_cast<const Index*>(indices);
989 }
990}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000992void Context::applyVertexBuffer(GLint first, GLsizei count)
993{
994 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
995
996 mVertexDataManager->preRenderValidate(first, count, translated);
997
998 lookupAttributeMapping(translated);
999
1000 mBufferBackEnd->preDraw(translated);
1001}
1002
1003void Context::applyVertexBuffer(GLsizei count, const void *indices, GLenum indexType)
1004{
1005 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1006
1007 mVertexDataManager->preRenderValidate(adjustIndexPointer(indices), count, translated);
1008
1009 lookupAttributeMapping(translated);
1010
1011 mBufferBackEnd->preDraw(translated);
1012}
1013
1014// Applies the indices and element array bindings to the Direct3D 9 device
1015void Context::applyIndexBuffer(const void *indices, GLsizei count)
1016{
1017 GLsizei length = count * sizeof(Index);
1018
1019 IDirect3DDevice9 *device = getDevice();
1020
1021 IDirect3DIndexBuffer9 *indexBuffer = NULL;
1022 void *data;
1023
1024 HRESULT result = device->CreateIndexBuffer(length, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &indexBuffer, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025
1026 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1027 {
1028 return error(GL_OUT_OF_MEMORY);
1029 }
1030
1031 ASSERT(SUCCEEDED(result));
1032
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001033 if (indexBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001035 indexBuffer->Lock(0, length, &data, 0);
1036 memcpy(data, adjustIndexPointer(indices), length);
1037 indexBuffer->Unlock();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038
1039 device->SetIndices(indexBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001040 indexBuffer->Release(); // Will only effectively be deleted when no longer in use
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001043 startIndex = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044}
1045
1046// Applies the shaders and shader constants to the Direct3D 9 device
1047void Context::applyShaders()
1048{
1049 IDirect3DDevice9 *device = getDevice();
1050 Program *programObject = getCurrentProgram();
1051 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1052 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1053
1054 device->SetVertexShader(vertexShader);
1055 device->SetPixelShader(pixelShader);
1056
1057 programObject->applyUniforms();
1058}
1059
1060// Applies the textures and sampler states to the Direct3D 9 device
1061void Context::applyTextures()
1062{
1063 IDirect3DDevice9 *device = getDevice();
1064 Program *programObject = getCurrentProgram();
1065
1066 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1067 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001068 int textureUnit = programObject->getSamplerMapping(sampler);
1069 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001071 SamplerType textureType = programObject->getSamplerType(sampler);
1072
1073 Texture *texture = getSamplerTexture(textureUnit, textureType);
1074
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001075 if (texture->isComplete())
1076 {
1077 GLenum wrapS = texture->getWrapS();
1078 GLenum wrapT = texture->getWrapT();
1079 GLenum minFilter = texture->getMinFilter();
1080 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001082 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1083 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001085 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1086 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1087 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1088 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1089 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001091 device->SetTexture(sampler, texture->getTexture());
1092 }
1093 else
1094 {
1095 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1096 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001098 else
1099 {
1100 device->SetTexture(sampler, NULL);
1101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102 }
1103}
1104
1105void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1106{
1107 Framebuffer *framebuffer = getFramebuffer();
1108 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1109 IDirect3DDevice9 *device = getDevice();
1110
1111 D3DSURFACE_DESC desc;
1112 renderTarget->GetDesc(&desc);
1113
1114 IDirect3DSurface9 *systemSurface;
1115 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1116
1117 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1118 {
1119 return error(GL_OUT_OF_MEMORY);
1120 }
1121
1122 ASSERT(SUCCEEDED(result));
1123
1124 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1125 {
1126 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1127 }
1128
1129 result = device->GetRenderTargetData(renderTarget, systemSurface);
1130
1131 if (result == D3DERR_DRIVERINTERNALERROR)
1132 {
1133 systemSurface->Release();
1134
1135 return error(GL_OUT_OF_MEMORY);
1136 }
1137
1138 if (FAILED(result))
1139 {
1140 UNREACHABLE();
1141 systemSurface->Release();
1142
1143 return; // No sensible error to generate
1144 }
1145
1146 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001147 RECT rect = {std::max(x, 0),
1148 std::max(y, 0),
1149 std::min(x + width, (int)desc.Width),
1150 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151
1152 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1153
1154 if (FAILED(result))
1155 {
1156 UNREACHABLE();
1157 systemSurface->Release();
1158
1159 return; // No sensible error to generate
1160 }
1161
1162 unsigned char *source = (unsigned char*)lock.pBits;
1163 unsigned char *dest = (unsigned char*)pixels;
1164
1165 for (int j = 0; j < rect.bottom - rect.top; j++)
1166 {
1167 for (int i = 0; i < rect.right - rect.left; i++)
1168 {
1169 float r;
1170 float g;
1171 float b;
1172 float a;
1173
1174 switch (desc.Format)
1175 {
1176 case D3DFMT_R5G6B5:
1177 {
1178 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1179
1180 a = 1.0f;
1181 b = (rgb & 0x001F) * (1.0f / 0x001F);
1182 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1183 r = (rgb & 0xF800) * (1.0f / 0xF800);
1184 }
1185 break;
1186 case D3DFMT_X1R5G5B5:
1187 {
1188 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1189
1190 a = 1.0f;
1191 b = (xrgb & 0x001F) * (1.0f / 0x001F);
1192 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
1193 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
1194 }
1195 break;
1196 case D3DFMT_A1R5G5B5:
1197 {
1198 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1199
1200 a = (argb & 0x8000) ? 1.0f : 0.0f;
1201 b = (argb & 0x001F) * (1.0f / 0x001F);
1202 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1203 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1204 }
1205 break;
1206 case D3DFMT_A8R8G8B8:
1207 {
1208 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1209
1210 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1211 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1212 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1213 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1214 }
1215 break;
1216 case D3DFMT_X8R8G8B8:
1217 {
1218 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1219
1220 a = 1.0f;
1221 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1222 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1223 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1224 }
1225 break;
1226 case D3DFMT_A2R10G10B10:
1227 {
1228 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1229
1230 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1231 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1232 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1233 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1234 }
1235 break;
1236 default:
1237 UNIMPLEMENTED(); // FIXME
1238 UNREACHABLE();
1239 }
1240
1241 switch (format)
1242 {
1243 case GL_RGBA:
1244 switch (type)
1245 {
1246 case GL_UNSIGNED_BYTE:
1247 dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f);
1248 dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f);
1249 dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f);
1250 dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f);
1251 break;
1252 default: UNREACHABLE();
1253 }
1254 break;
1255 case IMPLEMENTATION_COLOR_READ_FORMAT:
1256 switch (type)
1257 {
1258 case IMPLEMENTATION_COLOR_READ_TYPE:
1259 break;
1260 default: UNREACHABLE();
1261 }
1262 break;
1263 default: UNREACHABLE();
1264 }
1265 }
1266 }
1267
1268 systemSurface->UnlockRect();
1269
1270 systemSurface->Release();
1271}
1272
1273void Context::clear(GLbitfield mask)
1274{
1275 IDirect3DDevice9 *device = getDevice();
1276 DWORD flags = 0;
1277
1278 if (mask & GL_COLOR_BUFFER_BIT)
1279 {
1280 mask &= ~GL_COLOR_BUFFER_BIT;
1281 flags |= D3DCLEAR_TARGET;
1282 }
1283
1284 if (mask & GL_DEPTH_BUFFER_BIT)
1285 {
1286 mask &= ~GL_DEPTH_BUFFER_BIT;
1287 if (depthMask)
1288 {
1289 flags |= D3DCLEAR_ZBUFFER;
1290 }
1291 }
1292
1293 Framebuffer *framebufferObject = getFramebuffer();
1294 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1295
1296 GLuint stencilUnmasked = 0x0;
1297
1298 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
1299 {
1300 D3DSURFACE_DESC desc;
1301 depthStencil->GetDesc(&desc);
1302
1303 mask &= ~GL_STENCIL_BUFFER_BIT;
1304 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
1305 stencilUnmasked = (0x1 << stencilSize) - 1;
1306
1307 if (stencilUnmasked != 0x0)
1308 {
1309 flags |= D3DCLEAR_STENCIL;
1310 }
1311 }
1312
1313 if (mask != 0)
1314 {
1315 return error(GL_INVALID_VALUE);
1316 }
1317
1318 applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport
1319
1320 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue));
1321 float depth = clamp01(depthClearValue);
1322 int stencil = stencilClearValue & 0x000000FF;
1323
1324 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1325
1326 D3DSURFACE_DESC desc;
1327 renderTarget->GetDesc(&desc);
1328
1329 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha;
1330
1331 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
1332 (stencilWritemask & stencilUnmasked) != stencilUnmasked;
1333 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001334 !(colorMaskRed && colorMaskGreen &&
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001335 colorMaskBlue && alphaUnmasked);
1336
1337 if (needMaskedColorClear || needMaskedStencilClear)
1338 {
1339 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1340 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1341 device->SetRenderState(D3DRS_ZENABLE, FALSE);
1342 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1343 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1344 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1345 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1346 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1347
1348 if (flags & D3DCLEAR_TARGET)
1349 {
1350 device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
1351 (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
1352 (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
1353 (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
1354 }
1355 else
1356 {
1357 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1358 }
1359
1360 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
1361 {
1362 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1363 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1364 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1365 device->SetRenderState(D3DRS_STENCILREF, stencil);
1366 device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001367 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001368 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1369 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1370 }
1371 else
1372 {
1373 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1374 }
1375
1376 device->SetPixelShader(NULL);
1377 device->SetVertexShader(NULL);
1378 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1379
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001380 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381 {
1382 float x, y, z, w;
1383 D3DCOLOR diffuse;
1384 };
1385
1386 Vertex quad[4];
1387 quad[0].x = 0.0f;
1388 quad[0].y = (float)desc.Height;
1389 quad[0].z = 0.0f;
1390 quad[0].w = 1.0f;
1391 quad[0].diffuse = color;
1392
1393 quad[1].x = (float)desc.Width;
1394 quad[1].y = (float)desc.Height;
1395 quad[1].z = 0.0f;
1396 quad[1].w = 1.0f;
1397 quad[1].diffuse = color;
1398
1399 quad[2].x = 0.0f;
1400 quad[2].y = 0.0f;
1401 quad[2].z = 0.0f;
1402 quad[2].w = 1.0f;
1403 quad[2].diffuse = color;
1404
1405 quad[3].x = (float)desc.Width;
1406 quad[3].y = 0.0f;
1407 quad[3].z = 0.0f;
1408 quad[3].w = 1.0f;
1409 quad[3].diffuse = color;
1410
1411 device->BeginScene();
1412 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
1413 device->EndScene();
1414
1415 if (flags & D3DCLEAR_ZBUFFER)
1416 {
1417 device->SetRenderState(D3DRS_ZENABLE, TRUE);
1418 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1419 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1420 }
1421 }
1422 else
1423 {
1424 device->Clear(0, NULL, flags, color, depth, stencil);
1425 }
1426}
1427
1428void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
1429{
1430 if (!currentProgram)
1431 {
1432 return error(GL_INVALID_OPERATION);
1433 }
1434
1435 IDirect3DDevice9 *device = getDevice();
1436 D3DPRIMITIVETYPE primitiveType;
1437 int primitiveCount;
1438
1439 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1440 return error(GL_INVALID_ENUM);
1441
1442 if (primitiveCount <= 0)
1443 {
1444 return;
1445 }
1446
1447 if (!applyRenderTarget(false))
1448 {
1449 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1450 }
1451
1452 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001453 applyVertexBuffer(first, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001454 applyShaders();
1455 applyTextures();
1456
1457 device->BeginScene();
1458 device->DrawPrimitive(primitiveType, first, primitiveCount);
1459 device->EndScene();
1460}
1461
1462void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
1463{
1464 if (!currentProgram)
1465 {
1466 return error(GL_INVALID_OPERATION);
1467 }
1468
1469 if (!indices && !elementArrayBuffer)
1470 {
1471 return error(GL_INVALID_OPERATION);
1472 }
1473
1474 IDirect3DDevice9 *device = getDevice();
1475 D3DPRIMITIVETYPE primitiveType;
1476 int primitiveCount;
1477
1478 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1479 return error(GL_INVALID_ENUM);
1480
1481 if (primitiveCount <= 0)
1482 {
1483 return;
1484 }
1485
1486 if (!applyRenderTarget(false))
1487 {
1488 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1489 }
1490
1491 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001492 applyVertexBuffer(count, indices, type);
1493 applyIndexBuffer(indices, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494 applyShaders();
1495 applyTextures();
1496
1497 device->BeginScene();
1498 device->DrawIndexedPrimitive(primitiveType, 0, 0, count, startIndex, primitiveCount);
1499 device->EndScene();
1500}
1501
1502void Context::finish()
1503{
1504 IDirect3DDevice9 *device = getDevice();
1505 IDirect3DQuery9 *occlusionQuery = NULL;
1506
1507 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
1508
1509 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1510 {
1511 return error(GL_OUT_OF_MEMORY);
1512 }
1513
1514 ASSERT(SUCCEEDED(result));
1515
1516 if (occlusionQuery)
1517 {
1518 occlusionQuery->Issue(D3DISSUE_BEGIN);
1519
1520 // Render something outside the render target
1521 device->SetPixelShader(NULL);
1522 device->SetVertexShader(NULL);
1523 device->SetFVF(D3DFVF_XYZRHW);
1524 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
1525 device->BeginScene();
1526 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
1527 device->EndScene();
1528
1529 occlusionQuery->Issue(D3DISSUE_END);
1530
1531 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1532 {
1533 // Keep polling, but allow other threads to do something useful first
1534 Sleep(0);
1535 }
1536
1537 occlusionQuery->Release();
1538 }
1539}
1540
1541void Context::flush()
1542{
1543 IDirect3DDevice9 *device = getDevice();
1544 IDirect3DQuery9 *eventQuery = NULL;
1545
1546 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
1547
1548 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1549 {
1550 return error(GL_OUT_OF_MEMORY);
1551 }
1552
1553 ASSERT(SUCCEEDED(result));
1554
1555 if (eventQuery)
1556 {
1557 eventQuery->Issue(D3DISSUE_END);
1558
1559 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1560 {
1561 // Keep polling, but allow other threads to do something useful first
1562 Sleep(0);
1563 }
1564
1565 eventQuery->Release();
1566 }
1567}
1568
1569void Context::recordInvalidEnum()
1570{
1571 mInvalidEnum = true;
1572}
1573
1574void Context::recordInvalidValue()
1575{
1576 mInvalidValue = true;
1577}
1578
1579void Context::recordInvalidOperation()
1580{
1581 mInvalidOperation = true;
1582}
1583
1584void Context::recordOutOfMemory()
1585{
1586 mOutOfMemory = true;
1587}
1588
1589void Context::recordInvalidFramebufferOperation()
1590{
1591 mInvalidFramebufferOperation = true;
1592}
1593
1594// Get one of the recorded errors and clear its flag, if any.
1595// [OpenGL ES 2.0.24] section 2.5 page 13.
1596GLenum Context::getError()
1597{
1598 if (mInvalidEnum)
1599 {
1600 mInvalidEnum = false;
1601
1602 return GL_INVALID_ENUM;
1603 }
1604
1605 if (mInvalidValue)
1606 {
1607 mInvalidValue = false;
1608
1609 return GL_INVALID_VALUE;
1610 }
1611
1612 if (mInvalidOperation)
1613 {
1614 mInvalidOperation = false;
1615
1616 return GL_INVALID_OPERATION;
1617 }
1618
1619 if (mOutOfMemory)
1620 {
1621 mOutOfMemory = false;
1622
1623 return GL_OUT_OF_MEMORY;
1624 }
1625
1626 if (mInvalidFramebufferOperation)
1627 {
1628 mInvalidFramebufferOperation = false;
1629
1630 return GL_INVALID_FRAMEBUFFER_OPERATION;
1631 }
1632
1633 return GL_NO_ERROR;
1634}
1635
1636void Context::detachBuffer(GLuint buffer)
1637{
1638 // [OpenGL ES 2.0.24] section 2.9 page 22:
1639 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
1640 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
1641
1642 if (arrayBuffer == buffer)
1643 {
1644 arrayBuffer = 0;
1645 }
1646
1647 if (elementArrayBuffer == buffer)
1648 {
1649 elementArrayBuffer = 0;
1650 }
1651
1652 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
1653 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001654 if (vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001655 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001656 vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 }
1658 }
1659}
1660
1661void Context::detachTexture(GLuint texture)
1662{
1663 // [OpenGL ES 2.0.24] section 3.8 page 84:
1664 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
1665 // rebound to texture object zero
1666
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001667 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001669 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001671 if (samplerTexture[type][sampler] == texture)
1672 {
1673 samplerTexture[type][sampler] = 0;
1674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 }
1676 }
1677
1678 // [OpenGL ES 2.0.24] section 4.4 page 112:
1679 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
1680 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
1681 // image was attached in the currently bound framebuffer.
1682
1683 Framebuffer *framebuffer = getFramebuffer();
1684
1685 if (framebuffer)
1686 {
1687 framebuffer->detachTexture(texture);
1688 }
1689}
1690
1691void Context::detachFramebuffer(GLuint framebuffer)
1692{
1693 // [OpenGL ES 2.0.24] section 4.4 page 107:
1694 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
1695 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
1696
1697 if (this->framebuffer == framebuffer)
1698 {
1699 bindFramebuffer(0);
1700 }
1701}
1702
1703void Context::detachRenderbuffer(GLuint renderbuffer)
1704{
1705 // [OpenGL ES 2.0.24] section 4.4 page 109:
1706 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
1707 // had been executed with the target RENDERBUFFER and name of zero.
1708
1709 if (this->renderbuffer == renderbuffer)
1710 {
1711 bindRenderbuffer(0);
1712 }
1713
1714 // [OpenGL ES 2.0.24] section 4.4 page 111:
1715 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
1716 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
1717 // point to which this image was attached in the currently bound framebuffer.
1718
1719 Framebuffer *framebuffer = getFramebuffer();
1720
1721 if (framebuffer)
1722 {
1723 framebuffer->detachRenderbuffer(renderbuffer);
1724 }
1725}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001726
1727Texture *Context::getIncompleteTexture(SamplerType type)
1728{
1729 Texture *t = mIncompleteTextures[type];
1730
1731 if (t == NULL)
1732 {
1733 static const GLubyte color[] = { 0, 0, 0, 255 };
1734
1735 switch (type)
1736 {
1737 default:
1738 UNREACHABLE();
1739 // default falls through to SAMPLER_2D
1740
1741 case SAMPLER_2D:
1742 {
1743 Texture2D *incomplete2d = new Texture2D;
1744 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1745 t = incomplete2d;
1746 }
1747 break;
1748
1749 case SAMPLER_CUBE:
1750 {
1751 TextureCubeMap *incompleteCube = new TextureCubeMap;
1752
1753 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1754 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1755 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1756 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1757 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1758 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
1759
1760 t = incompleteCube;
1761 }
1762 break;
1763 }
1764
1765 mIncompleteTextures[type] = t;
1766 }
1767
1768 return t;
1769}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770}
1771
1772extern "C"
1773{
1774gl::Context *glCreateContext(const egl::Config *config)
1775{
1776 return new gl::Context(config);
1777}
1778
1779void glDestroyContext(gl::Context *context)
1780{
1781 delete context;
1782
1783 if (context == gl::getContext())
1784 {
1785 gl::makeCurrent(NULL, NULL, NULL);
1786 }
1787}
1788
1789void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
1790{
1791 gl::makeCurrent(context, display, surface);
1792}
1793
1794gl::Context *glGetCurrentContext()
1795{
1796 return gl::getContext();
1797}
1798}