123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355 |
- /**
- * Copyright (c) 2006-2022 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
- // LOVE
- #include "Graphics.h"
- #include "Buffer.h"
- #include "math/MathModule.h"
- #include "data/DataModule.h"
- #include "Polyline.h"
- #include "font/Font.h"
- #include "window/Window.h"
- #include "SpriteBatch.h"
- #include "ParticleSystem.h"
- #include "Font.h"
- #include "Video.h"
- #include "Text.h"
- #include "common/deprecation.h"
- #include "common/config.h"
- // C++
- #include <algorithm>
- #include <stdlib.h>
- namespace love
- {
- namespace graphics
- {
- static bool gammaCorrect = false;
- static bool debugMode = false;
- static bool debugModeQueried = false;
- void setGammaCorrect(bool gammacorrect)
- {
- gammaCorrect = gammacorrect;
- }
- bool isGammaCorrect()
- {
- return gammaCorrect;
- }
- void gammaCorrectColor(Colorf &c)
- {
- if (isGammaCorrect())
- {
- c.r = math::gammaToLinear(c.r);
- c.g = math::gammaToLinear(c.g);
- c.b = math::gammaToLinear(c.b);
- }
- }
- Colorf gammaCorrectColor(const Colorf &c)
- {
- Colorf r = c;
- gammaCorrectColor(r);
- return r;
- }
- void unGammaCorrectColor(Colorf &c)
- {
- if (isGammaCorrect())
- {
- c.r = math::linearToGamma(c.r);
- c.g = math::linearToGamma(c.g);
- c.b = math::linearToGamma(c.b);
- }
- }
- Colorf unGammaCorrectColor(const Colorf &c)
- {
- Colorf r = c;
- unGammaCorrectColor(r);
- return r;
- }
- bool isDebugEnabled()
- {
- if (!debugModeQueried)
- {
- const char *debugenv = getenv("LOVE_GRAPHICS_DEBUG");
- debugMode = debugenv != nullptr && debugenv[0] != '0';
- debugModeQueried = true;
- }
- return debugMode;
- }
- love::Type Graphics::type("graphics", &Module::type);
- namespace opengl { extern love::graphics::Graphics *createInstance(); }
- #ifdef LOVE_GRAPHICS_METAL
- namespace metal { extern love::graphics::Graphics *createInstance(); }
- #endif
- static const Renderer rendererOrder[] = {
- RENDERER_OPENGL,
- RENDERER_METAL,
- };
- static std::vector<Renderer> defaultRenderers =
- {
- RENDERER_METAL,
- RENDERER_OPENGL,
- };
- static std::vector<Renderer> _renderers = defaultRenderers;
- const std::vector<Renderer> &getDefaultRenderers()
- {
- return defaultRenderers;
- }
- const std::vector<Renderer> &getRenderers()
- {
- return _renderers;
- }
- void setRenderers(const std::vector<Renderer> &renderers)
- {
- _renderers = renderers;
- }
- Graphics *Graphics::createInstance()
- {
- Graphics *instance = Module::getInstance<Graphics>(M_GRAPHICS);
- if (instance != nullptr)
- instance->retain();
- else
- {
- for (auto r : rendererOrder)
- {
- if (std::find(_renderers.begin(), _renderers.end(), r) == _renderers.end())
- continue;
- if (r == RENDERER_OPENGL)
- instance = opengl::createInstance();
- #ifdef LOVE_GRAPHICS_METAL
- if (r == RENDERER_METAL)
- instance = metal::createInstance();
- #endif
- if (instance != nullptr)
- break;
- }
- }
- return instance;
- }
- Graphics::DisplayState::DisplayState()
- {
- defaultSamplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_LINEAR;
- }
- Graphics::Graphics()
- : width(0)
- , height(0)
- , pixelWidth(0)
- , pixelHeight(0)
- , created(false)
- , active(true)
- , batchedDrawState()
- , deviceProjectionMatrix()
- , renderTargetSwitchCount(0)
- , drawCalls(0)
- , drawCallsBatched(0)
- , quadIndexBuffer(nullptr)
- , capabilities()
- , cachedShaderStages()
- {
- transformStack.reserve(16);
- transformStack.push_back(Matrix4());
- pixelScaleStack.reserve(16);
- pixelScaleStack.push_back(1);
- states.reserve(10);
- states.push_back(DisplayState());
- if (!Shader::initialize())
- throw love::Exception("Shader support failed to initialize!");
- }
- Graphics::~Graphics()
- {
- delete quadIndexBuffer;
- // Clean up standard shaders before the active shader. If we do it after,
- // the active shader may try to activate a standard shader when deactivating
- // itself, which will cause problems since it calls Graphics methods in the
- // Graphics destructor.
- for (int i = 0; i < Shader::STANDARD_MAX_ENUM; i++)
- {
- if (Shader::standardShaders[i])
- {
- Shader::standardShaders[i]->release();
- Shader::standardShaders[i] = nullptr;
- }
- }
- states.clear();
- defaultFont.set(nullptr);
- if (batchedDrawState.vb[0])
- batchedDrawState.vb[0]->release();
- if (batchedDrawState.vb[1])
- batchedDrawState.vb[1]->release();
- if (batchedDrawState.indexBuffer)
- batchedDrawState.indexBuffer->release();
- for (int i = 0; i < (int) SHADERSTAGE_MAX_ENUM; i++)
- cachedShaderStages[i].clear();
- Shader::deinitialize();
- }
- void Graphics::createQuadIndexBuffer()
- {
- if (quadIndexBuffer != nullptr)
- return;
- size_t size = sizeof(uint16) * (LOVE_UINT16_MAX / 4) * 6;
- Buffer::Settings settings(BUFFERUSAGEFLAG_INDEX, BUFFERDATAUSAGE_STATIC);
- quadIndexBuffer = newBuffer(settings, DATAFORMAT_UINT16, nullptr, size, 0);
- Buffer::Mapper map(*quadIndexBuffer);
- fillIndices(TRIANGLEINDEX_QUADS, 0, LOVE_UINT16_MAX, (uint16 *) map.data);
- quadIndexBuffer->setImmutable(true);
- }
- Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
- {
- return new Quad(v, sw, sh);
- }
- Font *Graphics::newFont(love::font::Rasterizer *data)
- {
- return new Font(data, states.back().defaultSamplerState);
- }
- Font *Graphics::newDefaultFont(int size, font::TrueTypeRasterizer::Hinting hinting)
- {
- auto fontmodule = Module::getInstance<font::Font>(M_FONT);
- if (!fontmodule)
- throw love::Exception("Font module has not been loaded.");
- StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(size, hinting), Acquire::NORETAIN);
- return newFont(r.get());
- }
- Video *Graphics::newVideo(love::video::VideoStream *stream, float dpiscale)
- {
- return new Video(this, stream, dpiscale);
- }
- love::graphics::SpriteBatch *Graphics::newSpriteBatch(Texture *texture, int size, BufferDataUsage usage)
- {
- return new SpriteBatch(this, texture, size, usage);
- }
- love::graphics::ParticleSystem *Graphics::newParticleSystem(Texture *texture, int size)
- {
- return new ParticleSystem(texture, size);
- }
- ShaderStage *Graphics::newShaderStage(ShaderStageType stage, const std::string &source, const Shader::SourceInfo &info)
- {
- ShaderStage *s = nullptr;
- std::string cachekey;
- if (!source.empty())
- {
- data::HashFunction::Value hashvalue;
- data::hash(data::HashFunction::FUNCTION_SHA1, source.c_str(), source.size(), hashvalue);
- cachekey = std::string(hashvalue.data, hashvalue.size);
- auto it = cachedShaderStages[stage].find(cachekey);
- if (it != cachedShaderStages[stage].end())
- {
- s = it->second;
- s->retain();
- }
- }
- if (s == nullptr)
- {
- bool glsles = usesGLSLES();
- std::string glsl = Shader::createShaderStageCode(this, stage, source, info, glsles, true);
- s = newShaderStageInternal(stage, cachekey, glsl, glsles);
- if (!cachekey.empty())
- cachedShaderStages[stage][cachekey] = s;
- }
- return s;
- }
- Shader *Graphics::newShader(const std::vector<std::string> &stagessource)
- {
- StrongRef<ShaderStage> stages[SHADERSTAGE_MAX_ENUM] = {};
- bool validstages[SHADERSTAGE_MAX_ENUM] = {};
- validstages[SHADERSTAGE_VERTEX] = true;
- validstages[SHADERSTAGE_PIXEL] = true;
- for (const std::string &source : stagessource)
- {
- Shader::SourceInfo info = Shader::getSourceInfo(source);
- bool isanystage = false;
- for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++)
- {
- if (!validstages[i])
- continue;
- if (info.stages[i] != Shader::ENTRYPOINT_NONE)
- {
- isanystage = true;
- stages[i].set(newShaderStage((ShaderStageType) i, source, info), Acquire::NORETAIN);
- }
- }
- if (!isanystage)
- throw love::Exception("Could not parse shader code (missing 'position' or 'effect' function?)");
- }
- for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++)
- {
- auto stype = (ShaderStageType) i;
- if (validstages[i] && stages[i].get() == nullptr)
- {
- const std::string &source = Shader::getDefaultCode(Shader::STANDARD_DEFAULT, stype);
- Shader::SourceInfo info = Shader::getSourceInfo(source);
- stages[i].set(newShaderStage(stype, source, info), Acquire::NORETAIN);
- }
- }
- return newShaderInternal(stages);
- }
- Shader *Graphics::newComputeShader(const std::string &source)
- {
- Shader::SourceInfo info = Shader::getSourceInfo(source);
- if (info.stages[SHADERSTAGE_COMPUTE] == Shader::ENTRYPOINT_NONE)
- throw love::Exception("Could not parse compute shader code (missing 'computemain' function?)");
- StrongRef<ShaderStage> stages[SHADERSTAGE_MAX_ENUM];
- stages[SHADERSTAGE_COMPUTE].set(newShaderStage(SHADERSTAGE_COMPUTE, source, info));
- return newShaderInternal(stages);
- }
- Buffer *Graphics::newBuffer(const Buffer::Settings &settings, DataFormat format, const void *data, size_t size, size_t arraylength)
- {
- std::vector<Buffer::DataDeclaration> dataformat = {{"", format, 0}};
- return newBuffer(settings, dataformat, data, size, arraylength);
- }
- Mesh *Graphics::newMesh(const std::vector<Buffer::DataDeclaration> &vertexformat, int vertexcount, PrimitiveType drawmode, BufferDataUsage usage)
- {
- return new Mesh(this, vertexformat, vertexcount, drawmode, usage);
- }
- Mesh *Graphics::newMesh(const std::vector<Buffer::DataDeclaration> &vertexformat, const void *data, size_t datasize, PrimitiveType drawmode, BufferDataUsage usage)
- {
- return new Mesh(this, vertexformat, data, datasize, drawmode, usage);
- }
- Mesh *Graphics::newMesh(const std::vector<Mesh::BufferAttribute> &attributes, PrimitiveType drawmode)
- {
- return new Mesh(attributes, drawmode);
- }
- love::graphics::Text *Graphics::newText(graphics::Font *font, const std::vector<Font::ColoredString> &text)
- {
- return new Text(font, text);
- }
- void Graphics::cleanupCachedShaderStage(ShaderStageType type, const std::string &hashkey)
- {
- cachedShaderStages[type].erase(hashkey);
- }
- bool Graphics::validateShader(bool gles, const std::vector<std::string> &stagessource, std::string &err)
- {
- StrongRef<ShaderStage> stages[SHADERSTAGE_MAX_ENUM] = {};
- bool validstages[SHADERSTAGE_MAX_ENUM] = {};
- validstages[SHADERSTAGE_VERTEX] = true;
- validstages[SHADERSTAGE_PIXEL] = true;
- validstages[SHADERSTAGE_COMPUTE] = true;
- // Don't use cached shader stages, since the gles flag may not match the
- // current renderer.
- for (const std::string &source : stagessource)
- {
- Shader::SourceInfo info = Shader::getSourceInfo(source);
- bool isanystage = false;
- for (int i = 0; i < SHADERSTAGE_MAX_ENUM; i++)
- {
- auto stype = (ShaderStageType) i;
- if (!validstages[i])
- continue;
- if (info.stages[i] != Shader::ENTRYPOINT_NONE)
- {
- isanystage = true;
- std::string glsl = Shader::createShaderStageCode(this, stype, source, info, gles, false);
- stages[i].set(new ShaderStageForValidation(this, stype, glsl, gles), Acquire::NORETAIN);
- }
- }
- if (!isanystage)
- {
- err = "Could not parse shader code (missing 'position' or 'effect' function?)";
- return false;
- }
- }
- return Shader::validate(stages, err);
- }
- int Graphics::getWidth() const
- {
- return width;
- }
- int Graphics::getHeight() const
- {
- return height;
- }
- int Graphics::getPixelWidth() const
- {
- return pixelWidth;
- }
- int Graphics::getPixelHeight() const
- {
- return pixelHeight;
- }
- double Graphics::getCurrentDPIScale() const
- {
- const auto &rt = states.back().renderTargets.getFirstTarget();
- if (rt.texture.get())
- return rt.texture->getDPIScale();
- return getScreenDPIScale();
- }
- double Graphics::getScreenDPIScale() const
- {
- return (double) getPixelHeight() / (double) getHeight();
- }
- bool Graphics::isCreated() const
- {
- return created;
- }
- bool Graphics::isActive() const
- {
- // The graphics module is only completely 'active' if there's a window, a
- // context, and the active variable is set.
- auto window = getInstance<love::window::Window>(M_WINDOW);
- return active && isCreated() && window != nullptr && window->isOpen();
- }
- void Graphics::reset()
- {
- DisplayState s;
- stopDrawToStencilBuffer();
- restoreState(s);
- origin();
- }
- /**
- * State functions.
- **/
- void Graphics::restoreState(const DisplayState &s)
- {
- setColor(s.color);
- setBackgroundColor(s.backgroundColor);
- setBlendState(s.blend);
- setLineWidth(s.lineWidth);
- setLineStyle(s.lineStyle);
- setLineJoin(s.lineJoin);
- setPointSize(s.pointSize);
- if (s.scissor)
- setScissor(s.scissorRect);
- else
- setScissor();
- if (s.stencil.action != STENCIL_KEEP)
- drawToStencilBuffer(s.stencil.action, s.stencil.value);
- else
- stopDrawToStencilBuffer();
- setStencilTest(s.stencil.compare, s.stencil.value);
- setDepthMode(s.depthTest, s.depthWrite);
- setMeshCullMode(s.meshCullMode);
- setFrontFaceWinding(s.winding);
- setFont(s.font.get());
- setShader(s.shader.get());
- setRenderTargets(s.renderTargets);
- setColorMask(s.colorMask);
- setWireframe(s.wireframe);
- setDefaultSamplerState(s.defaultSamplerState);
- if (s.useCustomProjection)
- updateDeviceProjection(s.customProjection);
- else
- resetProjection();
- }
- void Graphics::restoreStateChecked(const DisplayState &s)
- {
- const DisplayState &cur = states.back();
- if (s.color != cur.color)
- setColor(s.color);
- setBackgroundColor(s.backgroundColor);
- if (!(s.blend == cur.blend))
- setBlendState(s.blend);
- // These are just simple assignments.
- setLineWidth(s.lineWidth);
- setLineStyle(s.lineStyle);
- setLineJoin(s.lineJoin);
- if (s.pointSize != cur.pointSize)
- setPointSize(s.pointSize);
- if (s.scissor != cur.scissor || (s.scissor && !(s.scissorRect == cur.scissorRect)))
- {
- if (s.scissor)
- setScissor(s.scissorRect);
- else
- setScissor();
- }
- if (s.stencil.action != cur.stencil.action)
- {
- if (s.stencil.action != STENCIL_KEEP)
- drawToStencilBuffer(s.stencil.action, s.stencil.value);
- else
- stopDrawToStencilBuffer();
- }
- if (s.stencil.compare != cur.stencil.compare || s.stencil.value != cur.stencil.value)
- setStencilTest(s.stencil.compare, s.stencil.value);
- if (s.depthTest != cur.depthTest || s.depthWrite != cur.depthWrite)
- setDepthMode(s.depthTest, s.depthWrite);
- setMeshCullMode(s.meshCullMode);
- if (s.winding != cur.winding)
- setFrontFaceWinding(s.winding);
- setFont(s.font.get());
- setShader(s.shader.get());
- const auto &sRTs = s.renderTargets;
- const auto &curRTs = cur.renderTargets;
- bool rtschanged = sRTs.colors.size() != curRTs.colors.size();
- if (!rtschanged)
- {
- for (size_t i = 0; i < sRTs.colors.size() && i < curRTs.colors.size(); i++)
- {
- if (sRTs.colors[i] != curRTs.colors[i])
- {
- rtschanged = true;
- break;
- }
- }
- if (!rtschanged && sRTs.depthStencil != curRTs.depthStencil)
- rtschanged = true;
- if (sRTs.temporaryRTFlags != curRTs.temporaryRTFlags)
- rtschanged = true;
- }
- if (rtschanged)
- setRenderTargets(s.renderTargets);
- if (s.colorMask != cur.colorMask)
- setColorMask(s.colorMask);
- if (s.wireframe != cur.wireframe)
- setWireframe(s.wireframe);
- setDefaultSamplerState(s.defaultSamplerState);
- if (s.useCustomProjection)
- setCustomProjection(s.customProjection);
- else if (cur.useCustomProjection)
- resetProjection();
- }
- Colorf Graphics::getColor() const
- {
- return states.back().color;
- }
- void Graphics::setBackgroundColor(Colorf c)
- {
- states.back().backgroundColor = c;
- }
- Colorf Graphics::getBackgroundColor() const
- {
- return states.back().backgroundColor;
- }
- void Graphics::checkSetDefaultFont()
- {
- // We don't create or set the default Font if an existing font is in use.
- if (states.back().font.get() != nullptr)
- return;
- // Create a new default font if we don't have one yet.
- if (!defaultFont.get())
- defaultFont.set(newDefaultFont(13, font::TrueTypeRasterizer::HINTING_NORMAL), Acquire::NORETAIN);
- states.back().font.set(defaultFont.get());
- }
- void Graphics::setFont(love::graphics::Font *font)
- {
- // We don't need to set a default font here if null is passed in, since we
- // only care about the default font in getFont and print.
- DisplayState &state = states.back();
- state.font.set(font);
- }
- love::graphics::Font *Graphics::getFont()
- {
- checkSetDefaultFont();
- return states.back().font.get();
- }
- void Graphics::setShader(love::graphics::Shader *shader)
- {
- if (shader == nullptr)
- return setShader();
- shader->attach();
- states.back().shader.set(shader);
- }
- void Graphics::setShader()
- {
- Shader::attachDefault(Shader::STANDARD_DEFAULT);
- states.back().shader.set(nullptr);
- }
- love::graphics::Shader *Graphics::getShader() const
- {
- return states.back().shader.get();
- }
- void Graphics::setRenderTarget(RenderTarget rt, uint32 temporaryRTFlags)
- {
- if (rt.texture == nullptr)
- return setRenderTarget();
- RenderTargets rts;
- rts.colors.push_back(rt);
- rts.temporaryRTFlags = temporaryRTFlags;
- setRenderTargets(rts);
- }
- void Graphics::setRenderTargets(const RenderTargetsStrongRef &rts)
- {
- RenderTargets targets;
- targets.colors.reserve(rts.colors.size());
- for (const auto &rt : rts.colors)
- targets.colors.emplace_back(rt.texture.get(), rt.slice, rt.mipmap);
- targets.depthStencil = RenderTarget(rts.depthStencil.texture, rts.depthStencil.slice, rts.depthStencil.mipmap);
- targets.temporaryRTFlags = rts.temporaryRTFlags;
- return setRenderTargets(targets);
- }
- void Graphics::setRenderTargets(const RenderTargets &rts)
- {
- DisplayState &state = states.back();
- int rtcount = (int) rts.colors.size();
- RenderTarget firsttarget = rts.getFirstTarget();
- Texture *firsttex = firsttarget.texture;
- if (firsttex == nullptr)
- return setRenderTarget();
- const auto &prevRTs = state.renderTargets;
- if (rtcount == (int) prevRTs.colors.size())
- {
- bool modified = false;
- for (int i = 0; i < rtcount; i++)
- {
- if (rts.colors[i] != prevRTs.colors[i])
- {
- modified = true;
- break;
- }
- }
- if (!modified && rts.depthStencil != prevRTs.depthStencil)
- modified = true;
- if (rts.temporaryRTFlags != prevRTs.temporaryRTFlags)
- modified = true;
- if (!modified)
- return;
- }
- if (rtcount > capabilities.limits[LIMIT_RENDER_TARGETS])
- throw love::Exception("This system can't simultaneously render to %d textures.", rtcount);
- bool multiformatsupported = capabilities.features[FEATURE_MULTI_RENDER_TARGET_FORMATS];
- PixelFormat firstcolorformat = PIXELFORMAT_UNKNOWN;
- if (!rts.colors.empty())
- firstcolorformat = rts.colors[0].texture->getPixelFormat();
- if (!firsttex->isRenderTarget())
- throw love::Exception("Texture must be created as a render target to be used in setRenderTargets.");
- if (isPixelFormatDepthStencil(firstcolorformat))
- throw love::Exception("Depth/stencil format textures must be used with the 'depthstencil' field of the table passed into setRenderTargets.");
- if (firsttarget.mipmap < 0 || firsttarget.mipmap >= firsttex->getMipmapCount())
- throw love::Exception("Invalid mipmap level %d.", firsttarget.mipmap + 1);
- if (!firsttex->isValidSlice(firsttarget.slice))
- throw love::Exception("Invalid slice index: %d.", firsttarget.slice + 1);
- bool hasSRGBtexture = isPixelFormatSRGB(firstcolorformat);
- int pixelw = firsttex->getPixelWidth(firsttarget.mipmap);
- int pixelh = firsttex->getPixelHeight(firsttarget.mipmap);
- int reqmsaa = firsttex->getRequestedMSAA();
- for (int i = 1; i < rtcount; i++)
- {
- Texture *c = rts.colors[i].texture;
- PixelFormat format = c->getPixelFormat();
- int mip = rts.colors[i].mipmap;
- int slice = rts.colors[i].slice;
- if (!c->isRenderTarget())
- throw love::Exception("Texture must be created as a render target to be used in setRenderTargets.");
- if (mip < 0 || mip >= c->getMipmapCount())
- throw love::Exception("Invalid mipmap level %d.", mip + 1);
- if (!c->isValidSlice(slice))
- throw love::Exception("Invalid slice index: %d.", slice + 1);
- if (c->getPixelWidth(mip) != pixelw || c->getPixelHeight(mip) != pixelh)
- throw love::Exception("All textures must have the same pixel dimensions.");
- if (!multiformatsupported && format != firstcolorformat)
- throw love::Exception("This system doesn't support multi-render-target rendering with different texture formats.");
- if (c->getRequestedMSAA() != reqmsaa)
- throw love::Exception("All textures must have the same MSAA value.");
- if (isPixelFormatDepthStencil(format))
- throw love::Exception("Depth/stencil format textures must be used with the 'depthstencil' field of the table passed into setRenderTargets.");
- if (isPixelFormatSRGB(format))
- hasSRGBtexture = true;
- }
- if (rts.depthStencil.texture != nullptr)
- {
- Texture *c = rts.depthStencil.texture;
- int mip = rts.depthStencil.mipmap;
- int slice = rts.depthStencil.slice;
- if (!c->isRenderTarget())
- throw love::Exception("Texture must be created as a render target to be used in setRenderTargets.");
- if (!isPixelFormatDepthStencil(c->getPixelFormat()))
- throw love::Exception("Only depth/stencil format textures can be used with the 'depthstencil' field of the table passed into setRenderTargets.");
- if (c->getPixelWidth(mip) != pixelw || c->getPixelHeight(mip) != pixelh)
- throw love::Exception("All Textures must have the same pixel dimensions.");
- if (c->getRequestedMSAA() != firsttex->getRequestedMSAA())
- throw love::Exception("All Textures must have the same MSAA value.");
- if (mip < 0 || mip >= c->getMipmapCount())
- throw love::Exception("Invalid mipmap level %d.", mip + 1);
- if (!c->isValidSlice(slice))
- throw love::Exception("Invalid slice index: %d.", slice + 1);
- }
- int w = firsttex->getWidth(firsttarget.mipmap);
- int h = firsttex->getHeight(firsttarget.mipmap);
- flushBatchedDraws();
- if (rts.depthStencil.texture == nullptr && rts.temporaryRTFlags != 0)
- {
- bool wantsdepth = (rts.temporaryRTFlags & TEMPORARY_RT_DEPTH) != 0;
- bool wantsstencil = (rts.temporaryRTFlags & TEMPORARY_RT_STENCIL) != 0;
- PixelFormat dsformat = PIXELFORMAT_STENCIL8;
- if (wantsdepth && wantsstencil)
- dsformat = PIXELFORMAT_DEPTH24_UNORM_STENCIL8;
- else if (wantsdepth && isPixelFormatSupported(PIXELFORMAT_DEPTH24_UNORM, PIXELFORMATUSAGEFLAGS_RENDERTARGET, false))
- dsformat = PIXELFORMAT_DEPTH24_UNORM;
- else if (wantsdepth)
- dsformat = PIXELFORMAT_DEPTH16_UNORM;
- else if (wantsstencil)
- dsformat = PIXELFORMAT_STENCIL8;
- // We want setRenderTargetsInternal to have a pointer to the temporary RT,
- // but we don't want to directly store it in the main graphics state.
- RenderTargets realRTs = rts;
- realRTs.depthStencil.texture = getTemporaryTexture(dsformat, pixelw, pixelh, reqmsaa);
- realRTs.depthStencil.slice = 0;
- setRenderTargetsInternal(realRTs, w, h, pixelw, pixelh, hasSRGBtexture);
- }
- else
- setRenderTargetsInternal(rts, w, h, pixelw, pixelh, hasSRGBtexture);
- RenderTargetsStrongRef refs;
- refs.colors.reserve(rts.colors.size());
- for (auto c : rts.colors)
- refs.colors.emplace_back(c.texture, c.slice, c.mipmap);
- refs.depthStencil = RenderTargetStrongRef(rts.depthStencil.texture, rts.depthStencil.slice);
- refs.temporaryRTFlags = rts.temporaryRTFlags;
- std::swap(state.renderTargets, refs);
- renderTargetSwitchCount++;
- resetProjection();
- }
- void Graphics::setRenderTarget()
- {
- DisplayState &state = states.back();
- if (state.renderTargets.colors.empty() && state.renderTargets.depthStencil.texture == nullptr)
- return;
- flushBatchedDraws();
- setRenderTargetsInternal(RenderTargets(), width, height, pixelWidth, pixelHeight, isGammaCorrect());
- state.renderTargets = RenderTargetsStrongRef();
- renderTargetSwitchCount++;
- resetProjection();
- }
- Graphics::RenderTargets Graphics::getRenderTargets() const
- {
- const auto &curRTs = states.back().renderTargets;
- RenderTargets rts;
- rts.colors.reserve(curRTs.colors.size());
- for (const auto &rt : curRTs.colors)
- rts.colors.emplace_back(rt.texture.get(), rt.slice, rt.mipmap);
- rts.depthStencil = RenderTarget(curRTs.depthStencil.texture, curRTs.depthStencil.slice, curRTs.depthStencil.mipmap);
- rts.temporaryRTFlags = curRTs.temporaryRTFlags;
- return rts;
- }
- bool Graphics::isRenderTargetActive() const
- {
- const auto &rts = states.back().renderTargets;
- return !rts.colors.empty() || rts.depthStencil.texture != nullptr;
- }
- bool Graphics::isRenderTargetActive(Texture *texture) const
- {
- const auto &rts = states.back().renderTargets;
- for (const auto &rt : rts.colors)
- {
- if (rt.texture.get() == texture)
- return true;
- }
- if (rts.depthStencil.texture.get() == texture)
- return true;
- return false;
- }
- bool Graphics::isRenderTargetActive(Texture *texture, int slice) const
- {
- const auto &rts = states.back().renderTargets;
- for (const auto &rt : rts.colors)
- {
- if (rt.texture.get() == texture && rt.slice == slice)
- return true;
- }
- if (rts.depthStencil.texture.get() == texture && rts.depthStencil.slice == slice)
- return true;
- return false;
- }
- Texture *Graphics::getTemporaryTexture(PixelFormat format, int w, int h, int samples)
- {
- Texture *texture = nullptr;
- for (TemporaryTexture &temp : temporaryTextures)
- {
- Texture *c = temp.texture;
- if (c->getPixelFormat() == format && c->getPixelWidth() == w
- && c->getPixelHeight() == h && c->getRequestedMSAA() == samples)
- {
- texture = c;
- temp.framesSinceUse = 0;
- break;
- }
- }
- if (texture == nullptr)
- {
- Texture::Settings settings;
- settings.renderTarget = true;
- settings.format = format;
- settings.width = w;
- settings.height = h;
- settings.msaa = samples;
- texture = newTexture(settings);
- temporaryTextures.emplace_back(texture);
- }
- return texture;
- }
- void Graphics::intersectScissor(const Rect &rect)
- {
- Rect currect = states.back().scissorRect;
- if (!states.back().scissor)
- {
- currect.x = 0;
- currect.y = 0;
- currect.w = std::numeric_limits<int>::max();
- currect.h = std::numeric_limits<int>::max();
- }
- int x1 = std::max(currect.x, rect.x);
- int y1 = std::max(currect.y, rect.y);
- int x2 = std::min(currect.x + currect.w, rect.x + rect.w);
- int y2 = std::min(currect.y + currect.h, rect.y + rect.h);
- Rect newrect = {x1, y1, std::max(0, x2 - x1), std::max(0, y2 - y1)};
- setScissor(newrect);
- }
- bool Graphics::getScissor(Rect &rect) const
- {
- const DisplayState &state = states.back();
- rect = state.scissorRect;
- return state.scissor;
- }
- void Graphics::setStencilTest()
- {
- setStencilTest(COMPARE_ALWAYS, 0);
- }
- void Graphics::getStencilTest(CompareMode &compare, int &value) const
- {
- const DisplayState &state = states.back();
- compare = state.stencil.compare;
- value = state.stencil.value;
- }
- void Graphics::setDepthMode()
- {
- setDepthMode(COMPARE_ALWAYS, false);
- }
- void Graphics::getDepthMode(CompareMode &compare, bool &write) const
- {
- const DisplayState &state = states.back();
- compare = state.depthTest;
- write = state.depthWrite;
- }
- void Graphics::setMeshCullMode(CullMode cull)
- {
- // Handled inside the draw() graphics API implementations.
- states.back().meshCullMode = cull;
- }
- CullMode Graphics::getMeshCullMode() const
- {
- return states.back().meshCullMode;
- }
- Winding Graphics::getFrontFaceWinding() const
- {
- return states.back().winding;
- }
- ColorChannelMask Graphics::getColorMask() const
- {
- return states.back().colorMask;
- }
- void Graphics::setBlendMode(BlendMode mode, BlendAlpha alphamode)
- {
- if (alphamode == BLENDALPHA_MULTIPLY && !isAlphaMultiplyBlendSupported(mode))
- {
- const char *modestr = "unknown";
- love::graphics::getConstant(mode, modestr);
- throw love::Exception("The '%s' blend mode must be used with premultiplied alpha.", modestr);
- }
- setBlendState(computeBlendState(mode, alphamode));
- }
- BlendMode Graphics::getBlendMode(BlendAlpha &alphamode) const
- {
- return computeBlendMode(states.back().blend, alphamode);
- }
- const BlendState &Graphics::getBlendState() const
- {
- return states.back().blend;
- }
- void Graphics::setDefaultSamplerState(const SamplerState &s)
- {
- states.back().defaultSamplerState = s;
- }
- const SamplerState &Graphics::getDefaultSamplerState() const
- {
- return states.back().defaultSamplerState;
- }
- void Graphics::setLineWidth(float width)
- {
- states.back().lineWidth = width;
- }
- void Graphics::setLineStyle(Graphics::LineStyle style)
- {
- states.back().lineStyle = style;
- }
- void Graphics::setLineJoin(Graphics::LineJoin join)
- {
- states.back().lineJoin = join;
- }
- float Graphics::getLineWidth() const
- {
- return states.back().lineWidth;
- }
- Graphics::LineStyle Graphics::getLineStyle() const
- {
- return states.back().lineStyle;
- }
- Graphics::LineJoin Graphics::getLineJoin() const
- {
- return states.back().lineJoin;
- }
- float Graphics::getPointSize() const
- {
- return states.back().pointSize;
- }
- bool Graphics::isWireframe() const
- {
- return states.back().wireframe;
- }
- void Graphics::captureScreenshot(const ScreenshotInfo &info)
- {
- pendingScreenshotCallbacks.push_back(info);
- }
- void Graphics::copyBuffer(Buffer *source, Buffer *dest, size_t sourceoffset, size_t destoffset, size_t size)
- {
- if (!capabilities.features[FEATURE_COPY_BUFFER])
- throw love::Exception("Buffer copying is not supported on this system.");
- Range sourcerange(sourceoffset, size);
- Range destrange(destoffset, size);
- if (dest->getDataUsage() == BUFFERDATAUSAGE_STREAM)
- throw love::Exception("Buffers created with 'stream' data usage cannot be used as a copy destination.");
- if (sourcerange.getMax() >= source->getSize())
- throw love::Exception("Buffer copy source offset and size doesn't fit within the source Buffer's size.");
- if (destrange.getMax() >= dest->getSize())
- throw love::Exception("Buffer copy destination offset and size doesn't fit within the destination buffer's size.");
- if (source == dest && sourcerange.intersects(destrange))
- throw love::Exception("Copying a portion of a buffer to the same buffer requires non-overlapping source and destination offsets.");
- if (dest->isImmutable())
- throw love::Exception("Cannot copy to an immutable buffer.");
- source->copyTo(dest, sourceoffset, destoffset, size);
- }
- void Graphics::copyTextureToBuffer(Texture *source, Buffer *dest, int slice, int mipmap, const Rect &rect, size_t destoffset, int destwidth)
- {
- if (!capabilities.features[FEATURE_COPY_TEXTURE_TO_BUFFER])
- {
- if (!source->isRenderTarget())
- throw love::Exception("Copying a non-render target Texture to a Buffer is not supported on this system.");
- if (!capabilities.features[FEATURE_COPY_RENDER_TARGET_TO_BUFFER])
- throw love::Exception("Copying a render target Texture to a Buffer is not supported on this system.");
- }
- PixelFormat format = source->getPixelFormat();
- if (isPixelFormatDepthStencil(format))
- throw love::Exception("Copying a depth/stencil Texture to a Buffer is not supported.");
- if (!source->isReadable())
- throw love::Exception("copyTextureToBuffer can only be called on readable Textures.");
- if (dest->getDataUsage() == BUFFERDATAUSAGE_STREAM)
- throw love::Exception("Buffers created with 'stream' data usage cannot be used as a copy destination.");
- if (dest->isImmutable())
- throw love::Exception("Cannot copy to an immutable buffer.");
- if (isRenderTargetActive(source))
- throw love::Exception("copyTextureToBuffer cannot be called while the Texture is an active render target.");
- if (mipmap < 0 || mipmap >= source->getMipmapCount())
- throw love::Exception("Invalid texture mipmap index %d.", mipmap + 1);
- TextureType textype = source->getTextureType();
- if (slice < 0 || (textype == TEXTURE_CUBE && slice >= 6)
- || (textype == TEXTURE_VOLUME && slice >= source->getDepth(mipmap))
- || (textype == TEXTURE_2D_ARRAY && slice >= source->getLayerCount()))
- {
- throw love::Exception("Invalid texture slice index %d.", slice + 1);
- }
- int mipw = source->getPixelWidth(mipmap);
- int miph = source->getPixelHeight(mipmap);
- if (rect.x < 0 || rect.y < 0 || rect.w <= 0 || rect.h <= 0
- || (rect.x + rect.w) > mipw || (rect.y + rect.h) > miph)
- {
- throw love::Exception("Invalid rectangle dimensions (x=%d, y=%d, w=%d, h=%d) for %dx%d texture.", rect.x, rect.y, rect.w, rect.h, mipw, miph);
- }
- if (destwidth <= 0)
- destwidth = rect.w;
- size_t size = 0;
- if (isPixelFormatCompressed(format))
- {
- if (destwidth != rect.w) // OpenGL limitation...
- throw love::Exception("Copying a compressed texture to a buffer cannot use a custom destination width.");
- const PixelFormatInfo &info = getPixelFormatInfo(format);
- int bw = (int) info.blockWidth;
- int bh = (int) info.blockHeight;
- if (rect.x % bw != 0 || rect.y % bh != 0 ||
- ((rect.w % bw != 0 || rect.h % bh != 0) && rect.x + rect.w != source->getPixelWidth(mipmap)))
- {
- const char *name = nullptr;
- love::getConstant(format, name);
- throw love::Exception("Compressed texture format %s only supports copying a sub-rectangle with offset and dimensions that are a multiple of %d x %d.", name, bw, bh);
- }
- // Note: this will need to change if destwidth == rect.w restriction
- // is removed.
- size = getPixelFormatSliceSize(format, destwidth, rect.h);
- }
- else
- {
- // Not the cleanest, but should work since uncompressed formats always
- // have 1x1 blocks.
- int pixels = (rect.h - 1) * destwidth + rect.w;
- size = getPixelFormatUncompressedRowSize(format, pixels);
- }
- Range destrange(destoffset, size);
- if (destrange.getMax() >= dest->getSize())
- throw love::Exception("Buffer copy destination offset and width/height doesn't fit within the destination Buffer.");
- source->copyToBuffer(dest, slice, mipmap, rect, destoffset, destwidth, size);
- }
- void Graphics::copyBufferToTexture(Buffer *source, Texture *dest, size_t sourceoffset, int sourcewidth, int slice, int mipmap, const Rect &rect)
- {
- if (!capabilities.features[FEATURE_COPY_BUFFER_TO_TEXTURE])
- throw love::Exception("Copying a Buffer to a Texture is not supported on this system.");
- PixelFormat format = dest->getPixelFormat();
- if (isPixelFormatDepthStencil(format))
- throw love::Exception("Copying a Buffer to a depth/stencil Texture is not supported.");
- if (!dest->isReadable())
- throw love::Exception("copyBufferToTexture can only be called on readable Textures.");
- if (isRenderTargetActive(dest))
- throw love::Exception("copyBufferToTexture cannot be called while the Texture is an active render target.");
- if (mipmap < 0 || mipmap >= dest->getMipmapCount())
- throw love::Exception("Invalid texture mipmap index %d.", mipmap + 1);
- TextureType textype = dest->getTextureType();
- if (slice < 0 || (textype == TEXTURE_CUBE && slice >= 6)
- || (textype == TEXTURE_VOLUME && slice >= dest->getDepth(mipmap))
- || (textype == TEXTURE_2D_ARRAY && slice >= dest->getLayerCount()))
- {
- throw love::Exception("Invalid texture slice index %d.", slice + 1);
- }
- int mipw = dest->getPixelWidth(mipmap);
- int miph = dest->getPixelHeight(mipmap);
- if (rect.x < 0 || rect.y < 0 || rect.w <= 0 || rect.h <= 0
- || (rect.x + rect.w) > mipw || (rect.y + rect.h) > miph)
- {
- throw love::Exception("Invalid rectangle dimensions (x=%d, y=%d, w=%d, h=%d) for %dx%d texture.", rect.x, rect.y, rect.w, rect.h, mipw, miph);
- }
- if (sourcewidth <= 0)
- sourcewidth = rect.w;
- size_t size = 0;
- if (isPixelFormatCompressed(format))
- {
- if (sourcewidth != rect.w) // OpenGL limitation...
- throw love::Exception("Copying a buffer to a compressed texture cannot use a custom source width.");
- const PixelFormatInfo &info = getPixelFormatInfo(format);
- int bw = (int) info.blockWidth;
- int bh = (int) info.blockHeight;
- if (rect.x % bw != 0 || rect.y % bh != 0 ||
- ((rect.w % bw != 0 || rect.h % bh != 0) && rect.x + rect.w != dest->getPixelWidth(mipmap)))
- {
- const char *name = nullptr;
- love::getConstant(format, name);
- throw love::Exception("Compressed texture format %s only supports copying a sub-rectangle with offset and dimensions that are a multiple of %d x %d.", name, bw, bh);
- }
- // Note: this will need to change if sourcewidth == rect.w restriction
- // is removed.
- size = getPixelFormatSliceSize(format, sourcewidth, rect.h);
- }
- else
- {
- // Not the cleanest, but should work since uncompressed formats always
- // have 1x1 blocks.
- int pixels = (rect.h - 1) * sourcewidth + rect.w;
- size = getPixelFormatUncompressedRowSize(format, pixels);
- }
- Range sourcerange(sourceoffset, size);
- if (sourcerange.getMax() >= source->getSize())
- throw love::Exception("Buffer copy source offset and width/height doesn't fit within the source Buffer.");
- dest->copyFromBuffer(source, sourceoffset, sourcewidth, size, slice, mipmap, rect);
- }
- void Graphics::dispatchThreadgroups(Shader* shader, int x, int y, int z)
- {
- if (!shader->hasStage(SHADERSTAGE_COMPUTE))
- throw love::Exception("Only compute shaders can have threads dispatched.");
- if (x <= 0 || y <= 0 || z <= 0)
- throw love::Exception("Threadgroup dispatch size must be positive.");
- if (x > capabilities.limits[LIMIT_THREADGROUPS_X]
- || y > capabilities.limits[LIMIT_THREADGROUPS_Y]
- || z > capabilities.limits[LIMIT_THREADGROUPS_Z])
- {
- throw love::Exception("Too many threadgroups dispatched.");
- }
- flushBatchedDraws();
- auto prevshader = Shader::current;
- shader->attach();
- bool success = dispatch(x, y, z);
- if (prevshader != nullptr)
- prevshader->attach();
- if (!success)
- throw love::Exception("Compute shader must have resources bound to all writable texture and buffer variables.");
- }
- Graphics::BatchedVertexData Graphics::requestBatchedDraw(const BatchedDrawCommand &cmd)
- {
- BatchedDrawState &state = batchedDrawState;
- bool shouldflush = false;
- bool shouldresize = false;
- if (cmd.primitiveMode != state.primitiveMode
- || cmd.formats[0] != state.formats[0] || cmd.formats[1] != state.formats[1]
- || ((cmd.indexMode != TRIANGLEINDEX_NONE) != (state.indexCount > 0))
- || cmd.texture != state.texture
- || cmd.standardShaderType != state.standardShaderType)
- {
- shouldflush = true;
- }
- int totalvertices = state.vertexCount + cmd.vertexCount;
- // We only support uint16 index buffers for now.
- if (totalvertices > LOVE_UINT16_MAX && cmd.indexMode != TRIANGLEINDEX_NONE)
- shouldflush = true;
- int reqIndexCount = getIndexCount(cmd.indexMode, cmd.vertexCount);
- size_t reqIndexSize = reqIndexCount * sizeof(uint16);
- size_t newdatasizes[2] = {0, 0};
- size_t buffersizes[3] = {0, 0, 0};
- for (int i = 0; i < 2; i++)
- {
- if (cmd.formats[i] == CommonFormat::NONE)
- continue;
- size_t stride = getFormatStride(cmd.formats[i]);
- size_t datasize = stride * totalvertices;
- if (state.vbMap[i].data != nullptr && datasize > state.vbMap[i].size)
- shouldflush = true;
- if (datasize > state.vb[i]->getUsableSize())
- {
- buffersizes[i] = std::max(datasize, state.vb[i]->getSize() * 2);
- shouldresize = true;
- }
- newdatasizes[i] = stride * cmd.vertexCount;
- }
- if (cmd.indexMode != TRIANGLEINDEX_NONE)
- {
- size_t datasize = (state.indexCount + reqIndexCount) * sizeof(uint16);
- if (state.indexBufferMap.data != nullptr && datasize > state.indexBufferMap.size)
- shouldflush = true;
- if (datasize > state.indexBuffer->getUsableSize())
- {
- buffersizes[2] = std::max(datasize, state.indexBuffer->getSize() * 2);
- shouldresize = true;
- }
- }
- if (shouldflush || shouldresize)
- {
- flushBatchedDraws();
- state.primitiveMode = cmd.primitiveMode;
- state.formats[0] = cmd.formats[0];
- state.formats[1] = cmd.formats[1];
- state.texture = cmd.texture;
- state.standardShaderType = cmd.standardShaderType;
- }
- if (state.vertexCount == 0)
- {
- if (Shader::isDefaultActive())
- Shader::attachDefault(state.standardShaderType);
- if (Shader::current != nullptr)
- Shader::current->validateDrawState(cmd.primitiveMode, cmd.texture);
- }
- if (shouldresize)
- {
- for (int i = 0; i < 2; i++)
- {
- if (state.vb[i]->getSize() < buffersizes[i])
- {
- state.vb[i]->release();
- state.vb[i] = newStreamBuffer(BUFFERUSAGE_VERTEX, buffersizes[i]);
- }
- }
- if (state.indexBuffer->getSize() < buffersizes[2])
- {
- state.indexBuffer->release();
- state.indexBuffer = newStreamBuffer(BUFFERUSAGE_INDEX, buffersizes[2]);
- }
- }
- if (cmd.indexMode != TRIANGLEINDEX_NONE)
- {
- if (state.indexBufferMap.data == nullptr)
- state.indexBufferMap = state.indexBuffer->map(reqIndexSize);
- uint16 *indices = (uint16 *) state.indexBufferMap.data;
- fillIndices(cmd.indexMode, state.vertexCount, cmd.vertexCount, indices);
- state.indexBufferMap.data += reqIndexSize;
- }
- BatchedVertexData d;
- for (int i = 0; i < 2; i++)
- {
- if (newdatasizes[i] > 0)
- {
- if (state.vbMap[i].data == nullptr)
- state.vbMap[i] = state.vb[i]->map(newdatasizes[i]);
- d.stream[i] = state.vbMap[i].data;
- state.vbMap[i].data += newdatasizes[i];
- }
- }
- if (state.vertexCount > 0)
- drawCallsBatched++;
- state.vertexCount += cmd.vertexCount;
- state.indexCount += reqIndexCount;
- return d;
- }
- void Graphics::flushBatchedDraws()
- {
- auto &sbstate = batchedDrawState;
- if (sbstate.vertexCount == 0 && sbstate.indexCount == 0)
- return;
- VertexAttributes attributes;
- BufferBindings buffers;
- size_t usedsizes[3] = {0, 0, 0};
- for (int i = 0; i < 2; i++)
- {
- if (sbstate.formats[i] == CommonFormat::NONE)
- continue;
- attributes.setCommonFormat(sbstate.formats[i], (uint8) i);
- usedsizes[i] = getFormatStride(sbstate.formats[i]) * sbstate.vertexCount;
- size_t offset = sbstate.vb[i]->unmap(usedsizes[i]);
- buffers.set(i, sbstate.vb[i], offset);
- sbstate.vbMap[i] = StreamBuffer::MapInfo();
- }
- if (attributes.enableBits == 0)
- return;
- Colorf nc = getColor();
- if (attributes.isEnabled(ATTRIB_COLOR))
- setColor(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
- pushIdentityTransform();
- if (sbstate.indexCount > 0)
- {
- usedsizes[2] = sizeof(uint16) * sbstate.indexCount;
- DrawIndexedCommand cmd(&attributes, &buffers, sbstate.indexBuffer);
- cmd.primitiveType = sbstate.primitiveMode;
- cmd.indexCount = sbstate.indexCount;
- cmd.indexType = INDEX_UINT16;
- cmd.indexBufferOffset = sbstate.indexBuffer->unmap(usedsizes[2]);
- cmd.texture = sbstate.texture;
- draw(cmd);
- sbstate.indexBufferMap = StreamBuffer::MapInfo();
- }
- else
- {
- DrawCommand cmd(&attributes, &buffers);
- cmd.primitiveType = sbstate.primitiveMode;
- cmd.vertexStart = 0;
- cmd.vertexCount = sbstate.vertexCount;
- cmd.texture = sbstate.texture;
- draw(cmd);
- }
- for (int i = 0; i < 2; i++)
- {
- if (usedsizes[i] > 0)
- sbstate.vb[i]->markUsed(usedsizes[i]);
- }
- if (usedsizes[2] > 0)
- sbstate.indexBuffer->markUsed(usedsizes[2]);
- popTransform();
- if (attributes.isEnabled(ATTRIB_COLOR))
- setColor(nc);
- batchedDrawState.vertexCount = 0;
- batchedDrawState.indexCount = 0;
- }
- void Graphics::flushBatchedDrawsGlobal()
- {
- Graphics *instance = getInstance<Graphics>(M_GRAPHICS);
- if (instance != nullptr)
- instance->flushBatchedDraws();
- }
- /**
- * Drawing
- **/
- void Graphics::draw(Drawable *drawable, const Matrix4 &m)
- {
- drawable->draw(this, m);
- }
- void Graphics::draw(Texture *texture, Quad *quad, const Matrix4 &m)
- {
- texture->draw(this, quad, m);
- }
- void Graphics::drawLayer(Texture *texture, int layer, const Matrix4 &m)
- {
- texture->drawLayer(this, layer, m);
- }
- void Graphics::drawLayer(Texture *texture, int layer, Quad *quad, const Matrix4 &m)
- {
- texture->drawLayer(this, layer, quad, m);
- }
- void Graphics::drawInstanced(Mesh *mesh, const Matrix4 &m, int instancecount)
- {
- mesh->drawInstanced(this, m, instancecount);
- }
- void Graphics::drawShaderVertices(PrimitiveType primtype, int vertexcount, int instancecount, Texture *maintexture)
- {
- flushBatchedDraws();
- if (!capabilities.features[FEATURE_GLSL3])
- throw love::Exception("drawShaderVertices is not supported on this system (GLSL3 support is required.)");
- if (Shader::isDefaultActive() || !Shader::current)
- throw love::Exception("drawShaderVertices can only be used with a custom shader.");
- if (vertexcount < 0 || instancecount < 0)
- throw love::Exception("drawShaderVertices vertex and instance count parameters must not be negative.");
- Shader::current->validateDrawState(primtype, maintexture);
- VertexAttributes attributes;
- BufferBindings buffers;
- DrawCommand cmd(&attributes, &buffers);
- cmd.primitiveType = primtype;
- cmd.vertexCount = vertexcount;
- cmd.instanceCount = std::max(1, instancecount);
- cmd.texture = maintexture;
- draw(cmd);
- }
- void Graphics::drawShaderVertices(Buffer *indexbuffer, int indexcount, int instancecount, int startindex, Texture *maintexture)
- {
- flushBatchedDraws();
- if (!capabilities.features[FEATURE_GLSL3])
- throw love::Exception("drawShaderVertices is not supported on this system (GLSL3 support is required.)");
- if (!(indexbuffer->getUsageFlags() & BUFFERUSAGEFLAG_INDEX))
- throw love::Exception("The buffer passed to drawShaderVertices must be an index buffer.");
- if (startindex < 0)
- throw love::Exception("drawShaderVertices startindex parameter must not be negative.");
- if (indexcount < 0 || instancecount < 0)
- throw love::Exception("drawShaderVertices index and instance count parameters must not be negative.");
- if ((size_t)(startindex + indexcount) > indexbuffer->getArrayLength() * indexbuffer->getDataMembers().size())
- throw love::Exception("drawShaderVertices startindex and index count parameters do not fit in the given index buffer.");
- if (Shader::isDefaultActive() || !Shader::current)
- throw love::Exception("drawShaderVertices can only be used with a custom shader.");
- Shader::current->validateDrawState(PRIMITIVE_TRIANGLES, maintexture);
- VertexAttributes attributes;
- BufferBindings buffers;
- DrawIndexedCommand cmd(&attributes, &buffers, indexbuffer);
- cmd.primitiveType = PRIMITIVE_TRIANGLES;
- cmd.indexCount = indexcount;
- cmd.instanceCount = std::max(1, instancecount);
- cmd.indexType = getIndexDataType(indexbuffer->getDataMember(0).decl.format);
- cmd.indexBufferOffset = startindex * getIndexDataSize(cmd.indexType);
- cmd.texture = maintexture;
- draw(cmd);
- }
- void Graphics::print(const std::vector<Font::ColoredString> &str, const Matrix4 &m)
- {
- checkSetDefaultFont();
- if (states.back().font.get() != nullptr)
- print(str, states.back().font.get(), m);
- }
- void Graphics::print(const std::vector<Font::ColoredString> &str, Font *font, const Matrix4 &m)
- {
- font->print(this, str, m, states.back().color);
- }
- void Graphics::printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m)
- {
- checkSetDefaultFont();
- if (states.back().font.get() != nullptr)
- printf(str, states.back().font.get(), wrap, align, m);
- }
- void Graphics::printf(const std::vector<Font::ColoredString> &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m)
- {
- font->printf(this, str, wrap, align, m, states.back().color);
- }
- /**
- * Primitives (points, shapes, lines).
- **/
- void Graphics::points(const Vector2 *positions, const Colorf *colors, size_t numpoints)
- {
- const Matrix4 &t = getTransform();
- bool is2D = t.isAffine2DTransform();
- BatchedDrawCommand cmd;
- cmd.primitiveMode = PRIMITIVE_POINTS;
- cmd.formats[0] = getSinglePositionFormat(is2D);
- cmd.formats[1] = CommonFormat::RGBAub;
- cmd.vertexCount = (int) numpoints;
- cmd.standardShaderType = Shader::STANDARD_POINTS;
- BatchedVertexData data = requestBatchedDraw(cmd);
- if (is2D)
- t.transformXY((Vector2 *) data.stream[0], positions, cmd.vertexCount);
- else
- t.transformXY0((Vector3 *) data.stream[0], positions, cmd.vertexCount);
- Color32 *colordata = (Color32 *) data.stream[1];
- if (colors)
- {
- Colorf nc = getColor();
- gammaCorrectColor(nc);
- if (isGammaCorrect())
- {
- for (int i = 0; i < cmd.vertexCount; i++)
- {
- Colorf ci = colors[i];
- gammaCorrectColor(ci);
- ci *= nc;
- unGammaCorrectColor(ci);
- colordata[i] = toColor32(ci);
- }
- }
- else
- {
- for (int i = 0; i < cmd.vertexCount; i++)
- colordata[i] = toColor32(nc * colors[i]);
- }
- }
- else
- {
- Color32 c = toColor32(getColor());
- for (int i = 0; i < cmd.vertexCount; i++)
- colordata[i] = c;
- }
- }
- int Graphics::calculateEllipsePoints(float rx, float ry) const
- {
- int points = (int) sqrtf(((rx + ry) / 2.0f) * 20.0f * (float) pixelScaleStack.back());
- return std::max(points, 8);
- }
- void Graphics::polyline(const Vector2 *vertices, size_t count)
- {
- float halfwidth = getLineWidth() * 0.5f;
- LineJoin linejoin = getLineJoin();
- LineStyle linestyle = getLineStyle();
- float pixelsize = 1.0f / std::max((float) pixelScaleStack.back(), 0.000001f);
- if (linejoin == LINE_JOIN_NONE)
- {
- NoneJoinPolyline line;
- line.render(vertices, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
- line.draw(this);
- }
- else if (linejoin == LINE_JOIN_BEVEL)
- {
- BevelJoinPolyline line;
- line.render(vertices, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
- line.draw(this);
- }
- else if (linejoin == LINE_JOIN_MITER)
- {
- MiterJoinPolyline line;
- line.render(vertices, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
- line.draw(this);
- }
- }
- void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
- {
- Vector2 coords[] = {Vector2(x,y), Vector2(x,y+h), Vector2(x+w,y+h), Vector2(x+w,y), Vector2(x,y)};
- polygon(mode, coords, 5);
- }
- void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points)
- {
- if (rx <= 0 || ry <= 0)
- {
- rectangle(mode, x, y, w, h);
- return;
- }
- // Radius values that are more than half the rectangle's size aren't handled
- // correctly (for now)...
- if (w >= 0.02f)
- rx = std::min(rx, w / 2.0f - 0.01f);
- if (h >= 0.02f)
- ry = std::min(ry, h / 2.0f - 0.01f);
- points = std::max(points / 4, 1);
- const float half_pi = static_cast<float>(LOVE_M_PI / 2);
- float angle_shift = half_pi / ((float) points + 1.0f);
- int num_coords = (points + 2) * 4;
- Vector2 *coords = getScratchBuffer<Vector2>(num_coords + 1);
- float phi = .0f;
- for (int i = 0; i <= points + 2; ++i, phi += angle_shift)
- {
- coords[i].x = x + rx * (1 - cosf(phi));
- coords[i].y = y + ry * (1 - sinf(phi));
- }
- phi = half_pi;
- for (int i = points + 2; i <= 2 * (points + 2); ++i, phi += angle_shift)
- {
- coords[i].x = x + w - rx * (1 + cosf(phi));
- coords[i].y = y + ry * (1 - sinf(phi));
- }
- phi = 2 * half_pi;
- for (int i = 2 * (points + 2); i <= 3 * (points + 2); ++i, phi += angle_shift)
- {
- coords[i].x = x + w - rx * (1 + cosf(phi));
- coords[i].y = y + h - ry * (1 + sinf(phi));
- }
- phi = 3 * half_pi;
- for (int i = 3 * (points + 2); i <= 4 * (points + 2); ++i, phi += angle_shift)
- {
- coords[i].x = x + rx * (1 - cosf(phi));
- coords[i].y = y + h - ry * (1 + sinf(phi));
- }
- coords[num_coords] = coords[0];
- polygon(mode, coords, num_coords + 1);
- }
- void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry)
- {
- int points = calculateEllipsePoints(std::min(rx, std::abs(w/2)), std::min(ry, std::abs(h/2)));
- rectangle(mode, x, y, w, h, rx, ry, points);
- }
- void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
- {
- ellipse(mode, x, y, radius, radius, points);
- }
- void Graphics::circle(DrawMode mode, float x, float y, float radius)
- {
- ellipse(mode, x, y, radius, radius);
- }
- void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
- {
- float two_pi = (float) (LOVE_M_PI * 2);
- if (points <= 0) points = 1;
- float angle_shift = (two_pi / points);
- float phi = .0f;
- // 1 extra point at the end for a closed loop, and 1 extra point at the
- // start in filled mode for the vertex in the center of the ellipse.
- int extrapoints = 1 + (mode == DRAW_FILL ? 1 : 0);
- Vector2 *polygoncoords = getScratchBuffer<Vector2>(points + extrapoints);
- Vector2 *coords = polygoncoords;
- if (mode == DRAW_FILL)
- {
- coords[0].x = x;
- coords[0].y = y;
- coords++;
- }
- for (int i = 0; i < points; ++i, phi += angle_shift)
- {
- coords[i].x = x + a * cosf(phi);
- coords[i].y = y + b * sinf(phi);
- }
- coords[points] = coords[0];
- // Last argument to polygon(): don't skip the last vertex in fill mode.
- polygon(mode, polygoncoords, points + extrapoints, false);
- }
- void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
- {
- ellipse(mode, x, y, a, b, calculateEllipsePoints(a, b));
- }
- void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
- {
- // Nothing to display with no points or equal angles. (Or is there with line mode?)
- if (points <= 0 || angle1 == angle2)
- return;
- // Oh, you want to draw a circle?
- if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
- {
- circle(drawmode, x, y, radius, points);
- return;
- }
- float angle_shift = (angle2 - angle1) / points;
- // Bail on precision issues.
- if (angle_shift == 0.0)
- return;
- // Prevent the connecting line from being drawn if a closed line arc has a
- // small angle. Avoids some visual issues when connected lines are at sharp
- // angles, due to the miter line join drawing code.
- if (drawmode == DRAW_LINE && arcmode == ARC_CLOSED && fabsf(angle1 - angle2) < LOVE_TORAD(4))
- arcmode = ARC_OPEN;
- // Quick fix for the last part of a filled open arc not being drawn (because
- // polygon(DRAW_FILL, ...) doesn't work without a closed loop of vertices.)
- if (drawmode == DRAW_FILL && arcmode == ARC_OPEN)
- arcmode = ARC_CLOSED;
- float phi = angle1;
- Vector2 *coords = nullptr;
- int num_coords = 0;
- const auto createPoints = [&](Vector2 *coordinates)
- {
- for (int i = 0; i <= points; ++i, phi += angle_shift)
- {
- coordinates[i].x = x + radius * cosf(phi);
- coordinates[i].y = y + radius * sinf(phi);
- }
- };
- if (arcmode == ARC_PIE)
- {
- num_coords = points + 3;
- coords = getScratchBuffer<Vector2>(num_coords);
- coords[0] = coords[num_coords - 1] = Vector2(x, y);
- createPoints(coords + 1);
- }
- else if (arcmode == ARC_OPEN)
- {
- num_coords = points + 1;
- coords = getScratchBuffer<Vector2>(num_coords);
- createPoints(coords);
- }
- else // ARC_CLOSED
- {
- num_coords = points + 2;
- coords = getScratchBuffer<Vector2>(num_coords);
- createPoints(coords);
- // Connect the ends of the arc.
- coords[num_coords - 1] = coords[0];
- }
- polygon(drawmode, coords, num_coords);
- }
- void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2)
- {
- float points = (float) calculateEllipsePoints(radius, radius);
- // The amount of points is based on the fraction of the circle created by the arc.
- float angle = fabsf(angle1 - angle2);
- if (angle < 2.0f * (float) LOVE_M_PI)
- points *= angle / (2.0f * (float) LOVE_M_PI);
- arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
- }
- void Graphics::polygon(DrawMode mode, const Vector2 *coords, size_t count, bool skipLastFilledVertex)
- {
- // coords is an array of a closed loop of vertices, i.e.
- // coords[count-1] == coords[0]
- if (mode == DRAW_LINE)
- {
- polyline(coords, count);
- }
- else
- {
- const Matrix4 &t = getTransform();
- bool is2D = t.isAffine2DTransform();
- BatchedDrawCommand cmd;
- cmd.formats[0] = getSinglePositionFormat(is2D);
- cmd.formats[1] = CommonFormat::RGBAub;
- cmd.indexMode = TRIANGLEINDEX_FAN;
- cmd.vertexCount = (int)count - (skipLastFilledVertex ? 1 : 0);
- BatchedVertexData data = requestBatchedDraw(cmd);
- if (is2D)
- t.transformXY((Vector2 *) data.stream[0], coords, cmd.vertexCount);
- else
- t.transformXY0((Vector3 *) data.stream[0], coords, cmd.vertexCount);
- Color32 c = toColor32(getColor());
- Color32 *colordata = (Color32 *) data.stream[1];
- for (int i = 0; i < cmd.vertexCount; i++)
- colordata[i] = c;
- }
- }
- const Graphics::Capabilities &Graphics::getCapabilities() const
- {
- return capabilities;
- }
- Graphics::Stats Graphics::getStats() const
- {
- Stats stats;
- getAPIStats(stats.shaderSwitches);
- stats.drawCalls = drawCalls;
- if (batchedDrawState.vertexCount > 0)
- stats.drawCalls++;
- stats.renderTargetSwitches = renderTargetSwitchCount;
- stats.drawCallsBatched = drawCallsBatched;
- stats.textures = Texture::textureCount;
- stats.fonts = Font::fontCount;
- stats.textureMemory = Texture::totalGraphicsMemory;
-
- return stats;
- }
- size_t Graphics::getStackDepth() const
- {
- return stackTypeStack.size();
- }
- void Graphics::push(StackType type)
- {
- if (stackTypeStack.size() == MAX_USER_STACK_DEPTH)
- throw Exception("Maximum stack depth reached (more pushes than pops?)");
- pushTransform();
- pixelScaleStack.push_back(pixelScaleStack.back());
- if (type == STACK_ALL)
- states.push_back(states.back());
- stackTypeStack.push_back(type);
- }
- void Graphics::pop()
- {
- if (stackTypeStack.size() < 1)
- throw Exception("Minimum stack depth reached (more pops than pushes?)");
- popTransform();
- pixelScaleStack.pop_back();
- if (stackTypeStack.back() == STACK_ALL)
- {
- DisplayState &newstate = states[states.size() - 2];
- restoreStateChecked(newstate);
- // The last two states in the stack should be equal now.
- states.pop_back();
- }
- stackTypeStack.pop_back();
- }
- /**
- * Transform and stack functions.
- **/
- const Matrix4 &Graphics::getTransform() const
- {
- return transformStack.back();
- }
- const Matrix4 &Graphics::getDeviceProjection() const
- {
- return deviceProjectionMatrix;
- }
- void Graphics::pushTransform()
- {
- transformStack.push_back(transformStack.back());
- }
- void Graphics::pushIdentityTransform()
- {
- transformStack.push_back(Matrix4());
- }
- void Graphics::popTransform()
- {
- transformStack.pop_back();
- }
- void Graphics::rotate(float r)
- {
- transformStack.back().rotate(r);
- }
- void Graphics::scale(float x, float y)
- {
- transformStack.back().scale(x, y);
- pixelScaleStack.back() *= (fabs(x) + fabs(y)) / 2.0;
- }
- void Graphics::translate(float x, float y)
- {
- transformStack.back().translate(x, y);
- }
- void Graphics::shear(float kx, float ky)
- {
- transformStack.back().shear(kx, ky);
- }
- void Graphics::origin()
- {
- transformStack.back().setIdentity();
- pixelScaleStack.back() = 1;
- }
- void Graphics::applyTransform(const Matrix4 &m)
- {
- Matrix4 ¤t = transformStack.back();
- current *= m;
- float sx, sy;
- current.getApproximateScale(sx, sy);
- pixelScaleStack.back() = (sx + sy) / 2.0;
- }
- void Graphics::replaceTransform(const Matrix4 &m)
- {
- transformStack.back() = m;
- float sx, sy;
- m.getApproximateScale(sx, sy);
- pixelScaleStack.back() = (sx + sy) / 2.0;
- }
- Vector2 Graphics::transformPoint(Vector2 point)
- {
- Vector2 p;
- transformStack.back().transformXY(&p, &point, 1);
- return p;
- }
- Vector2 Graphics::inverseTransformPoint(Vector2 point)
- {
- Vector2 p;
- // TODO: We should probably cache the inverse transform so we don't have to
- // re-calculate it every time this is called.
- transformStack.back().inverse().transformXY(&p, &point, 1);
- return p;
- }
- void Graphics::setOrthoProjection(float w, float h, float near, float far)
- {
- if (near >= far)
- throw love::Exception("Orthographic projection Z far value must be greater than the Z near value.");
- Matrix4 m = Matrix4::ortho(0.0f, w, 0.0f, h, near, far);
- setCustomProjection(m);
- }
- void Graphics::setPerspectiveProjection(float verticalfov, float aspect, float near, float far)
- {
- if (near <= 0.0f)
- throw love::Exception("Perspective projection Z near value must be greater than 0.");
- if (near >= far)
- throw love::Exception("Perspective projection Z far value must be greater than the Z near value.");
- Matrix4 m = Matrix4::perspective(verticalfov, aspect, near, far);
- setCustomProjection(m);
- }
- void Graphics::setCustomProjection(const Matrix4 &m)
- {
- flushBatchedDraws();
- auto &state = states.back();
- state.useCustomProjection = true;
- state.customProjection = m;
- updateDeviceProjection(m);
- }
- void Graphics::resetProjection()
- {
- flushBatchedDraws();
- auto &state = states.back();
- int w = getWidth();
- int h = getHeight();
- const auto &rt = state.renderTargets.getFirstTarget();
- if (rt.texture.get())
- {
- w = rt.texture->getWidth(rt.mipmap);
- h = rt.texture->getHeight(rt.mipmap);
- }
- state.useCustomProjection = false;
- updateDeviceProjection(Matrix4::ortho(0.0f, w, 0.0f, h, -10.0f, 10.0f));
- }
- void Graphics::updateDeviceProjection(const Matrix4 &projection)
- {
- // Note: graphics implementations define computeDeviceProjection.
- deviceProjectionMatrix = computeDeviceProjection(projection, isRenderTargetActive());
- }
- Matrix4 Graphics::calculateDeviceProjection(const Matrix4 &projection, uint32 flags) const
- {
- Matrix4 m = projection;
- bool reverseZ = (flags & DEVICE_PROJECTION_REVERSE_Z) != 0;
- if (flags & DEVICE_PROJECTION_FLIP_Y)
- m.setRow(1, -m.getRow(1));
- if (flags & DEVICE_PROJECTION_Z_01) // Go from Z [-1, 1] to Z [0, 1].
- m.setRow(2, m.getRow(2) * (reverseZ ? -0.5f : 0.5f) + m.getRow(3));
- else if (reverseZ)
- m.setRow(2, -m.getRow(2));
- return m;
- }
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::DrawMode, Graphics::DRAW_MAX_ENUM, drawMode)
- {
- { "line", Graphics::DRAW_LINE },
- { "fill", Graphics::DRAW_FILL },
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::DrawMode, Graphics::DRAW_MAX_ENUM, drawMode)
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::ArcMode, Graphics::ARC_MAX_ENUM, arcMode)
- {
- { "open", Graphics::ARC_OPEN },
- { "closed", Graphics::ARC_CLOSED },
- { "pie", Graphics::ARC_PIE },
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::ArcMode, Graphics::ARC_MAX_ENUM, arcMode)
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::LineStyle, Graphics::LINE_MAX_ENUM, lineStyle)
- {
- { "smooth", Graphics::LINE_SMOOTH },
- { "rough", Graphics::LINE_ROUGH }
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::LineStyle, Graphics::LINE_MAX_ENUM, lineStyle)
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM, lineJoin)
- {
- { "none", Graphics::LINE_JOIN_NONE },
- { "miter", Graphics::LINE_JOIN_MITER },
- { "bevel", Graphics::LINE_JOIN_BEVEL }
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM, lineJoin)
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::Feature, Graphics::FEATURE_MAX_ENUM, feature)
- {
- { "multirendertargetformats", Graphics::FEATURE_MULTI_RENDER_TARGET_FORMATS },
- { "clampzero", Graphics::FEATURE_CLAMP_ZERO },
- { "blendminmax", Graphics::FEATURE_BLEND_MINMAX },
- { "lighten", Graphics::FEATURE_LIGHTEN },
- { "fullnpot", Graphics::FEATURE_FULL_NPOT },
- { "pixelshaderhighp", Graphics::FEATURE_PIXEL_SHADER_HIGHP },
- { "shaderderivatives", Graphics::FEATURE_SHADER_DERIVATIVES },
- { "glsl3", Graphics::FEATURE_GLSL3 },
- { "glsl4", Graphics::FEATURE_GLSL4 },
- { "instancing", Graphics::FEATURE_INSTANCING },
- { "texelbuffer", Graphics::FEATURE_TEXEL_BUFFER },
- { "indexbuffer32bit", Graphics::FEATURE_INDEX_BUFFER_32BIT },
- { "copybuffer", Graphics::FEATURE_COPY_BUFFER },
- { "copybuffertotexture", Graphics::FEATURE_COPY_BUFFER_TO_TEXTURE },
- { "copytexturetobuffer", Graphics::FEATURE_COPY_TEXTURE_TO_BUFFER },
- { "copyrendertargettobuffer", Graphics::FEATURE_COPY_RENDER_TARGET_TO_BUFFER },
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::Feature, Graphics::FEATURE_MAX_ENUM, feature)
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM, systemLimit)
- {
- { "pointsize", Graphics::LIMIT_POINT_SIZE },
- { "texturesize", Graphics::LIMIT_TEXTURE_SIZE },
- { "texturelayers", Graphics::LIMIT_TEXTURE_LAYERS },
- { "volumetexturesize", Graphics::LIMIT_VOLUME_TEXTURE_SIZE },
- { "cubetexturesize", Graphics::LIMIT_CUBE_TEXTURE_SIZE },
- { "texelbuffersize", Graphics::LIMIT_TEXEL_BUFFER_SIZE },
- { "shaderstoragebuffersize", Graphics::LIMIT_SHADER_STORAGE_BUFFER_SIZE },
- { "threadgroupsx", Graphics::LIMIT_THREADGROUPS_X },
- { "threadgroupsy", Graphics::LIMIT_THREADGROUPS_Y },
- { "threadgroupsz", Graphics::LIMIT_THREADGROUPS_Z },
- { "rendertargets", Graphics::LIMIT_RENDER_TARGETS },
- { "texturemsaa", Graphics::LIMIT_TEXTURE_MSAA },
- { "anisotropy", Graphics::LIMIT_ANISOTROPY },
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM, systemLimit)
- STRINGMAP_CLASS_BEGIN(Graphics, Graphics::StackType, Graphics::STACK_MAX_ENUM, stackType)
- {
- { "all", Graphics::STACK_ALL },
- { "transform", Graphics::STACK_TRANSFORM },
- }
- STRINGMAP_CLASS_END(Graphics, Graphics::StackType, Graphics::STACK_MAX_ENUM, stackType)
- STRINGMAP_BEGIN(Renderer, RENDERER_MAX_ENUM, renderer)
- {
- { "opengl", RENDERER_OPENGL },
- { "metal", RENDERER_METAL },
- }
- STRINGMAP_END(Renderer, RENDERER_MAX_ENUM, renderer)
- } // graphics
- } // love
|