Skip to content

Instantly share code, notes, and snippets.

@Pctg-x8
Created December 23, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pctg-x8/da0000a41a640b7ac5a24c9adea7d02a to your computer and use it in GitHub Desktop.
Save Pctg-x8/da0000a41a640b7ac5a24c9adea7d02a to your computer and use it in GitHub Desktop.
PracticalPixelEffect Headers
#pragma once
/*
The MIT License (MIT)
Copyright (c) 2016 S.Percentage
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,
ubject 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.
*/
#include <Siv3D.hpp>
// PracticalPixelEffects: Wrapper classes for Pixel Shader things(Complex PostEffect/GPU-Accelerated Procedural Texture Generation)
// void(void)
using RendererFunction = std::function<void()>;
// Accelerated(GPU) Texture Generator Base
class AcceleratedGeneratorBase
{
struct TextureInfoBuffer
{
float width, height, aspect_wh, pad;
};
const PixelShader shader;
ConstantBuffer<TextureInfoBuffer> textureInfo;
protected:
// Construct with program path
AcceleratedGeneratorBase(const String& prog_path)
: shader(prog_path) {}
virtual void setAdditionalBuffers() {} /* override this */
public:
// Generate texture
void generate(const RenderTexture& texture_to)
{
auto revert_to = Graphics2D::GetRenderTarget();
auto revert_state_to = Graphics2D::GetBlendState();
Graphics2D::SetRenderTarget(texture_to);
Graphics2D::SetBlendState(BlendState::Opaque);
Graphics2D::BeginPS(this->shader);
this->textureInfo->width = static_cast<float>(texture_to.width);
this->textureInfo->height = static_cast<float>(texture_to.height);
this->textureInfo->aspect_wh = static_cast<float>(texture_to.width) / static_cast<float>(texture_to.height);
Graphics2D::SetConstant(ShaderStage::Pixel, 1, this->textureInfo);
this->setAdditionalBuffers();
Rect(Point(0, 0), texture_to.size).draw();
Graphics2D::EndPS();
Graphics2D::SetBlendState(revert_state_to);
Graphics2D::SetRenderTarget(revert_to);
}
};
// PostEffect
class PixelEffect
{
const PixelShader pshader;
RenderTexture back;
public:
// Initialize effect with default parameters
PixelEffect(const String& shader_path, const Size& framesize) : pshader(shader_path), back(framesize) {}
// Draw source texture with effect(Override this)
virtual void drawEffected(RendererFunction func)
{
this->renderToBackbuffer(func);
this->apply();
}
protected:
// Clears the Backbuffer
void clearBackbuffer(const Color& c)
{
this->back.clear(c);
}
// Render an image onto Backbuffer
void renderToBackbuffer(RendererFunction f)
{
const auto revertTo = Graphics2D::GetRenderTarget();
Graphics2D::SetRenderTarget(this->back);
f();
Graphics2D::SetRenderTarget(revertTo);
}
// Render an image applying Pixel Shader
void apply()
{
Graphics2D::BeginPS(this->pshader);
this->back.draw();
Graphics2D::EndPS();
}
const auto& shader() const { return this->pshader; }
const auto& backbuffer() const { return this->back; }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment