axmol/core/renderer/backend/RenderTarget.h

96 lines
2.6 KiB
C
Raw Normal View History

#pragma once
#include "base/CCRef.h"
#include "Texture.h"
#include <assert.h>
NS_AX_BACKEND_BEGIN
class RenderTarget : public axis::Ref
2021-12-25 10:04:45 +08:00
{
public:
2021-12-25 10:04:45 +08:00
struct RenderBuffer
{
TextureBackend* texture = nullptr;
2021-12-25 10:04:45 +08:00
uint8_t level = 0; // level when attached to a texture
explicit operator bool() const { return texture != nullptr; }
};
typedef RenderBuffer ColorAttachment[MAX_COLOR_ATTCHMENT];
RenderTarget(bool defaultRenderTarget) : _defaultRenderTarget(defaultRenderTarget) {}
virtual ~RenderTarget()
{
for (auto colorItem : _color)
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(colorItem.texture);
AX_SAFE_RELEASE(_depth.texture);
AX_SAFE_RELEASE(_stencil.texture);
}
2022-06-24 14:18:48 +08:00
bool isDefaultRenderTarget() const { return _defaultRenderTarget; }
void addFlag(TargetBufferFlags flag) {
setTargetFlags(_flags |= flag);
}
void removeFlag(TargetBufferFlags flag) {
setTargetFlags(_flags & ~flag);
}
2022-06-24 14:18:48 +08:00
TargetBufferFlags getTargetFlags() const { return _flags; }
void setTargetFlags(TargetBufferFlags flags) {
_flags = flags;
_dirty = true;
}
2022-06-24 14:18:48 +08:00
void setColorAttachment(ColorAttachment attachment)
2021-12-25 10:04:45 +08:00
{
for (auto colorItem : _color)
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(colorItem.texture);
memcpy(_color, attachment, sizeof(ColorAttachment));
for (auto colorItem : _color)
2022-07-15 19:17:01 +08:00
AX_SAFE_RETAIN(colorItem.texture);
2022-06-24 14:18:48 +08:00
_dirty = true;
};
2022-06-24 14:18:48 +08:00
void setColorAttachment(TextureBackend* attachment, int level = 0, int index = 0) {
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(_color[index].texture);
2022-06-24 14:18:48 +08:00
_color[index].texture = attachment;
_color[index].level = level;
2022-07-15 19:17:01 +08:00
AX_SAFE_RETAIN(_color[index].texture);
2022-06-24 14:18:48 +08:00
_dirty = true;
}
void setDepthAttachment(TextureBackend* attachment, int level = 0)
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(_depth.texture);
_depth.texture = attachment;
2021-12-25 10:04:45 +08:00
_depth.level = level;
2022-07-15 19:17:01 +08:00
AX_SAFE_RETAIN(_depth.texture);
2022-06-24 14:18:48 +08:00
_dirty = true;
};
2022-06-24 14:18:48 +08:00
void setStencilAttachment(TextureBackend* attachment, int level = 0)
2021-12-25 10:04:45 +08:00
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(_stencil.texture);
_stencil.texture = attachment;
2021-12-25 10:04:45 +08:00
_stencil.level = level;
2022-07-15 19:17:01 +08:00
AX_SAFE_RETAIN(_stencil.texture);
2021-12-25 10:04:45 +08:00
2022-06-24 14:18:48 +08:00
_dirty = true;
};
bool isDirty() const { return _dirty; }
2020-10-27 16:58:37 +08:00
ColorAttachment _color{};
RenderBuffer _depth{};
RenderBuffer _stencil{};
TargetBufferFlags _flags{};
2021-12-25 10:04:45 +08:00
2020-10-27 16:58:37 +08:00
protected:
bool _defaultRenderTarget = false;
2022-06-24 14:18:48 +08:00
mutable bool _dirty = false;
// uint8_t samples = 1;
};
NS_AX_BACKEND_END