C Ruby
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
benchmark
include/mruby
mrblib
src
tasks
test
.gitignore
.travis.yml
LICENSE
README.md
Rakefile
build_config_sample.rb
build_config_test.rb
mrbgem.rake

README.md

mruby-float4 travis status

Small vector classes for mruby. Primarily for game developers. Provides a set of functions similar to shading languages (GLSL/HLSL/Cg).

Installation

Add

  conf.gem github: 'dabroz/mruby-float4'

to your build_config.rb.

Usage

There are two ways to use this library. First is the "Ruby" way - operate on vector instances like they were regular classes:

a = Vec4.new(1.0, 2.0, 3.0, 4.0)
b = Vec4.new(2.0 ,4.0, 5.0, 6.0)
c = (a * 2.0 + b).normalize.dot(Vec4.new(0, 0, 1, 1))

Another is a typical shading language syntax, using an optional module import:

extend Vec4::GLSL
a = vec4(1.0, 2.0, 3.0, 4.0)
b = vec4(2.0 ,4.0, 5.0, 6.0)
c = dot(normalize(a * 2.0 + b), vec4(0, 0, 1, 1))

All vector instances are immutable (there is no x= and similar functions). This is done intentionally. Since Ruby has no "by value" structures it is very for someone used to shading languages to not create a duplicate of vector and modify multiple instances incorrectly.

Available types

  • Vec4 (4x float), Vec3 (3x float), Vec2 (2x float)
  • IVec4 (4x mrb_int), IVec3 (3x mrb_int), IVec2 (2x mrb_int)
  • BVec4 (4x mrb_bool), BVec3 (3x mrb_bool), BVec2 (2x mrb_bool)

Supported functions

  • +
  • -
  • *
  • /
  • -@
  • acos
  • asin
  • atan2
  • atan
  • ceil
  • clamp
  • cos
  • cross
  • degrees
  • dot
  • exp2
  • floor
  • fract
  • inversesqrt
  • length_squared
  • length
  • lerp
  • log2
  • log
  • max
  • min
  • mix
  • normalize
  • radians
  • reflect
  • refract
  • saturate
  • sin
  • smoothstep
  • sqrt
  • step
  • tan
  • all possible swizzle combinations (.xyz, .xyz, .xxx etc)

Helper functions:

  • data_size (4 for Vec4)
  • data_class (Float for Vec4)

Design

It is written completly in C (although C code is autogenerated from Ruby template) for performace reasons. C functions do not usually call Ruby, so things like overridden Math.floor won't change this gem behavior.

Data is stored withing the object as simple C structure (for size and performance), which has several side-effects:

  • any overrides on Float won't change this gem behavior
  • VecX family uses single-precision float numbers (mruby uses double-precision by default)

In 64-bit mode, inline structures (istruct) is used to store data to increate performance and avoid extra allocation.

Known limitations

  • 2.0 * Vec2.new(1.0, 2.0) won't work, because mruby doesn't currently support Fixnum/Float operators overloading. There is however a pull request for that in the works.

Performance

benchmark/bm_ao_render.rb        (pure Ruby)         [256x256] -> 1m40.712s
benchmark/bm_ao_render_float4.rb (32-bit)            [256x256] -> 0m59.265s
benchmark/bm_ao_render_float4.rb (64-bit, w/istruct) [256x256] -> 0m53.674s

So there is a slight performance bonus of using this gem. 64-bit version is even faster, thanks to inline structure storage.

Licence

MIT License

Copyright (c) 2016 Tomasz Dąbrowski

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.

Contributing

  1. Fork it ( https://github.com/dabroz/mruby-float4/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request