[solved] Can you post your FPS result please ?

For other topics related to the FreeBASIC project or its community.
paul doe
Posts: 919
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Can you post your FPS result please ?

Postby paul doe » Apr 04, 2018 15:38

srvaldez wrote:perhaps if you uncomment those lines?

Nope. I use glActiveTexture in my own implementation and it doesn't give me any trouble. I'll update the drivers and see what happens.
D.J.Peters
Posts: 7810
Joined: May 28, 2005 3:28

Re: [solved] Can you post your FPS result please ?

Postby D.J.Peters » Apr 04, 2018 20:59

The code is two years old but I updated glScreen in the past.

It's more stable of all kinds of full screen modes.

On two of my four PC's i have to load "glActiveTexture" from OpenGL lib manually.

By the way shadertoy.com used for some new shaders webgl2 today.
How ever in new shader code you have to replace iTime with iGlobalTime.

Some of you asked in the past how to use shadertoy shaders with textures.

Here are an example how to use FreeBASIC images as Textures for the shader slots iChannel0 ...3
Of course with fbimage you can use the original png textures from shadertoy easily.

Note:
shadertoy used in the web browser WebGL it's OpenGL ES on PC we use OpenGL
if you try shaders with textures you have to replace "texture" with "texture2D" in the shader code.

Joshy

Code: Select all

dim as string SCODE

/'
 The MIT License
 Copyright © 2015 Inigo Quilez
 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,
 subject 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.
'/


SCODE &= !"// How to do cubemapping when you don't have access to texture() or when you want to do it in the CPU.\n"
SCODE &= !"// This is a very cheap version without seam filtering or anything.\n"
SCODE &= !"// The point of this rutine was to do be super cheap and do the indexing without branches/conditionals.\n"

SCODE &= !"vec3 cubemap( sampler2D sam, in vec3 d ) {\n"
SCODE &= !"  vec3 n = abs(d);\n"
SCODE &= !"  vec2 uv = (n.x>n.y && n.x>n.z) ? d.yz/d.x: \n"
SCODE &= !"            (n.y>n.x && n.y>n.z) ? d.zx/d.y:\n"
SCODE &= !"                                   d.xy/d.z;\n"
SCODE &= !"  return texture2D( sam, uv ).xyz;\n"
SCODE &= !"}\n"

SCODE &= !"void mainImage( out vec4 fragColor, in vec2 fragCoord ) {\n"
SCODE &= !"  vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) / iResolution.y;\n"

SCODE &= !"  // camera movement  \n"
SCODE &= !"  float an = 0.2*iGlobalTime;\n"
SCODE &= !"  vec3 ro = vec3( 2.5*sin(an), 1.0, 2.5*cos(an) );\n"
SCODE &= !"  vec3 ta = vec3( 0.0, 1.0, 0.0 );\n"

SCODE &= !"  // camera matrix\n"
SCODE &= !"  vec3 ww = normalize( ta - ro );\n"
SCODE &= !"  vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );\n"
SCODE &= !"  vec3 vv = normalize( cross(uu,ww));\n"

SCODE &= !"  // create view ray\n"
SCODE &= !"  vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );\n"

SCODE &= !"  // sphere center  \n"
SCODE &= !"  vec3 sc = vec3(0.0,1.0,0.0);\n"
SCODE &= !"  vec3 col = vec3(0.0);\n"
   
SCODE &= !"  // raytrace-plane\n"
SCODE &= !"  float h = (0.0-ro.y)/rd.y;\n"
SCODE &= !"  if( h>0.0 ) { \n"
SCODE &= !"    vec3 pos = ro + h*rd;\n"
SCODE &= !"    vec3 nor = vec3(0.0,1.0,0.0); \n"
SCODE &= !"    vec3 di = sc - pos;\n"
SCODE &= !"    float l = length(di);\n"
SCODE &= !"    float occ = 1.0 - dot(nor,di/l)*1.0*1.0/(l*l);\n"
SCODE &= !"    col = texture2D( iChannel0, 0.5*pos.xz ).xyz;\n"
SCODE &= !"    col *= occ;\n"
SCODE &= !"    col *= exp(-0.1*h);\n"
SCODE &= !"  }\n"

SCODE &= !"  // raytrace-sphere\n"
SCODE &= !"  vec3  ce = ro - sc;\n"
SCODE &= !"  float b = dot( rd, ce );\n"
SCODE &= !"  float c = dot( ce, ce ) - 1.0; // -radius*radius\n"
SCODE &= !"  h = b*b - c;\n"
SCODE &= !"  if( h>0.0 ) {\n"
SCODE &= !"    h = -b - sqrt(h);\n"
SCODE &= !"    vec3 pos = ro + h*rd;\n"
SCODE &= !"    vec3 nor = normalize(ro+h*rd-sc); \n"
SCODE &= !"    float occ = 0.5 + 0.5*nor.y;\n"
SCODE &= !"    col = cubemap( iChannel0, nor );\n"
SCODE &= !"    col *= occ;\n"
SCODE &= !"  }\n"
SCODE &= !"  col = sqrt( col );\n"
SCODE &= !"  fragColor = vec4( col, 1.0 );\n"
SCODE &= !"}\n"

#lang "fb"
#include once "fbgfx.bi"
#include once "GL/gl.bi"
#include once "GL/glext.bi"

#ifndef NULL
#define NULL cptr(any ptr,0)
#endif

using fb

type vec3
  as GLfloat x,y,z
end type

type vec4
  as GLfloat x,y,z,w
end type


sub ErrorExit(msg as string)
  if screenptr() then screen 0
  dim as integer w,h
  screeninfo w,h : w*=0.25:h*=0.25
  screenres w,h
  print msg
  print "press any key to quit ..."
  beep : sleep : end 1
end sub

