mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Optimize the code by PR comments.
This commit is contained in:
@@ -1094,13 +1094,11 @@ void DeviceGraphics::commitTextures()
|
||||
//
|
||||
DeviceGraphics::Uniform::Uniform()
|
||||
: dirty(true)
|
||||
, value(nullptr)
|
||||
, elementType(UniformElementType::FLOAT)
|
||||
{}
|
||||
|
||||
DeviceGraphics::Uniform::Uniform(const void* v, size_t bytes, UniformElementType elementType_)
|
||||
: dirty(true)
|
||||
, value(nullptr)
|
||||
, elementType(elementType_)
|
||||
{
|
||||
setValue(v, bytes);
|
||||
|
||||
@@ -79,7 +79,7 @@ public:
|
||||
|
||||
void setValue(const void* v, size_t bytes);
|
||||
|
||||
void* value;
|
||||
void* value = NULL;
|
||||
bool dirty;
|
||||
UniformElementType elementType;
|
||||
private:
|
||||
|
||||
@@ -39,12 +39,22 @@ public:
|
||||
{
|
||||
_count = 0;
|
||||
_data.resize(size);
|
||||
};
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
_data[i] = new T();
|
||||
}
|
||||
}
|
||||
|
||||
~RecyclePool()
|
||||
{
|
||||
for (int i = 0, len = (int)_data.size(); i < len; i++)
|
||||
{
|
||||
delete _data[i];
|
||||
}
|
||||
|
||||
_data.clear();
|
||||
};
|
||||
}
|
||||
|
||||
const T* getData(const int index) const
|
||||
{
|
||||
@@ -54,7 +64,7 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &_data[index];
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
const int getLength() const
|
||||
@@ -65,26 +75,34 @@ public:
|
||||
void reset()
|
||||
{
|
||||
_count = 0;
|
||||
};
|
||||
}
|
||||
|
||||
T* add()
|
||||
{
|
||||
int size = (int)_data.size();
|
||||
if (_count >= size) {
|
||||
_data.resize(size * 2);
|
||||
if (_count >= size)
|
||||
{
|
||||
resize(size * 2);
|
||||
}
|
||||
|
||||
return &_data[_count++];
|
||||
};
|
||||
return _data[_count++];
|
||||
}
|
||||
|
||||
void resize(int size)
|
||||
{
|
||||
_data.resize(size);
|
||||
};
|
||||
if (size > _data.size())
|
||||
{
|
||||
for (int i = (int)_data.size(); i < size; i++)
|
||||
{
|
||||
_data[i] = new T();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void remove(int index)
|
||||
{
|
||||
if (index >= _count) {
|
||||
if (index >= _count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,10 +111,10 @@ public:
|
||||
_data[index] = _data[last];
|
||||
_data[last] = tmp;
|
||||
_count -= 1;
|
||||
};
|
||||
}
|
||||
private:
|
||||
int _count = 0;
|
||||
std::vector<T> _data;
|
||||
std::vector<T*> _data;
|
||||
|
||||
|
||||
};
|
||||
|
||||
@@ -170,11 +170,11 @@ void BaseRenderer::render(const View& view, const Scene* scene)
|
||||
}
|
||||
}
|
||||
|
||||
void BaseRenderer::setProperty (Property& prop)
|
||||
void BaseRenderer::setProperty (Effect::Property& prop)
|
||||
{
|
||||
Technique::Parameter::Type propType = prop.getType();
|
||||
auto& propName = prop.getName();
|
||||
if (Property::Type::UNKNOWN == propType)
|
||||
if (Effect::Property::Type::UNKNOWN == propType)
|
||||
{
|
||||
RENDERER_LOGW("Failed to set technique property, type unknown");
|
||||
return;
|
||||
@@ -182,9 +182,9 @@ void BaseRenderer::setProperty (Property& prop)
|
||||
|
||||
if (nullptr == prop.getValue())
|
||||
{
|
||||
prop = Property(propName, propType);
|
||||
prop = Effect::Property(propName, propType);
|
||||
|
||||
if (Property::Type::TEXTURE_2D == propType)
|
||||
if (Effect::Property::Type::TEXTURE_2D == propType)
|
||||
{
|
||||
prop.setTexture(_defaultTexture);
|
||||
}
|
||||
@@ -196,8 +196,8 @@ void BaseRenderer::setProperty (Property& prop)
|
||||
return;
|
||||
}
|
||||
|
||||
if (Property::Type::TEXTURE_2D == propType ||
|
||||
Property::Type::TEXTURE_CUBE == propType)
|
||||
if (Effect::Property::Type::TEXTURE_2D == propType ||
|
||||
Effect::Property::Type::TEXTURE_CUBE == propType)
|
||||
{
|
||||
if (1 == prop.getCount())
|
||||
{
|
||||
@@ -215,7 +215,7 @@ void BaseRenderer::setProperty (Property& prop)
|
||||
}
|
||||
|
||||
_device->setTextureArray(propName,
|
||||
std::move(prop.getTextureArray()),
|
||||
prop.getTextureArray(),
|
||||
slots);
|
||||
}
|
||||
}
|
||||
@@ -241,9 +241,9 @@ void BaseRenderer::setProperty (Property& prop)
|
||||
}
|
||||
|
||||
uint16_t bytes = prop.getBytes();
|
||||
if (Property::Type::INT == propType ||
|
||||
Property::Type::INT2 == propType ||
|
||||
Property::Type::INT4 == propType)
|
||||
if (Effect::Property::Type::INT == propType ||
|
||||
Effect::Property::Type::INT2 == propType ||
|
||||
Effect::Property::Type::INT4 == propType)
|
||||
{
|
||||
_device->setUniformiv(propName, bytes / sizeof(int), (const int*)prop.getValue());
|
||||
}
|
||||
@@ -269,7 +269,7 @@ void BaseRenderer::draw(const StageItem& item)
|
||||
auto& properties = effect->getProperties();
|
||||
for (auto it = properties.begin(); it != properties.end(); it++)
|
||||
{
|
||||
Property& prop = const_cast<Property&>(it->second);
|
||||
Effect::Property& prop = const_cast<Effect::Property&>(it->second);
|
||||
setProperty(prop);
|
||||
}
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ public:
|
||||
protected:
|
||||
void render(const View&, const Scene* scene);
|
||||
void draw(const StageItem& item);
|
||||
void setProperty (Property& prop);
|
||||
void setProperty (Effect::Property& prop);
|
||||
|
||||
struct StageInfo
|
||||
{
|
||||
@@ -125,8 +125,6 @@ protected:
|
||||
Program* _program = nullptr;
|
||||
Texture2D* _defaultTexture = nullptr;
|
||||
std::unordered_map<std::string, StageCallback> _stage2fn;
|
||||
//std::vector<DrawItem> _drawItems;
|
||||
//std::vector<StageInfo> _stageInfos;
|
||||
RecyclePool<DrawItem>* _drawItems;
|
||||
RecyclePool<StageInfo>* _stageInfos;
|
||||
RecyclePool<View>* _views;
|
||||
|
||||
@@ -47,7 +47,7 @@ void CustomProperties::setProperty(const std::string name, const Property& prope
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
const Property& CustomProperties::getProperty(std::string name) const
|
||||
const CustomProperties::Property& CustomProperties::getProperty(std::string name) const
|
||||
{
|
||||
static Property EMPTY_PROPERTY;
|
||||
if (_properties.end() == _properties.find(name))
|
||||
@@ -79,7 +79,7 @@ Value CustomProperties::getDefine(const std::string& name) const
|
||||
return Value::Null;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, Property> CustomProperties::extractProperties()
|
||||
std::unordered_map<std::string, CustomProperties::Property> CustomProperties::extractProperties()
|
||||
{
|
||||
return _properties;
|
||||
}
|
||||
|
||||
@@ -31,11 +31,11 @@
|
||||
|
||||
RENDERER_BEGIN
|
||||
|
||||
using Property = Technique::Parameter;
|
||||
|
||||
class CustomProperties
|
||||
{
|
||||
public:
|
||||
using Property = Technique::Parameter;
|
||||
|
||||
CustomProperties();
|
||||
~CustomProperties();
|
||||
|
||||
|
||||
@@ -153,13 +153,13 @@ void ForwardRenderer::submitLightsUniforms()
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
auto* light = _directionalLights.at(i);
|
||||
lightVec3.set(std::move(light->getDirectionUniform()));
|
||||
lightVec3.set(light->getDirectionUniform());
|
||||
*directions++ = lightVec3.x;
|
||||
*directions++ = lightVec3.y;
|
||||
*directions++ = lightVec3.z;
|
||||
*directions++ = 0;
|
||||
|
||||
colorVec3.set(std::move(light->getColorUniform()));
|
||||
colorVec3.set(light->getColorUniform());
|
||||
*colors++ = colorVec3.x;
|
||||
*colors++ = colorVec3.y;
|
||||
*colors++ = colorVec3.z;
|
||||
@@ -179,13 +179,13 @@ void ForwardRenderer::submitLightsUniforms()
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
auto* light = _pointLights.at(i);
|
||||
posVec3.set(std::move(light->getPositionUniform()));
|
||||
posVec3.set(light->getPositionUniform());
|
||||
*positionAndRanges++ = posVec3.x;
|
||||
*positionAndRanges++ = posVec3.y;
|
||||
*positionAndRanges++ = posVec3.z;
|
||||
*positionAndRanges++ = light->getRange();
|
||||
|
||||
colorVec3.set(std::move(light->getColorUniform()));
|
||||
colorVec3.set(light->getColorUniform());
|
||||
*colors++ = colorVec3.x;
|
||||
*colors++ = colorVec3.y;
|
||||
*colors++ = colorVec3.z;
|
||||
@@ -207,19 +207,19 @@ void ForwardRenderer::submitLightsUniforms()
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
auto* light = _spotLights.at(i);
|
||||
lightVec3.set(std::move(light->getDirectionUniform()));
|
||||
lightVec3.set(light->getDirectionUniform());
|
||||
*directions++ = lightVec3.x;
|
||||
*directions++ = lightVec3.y;
|
||||
*directions++ = lightVec3.z;
|
||||
*directions++ = 0;
|
||||
|
||||
posVec3.set(std::move(light->getPositionUniform()));
|
||||
posVec3.set(light->getPositionUniform());
|
||||
*positionAndRanges++ = posVec3.x;
|
||||
*positionAndRanges++ = posVec3.y;
|
||||
*positionAndRanges++ = posVec3.z;
|
||||
*positionAndRanges++ = light->getRange();
|
||||
|
||||
colorVec3.set(std::move(light->getColorUniform()));
|
||||
colorVec3.set(light->getColorUniform());
|
||||
*colors++ = colorVec3.x;
|
||||
*colors++ = colorVec3.y;
|
||||
*colors++ = colorVec3.z;
|
||||
@@ -237,7 +237,7 @@ void ForwardRenderer::submitLightsUniforms()
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
auto* light = _ambientLights.at(i);
|
||||
colorVec3.set(std::move(light->getColorUniform()));
|
||||
colorVec3.set(light->getColorUniform());
|
||||
*colors++ = colorVec3.x;
|
||||
*colors++ = colorVec3.y;
|
||||
*colors++ = colorVec3.z;
|
||||
@@ -335,7 +335,7 @@ void ForwardRenderer::drawItems(const std::vector<StageItem>& items)
|
||||
shadowMaps.push_back(light->getShadowMap());
|
||||
slots.push_back(allocTextureUnit());
|
||||
}
|
||||
_device->setTextureArray("cc_shadow_map", std::move(shadowMaps), slots);
|
||||
_device->setTextureArray("cc_shadow_map", shadowMaps, slots);
|
||||
updateShaderDefines(item);
|
||||
draw(item);
|
||||
}
|
||||
|
||||
@@ -84,12 +84,12 @@ void Model::setEffect(Effect* effect, CustomProperties* customProperties)
|
||||
_uniforms.clear();
|
||||
|
||||
if (effect != nullptr) {
|
||||
_defines.push_back(std::move(effect->extractDefines()));
|
||||
_defines.push_back(effect->extractDefines());
|
||||
_uniforms.push_back(effect->extractProperties());
|
||||
}
|
||||
|
||||
if (customProperties != nullptr) {
|
||||
_defines.push_back(std::move(customProperties->extractDefines()));
|
||||
_defines.push_back(customProperties->extractDefines());
|
||||
_uniforms.push_back(customProperties->extractProperties());
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ void Model::extractDrawItem(DrawItem& out) const
|
||||
{
|
||||
out.model = const_cast<Model*>(this);
|
||||
out.ia = nullptr;
|
||||
out.effect = std::move(_effect);
|
||||
out.effect = _effect;
|
||||
out.defines = out.effect->extractDefines();
|
||||
|
||||
return;
|
||||
@@ -108,7 +108,7 @@ void Model::extractDrawItem(DrawItem& out) const
|
||||
|
||||
out.model = const_cast<Model*>(this);
|
||||
out.ia = const_cast<InputAssembler*>(&(_inputAssembler));
|
||||
out.effect = std::move(_effect);
|
||||
out.effect = _effect;
|
||||
out.defines = out.effect->extractDefines();
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
/**
|
||||
* @brief Sets model matrix.
|
||||
*/
|
||||
inline void setWorldMatix(const Mat4& matrix) { _worldMatrix = std::move(matrix); }
|
||||
inline void setWorldMatix(const Mat4& matrix) { _worldMatrix = matrix; }
|
||||
/**
|
||||
* @brief Gets mode matrix.
|
||||
*/
|
||||
@@ -126,13 +126,13 @@ private:
|
||||
friend class ModelPool;
|
||||
void reset();
|
||||
|
||||
NodeProxy* _node;
|
||||
NodeProxy* _node = nullptr;
|
||||
Mat4 _worldMatrix;
|
||||
Effect* _effect;
|
||||
Effect* _effect = nullptr;
|
||||
|
||||
InputAssembler _inputAssembler;
|
||||
std::vector<ValueMap*> _defines;
|
||||
std::vector<std::unordered_map<std::string, Property>> _uniforms;
|
||||
std::vector<std::unordered_map<std::string, Effect::Property>> _uniforms;
|
||||
bool _dynamicIA = false;
|
||||
int _cullingMask = -1;
|
||||
int _userKey = -1;
|
||||
|
||||
Reference in New Issue
Block a user