local v=require("\102\102\105")local M=require("\114\097\121\108\105\098")local O=1920 local U=1080 local J=3 local W=1.0 local K={{2.0;"\080\101\114\102\111\114\109\097\110\099\101"},{1.7;"\066\097\108\097\110\099\101\100"},{1.5;"\081\117\097\108\105\116\121"},{1.3;"\085\108\116\114\097 \081\117\097\108\105\116\121"},{1.0;"\067\117\115\116\111\109 \040\078\097\116\105\118\101\041"}}local F=true local z=K[J][1]local s=15 local m=M.Vector2(O,U)local i=O/z local A=U/z local Z=M.Vector2(i,A)M.SetConfigFlags(M.FLAG_FULLSCREEN_MODE)M.InitWindow(O,U,"\114\097\121\108\117\097 \091\115\104\097\100\101\114\115\093 \101\120\097\109\112\108\101 - \065\077\068 \070\083\082")local h=M.LoadRenderTexture(i,A)local f=M.LoadRenderTexture(O,U)local V=M.LoadShader(nil,"\114\101\115\111\117\114\099\101\115\047\102\115\114\047\102\115\114\069\097\115\117\046\102\114\097\103")local r=M.GetShaderLocation(V,"\115\114\099\083\105\122\101")local N=M.GetShaderLocation(V,"\100\115\116\083\105\122\101")M.SetShaderValue(V,r,Z,M.SHADER_UNIFORM_VEC2)M.SetShaderValue(V,N,m,M.SHADER_UNIFORM_VEC2)local X=M.LoadShader(nil,"\114\101\115\111\117\114\099\101\115\047\102\115\114\047\102\115\114\082\099\097\115\046\102\114\097\103")local b=M.GetShaderLocation(X,"\115\104\097\114\112\110\101\115\115")local p=M.GetShaderLocation(X,"\100\115\116\083\105\122\101")local e=v.new("\102\108\111\097\116\091\049\093",W)M.SetShaderValue(X,b,e,M.SHADER_UNIFORM_FLOAT)M.SetShaderValue(X,p,m,M.SHADER_UNIFORM_VEC2)local L=M.Camera3D(M.Vector3(30,20,30),M.Vector3(0,0,0),M.Vector3(0,1,0),70,M.CAMERA_PERSPECTIVE)local H=false local c=false local E=false local o=.0 local G=false while not M.WindowShouldClose()do if not E then o=M.GetTime()end local v=(2.0+math.sin(o))*.7 local W=o*.3 L.position.x=math.cos(W)*40.0 L.position.z=math.sin(W)*40.0 if F then M.BeginTextureMode(h)else M.BeginDrawing()end M.BeginMode3D(L)M.ClearBackground(M.RAYWHITE)M.DrawGrid(10,5.0)for O=0,s-1,1 do for U=0,s-1,1 do for J=0,s-1,1 do local W=((O+U)+J)/30 local K=math.sin(W*20.0+o*4.0)local F=M.Vector3((O-s/2)*(v*3.0)+K,(U-s/2)*(v*2.0)+K,(J-s/2)*(v*3.0)+K)local z=M.ColorFromHSV((((O+U)+J)*18)%360,.75,.9)local m=(2.4-v)*W M.DrawCube(F,m,m,m,z)end end end M.EndMode3D()if F then M.EndTextureMode()M.BeginTextureMode(f)M.ClearBackground(M.RAYWHITE)if H then M.BeginShaderMode(V)end M.DrawTextureEx(h.texture,m,180,z,M.WHITE)if H then M.EndShaderMode()end M.EndTextureMode()M.BeginDrawing()M.ClearBackground(M.WHITE)if c then M.BeginShaderMode(X)end M.DrawTextureEx(f.texture,m,180,1.0,M.WHITE)if c then M.EndShaderMode()end end M.DrawFPS(10,10)if F then M.DrawText("\069\065\083\085",10,32,20,H and M.GREEN or M.RED)M.DrawText("\082\067\065\083",10,54,20,c and M.GREEN or M.RED)M.DrawText(string.format("\115\104\097\114\112\110\101\115\115\058 \037\046\049\102",e[0]),10,74,10,M.BLACK)M.DrawText(string.format("\080\114\101\115\101\116\058 \037\115 \040\037\046\049\102\041",K[J][2],z),10,86,10,M.BLACK)M.DrawText(string.format("\082\101\115\111\108\117\116\105\111\110\058 \037\100\120\037\100",i,A),10,98,10,M.BLACK)M.DrawText(string.format("\070\105\108\116\101\114\058 \037\115",G and"\098\105\108\105\110\101\097\114"or"\112\111\105\110\116"),10,110,10,M.BLACK)else M.DrawText("\078\065\084\073\086\069",10,32,20,M.BLUE)end M.EndDrawing()if M.IsKeyPressed(M.KEY_E)then H=not H end if M.IsKeyPressed(M.KEY_R)then c=not c end if M.IsKeyPressed(M.KEY_N)then F=not F end if M.IsKeyPressed(M.KEY_LEFT)then e[0]=math.max(.0,e[0]-.1)M.SetShaderValue(X,b,e,M.SHADER_UNIFORM_FLOAT)end if M.IsKeyPressed(M.KEY_RIGHT)then e[0]=math.min(2.0,e[0]+.1)M.SetShaderValue(X,b,e,M.SHADER_UNIFORM_FLOAT)end if M.IsKeyPressed(M.KEY_F)then G=not G M.SetTextureFilter(h.texture,G and M.TEXTURE_FILTER_BILINEAR or M.TEXTURE_FILTER_POINT)end local r=false if M.IsKeyPressed(M.KEY_UP)then r=true J=J+1 if J==#K+1 then J=1 end end if M.IsKeyPressed(M.KEY_DOWN)then r=true J=J-1 if J==0 then J=#K end end if M.IsKeyPressed(M.KEY_S)then E=not E end if M.IsKeyPressed(M.KEY_F11)then M.ToggleFullscreen()end if r then z=K[J][1]i=O/z A=U/z Z=M.Vector2(i,A)M.UnloadRenderTexture(h)h=M.LoadRenderTexture(i,A)M.SetTextureFilter(h.texture,G and M.TEXTURE_FILTER_BILINEAR or M.TEXTURE_FILTER_POINT)end end M.UnloadRenderTexture(h)M.UnloadRenderTexture(f)M.CloseWindow()
local ffi = require("ffi")
local rl = require("raylib")

local screenWidth = 1920
local screenHeight = 1080

local preset = 3
local default_sharpness = 1.0

local presets = {
  { 2.0, "Performance" },
  { 1.7, "Balanced" },
  { 1.5, "Quality" },
  { 1.3, "Ultra Quality" },
  { 1.0, "Custom (Native)" },
}

local use_fsr = true

local ratio = presets[preset][1]

local num_blocks = 15

local screenSize = rl.Vector2(screenWidth, screenHeight)

local fbWidth = screenWidth / ratio
local fbHeight = screenHeight / ratio

local fbSize = rl.Vector2(fbWidth, fbHeight)

rl.SetConfigFlags(rl.FLAG_FULLSCREEN_MODE)
rl.InitWindow(screenWidth, screenHeight, "raylua [shaders] example - AMD FSR")

local render_texture = rl.LoadRenderTexture(fbWidth, fbHeight)

local dest_fb = rl.LoadRenderTexture(screenWidth, screenHeight)

local easu_shader = rl.LoadShader(nil, "resources/fsr/fsrEasu.frag")

local srcSize_loc = rl.GetShaderLocation(easu_shader, "srcSize")
local dstSize_loc = rl.GetShaderLocation(easu_shader, "dstSize")

rl.SetShaderValue(easu_shader, srcSize_loc, fbSize, rl.SHADER_UNIFORM_VEC2)
rl.SetShaderValue(easu_shader, dstSize_loc, screenSize, rl.SHADER_UNIFORM_VEC2)

local rcas_shader = rl.LoadShader(nil, "resources/fsr/fsrRcas.frag")

local sharpness_loc = rl.GetShaderLocation(rcas_shader, "sharpness")
local size_loc = rl.GetShaderLocation(rcas_shader, "dstSize")

local sharpness = ffi.new("float[1]", default_sharpness)

rl.SetShaderValue(rcas_shader, sharpness_loc, sharpness, rl.SHADER_UNIFORM_FLOAT)
rl.SetShaderValue(rcas_shader, size_loc, screenSize, rl.SHADER_UNIFORM_VEC2)

local camera = rl.Camera3D(rl.Vector3(30, 20, 30), rl.Vector3(0, 0, 0), rl.Vector3(0, 1, 0), 70, rl.CAMERA_PERSPECTIVE)

local enable_easu = false
local enable_rcas = false

local stop_animation = false
local t = 0.0

local bilinear = false

while not rl.WindowShouldClose() do
  if not stop_animation then
    t = rl.GetTime()
  end

  local scale = (2.0 + math.sin(t)) * 0.7
  local camera_time = t * 0.3

  camera.position.x = math.cos(camera_time) * 40.0
  camera.position.z = math.sin(camera_time) * 40.0

  if use_fsr then
    rl.BeginTextureMode(render_texture)
  else
    rl.BeginDrawing()
  end

  rl.BeginMode3D(camera)
  rl.ClearBackground(rl.RAYWHITE)

  rl.DrawGrid(10, 5.0)

  for x = 0, num_blocks - 1 do
    for y = 0, num_blocks - 1 do
      for z = 0, num_blocks - 1 do
        local block_scale = (x + y + z) / 30
        local scatter = math.sin(block_scale * 20.0 + t * 4.0)

        local cube_pos = rl.Vector3(
          (x - num_blocks / 2) * (scale * 3.0) + scatter,
          (y - num_blocks / 2) * (scale * 2.0) + scatter,
          (z - num_blocks / 2) * (scale * 3.0) + scatter
        )

        local cube_color = rl.ColorFromHSV((((x + y + z) * 18) % 360), 0.75, 0.9)

        local cube_size = (2.4 - scale) * block_scale

        rl.DrawCube(cube_pos, cube_size, cube_size, cube_size, cube_color)
      end
    end
  end

  rl.EndMode3D()

  if use_fsr then
    rl.EndTextureMode()

    rl.BeginTextureMode(dest_fb)
    rl.ClearBackground(rl.RAYWHITE)

    if enable_easu then
      rl.BeginShaderMode(easu_shader)
    end
    rl.DrawTextureEx(render_texture.texture, screenSize, 180, ratio, rl.WHITE)
    if enable_easu then
      rl.EndShaderMode()
    end

    rl.EndTextureMode()

    rl.BeginDrawing()
    rl.ClearBackground(rl.WHITE)

    if enable_rcas then
      rl.BeginShaderMode(rcas_shader)
    end
    rl.DrawTextureEx(dest_fb.texture, screenSize, 180, 1.0, rl.WHITE)
    if enable_rcas then
      rl.EndShaderMode()
    end
  end

  rl.DrawFPS(10, 10)

  if use_fsr then
    rl.DrawText("EASU", 10, 32, 20, enable_easu and rl.GREEN or rl.RED)
    rl.DrawText("RCAS", 10, 54, 20, enable_rcas and rl.GREEN or rl.RED)
    rl.DrawText(string.format("sharpness: %.1f", sharpness[0]), 10, 74, 10, rl.BLACK)
    rl.DrawText(string.format("Preset: %s (%.1f)", presets[preset][2], ratio), 10, 86, 10, rl.BLACK)
    rl.DrawText(string.format("Resolution: %dx%d", fbWidth, fbHeight), 10, 98, 10, rl.BLACK)
    rl.DrawText(string.format("Filter: %s", bilinear and "bilinear" or "point"), 10, 110, 10, rl.BLACK)
  else
    rl.DrawText("NATIVE", 10, 32, 20, rl.BLUE)
  end

  rl.EndDrawing()

  if rl.IsKeyPressed(rl.KEY_E) then
    enable_easu = not enable_easu
  end

  if rl.IsKeyPressed(rl.KEY_R) then
    enable_rcas = not enable_rcas
  end

  if rl.IsKeyPressed(rl.KEY_N) then
    use_fsr = not use_fsr
  end

  if rl.IsKeyPressed(rl.KEY_LEFT) then
    sharpness[0] = math.max(0.0, sharpness[0] - 0.1)
    rl.SetShaderValue(rcas_shader, sharpness_loc, sharpness, rl.SHADER_UNIFORM_FLOAT)
  end

  if rl.IsKeyPressed(rl.KEY_RIGHT) then
    sharpness[0] = math.min(2.0, sharpness[0] + 0.1)
    rl.SetShaderValue(rcas_shader, sharpness_loc, sharpness, rl.SHADER_UNIFORM_FLOAT)
  end

  if rl.IsKeyPressed(rl.KEY_F) then
    bilinear = not bilinear

    rl.SetTextureFilter(render_texture.texture, bilinear and rl.TEXTURE_FILTER_BILINEAR or rl.TEXTURE_FILTER_POINT)
  end

  local preset_changed = false
  if rl.IsKeyPressed(rl.KEY_UP) then
    preset_changed = true
    preset = preset + 1
    if preset == #presets + 1 then
      preset = 1
    end
  end

  if rl.IsKeyPressed(rl.KEY_DOWN) then
    preset_changed = true
    preset = preset - 1
    if preset == 0 then
      preset = #presets
    end
  end

  if rl.IsKeyPressed(rl.KEY_S) then
    stop_animation = not stop_animation
  end

  if rl.IsKeyPressed(rl.KEY_F11) then
    rl.ToggleFullscreen()
  end

  if preset_changed then
    ratio = presets[preset][1]
    fbWidth = screenWidth / ratio
    fbHeight = screenHeight / ratio

    fbSize = rl.Vector2(fbWidth, fbHeight)

    rl.UnloadRenderTexture(render_texture)

    render_texture = rl.LoadRenderTexture(fbWidth, fbHeight)

    rl.SetTextureFilter(render_texture.texture, bilinear and rl.TEXTURE_FILTER_BILINEAR or rl.TEXTURE_FILTER_POINT)
  end
end

rl.UnloadRenderTexture(render_texture)
rl.UnloadRenderTexture(dest_fb)
rl.CloseWindow()