' define OpenGL proc's
#define glDefine(n) dim shared as PFN##n##PROC n

#ifndef glActiveTexture
glDefine(glActiveTexture)
#endif

' shader
glDefine(glCreateShader)
glDefine(glDeleteShader)
glDefine(glShaderSource)
glDefine(glCompileShader)
glDefine(glGetShaderiv)
glDefine(glGetShaderInfoLog)
' program
glDefine(glCreateProgram)
glDefine(glDeleteProgram)
glDefine(glAttachShader)
glDefine(glDetachShader)
glDefine(glLinkProgram)
glDefine(glGetProgramiv)
glDefine(glGetProgramInfoLog)
glDefine(glUseProgram)

' uniform
glDefine(glGetUniformLocation)
glDefine(glUniform1i)

glDefine(glUniform1f)

glDefine(glUniform3f)
glDefine(glUniform3fv)

glDefine(glUniform4f)
glDefine(glUniform4fv)

#undef glDefine

sub glScreen(pixelWidth as integer=-1, pixelHeight as integer=-1, colorBits as integer=-1, depthBits as integer=-1, fullScreen as boolean=false)
  dim as integer w,h,b 
  if ScreenPtr() then screen 0
  screeninfo w,h,b ': h=w/16*9
 
  if pixelWidth<=0 andalso pixelHeight<=0 then
    ' original shadertoy 16:9 window resolution
    pixelWidth=640 : pixelHeight=350 : fullScreen=false
  elseif pixelWidth>0 andalso pixelHeight<=0 then
    pixelHeight=pixelWidth/16*9
  elseif pixelWidth<=0 andalso pixelHeight>0 then
    pixelWidth=pixelHeight/9*16
  end if
 
  if colorBits<16 then
    colorBits=b
  elseif colorBits<24 then
    colorBits=16 
  elseif colorBits<32 then
    colorBits=24
  else
    colorBits=32
  end if
 
  if depthBits<=0 then
    depthBits=0
  elseif depthBits<16 then
      depthBits=16
  elseif depthBits<32 then
    depthBits=24
  else 
    depthBits=32
  end if
  ScreenControl FB.SET_GL_DEPTH_BITS,depthBits
 
  if fullScreen then
    ' fullscreen doesn't work with low resolutions like 320x200 ...
    if pixelWidth<640 or pixelHeight<480 then
      pixelWidth=640 : pixelHeight=480
    end if
    ' try size and color from user at first
    dim as long foundMode,mode=ScreenList(colorBits)
    ' users colorBits are supported get nearest mode
    if mode then
      dim as integer errorValue=2^30,errorWidth,errorHeight
      While (mode <> 0)
        errorWidth =abs(HiWord(mode)-pixelWidth)
        errorHeight=abs(LoWord(mode)-pixelHeight)
        if (errorWidth+errorHeight)<errorValue then
          errorValue=errorWidth+errorHeight
          foundMode=mode
        end if 
        Mode = ScreenList()
      Wend
    else 
      ' users colorBits not supported try other bits also
      dim as long Bits(1)
      if colorBits=16 then
        ' try 32 bits at first
        bits(0)=32:bits(1)=24
      elseif colorBits=24 then 
        ' try 32 bits at first
        bits(0)=32:bits(1)=16
      else ' 32
        ' try 24 bits at first
        Bits(0)=24:Bits(1)=16
      end if
      for i as integer=0 to 1
        colorBits=Bits(i)
        mode=ScreenList(colorBits)
        if mode then
          dim as integer errorValue=2^30,errorWidth,errorHeight
          While (mode <> 0)
            errorWidth =abs(HiWord(mode)-pixelWidth)
            errorHeight=abs(LoWord(mode)-pixelHeight)
            if (errorWidth+errorHeight)<errorValue then
              errorValue=errorWidth+errorHeight
              foundMode=mode
            end if 
            Mode = ScreenList()
          Wend
        end if
        if foundMode then exit for
      next 
    end if
   
    if foundMode then
      pixelWidth =HiWord(foundMode)
      pixelHeight=LoWord(foundMode)
    else
      fullScreen=false
    end if
  end if
 
  if ScreenRes(pixelWidth,pixelHeight,colorBits,,FB.GFX_OPENGL or iif(fullScreen<>0,GFX_FULLSCREEN,0)) then
    ' fallback mode
    fullScreen=false
    if ScreenRes(640,350,b,,FB.GFX_OPENGL) then
      ErrorExit("screenres(" & w & "," & h &") failed !")
    end if 
  end if
  flip
 
  ' get OpenGL proc's (abort if something goes wrong)
  #define glProc(n) n = ScreenGLProc(#n) : if (n = NULL) then ErrorExit(#n)


  if glActiveTexture=0 then
  glProc(glActiveTexture)
  end if

  ' shader
  glProc(glCreateShader)
  glProc(glDeleteShader)
  glProc(glShaderSource)
  glProc(glCompileShader)
  glProc(glGetShaderiv)
  glProc(glGetShaderInfoLog)
  ' program
  glProc(glCreateProgram)
  glProc(glDeleteProgram)
  glProc(glAttachShader)
  glProc(glDetachShader)
  glProc(glLinkProgram)
  glProc(glGetProgramiv)
  glProc(glGetProgramInfoLog)
  glProc(glUseProgram)
  ' uniform
  glProc(glGetUniformLocation)
  glProc(glUniform1i)
 
  glProc(glUniform1f)
 
  glProc(glUniform3f)
  glProc(glUniform3fv)
 
  glProc(glUniform4f)
  glProc(glUniform4fv)

  #undef glProc
end sub

type ShaderToy
  declare destructor
  declare function CompileFile(Filename as string) as boolean
  declare function CompileCode(Code as string) as boolean
  as GLuint FragmentShader
  as GLuint ProgramObject
  as string Shaderlog
