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

6 ➡️ 7 (main) #507

Merged
merged 5 commits into from
Dec 8, 2021
Merged
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
78 changes: 73 additions & 5 deletions ogre2/src/Ogre2Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <ignition/common/Console.hh>
#include <ignition/common/Filesystem.hh>
#include <ignition/common/Image.hh>

#include "ignition/rendering/ShaderParams.hh"
#include "ignition/rendering/ShaderType.hh"
Expand Down Expand Up @@ -541,7 +542,8 @@ void Ogre2Material::ClearLightMap()
this->lightMapUvSet = 0u;

// in ogre 2.2, we swtiched to use the emissive map slot for light map
this->ogreDatablock->setTexture(Ogre::PBSM_EMISSIVE, this->lightMapName);
if (this->ogreDatablock->getUseEmissiveAsLightmap())
this->ogreDatablock->setTexture(Ogre::PBSM_EMISSIVE, this->lightMapName);
this->ogreDatablock->setUseEmissiveAsLightmap(false);
}

Expand Down Expand Up @@ -635,16 +637,82 @@ void Ogre2Material::SetTextureMapImpl(const std::string &_texture,
}
}

Ogre::Root *root = Ogre2RenderEngine::Instance()->OgreRoot();
Ogre::TextureGpuManager *textureMgr =
root->getRenderSystem()->getTextureGpuManager();

// workaround for grayscale emissive texture
// convert to RGB otherwise the emissive map is rendered red
if (_type == Ogre::PBSM_EMISSIVE &&
!this->ogreDatablock->getUseEmissiveAsLightmap())
{
common::Image img(_texture);
// check for 8 bit pixel
if (img.BPP() == 8u)
{
std::string parentPath = common::parentPath(_texture);
// set a custom name for the rgb texture by appending ign_ prefix
std::string rgbTexName = "ign_" + baseName;
baseName = rgbTexName;
std::string filename = common::joinPaths(parentPath, rgbTexName);
auto tex = textureMgr->findTextureNoThrow(rgbTexName);
if (!tex)
{
ignmsg << "Grayscale emissive texture detected. Converting to RGB: "
<< rgbTexName << std::endl;
// need to be 4 channels for gpu texture
unsigned int channels = 4u;
unsigned int size = img.Width() * img.Height() * channels;
unsigned char *data = new unsigned char[size];
for (unsigned int i = 0; i < img.Height(); ++i)
{
for (unsigned int j = 0; j < img.Width(); ++j)
{
// flip Y
math::Color c = img.Pixel(j, img.Height() - i - 1u);
unsigned int idx = i * img.Width() * channels + j * channels;
data[idx] = static_cast<uint8_t>(c.R() * 255u);
data[idx + 1u] = static_cast<uint8_t>(c.R() * 255u);
data[idx + 2u] = static_cast<uint8_t>(c.R() * 255u);
data[idx + 3u] = 255u;
}
}

// create the gpu texture
Ogre::uint32 textureFlags = 0;
textureFlags |= Ogre::TextureFlags::AutomaticBatching;
if (this->ogreDatablock->suggestUsingSRGB(_type))
textureFlags |= Ogre::TextureFlags::PrefersLoadingFromFileAsSRGB;
Ogre::TextureGpu *texture = textureMgr->createOrRetrieveTexture(
rgbTexName,
Ogre::GpuPageOutStrategy::Discard,
textureFlags | Ogre::TextureFlags::ManualTexture,
Ogre::TextureTypes::Type2D,
Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME,
0u);

texture->setPixelFormat(Ogre::PFG_RGBA8_UNORM_SRGB);
texture->setTextureType(Ogre::TextureTypes::Type2D);
texture->setNumMipmaps(1u);
texture->setResolution(img.Width(), img.Height());
texture->scheduleTransitionTo(Ogre::GpuResidency::Resident);
texture->waitForData();

// upload raw color image data to gpu texture
Ogre::Image2 image;
image.loadDynamicImage(data, false, texture);
image.uploadTo(texture, 0, 0);
delete [] data;
}
}
}

Ogre::HlmsSamplerblock samplerBlockRef;
samplerBlockRef.mU = Ogre::TAM_WRAP;
samplerBlockRef.mV = Ogre::TAM_WRAP;
samplerBlockRef.mW = Ogre::TAM_WRAP;

this->ogreDatablock->setTexture(_type, baseName, &samplerBlockRef);

Ogre::Root *root = Ogre2RenderEngine::Instance()->OgreRoot();
Ogre::TextureGpuManager *textureMgr =
root->getRenderSystem()->getTextureGpuManager();
auto tex = textureMgr->findTextureNoThrow(baseName);

if (tex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ fragment float4 main_metal
// values close to 0 get rounded to 0)
//
// See https://github.com/ignitionrobotics/ign-rendering/issues/332
// float4 p = texelFetch(inputTexture, int2(inPs.uv0 * params.texResolution.xy), 0);
// TODO - establish Metal equivalent - use standard sampler as interim approx.
float4 p = inputTexture.sample(inputSampler, inPs.uv0);
// Either: Metal equivalent of texelFetch
float4 p = inputTexture.read(uint2(inPs.uv0 * params.texResolution.xy), 0);
// Or: Use standard sampler
// float4 p = inputTexture.sample(inputSampler, inPs.uv0);

float3 point = p.xyz;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct Params
float2 projectionParams;
float far;
float inf;
float4 colorTexResolution;
};

float packFloat(float4 color)
Expand Down Expand Up @@ -66,7 +67,11 @@ fragment float4 main_metal
point = float3(p.inf);

// color
float4 color = colorTexture.sample(colorSampler, inPs.uv0);
// Either: Metal equivalent of texelFetch
float4 color = colorTexture.read(
uint2(inPs.uv0 * p.colorTexResolution.xy), 0);
// Or: Use standard sampler
// float4 color = colorTexture.sample(colorSampler, inPs.uv0);

float rgba = packFloat(color);

Expand Down