2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
Ideas taken from:
|
|
|
|
- GamePlay3D: http://gameplay3d.org/
|
|
|
|
- OGRE3D: http://www.ogre3d.org/
|
|
|
|
- Qt3D: http://qt-project.org/
|
|
|
|
****************************************************************************/
|
|
|
|
#include "renderer/CCMaterial.h"
|
|
|
|
#include "renderer/CCTechnique.h"
|
|
|
|
#include "renderer/CCPass.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
#include "base/CCProperties.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/CCConsole.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
|
2021-12-25 10:04:45 +08:00
|
|
|
# define strcasecmp _stricmp
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string replaceDefines(std::string_view compileTimeDefines)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto defineParts = Console::Utility::split(compileTimeDefines, ';');
|
|
|
|
std::stringstream ss;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& p : defineParts)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (p.find("#define ") == std::string::npos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ss << "#define " << p << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ss << p << std::endl;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return ss.str();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Helpers declaration
|
|
|
|
static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue);
|
|
|
|
static bool isValidUniform(const char* name);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Material* Material::createWithFilename(std::string_view filepath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto validfilename = FileUtils::getInstance()->fullPathForFilename(filepath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!validfilename.empty())
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto mat = new Material();
|
|
|
|
if (mat->initWithFile(validfilename))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
delete mat;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material* Material::createWithProperties(Properties* materialProperties)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto mat = new Material();
|
|
|
|
if (mat->initWithProperties(materialProperties))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
delete mat;
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Material* Material::createWithProgramState(backend::ProgramState* programState)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(programState, "Invalid Program State");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto mat = new Material();
|
|
|
|
if (mat->initWithProgramState(programState))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
mat->autorelease();
|
|
|
|
return mat;
|
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
delete mat;
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Material::initWithProgramState(backend::ProgramState* state)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto technique = Technique::createWithProgramState(this, state);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (technique)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_techniques.pushBack(technique);
|
|
|
|
|
|
|
|
// weak pointer
|
|
|
|
_currentTechnique = technique;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool Material::initWithFile(std::string_view validfilename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Warning: properties is not a "Ref" object, must be manually deleted
|
|
|
|
Properties* properties = Properties::createNonRefCounted(validfilename);
|
|
|
|
|
|
|
|
// get the first material
|
|
|
|
parseProperties((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(properties);
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::initWithProperties(Properties* materialProperties)
|
|
|
|
{
|
|
|
|
return parseProperties(materialProperties);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Material::draw(MeshCommand* meshCommands,
|
|
|
|
float globalZOrder,
|
|
|
|
backend::Buffer* vertexBuffer,
|
|
|
|
backend::Buffer* indexBuffer,
|
|
|
|
CustomCommand::PrimitiveType primitive,
|
|
|
|
CustomCommand::IndexFormat indexFormat,
|
|
|
|
unsigned int indexCount,
|
|
|
|
const Mat4& modelView)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& pass : _currentTechnique->_passes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
pass->draw(&meshCommands[i], globalZOrder, vertexBuffer, indexBuffer, primitive, indexFormat, indexCount,
|
|
|
|
modelView);
|
2019-11-23 20:27:39 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void Material::setTarget(ax::Node* target)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::parseProperties(Properties* materialProperties)
|
|
|
|
{
|
|
|
|
setName(materialProperties->getId());
|
|
|
|
auto space = materialProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
|
|
|
if (strcmp(name, "technique") == 0)
|
|
|
|
{
|
|
|
|
parseTechnique(space);
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "renderState") == 0)
|
|
|
|
{
|
|
|
|
parseRenderState(&_renderState.getStateBlock(), space);
|
|
|
|
}
|
|
|
|
|
|
|
|
space = materialProperties->getNextNamespace();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::parseTechnique(Properties* techniqueProperties)
|
|
|
|
{
|
|
|
|
auto technique = Technique::create(this);
|
|
|
|
_techniques.pushBack(technique);
|
|
|
|
|
|
|
|
// first one is the default one
|
|
|
|
if (!_currentTechnique)
|
|
|
|
_currentTechnique = technique;
|
|
|
|
|
|
|
|
// name
|
|
|
|
technique->setName(techniqueProperties->getId());
|
|
|
|
|
|
|
|
// passes
|
|
|
|
auto space = techniqueProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
|
|
|
if (strcmp(name, "pass") == 0)
|
|
|
|
{
|
|
|
|
parsePass(technique, space);
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "renderState") == 0)
|
|
|
|
{
|
|
|
|
parseRenderState(&technique->getStateBlock(), space);
|
|
|
|
}
|
|
|
|
|
|
|
|
space = techniqueProperties->getNextNamespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::parsePass(Technique* technique, Properties* passProperties)
|
|
|
|
{
|
|
|
|
auto pass = Pass::create(technique);
|
|
|
|
technique->addPass(pass);
|
|
|
|
|
|
|
|
pass->setName(passProperties->getId());
|
|
|
|
|
|
|
|
// Pass can have 3 different namespaces:
|
|
|
|
// - one or more "sampler"
|
|
|
|
// - one "renderState"
|
|
|
|
// - one "shader"
|
|
|
|
|
|
|
|
auto space = passProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
|
|
|
if (strcmp(name, "shader") == 0)
|
|
|
|
{
|
|
|
|
parseShader(pass, space);
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "renderState") == 0)
|
|
|
|
{
|
|
|
|
parseRenderState(&pass->_renderState.getStateBlock(), space);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid namespace");
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
space = passProperties->getNextNamespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cocos2d-x doesn't support Samplers yet. But will be added soon
|
|
|
|
bool Material::parseSampler(backend::ProgramState* programState, Properties* samplerProperties)
|
|
|
|
{
|
2022-10-13 21:44:42 +08:00
|
|
|
AXASSERT(!samplerProperties->getId().empty(), "Sampler must have an id. The id is the uniform name");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// required
|
|
|
|
auto filename = samplerProperties->getString("path");
|
|
|
|
|
|
|
|
auto texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!texture)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Invalid filepath");
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// optionals
|
|
|
|
{
|
|
|
|
Texture2D::TexParams texParams;
|
|
|
|
|
|
|
|
// mipmap
|
2021-12-25 10:04:45 +08:00
|
|
|
bool usemipmap = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* mipmap = getOptionalString(samplerProperties, "mipmap", "false");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (mipmap && strcasecmp(mipmap, "true") == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
texture->generateMipmap();
|
|
|
|
usemipmap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid options: REPEAT, CLAMP
|
|
|
|
const char* wrapS = getOptionalString(samplerProperties, "wrapS", "CLAMP_TO_EDGE");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (strcasecmp(wrapS, "REPEAT") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.sAddressMode = backend::SamplerAddressMode::REPEAT;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(wrapS, "CLAMP_TO_EDGE") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
else
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Invalid wrapS: %s", wrapS);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// valid options: REPEAT, CLAMP
|
|
|
|
const char* wrapT = getOptionalString(samplerProperties, "wrapT", "CLAMP_TO_EDGE");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (strcasecmp(wrapT, "REPEAT") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.tAddressMode = backend::SamplerAddressMode::REPEAT;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(wrapT, "CLAMP_TO_EDGE") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
|
|
|
|
else
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Invalid wrapT: %s", wrapT);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// valid options: NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, LINEAR_MIPMAP_NEAREST, NEAREST_MIPMAP_LINEAR,
|
|
|
|
// LINEAR_MIPMAP_LINEAR
|
|
|
|
const char* minFilter =
|
|
|
|
getOptionalString(samplerProperties, "minFilter", usemipmap ? "LINEAR_MIPMAP_NEAREST" : "LINEAR");
|
|
|
|
if (strcasecmp(minFilter, "NEAREST") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::NEAREST;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(minFilter, "LINEAR") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(minFilter, "NEAREST_MIPMAP_NEAREST") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::NEAREST;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(minFilter, "LINEAR_MIPMAP_NEAREST") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(minFilter, "NEAREST_MIPMAP_LINEAR") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(minFilter, "LINEAR_MIPMAP_LINEAR") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.minFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
else
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Invalid minFilter: %s", minFilter);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// valid options: NEAREST, LINEAR
|
|
|
|
const char* magFilter = getOptionalString(samplerProperties, "magFilter", "LINEAR");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (strcasecmp(magFilter, "NEAREST") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.magFilter = backend::SamplerFilter::NEAREST;
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (strcasecmp(magFilter, "LINEAR") == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
texParams.magFilter = backend::SamplerFilter::LINEAR;
|
|
|
|
else
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Invalid magFilter: %s", magFilter);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
texture->setTexParameters(texParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto textureName = samplerProperties->getId();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto location = programState->getUniformLocation(textureName);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!location)
|
|
|
|
{
|
2022-10-27 13:01:18 +08:00
|
|
|
AXLOG("warning: failed to find texture uniform location %s when parsing material", textureName.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_textureSlots.find(textureName) == _textureSlots.end())
|
|
|
|
{
|
|
|
|
_textureSlots[textureName] = _textureSlotIndex;
|
|
|
|
programState->setTexture(location, _textureSlotIndex++, texture->getBackendTexture());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
programState->setTexture(location, _textureSlots[textureName], texture->getBackendTexture());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::parseShader(Pass* pass, Properties* shaderProperties)
|
|
|
|
{
|
|
|
|
// vertexShader
|
|
|
|
const char* vertShader = getOptionalString(shaderProperties, "vertexShader", nullptr);
|
|
|
|
|
|
|
|
// fragmentShader
|
|
|
|
const char* fragShader = getOptionalString(shaderProperties, "fragmentShader", nullptr);
|
|
|
|
|
|
|
|
// compileTimeDefines
|
|
|
|
const char* compileTimeDefines = getOptionalString(shaderProperties, "defines", "");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* fu = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (vertShader && fragShader)
|
|
|
|
{
|
|
|
|
|
|
|
|
auto vertShaderSrc = fu->getStringFromFile(vertShader);
|
|
|
|
auto fragShaderSrc = fu->getStringFromFile(fragShader);
|
|
|
|
|
|
|
|
auto defs = replaceDefines(compileTimeDefines);
|
|
|
|
|
|
|
|
vertShaderSrc = defs + "\n" + vertShaderSrc;
|
|
|
|
fragShaderSrc = defs + "\n" + fragShaderSrc;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* program = backend::Device::getInstance()->newProgram(vertShaderSrc, fragShaderSrc);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto programState = new backend::ProgramState(program);
|
|
|
|
pass->setProgramState(programState);
|
|
|
|
|
|
|
|
// Parse uniforms only if the GLProgramState was created
|
|
|
|
auto property = shaderProperties->getNextProperty();
|
|
|
|
while (property)
|
|
|
|
{
|
|
|
|
if (isValidUniform(property))
|
|
|
|
{
|
|
|
|
parseUniform(programState, shaderProperties, property);
|
|
|
|
}
|
|
|
|
|
|
|
|
property = shaderProperties->getNextProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto space = shaderProperties->getNextNamespace();
|
|
|
|
while (space)
|
|
|
|
{
|
|
|
|
const char* name = space->getNamespace();
|
|
|
|
if (strcmp(name, "sampler") == 0)
|
|
|
|
{
|
|
|
|
parseSampler(programState, space);
|
|
|
|
}
|
|
|
|
space = shaderProperties->getNextNamespace();
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(program);
|
|
|
|
AX_SAFE_RELEASE(programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Material::parseUniform(backend::ProgramState* programState, Properties* properties, const char* uniformName)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
auto type = properties->getType(uniformName);
|
|
|
|
|
|
|
|
backend::UniformLocation location;
|
|
|
|
location = programState->getUniformLocation(uniformName);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Properties::Type::NUMBER:
|
|
|
|
{
|
|
|
|
auto f = properties->getFloat(uniformName);
|
|
|
|
programState->setUniform(location, &f, sizeof(f));
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case Properties::Type::VECTOR2:
|
|
|
|
{
|
|
|
|
Vec2 v2;
|
|
|
|
properties->getVec2(uniformName, &v2);
|
|
|
|
programState->setUniform(location, &v2, sizeof(v2));
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case Properties::Type::VECTOR3:
|
|
|
|
{
|
|
|
|
Vec3 v3;
|
|
|
|
properties->getVec3(uniformName, &v3);
|
|
|
|
programState->setUniform(location, &v3, sizeof(v3));
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case Properties::Type::VECTOR4:
|
|
|
|
{
|
|
|
|
Vec4 v4;
|
|
|
|
properties->getVec4(uniformName, &v4);
|
|
|
|
programState->setUniform(location, &v4, sizeof(v4));
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case Properties::Type::MATRIX:
|
|
|
|
{
|
|
|
|
Mat4 m4;
|
|
|
|
properties->getMat4(uniformName, &m4);
|
|
|
|
programState->setUniform(location, &m4.m, sizeof(m4.m));
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case Properties::Type::STRING:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Assume this is a parameter auto-binding.
|
|
|
|
programState->setParameterAutoBinding(uniformName, properties->getString());
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Material::parseRenderState(RenderState::StateBlock* state, Properties* properties)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (nullptr == state)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto property = properties->getNextProperty();
|
|
|
|
while (property)
|
|
|
|
{
|
|
|
|
// Parse uniforms only if the GLProgramState was created
|
|
|
|
// Render state only can have "strings" or numbers as values. No new namespaces
|
|
|
|
state->setState(property, properties->getString(property));
|
|
|
|
|
|
|
|
property = properties->getNextProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Material::setName(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Material::getName() const
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2022-08-06 16:17:55 +08:00
|
|
|
void Material::setTransparent(bool value)
|
|
|
|
{
|
|
|
|
_isTransparent = value;
|
|
|
|
getStateBlock().setBlend(_force2DQueue || _isTransparent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material::setForce2DQueue(bool value)
|
|
|
|
{
|
|
|
|
_force2DQueue = value;
|
|
|
|
getStateBlock().setBlend(_force2DQueue || _isTransparent);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Material::Material() : _name(""), _currentTechnique(nullptr), _target(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Material::~Material() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Material* Material::clone() const
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto material = new Material();
|
2021-12-25 10:04:45 +08:00
|
|
|
// RenderState::cloneInto(material);
|
2021-12-08 00:11:53 +08:00
|
|
|
material->_renderState = _renderState;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& technique : _techniques)
|
2021-12-08 00:11:53 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto t = technique->clone();
|
2021-12-08 00:11:53 +08:00
|
|
|
t->_material = material;
|
|
|
|
material->_techniques.pushBack(t);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
|
|
|
|
// current technique
|
2021-12-25 10:04:45 +08:00
|
|
|
auto name = _currentTechnique->getName();
|
2021-12-08 00:11:53 +08:00
|
|
|
material->_currentTechnique = material->getTechniqueByName(name);
|
2021-12-25 10:04:45 +08:00
|
|
|
material->_textureSlots = material->_textureSlots;
|
2021-12-08 00:11:53 +08:00
|
|
|
material->_textureSlotIndex = material->_textureSlotIndex;
|
|
|
|
material->autorelease();
|
2019-11-23 20:27:39 +08:00
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
|
|
|
Technique* Material::getTechnique() const
|
|
|
|
{
|
|
|
|
return _currentTechnique;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<Technique*>& Material::getTechniques() const
|
|
|
|
{
|
|
|
|
return _techniques;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Technique* Material::getTechniqueByName(std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& technique : _techniques)
|
|
|
|
{
|
|
|
|
if (technique->getName().compare(name) == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
return technique;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Technique* Material::getTechniqueByIndex(ssize_t index)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_ASSERT(index >= 0 && index < _techniques.size() && "Invalid size");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return _techniques.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Material::addTechnique(Technique* technique)
|
|
|
|
{
|
|
|
|
_techniques.pushBack(technique);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Material::setTechnique(std::string_view techniqueName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto technique = getTechniqueByName(techniqueName);
|
|
|
|
if (technique)
|
|
|
|
_currentTechnique = technique;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Material::getTechniqueCount() const
|
|
|
|
{
|
|
|
|
return _techniques.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers implementation
|
|
|
|
static bool isValidUniform(const char* name)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return !(strcmp(name, "defines") == 0 || strcmp(name, "vertexShader") == 0 || strcmp(name, "fragmentShader") == 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* getOptionalString(Properties* properties, const char* key, const char* defaultValue)
|
|
|
|
{
|
|
|
|
|
|
|
|
const char* ret = properties->getString(key);
|
|
|
|
if (!ret)
|
|
|
|
ret = defaultValue;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|