end type
destructor ShaderToy
  if ProgramObject then
   glUseprogram(0)
   if FragmentShader  then
     glDetachShader(ProgramObject,FragmentShader)
     glDeleteShader(FragmentShader)
   end if
   glDeleteProgram(ProgramObject)
  end if
end destructor

function ShaderToy.CompileFile(filename as string) as boolean
  dim as string code
  var hFile = FreeFile()
  if open(filename,for input, as #hFile) then
    ShaderLog = "can't read shader: " & chr(34) & filename  & chr(34) & " !"
    return false
  end if
  while not eof(hFile)
    dim as string aLine
    line input #hFile,aLine
    code &= aLine & !"\n"
  wend
  close #hFile
  return CompileCode(code)
end function

function ShaderToy.CompileCode(UserCode as string) as boolean
  dim as GLint logSize
  dim as GLint status
  dim as string FragmentProlog
  FragmentProlog & =!"#version 120\n"
 
  FragmentProlog & =!"uniform float     iGlobalTime;\n" ' shader playback time (in seconds)
  FragmentProlog & =!"uniform vec3      iResolution;\n" ' viewport resolution (in pixels)
  FragmentProlog & =!"uniform vec4      iMouse;\n"      ' mouse pixel coords. xy: current (if MLB down), zw: click
  'FragmentProlog & =!"uniform vec4      iDate;\n"       ' (year, month, day, time in seconds)
 
  FragmentProlog & =!"uniform sampler2D iChannel0;\n"
  FragmentProlog & =!"uniform sampler2D iChannel1;\n"
  FragmentProlog & =!"uniform sampler2D iChannel2;\n"
  FragmentProlog & =!"uniform sampler2D iChannel3;\n"
  FragmentProlog & =!"uniform samplerCube iCube;\n"
 
  dim as string FragmentEpilog
  FragmentEpilog &= !"void main() {\n"
  FragmentEpilog &= !"  vec4 color;\n"
  FragmentEpilog &= !"  // call user shader\n"
  FragmentEpilog &= !"  mainImage(color, gl_FragCoord.xy);\n"
  FragmentEpilog &= !"  color.w = 1.0;\n"
  FragmentEpilog &= !"  gl_FragColor = color;\n"
  FragmentEpilog &= !"}\n"

  dim as string FragmentCode = FragmentProlog & UserCode & FragmentEpilog

  FragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
  if FragmentShader=0 then
    ShaderLog = "glCreateShader(GL_FRAGMENT_SHADER) failed !"
    return false
  end if
  dim as GLchar ptr pCode=strptr(FragmentCode)
  glShaderSource (FragmentShader, 1, @pCode, NULL)
  glCompileShader(FragmentShader)
  glGetShaderiv  (FragmentShader, GL_COMPILE_STATUS, @status)
  if status = GL_FALSE then
    glGetShaderiv(FragmentShader, GL_INFO_LOG_LENGTH, @logSize)
    ShaderLog = space(logSize)
    glGetShaderInfoLog(FragmentShader, logSize, NULL, cptr(GLchar ptr,strptr(ShaderLog)) )
    ShaderLog = !"glCompileShader(FragmentShader) failed !\n" & Shaderlog
    glDeleteShader(FragmentShader) : FragmentShader = 0
    return false
  end if

  ProgramObject = glCreateProgram()
  if ProgramObject=0 then
    ShaderLog = "glCreateProgram() failed !"
    glDeleteShader(FragmentShader) : FragmentShader = 0
    return false
  end if
  glAttachShader(ProgramObject,FragmentShader)
  glLinkProgram (ProgramObject)
  glGetProgramiv(ProgramObject, GL_LINK_STATUS, @status)
  if (status = GL_FALSE) then
    glGetProgramiv(ProgramObject, GL_INFO_LOG_LENGTH, @logSize)
    ShaderLog = space(logSize)
    glGetProgramInfoLog (ProgramObject, logSize, NULL, cptr(GLchar ptr,strptr(ShaderLog)) )
    ShaderLog = !"glLinkProgram() failed !\n" & Shaderlog
    glDeleteShader(FragmentShader) : FragmentShader = 0
    return false
  end if
  return true
end function


'
' main
'


' init Screenres, create the OpenGL context and load some OpenGL procs.
'glScreen([Width],[Height],[colorBits],[depthBits],[bFullscreen])

glScreen()  ' original shadertoy resolution

' glScreen(640,480,,,true) ' smallest fullscreen
' glScreen(,,,,true) ' current desktop resolution in fullscreen
' glScreen(1024) ' w = 1024 h = 16:9 window
' glScreen(4096,4096,4096,4096,true) ' !!! get the nearest working fullscreen from wrong user input !!!

' get curent resolution
dim as integer scr_w,scr_h,scr_b
screeninfo scr_w,scr_h,scr_b

dim as vec3 v3
v3.x=scr_w     ' width in pixle
v3.y=scr_h     '`height in pixle
v3.z=v3.x/v3.y ' pixel ratio

dim as ShaderToy Shader

if Shader.CompileCode(SCODE)=false then
  ErrorExit Shader.ShaderLog
end if


' create or load texture
var img = ImageCreate(128,128,0,32)
if img=NULL then ErrorExit "can't create image !"

for y as integer = 0 to 7
  for x as integer = 0 to 7
    line img,(x*16,y*16)-step(15,15),iif((x+y) mod 2=0,&HFFFFFF,0),BF
  next
next

draw string img,(0,0),"FreeBASIC",rgb(255,0,0)
draw string img,(24,24),"Shadertoy.com",rgb(0,255,0)

dim as integer tw,th
dim as any ptr pixels

dim as GLint Texture0
glGenTextures(1,@Texture0)

if imageinfo(img,tw,th,,,pixels) then
  ErrorExit "can't get imageinfo image !"
end if


glBindTexture(GL_TEXTURE_2D,Texture0)

' for the cheker board I enable mipmaping
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP   , GL_TRUE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
 
'glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST)
'glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST)
 
'glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR)
'glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR)

' copy the pixels from FBImage to the GPU
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,tw,th,0,GL_BGRA,GL_UNSIGNED_BYTE,pixels)
glBindTexture(GL_TEXTURE_2D,0)

' at this point the pixels from image are on the GPU memory
' so we can delete the image from CPU memory
Imagedestroy img : img=NULL

' enable shader
glUseProgram(Shader.ProgramObject)

' get uniforms locations in shader program
var iGlobalTime = glGetUniformLocation(Shader.ProgramObject,"iGlobalTime")
var iResolution = glGetUniformLocation(Shader.ProgramObject,"iResolution")
var iMouse      = glGetUniformLocation(Shader.ProgramObject,"iMouse")

' we have 4 slots for 2D textures and / or cubemaps
var iChannel0   = glGetUniformLocation(Shader.ProgramObject, "iChannel0")
var iChannel1   = glGetUniformLocation(Shader.ProgramObject, "iChannel1")
var iChannel2   = glGetUniformLocation(Shader.ProgramObject, "iChannel2")
var iChannel3   = glGetUniformLocation(Shader.ProgramObject, "iChannel3")

' for testing I use only one texture in iChannel0
glUniform1i(iChannel0,0)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, Texture0)

' if you use more than one texture

'glUniform1i(iChannel1,1)
'glActiveTexture(GL_TEXTURE0)
'glBindTexture(GL_TEXTURE_2D, Texture1)

'glUniform1i(iChannel2,2)
'glActiveTexture(GL_TEXTURE0)
'glBindTexture(GL_TEXTURE_2D, Texture2)

'glUniform1i(iChannel3,3)
'glActiveTexture(GL_TEXTURE0)
'glBindTexture(GL_TEXTURE_2D, Texture3)


dim as integer ox,oy,ob,mx,my,mb,frames,fps
dim as double tRuntime,tStart = Timer()
dim as double tNow,tLast = tStart

' set vec3 iResolution
glUniform3fv(iResolution,1,@v3.x)

' set uniform float iGlobalTime = 0
glUniform1f(iGlobalTime,tRuntime)

while inkey()<>chr(27)
  ' draw a rectangle (2 triangles over the whole screen)
  ' this trigers the shader code for every pixel
  glRectf(-1,-1,1,1)
  ' swap the buffer on screen
  flip
  ' count the frames
  frames+=1

  ' set uniform vec4 iMouse
  if getMouse(mx,my,,mb)=0 then
    ' update only if one or more properties are changed
    if mx<>ox orelse my=oy orelse mb<>ob then
      ox=mx:oy=my:ob=mb
      if mb<>1 then mb=0 ' triger only the left mouse button
      glUniform4f(iMouse,mx,scr_h-my,mb,mb)
    end if
  end if

  ' get the time
  tNow=Timer() : tRuntime=tNow-tStart
  ' update uniform float iGlobalTime
  glUniform1f(iGlobalTime,tRuntime)
 
  ' update fps (not every frame Windowtitle on Windows or Linux are slow)
  if frames mod 100=0 then
    fps=100/(tNow-tLast) : tLast=tNow
    Windowtitle "" & scr_w & " x " & scr_h & " x "  & scr_b & " fps: " & fps
  end if
wend

' disable shader
glUseProgram(0)
srvaldez
Posts: 2063
Joined: Sep 25, 2005 21:54

Re: [solved] Can you post your FPS result please ?

Postby srvaldez » Apr 05, 2018 14:55

thank you Joshy for the update :-)
srvaldez
Posts: 2063
Joined: Sep 25, 2005 21:54

Re: [solved] Can you post your FPS result please ?

Postby srvaldez » Apr 05, 2018 15:34

I do have some issues however, the following code is not accepted on my Mac

Code: Select all

  if glActiveTexture=0 then
  glProc(glActiveTexture)
  end if

Code: Select all

shadertoy.bas(259) error 1: Argument count mismatch, found '=' in 'if glActiveTexture=0 then'
shadertoy.bas(260) error 316: Function result assignment outside of the function, found '=' in 'glProc(glActiveTexture)'
shadertoy.bas(260) error 1: Argument count mismatch, found '=' in 'glProc(glActiveTexture)'
Compilation failed.

if I comment-out those lines then it compiles, however I get these warnings

Code: Select all

shadertoy.c:290:27: warning: taking the absolute value of unsigned type 'unsigned long long' has no effect [-Wabsolute-value]
                                ERRORWIDTH$3 = (int64)__builtin_llabs( ((((uint64)MODE$2 & 4294901760ull) >> (16ll & 63ll)) - (uint64)PIXELWIDTH$1) );
                                                      ^
shadertoy.c:290:27: note: remove the call to '__builtin_llabs' since unsigned values cannot be negative
                                ERRORWIDTH$3 = (int64)__builtin_llabs( ((((uint64)MODE$2 & 4294901760ull) >> (16ll & 63ll)) - (uint64)PIXELWIDTH$1) );
                                                      ^~~~~~~~~~~~~~~
shadertoy.c:291:28: warning: taking the absolute value of unsigned type 'unsigned long long' has no effect [-Wabsolute-value]
                                ERRORHEIGHT$3 = (int64)__builtin_llabs( (((uint64)MODE$2 & 65535ull) - (uint64)PIXELHEIGHT$1) );
                                                       ^
shadertoy.c:291:28: note: remove the call to '__builtin_llabs' since unsigned values cannot be negative
                                ERRORHEIGHT$3 = (int64)__builtin_llabs( (((uint64)MODE$2 & 65535ull) - (uint64)PIXELHEIGHT$1) );
                                                       ^~~~~~~~~~~~~~~
shadertoy.c:366:30: warning: taking the absolute value of unsigned type 'unsigned long long' has no effect [-Wabsolute-value]
                                                        ERRORWIDTH$6 = (int64)__builtin_llabs( ((((uint64)MODE$2 & 4294901760ull) >> (16ll & 63ll)) - (uint64)PIXELWIDTH$1) );
                                                                              ^
shadertoy.c:366:30: note: remove the call to '__builtin_llabs' since unsigned values cannot be negative
                                                        ERRORWIDTH$6 = (int64)__builtin_llabs( ((((uint64)MODE$2 & 4294901760ull) >> (16ll & 63ll)) - (uint64)PIXELWIDTH$1) );
                                                                              ^~~~~~~~~~~~~~~
shadertoy.c:367:31: warning: taking the absolute value of unsigned type 'unsigned long long' has no effect [-Wabsolute-value]
                                                        ERRORHEIGHT$6 = (int64)__builtin_llabs( (((uint64)MODE$2 & 65535ull) - (uint64)PIXELHEIGHT$1) );
                                                                               ^
shadertoy.c:367:31: note: remove the call to '__builtin_llabs' since unsigned values cannot be negative
                                                        ERRORHEIGHT$6 = (int64)__builtin_llabs( (((uint64)MODE$2 & 65535ull) - (uint64)PIXELHEIGHT$1) );
                                                                               ^~~~~~~~~~~~~~~
4 warnings generated.
Compilation finished successfully.

if I remove abs from the statements

Code: Select all

            errorWidth =abs(HiWord(mode)-pixelWidth)
            errorHeight=abs(LoWord(mode)-pixelHeight)

there are 2 instances of these, then it compiles without warning
D.J.Peters
Posts: 7810
Joined: May 28, 2005 3:28

Re: [solved] Can you post your FPS result please ?

Postby D.J.Peters » Apr 05, 2018 15:58

Hello srvaldez thank you for testing and the report.

On windows glActiveTexture is in file "GL/glext.bi"

Code: Select all

#ifdef GL_GLEXT_PROTOTYPES
declare sub glActiveTexture(byval texture as GLenum)
so it must be loaded from OpenGL lib manually.
On Linux or your MAC it's in "GL/gl.bi"

Code: Select all

declare sub glActiveTexture(byval texture as GLenum)
And are part of OpenGL lib !

I will try to fix it

But the problem with ABS() you reported must be a problem of your "private" MAC build ?

Joshy
D.J.Peters
Posts: 7810
Joined: May 28, 2005 3:28

Re: [solved] Can you post your FPS result please ?

Postby D.J.Peters » Apr 05, 2018 16:32

I hope the trouble with glActiveTexture is fixed know :-)

The trouble with ABS() is it only on your MAC port or Linux also ?

I don't get the warning's here.

Joshy

Code: Select all

dim as string SCODE
SCODE &= !"// How to do cubemapping when you don't have access to texture() or when you want to do it in the CPU.\n"
SCODE &= !"// This is a very cheap version without seam filtering or anything.\n"
SCODE &= !"// The point of this rutine was to do be super cheap and do the indexing without branches/conditionals.\n"

SCODE &= !"vec3 cubemap( sampler2D sam, in vec3 d ) {\n"
SCODE &= !"  vec3 n = abs(d);\n"
SCODE &= !"  vec2 uv = (n.x>n.y && n.x>n.z) ? d.yz/d.x: \n"
SCODE &= !"            (n.y>n.x && n.y>n.z) ? d.zx/d.y:\n"
SCODE &= !"                                   d.xy/d.z;\n"
SCODE &= !"  return texture2D( sam, uv ).xyz;\n"
SCODE &= !"}\n"

SCODE &= !"void mainImage( out vec4 fragColor, in vec2 fragCoord ) {\n"
SCODE &= !"  vec2 p = (-iResolution.xy + 2.0*fragCoord.xy) / iResolution.y;\n"

SCODE &= !"  // camera movement  \n"
SCODE &= !"  float an = 0.2*iGlobalTime;\n"
SCODE &= !"  vec3 ro = vec3( 2.5*sin(an), 1.0, 2.5*cos(an) );\n"
SCODE &= !"  vec3 ta = vec3( 0.0, 1.0, 0.0 );\n"

SCODE &= !"  // camera matrix\n"
SCODE &= !"  vec3 ww = normalize( ta - ro );\n"
SCODE &= !"  vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );\n"
SCODE &= !"  vec3 vv = normalize( cross(uu,ww));\n"

SCODE &= !"  // create view ray\n"
SCODE &= !"  vec3 rd = normalize( p.x*uu + p.y*vv + 1.5*ww );\n"

SCODE &= !"  // sphere center  \n"
SCODE &= !"  vec3 sc = vec3(0.0,1.0,0.0);\n"
SCODE &= !"  vec3 col = vec3(0.0);\n"
   
SCODE &= !"  // raytrace-plane\n"
SCODE &= !"  float h = (0.0-ro.y)/rd.y;\n"
SCODE &= !"  if( h>0.0 ) { \n"
SCODE &= !"    vec3 pos = ro + h*rd;\n"
SCODE &= !"    vec3 nor = vec3(0.0,1.0,0.0); \n"
SCODE &= !"    vec3 di = sc - pos;\n"
SCODE &= !"    float l = length(di);\n"
SCODE &= !"    float occ = 1.0 - dot(nor,di/l)*1.0*1.0/(l*l);\n"
SCODE &= !"    col = texture2D( iChannel0, 0.5*pos.xz ).xyz;\n"
SCODE &= !"    col *= occ;\n"
SCODE &= !"    col *= exp(-0.1*h);\n"
SCODE &= !"  }\n"

SCODE &= !"  // raytrace-sphere\n"
SCODE &= !"  vec3  ce = ro - sc;\n"
SCODE &= !"  float b = dot( rd, ce );\n"
SCODE &= !"  float c = dot( ce, ce ) - 1.0; // -radius*radius\n"
SCODE &= !"  h = b*b - c;\n"
SCODE &= !"  if( h>0.0 ) {\n"
SCODE &= !"    h = -b - sqrt(h);\n"
SCODE &= !"    vec3 pos = ro + h*rd;\n"
SCODE &= !"    vec3 nor = normalize(ro+h*rd-sc); \n"
SCODE &= !"    float occ = 0.5 + 0.5*nor.y;\n"
SCODE &= !"    col = cubemap( iChannel0, nor );\n"
SCODE &= !"    col *= occ;\n"
SCODE &= !"  }\n"
SCODE &= !"  col = sqrt( col );\n"
SCODE &= !"  fragColor = vec4( col, 1.0 );\n"
SCODE &= !"}\n"

#lang "fb"
#include once "fbgfx.bi"
#include once "GL/gl.bi"
#include once "GL/glext.bi"

#ifndef NULL
#define NULL cptr(any ptr,0)
#endif

#ifndef glActiveTexture
#define LOAD_GLACTIVETEXTURE
#endif

using fb

type vec3
  as GLfloat x,y,z
end type

type vec4
  as GLfloat x,y,z,w
end type


sub ErrorExit(msg as string)
  if screenptr() then screen 0
  dim as integer w,h
  screeninfo w,h : w*=0.25:h*=0.25
  screenres w,h
  print msg
  print "press any key to quit ..."
  beep : sleep : end 1
end sub

' define OpenGL proc's
#define glDefine(n) dim shared as PFN##n##PROC n

#ifdef LOAD_GLACTIVETEXTURE
glDefine(glActiveTexture)
#endif

' shader
glDefine(glCreateShader)
glDefine(glDeleteShader)
glDefine(glShaderSource)
glDefine(glCompileShader)
glDefine(glGetShaderiv)
glDefine(glGetShaderInfoLog)

' program
glDefine(glCreateProgram)
glDefine(glDeleteProgram)
glDefine(glAttachShader)
glDefine(glDetachShader)
glDefine(glLinkProgram)
glDefine(glGetProgramiv)
glDefine(glGetProgramInfoLog)
glDefine(glUseProgram)

' uniform
glDefine(glGetUniformLocation)
glDefine(glUniform1i)

glDefine(glUniform1f)

glDefine(glUniform3f)
glDefine(glUniform3fv)

glDefine(glUniform4f)
glDefine(glUniform4fv)

#undef glDefine

sub glScreen(pixelWidth as integer=-1, pixelHeight as integer=-1, colorBits as integer=-1, depthBits as integer=-1, fullScreen as boolean=false)
  dim as integer w,h,b 
  if ScreenPtr() then screen 0
  screeninfo w,h,b
 
  if pixelWidth<=0 andalso pixelHeight<=0 then
    ' original shadertoy 16:9 window resolution
    pixelWidth=640 : pixelHeight=350 : fullScreen=false
  elseif pixelWidth>0 andalso pixelHeight<=0 then
    pixelHeight=pixelWidth/16*9
  elseif pixelWidth<=0 andalso pixelHeight>0 then
    pixelWidth=pixelHeight/9*16
  end if
 
  if colorBits<16 then
    colorBits=b
  elseif colorBits<24 then
    colorBits=16 
  elseif colorBits<32 then
    colorBits=24
  else
    colorBits=32
  end if
 
  if depthBits<=0 then
    depthBits=0
  elseif depthBits<16 then
      depthBits=16
  elseif depthBits<32 then
    depthBits=24
  else 
    depthBits=32
  end if
  ScreenControl FB.SET_GL_DEPTH_BITS,depthBits
 
  if fullScreen then
    ' fullscreen doesn't work with low resolutions like 320x200 ...
    if pixelWidth<640 or pixelHeight<480 then
      pixelWidth=640 : pixelHeight=480
    end if
    ' try size and color from user at first
    dim as long foundMode,mode=ScreenList(colorBits)
    ' users colorBits are supported get nearest mode
    if mode then
      dim as integer errorValue=2^30,errorWidth,errorHeight
      While (mode <> 0)
        errorWidth =abs(HiWord(mode)-pixelWidth)
        errorHeight=abs(LoWord(mode)-pixelHeight)
        if (errorWidth+errorHeight)<errorValue then
          errorValue=errorWidth+errorHeight
          foundMode=mode
        end if 
        Mode = ScreenList()
      Wend
    else 
      ' users colorBits not supported try other bits also
      dim as long Bits(1)
      if colorBits=16 then
        ' try 32 bits at first
        bits(0)=32:bits(1)=24
      elseif colorBits=24 then 
        ' try 32 bits at first
        bits(0)=32:bits(1)=16
      else ' 32
        ' try 24 bits at first
        Bits(0)=24:Bits(1)=16
      end if
      for i as integer=0 to 1
        colorBits=Bits(i)
        mode=ScreenList(colorBits)
        if mode then
          dim as integer errorValue=2^30,errorWidth,errorHeight
          While (mode <> 0)
            errorWidth =abs(HiWord(mode)-pixelWidth)
            errorHeight=abs(LoWord(mode)-pixelHeight)
            if (errorWidth+errorHeight)<errorValue then
              errorValue=errorWidth+errorHeight
              foundMode=mode
            end if 
            Mode = ScreenList()
          Wend
        end if
        if foundMode then exit for
      next 
    end if
   
    if foundMode then
      pixelWidth =HiWord(foundMode)
      pixelHeight=LoWord(foundMode)
    else
      fullScreen=false
    end if
  end if
 
  if ScreenRes(pixelWidth,pixelHeight,colorBits,,FB.GFX_OPENGL or iif(fullScreen<>0,GFX_FULLSCREEN,0)) then
    ' fallback mode
    fullScreen=false
    if ScreenRes(640,350,b,,FB.GFX_OPENGL) then
      ErrorExit("screenres() failed !")
    end if 
  end if
  flip
 
  ' get OpenGL proc's (abort if something goes wrong)
  #define glProc(n) n = ScreenGLProc(#n) : if (n = NULL) then ErrorExit(#n)

#ifdef LOAD_GLACTIVETEXTURE
  glProc(glActiveTexture)
#endif

  ' shader
  glProc(glCreateShader)
  glProc(glDeleteShader)
  glProc(glShaderSource)
  glProc(glCompileShader)
  glProc(glGetShaderiv)
  glProc(glGetShaderInfoLog)
  ' program
  glProc(glCreateProgram)
  glProc(glDeleteProgram)
  glProc(glAttachShader)
  glProc(glDetachShader)
  glProc(glLinkProgram)
  glProc(glGetProgramiv)
  glProc(glGetProgramInfoLog)
  glProc(glUseProgram)
  ' uniform
  glProc(glGetUniformLocation)
  glProc(glUniform1i)
 
  glProc(glUniform1f)
 
  glProc(glUniform3f)
  glProc(glUniform3fv)
 
  glProc(glUniform4f)
  glProc(glUniform4fv)

  #undef glProc
end sub

type ShaderToy
  declare destructor
  declare function CompileFile(Filename as string) as boolean
  declare function CompileCode(Code as string) as boolean
  as GLuint FragmentShader
  as GLuint ProgramObject
  as string Shaderlog
end type
destructor ShaderToy
  if ProgramObject then
   glUseprogram(0)
   if FragmentShader  then
     glDetachShader(ProgramObject,FragmentShader)
     glDeleteShader(FragmentShader)
   end if
   glDeleteProgram(ProgramObject)
  end if
end destructor

function ShaderToy.CompileFile(filename as string) as boolean
  dim as string code
  var hFile = FreeFile()
  if open(filename,for input, as #hFile) then
    ShaderLog = "can't read shader: " & chr(34) & filename  & chr(34) & " !"
    return false
  end if
  while not eof(hFile)
    dim as string aLine
    line input #hFile,aLine
    code &= aLine & !"\n"
  wend
  close #hFile
  return CompileCode(code)
end function

function ShaderToy.CompileCode(UserCode as string) as boolean
  dim as GLint logSize
  dim as GLint status
  dim as string FragmentProlog
  FragmentProlog & =!"#version 120\n"
 
  FragmentProlog & =!"uniform float     iGlobalTime;\n" ' shader playback time (in seconds)
  FragmentProlog & =!"uniform vec3      iResolution;\n" ' viewport resolution (in pixels)
  FragmentProlog & =!"uniform vec4      iMouse;\n"      ' mouse pixel coords. xy: current (if MLB down), zw: click
  'FragmentProlog & =!"uniform vec4      iDate;\n"       ' (year, month, day, time in seconds)
 
  FragmentProlog & =!"uniform sampler2D iChannel0;\n"
  FragmentProlog & =!"uniform sampler2D iChannel1;\n"
  FragmentProlog & =!"uniform sampler2D iChannel2;\n"
  FragmentProlog & =!"uniform sampler2D iChannel3;\n"
  FragmentProlog & =!"uniform samplerCube iCube;\n"
 
  dim as string FragmentEpilog
  FragmentEpilog &= !"void main() {\n"
  FragmentEpilog &= !"  vec4 color;\n"
  FragmentEpilog &= !"  // call user shader\n"
  FragmentEpilog &= !"  mainImage(color, gl_FragCoord.xy);\n"
  FragmentEpilog &= !"  color.w = 1.0;\n"
  FragmentEpilog &= !"  gl_FragColor = color;\n"
  FragmentEpilog &= !"}\n"

  dim as string FragmentCode = FragmentProlog & UserCode & FragmentEpilog

  FragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
  if FragmentShader=0 then
    ShaderLog = "glCreateShader(GL_FRAGMENT_SHADER) failed !"
    return false
  end if
  dim as GLchar ptr pCode=strptr(FragmentCode)
  glShaderSource (FragmentShader, 1, @pCode, NULL)
  glCompileShader(FragmentShader)
  glGetShaderiv  (FragmentShader, GL_COMPILE_STATUS, @status)
  if status = GL_FALSE then
    glGetShaderiv(FragmentShader, GL_INFO_LOG_LENGTH, @logSize)
    ShaderLog = space(logSize)
    glGetShaderInfoLog(FragmentShader, logSize, NULL, cptr(GLchar ptr,strptr(ShaderLog)) )
    ShaderLog = !"glCompileShader(FragmentShader) failed !\n" & Shaderlog
    glDeleteShader(FragmentShader) : FragmentShader = 0
    return false
  end if

  ProgramObject = glCreateProgram()
  if ProgramObject=0 then
    ShaderLog = "glCreateProgram() failed !"
    glDeleteShader(FragmentShader) : FragmentShader = 0
    return false
  end if
  glAttachShader(ProgramObject,FragmentShader)
  glLinkProgram (ProgramObject)
  glGetProgramiv(ProgramObject, GL_LINK_STATUS, @status)
  if (status = GL_FALSE) then
    glGetProgramiv(ProgramObject, GL_INFO_LOG_LENGTH, @logSize)
    ShaderLog = space(logSize)
    glGetProgramInfoLog (ProgramObject, logSize, NULL, cptr(GLchar ptr,strptr(ShaderLog)) )
    ShaderLog = !"glLinkProgram() failed !\n" & Shaderlog
    glDeleteShader(FragmentShader) : FragmentShader = 0
    return false
  end if
  return true
end function


'
' main
'


' init Screenres, create the OpenGL context and load some OpenGL procs.
'glScreen([Width],[Height],[colorBits],[depthBits],[bFullscreen])

glScreen()  ' original shadertoy resolution

' glScreen(640,480,,,true) ' smallest fullscreen
' glScreen(,,,,true) ' current desktop resolution in fullscreen
' glScreen(1024) ' w = 1024 h = 16:9 window
' glScreen(4096,4096,4096,4096,true) ' !!! get the nearest working fullscreen from wrong user input !!!

' get curent resolution
dim as integer scr_w,scr_h,scr_b
screeninfo scr_w,scr_h,scr_b

dim as vec3 v3
v3.x=scr_w     ' width in pixle
v3.y=scr_h     '`height in pixle
v3.z=v3.x/v3.y ' pixel ratio

dim as ShaderToy Shader

if Shader.CompileCode(SCODE)=false then
  ErrorExit Shader.ShaderLog
end if


' create or load texture
var img = ImageCreate(128,128,0,32)
if img=NULL then ErrorExit "can't create image !"

for y as integer = 0 to 7
  for x as integer = 0 to 7
    line img,(x*16,y*16)-step(15,15),iif((x+y) mod 2=0,&HFFFFFF,0),BF
  next
next

draw string img,(0,0),"FreeBASIC",rgb(255,0,0)
draw string img,(24,24),"Shadertoy.com",rgb(0,255,0)

dim as integer tw,th
dim as any ptr pixels

dim as GLint Texture0
glGenTextures(1,@Texture0)

if imageinfo(img,tw,th,,,pixels) then
  ErrorExit "can't get imageinfo image !"
end if


glBindTexture(GL_TEXTURE_2D,Texture0)

' for the cheker board I enable mipmaping
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP   , GL_TRUE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
 
'glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
'glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
 
'glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
'glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

' copy the pixels from FBImage to the GPU
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,tw,th,0,GL_BGRA,GL_UNSIGNED_BYTE,pixels)
glBindTexture(GL_TEXTURE_2D,0)

' at this point the pixels from image are on the GPU memory
' so we can delete the image from CPU memory
Imagedestroy img : img=NULL

' enable shader
glUseProgram(Shader.ProgramObject)

' get uniforms locations in shader program
var iGlobalTime = glGetUniformLocation(Shader.ProgramObject,"iGlobalTime")
var iResolution = glGetUniformLocation(Shader.ProgramObject,"iResolution")
var iMouse      = glGetUniformLocation(Shader.ProgramObject,"iMouse")

' we have 4 slots for 2D textures and / or cubemaps
var iChannel0   = glGetUniformLocation(Shader.ProgramObject, "iChannel0")
var iChannel1   = glGetUniformLocation(Shader.ProgramObject, "iChannel1")
var iChannel2   = glGetUniformLocation(Shader.ProgramObject, "iChannel2")
var iChannel3   = glGetUniformLocation(Shader.ProgramObject, "iChannel3")

' for testing I use only one texture in iChannel0
glUniform1i(iChannel0,0)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, Texture0)

' if you use more than one texture

'glGenTextures(1,@Texture1)
'glGenTextures(1,@Texture2)
'glGenTextures(1,@Texture3)

'glUniform1i(iChannel1,1)
'glActiveTexture(GL_TEXTURE0)
'glBindTexture(GL_TEXTURE_2D, Texture1)

'glUniform1i(iChannel2,2)
'glActiveTexture(GL_TEXTURE0)
'glBindTexture(GL_TEXTURE_2D, Texture2)

'glUniform1i(iChannel3,3)
'glActiveTexture(GL_TEXTURE0)
'glBindTexture(GL_TEXTURE_2D, Texture3)

dim as integer ox,oy,ob,mx,my,mb,frames,fps
dim as double tRuntime,tStart = Timer()
dim as double tNow,tLast = tStart

' set vec3 iResolution
glUniform3fv(iResolution,1,@v3.x)

' set uniform float iGlobalTime = 0
glUniform1f(iGlobalTime,tRuntime)

while inkey()<>chr(27)
  ' draw a rectangle (2 triangles over the whole screen)
  ' this trigers the shader code for every pixel
  glRectf(-1,-1,1,1)
  ' swap the buffer on screen
  flip
  ' count the frames
  frames+=1

  ' set uniform vec4 iMouse
  if getMouse(mx,my,,mb)=0 then
    ' update only if one or more properties are changed
    if mx<>ox orelse my=oy orelse mb<>ob then
      ox=mx:oy=my:ob=mb
      if mb<>1 then mb=0 ' triger only the left mouse button
      glUniform4f(iMouse,mx,scr_h-my,mb,mb)
    end if
  end if

  ' get the time
  tNow=Timer() : tRuntime=tNow-tStart
  ' update uniform float iGlobalTime
  glUniform1f(iGlobalTime,tRuntime)
 
  ' update fps (not every frame Windowtitle on Windows or Linux are slow)
  if frames mod 100=0 then
    fps=100/(tNow-tLast) : tLast=tNow
    Windowtitle "" & scr_w & " x " & scr_h & " x "  & scr_b & " fps: " & fps
  end if
wend

' disable shader
glUseProgram(0)
srvaldez
Posts: 2063
Joined: Sep 25, 2005 21:54

Re: [solved] Can you post your FPS result please ?

Postby srvaldez » Apr 05, 2018 16:44

thank you Joshy :-)
that fixed it, the ABS warnings are only on my Mac, I suspect that it has to do with the gcc version used, I use the latest that Apple has to offer.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests