rays 0.1.28 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/exception.cpp +45 -0
  3. data/.doc/ext/rays/native.cpp +2 -0
  4. data/.doc/ext/rays/shader.cpp +100 -6
  5. data/VERSION +1 -1
  6. data/ext/rays/defs.h +1 -0
  7. data/ext/rays/exception.cpp +45 -0
  8. data/ext/rays/native.cpp +2 -0
  9. data/ext/rays/shader.cpp +101 -5
  10. data/include/rays/exception.h +11 -0
  11. data/include/rays/ruby/bitmap.h +0 -1
  12. data/include/rays/ruby/bounds.h +0 -2
  13. data/include/rays/ruby/camera.h +0 -1
  14. data/include/rays/ruby/color.h +0 -2
  15. data/include/rays/ruby/color_space.h +0 -1
  16. data/include/rays/ruby/defs.h +30 -0
  17. data/include/rays/ruby/exception.h +28 -0
  18. data/include/rays/ruby/font.h +0 -1
  19. data/include/rays/ruby/image.h +0 -1
  20. data/include/rays/ruby/matrix.h +0 -1
  21. data/include/rays/ruby/painter.h +0 -1
  22. data/include/rays/ruby/point.h +0 -2
  23. data/include/rays/ruby/polygon.h +0 -1
  24. data/include/rays/ruby/polyline.h +0 -1
  25. data/include/rays/ruby/rays.h +0 -1
  26. data/include/rays/ruby/shader.h +0 -1
  27. data/include/rays/ruby.h +2 -0
  28. data/include/rays/shader.h +48 -1
  29. data/lib/rays/painter.rb +6 -5
  30. data/lib/rays/point.rb +1 -0
  31. data/lib/rays/shader.rb +18 -5
  32. data/rays.gemspec +2 -2
  33. data/src/exception.cpp +13 -0
  34. data/src/image.cpp +6 -4
  35. data/src/opengl.cpp +21 -7
  36. data/src/opengl.h +5 -3
  37. data/src/osx/font.mm +0 -2
  38. data/src/osx/opengl.mm +17 -2
  39. data/src/painter.cpp +196 -125
  40. data/src/shader.cpp +333 -53
  41. data/src/shader.h +53 -14
  42. data/src/shader_program.cpp +53 -27
  43. data/src/shader_program.h +8 -1
  44. data/src/shader_source.cpp +2 -2
  45. data/src/texture.cpp +75 -63
  46. data/test/test_point.rb +6 -5
  47. data/test/test_rays.rb +2 -2
  48. data/test/test_shader.rb +151 -14
  49. metadata +11 -6
@@ -4,6 +4,7 @@
4
4
  #define __RAYS_SHADER_H__
5
5
 
6
6
 
7
+ #include <vector>
7
8
  #include <xot/pimpl.h>
8
9
  #include <rays/defs.h>
9
10
  #include <rays/coord.h>
@@ -14,6 +15,7 @@ namespace Rays
14
15
 
15
16
 
16
17
  class Image;
18
+ class ShaderEnv;
17
19
 
18
20
 
19
21
  class Shader
@@ -23,7 +25,14 @@ namespace Rays
23
25
 
24
26
  public:
25
27
 
26
- Shader (const char* source = NULL);
28
+ Shader (
29
+ const char* fragment_shader_source = NULL,
30
+ const char* vertex_shader_source = NULL);
31
+
32
+ Shader (
33
+ const char* fragment_shader_source,
34
+ const char* vertex_shader_source,
35
+ ShaderEnv env);
27
36
 
28
37
  ~Shader ();
29
38
 
@@ -55,6 +64,10 @@ namespace Rays
55
64
 
56
65
  void set_uniform (const char* name, const Image& texture);
57
66
 
67
+ const char* vertex_shader_source () const;
68
+
69
+ const char* fragment_shader_source () const;
70
+
58
71
  operator bool () const;
59
72
 
60
73
  bool operator ! () const;
@@ -70,6 +83,40 @@ namespace Rays
70
83
  };// Shader
71
84
 
72
85
 
86
+ class ShaderEnv
87
+ {
88
+
89
+ public:
90
+
91
+ typedef std::vector<String> NameList;
92
+
93
+ enum Flags
94
+ {
95
+ IGNORE_NO_UNIFORM_LOCATION_ERROR = 0x1 << 0,
96
+ };
97
+
98
+ ShaderEnv (
99
+ const NameList& attribute_position_names = {},
100
+ const NameList& attribute_texcoord_names = {},
101
+ const NameList& attribute_color_names = {},
102
+ const char* varying_position_name = NULL,
103
+ const char* varying_texcoord_name = NULL,
104
+ const char* varying_color_name = NULL,
105
+ const NameList& uniform_position_matrix_names = {},
106
+ const NameList& uniform_texcoord_matrix_names = {},
107
+ const NameList& uniform_texcoord_min_names = {},
108
+ const NameList& uniform_texcoord_max_names = {},
109
+ const NameList& uniform_texcoord_offset_names = {},
110
+ const NameList& uniform_texture_names = {},
111
+ uint flags = 0);
112
+
113
+ struct Data;
114
+
115
+ Xot::PSharedImpl<Data> self;
116
+
117
+ };// ShaderEnv
118
+
119
+
73
120
  }// Rays
