Realistic perspective – Pixel art – Bloom effect

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Noiecity
Posts: 429
Joined: Wed Aug 23, 2023 7:22 pm
Contact:

Realistic perspective – Pixel art – Bloom effect

Post by Noiecity »

Image
Image

It's like Oblivion style, but in pixelart.
main.cpp

Code: Select all

#ifdef _MSC_VER
#pragma comment(lib,"Irrlicht.lib")
#pragma comment(linker,"/subsystem:windows /ENTRY:mainCRTStartup")
#endif
#include <irrlicht.h>
#include <cstdio>
#include <cmath>
#include "bloom_filter.hpp"
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
static const int BLOOM_BLUR_PASSES=4;
static const float BLOOM_THRESHOLD=2.5f;
static const float BLOOM_SOFTNESS=2.0f;
static const float BLOOM_RADIUS=3.0f;
static const float BLOOM_STRENGTH=300.0f;
static const float BLOOM_OPACITY=1.0f;
static const bool BLOOM_LIMIT_EXPOSURE=false;
static const float TILE_FOV_DEG=53.13f;
static const float TILE_FOV_RAD=TILE_FOV_DEG*core::DEGTORAD;
static ITexture* createHDRRenderTarget(IVideoDriver* driver,u32 width,u32 height,const char* name){
 ITexture* tex=driver->addRenderTargetTexture(dimension2d<u32>(width,height),name,ECF_A16B16G16R16F);
 if(!tex) std::printf("[HDR] ERROR: could not create A16B16G16R16F render target '%s'.\n",name);
 return tex;
}
static void drawScreenQuad(IVideoDriver* driver,ITexture* tex,f32 x0,f32 y0,f32 x1,f32 y1,f32 u0=0.0f,f32 v0=0.0f,f32 u1=1.0f,f32 v1=1.0f){
 const SColor white(255,255,255,255);
 S3DVertex v[4];
 v[0]=S3DVertex(x0,y0,0.0f,0,0,1,white,u0,v0);
 v[1]=S3DVertex(x1,y0,0.0f,0,0,1,white,u1,v0);
 v[2]=S3DVertex(x1,y1,0.0f,0,0,1,white,u1,v1);
 v[3]=S3DVertex(x0,y1,0.0f,0,0,1,white,u0,v1);
 const u16 idx[6]={0,1,2,0,2,3};
 SMaterial drawMat;
 drawMat.Lighting=false;
 drawMat.ZBuffer=ECFN_DISABLED;
 drawMat.ZWriteEnable=EZW_OFF;
 drawMat.MaterialType=EMT_SOLID;
 drawMat.TextureLayer[0].Texture=tex;
 drawMat.TextureLayer[0].BilinearFilter=false;
 drawMat.TextureLayer[0].TrilinearFilter=false;
 drawMat.TextureLayer[0].AnisotropicFilter=0;
 drawMat.TextureLayer[0].TextureWrapU=ETC_CLAMP_TO_EDGE;
 drawMat.TextureLayer[0].TextureWrapV=ETC_CLAMP_TO_EDGE;
 drawMat.TextureLayer[0].LODBias=0;
 driver->setMaterial(drawMat);
 driver->drawVertexPrimitiveList(v,4,idx,2,EVT_STANDARD,EPT_TRIANGLES);
}
static vector3df rotateAroundAxis(const vector3df& v,const vector3df& axis,f32 angle){
 vector3df a=axis;
 if(a.getLength()<1e-4f) return v;
 a.normalize();
 const f32 c=cosf(angle);
 const f32 s=sinf(angle);
 return v*c+a.crossProduct(v)*s+a*(a.dotProduct(v)*(1.0f-c));
}
class TwoViewportController:public IEventReceiver{
public:
 TwoViewportController(ICameraSceneNode* left,ICameraSceneNode* right,const vector3df& startPos,const vector3df& startTarget,ICursorControl* cursorControl,s32 centerX,s32 centerY,s32 screenW,s32 screenH,f32 moveSpeed=30.0f,f32 mouseSensitivity=0.15f,s32 edgeMargin=100)
  :CamLeft(left),CamRight(right),Position(startPos),MoveSpeed(moveSpeed),MouseSensitivity(mouseSensitivity),CursorControl(cursorControl),CenterX(centerX),CenterY(centerY),ScreenW(screenW),ScreenH(screenH),EdgeMargin(edgeMargin),LastMouseX(-1),LastMouseY(-1){
  const vector3df dir=(startTarget-startPos).normalize();
  Yaw=atan2f(dir.X,dir.Z);
  Pitch=asinf(core::clamp(dir.Y,-1.0f,1.0f));
  for(u32 i=0;i<KEY_KEY_CODES_COUNT;++i) KeyIsDown[i]=false;
  updateCameras();
 }
 void setScreenMetrics(s32 screenW,s32 screenH){
  if(screenW<=0||screenH<=0) return;
  ScreenW=screenW; ScreenH=screenH;
  CenterX=screenW/2; CenterY=screenH/2;
  const s32 minSide=(screenW<screenH)?screenW:screenH;
  s32 margin=minSide/10;
  if(margin<16) margin=16;
  if(margin>100) margin=100;
  EdgeMargin=margin;
  if(LastMouseX<0||LastMouseX>ScreenW-1||LastMouseY<0||LastMouseY>ScreenH-1){
   LastMouseX=CenterX; LastMouseY=CenterY;
   if(CursorControl) CursorControl->setPosition(CenterX,CenterY);
  }
 }
 bool OnEvent(const SEvent& event){
  if(event.EventType==EET_KEY_INPUT_EVENT){
   KeyIsDown[event.KeyInput.Key]=event.KeyInput.PressedDown;
   return false;
  }
  if(event.EventType==EET_MOUSE_INPUT_EVENT&&event.MouseInput.Event==EMIE_MOUSE_MOVED){
   const s32 x=event.MouseInput.X;
   const s32 y=event.MouseInput.Y;
   if(LastMouseX<0){ LastMouseX=x; LastMouseY=y; return true; }
   const s32 dx=x-LastMouseX;
   const s32 dy=y-LastMouseY;
   LastMouseX=x; LastMouseY=y;
   if(dx!=0||dy!=0){
    Yaw+=(f32)dx*MouseSensitivity*core::DEGTORAD;
    Pitch-=(f32)dy*MouseSensitivity*core::DEGTORAD;
    const f32 limit=89.0f*core::DEGTORAD;
    if(Pitch>limit) Pitch=limit;
    if(Pitch<-limit) Pitch=-limit;
   }
   if(CursorControl&&(x<EdgeMargin||x>ScreenW-EdgeMargin||y<EdgeMargin||y>ScreenH-EdgeMargin)){
    LastMouseX=CenterX; LastMouseY=CenterY;
    CursorControl->setPosition(CenterX,CenterY);
   }
   return true;
  }
  return false;
 }
 void update(f32 dt){
  vector3df fwd=getBaseDirection();
  fwd.Y=0;
  if(fwd.getLength()<1e-4f) fwd=vector3df(0,0,1);
  else fwd.normalize();
  const vector3df right=fwd.crossProduct(vector3df(0,1,0)).normalize();
  vector3df move(0,0,0);
  if(KeyIsDown[KEY_KEY_W]) move+=fwd;
  if(KeyIsDown[KEY_KEY_S]) move-=fwd;
  if(KeyIsDown[KEY_KEY_D]) move-=right;
  if(KeyIsDown[KEY_KEY_A]) move+=right;
  if(move.getLength()>1e-4f){ move.normalize(); Position+=move*MoveSpeed*dt; }
  updateCameras();
 }
private:
 vector3df getBaseDirection() const{
  vector3df d;
  d.X=cosf(Pitch)*sinf(Yaw);
  d.Y=sinf(Pitch);
  d.Z=cosf(Pitch)*cosf(Yaw);
  return d;
 }
 void updateCameras(){
  const vector3df baseDir=getBaseDirection();
  vector3df commonUp(0,1,0);
  commonUp-=baseDir*commonUp.dotProduct(baseDir);
  if(commonUp.getLength()<1e-4f) commonUp=vector3df(0,0,1);
  else commonUp.normalize();
  const f32 halfTileFov=0.5f*TILE_FOV_RAD;
  const vector3df leftDir=rotateAroundAxis(baseDir,commonUp,-halfTileFov);
  const vector3df rightDir=rotateAroundAxis(baseDir,commonUp,+halfTileFov);
  CamLeft->setPosition(Position);
  CamRight->setPosition(Position);
  CamLeft->setTarget(Position+leftDir);
  CamRight->setTarget(Position+rightDir);
  CamLeft->setUpVector(commonUp);
  CamRight->setUpVector(commonUp);
  CamLeft->setAspectRatio(1.0f);
  CamRight->setAspectRatio(1.0f);
  CamLeft->setFOV(TILE_FOV_RAD);
  CamRight->setFOV(TILE_FOV_RAD);
 }
private:
 ICameraSceneNode* CamLeft;
 ICameraSceneNode* CamRight;
 vector3df Position;
 f32 Yaw;
 f32 Pitch;
 f32 MoveSpeed;
 f32 MouseSensitivity;
 ICursorControl* CursorControl;
 s32 CenterX;
 s32 CenterY;
 s32 ScreenW;
 s32 ScreenH;
 s32 EdgeMargin;
 s32 LastMouseX;
 s32 LastMouseY;
 bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main(){
 IrrlichtDevice* device=createDevice(video::EDT_DIRECT3D9,dimension2d<u32>(1920,1080),32,false,false,false,0);
 if(!device) return 1;
 IVideoDriver* driver=device->getVideoDriver();
 ISceneManager* smgr=device->getSceneManager();
 IGUIEnvironment* guienv=device->getGUIEnvironment();
 driver->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS,false);
 IAnimatedMesh* mesh=smgr->getMesh("../../media/sydney.md2");
 if(!mesh){ device->drop(); return 1; }
 IAnimatedMeshSceneNode* node=smgr->addAnimatedMeshSceneNode(mesh);
 if(node){
  node->setMaterialFlag(EMF_LIGHTING,false);
  node->setMD2Animation(scene::EMAT_STAND);
  node->setMaterialFlag(EMF_BILINEAR_FILTER,false);
  node->setMaterialTexture(0,driver->getTexture("../../media/sydney.bmp"));
 }
 ICameraSceneNode* camLeft=smgr->addCameraSceneNode();
 camLeft->setNearValue(1.0f);
 camLeft->setFarValue(400.0f);
 ICameraSceneNode* camRight=smgr->addCameraSceneNode();
 camRight->setNearValue(1.0f);
 camRight->setFarValue(400.0f);
 const u32 RT_SIZE=128;
 const u32 COMBINED_W=RT_SIZE*2;
 const u32 COMBINED_H=RT_SIZE;
 ITexture* rtCombined=createHDRRenderTarget(driver,COMBINED_W,COMBINED_H,"rtCombined");
 if(!rtCombined){ device->drop(); return 1; }
 std::printf("[Main] combined HDR RT format = %d\n",(int)rtCombined->getColorFormat());
 bloom::BloomFilter bloomFilter(driver,COMBINED_W,COMBINED_H,0.0f,rtCombined->getColorFormat());
 if(!bloomFilter.isValid()){ device->drop(); return 1; }
 bloomFilter.setThreshold(BLOOM_THRESHOLD);
 bloomFilter.setSoftness(BLOOM_SOFTNESS);
 bloomFilter.setRadius(BLOOM_RADIUS);
 bloomFilter.setStrength(BLOOM_STRENGTH);
 bloomFilter.setLimitExposure(BLOOM_LIMIT_EXPOSURE);
 bloomFilter.setOpacity(BLOOM_OPACITY);
 ICursorControl* cursorControl=device->getCursorControl();
 const s32 screenW=(s32)driver->getScreenSize().Width;
 const s32 screenH=(s32)driver->getScreenSize().Height;
 const s32 centerX=screenW/2;
 const s32 centerY=screenH/2;
 TwoViewportController controller(camLeft,camRight,vector3df(0,20,-40),vector3df(0,5,0),cursorControl,centerX,centerY,screenW,screenH,30.0f,0.15f,100);
 device->setEventReceiver(&controller);
 if(cursorControl){ cursorControl->setVisible(false); cursorControl->setPosition(centerX,centerY); }
 IGUIStaticText* fpsText=guienv->addStaticText(L"FPS: 0",rect<s32>(10,10,240,30),false,false,0,-1,true);
 fpsText->setOverrideColor(SColor(255,255,255,0));
 fpsText->setBackgroundColor(SColor(160,0,0,0));
 u32 lastTime=device->getTimer()->getTime();
 while(device->run()){
  const u32 now=device->getTimer()->getTime();
  const f32 dt=(f32)(now-lastTime)/1000.0f;
  lastTime=now;
  {
   const dimension2d<u32> curSize=driver->getScreenSize();
   controller.setScreenMetrics((s32)curSize.Width,(s32)curSize.Height);
  }
  controller.update(dt);
  driver->beginScene(true,true,SColor(255,0,0,0));
  driver->setRenderTarget(rtCombined,true,true,SColor(255,0,0,0));
  driver->setViewPort(rect<s32>(0,0,(s32)RT_SIZE,(s32)RT_SIZE));
  smgr->setActiveCamera(camLeft);
  smgr->drawAll();
  driver->setViewPort(rect<s32>((s32)RT_SIZE,0,(s32)COMBINED_W,(s32)RT_SIZE));
  smgr->setActiveCamera(camRight);
  smgr->drawAll();
  driver->setViewPort(rect<s32>(0,0,(s32)COMBINED_W,(s32)COMBINED_H));
  ITexture* combinedFiltered=bloomFilter.process(rtCombined,BLOOM_BLUR_PASSES);
  driver->setRenderTarget(0,false,false);
  const dimension2d<u32> screenSize=driver->getScreenSize();
  const f32 windowW=(f32)screenSize.Width;
  const f32 windowH=(f32)screenSize.Height;
  if(windowW<=0.0f||windowH<=0.0f) continue;
  const f32 sourceW=(f32)COMBINED_W;
  const f32 sourceH=(f32)COMBINED_H;
  const f32 sourceAspect=sourceW/sourceH;
  const f32 windowAspect=windowW/windowH;
  f32 displayW;
  f32 displayH;
  if(windowAspect<sourceAspect){ displayH=windowH; displayW=displayH*sourceAspect; }
  else{ displayW=windowW; displayH=displayW/sourceAspect; }
  const f32 offsetX=(windowW-displayW)*0.5f;
  const f32 offsetY=(windowH-displayH)*0.5f;
  matrix4 ortho;
  ortho.makeIdentity();
  ortho[0]=2.0f/windowW;
  ortho[5]=-2.0f/windowH;
  ortho[12]=-1.0f;
  ortho[13]=1.0f;
  matrix4 identity;
  identity.makeIdentity();
  driver->setTransform(ETS_PROJECTION,ortho);
  driver->setTransform(ETS_VIEW,identity);
  driver->setTransform(ETS_WORLD,identity);
  driver->setViewPort(rect<s32>(0,0,(s32)screenSize.Width,(s32)screenSize.Height));
  drawScreenQuad(driver,combinedFiltered,offsetX,offsetY,offsetX+displayW,offsetY+displayH,0.0f,0.0f,1.0f,1.0f);
  core::stringw fpsStr=L"FPS: ";
  fpsStr+=driver->getFPS();
  fpsText->setText(fpsStr.c_str());
  guienv->drawAll();
  driver->endScene();
 }
 device->drop();
 return 0;
}

