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

Rename ShaderVar::Type to SLType and add some integer types. #1758

Merged
merged 1 commit into from
Oct 7, 2023
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
3 changes: 1 addition & 2 deletions tgfx/src/gpu/FragmentShaderBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ void FragmentShaderBuilder::onFinalize() {
}

void FragmentShaderBuilder::declareCustomOutputColor() {
outputs.emplace_back(CustomColorOutputName(), ShaderVar::Type::Float4,
ShaderVar::TypeModifier::Out);
outputs.emplace_back(CustomColorOutputName(), SLType::Float4, ShaderVar::TypeModifier::Out);
}

void FragmentShaderBuilder::onBeforeChildProcEmitCode() {
Expand Down
2 changes: 1 addition & 1 deletion tgfx/src/gpu/ProgramBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void ProgramBuilder::emitAndInstallGeoProc(std::string* outputColor, std::string
nameExpression(outputColor, "outputColor");
nameExpression(outputCoverage, "outputCoverage");

uniformHandler()->addUniform(ShaderFlags::Vertex, ShaderVar::Type::Float4, RTAdjustName);
uniformHandler()->addUniform(ShaderFlags::Vertex, SLType::Float4, RTAdjustName);
auto geometryProcessor = pipeline->getGeometryProcessor();
// Enclose custom code in a block to avoid namespace conflicts
fragmentShaderBuilder()->codeAppendf("{ // Stage %d %s\n", _stageIndex,
Expand Down
42 changes: 42 additions & 0 deletions tgfx/src/gpu/SLType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

namespace tgfx {
/**
* Types of shader-language-specific boxed variables we can create.
*/
enum class SLType {
Void,
Float,
Float2,
Float3,
Float4,
Float2x2,
Float3x3,
Float4x4,
Int,
Int2,
Int3,
Int4,
Texture2DSampler,
TextureExternalSampler,
Texture2DRectSampler,
};
} // namespace tgfx
24 changes: 6 additions & 18 deletions tgfx/src/gpu/ShaderVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include "BitmaskOperators.h"
#include "SLType.h"

namespace tgfx {
enum class ShaderFlags : unsigned {
Expand All @@ -32,19 +33,6 @@ enum class ShaderFlags : unsigned {

class ShaderVar {
public:
enum class Type {
Void,
Float,
Float2,
Float3,
Float4,
Float3x3,
Float4x4,
Texture2DSampler,
TextureExternalSampler,
Texture2DRectSampler,
};

enum class TypeModifier {
None,
Attribute,
Expand All @@ -55,10 +43,10 @@ class ShaderVar {

ShaderVar() = default;

ShaderVar(std::string name, Type type) : _type(type), _name(std::move(name)) {
ShaderVar(std::string name, SLType type) : _type(type), _name(std::move(name)) {
}

ShaderVar(std::string name, Type type, TypeModifier typeModifier)
ShaderVar(std::string name, SLType type, TypeModifier typeModifier)
: _type(type), _typeModifier(typeModifier), _name(std::move(name)) {
}

Expand All @@ -70,11 +58,11 @@ class ShaderVar {
return _name;
}

void setType(Type type) {
void setType(SLType type) {
_type = type;
}

Type type() const {
SLType type() const {
return _type;
}

Expand All @@ -87,7 +75,7 @@ class ShaderVar {
}

private:
Type _type = Type::Void;
SLType _type = SLType::Void;
TypeModifier _typeModifier = TypeModifier::None;
std::string _name;
};
Expand Down
4 changes: 2 additions & 2 deletions tgfx/src/gpu/UniformHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UniformHandler {
* be accessible. At least one bit must be set. The actual uniform name will be mangled. Returns
* the final uniform name.
*/
std::string addUniform(ShaderFlags visibility, ShaderVar::Type type, const std::string& name) {
std::string addUniform(ShaderFlags visibility, SLType type, const std::string& name) {
bool mangle = name.find(NO_MANGLE_PREFIX) != 0;
return internalAddUniform(visibility, type, name, mangle);
}
Expand All @@ -57,7 +57,7 @@ class UniformHandler {

virtual SamplerHandle addSampler(const TextureSampler* sampler, const std::string& name) = 0;

virtual std::string internalAddUniform(ShaderFlags visibility, ShaderVar::Type type,
virtual std::string internalAddUniform(ShaderFlags visibility, SLType type,
const std::string& name, bool mangleName) = 0;

virtual std::string getUniformDeclarations(ShaderFlags visibility) const = 0;
Expand Down
2 changes: 1 addition & 1 deletion tgfx/src/gpu/VaryingHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "ProgramBuilder.h"

namespace tgfx {
Varying VaryingHandler::addVarying(const std::string& name, ShaderVar::Type type) {
Varying VaryingHandler::addVarying(const std::string& name, SLType type) {
Varying varying;
varying._type = type;
varying._name = programBuilder->nameVariable('v', name);
Expand Down
6 changes: 3 additions & 3 deletions tgfx/src/gpu/VaryingHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class Varying {
const std::string& name() const {
return _name;
}
ShaderVar::Type type() const {
SLType type() const {
return _type;
}

private:
ShaderVar::Type _type = ShaderVar::Type::Void;
SLType _type = SLType::Void;
std::string _name;

friend class VaryingHandler;
Expand All @@ -55,7 +55,7 @@ class VaryingHandler {

virtual ~VaryingHandler() = default;

Varying addVarying(const std::string& name, ShaderVar::Type type);
Varying addVarying(const std::string& name, SLType type);

void emitAttributes(const GeometryProcessor& processor);

Expand Down
4 changes: 2 additions & 2 deletions tgfx/src/gpu/processors/DefaultGeometryProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ DefaultGeometryProcessor::DefaultGeometryProcessor(Color color, int width, int h
height(height),
viewMatrix(viewMatrix),
localMatrix(localMatrix) {
position = {"aPosition", ShaderVar::Type::Float2};
coverage = {"inCoverage", ShaderVar::Type::Float};
position = {"aPosition", SLType::Float2};
coverage = {"inCoverage", SLType::Float};
setVertexAttributes(&position, 2);
}
} // namespace tgfx
10 changes: 5 additions & 5 deletions tgfx/src/gpu/processors/EllipseGeometryProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ EllipseGeometryProcessor::EllipseGeometryProcessor(int width, int height, bool s
localMatrix(localMatrix),
stroke(stroke),
useScale(useScale) {
inPosition = {"inPosition", ShaderVar::Type::Float2};
inColor = {"inColor", ShaderVar::Type::Float4};
inPosition = {"inPosition", SLType::Float2};
inColor = {"inColor", SLType::Float4};
if (useScale) {
inEllipseOffset = {"inEllipseOffset", ShaderVar::Type::Float3};
inEllipseOffset = {"inEllipseOffset", SLType::Float3};
} else {
inEllipseOffset = {"inEllipseOffset", ShaderVar::Type::Float2};
inEllipseOffset = {"inEllipseOffset", SLType::Float2};
}
inEllipseRadii = {"inEllipseRadii", ShaderVar::Type::Float4};
inEllipseRadii = {"inEllipseRadii", SLType::Float4};
this->setVertexAttributes(&inPosition, 4);
}

Expand Down
23 changes: 15 additions & 8 deletions tgfx/src/gpu/processors/GeometryProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,24 @@ static constexpr char TRANSFORM_UNIFORM_PREFIX[] = "CoordTransformMatrix_";
/**
* Returns the size of the attrib type in bytes.
*/
static constexpr size_t VertexAttribTypeSize(ShaderVar::Type type) {
static constexpr size_t VertexAttribTypeSize(SLType type) {
switch (type) {
case ShaderVar::Type::Float:
case SLType::Float:
return sizeof(float);
case ShaderVar::Type::Float2:
case SLType::Float2:
return 2 * sizeof(float);
case ShaderVar::Type::Float3:
case SLType::Float3:
return 3 * sizeof(float);
case ShaderVar::Type::Float4:
case SLType::Float4:
return 4 * sizeof(float);
case SLType::Int:
return sizeof(int32_t);
case SLType::Int2:
return 2 * sizeof(int32_t);
case SLType::Int3:
return 3 * sizeof(int32_t);
case SLType::Int4:
return 4 * sizeof(int32_t);
default:
return 0;
}
Expand Down Expand Up @@ -90,11 +98,10 @@ void GeometryProcessor::emitTransforms(VertexShaderBuilder* vertexBuilder,
while (transformHandler->nextCoordTransform() != nullptr) {
std::string strUniName = TRANSFORM_UNIFORM_PREFIX;
strUniName += std::to_string(i);
auto uniName =
uniformHandler->addUniform(ShaderFlags::Vertex, ShaderVar::Type::Float3x3, strUniName);
auto uniName = uniformHandler->addUniform(ShaderFlags::Vertex, SLType::Float3x3, strUniName);
std::string strVaryingName = "TransformedCoords_";
strVaryingName += std::to_string(i);
ShaderVar::Type varyingType = ShaderVar::Type::Float2;
SLType varyingType = SLType::Float2;
auto varying = varyingHandler->addVarying(strVaryingName, varyingType);

transformHandler->specifyCoordsForCurrCoordTransform(varying.name(), varyingType);
Expand Down
7 changes: 3 additions & 4 deletions tgfx/src/gpu/processors/GeometryProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class GeometryProcessor : public Processor {
class Attribute {
public:
Attribute() = default;
Attribute(std::string name, ShaderVar::Type gpuType)
: _name(std::move(name)), _gpuType(gpuType) {
Attribute(std::string name, SLType gpuType) : _name(std::move(name)), _gpuType(gpuType) {
}

bool isInitialized() const {
Expand All @@ -52,7 +51,7 @@ class GeometryProcessor : public Processor {
const std::string& name() const {
return _name;
}
ShaderVar::Type gpuType() const {
SLType gpuType() const {
return _gpuType;
}

Expand All @@ -68,7 +67,7 @@ class GeometryProcessor : public Processor {

private:
std::string _name;
ShaderVar::Type _gpuType = ShaderVar::Type::Float;
SLType _gpuType = SLType::Float;
};

const std::vector<const Attribute*>& vertexAttributes() const {
Expand Down
8 changes: 4 additions & 4 deletions tgfx/src/gpu/processors/QuadPerEdgeAAGeometryProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ QuadPerEdgeAAGeometryProcessor::QuadPerEdgeAAGeometryProcessor(int width, int he
bool hasColor)
: GeometryProcessor(ClassID()), width(width), height(height), aa(aa) {
if (aa == AAType::Coverage) {
position = {"aPositionWithCoverage", ShaderVar::Type::Float3};
position = {"aPositionWithCoverage", SLType::Float3};
} else {
position = {"aPosition", ShaderVar::Type::Float2};
position = {"aPosition", SLType::Float2};
}
localCoord = {"localCoord", ShaderVar::Type::Float2};
localCoord = {"localCoord", SLType::Float2};
if (hasColor) {
color = {"inColor", ShaderVar::Type::Float4};
color = {"inColor", SLType::Float4};
}
setVertexAttributes(&position, 3);
}
Expand Down
12 changes: 6 additions & 6 deletions tgfx/src/opengl/GLOpsRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ struct AttribLayout {
unsigned type = 0;
};

static constexpr std::pair<ShaderVar::Type, AttribLayout> attribLayoutPair[] = {
{ShaderVar::Type::Float, {false, 1, GL_FLOAT}},
{ShaderVar::Type::Float2, {false, 2, GL_FLOAT}},
{ShaderVar::Type::Float3, {false, 3, GL_FLOAT}},
{ShaderVar::Type::Float4, {false, 4, GL_FLOAT}}};
static constexpr std::pair<SLType, AttribLayout> attribLayoutPair[] = {
{SLType::Float, {false, 1, GL_FLOAT}}, {SLType::Float2, {false, 2, GL_FLOAT}},
{SLType::Float3, {false, 3, GL_FLOAT}}, {SLType::Float4, {false, 4, GL_FLOAT}},
{SLType::Int, {false, 1, GL_INT}}, {SLType::Int2, {false, 2, GL_INT}},
{SLType::Int3, {false, 3, GL_INT}}, {SLType::Int4, {false, 4, GL_INT}}};

static AttribLayout GetAttribLayout(ShaderVar::Type type) {
static AttribLayout GetAttribLayout(SLType type) {
for (const auto& pair : attribLayoutPair) {
if (pair.first == type) {
return pair.second;
Expand Down
2 changes: 1 addition & 1 deletion tgfx/src/opengl/GLProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace tgfx {
class GLProgram : public Program {
public:
struct Attribute {
ShaderVar::Type gpuType = ShaderVar::Type::Float;
SLType gpuType = SLType::Float;
size_t offset = 0;
int location = 0;
};
Expand Down
31 changes: 18 additions & 13 deletions tgfx/src/opengl/GLProgramBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,26 @@ static std::string TypeModifierString(bool isDesktopGL, ShaderVar::TypeModifier
}
}

static constexpr std::pair<ShaderVar::Type, const char*> kSLTypes[] = {
{ShaderVar::Type::Void, "void"},
{ShaderVar::Type::Float, "float"},
{ShaderVar::Type::Float2, "vec2"},
{ShaderVar::Type::Float3, "vec3"},
{ShaderVar::Type::Float4, "vec4"},
{ShaderVar::Type::Float3x3, "mat3"},
{ShaderVar::Type::Float4x4, "mat4"},
{ShaderVar::Type::Texture2DRectSampler, "sampler2DRect"},
{ShaderVar::Type::TextureExternalSampler, "samplerExternalOES"},
{ShaderVar::Type::Texture2DSampler, "sampler2D"},
static constexpr std::pair<SLType, const char*> SLTypes[] = {
{SLType::Void, "void"},
{SLType::Float, "float"},
{SLType::Float2, "vec2"},
{SLType::Float3, "vec3"},
{SLType::Float4, "vec4"},
{SLType::Float2x2, "mat2"},
{SLType::Float3x3, "mat3"},
{SLType::Float4x4, "mat4"},
{SLType::Int, "int"},
{SLType::Int2, "ivec2"},
{SLType::Int3, "ivec3"},
{SLType::Int4, "ivec4"},
{SLType::Texture2DRectSampler, "sampler2DRect"},
{SLType::TextureExternalSampler, "samplerExternalOES"},
{SLType::Texture2DSampler, "sampler2D"},
};

static std::string SLTypeString(ShaderVar::Type t) {
for (const auto& pair : kSLTypes) {
static std::string SLTypeString(SLType t) {
for (const auto& pair : SLTypes) {
if (pair.first == t) {
return pair.second;
}
Expand Down
Loading