12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142 |
- /**
- * Copyright (c) 2006-2020 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 "ShaderStage.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(love::graphics::ShaderStage *vertex, love::graphics::ShaderStage *pixel)
- : love::graphics::Shader(vertex, pixel)
- , program(0)
- , builtinUniforms()
- , builtinUniformInfo()
- {
- // 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.baseType = getUniformBaseType(gltype);
- u.textureType = getUniformTextureType(gltype);
- u.dataBaseType = getUniformTexelBaseType(gltype);
- u.isDepthSampler = isDepthTextureType(gltype);
- if (u.baseType == UNIFORM_MATRIX)
- u.matrix = getMatrixSize(gltype);
- else
- u.components = getUniformTypeComponents(gltype);
- // 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);
- }
- 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;
- 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;
- }
- // 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();
- storageBufferBindingIndexToActiveBinding[binding.bindingindex] = activeindex;
- activeStorageBufferBindings.push_back(binding);
- }
- 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(), -1);
- activeStorageBufferBindings.clear();
- for (const auto &stage : stages)
- {
- if (stage.get() != nullptr)
- ((ShaderStage*)stage.get())->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();
- 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);
- }
- }
- 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 (!validateTexture(info, tex, internalUpdate))
- continue;
- 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);
- 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)
- {
- bool texelbinding = info->baseType == UNIFORM_TEXELBUFFER;
- bool storagebinding = info->baseType == UNIFORM_STORAGEBUFFER;
- if (!texelbinding && !storagebinding)
- 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 (!validateBuffer(info, buffer, internalUpdate))
- continue;
- buffer->retain();
- }
- if (info->buffers[i] != nullptr)
- info->buffers[i]->release();
- info->buffers[i] = buffer;
- if (texelbinding)
- {
- GLuint gltex = 0;
- if (buffers[i] != 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 (buffers[i] != nullptr)
- glbuffer = (GLuint) buffer->getHandle();
- else
- glbuffer = gl.getDefaultStorageBuffer();
- if (shaderactive)
- gl.bindIndexedBuffer(glbuffer, BUFFERUSAGE_SHADER_STORAGE, bindingindex);
- int activeindex = storageBufferBindingIndexToActiveBinding[bindingindex];
- if (activeindex >= 0)
- activeStorageBufferBindings[activeindex].buffer = glbuffer;
- }
- }
- }
- 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
- {
- UniformType basetype = getUniformBaseType(type);
- if (basetype == UNIFORM_SAMPLER || basetype == UNIFORM_TEXELBUFFER)
- return 1;
- 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;
- }
- Shader::UniformType Shader::getUniformBaseType(GLenum type) const
- {
- switch (type)
- {
- case GL_INT:
- case GL_INT_VEC2:
- case GL_INT_VEC3:
- case GL_INT_VEC4:
- return UNIFORM_INT;
- case GL_UNSIGNED_INT:
- case GL_UNSIGNED_INT_VEC2:
- case GL_UNSIGNED_INT_VEC3:
- case GL_UNSIGNED_INT_VEC4:
- return UNIFORM_UINT;
- case GL_FLOAT:
- case GL_FLOAT_VEC2:
- case GL_FLOAT_VEC3:
- case GL_FLOAT_VEC4:
- return UNIFORM_FLOAT;
- 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:
- return UNIFORM_MATRIX;
- case GL_BOOL:
- case GL_BOOL_VEC2:
- case GL_BOOL_VEC3:
- case GL_BOOL_VEC4:
- return UNIFORM_BOOL;
- case GL_SAMPLER_1D:
- case GL_SAMPLER_1D_SHADOW:
- case GL_SAMPLER_1D_ARRAY:
- case GL_SAMPLER_1D_ARRAY_SHADOW:
- case GL_SAMPLER_2D:
- case GL_SAMPLER_2D_MULTISAMPLE:
- case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
- case GL_SAMPLER_2D_RECT:
- case GL_SAMPLER_2D_RECT_SHADOW:
- case GL_SAMPLER_2D_SHADOW:
- case GL_SAMPLER_2D_ARRAY:
- case GL_SAMPLER_2D_ARRAY_SHADOW:
- case GL_SAMPLER_3D:
- case GL_SAMPLER_CUBE:
- case GL_SAMPLER_CUBE_SHADOW:
- case GL_SAMPLER_CUBE_MAP_ARRAY:
- case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
- return UNIFORM_SAMPLER;
- case GL_SAMPLER_BUFFER:
- case GL_INT_SAMPLER_BUFFER:
- case GL_UNSIGNED_INT_SAMPLER_BUFFER:
- return UNIFORM_TEXELBUFFER;
- default:
- return UNIFORM_UNKNOWN;
- }
- }
- TextureType Shader::getUniformTextureType(GLenum type) const
- {
- switch (type)
- {
- case GL_SAMPLER_1D:
- case GL_SAMPLER_1D_SHADOW:
- case GL_SAMPLER_1D_ARRAY:
- case GL_SAMPLER_1D_ARRAY_SHADOW:
- // 1D-typed textures are not supported.
- return TEXTURE_MAX_ENUM;
- case GL_SAMPLER_2D:
- case GL_SAMPLER_2D_SHADOW:
- return TEXTURE_2D;
- case GL_SAMPLER_2D_MULTISAMPLE:
- case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
- // Multisample textures are not supported.
- return TEXTURE_MAX_ENUM;
- case GL_SAMPLER_2D_RECT:
- case GL_SAMPLER_2D_RECT_SHADOW:
- // Rectangle textures are not supported.
- return TEXTURE_MAX_ENUM;
- case GL_SAMPLER_2D_ARRAY:
- case GL_SAMPLER_2D_ARRAY_SHADOW:
- return TEXTURE_2D_ARRAY;
- case GL_SAMPLER_3D:
- return TEXTURE_VOLUME;
- case GL_SAMPLER_CUBE:
- case GL_SAMPLER_CUBE_SHADOW:
- return TEXTURE_CUBE;
- case GL_SAMPLER_CUBE_MAP_ARRAY:
- case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
- // Cubemap array textures are not supported.
- return TEXTURE_MAX_ENUM;
- default:
- return TEXTURE_MAX_ENUM;
- }
- }
- DataBaseType Shader::getUniformTexelBaseType(GLenum type) const
- {
- switch (type)
- {
- case GL_SAMPLER_BUFFER:
- return DATA_BASETYPE_FLOAT;
- case GL_INT_SAMPLER_BUFFER:
- return DATA_BASETYPE_INT;
- case GL_UNSIGNED_INT_SAMPLER_BUFFER:
- return DATA_BASETYPE_UINT;
- default:
- return DATA_BASETYPE_MAX_ENUM;
- }
- }
- bool Shader::isDepthTextureType(GLenum type) const
- {
- switch (type)
- {
- case GL_SAMPLER_1D_SHADOW:
- case GL_SAMPLER_1D_ARRAY_SHADOW:
- case GL_SAMPLER_2D_SHADOW:
- case GL_SAMPLER_2D_ARRAY_SHADOW:
- case GL_SAMPLER_CUBE_SHADOW:
- case GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW:
- return true;
- default:
- return false;
- }
- }
- } // opengl
- } // graphics
- } // love
|