bloom_filter.hpp:

Code: Select all

// bloom_filter.hpp
#ifndef BLOOM_FILTER_HPP
#define BLOOM_FILTER_HPP

#include <irrlicht.h>
#include <cstdio>
#include <cmath>
#include <algorithm>

namespace bloom {

// -----------------------------------------------------------------------------
// Fullscreen vertex shader
// Shader Model 3.0
// -----------------------------------------------------------------------------
static const char* VS_FULLSCREEN_HLSL =
    "struct VS_OUT {\n"
    "    float4 Pos : POSITION;\n"
    "    float2 UV  : TEXCOORD0;\n"
    "};\n"
    "VS_OUT main(float4 Pos : POSITION, float2 UV : TEXCOORD0)\n"
    "{\n"
    "    VS_OUT o;\n"
    "    o.Pos = Pos;\n"
    "    o.UV  = UV;\n"
    "    return o;\n"
    "}\n";

// -----------------------------------------------------------------------------
// Extract bloom mask.
//
// Mirrors the important structure of GEGL/GIMP Bloom:
//   luminance -> levels -> clip -> multiply(original, mask)
//
// threshold : 0..100
// softness  : edge softness in the same 0..100 UI scale
// strength  : output-high / bloom energy, 100 == 1.0
// -----------------------------------------------------------------------------
static const char* PS_EXTRACT_HLSL =
    "sampler2D tex : register(s0);\n"
    "float2 halfBoundary;\n"
    "float2 globalTexelSize;\n"
    "float threshold;\n"
    "float softness;\n"
    "float strength;\n"
    "\n"
    "float srgbToLinear(float x)\n"
    "{\n"
    "    if (x <= 0.04045) return x / 12.92;\n"
    "    if (x <= 1.0) return pow((x + 0.055) / 1.055, 2.4);\n"
    "    return x;\n"
    "}\n"
    "\n"
    "float3 srgbToLinear3(float3 c)\n"
    "{\n"
    "    return float3(\n"
    "        srgbToLinear(c.r),\n"
    "        srgbToLinear(c.g),\n"
    "        srgbToLinear(c.b));\n"
    "}\n"
    "\n"
    "float4 main(float2 uv : TEXCOORD0) : COLOR\n"
    "{\n"
    "    float2 uvMin = float2(0.0, 0.0);\n"
    "    float2 uvMax = float2(1.0, 1.0);\n"
    "\n"
    "    // Keep the two stereo images completely independent.\n"
    "    if (halfBoundary.x > 0.0)\n"
    "    {\n"
    "        if (uv.x < halfBoundary.x)\n"
    "            uvMax.x = halfBoundary.x - globalTexelSize.x * 0.5;\n"
    "        else\n"
    "            uvMin.x = halfBoundary.x + globalTexelSize.x * 0.5;\n"
    "    }\n"
    "\n"
    "    float2 sampleUV = clamp(uv, uvMin, uvMax);\n"
    "    float3 c = tex2D(tex, sampleUV).rgb;\n"
    "\n"
    "    // Approximation of GEGL's Y -> linear luminance conversion.\n"
    "    float3 linearC = srgbToLinear3(max(c, 0.0));\n"
    "    float lum = dot(linearC, float3(0.2126, 0.7152, 0.0722));\n"
    "\n"
    "    float inLow   = (threshold - softness) / 100.0;\n"
    "    float inHigh  = (threshold + softness) / 100.0;\n"
    "    float outHigh = max(strength / 100.0, 0.0);\n"
    "\n"
    "    // Levels with gamma = 1.0, followed by the rgb-clip high limit.\n"
    "    float t = saturate(\n"
    "        (lum - inLow) /\n"
    "        max(inHigh - inLow, 1e-6));\n"
    "\n"
    "    float mask = min(t * outHigh, outHigh);\n"
    "\n"
    "    // Preserve the original bloom color.\n"
    "    return float4(c * mask, 1.0);\n"
    "}\n";

// -----------------------------------------------------------------------------
// Separable Gaussian blur, 17 taps.
//
// 1 center + 8 symmetric pairs.
//
// IMPORTANT:
// The sample offsets passed in (sampleOffset01..67) are already expressed
// in UV units. They are pre-multiplied on the CPU by the per-eye texel size
// (see BlurCB). The shader therefore does not need a `texelSize` uniform,
// and one is intentionally not declared. Declaring an unused uniform would
// cause the HLSL compiler to strip it, making getPixelShaderConstantID
// return -1 and printing the "Variable to get ID not found" warning.
//
// Weights and positions are both derived from sigma.
// Radius is converted to sigma per pass:
//
//     sigmaPass = radius / sqrt(numberOfPasses)
//
// Since Gaussian variances add when Gaussians are convolved, N passes of
// sigmaPass approximately produce the requested final radius.
//
// Point sampling is used intentionally so a sample never interpolates through
// the stereo seam.
// -----------------------------------------------------------------------------
static const char* PS_BLUR_HLSL =
    "sampler2D tex : register(s0);\n"
    "float2 globalTexelSize;\n"
    "float2 halfBoundary;\n"
    "\n"
    "float4 sampleOffset01;\n"
    "float4 sampleOffset23;\n"
    "float4 sampleOffset45;\n"
    "float4 sampleOffset67;\n"
    "\n"
    "float4 sampleWeight0123;\n"
    "float4 sampleWeight4567;\n"
    "float sampleWeight8;\n"
    "\n"
    "// When halfBoundary.x <= 0 (split disabled), no clamping is performed\n"
    "// here. The sampler is configured with ETC_MIRROR, so out-of-range UVs\n"
    "// are reflected back inside the texture instead of being flattened to\n"
    "// the edge texel. Flattening to the edge produces a visible brightness\n"
    "// discontinuity along the texture borders (the seam).\n"
    "//\n"
    "// When halfBoundary.x > 0 (stereo packed in one texture), the clamp\n"
    "// keeps each eye's samples on its own side.\n"
    "float2 clampIfSplit(float2 p, float2 uvBase)\n"
    "{\n"
    "    if (halfBoundary.x <= 0.0)\n"
    "        return p;\n"
    "\n"
    "    float2 uvMin = float2(0.0, 0.0);\n"
    "    float2 uvMax = float2(1.0, 1.0);\n"
    "\n"
    "    if (uvBase.x < halfBoundary.x)\n"
    "        uvMax.x = halfBoundary.x - globalTexelSize.x * 0.5;\n"
    "    else\n"
    "        uvMin.x = halfBoundary.x + globalTexelSize.x * 0.5;\n"
    "\n"
    "    return clamp(p, uvMin, uvMax);\n"
    "}\n"
    "\n"
    "float4 main(float2 uv : TEXCOORD0) : COLOR\n"
    "{\n"
    "    float3 sum =\n"
    "        tex2D(tex, clampIfSplit(uv, uv)).rgb *\n"
    "        sampleWeight0123.x;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset01.xy, uv)).rgb *\n"
    "        sampleWeight0123.y;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset01.xy, uv)).rgb *\n"
    "        sampleWeight0123.y;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset01.zw, uv)).rgb *\n"
    "        sampleWeight0123.z;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset01.zw, uv)).rgb *\n"
    "        sampleWeight0123.z;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset23.xy, uv)).rgb *\n"
    "        sampleWeight0123.w;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset23.xy, uv)).rgb *\n"
    "        sampleWeight0123.w;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset23.zw, uv)).rgb *\n"
    "        sampleWeight4567.x;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset23.zw, uv)).rgb *\n"
    "        sampleWeight4567.x;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset45.xy, uv)).rgb *\n"
    "        sampleWeight4567.y;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset45.xy, uv)).rgb *\n"
    "        sampleWeight4567.y;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset45.zw, uv)).rgb *\n"
    "        sampleWeight4567.z;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset45.zw, uv)).rgb *\n"
    "        sampleWeight4567.z;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset67.xy, uv)).rgb *\n"
    "        sampleWeight4567.w;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset67.xy, uv)).rgb *\n"
    "        sampleWeight4567.w;\n"
    "\n"
    "    sum += tex2D(tex, clampIfSplit(uv + sampleOffset67.zw, uv)).rgb *\n"
    "        sampleWeight8;\n"
    "    sum += tex2D(tex, clampIfSplit(uv - sampleOffset67.zw, uv)).rgb *\n"
    "        sampleWeight8;\n"
    "\n"
    "    return float4(sum, 1.0);\n"
    "}\n";

// -----------------------------------------------------------------------------
// Combine original + blurred bloom.
//
// GEGL:
//   limit-exposure = false -> add
//   limit-exposure = true  -> screen
// -----------------------------------------------------------------------------
static const char* PS_COMBINE_HLSL =
    "sampler2D texOrig  : register(s0);\n"
    "sampler2D texBloom : register(s1);\n"
    "float opacity;\n"
    "float limitExposure;\n"
    "\n"
    "float4 main(float2 uv : TEXCOORD0) : COLOR\n"
    "{\n"
    "    float3 a = tex2D(texOrig,  uv).rgb;\n"
    "    float3 b = tex2D(texBloom, uv).rgb;\n"
    "\n"
    "    float3 result;\n"
    "\n"
    "    if (limitExposure > 0.5)\n"
    "    {\n"
    "        result = 1.0 - (1.0 - a) * (1.0 - b);\n"
    "    }\n"
    "    else\n"
    "    {\n"
    "        result = a + b;\n"
    "    }\n"
    "\n"
    "    return float4(\n"
    "        lerp(a, result, saturate(opacity)),\n"
    "        1.0);\n"
    "}\n";

// -----------------------------------------------------------------------------
// Callback: extract
// -----------------------------------------------------------------------------
struct ExtractCB : public irr::video::IShaderConstantSetCallBack
{
    irr::s32 halfBoundaryID;
    irr::s32 globalTexelSizeID;
    irr::s32 thresholdID;
    irr::s32 softnessID;
    irr::s32 strengthID;

