imported>Administrator |
imported>Administrator |
1行目: |
1行目: |
− | 1つの[[ポリゴンメッシュ]]を[[GPU]]側で複製することで[[Drawコール]]を減らして描画を高速化する「[[ハードウェアインスタンシング]]」という技術がある。
| + | '''社畜PC'''(しゃちくぴーしー)とは、[[ブラック企業]]が[[社畜]]に配布することを想定して開発および販売されている[[PC]]のことである。 |
| | | |
− | ただ[[MonoGame]]と[[OpenGL]]の組み合わせの環境では長らく「new NotImplementedException()」であった。
| + | == 概要 == |
| + | 一般的には「これ[[スマホ]]よりも性能悪いんじゃないのか」という限界まで「安さ」だけを追求した[[社畜]]を酷使するための[[PC]]である。 |
| | | |
− | これがついにMonoGame 3.7で[[OpenGL]]環境でも[[ハードウェアインスタンシング]]が使えるようになったそうだ。さっそく[[MacOS]]上で試してみた。[[ハードウェアインスタンシング]]が使えるとなると[[ボリュームレンダリング]]の実装が捗る可能性がある。
| + | [[CPU]]などは数字を大きく見せる目的で結構マシなものが採用される傾向があるが、[[キーボード]]や[[マウス]]、[[液晶]]などの数字で表現しにくいパーツは人体に悪影響のあるレベルで限界に挑戦したものが多い。 |
| | | |
− | ==HLSLを書く==
| + | 一方で歴史ある[[ソフトウェア]]の開発会社などの[[IT企業]]では「[[iMac 5K]]よりも高価で低性能」な[[富士通]]や[[NEC]]など[[PC]]を採用し[[ITドカタ]]に配給していることが多い。いわゆる[[大人の事情]]である。あきらかに意味不明な価格であっても買わなければならないのである。それが大人である。 |
− | [[HLSL]]はこんな感じ。今回は移動だけで回転はしていない。 [[Mac]]上で[[HLSL]]を[[コンパイル]]する方法は「 [[MonoGameでプログラマブルシェーダーを使う]]」および「[[InfinitespaceStudios.Pipeline]]」の項目を参照。 | |
| | | |
− | <syntaxhighlight lang="text">
| + | ==デスクトップ== |
− | // file: effect4.fx
| |
| | | |
− | // ----------------------------------------------------------------
| + | *[[PCケース]]は[[スリムタワー]] |
− | // MonoGame PipelineでOpenGL環境の場合は「OPENGL」シンボルが立っている。
| + | *[[CPU]]は[[セレロン]](CPUは数字で表現できるので、ここだけCore i7というのも多い) |
− | // 以下は定型文だと思ってコピペしとけ。
| + | *[[HDD]]は500GB(最近は[[HDD]]並に遅い[[SSD]]も人気) |
− | #if OPENGL
| + | *[[メモリ]]は4GB |
− | #define SV_POSITION POSITION
| + | *[[液晶]]は目潰し |
− | #define VS_SHADERMODEL vs_3_0
| + | *[[MIMO]]非対応の[[WiFi]] |
− | #define PS_SHADERMODEL ps_3_0
| + | *腱鞘炎になる[[キーボード]] |
− | #else
| + | *保証期間が過ぎると爆発炎上する[[電解コンデンサー]] |
− | #define VS_SHADERMODEL vs_4_0_level_9_1
| |
− | #define PS_SHADERMODEL ps_4_0_level_9_1
| |
− | #endif
| |
| | | |
− | // ----------------------------------------------------------------
| + | ==ノートPC== |
− | // 呼び出し側(C#)から設定されるグローバル変数(シェーダー内では実質定数)
| |
− | float4x4 myView : VIEW;
| |
− | float4x4 myProjection : PROJECTION;
| |
| | | |
− | // ----------------------------------------------------------------
| + | *[[レッツノート]]を抱えて颯爽と新幹線に乗り込み、[[ドヤ顔]]で[[パワポ]]を編集する、そんな[[社畜]]に、私はなりたい |
− | // 入出力用の構造体
| + | *[[液晶]]の[[解像度]]は1366x768 |
− | struct VertexPositionColor
| |
− | {
| |
− | float4 Position : POSITION0;
| |
− | float4 Color : COLOR;
| |
− | };
| |
− | | |
− | // ----------------------------------------------------------------
| |
− | // バーテックスシェーダー
| |
− | VertexPositionColor MyVertexShader(
| |
− | VertexPositionColor input,
| |
− | float3 position : POSITION1
| |
− | )
| |
− | {
| |
− | // inputの位置をpositionほど移動させる。
| |
− | // 今回は移動だけ。
| |
− | // 回転したい場合なども同じ要領でここで変形させる。
| |
− | input.Position.xyz += position;
| |
− | | |
− | // 頂点をカメラから見た座標に変換する。
| |
− | input.Position = mul( input.Position, mul(myView, myProjection));
| |
− | return input;
| |
− | }
| |
− | | |
− | // ----------------------------------------------------------------
| |
− | // ピクセルシェーダー
| |
− | float4 MyPixelShader(float4 color : COLOR) : COLOR0
| |
− | {
| |
− | return color;
| |
− | }
| |
− | | |
− | technique HardwareInstancing
| |
− | {
| |
− | pass Pass1
| |
− | {
| |
− | VertexShader = compile VS_SHADERMODEL MyVertexShader();
| |
− | PixelShader = compile PS_SHADERMODEL MyPixelShader();
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | ==C#で呼び出してみる==
| |
− | この[[コード]]は「[[MonoGameのカメラを作る]]」で作ったカメラを使っている。Cameraクラスは[[MonoGame]]標準物ではないので注意。
| |
− | | |
− | <syntaxhighlight lang="csharp">
| |
− | using System;
| |
− | using System.Collections.Generic;
| |
− | using System.Text;
| |
− | using Microsoft.Xna.Framework;
| |
− | using Microsoft.Xna.Framework.Graphics;
| |
− | using Microsoft.Xna.Framework.Input;
| |
− | | |
− | public class Game1 : Game
| |
− | {
| |
− | GraphicsDeviceManager graphics;
| |
− | | |
− | Camera camera;
| |
− | Effect effect;
| |
− | VertexBuffer triangleVertexBuffer;
| |
− | IndexBuffer indexBuffer;
| |
− | DynamicVertexBuffer instanceVertexBuffer;
| |
− | | |
− | VertexPositionColor[] vertices = {
| |
− | new VertexPositionColor(new Vector3(-1, 1, 0), Color.Blue ),
| |
− | new VertexPositionColor(new Vector3( 1, 1, 0), Color.White),
| |
− | new VertexPositionColor(new Vector3( 1, -1, 0), Color.Red )
| |
− | };
| |
− | | |
− | Vector3[] instances = new Vector3[] {
| |
− | new Vector3(0f, 0f, 0f),
| |
− | new Vector3(1f, 1f, 0f),
| |
− | new Vector3(2f, 2f, 0f)
| |
− | };
| |
− | | |
− | public Game1()
| |
− | {
| |
− | graphics = new GraphicsDeviceManager(this);
| |
− | Content.RootDirectory = "Content";
| |
− | }
| |
− | | |
− | protected override void Initialize()
| |
− | {
| |
− | IsMouseVisible = true;
| |
− | base.Initialize();
| |
− | }
| |
− | | |
− | protected override void LoadContent()
| |
− | {
| |
− | // 即席カメラ
| |
− | camera = new Camera(this);
| |
− | camera.Position = new Vector3(0, 0, 10);
| |
− | camera.Target = new Vector3(0, 0, 0);
| |
− | | |
− | // エフェクト読み込み
| |
− | effect = Content.Load<Effect>("effect4");
| |
− | | |
− | // バーテックスバッファーを生成
| |
− | triangleVertexBuffer = new VertexBuffer(
| |
− | GraphicsDevice,
| |
− | VertexPositionColor.VertexDeclaration,
| |
− | vertices.Length,
| |
− | BufferUsage.WriteOnly);
| |
− | triangleVertexBuffer.SetData<VertexPositionColor>(vertices);
| |
− | | |
− | // インデックスバッファーを生成
| |
− | var index = new ushort[] { 0, 1, 2 };
| |
− | indexBuffer = new IndexBuffer(
| |
− | GraphicsDevice,
| |
− | IndexElementSize.SixteenBits,
| |
− | index.Length,
| |
− | BufferUsage.WriteOnly);
| |
− | indexBuffer.SetData<ushort>(index);
| |
− | | |
− | // インスタンスバッファーを生成
| |
− | instanceVertexBuffer = new DynamicVertexBuffer(
| |
− | GraphicsDevice,
| |
− | new VertexDeclaration(
| |
− | new VertexElement(
| |
− | offset: 0,
| |
− | elementFormat: VertexElementFormat.Vector3,
| |
− | elementUsage: VertexElementUsage.Position,
| |
− | usageIndex: 1)),
| |
− | instances.Length,
| |
− | BufferUsage.WriteOnly);
| |
− | instanceVertexBuffer.SetData(instances);
| |
− | | |
− | }
| |
− | | |
− | protected override void Update(GameTime gameTime)
| |
− | {
| |
− | if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
| |
− | Exit();
| |
− | | |
− | // TODO: Add your update logic here
| |
− | camera.Angle += 1f;
| |
− | | |
− | base.Update(gameTime);
| |
− | }
| |
− | | |
− | protected override void Draw(GameTime gameTime)
| |
− | {
| |
− | GraphicsDevice.Clear(Color.CornflowerBlue);
| |
− | | |
− | // シェーダーのグローバル変数に値をセットする
| |
− | effect.Parameters["myView"].SetValue(camera.View);
| |
− | effect.Parameters["myProjection"].SetValue(camera.Projection);
| |
− | | |
− | // バーテックスバッファーを転送
| |
− | GraphicsDevice.SetVertexBuffers(
| |
− | new VertexBufferBinding(triangleVertexBuffer, 0, 0),
| |
− | new VertexBufferBinding(instanceVertexBuffer, 0, 1));
| |
− | | |
− | // インデックスバッファーを転送
| |
− | GraphicsDevice.Indices = indexBuffer;
| |
− | | |
− | // インスタンスバッファーを転送
| |
− | instanceVertexBuffer.SetData(instances, 0, instances.Length, SetDataOptions.Discard);
| |
− | | |
− | // 描画
| |
− | foreach (EffectPass pass in effect.CurrentTechnique.Passes)
| |
− | {
| |
− | pass.Apply();
| |
− | | |
− | graphics.GraphicsDevice.DrawInstancedPrimitives(
| |
− | primitiveType: PrimitiveType.TriangleList,
| |
− | baseVertex: 0,
| |
− | startIndex: 0,
| |
− | primitiveCount: vertices.Length / 3,
| |
− | instanceCount: instances.Length);
| |
− | | |
− | }
| |
− | | |
− | base.Draw(gameTime);
| |
− | }
| |
− | }
| |
− | </syntaxhighlight>
| |
− | | |
− | ==Mac上で動かしてみる==
| |
− | こんな感じ。[[Mac]]でも[[HLSL]]で書いた[[シェーダー]]が使えてる。なお、[[OpenGL]]系の環境では[[シェーダーモデル]]は3.0までだ。まあSM3.0もあれば元気があれば何でもできる。
| |
− | | |
− | <movie>https://youtu.be/qU-toUOhvRI</movie>
| |
| | | |
| ==関連項目== | | ==関連項目== |
| | | |
− | *[[MonoGameでプログラマブルシェーダーを使う]] | + | *[[クオカードの歌]] |
− | *[[MonoGameでピクセルシェーダーを使ってテクスチャを貼る]]
| |
− | *[[ハードウェアインスタンシング]]
| |
− | *[[疑似インスタンシング]]
| |
− | *[[メッシュベイカー]]
| |
− | *[[ドローコール]]
| |
− | | |
− | [[category: MonoGame]]
| |
− | [[category: HLSL]]
| |