74
121
 
75
122
 
data/lib/rays/painter.rb CHANGED
@@ -27,15 +27,15 @@ module Rays
27
27
  if block
28
28
  attributes.each do |key, value|
29
29
  attributes[key] = __send__ key
30
- __send__ key, *value
30
+ __send__ key, *(value.nil? ? [nil] : value)
31
31
  end
32
-
33
32
  Xot::BlockUtil.instance_eval_or_block_call self, &block
34
-
33
+ end
34
+ ensure
35
+ if block
35
36
  attributes.each do |key, value|
36
- __send__ key, *value
37
+ __send__ key, *(value.nil? ? [nil] : value)
37
38
  end
38
-
39
39
  pop(*types)
40
40
  end
41
41
  end
@@ -92,6 +92,7 @@ module Rays
92
92
  end
93
93
 
94
94
  def shader=(shader, **uniforms)
95
+ shader = Shader.new shader if shader.is_a?(String)
95
96
  shader.uniform(**uniforms) if shader && !uniforms.empty?
96
97
  set_shader shader
97
98
  end
data/lib/rays/point.rb CHANGED
@@ -33,6 +33,7 @@ module Rays
33
33
  when 1 then [x]
34
34
  when 2 then [x, y]
35
35
  when 3 then [x, y, z]
36
+ when 4 then [x, y, z, 1.0]
36
37
  else raise ArgumentError
37
38
  end
38
39
  end
data/lib/rays/shader.rb CHANGED
@@ -10,11 +10,24 @@ module Rays
10
10
 
11
11
  class Shader
12
12
 
13
- def initialize(source = nil, **uniforms, &block)
14
- if source
15
- setup source
16
- uniform(**uniforms) unless uniforms.empty?
17
- end
13
+ def initialize(
14
+ fragment_shader_source,
15
+ vertex_shader_source = nil,
16
+ builtin_variable_names = nil,
17
+ ignore_no_uniform_location_error: false,
18
+ **uniforms, &block)
19
+
20
+ setup(
21
+ fragment_shader_source, vertex_shader_source,
22
+ builtin_variable_names&.values_at(
23
+ :attribute_position, :attribute_texcoord, :attribute_color,
24
+ :varying_position, :varying_texcoord, :varying_color,
25
+ :uniform_position_matrix, :uniform_texcoord_matrix,
26
+ :uniform_texcoord_min, :uniform_texcoord_max, :uniform_texcoord_offset,
27
+ :uniform_texture),
28
+ ignore_no_uniform_location_error)
29
+
30
+ uniform(**uniforms) unless uniforms.empty?
18
31
 
19
32
  Xot::BlockUtil.instance_eval_or_block_call self, &block if block
20
33
  end
data/rays.gemspec CHANGED
@@ -28,8 +28,8 @@ Gem::Specification.new do |s|
28
28
  s.platform = Gem::Platform::RUBY
29
29
  s.required_ruby_version = '>= 2.6.0'
30
30
 
31
- s.add_runtime_dependency 'xot', '~> 0.1.28'
32
- s.add_runtime_dependency 'rucy', '~> 0.1.28'
31
+ s.add_runtime_dependency 'xot', '~> 0.1.29'
32
+ s.add_runtime_dependency 'rucy', '~> 0.1.29'
33
33
 
34
34
  s.files = `git ls-files`.split $/
35
35
  s.executables = s.files.grep(%r{^bin/}) {|f| File.basename f}
data/src/exception.cpp CHANGED
@@ -20,6 +20,12 @@ namespace Rays
20
20
  }
21
21
 
22
22
 
23
+ ShaderError::ShaderError (const char* str)
24
+ : Super(str)
25
+ {
26
+ }
27
+
28
+
23
29
  namespace ErrorFunctions
24
30
  {
25
31
 
@@ -37,6 +43,13 @@ namespace Rays
37
43
  throw OpenGLError(Xot::error_text(file, line, s));
38
44
  }
39
45
 
46
+ void
47
+ shader_error (const char* file, int line, const char* format, ...)
48
+ {
49
+ XOT_STRINGF(format, s);
50
+ throw ShaderError(Xot::error_text(file, line, s));
51
+ }
52
+
40
53
  }// ErrorFunctions
41
54
 
42
55
 
data/src/image.cpp CHANGED
@@ -58,9 +58,11 @@ namespace Rays
58
58
  }
59
59
 
60
60
  static Bitmap&
61
- get_bitmap (Image& image)
61
+ get_bitmap (Image* image)
62
62
  {
63
- Image::Data* self = image.self.get();
63
+ assert(image);
64
+
65
+ Image::Data* self = image->self.get();
64
66
 
65
67
  if (!image)
66
68
  {
@@ -230,13 +232,13 @@ namespace Rays
230
232
  Bitmap&
231
233
  Image::bitmap ()
232
234
  {
233
- return get_bitmap(*this);
235
+ return get_bitmap(this);
234
236
  }
235
237
 
236
238
  const Bitmap&
237
239
  Image::bitmap () const
238
240
  {
239
- return get_bitmap(const_cast<Image&>(*this));
241
+ return get_bitmap(const_cast<Image*>(this));
240
242
  }
241
243
 
242
244
  Image::operator bool () const
data/src/opengl.cpp CHANGED
@@ -8,10 +8,10 @@ namespace Rays
8
8
  {
9
9
 
10
10
 
11
- GLenum
12
- OpenGL_get_error ()
11
+ bool
12
+ OpenGL_has_error ()
13
13
  {
14
- return glGetError();
14
+ return glGetError() != GL_NO_ERROR;
15
15
  }
16
16
 
17
17
  static String
@@ -24,12 +24,12 @@ namespace Rays
24
24
  case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
25
25
  case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
26
26
  case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
27
- #ifndef IOS
27
+ #if !defined(GL_VERSION_3_0)
28
28
  case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
29
29
  case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
30
+ case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
31
+ case GL_TABLE_TOO_LARGE: return "GL_TABLE_TOO_LARGE";
30
32
  #endif
31
- case 0x506: return "GL_INVALID_FRAMEBUFFER_OPERATION";
32
- case 0x8031: return "GL_TABLE_TOO_LARGE";
33
33
  default: return "UNKNOWN ERROR";
34
34
  }
35
35
  }
@@ -37,10 +37,24 @@ namespace Rays
37
37
  void
38
38
  OpenGL_check_error (const char* file, int line)
39
39
  {
40
- GLenum e = OpenGL_get_error();
40
+ GLenum e = glGetError();
41
41
  if (e != GL_NO_ERROR)
42
42
  opengl_error(file, line, "OpenGL error %s", get_error_name(e).c_str());
43
43
  }
44
44
 
45
+ void
46
+ OpenGL_check_error (const char* file, int line, const char* format, ...)
47
+ {
48
+ XOT_STRINGF(format, s);
49
+
50
+ GLenum e = glGetError();
51
+ if (e != GL_NO_ERROR)
52
+ {
53
+ opengl_error(
54
+ file, line,
55
+ "OpenGL error %s: %s", get_error_name(e).c_str(), s.c_str());
56
+ }
57
+ }
58
+
45
59
 
46
60
  }// Rays
data/src/opengl.h CHANGED
@@ -22,13 +22,15 @@ namespace Rays
22
22
  {
23
23
 
24
24
 
25
- void OpenGL_set_context (Context context);
25
+ void OpenGL_set_context (Context context);
26
26
 
27
27
  Context OpenGL_get_context ();
28
28
 
29
- GLenum OpenGL_get_error ();
29
+ bool OpenGL_has_error ();
30
30
 
31
- void OpenGL_check_error (const char* file, int line);
31
+ void OpenGL_check_error (const char* file, int line);
32
+
33
+ void OpenGL_check_error (const char* file, int line, const char* format, ...);
32
34
 
33
35
 
34
36
  }// Rays
data/src/osx/font.mm CHANGED
@@ -104,8 +104,6 @@ namespace Rays
104
104
 
105
105
  CGRect rect = CGRectMake(x, context_height - height - y, width, height);
106
106
  CGContextClearRect(context, rect);
107
- //CGContextSetRGBFillColor(context, 0, 0, 0, 1);
108
- //CGContextFillRect(context, rect);
109
107
  CGContextSetRGBFillColor(context, 1, 1, 1, 1);
110
108
 
111
109
  CGContextSaveGState(context);
data/src/osx/opengl.mm CHANGED
@@ -29,8 +29,23 @@ namespace Rays
29
29
  static Context context = NULL;
30
30
  if (!context)
31
31
  {
32
- NSOpenGLPixelFormat* pf = [NSOpenGLView defaultPixelFormat];
33
- context = [[NSOpenGLContext alloc] initWithFormat: pf shareContext: nil];
32
+ NSOpenGLPixelFormatAttribute attribs[] =
33
+ {
34
+ //NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
35
+ //NSOpenGLPFAAccelerated, NSOpenGLPFANoRecovery,
36
+ //NSOpenGLPFADoubleBuffer,
37
+ NSOpenGLPFAAllowOfflineRenderers,
38
+ NSOpenGLPFAColorSize, 32,
39
+ NSOpenGLPFADepthSize, 32,
40
+ //NSOpenGLPFAMultisample,
41
+ //NSOpenGLPFASampleBuffers, 1,
42
+ //NSOpenGLPFASamples, 4,
43
+ 0
44
+ };
45
+ NSOpenGLPixelFormat* pf = [[[NSOpenGLPixelFormat alloc]
46
+ initWithAttributes: attribs] autorelease];
47
+ context = [[[NSOpenGLContext alloc]
48
+ initWithFormat: pf shareContext: nil] autorelease];
34
49
  }
35
50
  return context;
36
51
  }