    irr::f32 halfBoundary[2];
    irr::f32 globalTexelSize[2];

    irr::f32 threshold;
    irr::f32 softness;
    irr::f32 strength;

    ExtractCB(irr::f32 combinedW,
              irr::f32 combinedH,
              irr::f32 splitX,
              irr::f32 t,
              irr::f32 s,
              irr::f32 str)
        : halfBoundaryID(-1),
          globalTexelSizeID(-1),
          thresholdID(-1),
          softnessID(-1),
          strengthID(-1),
          threshold(t),
          softness(s),
          strength(str)
    {
        halfBoundary[0] = splitX;
        halfBoundary[1] = 1.0f;

        globalTexelSize[0] =
            1.0f / std::max(combinedW, 1.0f);

        globalTexelSize[1] =
            1.0f / std::max(combinedH, 1.0f);
    }

    void OnCreate(
        irr::video::IMaterialRendererServices* s,
        irr::s32) override
    {
        halfBoundaryID =
            s->getPixelShaderConstantID("halfBoundary");

        globalTexelSizeID =
            s->getPixelShaderConstantID("globalTexelSize");

        thresholdID =
            s->getPixelShaderConstantID("threshold");

        softnessID =
            s->getPixelShaderConstantID("softness");

        strengthID =
            s->getPixelShaderConstantID("strength");
    }

