Skip to content

Instantly share code, notes, and snippets.

@wanabe
Created December 7, 2014 13:58
Show Gist options
  • Select an option

  • Save wanabe/64602cb2ea501357114f to your computer and use it in GitHub Desktop.

Select an option

Save wanabe/64602cb2ea501357114f to your computer and use it in GitHub Desktop.
diff --git a/sample/shader_sample/Shader/Flash.rb b/sample/shader_sample/Shader/Flash.rb
index 4fc6bd7..f83d281 100644
--- a/sample/shader_sample/Shader/Flash.rb
+++ b/sample/shader_sample/Shader/Flash.rb
@@ -1,58 +1,42 @@
# -*- coding: Windows-31J -*-
+require 'dxrubyhlsl'
-class FlashShader < DXRuby::Shader
- hlsl = <<EOS
-// (1) グローバル変数
- float3 g_color;
- float g_level;
- texture tex0;
-
-// (2) サンプラ
- sampler Samp0 = sampler_state
- {
- Texture =<tex0>;
- AddressU=BORDER;
- AddressV=BORDER;
- };
+class Flash < Hlsl
+# (1) グローバル変数
+ float3 :g_color
+ float :g_level
-// (3) 入出力の構造体
- struct PixelIn
- {
- float2 UV : TEXCOORD0;
- };
- struct PixelOut
- {
- float4 Color : COLOR0;
- };
+# (2) サンプラ
+ sampler def Samp0
+ @AddressU=BORDER
+ @AddressV=BORDER
+ end
-// (4) ピクセルシェーダのプログラム
- PixelOut PS(PixelIn input)
+# (3) 入出力の構造体
+ struct PixelIn =
{
- PixelOut output;
- output.Color.rgb = tex2D( Samp0, input.UV ).rgb * g_level + g_color * (1.0-g_level);
- output.Color.a = tex2D( Samp0, input.UV ).a;
-
- return output;
+ UV: float2.TEXCOORD0
}
-
-// (5) technique定義
- technique Flash
+ struct PixelOut =
{
- pass P0 // パス
- {
- PixelShader = compile ps_2_0 PS();
- }
+ Color: float4.COLOR0
}
-EOS
- @@core = DXRuby::Shader::Core.new(
- hlsl,
- {
- :g_color => :float,
- :g_level => :float,
- }
- )
+# (4) ピクセルシェーダのプログラム
+ PixelOut def PS(input = PixelIn)
+ PixelOut output
+ output.Color.rgb = tex2D( Samp0, input.UV ).rgb * g_level + g_color * (1.0-g_level)
+ output.Color.a = tex2D( Samp0, input.UV ).a
+
+ return output
+ end
+
+# (5) technique定義
+ technique :Flash, :PS
+end
+class FlashShader < DXRuby::Shader
+ @@core = Flash.core
# colorはフラッシュ色、durationは遷移にかけるフレーム数
def initialize(duration, color = [255, 255, 255])
diff --git a/sample/shader_sample/Shader/RasterScroll.rb b/sample/shader_sample/Shader/RasterScroll.rb
index 534786c..47e3f7d 100644
--- a/sample/shader_sample/Shader/RasterScroll.rb
+++ b/sample/shader_sample/Shader/RasterScroll.rb
@@ -1,51 +1,36 @@
# -*- coding: Windows-31J -*-
+require 'dxrubyhlsl'
class RasterScrollShader < DXRuby::Shader
-hlsl = <<EOS
-float g_start;
-float g_level;
-texture tex0;
-sampler Samp = sampler_state
-{
- Texture =<tex0>;
- AddressU = CLAMP;
- AddressV = CLAMP;
-};
+ class Hlsl < DXRuby::Hlsl
+ float :g_start
+ float :g_level
+ sampler def Samp
+ @AddressU = CLAMP
+ @AddressV = CLAMP
+ end
-struct PixelIn
-{
- float2 UV : TEXCOORD0;
-};
-struct PixelOut
-{
- float4 Color : COLOR0;
-};
+ struct PixelIn =
+ {
+ UV: float2.TEXCOORD0
+ }
+ struct PixelOut =
+ {
+ Color: float4.COLOR0
+ }
-PixelOut PS(PixelIn input)
-{
- PixelOut output;
- input.UV.x = input.UV.x + sin(radians(input.UV.y*360-g_start))*g_level;
- output.Color = tex2D( Samp, input.UV );
+ PixelOut def PS(input = PixelIn)
+ PixelOut output
+ input.UV.x = input.UV.x + sin(radians(input.UV.y*360-g_start))*g_level
+ output.Color = tex2D( Samp, input.UV )
- return output;
-}
+ return output
+ end
-technique Raster
-{
- pass P0
- {
- PixelShader = compile ps_2_0 PS();
- }
-}
-EOS
+ technique :Raster, :PS
+ end
- @@core = DXRuby::Shader::Core.new(
- hlsl,
- {
- :g_start => :float,
- :g_level => :float,
- }
- )
+ @@core = Hlsl.core
attr_accessor :speed, :level
diff --git a/sample/shader_sample/Shader/RgssSprite.rb b/sample/shader_sample/Shader/RgssSprite.rb
index dea7d0a..e689ecf 100644
--- a/sample/shader_sample/Shader/RgssSprite.rb
+++ b/sample/shader_sample/Shader/RgssSprite.rb
@@ -1,57 +1,44 @@
+require 'dxrubyhlsl'
+
class SpriteShader < Shader
- hlsl = <<EOS
- float4 g_blend;
- float4 g_tone;
-
- texture tex0;
- sampler Samp = sampler_state
- {
- Texture =<tex0>;
- };
-
- struct PixelIn
- {
- float2 UV : TEXCOORD0;
- };
- struct PixelOut
- {
- float4 Color : COLOR0;
- };
-
- PixelOut PS(PixelIn input)
- {
- PixelOut output;
- float3 temp;
- float3 ntsc = {0.298912, 0.586611, 0.114478};
- float ntscy;
- output.Color = tex2D( Samp, input.UV );
-
- ntsc = ntsc * output.Color.rgb;
- ntscy = ntsc.r + ntsc.g + ntsc.b;
- temp.r = output.Color.r + ((ntscy - output.Color.r) * g_tone.a);
- temp.g = output.Color.g + ((ntscy - output.Color.g) * g_tone.a);
- temp.b = output.Color.b + ((ntscy - output.Color.b) * g_tone.a);
-
- output.Color.rgb = min(1.0, temp + g_tone.rgb) * (1.0 - g_blend.a) + g_blend.rgb * g_blend.a;
-
- return output;
- }
-
- technique TShader
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS();
- }
- }
-EOS
-
- @@core = Shader::Core.new(hlsl,
+ class Hlsl < DXRuby::Hlsl
+ float4 :g_blend
+ float4 :g_tone
+
+ sampler def Samp
+ end
+
+ struct PixelIn =
{
- :g_blend => :float,
- :g_tone => :float,
+ UV: float2.TEXCOORD0
}
- )
+ struct PixelOut =
+ {
+ Color: float4.COLOR0
+ }
+
+ PixelOut def PS(input = PixelIn)
+ PixelOut output
+ float3 temp
+ float3 ntsc = [0.298912, 0.586611, 0.114478]
+ float ntscy
+ output.Color = tex2D( Samp, input.UV )
+
+ ntsc = ntsc * output.Color.rgb
+ ntscy = ntsc.r + ntsc.g + ntsc.b
+ temp.r = output.Color.r + ((ntscy - output.Color.r) * g_tone.a)
+ temp.g = output.Color.g + ((ntscy - output.Color.g) * g_tone.a)
+ temp.b = output.Color.b + ((ntscy - output.Color.b) * g_tone.a)
+
+ output.Color.rgb = min(1.0, temp + g_tone.rgb) * (1.0 - g_blend.a) + g_blend.rgb * g_blend.a
+
+ return output
+ end
+
+ technique :TShader, :PS
+ end
+
+ @@core = Hlsl.core
def initialize
super(@@core, "TShader")
diff --git a/sample/shader_sample/Shader/Transition.rb b/sample/shader_sample/Shader/Transition.rb
index bf48ff3..f6c160e 100644
--- a/sample/shader_sample/Shader/Transition.rb
+++ b/sample/shader_sample/Shader/Transition.rb
@@ -1,59 +1,41 @@
# -*- coding: Windows-31J -*-
+require 'dxrubyhlsl'
class TransitionShader < DXRuby::Shader
- hlsl = <<EOS
- float g_min;
- float g_max;
- float2 scale;
- texture tex0;
- texture tex1;
- sampler Samp0 = sampler_state
- {
- Texture =<tex0>;
- };
- sampler Samp1 = sampler_state
- {
- Texture =<tex1>;
- AddressU = WRAP;
- AddressV = WRAP;
- };
+ class Hlsl < DXRuby::Hlsl
+ float :g_min
+ float :g_max
+ float2 :scale
+ texture :tex1
+ sampler def Samp0
+ end
+ sampler def Samp1
+ @Texture = tex1
+ @AddressU = WRAP
+ @AddressV = WRAP
+ end
- struct PixelIn
- {
- float2 UV : TEXCOORD0;
- };
- struct PixelOut
- {
- float4 Color : COLOR0;
- };
+ struct PixelIn =
+ {
+ UV: float2.TEXCOORD0
+ }
+ struct PixelOut =
+ {
+ Color: float4.COLOR0
+ }
- PixelOut PS(PixelIn input)
- {
- PixelOut output;
- output.Color = tex2D( Samp0, input.UV );
- output.Color.a *= smoothstep(g_min, g_max, tex2D( Samp1, input.UV * scale ).r );
+ PixelOut def PS(input = PixelIn)
+ PixelOut output
+ output.Color = tex2D( Samp0, input.UV )
+ output.Color.a *= smoothstep(g_min, g_max, tex2D( Samp1, input.UV * scale ).r )
- return output;
- }
+ return output
+ end
- technique Transition
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS();
- }
- }
-EOS
+ technique :Transition, :PS
+ end
- @@core = DXRuby::Shader::Core.new(
- hlsl,
- {
- :g_min => :float,
- :g_max => :float,
- :scale => :float, # HLSL側がfloat2の場合は:floatを指定して[Float, Flaot]という形で渡す
- :tex1 => :texture,
- }
- )
+ @@core = Hlsl.core
# durationは遷移にかけるフレーム数、imageはルール画像のImageオブジェクト(省略でクロスフェード)、vagueは曖昧さ
def initialize(duration, image=nil, vague=nil)
diff --git a/sample/shader_sample/sample_blur.rb b/sample/shader_sample/sample_blur.rb
index 3965947..7fd0425 100644
--- a/sample/shader_sample/sample_blur.rb
+++ b/sample/shader_sample/sample_blur.rb
@@ -1,95 +1,79 @@
require 'dxruby'
+require 'dxrubyhlsl'
-hlsl = <<EOS
- texture tex0;
- float2 g_size;
+class Blur < Hlsl
+ float2 :g_size
- sampler Samp0 = sampler_state
- {
- Texture =<tex0>;
- AddressU=BORDER;
- AddressV=BORDER;
- };
+ sampler def Samp0
+ @AddressU = BORDER
+ @AddressV = BORDER
+ end
- float4 PS1(float2 In : TEXCOORD0) : COLOR0
- {
- float2 Texel0 = In + float2( -1.0/g_size.x, 0.0f );
- float2 Texel1 = In + float2( -1.0/g_size.x*2, 0.0f );
- float2 Texel2 = In + float2( -1.0/g_size.x*3, 0.0f );
- float2 Texel3 = In + float2( -1.0/g_size.x*4, 0.0f );
- float2 Texel4 = In + float2( -1.0/g_size.x*5, 0.0f );
+ float4 def PS1(input = float2.TEXCOORD0) COLOR0
+ float2 texel0 = input + float2( -1.0/g_size.x, 0.0.f )
+ float2 texel1 = input + float2( -1.0/g_size.x*2, 0.0.f )
+ float2 texel2 = input + float2( -1.0/g_size.x*3, 0.0.f )
+ float2 texel3 = input + float2( -1.0/g_size.x*4, 0.0.f )
+ float2 texel4 = input + float2( -1.0/g_size.x*5, 0.0.f )
- float2 Texel5 = In + float2( 1.0/g_size.x, 0.0f );
- float2 Texel6 = In + float2( 1.0/g_size.x*2, 0.0f );
- float2 Texel7 = In + float2( 1.0/g_size.x*3, 0.0f );
- float2 Texel8 = In + float2( 1.0/g_size.x*4, 0.0f );
- float2 Texel9 = In + float2( 1.0/g_size.x*5, 0.0f );
+ float2 texel5 = input + float2( 1.0/g_size.x, 0.0.f )
+ float2 texel6 = input + float2( 1.0/g_size.x*2, 0.0.f )
+ float2 texel7 = input + float2( 1.0/g_size.x*3, 0.0.f )
+ float2 texel8 = input + float2( 1.0/g_size.x*4, 0.0.f )
+ float2 texel9 = input + float2( 1.0/g_size.x*5, 0.0.f )
- float4 p0 = tex2D( Samp0, In ) * 0.20f;
+ float4 p0 = tex2D( Samp0, input ) * 0.20.f
- float4 p1 = tex2D( Samp0, Texel0 ) * 0.12f;
- float4 p2 = tex2D( Samp0, Texel1 ) * 0.10f;
- float4 p3 = tex2D( Samp0, Texel2 ) * 0.08f;
- float4 p4 = tex2D( Samp0, Texel3 ) * 0.06f;
- float4 p5 = tex2D( Samp0, Texel4 ) * 0.04f;
+ float4 p1 = tex2D( Samp0, texel0 ) * 0.12.f
+ float4 p2 = tex2D( Samp0, texel1 ) * 0.10.f
+ float4 p3 = tex2D( Samp0, texel2 ) * 0.08.f
+ float4 p4 = tex2D( Samp0, texel3 ) * 0.06.f
+ float4 p5 = tex2D( Samp0, texel4 ) * 0.04.f
- float4 p6 = tex2D( Samp0, Texel5 ) * 0.12f;
- float4 p7 = tex2D( Samp0, Texel6 ) * 0.10f;
- float4 p8 = tex2D( Samp0, Texel7 ) * 0.08f;
- float4 p9 = tex2D( Samp0, Texel8 ) * 0.06f;
- float4 p10 = tex2D( Samp0, Texel9 ) * 0.04f;
+ float4 p6 = tex2D( Samp0, texel5 ) * 0.12.f
+ float4 p7 = tex2D( Samp0, texel6 ) * 0.10.f
+ float4 p8 = tex2D( Samp0, texel7 ) * 0.08.f
+ float4 p9 = tex2D( Samp0, texel8 ) * 0.06.f
+ float4 p10 = tex2D( Samp0, texel9 ) * 0.04.f
- return p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10;
- }
+ return p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10
+ end
- float4 PS2(float2 In : TEXCOORD0) : COLOR0
- {
- float2 Texel0 = In + float2( 0.0f, -1.0/g_size.y );
- float2 Texel1 = In + float2( 0.0f, -1.0/g_size.y*2 );
- float2 Texel2 = In + float2( 0.0f, -1.0/g_size.y*3 );
- float2 Texel3 = In + float2( 0.0f, -1.0/g_size.y*4 );
- float2 Texel4 = In + float2( 0.0f, -1.0/g_size.y*5 );
+ float4 def PS2(input = float2.TEXCOORD0) COLOR0
+ float2 texel0 = input + float2( 0.0.f, -1.0/g_size.y )
+ float2 texel1 = input + float2( 0.0.f, -1.0/g_size.y*2 )
+ float2 texel2 = input + float2( 0.0.f, -1.0/g_size.y*3 )
+ float2 texel3 = input + float2( 0.0.f, -1.0/g_size.y*4 )
+ float2 texel4 = input + float2( 0.0.f, -1.0/g_size.y*5 )
- float2 Texel5 = In + float2( 0.0f, 1.0/g_size.y );
- float2 Texel6 = In + float2( 0.0f, 1.0/g_size.y*2 );
- float2 Texel7 = In + float2( 0.0f, 1.0/g_size.y*3 );
- float2 Texel8 = In + float2( 0.0f, 1.0/g_size.y*4 );
- float2 Texel9 = In + float2( 0.0f, 1.0/g_size.y*5 );
+ float2 texel5 = input + float2( 0.0.f, 1.0/g_size.y )
+ float2 texel6 = input + float2( 0.0.f, 1.0/g_size.y*2 )
+ float2 texel7 = input + float2( 0.0.f, 1.0/g_size.y*3 )
+ float2 texel8 = input + float2( 0.0.f, 1.0/g_size.y*4 )
+ float2 texel9 = input + float2( 0.0.f, 1.0/g_size.y*5 )
- float4 p0 = tex2D( Samp0, In ) * 0.20f;
+ float4 p0 = tex2D( Samp0, input ) * 0.20.f
- float4 p1 = tex2D( Samp0, Texel0 ) * 0.12f;
- float4 p2 = tex2D( Samp0, Texel1 ) * 0.10f;
- float4 p3 = tex2D( Samp0, Texel2 ) * 0.08f;
- float4 p4 = tex2D( Samp0, Texel3 ) * 0.06f;
- float4 p5 = tex2D( Samp0, Texel4 ) * 0.04f;
+ float4 p1 = tex2D( Samp0, texel0 ) * 0.12.f
+ float4 p2 = tex2D( Samp0, texel1 ) * 0.10.f
+ float4 p3 = tex2D( Samp0, texel2 ) * 0.08.f
+ float4 p4 = tex2D( Samp0, texel3 ) * 0.06.f
+ float4 p5 = tex2D( Samp0, texel4 ) * 0.04.f
- float4 p6 = tex2D( Samp0, Texel5 ) * 0.12f;
- float4 p7 = tex2D( Samp0, Texel6 ) * 0.10f;
- float4 p8 = tex2D( Samp0, Texel7 ) * 0.08f;
- float4 p9 = tex2D( Samp0, Texel8 ) * 0.06f;
- float4 p10 = tex2D( Samp0, Texel9 ) * 0.04f;
+ float4 p6 = tex2D( Samp0, texel5 ) * 0.12.f
+ float4 p7 = tex2D( Samp0, texel6 ) * 0.10.f
+ float4 p8 = tex2D( Samp0, texel7 ) * 0.08.f
+ float4 p9 = tex2D( Samp0, texel8 ) * 0.06.f
+ float4 p10 = tex2D( Samp0, texel9 ) * 0.04.f
- return p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10;
- }
+ return p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10
+ end
- technique Blur1
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS1();
- }
- }
- technique Blur2
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS2();
- }
- }
-EOS
+ technique :Blur1, :PS1
+ technique :Blur2, :PS2
+end
-core = Shader::Core.new(hlsl,{:g_size => :float})
+core = Blur.core
shader1 = Shader.new(core, "Blur1")
shader2 = Shader.new(core, "Blur2")
@@ -108,5 +92,5 @@ Window.loop do
rt2.draw_ex(0, 0, rt1, :blend=>:nond, :shader=>shader2)
rt2.update
Window.draw(350, 50, rt2)
- break if Input.key_push?(K_ESCAPE)
+ break if Input.key_push?(K_ESCAPE)
end
diff --git a/sample/shader_sample/sample_divide.rb b/sample/shader_sample/sample_divide.rb
index 611020d..24d1bcf 100644
--- a/sample/shader_sample/sample_divide.rb
+++ b/sample/shader_sample/sample_divide.rb
@@ -1,45 +1,29 @@
require 'dxruby'
+require 'dxrubyhlsl'
-hlsl = <<EOS
- float3 p1, p2;
- texture tex0;
+class Divide < Hlsl
+ float3 :p1, :p2
- sampler Samp0 = sampler_state
- {
- Texture =<tex0>;
- };
+ sampler def Samp0
+ end
- float4 PS1(float2 input : TEXCOORD0) : COLOR0
- {
- clip( cross( float3(p2.x - p1.x, p2.y - p1.y, 0), float3(input.x - p1.x, input.y - p1.y, 0) ).z );
- return tex2D( Samp0, input );
- }
+ float4 def PS1(input = float2.TEXCOORD0) COLOR0
+ clip( cross( float3(p2.x - p1.x, p2.y - p1.y, 0), float3(input.x - p1.x, input.y - p1.y, 0) ).z )
+ return tex2D( Samp0, input )
+ end
- float4 PS2(float2 input : TEXCOORD0) : COLOR0
- {
- clip( cross( float3(p1.x - p2.x, p1.y - p2.y, 0), float3(input.x - p1.x, input.y - p1.y, 0) ).z );
- return tex2D( Samp0, input );
- }
+ float4 def PS2(input = float2.TEXCOORD0) COLOR0
+ clip( cross( float3(p1.x - p2.x, p1.y - p2.y, 0), float3(input.x - p1.x, input.y - p1.y, 0) ).z )
+ return tex2D( Samp0, input )
+ end
- technique Zan1
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS1();
- }
- }
- technique Zan2
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS2();
- }
- }
-EOS
+ technique :Zan1, :PS1
+ technique :Zan2, :PS2
+end
Window.width, Window.height = 800, 600
-core = Shader::Core.new(hlsl,{:p1=>:float, :p2=>:float})
+core = Divide.core
shader1 = Shader.new(core, "Zan1")
shader2 = Shader.new(core, "Zan2")
image = Image.load("bgimage/BG42a.jpg")
diff --git a/sample/shader_sample/sample_lens.rb b/sample/shader_sample/sample_lens.rb
index 65023ae..018c15c 100644
--- a/sample/shader_sample/sample_lens.rb
+++ b/sample/shader_sample/sample_lens.rb
@@ -1,50 +1,41 @@
require 'dxruby'
+require 'dxrubyhlsl'
-hlsl = <<EOS
-texture tex0;
-texture tex1;
-float2 scale;
-float2 point;
-float r, distance;
+class Lens < Hlsl
+ texture :tex1
+ float2 :scale
+ float2 :point
+ float :r, :distance
-sampler Samp0 = sampler_state
-{
- Texture =<tex0>;
-};
-sampler Samp1 = sampler_state
-{
- Texture =<tex1>;
- AddressU = BORDER;
- AddressV = BORDER;
-};
+ sampler def Samp0
+ end
-float4 PS(float2 input : TEXCOORD0) : COLOR0
-{
- float4 output;
- float d;
+ sampler def Samp1
+ @Texture = tex1
+ @AddressU = BORDER
+ @AddressV = BORDER
+ end
- clip(tex2D( Samp0, input ).r - 1.0);
+ float4 def PS(input = float2.TEXCOORD0) COLOR0
+ float4 output
+ float d
- d = sqrt((input.x-0.5)*(input.x-0.5) + (input.y-0.5)*(input.y-0.5))+1;
- output = tex2D( Samp1, float2( point.x + (input.x-0.5) / scale.x * distance / r * d, point.y + (input.y-0.5) / scale.y * distance / r * d));
- return output;
-}
+ clip(tex2D( Samp0, input ).r - 1.0)
-technique Lens
-{
- pass P0
- {
- PixelShader = compile ps_2_0 PS();
- }
-}
-EOS
+ d = sqrt((input.x-0.5)*(input.x-0.5) + (input.y-0.5)*(input.y-0.5))+1
+ output = tex2D( Samp1, float2( point.x + (input.x-0.5) / scale.x * distance / r * d, point.y + (input.y-0.5) / scale.y * distance / r * d))
+ return output
+ end
+
+ technique :Lens, :PS
+end
Window.width, Window.height = 800, 600
bgimage = Image.load("bgimage/BG42a.jpg")
image = Image.new(200,200).circle_fill(100,100,100,C_WHITE)
loupeimage = Image.new(200,200).circle(100,100,100,C_WHITE)
-core = DXRuby::Shader::Core.new(hlsl, {:scale=>:float, :r=>:float, :distance=>:float, :tex1=>:texture, :point=>:float})
+core = Lens.core
shader = Shader.new(core, "Lens")
shader.scale = [bgimage.width.quo(image.width), bgimage.height.quo(image.height)]
shader.r = 1000
diff --git a/sample/shader_sample/sample_mapping.rb b/sample/shader_sample/sample_mapping.rb
index 69579c2..2694e27 100644
--- a/sample/shader_sample/sample_mapping.rb
+++ b/sample/shader_sample/sample_mapping.rb
@@ -1,41 +1,32 @@
require 'dxruby'
-
-hlsl = <<EOS
-float g_start;
-float g_level;
-texture tex0;
-sampler Samp = sampler_state
-{
- Texture =<tex0>;
- AddressU = BORDER;
- AddressV = BORDER;
-};
-
-float4 PS(float2 input : TEXCOORD0) : COLOR0
-{
- float4 output;
- float distance = radians(distance(input, float2(0.5, 0.5)) * 360 * 4 - g_start);
- float height = sin(distance);
- float slope = cos(distance);
- float d = clamp(-1,1,dot(normalize(float3(input.y - 0.5, input.x - 0.5,0 )), float3(0.5,-0.5,0.5)))*slope+1;
- input.y = input.y + height * g_level;
-
- output = tex2D( Samp, input ) * d;
-
- return output;
-}
-
-technique Raster
-{
- pass P0
- {
- PixelShader = compile ps_2_0 PS();
- }
-}
-EOS
+require 'dxrubyhlsl'
+
+class Mapping < Hlsl
+ float :g_start
+ float :g_level
+ sampler def Samp
+ @AddressU = BORDER
+ @AddressV = BORDER
+ end
+
+ float4 def PS(input = float2.TEXCOORD0) COLOR0
+ float4 output
+ float distance = radians(distance(input, float2(0.5, 0.5)) * 360 * 4 - g_start)
+ float height = sin(distance)
+ float slope = cos(distance)
+ float d = clamp(-1,1,dot(normalize(float3(input.y - 0.5, input.x - 0.5,0 )), float3(0.5,-0.5,0.5)))*slope+1
+ input.y = input.y + height * g_level
+
+ output = tex2D( Samp, input ) * d
+
+ return output
+ end
+
+ technique :Raster, :PS
+end
Window.width, Window.height = 800, 600
-core = DXRuby::Shader::Core.new(hlsl, {:g_start=>:float, :g_level=>:float})
+core = Mapping.core
shader = Shader.new(core, "Raster")
shader.g_start = 0
shader.g_level = 0
diff --git a/sample/shader_sample/sample_spehari.rb b/sample/shader_sample/sample_spehari.rb
index d6bde38..ebd25cc 100644
--- a/sample/shader_sample/sample_spehari.rb
+++ b/sample/shader_sample/sample_spehari.rb
@@ -1,30 +1,20 @@
require 'dxruby'
+require 'dxrubyhlsl'
-hlsl = <<EOS
- texture tex0;
+class Spehari < Hlsl
+ sampler def Samp0
+ @AddressU = WRAP
+ @AddressV = WRAP
+ end
- sampler Samp0 = sampler_state
- {
- Texture =<tex0>;
- AddressU = WRAP;
- AddressV = WRAP;
- };
+ float4 def PS(input = float2.TEXCOORD0) COLOR0
+ return tex2D( Samp0, float2((input.x-0.5) / (input.y+0.20), (input.y-1) / (input.y+0.20)) )
+ end
- float4 PS(float2 input : TEXCOORD0) : COLOR0
- {
- return tex2D( Samp0, float2((input.x-0.5) / (input.y+0.20), (input.y-1) / (input.y+0.20)) );
- }
-
- technique SH
- {
- pass P0
- {
- PixelShader = compile ps_2_0 PS();
- }
- }
-EOS
+ technique :SH, :PS
+end
-core = Shader::Core.new(hlsl,{})
+core = Spehari.core
shader = Shader.new(core, "SH")
image = Image.new(80, 80,[0, 255, 0])
diff --git a/sample/shader_sample/sample_sphere.rb b/sample/shader_sample/sample_sphere.rb
index 5b477a5..0f45c6f 100644
--- a/sample/shader_sample/sample_sphere.rb
+++ b/sample/shader_sample/sample_sphere.rb
@@ -1,53 +1,37 @@
require 'dxruby'
-
-hlsl = <<EOS
-texture tex0;
-float2 raito;
-
-sampler Samp0 = sampler_state
-{
- Texture =<tex0>;
- AddressU = BORDER;
- AddressV = BORDER;
-};
-
-float4 PS1(float2 input : TEXCOORD0) : COLOR0
-{
- float pi = acos(-1);
- return tex2D( Samp0,
- float2( ((1 - (acos((input.x*2 - 1.0) ) / pi)) - 0.5) + 0.5
- , ((1 - (acos((input.y*2 - 1.0) ) / pi)) - 0.5) + 0.5) );
-// , input.y ));
-}
-
-float4 PS2(float2 input : TEXCOORD0) : COLOR0
-{
- return tex2D( Samp0,
- float2((input.x - 0.5) / cos(asin((input.y*2 - 1.0))) / raito.x + 0.5
- , (input.y - 0.5) / raito.y + 0.5) );
-}
-
-technique Sphere1
-{
- pass P0
- {
- PixelShader = compile ps_2_0 PS1();
- }
-}
-technique Sphere2
-{
- pass P0
- {
- PixelShader = compile ps_2_0 PS2();
- }
-}
-EOS
+require 'dxrubyhlsl'
+
+class Sphere < Hlsl
+ float2 :raito
+
+ sampler def Samp0
+ @AddressU = BORDER
+ @AddressV = BORDER
+ end
+
+ float4 def PS1(input = float2.TEXCOORD0) COLOR0
+ float pi = acos(-1)
+ return tex2D( Samp0, \
+ float2( ((1 - (acos((input.x*2 - 1.0) ) / pi)) - 0.5) + 0.5 \
+ , ((1 - (acos((input.y*2 - 1.0) ) / pi)) - 0.5) + 0.5) )
+# , input.y ));
+ end
+
+ float4 def PS2(input = float2.TEXCOORD0) COLOR0
+ return tex2D( Samp0, \
+ float2((input.x - 0.5) / cos(asin((input.y*2 - 1.0))) / raito.x + 0.5 \
+ , (input.y - 0.5) / raito.y + 0.5) )
+ end
+
+ technique :Sphere1, :PS1
+ technique :Sphere2, :PS2
+end
Window.width, Window.height = 800, 600
image = Image.load("bgimage/world_map2.png")
image = image.slice(4, 5, image.width - 10, image.height - 9)
-core = DXRuby::Shader::Core.new(hlsl, {:raito=>:float})
+core = Sphere.core
shader1 = Shader.new(core, "Sphere1")
shader2 = Shader.new(core, "Sphere2")
shader1.raito = shader2.raito = [600.quo(Window.width), 600.quo(Window.height)]
diff --git a/sample/shader_sample/sample_vertexshader.rb b/sample/shader_sample/sample_vertexshader.rb
index 8440a10..cc6c27d 100644
--- a/sample/shader_sample/sample_vertexshader.rb
+++ b/sample/shader_sample/sample_vertexshader.rb
@@ -1,4 +1,5 @@
require 'dxruby'
+require 'dxrubyhlsl'
class Vector
def initialize(*v)
@@ -134,47 +135,35 @@ class Matrix
end
-hlsl = <<EOS
-float4x4 g_world, g_view, g_proj;
-texture tex0;
+class VertexShader < Hlsl
+ float4x4 :g_world, :g_view, :g_proj
-sampler Samp = sampler_state
-{
- Texture =<tex0>;
-};
+ sampler def Samp
+ end
-struct VS_OUTPUT
-{
- float4 pos : POSITION;
- float2 tex : TEXCOORD0;
-};
+ struct VS_OUTPUT =
+ {
+ pos: float4.POSITION,
+ tex: float2.TEXCOORD0
+ }
-VS_OUTPUT VS(float4 pos: POSITION, float2 tex: TEXCOORD0)
-{
- VS_OUTPUT output;
+ VS_OUTPUT def VS(pos = float4.POSITION, tex = float2.TEXCOORD0)
+ VS_OUTPUT output
- output.pos = mul(mul(mul(pos, g_world), g_view), g_proj);
- output.tex = tex;
+ output.pos = mul(mul(mul(pos, g_world), g_view), g_proj)
+ output.tex = tex
- return output;
-}
+ return output
+ end
-float4 PS(float2 input : TEXCOORD0) : COLOR0
-{
- return tex2D( Samp, input );
-}
+ float4 def PS(input = float2.TEXCOORD0) COLOR0
+ return tex2D( Samp, input )
+ end
-technique
-{
- pass
- {
- VertexShader = compile vs_2_0 VS();
- PixelShader = compile ps_2_0 PS();
- }
-}
-EOS
+ technique [:VS, :PS]
+end
-core = Shader::Core.new(hlsl, {:g_world=>:float, :g_view=>:float, :g_proj=>:float})
+core = VertexShader.core
shader = Shader.new(core)
shader.g_view = Matrix.new([1, 0, 0, 0],
require 'erb'
module DXRuby
class Hlsl
class << self
def inherited(klass)
klass.instance_variable_set :@parser, Parser.new(klass)
end
def float2(*names)
float 2, *names
end
def float3(*names)
float 3, *names
end
def float4(*names)
float 4, *names
end
def float4x4(*names)
float "4x4", *names
end
def float(*names)
set_type :float, *names
end
def texture(*names)
set_type :texture, *names
end
def set_type(type, *names)
@parser.set_type type, *names
self
end
def TEXCOORD0
:TEXCOORD0
end
def COLOR0
:COLOR0
end
def POSITION
:POSITION
end
def struct(o)
type = @parser.struct
define_singleton_method(type) do |*names|
set_type type, *names
end
end
def sampler(name)
@parser.sampler name
end
def technique(name, func = nil)
@parser.technique name, func
end
def core
Shader::Core.new(@parser.shader, @parser.table)
end
end
class Parser
def initialize(klass)
@klass = klass
@src_table = {}
@var_table = {}
@func_table = {}
@samp_table = {}
@strc_table = {}
@tech_table = {}
end
def sampler(name)
@samp_table[name] = decompress(name) do |src|
src.gsub!(/@| *#.*/, "")
src.gsub!(/$/, ";")
src.chop!
src.sub!(/\A(\s*)(?:sampler\s+)?def\s+(\w+);/) { "#{$1}sampler #{$2} = sampler_state\n#{$1}{" }
src.sub!(/^(\s*Texture\s*=\s*)(.*?)(\s*;)/) {"#{$1}<#{$2}>#{$3}"} ||
src.sub!(/^(\s*)({.*)\n/) { "#{$1}#{$2}\n #{$1}Texture =<tex0>;\n" }
src.sub!(/^(\s*)end(\s.*)?;/) { "#{$1}};" }
end
end
def set_type(type, *names)
if names.first.is_a?(Integer) || names.first =~ /^\d/
size = names.shift
else
size = nil
end
names.each do |name|
if @klass.method_defined? name
@func_table[name] = decompress(name) do |src|
src.gsub!(/@| *#.*/, "")
src.gsub!(/$/, ";")
src.gsub!(/\s*\\;$/, "")
src.chop!
src.tr!("[]", "{}")
src.gsub!(/([0-9])\.f/) { "#{$1}f" }
src.gsub!(/^\s*;$/, "")
src.sub!(/\A(\s*)(?:\w+\s+)?def\s+\w+\((.*)\)\s*(\w+)?\s*;/) do
indent, args, ret = $1, $2, $3
args.gsub!(".", " : ")
args.gsub!(/(\w+)\s*=\s*(\w+)/) { "#{$2} #{$1}" }
ret = " : #{ret}" if ret
"#{indent}#{type}#{size} #{name}(#{args})#{ret} {"
end
src.sub!(/^(\s*)end(\s.*)?;/) { "#{$1}}" }
end
else
@var_table[name] = [type, size]
end
end
end
def struct
caller[1][/(.*):(\d+):/]
path, at = $1, $2.to_i
src = src(path, at) {|l, indent| l =~ /^#{indent}}/ }
name = src[/\sstruct\s+([^\s]+)/, 1]
src.gsub!(/(\s*)(.*?\s*):(?:(.+)\.)?(\w+)\s*,?/) { "#{$1}#{$3} #{$2}:#{$4};" }
src.sub!(/\s*=\s*$/, "")
src.sub!(/}/, "};")
@strc_table[name] = src
name
end
def technique(name, func)
if !func
func = name
name = ""
end
@tech_table[name] = func
end
def shader
ERB.new(<<-EOS, nil, "-").result(binding)
texture tex0;
<%- @var_table.each do |name, (type, size)| -%>
<%= type %><%= size %> <%= name %>;
<%- end -%>
<%- @samp_table.each do |name, src| -%>
<%= src %>
<%- end -%>
<%- @strc_table.each do |name, src| -%>
<%= src %>
<%- end -%>
<%- @func_table.each do |name, src| -%>
<%= src %>
<%- end -%>
<%- @tech_table.each do |name, func| -%>
technique <%= name %>
{
<%- if func.is_a? Array -%>
pass
{
VertexShader = compile vs_2_0 <%= func[0] %>();
PixelShader = compile ps_2_0 <%= func[1] %>();
}
<%- else -%>
pass P0
{
PixelShader = compile ps_2_0 <%= func %>();
}
<%- end %>
}
<%- end -%>
EOS
end
def table
t = {}
@var_table.each do |name, (type, size)|
t[name] = type
end
t
end
private
def init
@stack = []
@state = nil
@type = nil
end
def decompress(target)
init
fname, at = @klass.instance_method(target).source_location
path = File.expand_path(fname)
yield src(path, at) {|l, indent| l =~ /^#{indent}end/ }
end
def src(path, at)
lines = @src_table[path] ||= File.readlines(path)
index = at - 1
src = lines[index]
indent = src[/^ */]
until yield lines[index], indent
index += 1
src << lines[index]
end
src
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment