Skip to content

Commit

Permalink
Added screen_fx module
Browse files Browse the repository at this point in the history
Some support for auto-loading varyings with a segment
  • Loading branch information
ggcrunchy committed Apr 6, 2017
1 parent cd4d55e commit 26a9388
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 3 deletions.
68 changes: 65 additions & 3 deletions loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,34 @@ local function IgnorePreprocessor (str, ignore)
return ignore
end

-- --
local UsesVaryings = {}

-- Loads one or more code segments
local function LoadSegments (input)
local ignore, replacements, f, s, v = GetInputAndIgnoreList(input)

for _, str in f, s, v do
--
local varyings

if type(str) == "table" then
local t = str

str, varyings = t.code, {}

assert(type(str) == "string", "Segment code missing or not a string")

for name, vtype in pairs(t) do
if name ~= "code" then
assert(type(name) == "string", "Varying name not a string")
assert(type(vtype) == "string", "Varying type not a string")

varyings[name] = vtype
end
end
end

str = parser.StripComments(str)
str = parser.InsertReplacements(str, replacements)

Expand Down Expand Up @@ -185,6 +208,13 @@ local function LoadSegments (input)
local_ignore = IgnorePreprocessor(outer, local_ignore)
local_ignore = IgnorePreprocessor(body, local_ignore)

-- Ignore any varyings.
if varyings then
for name in pairs(varyings) do
local_ignore = AddToList(local_ignore, name)
end
end

-- With the preprocessor directives and parameters addressed, filter them
-- out of the code. Ignore any variables declared in the function body.
local stripped = body
Expand Down Expand Up @@ -214,7 +244,7 @@ local function LoadSegments (input)
end

-- Register the code and its dependencies.
ID, Code[ID], DependsOn[ID] = ID + 1, str, depends_on
ID, Code[ID], DependsOn[ID], UsesVaryings[ID] = ID + 1, str, depends_on, varyings
end
end

Expand Down Expand Up @@ -365,6 +395,24 @@ local function CollectIntoList (code, patt, ignore, collect)
return collect
end

--
local function PrependVaryings (varyings)
if varyings then
local list = {}

for k, v in pairs(varyings) do
list[#list + 1] = ("\tvarying %s %s;"):format(v, k)
end

list[#list + 1] = ""
list[#list + 1] = ""

return concat(list, "\n")
else
return ""
end
end

-- Infers depended-on code to prepend
local function Include (code)
-- Ignore any local assignments and definitions.
Expand All @@ -387,19 +435,33 @@ local function Include (code)
if collect then
sort(collect)

local pieces, prev = {}
local pieces, prev, varyings = {}

for i = 1, #collect do
local id = collect[i]

if id ~= prev then
pieces[#pieces + 1] = Code[id]

local uses_varyings = UsesVaryings[id]

if uses_varyings then
varyings = varyings or {}

for k, v in pairs(uses_varyings) do
local cur = varyings[k]

assert(not cur or cur == v, "Inconsistent duplicate varying")

varyings[k] = v
end
end
end

prev = id
end

return concat(pieces, "\n")
return PrependVaryings(varyings) .. concat(pieces, "\n")
end
end

Expand Down
77 changes: 77 additions & 0 deletions screen_fx.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
--- This module provides various screen-based effect routines.

--
-- 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.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--

-- Exports --
local M = {}

-- --
local PosVarying = "v_Pos"

-- --
local PosVaryingType = "P_POSITION vec2"

-- --
local PosVaryingDecl = ("%s %s"):format(PosVaryingType, PosVarying)

-- --
local PassThroughVertexKernel = ([[
varying %s;
P_POSITION vec2 VertexKernel (P_POSITION vec2 pos)
{
v_Pos = pos;
return pos;
}
]]):format(PosVaryingDecl)

--- DOCME
function M.GetPassThroughVertexKernelSource ()
return PassThroughVertexKernel
end

--- DOCME
function M.GetPosVaryingName ()
return PosVarying
end

--- DOCME
function M.GetPosVaryingType ()
return PosVaryingType
end

-- --
local Prelude = ([[
varying %s;
]]):format(PosVaryingDecl)

--- DOCME
function M.GetPrelude ()
return Prelude
end

-- Export the module.
return M

0 comments on commit 26a9388

Please sign in to comment.