    void OnSetConstants(
        irr::video::IMaterialRendererServices* s,
        irr::s32) override
    {
        if (halfBoundaryID >= 0)
            s->setPixelShaderConstant(
                halfBoundaryID,
                halfBoundary,
                2);

        if (globalTexelSizeID >= 0)
            s->setPixelShaderConstant(
                globalTexelSizeID,
                globalTexelSize,
                2);

        if (thresholdID >= 0)
            s->setPixelShaderConstant(
                thresholdID,
                &threshold,
                1);

        if (softnessID >= 0)
            s->setPixelShaderConstant(
                softnessID,
                &softness,
                1);

        if (strengthID >= 0)
            s->setPixelShaderConstant(
                strengthID,
                &strength,
                1);
    }

    void OnSetMaterial(
        const irr::video::SMaterial&) override {}
};

// -----------------------------------------------------------------------------
// Callback: blur
//
// NOTE:
// The blur pixel shader does NOT declare a `texelSize` uniform. All UV-space
// sample offsets are pre-multiplied here on the CPU by the per-eye texel size.
// If a `texelSize` uniform were declared in the shader but unused, the HLSL
// compiler would strip it, and getPixelShaderConstantID("texelSize") would
// return -1, producing the "HLSL Variable to get ID not found" warning.
// -----------------------------------------------------------------------------
struct BlurCB : public irr::video::IShaderConstantSetCallBack
{
    irr::s32 globalTexelSizeID;
    irr::s32 halfBoundaryID;

    irr::s32 sampleOffset01ID;
    irr::s32 sampleOffset23ID;
    irr::s32 sampleOffset45ID;
    irr::s32 sampleOffset67ID;

    irr::s32 sampleWeight0123ID;
    irr::s32 sampleWeight4567ID;
    irr::s32 sampleWeight8ID;

    // Per-eye inverse texel size, used only on the CPU to build UV offsets.
    irr::f32 invEyeTexel[2];

    irr::f32 globalTexelSize[2];
    irr::f32 halfBoundary[2];

    irr::f32 sampleOffset[16];
    irr::f32 sampleWeight[9];

    irr::f32 sigma;
    irr::f32 eyeWidth;
    irr::f32 eyeHeight;

    BlurCB(irr::f32 combinedW,
           irr::f32 combinedH,
           irr::f32 splitX)
        : globalTexelSizeID(-1),
          halfBoundaryID(-1),
          sampleOffset01ID(-1),
          sampleOffset23ID(-1),
          sampleOffset45ID(-1),
          sampleOffset67ID(-1),
          sampleWeight0123ID(-1),
          sampleWeight4567ID(-1),
          sampleWeight8ID(-1),
          sigma(0.0f),
          eyeWidth(combinedW),
          eyeHeight(combinedH)
    {
        // For the stereo texture [eye | eye], one eye is only half of
        // the combined width. Horizontal offsets must therefore use
        // 1 / eyeWidth, not 1 / combinedWidth.
        if (splitX > 0.0f && splitX < 1.0f)
            eyeWidth = combinedW * splitX;

        invEyeTexel[0] =
            1.0f / std::max(eyeWidth, 1.0f);

        invEyeTexel[1] =
            1.0f / std::max(eyeHeight, 1.0f);

        globalTexelSize[0] =
            1.0f / std::max(combinedW, 1.0f);

        globalTexelSize[1] =
            1.0f / std::max(combinedH, 1.0f);

        halfBoundary[0] = splitX;
        halfBoundary[1] = 1.0f;

        std::fill(
            sampleOffset,
            sampleOffset + 16,
            0.0f);

        std::fill(
            sampleWeight,
            sampleWeight + 9,
            0.0f);

        sampleWeight[0] = 1.0f;
    }

    void setSigma(irr::f32 newSigma)
    {
        sigma = std::max(newSigma, 0.0f);
        rebuildKernel();
    }

    void OnCreate(
        irr::video::IMaterialRendererServices* s,
        irr::s32) override
    {
        globalTexelSizeID =
            s->getPixelShaderConstantID("globalTexelSize");

        halfBoundaryID =
            s->getPixelShaderConstantID("halfBoundary");

        sampleOffset01ID =
            s->getPixelShaderConstantID("sampleOffset01");

        sampleOffset23ID =
            s->getPixelShaderConstantID("sampleOffset23");

        sampleOffset45ID =
            s->getPixelShaderConstantID("sampleOffset45");

        sampleOffset67ID =
            s->getPixelShaderConstantID("sampleOffset67");

        sampleWeight0123ID =
            s->getPixelShaderConstantID("sampleWeight0123");

        sampleWeight4567ID =
            s->getPixelShaderConstantID("sampleWeight4567");

        sampleWeight8ID =
            s->getPixelShaderConstantID("sampleWeight8");
    }

    void OnSetConstants(
        irr::video::IMaterialRendererServices* s,
        irr::s32) override
    {
        if (globalTexelSizeID >= 0)
            s->setPixelShaderConstant(
                globalTexelSizeID,
                globalTexelSize,
                2);

        if (halfBoundaryID >= 0)
            s->setPixelShaderConstant(
                halfBoundaryID,
                halfBoundary,
                2);

        if (sampleOffset01ID >= 0)
            s->setPixelShaderConstant(
                sampleOffset01ID,
                sampleOffset + 0,
                4);

        if (sampleOffset23ID >= 0)
            s->setPixelShaderConstant(
                sampleOffset23ID,
                sampleOffset + 4,
                4);

        if (sampleOffset45ID >= 0)
            s->setPixelShaderConstant(
                sampleOffset45ID,
                sampleOffset + 8,
                4);

        if (sampleOffset67ID >= 0)
            s->setPixelShaderConstant(
                sampleOffset67ID,
                sampleOffset + 12,
                4);

        if (sampleWeight0123ID >= 0)
            s->setPixelShaderConstant(
                sampleWeight0123ID,
                sampleWeight + 0,
                4);

        if (sampleWeight4567ID >= 0)
            s->setPixelShaderConstant(
                sampleWeight4567ID,
                sampleWeight + 4,
                4);

        if (sampleWeight8ID >= 0)
            s->setPixelShaderConstant(
                sampleWeight8ID,
                sampleWeight + 8,
                1);
    }

    void OnSetMaterial(
        const irr::video::SMaterial&) override {}

private:
    void rebuildKernel()
    {
        std::fill(
            sampleOffset,
            sampleOffset + 16,
            0.0f);

        std::fill(
            sampleWeight,
            sampleWeight + 9,
            0.0f);

        if (sigma <= 1e-4f)
        {
            sampleWeight[0] = 1.0f;
            return;
        }

        // Center sample.
        sampleWeight[0] = 1.0f;

        float total = 1.0f;

        // Symmetric pairs at:
        //
        //   0.5 sigma
        //   1.0 sigma
        //   1.5 sigma
        //   2.0 sigma
        //   2.5 sigma
        //   3.0 sigma
        //   3.5 sigma
        //   4.0 sigma
        //
        // Point sampling requires the final offsets to be represented
        // as integer pixel distances.
        for (int i = 0; i < 8; ++i)
        {
            const float sigmaUnits =
                0.5f + 0.5f * (float)i;

            float pixelOffset =
                std::floor(
                    sigmaUnits * sigma + 0.5f);

            if (pixelOffset < 1.0f)
                pixelOffset = 1.0f;

            const float d =
                pixelOffset / sigma;

            const float w =
                std::exp(-0.5f * d * d);

            sampleOffset[i * 2 + 0] =
                pixelOffset * invEyeTexel[0];

            sampleOffset[i * 2 + 1] =
                pixelOffset * invEyeTexel[1];

            sampleWeight[i + 1] = w;

            total += 2.0f * w;
        }

        const float invTotal =
            1.0f / std::max(total, 1e-6f);

        for (int i = 0; i < 9; ++i)
            sampleWeight[i] *= invTotal;

        configureDirection(1.0f, 0.0f);
    }

public:
    void configureDirection(
        irr::f32 dx,
        irr::f32 dy)
    {
        if (sigma <= 1e-4f)
        {
            std::fill(
                sampleOffset,
                sampleOffset + 16,
                0.0f);

            return;
        }

        for (int i = 0; i < 8; ++i)
        {
            const float sigmaUnits =
                0.5f + 0.5f * (float)i;

            float pixelOffset =
                std::floor(
                    sigmaUnits * sigma + 0.5f);

            if (pixelOffset < 1.0f)
                pixelOffset = 1.0f;

            const float ox =
                dx * pixelOffset * invEyeTexel[0];

            const float oy =
                dy * pixelOffset * invEyeTexel[1];

            sampleOffset[i * 2 + 0] = ox;
            sampleOffset[i * 2 + 1] = oy;
        }
    }
};

// -----------------------------------------------------------------------------
// Callback: combine
// -----------------------------------------------------------------------------
struct CombineCB :
    public irr::video::IShaderConstantSetCallBack
{
    irr::s32 opacityID;
    irr::s32 limitExposureID;

    irr::f32 opacity;
    irr::f32 limitExposure;

    CombineCB()
        : opacityID(-1),
          limitExposureID(-1),
          opacity(1.0f),
          limitExposure(0.0f)
    {}

    void OnCreate(
        irr::video::IMaterialRendererServices* s,
        irr::s32) override
    {
        opacityID =
            s->getPixelShaderConstantID("opacity");

        limitExposureID =
            s->getPixelShaderConstantID(
                "limitExposure");
    }

    void OnSetConstants(
        irr::video::IMaterialRendererServices* s,
        irr::s32) override
    {
        if (opacityID >= 0)
            s->setPixelShaderConstant(
                opacityID,
                &opacity,
                1);

        if (limitExposureID >= 0)
            s->setPixelShaderConstant(
                limitExposureID,
                &limitExposure,
                1);
    }

    void OnSetMaterial(
        const irr::video::SMaterial&) override {}
};

// -----------------------------------------------------------------------------
// BloomFilter
//
// splitX = 0.0 -> normal single image
// splitX = 0.5 -> [left eye | right eye]
//
// GEGL-style topology:
//
//   input
//      |
//      +---- luminance -> levels -> rgb-clip ----+
//                                                |
//   input ------------------------------------ multiply
//                                                |
//                                         gaussian blur
//                                                |
//                           input ---------------+
//                                                |
//                                           add / screen
//                                                |
//                                              output
// -----------------------------------------------------------------------------
class BloomFilter
{
public:
    BloomFilter(
        irr::video::IVideoDriver* drv,
        irr::u32 w,
        irr::u32 h,
        irr::f32 splitX = 0.0f,
        irr::video::ECOLOR_FORMAT format =
            irr::video::ECF_A16B16G16R16F)
        : driver(drv),
          width(w),
          height(h),
          split(splitX),
          colorFormat(format),
          rttA(0),
          rttB(0),
          rttOut(0),
          extractMatType(-1),
          blurMatType(-1),
          combineMatType(-1),
          extractCB(0),
          blurCB(0),
          combineCB(0),
          radius(10.0f),
          valid(false)
    {
        using namespace irr;
        using namespace irr::video;

        std::printf(
            "[Bloom] Pixel shader caps: "
            "1_1=%d 2_0=%d 3_0=%d\n",
            (int)driver->queryFeature(
                EVDF_PIXEL_SHADER_1_1),
            (int)driver->queryFeature(
                EVDF_PIXEL_SHADER_2_0),
            (int)driver->queryFeature(
                EVDF_PIXEL_SHADER_3_0));

        if (!driver->queryFeature(
                EVDF_PIXEL_SHADER_3_0))
        {
            std::printf(
                "[Bloom] ERROR: "
                "Pixel Shader 3.0 is required.\n");

            return;
        }

        if (!driver->queryFeature(
                EVDF_RENDER_TO_TARGET))
        {
            std::printf(
                "[Bloom] ERROR: "
                "render-to-target is required.\n");

            return;
        }

        IGPUProgrammingServices* gpu =
            driver->getGPUProgrammingServices();

        if (!gpu)
        {
            std::printf(
                "[Bloom] ERROR: "
                "GPU programming services unavailable.\n");

            return;
        }

        rttA =
            driver->addRenderTargetTexture(
                core::dimension2d<u32>(w, h),
                "bf_A",
                colorFormat);

        rttB =
            driver->addRenderTargetTexture(
                core::dimension2d<u32>(w, h),
                "bf_B",
                colorFormat);

        rttOut =
            driver->addRenderTargetTexture(
                core::dimension2d<u32>(w, h),
                "bf_Out",
                colorFormat);

        if (!rttA || !rttB || !rttOut)
        {
            std::printf(
                "[Bloom] ERROR: "
                "failed to create bloom render targets.\n");

            return;
        }

        // Same defaults as GEGL Bloom.
        extractCB =
            new ExtractCB(
                (irr::f32)w,
                (irr::f32)h,
                split,
                50.0f,
                25.0f,
                50.0f);

        blurCB =
            new BlurCB(
                (irr::f32)w,
                (irr::f32)h,
                split);

        combineCB =
            new CombineCB();

        extractMatType =
            gpu->addHighLevelShaderMaterial(
                VS_FULLSCREEN_HLSL,
                "main",
                EVST_VS_3_0,
                PS_EXTRACT_HLSL,
                "main",
                EPST_PS_3_0,
                extractCB,
                EMT_SOLID,
                0);

        if (extractMatType < 0)
        {
            std::printf(
                "[Bloom] ERROR: "
                "extract material creation failed.\n");

            return;
        }

        blurMatType =
            gpu->addHighLevelShaderMaterial(
                VS_FULLSCREEN_HLSL,
                "main",
                EVST_VS_3_0,
                PS_BLUR_HLSL,
                "main",
                EPST_PS_3_0,
                blurCB,
                EMT_SOLID,
                0);

        if (blurMatType < 0)
        {
            std::printf(
                "[Bloom] ERROR: "
                "blur material creation failed.\n");

            return;
        }

        combineMatType =
            gpu->addHighLevelShaderMaterial(
                VS_FULLSCREEN_HLSL,
                "main",
                EVST_VS_3_0,
                PS_COMBINE_HLSL,
                "main",
                EPST_PS_3_0,
                combineCB,
                EMT_SOLID,
                0);

        if (combineMatType < 0)
        {
            std::printf(
                "[Bloom] ERROR: "
                "combine material creation failed.\n");

            return;
        }

        extractMat.Lighting = false;
        extractMat.ZBuffer = false;
        extractMat.ZWriteEnable = EZW_OFF;
        extractMat.MaterialType =
            (E_MATERIAL_TYPE)extractMatType;
        extractMat.setFlag(
            EMF_BILINEAR_FILTER,
            false);
extractMat.TextureLayer[0].TextureWrapU = irr::video::ETC_MIRROR;
extractMat.TextureLayer[0].TextureWrapV = irr::video::ETC_MIRROR;
        blurMat.Lighting = false;
        blurMat.ZBuffer = false;
        blurMat.ZWriteEnable = EZW_OFF;
        blurMat.MaterialType =
            (E_MATERIAL_TYPE)blurMatType;
        blurMat.setFlag(
            EMF_BILINEAR_FILTER,
            false);
        blurMat.TextureLayer[0].TextureWrapU = irr::video::ETC_MIRROR;
blurMat.TextureLayer[0].TextureWrapV = irr::video::ETC_MIRROR;
        combineMat.Lighting = false;
        combineMat.ZBuffer = false;
        combineMat.ZWriteEnable = EZW_OFF;
        combineMat.MaterialType =
            (E_MATERIAL_TYPE)combineMatType;
        combineMat.setFlag(
            EMF_BILINEAR_FILTER,
            false);

        const SColor white(
            255, 255, 255, 255);

        verts[0] =
            S3DVertex(
                -1.0f,  1.0f,
                 0.0f,
                 0, 0, 1,
                 white,
                 0.0f, 0.0f);

        verts[1] =
            S3DVertex(
                 1.0f,  1.0f,
                 0.0f,
                 0, 0, 1,
                 white,
                 1.0f, 0.0f);

        verts[2] =
            S3DVertex(
                 1.0f, -1.0f,
                 0.0f,
                 0, 0, 1,
                 white,
                 1.0f, 1.0f);

        verts[3] =
            S3DVertex(
                -1.0f, -1.0f,
                 0.0f,
                 0, 0, 1,
                 white,
                 0.0f, 1.0f);

        idx[0] = 0;
        idx[1] = 1;
        idx[2] = 2;
        idx[3] = 0;
        idx[4] = 2;
        idx[5] = 3;

        valid = true;

        std::printf(
            "[Bloom] initialized: "
            "%ux%u split=%.3f format=%d\n",
            w,
            h,
            split,
            (int)colorFormat);
    }

    ~BloomFilter()
    {
        if (extractCB)
            extractCB->drop();

        if (blurCB)
            blurCB->drop();

        if (combineCB)
            combineCB->drop();
    }

    bool isValid() const
    {
        return valid;
    }

    void setThreshold(irr::f32 t)
    {
        if (extractCB)
            extractCB->threshold =
                std::max(t, 0.0f);
    }

    void setSoftness(irr::f32 s)
    {
        if (extractCB)
            extractCB->softness =
                std::max(s, 0.0f);
    }

    void setRadius(irr::f32 r)
    {
        radius =
            std::max(r, 0.0f);
    }

    void setStrength(irr::f32 s)
    {
        if (extractCB)
            extractCB->strength =
                std::max(s, 0.0f);
    }

    void setLimitExposure(bool b)
    {
        if (combineCB)
            combineCB->limitExposure =
                b ? 1.0f : 0.0f;
    }

    void setOpacity(irr::f32 o)
    {
        if (combineCB)
            combineCB->opacity =
                std::max(o, 0.0f);
    }

    irr::f32 getRadius() const
    {
        return radius;
    }

    // -------------------------------------------------------------------------
    // Process:
    //
    // 1. Extract bright areas.
    // 2. Blur with N separable Gaussian passes.
    // 3. Combine with original.
    // -------------------------------------------------------------------------
    irr::video::ITexture* process(
        irr::video::ITexture* input,
        int blurPasses)
    {
        using namespace irr::video;

        if (!valid || !input)
            return input;

        if (blurPasses < 1)
            blurPasses = 1;

        // -------------------------------------------------------------
        // Extract
        // -------------------------------------------------------------
        drawExtract(
            input,
            rttA);

        ITexture* src = rttA;
        ITexture* dst = rttB;

        // -------------------------------------------------------------
        // Gaussian blur
        //
        // sigmaFinal^2 = N * sigmaPass^2
        //
        // => sigmaPass = sigmaFinal / sqrt(N)
        // -------------------------------------------------------------
        const irr::f32 sigmaPass =
            radius /
            std::sqrt(
                (irr::f32)blurPasses);

        blurCB->setSigma(
            sigmaPass);

        for (int i = 0;
             i < blurPasses;
             ++i)
        {
            // Horizontal.
            blurCB->configureDirection(
                1.0f,
                0.0f);

            drawBlur(
                src,
                dst);

            // Vertical.
            blurCB->configureDirection(
                0.0f,
                1.0f);

            drawBlur(
                dst,
                src);
        }

        // -------------------------------------------------------------
        // Combine
        // -------------------------------------------------------------
        drawCombine(
            input,
            src,
            rttOut);

        return rttOut;
    }

private:
    void setIdentityTransforms()
    {
        using namespace irr::core;

        matrix4 I;
        I.makeIdentity();

        driver->setTransform(
            irr::video::ETS_WORLD,
            I);

        driver->setTransform(
            irr::video::ETS_VIEW,
            I);

        driver->setTransform(
            irr::video::ETS_PROJECTION,
            I);
    }

    void setBloomViewport()
    {
        driver->setViewPort(
            irr::core::rect<irr::s32>(
                0,
                0,
                (irr::s32)width,
                (irr::s32)height));
    }

    void drawExtract(
        irr::video::ITexture* src,
        irr::video::ITexture* dst)
    {
        using namespace irr;
        using namespace irr::video;

        driver->setRenderTarget(
            dst,
            true,
            false,
            SColor(
                255,
                0,
                0,
                0));

        setBloomViewport();

        extractMat.setTexture(
            0,
            src);

        extractMat.setTexture(
            1,
            0);

        driver->setMaterial(
            extractMat);

        setIdentityTransforms();

        driver->drawIndexedTriangleList(
            verts,
            4,
            idx,
            2);
    }

    void drawBlur(
        irr::video::ITexture* src,
        irr::video::ITexture* dst)
    {
        using namespace irr;
        using namespace irr::video;

        driver->setRenderTarget(
            dst,
            true,
            false,
            SColor(
                255,
                0,
                0,
                0));

        setBloomViewport();

        blurMat.setTexture(
            0,
            src);

        blurMat.setTexture(
            1,
            0);

        driver->setMaterial(
            blurMat);

        setIdentityTransforms();

        driver->drawIndexedTriangleList(
            verts,
            4,
            idx,
            2);
    }

    void drawCombine(
        irr::video::ITexture* orig,
        irr::video::ITexture* bloomTex,
        irr::video::ITexture* dst)
    {
        using namespace irr;
        using namespace irr::video;

        driver->setRenderTarget(
            dst,
            true,
            false,
            SColor(
                255,
                0,
                0,
                0));

        setBloomViewport();

        combineMat.setTexture(
            0,
            orig);

        combineMat.setTexture(
            1,
            bloomTex);

        driver->setMaterial(
            combineMat);

        setIdentityTransforms();

        driver->drawIndexedTriangleList(
            verts,
            4,
            idx,
            2);
    }

private:
    irr::video::IVideoDriver* driver;

    irr::u32 width;
    irr::u32 height;
    irr::f32 split;

    irr::video::ECOLOR_FORMAT colorFormat;

    irr::video::ITexture* rttA;
    irr::video::ITexture* rttB;
    irr::video::ITexture* rttOut;

    irr::s32 extractMatType;
    irr::s32 blurMatType;
    irr::s32 combineMatType;

    irr::video::SMaterial extractMat;
    irr::video::SMaterial blurMat;
    irr::video::SMaterial combineMat;

    irr::video::S3DVertex verts[4];
    irr::u16 idx[6];

    ExtractCB* extractCB;
    BlurCB* blurCB;
    CombineCB* combineCB;

    irr::f32 radius;

    bool valid;

    BloomFilter(
        const BloomFilter&);

    BloomFilter& operator=(
        const BloomFilter&);
};

} // namespace bloom

#endif // BLOOM_FILTER_HPP
Last edited by Noiecity on Wed Sep 30, 2026 8:37 am, edited 4 times in total.
Irrlicht is love, Irrlicht is life, long live to Irrlicht
Noiecity
Posts: 429
Joined: Wed Aug 23, 2023 7:22 pm
Contact:

Re: Realistic perspective – Pixel art – Bloom effect

Post by Noiecity »

If you’re using GCC (for example, in CodeBlocks), you’ll need to compile with d3d9. You can enable d3d9 by adding it to the #defines (right-click on the project, select ‘Build Options’ in CodeBlocks). In my case, I use ‘Win32 – Release – fast math – DLL’ for compilation; add ‘IRR_COMPILE_WITH_DX9_DEV_PACK’ at the end.

Image

Also, if you are compiling for 32-bit, you need to install and use the compiler included in the 32-bit version of CodeBlocks.

Before compiling Irrlicht 1.9.0, make sure you include the necessary libraries (which are included in the version of CodeBlocks that comes with MinGW).

(I’m not sure if all of these are necessary, but I’ve included them just in case)
Image

(I think you only need:
-ld3dx9
-ldinput8
-ldxguid
As these come with MinGW, but in the latest version of CodeBlocks I think it works differently now, even though the same commands are run in GCC).

Also, if you want to run Shader Model 3.0 properly, I’d recommend that you have the latest version of DirectX 9 installed on the computer running DirectX 9.

DirectX End-User Runtimes (June 2010),directx_Jun2010_redist.exe(~95.6 MB):
https://www.microsoft.com/en-us/downloa ... px?id=8109
Irrlicht is love, Irrlicht is life, long live to Irrlicht
Post Reply