Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sampler settings to more types of textures. #269

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 28 additions & 32 deletions src/SHADERed/Objects/ObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,12 @@ namespace ed {
glGenTextures(1, &item->Texture);
glBindTexture(GL_TEXTURE_2D, item->Texture);
glTexImage2D(GL_TEXTURE_2D, 0, rtObj->Format, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);

// depth texture
glGenTextures(1, &rtObj->DepthStencilBuffer);
glBindTexture(GL_TEXTURE_2D, rtObj->DepthStencilBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, size.x, size.y, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);

// color texture ms
Expand All @@ -255,6 +251,8 @@ namespace ed {
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, Settings::Instance().Preview.MSAA, GL_DEPTH24_STENCIL8, size.x, size.y, true);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);

UpdateTextureParameters(item);

return true;
}
bool ObjectManager::CreateTexture(const std::string& file)
Expand Down Expand Up @@ -298,10 +296,6 @@ namespace ed {
// normal texture
glGenTextures(1, &item->Texture);
glBindTexture(GL_TEXTURE_2D, item->Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
Expand All @@ -320,16 +314,14 @@ namespace ed {

glGenTextures(1, &item->FlippedTexture);
glBindTexture(GL_TEXTURE_2D, item->FlippedTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, flippedData);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

item->TextureSize = glm::ivec2(width, height);

UpdateTextureParameters(item);

free(flippedData);

if (isDDS)
Expand Down Expand Up @@ -363,18 +355,15 @@ namespace ed {

glGenTextures(1, &item->Texture);
glBindTexture(GL_TEXTURE_3D, item->Texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, item->Texture_WrapR);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, ddsImage->header.width, ddsImage->header.height, ddsImage->header.depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, ddsImage->pixels);
glGenerateMipmap(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, 0);

item->TextureSize = glm::ivec2(ddsImage->header.width, ddsImage->header.height);
item->Depth = ddsImage->header.depth;

UpdateTextureParameters(item);

dds_image_free(ddsImage);

return true;
Expand All @@ -398,11 +387,7 @@ namespace ed {
int width, height;

// properties
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
UpdateTextureParameters(item);

// left face
loadCubemapFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, m_parser->GetProjectPath(left), width, height);
Expand Down Expand Up @@ -517,8 +502,10 @@ namespace ed {

glGenTextures(1, &item->Texture);
glBindTexture(GL_TEXTURE_2D, item->Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);

Expand Down Expand Up @@ -548,8 +535,11 @@ namespace ed {

glGenTextures(1, &item->Texture);
glBindTexture(GL_TEXTURE_3D, item->Texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, item->Texture_WrapR);
glTexImage3D(GL_TEXTURE_3D, 0, iObj->Format, size.x, size.y, size.z, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_3D, 0);

Expand Down Expand Up @@ -1259,12 +1249,10 @@ namespace ed {
item->Texture_VFlipped = !item->Texture_VFlipped;
}
}
void ObjectManager::UpdateTextureParameters(const std::string& name)
void ObjectManager::UpdateTextureParameters(ObjectManagerItem* item)
{
ObjectManagerItem* item = Get(name);

if (item != nullptr) {
if (item->Type == ed::ObjectType::Texture) {
if (item->Type == ed::ObjectType::Texture || item->Type == ed::ObjectType::Image || item->Type == ed::ObjectType::RenderTexture) {
glBindTexture(GL_TEXTURE_2D, item->Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
Expand All @@ -1279,15 +1267,23 @@ namespace ed {

glBindTexture(GL_TEXTURE_2D, 0);
}

else if (item->Type == ed::ObjectType::Texture3D) {
else if (item->Type == ed::ObjectType::Texture3D || item->Type == ed::ObjectType::Image3D) {
glBindTexture(GL_TEXTURE_3D, item->Texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, item->Texture_WrapR);

glBindTexture(GL_TEXTURE_3D, 0);
} else if (item->Type == ed::ObjectType::CubeMap) {
glBindTexture(GL_TEXTURE_CUBE_MAP, item->Texture);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, item->Texture_MinFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, item->Texture_MagFilter);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, item->Texture_WrapS);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, item->Texture_WrapT);

glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SHADERed/Objects/ObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace ed {
std::vector<ed::ShaderVariable::ValueType> ParseBufferFormat(const std::string& str);

void FlipTexture(const std::string& name);
void UpdateTextureParameters(const std::string& name);
void UpdateTextureParameters(ObjectManagerItem* item);

void Bind(ObjectManagerItem* item, PipelineItem* pass);
void Unbind(ObjectManagerItem* item, PipelineItem* pass);
Expand Down
135 changes: 78 additions & 57 deletions src/SHADERed/Objects/ProjectParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ namespace ed {
if (rtObj->ClearColor.g != 0) textureNode.append_attribute("g").set_value(rtObj->ClearColor.g);
if (rtObj->ClearColor.b != 0) textureNode.append_attribute("b").set_value(rtObj->ClearColor.b);
if (rtObj->ClearColor.a != 0) textureNode.append_attribute("a").set_value(rtObj->ClearColor.a);
textureNode.append_attribute("min_filter").set_value(ed::gl::String::TextureMinFilter(item->Texture_MinFilter));
textureNode.append_attribute("mag_filter").set_value(ed::gl::String::TextureMagFilter(item->Texture_MagFilter));
textureNode.append_attribute("wrap_s").set_value(ed::gl::String::TextureWrap(item->Texture_WrapS));
textureNode.append_attribute("wrap_t").set_value(ed::gl::String::TextureWrap(item->Texture_WrapT));
}

if (isCube) {
Expand All @@ -637,6 +641,8 @@ namespace ed {
textureNode.append_attribute("bottom").set_value(GetTexturePath(texmaps[3], oldProjectPath).c_str());
textureNode.append_attribute("right").set_value(GetTexturePath(texmaps[4], oldProjectPath).c_str());
textureNode.append_attribute("back").set_value(GetTexturePath(texmaps[5], oldProjectPath).c_str());
textureNode.append_attribute("min_filter").set_value(ed::gl::String::TextureMinFilter(item->Texture_MinFilter));
textureNode.append_attribute("mag_filter").set_value(ed::gl::String::TextureMagFilter(item->Texture_MagFilter));
}

if ((isTexture && !isKeyboardTexture) || isTexture3D) {
Expand Down Expand Up @@ -664,6 +670,10 @@ namespace ed {
textureNode.append_attribute("width").set_value(iobj->Size.x);
textureNode.append_attribute("height").set_value(iobj->Size.y);
textureNode.append_attribute("format").set_value(gl::String::Format(iobj->Format));
textureNode.append_attribute("min_filter").set_value(ed::gl::String::TextureMinFilter(item->Texture_MinFilter));
textureNode.append_attribute("mag_filter").set_value(ed::gl::String::TextureMagFilter(item->Texture_MagFilter));
textureNode.append_attribute("wrap_s").set_value(ed::gl::String::TextureWrap(item->Texture_WrapS));
textureNode.append_attribute("wrap_t").set_value(ed::gl::String::TextureWrap(item->Texture_WrapT));
}
if (isImage3D) {
Image3DObject* iobj = item->Image3D;
Expand All @@ -672,6 +682,11 @@ namespace ed {
textureNode.append_attribute("height").set_value(iobj->Size.y);
textureNode.append_attribute("depth").set_value(iobj->Size.z);
textureNode.append_attribute("format").set_value(gl::String::Format(iobj->Format));
textureNode.append_attribute("min_filter").set_value(ed::gl::String::TextureMinFilter(item->Texture_MinFilter));
textureNode.append_attribute("mag_filter").set_value(ed::gl::String::TextureMagFilter(item->Texture_MagFilter));
textureNode.append_attribute("wrap_s").set_value(ed::gl::String::TextureWrap(item->Texture_WrapS));
textureNode.append_attribute("wrap_t").set_value(ed::gl::String::TextureWrap(item->Texture_WrapT));
textureNode.append_attribute("wrap_r").set_value(ed::gl::String::TextureWrap(item->Texture_WrapR));
}

PluginObject* pluginObj = item->Plugin;
Expand Down Expand Up @@ -1053,6 +1068,61 @@ namespace ed {
m_projectPath = std::filesystem::current_path().string();
}

void ProjectParser::m_parseTextureParameters(pugi::xml_node& objectNode, ObjectManagerItem* itemData)
{
// min filter
if (!objectNode.attribute("min_filter").empty()) {
auto filterName = objectNode.attribute("min_filter").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_MIN_FILTER_VALUES); i++)
if (strcmp(filterName, TEXTURE_MIN_FILTER_NAMES[i]) == 0) {
itemData->Texture_MinFilter = TEXTURE_MIN_FILTER_VALUES[i];
break;
}
}

// mag filter
if (!objectNode.attribute("mag_filter").empty()) {
auto filterName = objectNode.attribute("mag_filter").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_MAG_FILTER_VALUES); i++)
if (strcmp(filterName, TEXTURE_MAG_FILTER_NAMES[i]) == 0) {
itemData->Texture_MagFilter = TEXTURE_MAG_FILTER_VALUES[i];
break;
}
}

// wrap x
if (!objectNode.attribute("wrap_s").empty()) {
auto filterName = objectNode.attribute("wrap_s").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_WRAP_VALUES); i++)
if (strcmp(filterName, TEXTURE_WRAP_NAMES[i]) == 0) {
itemData->Texture_WrapS = TEXTURE_WRAP_VALUES[i];
break;
}
}

// wrap y
if (!objectNode.attribute("wrap_t").empty()) {
auto filterName = objectNode.attribute("wrap_t").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_WRAP_VALUES); i++)
if (strcmp(filterName, TEXTURE_WRAP_NAMES[i]) == 0) {
itemData->Texture_WrapT = TEXTURE_WRAP_VALUES[i];
break;
}
}

// wrap z
if (!objectNode.attribute("wrap_r").empty()) {
auto filterName = objectNode.attribute("wrap_r").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_WRAP_VALUES); i++)
if (strcmp(filterName, TEXTURE_WRAP_NAMES[i]) == 0) {
itemData->Texture_WrapR = TEXTURE_WRAP_VALUES[i];
break;
}
}

m_objects->UpdateTextureParameters(itemData);
}

void ProjectParser::m_parseVariableValue(pugi::xml_node& node, ShaderVariable* var)
{
int rowID = 0;
Expand Down Expand Up @@ -2835,70 +2905,17 @@ namespace ed {
}

// texture properties
if (!isCube) {
ObjectManagerItem* itemData = m_objects->Get(name);
if (itemData != nullptr) {
ObjectManagerItem* itemData = m_objects->Get(name);
if (itemData != nullptr) {
if (!isCube) {
// vflip
bool vflip = false;
if (!objectNode.attribute("vflip").empty())
vflip = objectNode.attribute("vflip").as_bool();
if (vflip)
m_objects->FlipTexture(name);

// min filter
if (!objectNode.attribute("min_filter").empty()) {
auto filterName = objectNode.attribute("min_filter").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_MIN_FILTER_VALUES); i++)
if (strcmp(filterName, TEXTURE_MIN_FILTER_NAMES[i]) == 0) {
itemData->Texture_MinFilter = TEXTURE_MIN_FILTER_VALUES[i];
break;
}
}

// mag filter
if (!objectNode.attribute("mag_filter").empty()) {
auto filterName = objectNode.attribute("mag_filter").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_MAG_FILTER_VALUES); i++)
if (strcmp(filterName, TEXTURE_MAG_FILTER_NAMES[i]) == 0) {
itemData->Texture_MagFilter = TEXTURE_MAG_FILTER_VALUES[i];
break;
}
}

// wrap x
if (!objectNode.attribute("wrap_s").empty()) {
auto filterName = objectNode.attribute("wrap_s").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_WRAP_VALUES); i++)
if (strcmp(filterName, TEXTURE_WRAP_NAMES[i]) == 0) {
itemData->Texture_WrapS = TEXTURE_WRAP_VALUES[i];
break;
}
}

// wrap y
if (!objectNode.attribute("wrap_t").empty()) {
auto filterName = objectNode.attribute("wrap_t").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_WRAP_VALUES); i++)
if (strcmp(filterName, TEXTURE_WRAP_NAMES[i]) == 0) {
itemData->Texture_WrapT = TEXTURE_WRAP_VALUES[i];
break;
}
}

