123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- /**
- * Copyright (c) 2006-2021 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 "common/config.h"
- #include "Shader.h"
- #include "Graphics.h"
- #include "graphics/vertex.h"
- // C++
- #include <algorithm>
- #include <limits>
- #include <sstream>
- namespace love
- {
- namespace graphics
- {
- namespace opengl
- {
- static bool isBuffer(Shader::UniformType utype)
- {
- return utype == Shader::UNIFORM_TEXELBUFFER || utype == Shader::UNIFORM_STORAGEBUFFER;
- }
- Shader::Shader(StrongRef<love::graphics::ShaderStage> stages[SHADERSTAGE_MAX_ENUM])
- : love::graphics::Shader(stages)
- , program(0)
- , builtinUniforms()
- , builtinUniformInfo()
- , builtinAttributes()
- {
- // load shader source and create program object
- loadVolatile();
- }
- Shader::~Shader()
- {
- unloadVolatile();
- for (const auto &p : uniforms)
- {
- // Allocated with malloc().
- if (p.second.data != nullptr)
- free(p.second.data);
- if (p.second.baseType == UNIFORM_SAMPLER)
- {
- for (int i = 0; i < p.second.count; i++)
- {
- if (p.second.textures[i] != nullptr)
- p.second.textures[i]->release();
- }
- delete[] p.second.textures;
- }
- else if (isBuffer(p.second.baseType))
- {
- for (int i = 0; i < p.second.count; i++)
- {
- if (p.second.buffers[i] != nullptr)
- p.second.buffers[i]->release();
- }
- delete[] p.second.buffers;
- }
- }
- }
- void Shader::mapActiveUniforms()
- {
- // Built-in uniform locations default to -1 (nonexistent.)
- for (int i = 0; i < int(BUILTIN_MAX_ENUM); i++)
- {
- builtinUniforms[i] = -1;
- builtinUniformInfo[i] = nullptr;
- }
- GLint activeprogram = 0;
- glGetIntegerv(GL_CURRENT_PROGRAM, &activeprogram);
- gl.useProgram(program);
- GLint numuniforms;
- glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numuniforms);
- GLchar cname[256];
- const GLint bufsize = (GLint) (sizeof(cname) / sizeof(GLchar));
- std::map<std::string, UniformInfo> olduniforms = uniforms;
- uniforms.clear();
- for (int uindex = 0; uindex < numuniforms; uindex++)
- {
- GLsizei namelen = 0;
- GLenum gltype = 0;
- UniformInfo u = {};
- glGetActiveUniform(program, (GLuint) uindex, bufsize, &namelen, &u.count, &gltype, cname);
- u.name = std::string(cname, (size_t) namelen);
- u.location = glGetUniformLocation(program, u.name.c_str());
- u.access = ACCESS_READ;
- computeUniformTypeInfo(gltype, u);
- // glGetActiveUniform appends "[0]" to the end of array uniform names...
- if (u.name.length() > 3)
- {
- size_t findpos = u.name.find("[0]");
- if (findpos != std::string::npos && findpos == u.name.length() - 3)
- u.name.erase(u.name.length() - 3);
- }
- // If this is a built-in (LOVE-created) uniform, store the location.
- BuiltinUniform builtin = BUILTIN_MAX_ENUM;
- if (getConstant(u.name.c_str(), builtin))
- builtinUniforms[int(builtin)] = u.location;
- if (u.location == -1)
- continue;
- if ((u.baseType == UNIFORM_SAMPLER && builtin != BUILTIN_TEXTURE_MAIN) || u.baseType == UNIFORM_TEXELBUFFER)
- {
- TextureUnit unit;
- unit.type = u.textureType;
- unit.active = true;
- if (u.baseType == UNIFORM_TEXELBUFFER)
- {
- unit.isTexelBuffer = true;
- unit.texture = gl.getDefaultTexelBuffer();
- }
- else
- {
- unit.isTexelBuffer = false;
- unit.texture = gl.getDefaultTexture(u.textureType, u.dataBaseType);
- }
- for (int i = 0; i < u.count; i++)
- textureUnits.push_back(unit);
- }
- // Make sure previously set uniform data is preserved, and shader-
- // initialized values are retrieved.
- auto oldu = olduniforms.find(u.name);
- if (oldu != olduniforms.end())
- {
- u.data = oldu->second.data;
- u.dataSize = oldu->second.dataSize;
- u.textures = oldu->second.textures;
- updateUniform(&u, u.count, true);
- }
- else
- {
- u.dataSize = 0;
- switch (u.baseType)
- {
- case UNIFORM_FLOAT:
- u.dataSize = sizeof(float) * u.components * u.count;
- u.data = malloc(u.dataSize);
- break;
- case UNIFORM_INT:
- case UNIFORM_BOOL:
- case UNIFORM_SAMPLER:
- case UNIFORM_TEXELBUFFER:
- u.dataSize = sizeof(int) * u.components * u.count;
- u.data = malloc(u.dataSize);
- break;
- case UNIFORM_UINT:
- u.dataSize = sizeof(unsigned int) * u.components * u.count;
- u.data = malloc(u.dataSize);
- break;
- case UNIFORM_MATRIX:
- u.dataSize = sizeof(float) * ((size_t)u.matrix.rows * u.matrix.columns) * u.count;
- u.data = malloc(u.dataSize);
- break;
- default:
- break;
- }
- if (u.dataSize > 0)
- {
- memset(u.data, 0, u.dataSize);
- if (u.baseType == UNIFORM_SAMPLER || u.baseType == UNIFORM_TEXELBUFFER)
- {
- int startunit = (int) textureUnits.size() - u.count;
- if (builtin == BUILTIN_TEXTURE_MAIN)
- startunit = 0;
- for (int i = 0; i < u.count; i++)
- u.ints[i] = startunit + i;
- glUniform1iv(u.location, u.count, u.ints);
- if (u.baseType == UNIFORM_TEXELBUFFER)
- {
- u.buffers = new love::graphics::Buffer*[u.count];
- memset(u.buffers, 0, sizeof(Buffer *) * u.count);
- }
- else
- {
- u.textures = new love::graphics::Texture*[u.count];
- memset(u.textures, 0, sizeof(Texture *) * u.count);
- }
- }
- }
- size_t offset = 0;
- // Store any shader-initialized values in our own memory.
- for (int i = 0; i < u.count; i++)
- {
- GLint location = u.location;
- if (u.count > 1)
- {
- std::ostringstream ss;
- ss << i;
- std::string indexname = u.name + "[" + ss.str() + "]";
- location = glGetUniformLocation(program, indexname.c_str());
- }
- if (location == -1)
- continue;
- switch (u.baseType)
- {
- case UNIFORM_FLOAT:
- glGetUniformfv(program, location, &u.floats[offset]);
- offset += u.components;
- break;
- case UNIFORM_INT:
- case UNIFORM_BOOL:
- glGetUniformiv(program, location, &u.ints[offset]);
- offset += u.components;
- break;
- case UNIFORM_UINT:
- glGetUniformuiv(program, location, &u.uints[offset]);
- offset += u.components;
- break;
- case UNIFORM_MATRIX:
- glGetUniformfv(program, location, &u.floats[offset]);
- offset += (size_t)u.matrix.rows * u.matrix.columns;
- break;
- default:
- break;
- }
- }
- }
- uniforms[u.name] = u;
- if (builtin != BUILTIN_MAX_ENUM)
- builtinUniformInfo[(int)builtin] = &uniforms[u.name];
- if (u.baseType == UNIFORM_SAMPLER)
- {
- // Make sure all stored textures have their Volatiles loaded before
- // the sendTextures call, since it calls getHandle().
- for (int i = 0; i < u.count; i++)
- {
- if (u.textures[i] == nullptr)
- continue;
- Volatile *v = dynamic_cast<Volatile *>(u.textures[i]);
- if (v != nullptr)
- v->loadVolatile();
- }
- sendTextures(&u, u.textures, u.count, true);
- }
- else if (u.baseType == UNIFORM_TEXELBUFFER)
- {
- for (int i = 0; i < u.count; i++)
- {
- if (u.buffers[i] == nullptr)
- continue;
- Volatile *v = dynamic_cast<Volatile *>(u.buffers[i]);
- if (v != nullptr)
- v->loadVolatile();
- }
- sendBuffers(&u, u.buffers, u.count, true);
- }
- }
- if (gl.isBufferUsageSupported(BUFFERUSAGE_SHADER_STORAGE))
- {
- GLint numstoragebuffers = 0;
- glGetProgramInterfaceiv(program, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &numstoragebuffers);
- char namebuffer[2048] = { '\0' };
- for (int sindex = 0; sindex < numstoragebuffers; sindex++)
- {
- UniformInfo u = {};
- u.baseType = UNIFORM_STORAGEBUFFER;
- u.access = ACCESS_READ;
- GLsizei namelength = 0;
- glGetProgramResourceName(program, GL_SHADER_STORAGE_BLOCK, sindex, 2048, &namelength, namebuffer);
- u.name = std::string(namebuffer, namelength);
- u.count = 1;
- const auto reflectionit = validationReflection.storageBuffers.find(u.name);
- if (reflectionit != validationReflection.storageBuffers.end())
- {
- u.bufferStride = reflectionit->second.stride;
- u.bufferMemberCount = reflectionit->second.memberCount;
- u.access = reflectionit->second.access;
- }
- // Make sure previously set uniform data is preserved, and shader-
- // initialized values are retrieved.
- auto oldu = olduniforms.find(u.name);
- if (oldu != olduniforms.end())
- {
- u.data = oldu->second.data;
- u.dataSize = oldu->second.dataSize;
- u.buffers = oldu->second.buffers;
- }
- else
- {
- u.dataSize = sizeof(int) * 1;
- u.data = malloc(u.dataSize);
- u.ints[0] = -1;
- u.buffers = new love::graphics::Buffer * [u.count];
- memset(u.buffers, 0, sizeof(Buffer*)* u.count);
- }
- GLenum props[] = { GL_BUFFER_BINDING };
- glGetProgramResourceiv(program, GL_SHADER_STORAGE_BLOCK, sindex, 1, props, 1, nullptr, u.ints);
- BufferBinding binding;
- binding.bindingindex = u.ints[0];
- binding.buffer = gl.getDefaultStorageBuffer();
- if (binding.bindingindex >= 0)
- {
- int activeindex = (int)activeStorageBufferBindings.size();
- activeStorageBufferBindings.push_back(binding);
- auto p = std::make_pair(activeindex, -1);
- if (u.access & ACCESS_WRITE)
- {
- p.second = (int)activeWritableStorageBuffers.size();
- activeWritableStorageBuffers.push_back(u.buffers[0]);
- }
- storageBufferBindingIndexToActiveBinding[binding.bindingindex] = p;
- }
- uniforms[u.name] = u;
- for (int i = 0; i < u.count; i++)
- {
- if (u.buffers[i] == nullptr)
- continue;
- Volatile* v = dynamic_cast<Volatile*>(u.buffers[i]);
- if (v != nullptr)
- v->loadVolatile();
- }
- sendBuffers(&u, u.buffers, u.count, true);
- }
- }
- // Make sure uniforms that existed before but don't exist anymore are
- // cleaned up. This theoretically shouldn't happen, but...
- for (const auto &p : olduniforms)
- {
- if (uniforms.find(p.first) == uniforms.end())
- {
- if (p.second.data != nullptr)
- free(p.second.data);
- if (p.second.baseType == UNIFORM_SAMPLER)
- {
- for (int i = 0; i < p.second.count; i++)
- {
- if (p.second.textures[i] != nullptr)
- p.second.textures[i]->release();
- }
- delete[] p.second.textures;
- }
- else if (isBuffer(p.second.baseType))
- {
- for (int i = 0; i < p.second.count; i++)
- {
- if (p.second.buffers[i] != nullptr)
- p.second.buffers[i]->release();
- }
- delete[] p.second.buffers;
- }
- }
- }
- gl.useProgram(activeprogram);
- }
- bool Shader::loadVolatile()
- {
- OpenGL::TempDebugGroup debuggroup("Shader load");
- // zero out active texture list
- textureUnits.clear();
- textureUnits.push_back(TextureUnit());
- storageBufferBindingIndexToActiveBinding.resize(gl.getMaxShaderStorageBufferBindings(), std::make_pair(-1, -1));
- activeStorageBufferBindings.clear();
- activeWritableStorageBuffers.clear();
- for (const auto &stage : stages)
- {
- if (stage.get() != nullptr)
- stage->loadVolatile();
- }
- program = glCreateProgram();
- if (program == 0)
- throw love::Exception("Cannot create shader program object.");
- for (const auto &stage : stages)
- {
- if (stage.get() != nullptr)
- glAttachShader(program, (GLuint) stage->getHandle());
- }
- // Bind generic vertex attribute indices to names in the shader.
- for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++)
- {
- const char *name = nullptr;
- if (graphics::getConstant((BuiltinVertexAttribute) i, name))
- glBindAttribLocation(program, i, (const GLchar *) name);
- }
- glLinkProgram(program);
- GLint status;
- glGetProgramiv(program, GL_LINK_STATUS, &status);
- if (status == GL_FALSE)
- {
- std::string warnings = getProgramWarnings();
- glDeleteProgram(program);
- program = 0;
- throw love::Exception("Cannot link shader program object:\n%s", warnings.c_str());
- }
- // Get all active uniform variables in this shader from OpenGL.
- mapActiveUniforms();
- for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++)
- {
- const char *name = nullptr;
- if (graphics::getConstant(BuiltinVertexAttribute(i), name))
- builtinAttributes[i] = glGetAttribLocation(program, name);
- else
- builtinAttributes[i] = -1;
- }
- if (current == this)
- {
- // make sure glUseProgram gets called.
- current = nullptr;
- attach();
- }
- return true;
- }
- void Shader::unloadVolatile()
- {
- if (program != 0)
- {
- if (current == this)
- gl.useProgram(0);
- glDeleteProgram(program);
- program = 0;
- }
- // active texture list is probably invalid, clear it
- textureUnits.clear();
- textureUnits.push_back(TextureUnit());
- attributes.clear();
- // And the locations of any built-in uniform variables.
- for (int i = 0; i < int(BUILTIN_MAX_ENUM); i++)
- builtinUniforms[i] = -1;
- }
- std::string Shader::getProgramWarnings() const
- {
- GLint strsize, nullpos;
- glGetProgramiv(program, GL_INFO_LOG_LENGTH, &strsize);
- if (strsize == 0)
- return "";
- char *tempstr = new char[strsize];
- // be extra sure that the error string will be 0-terminated
- memset(tempstr, '\0', strsize);
- glGetProgramInfoLog(program, strsize, &nullpos, tempstr);
- tempstr[nullpos] = '\0';
- std::string warnings(tempstr);
- delete[] tempstr;
- return warnings;
- }
- std::string Shader::getWarnings() const
- {
- std::string warnings;
- const char *stagestr;
- for (const auto &stage : stages)
- {
- if (stage.get() == nullptr)
- continue;
- const std::string &stagewarnings = stage->getWarnings();
- if (ShaderStage::getConstant(stage->getStageType(), stagestr))
- warnings += std::string(stagestr) + std::string(" shader:\n") + stagewarnings;
- }
- warnings += getProgramWarnings();
- return warnings;
- }
- void Shader::attach()
- {
- if (current != this)
- {
- Graphics::flushBatchedDrawsGlobal();
- gl.useProgram(program);
- current = this;
- // retain/release happens in Graphics::setShader.
- // Make sure all textures are bound to their respective texture units.
- for (int i = 0; i < (int) textureUnits.size(); ++i)
- {
- const TextureUnit &unit = textureUnits[i];
- if (unit.active)
- {
- if (unit.isTexelBuffer)
- gl.bindBufferTextureToUnit(unit.texture, i, false, false);
- else
- gl.bindTextureToUnit(unit.type, unit.texture, i, false, false);
- }
- }
- for (auto bufferbinding : activeStorageBufferBindings)
- gl.bindIndexedBuffer(bufferbinding.buffer, BUFFERUSAGE_SHADER_STORAGE, bufferbinding.bindingindex);
- // send any pending uniforms to the shader program.
- for (const auto &p : pendingUniformUpdates)
- updateUniform(p.first, p.second, true);
- pendingUniformUpdates.clear();
- }
- }
- const Shader::UniformInfo *Shader::getUniformInfo(const std::string &name) const
- {
- const auto it = uniforms.find(name);
- if (it == uniforms.end())
- return nullptr;
- return &(it->second);
- }
- const Shader::UniformInfo *Shader::getUniformInfo(BuiltinUniform builtin) const
- {
- return builtinUniformInfo[(int)builtin];
- }
- void Shader::updateUniform(const UniformInfo *info, int count)
- {
- updateUniform(info, count, false);
- }
- void Shader::updateUniform(const UniformInfo *info, int count, bool internalupdate)
- {
- if (current != this && !internalupdate)
- {
- pendingUniformUpdates.push_back(std::make_pair(info, count));
- return;
- }
- if (!internalupdate)
- flushBatchedDraws();
- int location = info->location;
- UniformType type = info->baseType;
- if (type == UNIFORM_FLOAT)
- {
- switch (info->components)
- {
- case 1:
- glUniform1fv(location, count, info->floats);
- break;
- case 2:
- glUniform2fv(location, count, info->floats);
- break;
- case 3:
- glUniform3fv(location, count, info->floats);
- break;
- case 4:
- glUniform4fv(location, count, info->floats);
- break;
- }
- }
- else if (type == UNIFORM_INT || type == UNIFORM_BOOL || type == UNIFORM_SAMPLER || type == UNIFORM_TEXELBUFFER)
- {
- switch (info->components)
- {
- case 1:
- glUniform1iv(location, count, info->ints);
- break;
- case 2:
- glUniform2iv(location, count, info->ints);
- break;
- case 3:
- glUniform3iv(location, count, info->ints);
- break;
- case 4:
- glUniform4iv(location, count, info->ints);
- break;
- }
- }
- else if (type == UNIFORM_UINT)
- {
- switch (info->components)
- {
- case 1:
- glUniform1uiv(location, count, info->uints);
- break;
- case 2:
- glUniform2uiv(location, count, info->uints);
- break;
- case 3:
- glUniform3uiv(location, count, info->uints);
- break;
- case 4:
- glUniform4uiv(location, count, info->uints);
- break;
- }
- }
- else if (type == UNIFORM_MATRIX)
- {
- int columns = info->matrix.columns;
- int rows = info->matrix.rows;
- if (columns == 2 && rows == 2)
- glUniformMatrix2fv(location, count, GL_FALSE, info->floats);
- else if (columns == 3 && rows == 3)
- glUniformMatrix3fv(location, count, GL_FALSE, info->floats);
- else if (columns == 4 && rows == 4)
- glUniformMatrix4fv(location, count, GL_FALSE, info->floats);
- else if (columns == 2 && rows == 3)
- glUniformMatrix2x3fv(location, count, GL_FALSE, info->floats);
- else if (columns == 2 && rows == 4)
- glUniformMatrix2x4fv(location, count, GL_FALSE, info->floats);
- else if (columns == 3 && rows == 2)
- glUniformMatrix3x2fv(location, count, GL_FALSE, info->floats);
- else if (columns == 3 && rows == 4)
- glUniformMatrix3x4fv(location, count, GL_FALSE, info->floats);
- else if (columns == 4 && rows == 2)
- glUniformMatrix4x2fv(location, count, GL_FALSE, info->floats);
- else if (columns == 4 && rows == 3)
- glUniformMatrix4x3fv(location, count, GL_FALSE, info->floats);
- }
- }
- static bool isResourceBaseTypeCompatible(DataBaseType a, DataBaseType b)
- {
- if (a == DATA_BASETYPE_FLOAT || a == DATA_BASETYPE_UNORM || a == DATA_BASETYPE_SNORM)
- return b == DATA_BASETYPE_FLOAT || b == DATA_BASETYPE_UNORM || b == DATA_BASETYPE_SNORM;
- if (a == DATA_BASETYPE_INT && b == DATA_BASETYPE_INT)
- return true;
- if (a == DATA_BASETYPE_UINT && b == DATA_BASETYPE_UINT)
- return true;
- return false;
- }
- static DataBaseType getDataBaseType(PixelFormat format)
- {
- switch (getPixelFormatInfo(format).dataType)
- {
- case PIXELFORMATTYPE_UNORM:
- return DATA_BASETYPE_UNORM;
- case PIXELFORMATTYPE_SNORM:
- return DATA_BASETYPE_SNORM;
- case PIXELFORMATTYPE_UFLOAT:
- case PIXELFORMATTYPE_SFLOAT:
- return DATA_BASETYPE_FLOAT;
- case PIXELFORMATTYPE_SINT:
- return DATA_BASETYPE_INT;
- case PIXELFORMATTYPE_UINT:
- return DATA_BASETYPE_UINT;
- default:
- return DATA_BASETYPE_FLOAT;
- }
- }
- void Shader::sendTextures(const UniformInfo *info, love::graphics::Texture **textures, int count)
- {
- Shader::sendTextures(info, textures, count, false);
- }
- void Shader::sendTextures(const UniformInfo *info, love::graphics::Texture **textures, int count, bool internalUpdate)
- {
- if (info->baseType != UNIFORM_SAMPLER)
- return;
- bool shaderactive = current == this;
- if (!internalUpdate && shaderactive)
- flushBatchedDraws();
- count = std::min(count, info->count);
- // Bind the textures to the texture units.
- for (int i = 0; i < count; i++)
- {
- love::graphics::Texture *tex = textures[i];
- if (tex != nullptr)
- {
- if (!tex->isReadable())
- {
- if (internalUpdate)
- continue;
- else
- throw love::Exception("Textures with non-readable formats cannot be sampled from in a shader.");
- }
- else if (info->isDepthSampler != tex->getSamplerState().depthSampleMode.hasValue)
- {
- if (internalUpdate)
- continue;
- else if (info->isDepthSampler)
- throw love::Exception("Depth comparison samplers in shaders can only be used with depth textures which have depth comparison set.");
- else
- throw love::Exception("Depth textures which have depth comparison set can only be used with depth/shadow samplers in shaders.");
- }
- else if (tex->getTextureType() != info->textureType)
- {
- if (internalUpdate)
- continue;
- else
- {
- const char *textypestr = "unknown";
- const char *shadertextypestr = "unknown";
- Texture::getConstant(tex->getTextureType(), textypestr);
- Texture::getConstant(info->textureType, shadertextypestr);
- throw love::Exception("Texture's type (%s) must match the type of %s (%s).", textypestr, info->name.c_str(), shadertextypestr);
- }
- }
- else if (!isResourceBaseTypeCompatible(info->dataBaseType, getDataBaseType(tex->getPixelFormat())))
- {
- if (internalUpdate)
- continue;
- else
- throw love::Exception("Texture's data format base type must match the uniform variable declared in the shader (float, int, or uint).");
- }
- tex->retain();
- }
- if (info->textures[i] != nullptr)
- info->textures[i]->release();
- info->textures[i] = tex;
- GLuint gltex = 0;
- if (textures[i] != nullptr)
- gltex = (GLuint) tex->getHandle();
- else
- gltex = gl.getDefaultTexture(info->textureType, info->dataBaseType);
- int texunit = info->ints[i];
- if (shaderactive)
- gl.bindTextureToUnit(info->textureType, gltex, texunit, false, false);
- // Store texture id so it can be re-bound to the texture unit later.
- textureUnits[texunit].texture = gltex;
- }
- }
- void Shader::sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffers, int count)
- {
- Shader::sendBuffers(info, buffers, count, false);
- }
- void Shader::sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffers, int count, bool internalUpdate)
- {
- uint32 requiredtypeflags = 0;
- bool texelbinding = info->baseType == UNIFORM_TEXELBUFFER;
- bool storagebinding = info->baseType == UNIFORM_STORAGEBUFFER;
- if (texelbinding)
- requiredtypeflags = BUFFERUSAGEFLAG_TEXEL;
- else if (storagebinding)
- requiredtypeflags = BUFFERUSAGEFLAG_SHADER_STORAGE;
- if (requiredtypeflags == 0)
- return;
- bool shaderactive = current == this;
- if (!internalUpdate && shaderactive)
- flushBatchedDraws();
- count = std::min(count, info->count);
- // Bind the textures to the texture units.
- for (int i = 0; i < count; i++)
- {
- love::graphics::Buffer *buffer = buffers[i];
- if (buffer != nullptr)
- {
- if ((buffer->getUsageFlags() & requiredtypeflags) == 0)
- {
- if (internalUpdate)
- continue;
- else if (texelbinding)
- throw love::Exception("Shader uniform '%s' is a texel buffer, but the given Buffer was not created with texel buffer capabilities.", info->name.c_str());
- else if (storagebinding)
- throw love::Exception("Shader uniform '%s' is a shader storage buffer block, but the given Buffer was not created with shader storage buffer capabilities.", info->name.c_str());
- else
- throw love::Exception("Shader uniform '%s' does not match the types supported by the given Buffer.", info->name.c_str());
- }
- if (texelbinding)
- {
- DataBaseType basetype = buffer->getDataMember(0).info.baseType;
- if (!isResourceBaseTypeCompatible(basetype, info->dataBaseType))
- {
- if (internalUpdate)
- continue;
- else
- throw love::Exception("Texel buffer's data format base type must match the variable declared in the shader.");
- }
- }
- else if (storagebinding)
- {
- if (info->bufferStride != buffer->getArrayStride())
- {
- if (internalUpdate)
- continue;
- else
- throw love::Exception("Shader storage block '%s' has an array stride of %d bytes, but the given Buffer has an array stride of %d bytes.",
- info->name.c_str(), info->bufferStride, buffer->getArrayStride());
- }
- else if (info->bufferMemberCount != buffer->getDataMembers().size())
- {
- if (internalUpdate)
- continue;
- else
- throw love::Exception("Shader storage block '%s' has a struct with %d fields, but the given Buffer has a format with %d members.",
- info->name.c_str(), info->bufferMemberCount, buffer->getDataMembers().size());
- }
- }
- buffer->retain();
- }
- if (info->buffers[i] != nullptr)
- info->buffers[i]->release();
- info->buffers[i] = buffer;
- if (texelbinding)
- {
- GLuint gltex = 0;
- if (buffer != nullptr)
- gltex = (GLuint) buffer->getTexelBufferHandle();
- else
- gltex = gl.getDefaultTexelBuffer();
- int texunit = info->ints[i];
- if (shaderactive)
- gl.bindBufferTextureToUnit(gltex, texunit, false, false);
- // Store texture id so it can be re-bound to the texture unit later.
- textureUnits[texunit].texture = gltex;
- }
- else if (storagebinding)
- {
- int bindingindex = info->ints[i];
- GLuint glbuffer = 0;
- if (buffer != nullptr)
- glbuffer = (GLuint) buffer->getHandle();
- else
- glbuffer = gl.getDefaultStorageBuffer();
- if (shaderactive)
- gl.bindIndexedBuffer(glbuffer, BUFFERUSAGE_SHADER_STORAGE, bindingindex);
- auto activeindex = storageBufferBindingIndexToActiveBinding[bindingindex];
- if (activeindex.first >= 0)
- activeStorageBufferBindings[activeindex.first].buffer = glbuffer;
- if (activeindex.second >= 0)
- activeWritableStorageBuffers[activeindex.second] = buffer;
- }
- }
- }
- void Shader::flushBatchedDraws() const
- {
- if (current == this)
- Graphics::flushBatchedDrawsGlobal();
- }
- bool Shader::hasUniform(const std::string &name) const
- {
- return uniforms.find(name) != uniforms.end();
- }
- ptrdiff_t Shader::getHandle() const
- {
- return program;
- }
- int Shader::getVertexAttributeIndex(const std::string &name)
- {
- auto it = attributes.find(name);
- if (it != attributes.end())
- return it->second;
- GLint location = glGetAttribLocation(program, name.c_str());
- attributes[name] = location;
- return location;
- }
- void Shader::setVideoTextures(love::graphics::Texture *ytexture, love::graphics::Texture *cbtexture, love::graphics::Texture *crtexture)
- {
- const BuiltinUniform builtins[3] = {
- BUILTIN_TEXTURE_VIDEO_Y,
- BUILTIN_TEXTURE_VIDEO_CB,
- BUILTIN_TEXTURE_VIDEO_CR,
- };
- love::graphics::Texture *textures[3] = {ytexture, cbtexture, crtexture};
- for (int i = 0; i < 3; i++)
- {
- const UniformInfo *info = builtinUniformInfo[builtins[i]];
- if (info != nullptr)
- sendTextures(info, &textures[i], 1, true);
- }
- }
- void Shader::updateBuiltinUniforms(love::graphics::Graphics *gfx, int viewportW, int viewportH)
- {
- if (current != this)
- return;
- BuiltinUniformData data;
- data.transformMatrix = gfx->getTransform();
- data.projectionMatrix = gfx->getProjection();
- // The normal matrix is the transpose of the inverse of the rotation portion
- // (top-left 3x3) of the transform matrix.
- {
- Matrix3 normalmatrix = Matrix3(data.transformMatrix).transposedInverse();
- const float *e = normalmatrix.getElements();
- for (int i = 0; i < 3; i++)
- {
- data.normalMatrix[i].x = e[i * 3 + 0];
- data.normalMatrix[i].y = e[i * 3 + 1];
- data.normalMatrix[i].z = e[i * 3 + 2];
- data.normalMatrix[i].w = 0.0f;
- }
- }
- // Store DPI scale in an unused component of another vector.
- data.normalMatrix[0].w = (float) gfx->getCurrentDPIScale();
- // Same with point size.
- data.normalMatrix[1].w = gfx->getPointSize();
- data.screenSizeParams.x = viewportW;
- data.screenSizeParams.y = viewportH;
- // The shader does pixcoord.y = gl_FragCoord.y * params.z + params.w.
- // This lets us flip pixcoord.y when needed, to be consistent (drawing
- // with no RT active makes the pixel coordinates y-flipped.)
- if (gfx->isRenderTargetActive())
- {
- // No flipping: pixcoord.y = gl_FragCoord.y * 1.0 + 0.0.
- data.screenSizeParams.z = 1.0f;
- data.screenSizeParams.w = 0.0f;
- }
- else
- {
- // gl_FragCoord.y is flipped when drawing to the screen, so we
- // un-flip: pixcoord.y = gl_FragCoord.y * -1.0 + height.
- data.screenSizeParams.z = -1.0f;
- data.screenSizeParams.w = viewportH;
- }
- data.constantColor = gfx->getColor();
- gammaCorrectColor(data.constantColor);
- GLint location = builtinUniforms[BUILTIN_UNIFORMS_PER_DRAW];
- if (location >= 0)
- glUniform4fv(location, 13, (const GLfloat *) &data);
- }
- int Shader::getUniformTypeComponents(GLenum type) const
- {
- switch (type)
- {
- case GL_INT:
- case GL_UNSIGNED_INT:
- case GL_FLOAT:
- case GL_BOOL:
- return 1;
- case GL_INT_VEC2:
- case GL_UNSIGNED_INT_VEC2:
- case GL_FLOAT_VEC2:
- case GL_FLOAT_MAT2:
- case GL_BOOL_VEC2:
- return 2;
- case GL_INT_VEC3:
- case GL_UNSIGNED_INT_VEC3:
- case GL_FLOAT_VEC3:
- case GL_FLOAT_MAT3:
- case GL_BOOL_VEC3:
- return 3;
- case GL_INT_VEC4:
- case GL_UNSIGNED_INT_VEC4:
- case GL_FLOAT_VEC4:
- case GL_FLOAT_MAT4:
- case GL_BOOL_VEC4:
- return 4;
- default:
- return 1;
- }
- }
- Shader::MatrixSize Shader::getMatrixSize(GLenum type) const
- {
- MatrixSize m;
- switch (type)
- {
- case GL_FLOAT_MAT2:
- m.columns = m.rows = 2;
- break;
- case GL_FLOAT_MAT3:
- m.columns = m.rows = 3;
- break;
- case GL_FLOAT_MAT4:
- m.columns = m.rows = 4;
- break;
- case GL_FLOAT_MAT2x3:
- m.columns = 2;
- m.rows = 3;
- break;
- case GL_FLOAT_MAT2x4:
- m.columns = 2;
- m.rows = 4;
- break;
- case GL_FLOAT_MAT3x2:
- m.columns = 3;
- m.rows = 2;
- break;
- case GL_FLOAT_MAT3x4:
- m.columns = 3;
- m.rows = 4;
- break;
- case GL_FLOAT_MAT4x2:
- m.columns = 4;
- m.rows = 2;
- break;
- case GL_FLOAT_MAT4x3:
- m.columns = 4;
- m.rows = 3;
- break;
- }
- return m;
- }
- void Shader::computeUniformTypeInfo(GLenum type, UniformInfo &u)
- {
- u.isDepthSampler = false;
- u.components = getUniformTypeComponents(type);
- u.baseType = UNIFORM_UNKNOWN;
- switch (type)
- {
- case GL_INT:
- case GL_INT_VEC2:
- case GL_INT_VEC3:
- case GL_INT_VEC4:
- u.baseType = UNIFORM_INT;
- u.dataBaseType = DATA_BASETYPE_INT;
- break;
- case GL_UNSIGNED_INT:
- case GL_UNSIGNED_INT_VEC2:
- case GL_UNSIGNED_INT_VEC3:
- case GL_UNSIGNED_INT_VEC4:
- u.baseType = UNIFORM_UINT;
- u.dataBaseType = DATA_BASETYPE_UINT;
- break;
- case GL_FLOAT:
- case GL_FLOAT_VEC2:
- case GL_FLOAT_VEC3:
- case GL_FLOAT_VEC4:
- u.baseType = UNIFORM_FLOAT;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- break;
- case GL_FLOAT_MAT2:
- case GL_FLOAT_MAT3:
- case GL_FLOAT_MAT4:
- case GL_FLOAT_MAT2x3:
- case GL_FLOAT_MAT2x4:
- case GL_FLOAT_MAT3x2:
- case GL_FLOAT_MAT3x4:
- case GL_FLOAT_MAT4x2:
- case GL_FLOAT_MAT4x3:
- u.baseType = UNIFORM_MATRIX;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.matrix = getMatrixSize(type);
- break;
- case GL_BOOL:
- case GL_BOOL_VEC2:
- case GL_BOOL_VEC3:
- case GL_BOOL_VEC4:
- u.baseType = UNIFORM_BOOL;
- u.dataBaseType = DATA_BASETYPE_BOOL;
- break;
- case GL_SAMPLER_2D:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_2D;
- break;
- case GL_SAMPLER_2D_SHADOW:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_2D;
- u.isDepthSampler = true;
- break;
- case GL_INT_SAMPLER_2D:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_INT;
- u.textureType = TEXTURE_2D;
- break;
- case GL_UNSIGNED_INT_SAMPLER_2D:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_UINT;
- u.textureType = TEXTURE_2D;
- break;
- case GL_SAMPLER_2D_ARRAY:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_2D_ARRAY;
- break;
- case GL_SAMPLER_2D_ARRAY_SHADOW:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_2D_ARRAY;
- u.isDepthSampler = true;
- break;
- case GL_INT_SAMPLER_2D_ARRAY:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_INT;
- u.textureType = TEXTURE_2D_ARRAY;
- break;
- case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_UINT;
- u.textureType = TEXTURE_2D_ARRAY;
- break;
- case GL_SAMPLER_3D:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_VOLUME;
- break;
- case GL_INT_SAMPLER_3D:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_INT;
- u.textureType = TEXTURE_VOLUME;
- break;
- case GL_UNSIGNED_INT_SAMPLER_3D:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_UINT;
- u.textureType = TEXTURE_VOLUME;
- break;
- case GL_SAMPLER_CUBE:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_CUBE;
- break;
- case GL_SAMPLER_CUBE_SHADOW:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- u.textureType = TEXTURE_CUBE;
- u.isDepthSampler = true;
- break;
- case GL_INT_SAMPLER_CUBE:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_INT;
- u.textureType = TEXTURE_CUBE;
- break;
- case GL_UNSIGNED_INT_SAMPLER_CUBE:
- u.baseType = UNIFORM_SAMPLER;
- u.dataBaseType = DATA_BASETYPE_UINT;
- u.textureType = TEXTURE_CUBE;
- break;
- case GL_SAMPLER_BUFFER:
- u.baseType = UNIFORM_TEXELBUFFER;
- u.dataBaseType = DATA_BASETYPE_FLOAT;
- break;
- case GL_INT_SAMPLER_BUFFER:
- u.baseType = UNIFORM_TEXELBUFFER;
- u.dataBaseType = DATA_BASETYPE_INT;
- break;
- case GL_UNSIGNED_INT_SAMPLER_BUFFER:
- u.baseType = UNIFORM_TEXELBUFFER;
- u.dataBaseType = DATA_BASETYPE_UINT;
- break;
- default:
- break;
- }
- }
- } // opengl
- } // graphics
- } // love
|