| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "AnimatedModel.h"
- #include "Animation.h"
- #include "AnimationController.h"
- #include "BillboardSet.h"
- #include "Camera.h"
- #include "Context.h"
- #include "DebugRenderer.h"
- #include "Graphics.h"
- #include "GraphicsEvents.h"
- #include "GraphicsImpl.h"
- #include "IndexBuffer.h"
- #include "Light.h"
- #include "Log.h"
- #include "Material.h"
- #include "Mutex.h"
- #include "Octree.h"
- #include "ParticleEmitter.h"
- #include "ProcessUtils.h"
- #include "Profiler.h"
- #include "RenderSurface.h"
- #include "Shader.h"
- #include "ShaderProgram.h"
- #include "ShaderVariation.h"
- #include "Skybox.h"
- #include "Technique.h"
- #include "Texture2D.h"
- #include "TextureCube.h"
- #include "VertexBuffer.h"
- #include "Zone.h"
- #include <stdio.h>
- #ifdef _MSC_VER
- #include <float.h>
- #else
- // From http://stereopsis.com/FPU.html
- #define FPU_CW_PREC_MASK 0x0300
- #define FPU_CW_PREC_SINGLE 0x0000
- #define FPU_CW_PREC_DOUBLE 0x0200
- #define FPU_CW_PREC_EXTENDED 0x0300
- #define FPU_CW_ROUND_MASK 0x0c00
- #define FPU_CW_ROUND_NEAR 0x0000
- #define FPU_CW_ROUND_DOWN 0x0400
- #define FPU_CW_ROUND_UP 0x0800
- #define FPU_CW_ROUND_CHOP 0x0c00
- inline unsigned GetFPUState()
- {
- unsigned control = 0;
- __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
- return control;
- }
- inline void SetFPUState(unsigned control)
- {
- __asm__ __volatile__ ("fldcw %0" : : "m" (control));
- }
- #endif
- #include "DebugNew.h"
- static const unsigned glCmpFunc[] =
- {
- GL_ALWAYS,
- GL_EQUAL,
- GL_NOTEQUAL,
- GL_LESS,
- GL_LEQUAL,
- GL_GREATER,
- GL_GEQUAL
- };
- static const unsigned glSrcBlend[] =
- {
- GL_ONE,
- GL_ONE,
- GL_DST_COLOR,
- GL_SRC_ALPHA,
- GL_SRC_ALPHA,
- GL_ONE,
- GL_ONE_MINUS_DST_ALPHA
- };
- static const unsigned glDestBlend[] =
- {
- GL_ZERO,
- GL_ONE,
- GL_ZERO,
- GL_ONE_MINUS_SRC_ALPHA,
- GL_ONE,
- GL_ONE_MINUS_SRC_ALPHA,
- GL_DST_ALPHA
- };
- static const unsigned glStencilOps[] =
- {
- GL_KEEP,
- GL_ZERO,
- GL_REPLACE,
- GL_INCR_WRAP,
- GL_DECR_WRAP
- };
- static unsigned numInstances = 0;
- static const String noParameter;
- int CloseCallback(GLFWwindow window)
- {
- // Do not let GLFW close the window, rather do it ourselves in a controlled fashion
- Context* context = GetWindowContext(window);
- if (context)
- {
- Graphics* graphics = context->GetSubsystem<Graphics>();
- if (graphics)
- graphics->Close();
- }
- return GL_FALSE;
- }
- OBJECTTYPESTATIC(Graphics);
- Graphics::Graphics(Context* context_) :
- Object(context_),
- impl_(new GraphicsImpl()),
- mode_(RENDER_FORWARD),
- width_(0),
- height_(0),
- multiSample_(1),
- fullscreen_(false),
- vsync_(false),
- flushGPU_(true),
- renderTargetSupport_(false),
- deferredSupport_(false),
- numPrimitives_(0),
- numBatches_(0),
- immediateVertexCount_(0),
- defaultTextureFilterMode_(FILTER_BILINEAR),
- shadowMapFormat_(0),
- hiresShadowMapFormat_(0),
- shaderParameterFrame_(0)
- {
- ResetCachedState();
- SetTextureUnitMappings();
-
- {
- MutexLock lock(GetStaticMutex());
- if (!numInstances)
- glfwInit();
-
- ++numInstances;
- }
- }
- Graphics::~Graphics()
- {
- Close();
-
- delete impl_;
- impl_ = 0;
-
- {
- MutexLock lock(GetStaticMutex());
-
- --numInstances;
- if (!numInstances)
- glfwTerminate();
- }
- }
- void Graphics::SetWindowTitle(const String& windowTitle)
- {
- windowTitle_ = windowTitle;
- if (impl_->window_)
- glfwSetWindowTitle(impl_->window_, windowTitle_.CString());
- }
- bool Graphics::SetMode(RenderMode mode, int width, int height, bool fullscreen, bool vsync, int multiSample)
- {
- PROFILE(SetScreenMode);
-
- multiSample = Clamp(multiSample, 1, 16);
-
- if (IsInitialized() && mode == mode_ && width == width_ && height == height_ && fullscreen == fullscreen_ &&
- vsync == vsync_ && multiSample == multiSample_)
- return true;
-
- // If only vsync changes, do not destroy/recreate the context
- if (IsInitialized() && mode == mode_ && width == width_ && height == height_ && fullscreen == fullscreen_ &&
- multiSample == multiSample_ && vsync != vsync_)
- {
- glfwSwapInterval(vsync ? 1 : 0);
- vsync_ = vsync;
- return true;
- }
-
- // If zero dimensions in windowed mode, set default. If zero in fullscreen, use desktop mode
- if (!width || !height)
- {
- if (!fullscreen)
- {
- width = 800;
- height = 600;
- }
- else
- {
- GLFWvidmode mode;
- glfwGetDesktopMode(&mode);
- width = mode.width;
- height = mode.height;
- }
- }
-
- // Close the existing window
- Release();
-
- {
- // GLFW window parameters and the window list are static, so need to operate under static lock
- MutexLock lock(GetStaticMutex());
-
- glfwOpenWindowHint(GLFW_RED_BITS, 8);
- glfwOpenWindowHint(GLFW_GREEN_BITS, 8);
- glfwOpenWindowHint(GLFW_BLUE_BITS, 8);
- glfwOpenWindowHint(GLFW_ALPHA_BITS, 0);
- glfwOpenWindowHint(GLFW_DEPTH_BITS, 24);
- glfwOpenWindowHint(GLFW_STENCIL_BITS, 8);
- glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
- if (multiSample > 1 && mode == RENDER_FORWARD)
- glfwOpenWindowHint(GLFW_FSAA_SAMPLES, multiSample);
- else
- glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 0);
-
- impl_->window_ = glfwOpenWindow(width, height, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOWED, windowTitle_.CString(), 0);
- if (!impl_->window_)
- {
- LOGERROR("Could not open window");
- return false;
- }
-
- // If OpenGL extensions not yet initialized, initialize now
- if (!GLeeInitialized())
- GLeeInit();
-
- if (!_GLEE_VERSION_2_0)
- {
- LOGERROR("OpenGL 2.0 is required");
- glfwCloseWindow(impl_->window_);
- return false;
- }
-
- // Set window close callback
- glfwSetWindowCloseCallback(CloseCallback);
-
- // Mimic Direct3D way of setting FPU into round-to-nearest, single precision mode
- // This is actually needed for ODE to behave predictably in float mode
- #ifdef _MSC_VER
- _controlfp(_RC_NEAR | _PC_24, _MCW_RC | _MCW_PC);
- #else
- unsigned control = GetFPUState();
- control &= ~(FPU_CW_PREC_MASK | FPU_CW_ROUND_MASK);
- control |= (FPU_CW_PREC_SINGLE | FPU_CW_ROUND_NEAR);
- SetFPUState(control);
- #endif
-
- // Associate GLFW window with the execution context
- SetWindowContext(impl_->window_, context_);
- }
-
- // Set vsync
- glfwSwapInterval(vsync ? 1 : 0);
-
- // Query for system backbuffer depth
- glGetIntegerv(GL_DEPTH_BITS, &impl_->windowDepthBits_);
- impl_->depthBits_ = impl_->windowDepthBits_;
-
- // Create the FBO if fully supported
- if (_GLEE_EXT_framebuffer_object && _GLEE_EXT_packed_depth_stencil)
- {
- glGenFramebuffersEXT(1, &impl_->fbo_);
-
- // Shadows, render targets and deferred rendering all depend on FBO & packed depth stencil
- shadowMapFormat_ = GL_DEPTH_COMPONENT16;
- hiresShadowMapFormat_ = GL_DEPTH_COMPONENT24;
- renderTargetSupport_ = true;
- deferredSupport_ = true;
- }
-
- // Set initial state to match Direct3D
- glEnable(GL_DEPTH_TEST);
- SetCullMode(CULL_CCW);
- SetDepthTest(CMP_LESSEQUAL);
-
- glfwGetWindowSize(impl_->window_, &width_, &height_);
- fullscreen_ = fullscreen;
- vsync_ = vsync;
- mode_ = mode;
- multiSample_ = multiSample;
-
- // Reset rendertargets and viewport for the new screen mode
- ResetRenderTargets();
- viewTexture_ = 0;
-
- // Clear the window to black now, because GPU object restore may take time
- Clear(CLEAR_COLOR);
- glfwSwapBuffers();
-
- // Create deferred rendering buffers as necessary
- CreateRenderTargets();
-
- // Let GPU objects restore themselves
- for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
- (*i)->OnDeviceReset();
-
- if (multiSample > 1)
- LOGINFO("Set screen mode " + String(width_) + "x" + String(height_) + " " + (fullscreen_ ? "fullscreen" : "windowed") +
- " multisample " + String(multiSample));
- else
- LOGINFO("Set screen mode " + String(width_) + "x" + String(height_) + " " + (fullscreen_ ? "fullscreen" : "windowed"));
-
- using namespace ScreenMode;
-
- VariantMap eventData;
- eventData[P_WIDTH] = width_;
- eventData[P_HEIGHT] = height_;
- eventData[P_FULLSCREEN] = fullscreen_;
- SendEvent(E_SCREENMODE, eventData);
-
- return true;
- }
- bool Graphics::SetMode(int width, int height)
- {
- return SetMode(mode_, width, height, fullscreen_, vsync_, multiSample_);
- }
- bool Graphics::SetMode(RenderMode mode)
- {
- return SetMode(mode, width_, height_, fullscreen_, vsync_, multiSample_);
- }
- bool Graphics::ToggleFullscreen()
- {
- return SetMode(mode_, width_, height_, !fullscreen_, vsync_, multiSample_);
- }
- void Graphics::Close()
- {
- if (!IsInitialized())
- return;
-
- // Release all GPU objects that still exist
- for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
- (*i)->Release();
- gpuObjects_.Clear();
- // Actually close the window
- Release();
- }
- bool Graphics::TakeScreenShot(Image& destImage)
- {
- PROFILE(TakeScreenShot);
-
- ResetRenderTargets();
- destImage.SetSize(width_, height_, 3);
- glReadPixels(0, 0, width_, height_, GL_RGB, GL_UNSIGNED_BYTE, destImage.GetData());
-
- return true;
- }
- void Graphics::SetFlushGPU(bool enable)
- {
- flushGPU_ = enable;
- }
- bool Graphics::BeginFrame()
- {
- PROFILE(BeginRendering);
- if (!IsInitialized())
- return false;
- // If we should be fullscreen, but are not currently active, do not render
- if (fullscreen_ && (!glfwGetWindowParam(impl_->window_, GLFW_ACTIVE) || glfwGetWindowParam(impl_->window_, GLFW_ICONIFIED)))
- return false;
-
- // Set default rendertarget and depth buffer
- ResetRenderTargets();
- viewTexture_ = 0;
-
- // Cleanup textures from previous frame
- for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
- SetTexture(i, 0);
-
- // Enable color and depth write
- SetColorWrite(true);
- SetDepthWrite(true);
-
- numPrimitives_ = 0;
- numBatches_ = 0;
-
- SendEvent(E_BEGINRENDER);
-
- return true;
- }
- void Graphics::EndFrame()
- {
- PROFILE(EndRendering);
-
- if (!IsInitialized())
- return;
-
- SendEvent(E_ENDRENDER);
-
- glfwSwapBuffers();
- }
- void Graphics::Clear(unsigned flags, const Color& color, float depth, unsigned stencil)
- {
- bool oldColorWrite = colorWrite_;
- bool oldDepthWrite = depthWrite_;
-
- if (flags & CLEAR_COLOR && !oldColorWrite)
- SetColorWrite(true);
- if (flags & CLEAR_DEPTH && !oldDepthWrite)
- SetDepthWrite(true);
- unsigned glFlags = 0;
- if (flags & CLEAR_COLOR)
- {
- glFlags |= GL_COLOR_BUFFER_BIT;
- glClearColor(color.r_, color.g_, color.b_, color.a_);
- }
- if (flags & CLEAR_DEPTH)
- {
- glFlags |= GL_DEPTH_BUFFER_BIT;
- glClearDepth(depth);
- }
- if (flags & CLEAR_STENCIL)
- {
- glFlags |= GL_STENCIL_BUFFER_BIT;
- glClearStencil(stencil);
- }
-
- glClear(glFlags);
-
- SetColorWrite(oldColorWrite);
- SetDepthWrite(oldDepthWrite);
- }
- void Graphics::Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount)
- {
- if (!vertexCount)
- return;
-
- unsigned primitiveCount = 0;
-
- switch (type)
- {
- case TRIANGLE_LIST:
- primitiveCount = vertexCount / 3;
- glDrawArrays(GL_TRIANGLES, vertexStart, vertexCount);
- break;
-
- case LINE_LIST:
- primitiveCount = vertexCount / 2;
- glDrawArrays(GL_LINES, vertexStart, vertexCount);
- break;
- }
-
- numPrimitives_ += primitiveCount;
- ++numBatches_;
- }
- void Graphics::Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount)
- {
- if (!indexCount || !indexBuffer_)
- return;
-
- unsigned primitiveCount = 0;
- unsigned indexSize = indexBuffer_->GetIndexSize();
-
- switch (type)
- {
- case TRIANGLE_LIST:
- primitiveCount = indexCount / 3;
- if (indexSize == sizeof(unsigned short))
- glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (const GLvoid*)(indexStart * indexSize));
- else
- glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (const GLvoid*)(indexStart * indexSize));
- break;
-
- case LINE_LIST:
- primitiveCount = indexCount / 2;
- if (indexSize == sizeof(unsigned short))
- glDrawElements(GL_LINES, indexCount, GL_UNSIGNED_SHORT, (const GLvoid*)(indexStart * indexSize));
- else
- glDrawElements(GL_LINES, indexCount, GL_UNSIGNED_INT, (const GLvoid*)(indexStart * indexSize));
- break;
- }
-
- numPrimitives_ += primitiveCount;
- ++numBatches_;
- }
- void Graphics::DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount)
- {
- }
- void Graphics::SetVertexBuffer(VertexBuffer* buffer)
- {
- Vector<VertexBuffer*> vertexBuffers(1);
- PODVector<unsigned> elementMasks(1);
- vertexBuffers[0] = buffer;
- elementMasks[0] = MASK_DEFAULT;
- SetVertexBuffers(vertexBuffers, elementMasks);
- }
- bool Graphics::SetVertexBuffers(const Vector<VertexBuffer*>& buffers, const PODVector<unsigned>& elementMasks,
- unsigned instanceOffset)
- {
- if (buffers.Size() > MAX_VERTEX_STREAMS)
- {
- LOGERROR("Too many vertex buffers");
- return false;
- }
- if (buffers.Size() != elementMasks.Size())
- {
- LOGERROR("Amount of element masks and vertex buffers does not match");
- return false;
- }
-
- // If no valid shader to determine the attribute bindings, can not set vertex buffers
- if (!shaderProgram_)
- return false;
- const int* attributeLocations = shaderProgram_->GetAttributeLocations();
-
- bool changed = false;
- unsigned newAttributes = 0;
-
- for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
- {
- VertexBuffer* buffer = 0;
- unsigned elementMask = 0;
-
- if (i < buffers.Size())
- {
- buffer = buffers[i];
- elementMask = elementMasks[i];
- if (elementMask == MASK_DEFAULT && buffer)
- elementMask = buffers[i]->GetElementMask();
- }
-
- // If buffer and element mask have stayed the same, skip to the next buffer
- if (buffer == vertexBuffers_[i] && elementMask == elementMasks_[i])
- continue;
-
- vertexBuffers_[i] = buffer;
- elementMasks_[i] = elementMask;
- changed = true;
-
- if (!buffer)
- continue;
-
- glBindBuffer(GL_ARRAY_BUFFER, buffer->GetGPUObject());
- unsigned vertexSize = buffer->GetVertexSize();
-
- for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
- {
- // If shader does not use the attribute, do not bind it (bandwidth optimization)
- int attributeIndex = attributeLocations[j];
- if (attributeIndex < 0)
- continue;
-
- unsigned elementBit = 1 << j;
- unsigned attributeBit = 1 << attributeIndex;
-
- if (elementMask & elementBit)
- {
- newAttributes |= attributeBit;
-
- // Enable attribute if not enabled yet
- if ((impl_->enabledAttributes_ & attributeBit) == 0)
- {
- glEnableVertexAttribArray(attributeIndex);
- impl_->enabledAttributes_ |= attributeBit;
- }
-
- // Set the attribute pointer
- glVertexAttribPointer(attributeIndex, VertexBuffer::elementComponents[j], VertexBuffer::elementType[j],
- VertexBuffer::elementNormalize[j], vertexSize, (const GLvoid*)(buffer->GetElementOffset((VertexElement)j)));
- }
- }
- }
-
- if (!changed)
- return true;
-
- // Now check which vertex attributes should be disabled
- unsigned disableAttributes = impl_->enabledAttributes_ & (~newAttributes);
- int disableIndex = 0;
- while (disableAttributes)
- {
- if (disableAttributes & 1)
- {
- glDisableVertexAttribArray(disableIndex);
- impl_->enabledAttributes_ &= ~(1 << disableIndex);
- }
- disableAttributes >>= 1;
- ++disableIndex;
- }
-
- return true;
- }
- bool Graphics::SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>&
- elementMasks, unsigned instanceOffset)
- {
- if (buffers.Size() > MAX_VERTEX_STREAMS)
- {
- LOGERROR("Too many vertex buffers");
- return false;
- }
- if (buffers.Size() != elementMasks.Size())
- {
- LOGERROR("Amount of element masks and vertex buffers does not match");
- return false;
- }
-
- // If no valid shader to determine the attribute bindings, can not set vertex buffers
- if (!shaderProgram_)
- return false;
- const int* attributeLocations = shaderProgram_->GetAttributeLocations();
-
- bool changed = false;
- unsigned newAttributes = 0;
-
- for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
- {
- VertexBuffer* buffer = 0;
- unsigned elementMask = 0;
-
- if (i < buffers.Size())
- {
- buffer = buffers[i];
- elementMask = elementMasks[i];
- if (elementMask == MASK_DEFAULT && buffer)
- elementMask = buffers[i]->GetElementMask();
- }
-
- // If buffer and element mask have stayed the same, skip to the next buffer
- if (buffer == vertexBuffers_[i] && elementMask == elementMasks_[i])
- continue;
-
- vertexBuffers_[i] = buffer;
- elementMasks_[i] = elementMask;
- changed = true;
-
- if (!buffer)
- continue;
-
- glBindBuffer(GL_ARRAY_BUFFER, buffer->GetGPUObject());
- unsigned vertexSize = buffer->GetVertexSize();
-
- for (unsigned j = 0; j < MAX_VERTEX_ELEMENTS; ++j)
- {
- // If shader does not use the attribute, do not bind it (bandwidth optimization)
- int attributeIndex = attributeLocations[j];
- if (attributeIndex < 0)
- continue;
-
- unsigned elementBit = 1 << j;
- unsigned attributeBit = 1 << attributeIndex;
-
- if (elementMask & elementBit)
- {
- newAttributes |= attributeBit;
-
- // Enable attribute if not enabled yet
- if ((impl_->enabledAttributes_ & attributeBit) == 0)
- {
- glEnableVertexAttribArray(attributeIndex);
- impl_->enabledAttributes_ |= attributeBit;
- }
-
- // Set the attribute pointer
- glVertexAttribPointer(attributeIndex, VertexBuffer::elementComponents[j], VertexBuffer::elementType[j],
- VertexBuffer::elementNormalize[j], vertexSize, (const GLvoid*)(buffer->GetElementOffset((VertexElement)j)));
- }
- }
- }
-
- if (!changed)
- return true;
-
- // Now check which vertex attributes should be disabled
- unsigned disableAttributes = impl_->enabledAttributes_ & (~newAttributes);
- int disableIndex = 0;
- while (disableAttributes)
- {
- if (disableAttributes & 1)
- {
- glDisableVertexAttribArray(disableIndex);
- impl_->enabledAttributes_ &= ~(1 << disableIndex);
- }
- disableAttributes >>= 1;
- ++disableIndex;
- }
-
- return true;
- }
- void Graphics::SetIndexBuffer(IndexBuffer* buffer)
- {
- if (indexBuffer_ == buffer)
- return;
-
- if (buffer)
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetGPUObject());
- else
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
-
- indexBuffer_ = buffer;
- }
- void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
- {
- if (vs == vertexShader_ && ps == pixelShader_)
- return;
-
- // Compile the shaders now if not yet compiled. If already attempted, do not retry
- if (vs && !vs->IsCompiled())
- {
- if (vs->GetCompilerOutput().Empty())
- {
- PROFILE(CompileVertexShader);
- bool success = vs->Create();
- if (success)
- LOGDEBUG("Compiled vertex shader " + vs->GetName());
- else
- {
- LOGERROR("Failed to compile vertex shader " + vs->GetName() + ":\n" + vs->GetCompilerOutput());
- vs = 0;
- }
- }
- else
- vs = 0;
- }
-
- if (ps && !ps->IsCompiled())
- {
- if (ps->GetCompilerOutput().Empty())
- {
- PROFILE(CompilePixelShader);
- bool success = ps->Create();
- if (success)
- LOGDEBUG("Compiled pixel shader " + ps->GetName());
- else
- {
- LOGERROR("Failed to compile pixel shader " + ps->GetName() + ":\n" + ps->GetCompilerOutput());
- ps = 0;
- }
- }
- else
- ps = 0;
- }
-
- for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
- {
- vertexBuffers_[i] = 0;
- elementMasks_[i] = 0;
- }
-
- if (!vs || !ps)
- {
- glUseProgram(0);
- vertexShader_ = 0;
- pixelShader_ = 0;
- shaderProgram_ = 0;
- }
- else
- {
- vertexShader_ = vs;
- pixelShader_ = ps;
-
- Pair<ShaderVariation*, ShaderVariation*> combination(vs, ps);
- ShaderProgramMap::Iterator i = shaderPrograms_.Find(combination);
-
- if (i != shaderPrograms_.End())
- {
- // Use the existing linked program
- if (i->second_->IsLinked())
- {
- glUseProgram(i->second_->GetGPUObject());
- shaderProgram_ = i->second_;
- }
- else
- {
- glUseProgram(0);
- shaderProgram_ = 0;
- }
- }
- else
- {
- // Link a new combination
- SharedPtr<ShaderProgram> newProgram(new ShaderProgram(this, vs, ps));
- if (newProgram->Link())
- {
- LOGDEBUG("Linked vertex shader " + vs->GetName() + " and pixel shader " + ps->GetName());
- // Note: Link() calls glUseProgram() to set the texture sampler uniforms,
- // so it is not necessary to call it again
- shaderProgram_ = newProgram;
- }
- else
- {
- LOGERROR("Failed to link vertex shader " + vs->GetName() + " and pixel shader " + ps->GetName() + ":\n" +
- newProgram->GetLinkerOutput());
- glUseProgram(0);
- shaderProgram_ = 0;
- }
-
- shaderPrograms_[combination] = newProgram;
- }
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const bool* data, unsigned count)
- {
- // Not supported
- }
- void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned count)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- {
- switch (info->type_)
- {
- case GL_FLOAT:
- glUniform1fv(info->location_, count, data);
- break;
-
- case GL_FLOAT_VEC2:
- glUniform2fv(info->location_, count / 2, data);
- break;
-
- case GL_FLOAT_VEC3:
- glUniform3fv(info->location_, count / 3, data);
- break;
-
- case GL_FLOAT_VEC4:
- glUniform4fv(info->location_, count / 4, data);
- break;
-
- case GL_FLOAT_MAT3:
- glUniformMatrix3fv(info->location_, count / 9, GL_TRUE, data);
- break;
-
- case GL_FLOAT_MAT4:
- glUniformMatrix4fv(info->location_, count / 16, GL_TRUE, data);
- break;
- }
- }
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const int* data, unsigned count)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- {
- switch (info->type_)
- {
- case GL_INT:
- glUniform1iv(info->location_, count, data);
- break;
-
- case GL_INT_VEC2:
- glUniform2iv(info->location_, count / 2, data);
- break;
-
- case GL_INT_VEC3:
- glUniform3iv(info->location_, count / 3, data);
- break;
-
- case GL_INT_VEC4:
- glUniform4iv(info->location_, count / 4, data);
- break;
- }
- }
- }
- }
- void Graphics::SetShaderParameter(StringHash param, float value)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- glUniform1fv(info->location_, 1, &value);
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const Color& color)
- {
- SetShaderParameter(param, color.GetData(), 4);
- }
- void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- glUniformMatrix3fv(info->location_, 1, GL_TRUE, matrix.GetData());
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- {
- // Check the uniform type to avoid mismatch
- switch (info->type_)
- {
- case GL_FLOAT:
- glUniform1fv(info->location_, 1, vector.GetData());
- break;
-
- case GL_FLOAT_VEC2:
- glUniform2fv(info->location_, 1, vector.GetData());
- break;
-
- case GL_FLOAT_VEC3:
- glUniform3fv(info->location_, 1, vector.GetData());
- break;
- }
- }
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- glUniformMatrix4fv(info->location_, 1, GL_TRUE, matrix.GetData());
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- {
- // Check the uniform type to avoid mismatch
- switch (info->type_)
- {
- case GL_FLOAT:
- glUniform1fv(info->location_, 1, vector.GetData());
- break;
-
- case GL_FLOAT_VEC2:
- glUniform2fv(info->location_, 1, vector.GetData());
- break;
-
- case GL_FLOAT_VEC3:
- glUniform3fv(info->location_, 1, vector.GetData());
- break;
-
- case GL_FLOAT_VEC4:
- glUniform4fv(info->location_, 1, vector.GetData());
- break;
- }
- }
- }
- }
- void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
- {
- if (shaderProgram_)
- {
- const ShaderParameter* info = shaderProgram_->GetParameter(param);
- if (info)
- {
- float data[16];
- data[0] = matrix.m00_;
- data[1] = matrix.m01_;
- data[2] = matrix.m02_;
- data[3] = matrix.m03_;
- data[4] = matrix.m10_;
- data[5] = matrix.m11_;
- data[6] = matrix.m12_;
- data[7] = matrix.m13_;
- data[8] = matrix.m20_;
- data[9] = matrix.m21_;
- data[10] = matrix.m22_;
- data[11] = matrix.m23_;
- data[12] = 0.0f;
- data[13] = 0.0f;
- data[14] = 0.0f;
- data[15] = 1.0f;
-
- glUniformMatrix4fv(info->location_, 1, GL_TRUE, data);
- }
- }
- }
- bool Graphics::NeedParameterUpdate(StringHash param, const void* source)
- {
- if (shaderProgram_)
- return shaderProgram_->NeedParameterUpdate(param, source, shaderParameterFrame_);
-
- return false;
- }
- bool Graphics::NeedTextureUnit(TextureUnit unit)
- {
- if (shaderProgram_ && shaderProgram_->HasTextureUnit(unit))
- return true;
-
- return false;
- }
- void Graphics::ClearParameterSources()
- {
- ++shaderParameterFrame_;
- }
- void Graphics::ClearTransformSources()
- {
- if (shaderProgram_)
- {
- shaderProgram_->ClearParameterSource(VSP_MODEL);
- shaderProgram_->ClearParameterSource(VSP_VIEWPROJ);
- }
- }
- void Graphics::CleanupShaderPrograms()
- {
- for (ShaderProgramMap::Iterator i = shaderPrograms_.Begin(); i != shaderPrograms_.End();)
- {
- ShaderProgramMap::Iterator current = i++;
- ShaderVariation* vs = current->second_->GetVertexShader();
- ShaderVariation* ps = current->second_->GetPixelShader();
-
- if (!vs || !ps || !vs->GetGPUObject() || !ps->GetGPUObject())
- shaderPrograms_.Erase(current);
- }
- }
- void Graphics::SetTexture(unsigned index, Texture* texture)
- {
- if (index >= MAX_TEXTURE_UNITS)
- return;
-
- // Check if texture is currently bound as a render target. In that case, use its backup texture, or blank if not defined
- if (texture)
- {
- if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
- texture = texture->GetBackupTexture();
- // Check also for the view texture, in case a specific rendering pass does not bind the destination render target,
- // but should still not sample it either
- else if (texture == viewTexture_)
- texture = texture->GetBackupTexture();
- }
-
- if (textures_[index] != texture)
- {
- if (impl_->activeTexture_ != index)
- {
- glActiveTexture(GL_TEXTURE0 + index);
- impl_->activeTexture_ = index;
- }
-
- if (texture)
- {
- unsigned glType = texture->GetTarget();
- if (glType != textureTypes_[index])
- {
- if (textureTypes_[index])
- glDisable(textureTypes_[index]);
-
- glEnable(glType);
- textureTypes_[index] = glType;
- }
-
- glBindTexture(glType, texture->GetGPUObject());
-
- if (texture->GetParametersDirty())
- texture->UpdateParameters();
- }
- else
- {
- if (textureTypes_[index])
- glBindTexture(textureTypes_[index], 0);
- }
-
- textures_[index] = texture;
- }
- else
- {
- if (texture && texture->GetParametersDirty())
- {
- if (impl_->activeTexture_ != index)
- {
- glActiveTexture(GL_TEXTURE0 + index);
- impl_->activeTexture_ = index;
- }
-
- glBindTexture(texture->GetTarget(), texture->GetGPUObject());
- texture->UpdateParameters();
- }
- }
- }
- void Graphics::SetTextureForUpdate(Texture* texture)
- {
- if (impl_->activeTexture_ != 0)
- {
- glActiveTexture(GL_TEXTURE0);
- impl_->activeTexture_ = 0;
- }
-
- glBindTexture(texture->GetTarget(), texture->GetGPUObject());
- textures_[0] = texture;
- }
- void Graphics::SetDefaultTextureFilterMode(TextureFilterMode mode)
- {
- if (mode != defaultTextureFilterMode_)
- {
- defaultTextureFilterMode_ = mode;
- SetTextureParametersDirty();
- }
- }
- void Graphics::SetTextureAnisotropy(unsigned level)
- {
- if (level != textureAnisotropy_)
- {
- textureAnisotropy_ = level;
- SetTextureParametersDirty();
- }
- }
- void Graphics::SetTextureParametersDirty()
- {
- for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
- {
- Texture* texture = dynamic_cast<Texture*>(*i);
- if (texture)
- texture->SetParametersDirty();
- }
- }
- void Graphics::ResetRenderTargets()
- {
- for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
- SetRenderTarget(i, (RenderSurface*)0);
- SetDepthStencil((RenderSurface*)0);
- }
- void Graphics::ResetRenderTarget(unsigned index)
- {
- SetRenderTarget(index, (RenderSurface*)0);
- }
- void Graphics::ResetDepthStencil()
- {
- SetDepthStencil((RenderSurface*)0);
- }
- void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
- {
- if (index >= MAX_RENDERTARGETS || !impl_->fbo_)
- return;
-
- if (renderTarget != renderTargets_[index])
- {
- renderTargets_[index] = renderTarget;
-
- // If the rendertarget is also bound as a texture, replace with backup texture or null
- if (renderTarget)
- {
- Texture* parentTexture = renderTarget->GetParentTexture();
-
- for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
- {
- if (textures_[i] == parentTexture)
- SetTexture(i, textures_[i]->GetBackupTexture());
- }
- }
-
- // Bind the FBO to be able to make changes to it
- if (!impl_->fboBound_)
- {
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->fbo_);
- impl_->fboBound_ = true;
- }
-
- if (renderTarget)
- {
- Texture* texture = renderTarget->GetParentTexture();
-
- // If texture's parameters are dirty, update before attaching
- if (texture->GetParametersDirty())
- {
- SetTextureForUpdate(texture);
- texture->UpdateParameters();
- SetTexture(0, 0);
- }
-
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, renderTarget->GetTarget(), texture->GetGPUObject(), 0);
- }
- else
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_2D, 0, 0);
-
- // Disable color buffer writing/reading if only a depth texture is to be used:
- // otherwise it is an OpenGL error (incomplete framebuffer)
- SetDrawBuffers();
-
- // If all rendertargets and the depth buffer are not textures, revert to backbuffer rendering
- bool noFBO = (depthStencil_ == 0);
- for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
- {
- if (renderTargets_[i])
- {
- noFBO = false;
- break;
- }
- }
-
- if (noFBO && impl_->fboBound_)
- {
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- impl_->fboBound_ = false;
- }
- }
- }
- void Graphics::SetRenderTarget(unsigned index, Texture2D* renderTexture)
- {
- RenderSurface* renderTarget = 0;
- if (renderTexture)
- renderTarget = renderTexture->GetRenderSurface();
-
- SetRenderTarget(index, renderTarget);
- }
- void Graphics::SetDepthStencil(RenderSurface* depthStencil)
- {
- if (impl_->fbo_ && depthStencil != depthStencil_)
- {
- // If we are using a rendertarget texture, it is required in OpenGL to also have an own depth stencil
- // Create a new depth stencil texture as necessary to be able to provide similar behaviour.
- if (renderTargets_[0] && !depthStencil)
- {
- int width = renderTargets_[0]->GetWidth();
- int height = renderTargets_[0]->GetHeight();
- int searchKey = (width << 16) | height;
- HashMap<int, SharedPtr<Texture2D> >::Iterator i = depthTextures_.Find(searchKey);
- if (i != depthTextures_.End())
- depthStencil = i->second_->GetRenderSurface();
- else
- {
- SharedPtr<Texture2D> newDepthTexture(new Texture2D(context_));
- newDepthTexture->SetSize(width, height, GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
- depthTextures_[searchKey] = newDepthTexture;
- depthStencil = newDepthTexture->GetRenderSurface();
- }
- }
-
- /// \todo Should check that the texture actually is in depth format
- depthStencil_ = depthStencil;
-
- // Bind the FBO to be able to make changes to it
- if (!impl_->fboBound_)
- {
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, impl_->fbo_);
- impl_->fboBound_ = true;
- }
-
- if (depthStencil)
- {
- // Bind either a renderbuffer or a depth texture, depending on what is available
- unsigned renderBufferID = depthStencil->GetRenderBuffer();
- if (!renderBufferID)
- {
- Texture* texture = depthStencil->GetParentTexture();
-
- // If texture's parameters are dirty, update before attaching
- if (texture->GetParametersDirty())
- {
- SetTextureForUpdate(texture);
- texture->UpdateParameters();
- SetTexture(0, 0);
- }
-
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texture->GetGPUObject(), 0);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
- impl_->depthBits_ = texture->GetDepthBits();
- }
- else
- {
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, renderBufferID);
- impl_->depthBits_ = 24;
- }
- }
- else
- {
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0);
- impl_->depthBits_ = impl_->windowDepthBits_;
- }
-
- // Disable color buffer writing/reading if only a depth texture is to be used:
- // otherwise it is an OpenGL error (incomplete framebuffer)
- SetDrawBuffers();
-
- // If all rendertargets and the depth buffer are not textures, revert to backbuffer rendering
- bool noFBO = (depthStencil_ == 0);
- for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
- {
- if (renderTargets_[i])
- {
- noFBO = false;
- break;
- }
- }
-
- if (noFBO && impl_->fboBound_)
- {
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- impl_->fboBound_ = false;
- }
- }
-
- // Reset viewport and scissor test
- IntVector2 viewSize = GetRenderTargetDimensions();
- SetViewport(IntRect(0, 0, viewSize.x_, viewSize.y_));
- }
- void Graphics::SetDepthStencil(Texture2D* depthTexture)
- {
- RenderSurface* depthStencil = 0;
- if (depthTexture)
- depthStencil = depthTexture->GetRenderSurface();
-
- SetDepthStencil(depthStencil);
- }
- void Graphics::SetViewport(const IntRect& rect)
- {
- IntVector2 rtSize = GetRenderTargetDimensions();
-
- IntRect rectCopy = rect;
-
- if (rectCopy.right_ <= rectCopy.left_)
- rectCopy.right_ = rectCopy.left_ + 1;
- if (rectCopy.bottom_ <= rectCopy.top_)
- rectCopy.bottom_ = rectCopy.top_ + 1;
- rectCopy.left_ = Clamp(rectCopy.left_, 0, rtSize.x_);
- rectCopy.top_ = Clamp(rectCopy.top_, 0, rtSize.y_);
- rectCopy.right_ = Clamp(rectCopy.right_, 0, rtSize.x_);
- rectCopy.bottom_ = Clamp(rectCopy.bottom_, 0, rtSize.y_);
-
- if (rectCopy != viewport_)
- {
- // Use Direct3D convention with the vertical coordinates ie. 0 is top
- glViewport(rectCopy.left_, rtSize.y_ - rectCopy.bottom_, rectCopy.right_ - rectCopy.left_, rectCopy.bottom_ - rectCopy.top_);
- viewport_ = rectCopy;
- }
-
- // Disable scissor test, needs to be re-enabled by the user
- SetScissorTest(false);
- }
- void Graphics::SetViewTexture(Texture* texture)
- {
- viewTexture_ = texture;
-
- // Check for the view texture being currently bound
- if (texture)
- {
- for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
- {
- if (textures_[i] == texture)
- SetTexture(i, textures_[i]->GetBackupTexture());
- }
- }
- }
- void Graphics::SetAlphaTest(bool enable, CompareMode mode, float alphaRef)
- {
- if (enable != alphaTest_)
- {
- if (enable)
- glEnable(GL_ALPHA_TEST);
- else
- glDisable(GL_ALPHA_TEST);
- alphaTest_ = enable;
- }
-
- if (enable)
- {
- alphaRef = Clamp(alphaRef, 0.0f, 1.0f);
- if (mode != alphaTestMode_ || alphaRef != alphaRef_)
- {
- glAlphaFunc(glCmpFunc[mode], alphaRef);
- alphaTestMode_ = mode;
- alphaRef_ = alphaRef;
- }
- }
- }
- void Graphics::SetBlendMode(BlendMode mode)
- {
- if (mode != blendMode_)
- {
- if (mode == BLEND_REPLACE)
- glDisable(GL_BLEND);
- else
- {
- glEnable(GL_BLEND);
- glBlendFunc(glSrcBlend[mode], glDestBlend[mode]);
- }
-
- blendMode_ = mode;
- }
- }
- void Graphics::SetColorWrite(bool enable)
- {
- if (enable != colorWrite_)
- {
- if (enable)
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
- else
- glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-
- colorWrite_ = enable;
- }
- }
- void Graphics::SetCullMode(CullMode mode)
- {
- if (mode != cullMode_)
- {
- if (mode == CULL_NONE)
- glDisable(GL_CULL_FACE);
- else
- {
- // Use Direct3D convention, ie. clockwise vertices define a front face
- glEnable(GL_CULL_FACE);
- glCullFace(mode == CULL_CCW ? GL_FRONT : GL_BACK);
- }
-
- cullMode_ = mode;
- }
- }
- void Graphics::SetDepthBias(float constantBias, float slopeScaledBias)
- {
- if (constantBias != constantDepthBias_ || slopeScaledBias != slopeScaledDepthBias_)
- {
- if (constantBias != 0.0f || slopeScaledBias != 0.0f)
- {
- // Bring the constant bias from Direct3D9 scale to OpenGL (depends on depth buffer bitdepth)
- // Zero depth bits may be returned if using the packed depth stencil format. Assume 24bit in that case
- int depthBits = Min(impl_->depthBits_, 23);
- if (!depthBits)
- depthBits = 23;
- float adjustedConstantBias = constantBias * (float)(1 << (depthBits - 1));
- float adjustedSlopeScaledBias = slopeScaledBias + 1.0f;
-
- glEnable(GL_POLYGON_OFFSET_FILL);
- glEnable(GL_POLYGON_OFFSET_LINE);
- glPolygonOffset(adjustedSlopeScaledBias, adjustedConstantBias);
- }
- else
- {
- glDisable(GL_POLYGON_OFFSET_FILL);
- glDisable(GL_POLYGON_OFFSET_LINE);
- }
-
- constantDepthBias_ = constantBias;
- slopeScaledDepthBias_ = slopeScaledBias;
- }
- }
- void Graphics::SetDepthTest(CompareMode mode)
- {
- if (mode != depthTestMode_)
- {
- glDepthFunc(glCmpFunc[mode]);
- depthTestMode_ = mode;
- }
- }
- void Graphics::SetDepthWrite(bool enable)
- {
- if (enable != depthWrite_)
- {
- glDepthMask(enable ? GL_TRUE : GL_FALSE);
- depthWrite_ = enable;
- }
- }
- void Graphics::SetFillMode(FillMode mode)
- {
- if (mode != fillMode_)
- {
- glPolygonMode(GL_FRONT_AND_BACK, mode == FILL_SOLID ? GL_FILL : GL_LINE);
- fillMode_ = mode;
- }
- }
- void Graphics::SetScissorTest(bool enable, const Rect& rect, bool borderInclusive)
- {
- // During some light rendering loops, a full rect is toggled on/off repeatedly.
- // Disable scissor in that case to reduce state changes
- if (rect.min_.x_ <= 0.0f && rect.min_.y_ <= 0.0f && rect.max_.y_ >= 1.0f && rect.max_.y_ >= 1.0f)
- enable = false;
-
- // Check for illegal rect, disable in that case
- if (rect.max_.x_ < rect.min_.x_ || rect.max_.y_ < rect.min_.y_)
- enable = false;
-
- if (enable)
- {
- IntVector2 rtSize(GetRenderTargetDimensions());
- IntVector2 viewSize(viewport_.right_ - viewport_.left_, viewport_.bottom_ - viewport_.top_);
- IntVector2 viewPos(viewport_.left_, viewport_.top_);
- IntRect intRect;
- int expand = borderInclusive ? 1 : 0;
-
- intRect.left_ = Clamp((int)((rect.min_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_, 0, rtSize.x_ - 1);
- intRect.top_ = Clamp((int)((-rect.max_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_, 0, rtSize.y_ - 1);
- intRect.right_ = Clamp((int)((rect.max_.x_ + 1.0f) * 0.5f * viewSize.x_) + viewPos.x_ + expand, 0, rtSize.x_);
- intRect.bottom_ = Clamp((int)((-rect.min_.y_ + 1.0f) * 0.5f * viewSize.y_) + viewPos.y_ + expand, 0, rtSize.y_);
-
- if (intRect.right_ == intRect.left_)
- intRect.right_++;
- if (intRect.bottom_ == intRect.top_)
- intRect.bottom_++;
-
- if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
- enable = false;
-
- if (enable && scissorRect_ != intRect)
- {
- // Use Direct3D convention with the vertical coordinates ie. 0 is top
- glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.right_ - intRect.left_, intRect.bottom_ - intRect.top_);
- scissorRect_ = intRect;
- }
- }
- else
- scissorRect_ = IntRect::ZERO;
-
- if (enable != scissorTest_)
- {
- if (enable)
- glEnable(GL_SCISSOR_TEST);
- else
- glDisable(GL_SCISSOR_TEST);
- scissorTest_ = enable;
- }
- }
- void Graphics::SetScissorTest(bool enable, const IntRect& rect)
- {
- IntVector2 rtSize(GetRenderTargetDimensions());
- IntVector2 viewSize(viewport_.right_ - viewport_.left_, viewport_.bottom_ - viewport_.top_);
- IntVector2 viewPos(viewport_.left_, viewport_.top_);
-
- // Full scissor is same as disabling the test
- if (rect.left_ <= 0 && rect.right_ >= viewSize.x_ && rect.top_ <= 0 && rect.bottom_ >= viewSize.y_)
- enable = false;
-
- // Check for illegal rect, disable in that case
- if (rect.right_ < rect.left_ || rect.bottom_ < rect.top_)
- enable = false;
-
- if (enable)
- {
- IntRect intRect;
- intRect.left_ = Clamp(rect.left_ + viewPos.x_, 0, rtSize.x_ - 1);
- intRect.top_ = Clamp(rect.top_ + viewPos.y_, 0, rtSize.y_ - 1);
- intRect.right_ = Clamp(rect.right_ + viewPos.x_, 0, rtSize.x_);
- intRect.bottom_ = Clamp(rect.bottom_ + viewPos.y_, 0, rtSize.y_);
-
- if (intRect.right_ == intRect.left_)
- intRect.right_++;
- if (intRect.bottom_ == intRect.top_)
- intRect.bottom_++;
-
- if (intRect.right_ < intRect.left_ || intRect.bottom_ < intRect.top_)
- enable = false;
-
- if (enable && scissorRect_ != intRect)
- {
- // Use Direct3D convention with the vertical coordinates ie. 0 is top
- glScissor(intRect.left_, rtSize.y_ - intRect.bottom_, intRect.right_ - intRect.left_, intRect.bottom_ - intRect.top_);
- scissorRect_ = intRect;
- }
- }
- else
- scissorRect_ = IntRect::ZERO;
-
- if (enable != scissorTest_)
- {
- if (enable)
- glEnable(GL_SCISSOR_TEST);
- else
- glDisable(GL_SCISSOR_TEST);
- scissorTest_ = enable;
- }
- }
- void Graphics::SetStreamFrequency(unsigned index, unsigned frequency)
- {
- }
- void Graphics::ResetStreamFrequencies()
- {
- }
- void Graphics::SetStencilTest(bool enable, CompareMode mode, StencilOp pass, StencilOp fail, StencilOp zFail, unsigned stencilRef, unsigned stencilMask)
- {
- if (enable != stencilTest_)
- {
- if (enable)
- glEnable(GL_STENCIL_TEST);
- else
- glDisable(GL_STENCIL_TEST);
- stencilTest_ = enable;
- }
-
- if (enable)
- {
- if (mode != stencilTestMode_ || stencilRef != stencilRef_ || stencilMask != stencilMask_)
- {
- glStencilFunc(glCmpFunc[mode], stencilRef, stencilMask);
- stencilTestMode_ = mode;
- stencilRef_ = stencilRef;
- stencilMask_ = stencilMask;
- }
- if (pass != stencilPass_ || fail != stencilFail_ || zFail != stencilZFail_)
- {
- glStencilOp(glStencilOps[fail], glStencilOps[zFail], glStencilOps[pass]);
- stencilPass_ = pass;
- stencilFail_ = fail;
- stencilZFail_ = zFail;
- }
- }
- }
- bool Graphics::BeginImmediate(PrimitiveType type, unsigned vertexCount, unsigned elementMask)
- {
- if (immediateVertexCount_)
- {
- LOGERROR("New immediate draw operation started before ending the last one");
- return false;
- }
- if (!(elementMask & MASK_POSITION))
- {
- LOGERROR("Immediate draw operation must contain vertex positions");
- return false;
- }
- if (!vertexCount)
- return true;
-
- // Resize the buffer if it is too small
- unsigned vertexSize = VertexBuffer::GetVertexSize(elementMask);
- if (immediateVertexData_.Size() < vertexCount * vertexSize)
- immediateVertexData_.Resize(vertexCount * vertexSize);
-
- // Note: the data pointer gets pre-decremented here, because the first call to DefineVertex() will increment it
- immediateDataPtr_ = &immediateVertexData_[0] - vertexSize;
- immediateType_= type;
- immediateVertexCount_ = vertexCount;
- immediateVertexSize_ = vertexSize;
- immediateElementMask_ = elementMask;
- immediateCurrentVertex_ = 0;
-
- unsigned dataOffset = 0;
- for (unsigned i = ELEMENT_POSITION; i <= ELEMENT_TEXCOORD1; ++i)
- {
- immediateElementOffsets_[i] = dataOffset;
- if (elementMask & (1 << i))
- dataOffset += VertexBuffer::elementSize[i];
- }
-
- return true;
- }
- bool Graphics::DefineVertex(const Vector3& vertex)
- {
- if (!immediateVertexCount_ || immediateCurrentVertex_ >= immediateVertexCount_)
- return false;
-
- immediateDataPtr_ += immediateVertexSize_;
- ++immediateCurrentVertex_;
-
- float* dest = (float*)(immediateDataPtr_ + immediateElementOffsets_[ELEMENT_POSITION]);
- const float* src = vertex.GetData();
- dest[0] = src[0];
- dest[1] = src[1];
- dest[2] = src[2];
-
- return true;
- }
- bool Graphics::DefineNormal(const Vector3& normal)
- {
- if (!immediateVertexCount_ || !(immediateElementMask_ & MASK_NORMAL) || !immediateCurrentVertex_)
- return false;
-
- float* dest = (float*)(immediateDataPtr_ + immediateElementOffsets_[ELEMENT_NORMAL]);
- const float* src = normal.GetData();
- dest[0] = src[0];
- dest[1] = src[1];
- dest[2] = src[2];
-
- return true;
- }
- bool Graphics::DefineTexCoord(const Vector2& texCoord)
- {
- if (!immediateVertexCount_ || !(immediateElementMask_ & MASK_TEXCOORD1) || !immediateCurrentVertex_)
- return false;
-
- float* dest = (float*)(immediateDataPtr_ + immediateElementOffsets_[ELEMENT_TEXCOORD1]);
- const float* src = texCoord.GetData();
- dest[0] = src[0];
- dest[1] = src[1];
-
- return true;
- }
- bool Graphics::DefineColor(const Color& color)
- {
- if (!immediateVertexCount_ || !(immediateElementMask_ & MASK_COLOR) || !immediateCurrentVertex_)
- return false;
-
- unsigned* dest = (unsigned*)(immediateDataPtr_ + immediateElementOffsets_[ELEMENT_COLOR]);
- *dest = color.ToUInt();
-
- return true;
- }
- bool Graphics::DefineColor(unsigned color)
- {
- if (!immediateVertexCount_ || !(immediateElementMask_ & MASK_COLOR) || !immediateCurrentVertex_)
- return false;
-
- unsigned* dest = (unsigned*)(immediateDataPtr_ + immediateElementOffsets_[ELEMENT_COLOR]);
- *dest = color;
-
- return true;
- }
- void Graphics::EndImmediate()
- {
- if (immediateVertexCount_)
- {
- SetVertexBuffer(0);
- SetIndexBuffer(0);
-
- if (shaderProgram_)
- {
- const int* attributeLocations = shaderProgram_->GetAttributeLocations();
- unsigned vertexSize = VertexBuffer::GetVertexSize(immediateElementMask_);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- for (unsigned i = ELEMENT_POSITION; i <= ELEMENT_TEXCOORD1; ++i)
- {
- unsigned attributeIndex = attributeLocations[i];
- unsigned attributeBit = 1 << attributeIndex;
- unsigned elementBit = 1 << i;
-
- if (immediateElementMask_ & elementBit)
- {
- if ((impl_->enabledAttributes_ & attributeBit) == 0)
- {
- glEnableVertexAttribArray(attributeIndex);
- impl_->enabledAttributes_ |= attributeBit;
- }
-
- glVertexAttribPointer(attributeIndex, VertexBuffer::elementComponents[i], VertexBuffer::elementType[i],
- VertexBuffer::elementNormalize[i], vertexSize, (const GLvoid*)&immediateVertexData_[immediateElementOffsets_[i]]);
- }
- else if (impl_->enabledAttributes_ & attributeBit)
- {
- glDisableVertexAttribArray(attributeIndex);
- impl_->enabledAttributes_ &= ~attributeBit;
- }
- }
-
- Draw(immediateType_, 0, immediateVertexCount_);
- }
-
- immediateVertexCount_ = 0;
- }
- }
- void Graphics::SetForceSM2(bool enable)
- {
- }
- bool Graphics::IsInitialized() const
- {
- return impl_->window_ != 0;
- }
- unsigned char* Graphics::GetImmediateDataPtr() const
- {
- if (!immediateVertexCount_)
- {
- LOGERROR("Immediate draw operation not started");
- return 0;
- }
-
- return const_cast<unsigned char*>(&immediateVertexData_[0]);
- }
- void* Graphics::GetWindowHandle() const
- {
- return impl_->window_;
- }
- PODVector<IntVector2> Graphics::GetResolutions() const
- {
- static const unsigned MAX_MODES = 256;
- GLFWvidmode modes[MAX_MODES];
-
- unsigned count = glfwGetVideoModes(modes, MAX_MODES);
- PODVector<IntVector2> ret;
-
- for (unsigned i = 0; i < count; ++i)
- {
- int width = modes[i].width;
- int height = modes[i].height;
-
- // Store mode if unique
- bool unique = true;
- for (unsigned j = 0; j < ret.Size(); ++i)
- {
- if (ret[j].x_ == width && ret[j].y_ == height)
- {
- unique = false;
- break;
- }
- }
-
- if (unique)
- ret.Push(IntVector2(width, height));
- }
-
- return ret;
- }
- PODVector<int> Graphics::GetMultiSampleLevels() const
- {
- PODVector<int> ret;
- // No multisampling always supported
- ret.Push(1);
- /// \todo Implement properly, if possible
-
- return ret;
- }
- VertexBuffer* Graphics::GetVertexBuffer(unsigned index) const
- {
- return index < MAX_VERTEX_STREAMS ? vertexBuffers_[index] : 0;
- }
- TextureUnit Graphics::GetTextureUnit(const String& name)
- {
- Map<String, TextureUnit>::Iterator i = textureUnits_.Find(name);
- if (i != textureUnits_.End())
- return i->second_;
- else
- return MAX_TEXTURE_UNITS;
- }
- const String& Graphics::GetTextureUnitName(TextureUnit unit)
- {
- for (Map<String, TextureUnit>::Iterator i = textureUnits_.Begin(); i != textureUnits_.End(); ++i)
- {
- if (i->second_ == unit)
- return i->first_;
- }
- return noParameter;
- }
- Texture* Graphics::GetTexture(unsigned index) const
- {
- return index < MAX_TEXTURE_UNITS ? textures_[index] : 0;
- }
- RenderSurface* Graphics::GetRenderTarget(unsigned index) const
- {
- return index < MAX_RENDERTARGETS ? renderTargets_[index] : 0;
- }
- IntVector2 Graphics::GetRenderTargetDimensions() const
- {
- int width, height;
-
- if (renderTargets_[0])
- {
- width = renderTargets_[0]->GetWidth();
- height = renderTargets_[0]->GetHeight();
- }
- else if (depthStencil_)
- {
- width = depthStencil_->GetWidth();
- height = depthStencil_->GetHeight();
- }
- else
- {
- width = width_;
- height = height_;
- }
-
- return IntVector2(width, height);
- }
- void Graphics::AddGPUObject(GPUObject* object)
- {
- gpuObjects_.Push(object);
- }
- void Graphics::RemoveGPUObject(GPUObject* object)
- {
- for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
- {
- if ((*i) == object)
- {
- gpuObjects_.Erase(i);
- return;
- }
- }
- }
- unsigned Graphics::GetAlphaFormat()
- {
- return GL_ALPHA;
- }
- unsigned Graphics::GetLuminanceFormat()
- {
- return GL_LUMINANCE;
- }
- unsigned Graphics::GetLuminanceAlphaFormat()
- {
- return GL_LUMINANCE_ALPHA;
- }
- unsigned Graphics::GetRGBFormat()
- {
- return GL_RGB;
- }
- unsigned Graphics::GetRGBAFormat()
- {
- return GL_RGBA;
- }
- unsigned Graphics::GetDepthFormat()
- {
- return GL_DEPTH_COMPONENT24;
- }
- unsigned Graphics::GetDepthStencilFormat()
- {
- return GL_DEPTH24_STENCIL8_EXT;
- }
- void Graphics::CreateRenderTargets()
- {
- if (mode_ != RENDER_FORWARD)
- {
- if (!diffBuffer_)
- {
- diffBuffer_ = new Texture2D(context_);
- diffBuffer_->SetSize(0, 0, GetRGBAFormat(), TEXTURE_RENDERTARGET);
- }
-
- if (!normalBuffer_)
- {
- normalBuffer_ = new Texture2D(context_);
- normalBuffer_->SetSize(0, 0, GetRGBAFormat(), TEXTURE_RENDERTARGET);
- }
-
- if (!depthBuffer_)
- {
- depthBuffer_ = new Texture2D(context_);
- depthBuffer_->SetSize(0, 0, GetDepthFormat(), TEXTURE_DEPTHSTENCIL);
- }
-
- // If deferred antialiasing is used, reserve screen buffer
- // (later we will probably want the screen buffer reserved in any case, to do for example distortion effects,
- // which will also be useful in forward rendering)
- if (multiSample_ > 1)
- {
- screenBuffer_ = new Texture2D(context_);
- screenBuffer_->SetSize(0, 0, GetRGBAFormat(), TEXTURE_RENDERTARGET);
- screenBuffer_->SetFilterMode(FILTER_BILINEAR);
- }
- else
- screenBuffer_.Reset();
- }
- else
- {
- diffBuffer_.Reset();
- normalBuffer_.Reset();
- depthBuffer_.Reset();
- screenBuffer_.Reset();
- }
- }
- void Graphics::ResetCachedState()
- {
- for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
- {
- vertexBuffers_[i] = 0;
- elementMasks_[i] = 0;
- }
-
- for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
- {
- textures_[i] = 0;
- textureTypes_[i] = 0;
- }
-
- for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
- renderTargets_[i] = 0;
-
- depthStencil_ = 0;
- viewport_ = IntRect(0, 0, 0, 0);
- viewTexture_ = 0;
- indexBuffer_ = 0;
- vertexShader_ = 0;
- pixelShader_ = 0;
- shaderProgram_ = 0;
- blendMode_ = BLEND_REPLACE;
- alphaTest_ = false;
- alphaTestMode_ = CMP_ALWAYS;
- alphaRef_ = 0.0f;
- textureAnisotropy_ = 1;
- colorWrite_ = true;
- cullMode_ = CULL_NONE;
- constantDepthBias_ = 0.0f;
- slopeScaledDepthBias_ = 0.0f;
- depthTestMode_ = CMP_ALWAYS;
- depthWrite_ = true;
- fillMode_ = FILL_SOLID;
- scissorTest_ = false;
- scissorRect_ = IntRect::ZERO;
- stencilTest_ = false;
- stencilTestMode_ = CMP_ALWAYS;
- stencilPass_ = OP_KEEP;
- stencilFail_ = OP_KEEP;
- stencilZFail_ = OP_KEEP;
- stencilRef_ = 0;
- stencilMask_ = M_MAX_UNSIGNED;
-
- impl_->activeTexture_ = 0;
- impl_->drawBuffers_ = M_MAX_UNSIGNED;
- impl_->enabledAttributes_ = 0;
- impl_->fboBound_ = false;
- }
- void Graphics::SetDrawBuffers()
- {
- // Calculate the bit combination of non-zero color rendertargets to first check if the combination changed
- unsigned newDrawBuffers = 0;
- for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
- {
- if (renderTargets_[i])
- newDrawBuffers |= 1 << i;
- }
- if (newDrawBuffers == impl_->drawBuffers_)
- return;
-
- // Check for no color rendertargets (depth rendering only)
- if (!newDrawBuffers)
- glDrawBuffer(GL_NONE);
- else
- {
- int drawBufferIds[4];
- unsigned drawBufferCount = 0;
-
- for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
- {
- if (renderTargets_[i])
- drawBufferIds[drawBufferCount++] = GL_COLOR_ATTACHMENT0_EXT + i;
- }
- glDrawBuffers(drawBufferCount, (const GLenum*)drawBufferIds);
- }
-
- glReadBuffer(GL_NONE);
- }
- void Graphics::Release()
- {
- if (!impl_->window_)
- return;
-
- diffBuffer_.Reset();
- normalBuffer_.Reset();
- depthBuffer_.Reset();
- screenBuffer_.Reset();
- depthTextures_.Clear();
-
- // If GPU objects exist ie. it's a context delete/recreate, not Close(), tell them to save and release themselves
- for (Vector<GPUObject*>::Iterator i = gpuObjects_.Begin(); i != gpuObjects_.End(); ++i)
- (*i)->OnDeviceLost();
-
- if (impl_->fbo_)
- {
- glDeleteFramebuffersEXT(1, &impl_->fbo_);
- impl_->fbo_ = 0;
- }
-
- // When the new context is initialized, it will have default state again
- ResetCachedState();
- ClearParameterSources();
-
- {
- MutexLock lock(GetStaticMutex());
-
- SetWindowContext(impl_->window_, 0);
- glfwCloseWindow(impl_->window_);
- impl_->window_ = 0;
- }
- }
- void Graphics::SetTextureUnitMappings()
- {
- // Map texture units
- textureUnits_["NormalMap"] = TU_NORMAL;
- textureUnits_["DiffMap"] = TU_DIFFUSE;
- textureUnits_["DiffCubeMap"] = TU_DIFFUSE;
- textureUnits_["SpecMap"] = TU_SPECULAR;
- textureUnits_["EmissiveMap"] = TU_EMISSIVE;
- textureUnits_["DetailMap"] = TU_DETAIL;
- textureUnits_["EnvironmentMap"] = TU_ENVIRONMENT;
- textureUnits_["EnvironmentCubeMap"] = TU_ENVIRONMENT;
- textureUnits_["LightRampMap"] = TU_LIGHTRAMP;
- textureUnits_["LightSpotMap"] = TU_LIGHTSPOT;
- textureUnits_["LightCubeMap"] = TU_LIGHTSPOT;
- textureUnits_["ShadowMap"] = TU_SHADOWMAP;
- textureUnits_["DiffBuffer"] = TU_DIFFBUFFER;
- textureUnits_["NormalBuffer"] = TU_NORMALBUFFER;
- textureUnits_["DepthBuffer"] = TU_DEPTHBUFFER;
- }
- void RegisterGraphicsLibrary(Context* context)
- {
- Animation::RegisterObject(context);
- Material::RegisterObject(context);
- Model::RegisterObject(context);
- Shader::RegisterObject(context);
- Technique::RegisterObject(context);
- Texture2D::RegisterObject(context);
- TextureCube::RegisterObject(context);
- Camera::RegisterObject(context);
- Drawable::RegisterObject(context);
- Light::RegisterObject(context);
- StaticModel::RegisterObject(context);
- Skybox::RegisterObject(context);
- AnimatedModel::RegisterObject(context);
- AnimationController::RegisterObject(context);
- BillboardSet::RegisterObject(context);
- ParticleEmitter::RegisterObject(context);
- DebugRenderer::RegisterObject(context);
- Octree::RegisterObject(context);
- Zone::RegisterObject(context);
- }
|