if (is3D) {
// wrap z
if (!objectNode.attribute("wrap_r").empty()) {
auto filterName = objectNode.attribute("wrap_r").as_string();
for (int i = 0; i < HARRAYSIZE(TEXTURE_WRAP_VALUES); i++)
if (strcmp(filterName, TEXTURE_WRAP_NAMES[i]) == 0) {
itemData->Texture_WrapT = TEXTURE_WRAP_VALUES[i];
break;
}
}
}

m_objects->UpdateTextureParameters(name);
}
m_parseTextureParameters(objectNode, itemData);
}
} else if (strcmp(objType, "rendertexture") == 0) {
const pugi::char_t* objName = objectNode.attribute("name").as_string();
Expand Down Expand Up @@ -2977,6 +2994,8 @@ namespace ed {
}
}
}

m_parseTextureParameters(objectNode, rtData);
} else if (strcmp(objType, "image") == 0) {
const pugi::char_t* objName = objectNode.attribute("name").as_string();

Expand Down Expand Up @@ -3035,6 +3054,7 @@ namespace ed {
}
}
}
m_parseTextureParameters(objectNode, iobjOwner);
} else if (strcmp(objType, "image3d") == 0) {
const pugi::char_t* objName = objectNode.attribute("name").as_string();

Expand Down Expand Up @@ -3091,6 +3111,7 @@ namespace ed {
}
}
}
m_parseTextureParameters(objectNode, iobjOwner);
} else if (strcmp(objType, "audio") == 0) {
pugi::char_t objPath[SHADERED_MAX_PATH];
strcpy(objPath, toGenericPath(objectNode.attribute("path").as_string()).c_str());
Expand Down
2 changes: 2 additions & 0 deletions src/SHADERed/Objects/ProjectParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace ed {
class PipelineManager;
class RenderEngine;
class ObjectManager;
class ObjectManagerItem;
class PluginManager;
class InputLayoutItem;
class DebugInformation;
Expand Down Expand Up @@ -70,6 +71,7 @@ namespace ed {
void m_parseV1(pugi::xml_node& projectNode); // old
void m_parseV2(pugi::xml_node& projectNode); // current -> merge blend, rasterizer and depth states into one "render state" ||| remove input layout parsing ||| ignore shader entry property

void m_parseTextureParameters(pugi::xml_node& node, ObjectManagerItem* itemData);
void m_parseVariableValue(pugi::xml_node& node, ShaderVariable* var);
void m_exportVariableValue(pugi::xml_node& node, ShaderVariable* vars);
void m_exportShaderVariables(pugi::xml_node& node, std::vector<ShaderVariable*>& vars);
Expand Down
Loading