gpu

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MAX_VERTEXS  = 6144
	MAX_POLYGONS = 2048
)
View Source
const (
	GX_TEST_BUSY    = 1 << 0
	GX_TEST_RESULT  = 1 << 1
	GX_PROJ_SP      = 1 << 13
	GX_MATRIX_STACK = 1 << 14
	GX_ERROR        = 1 << 15
	GX_FIFO_HALF    = 1 << 25
	GX_FIFO_EMPTY   = 1 << 26
	GX_BUSY         = 1 << 27
)

GXSTAT (0x0400_0600)

View Source
const (
	CMD_NOP          = 0x00
	CMD_MTX_MODE     = 0x10
	CMD_MTX_PUSH     = 0x11
	CMD_MTX_POP      = 0x12
	CMD_MTX_STORE    = 0x13
	CMD_MTX_RESTORE  = 0x14
	CMD_MTX_IDENTITY = 0x15
	CMD_MTX_LOAD_4x4 = 0x16
	CMD_MTX_LOAD_4x3 = 0x17
	CMD_MTX_MULT_4x4 = 0x18
	CMD_MTX_MULT_4x3 = 0x19
	CMD_MTX_MULT_3x3 = 0x1A
	CMD_MTX_SCALE    = 0x1B
	CMD_MTX_TRANS    = 0x1C
	CMD_COLOR        = 0x20
	CMD_NORMAL       = 0x21
	CMD_TEXCOORD     = 0x22
	CMD_DIF_AMB      = 0x30
	CMD_SPE_EMI      = 0x31
	CMD_LIGHT_VECTOR = 0x32
	CMD_LIGHT_COLOR  = 0x33
	CMD_SHININESS    = 0x34
	CMD_BEGIN_VTXS   = 0x40
	CMD_END_VTXS     = 0x41
	CMD_SWAP_BUFFERS = 0x50
	CMD_VIEWPORT     = 0x60
	CMD_BOX_TEST     = 0x70
	CMD_POS_TEST     = 0x71
	CMD_VEC_TEST     = 0x72
)
View Source
const (
	SCR_W = 256
	SCR_H = 192
)
View Source
const (
	SIZE_CMD_PIPE  = 4
	SIZE_CMD_FIFO  = 256
	SIZE_CMD_FLOOD = 64
)
View Source
const (
	LCDC_TEXTURE           = 1 << 0
	LCDC_HIGHLIGHT_SHADING = 1 << 1
	LCDC_ALPHA_TEST        = 1 << 2
	LCDC_ALPHA_BLEND       = 1 << 3
	LCDC_AA                = 1 << 4
	LCDC_EDGE_MARKING      = 1 << 5
	LCDC_FOG_MODE          = 1 << 6
	LCDC_FOG               = 1 << 7
	LCDC_RDLINE            = 1 << 12
	LCDC_OVERFLOW          = 1 << 13
	LCDC_REARPLANE_BITMAP  = 1 << 14
)

DISP3DCNT(0x0400_0060)

View Source
const (
	POLY_BACK_SURFACE  = (1 << 6)
	POLY_FRONT_SURFACE = (1 << 7)
	POLY_TRANSLUCENT_Z = (1 << 11) // 半透明ピクセルを描画する時に 半透明ピクセルの深度値で 深度バッファを更新するか(0=しない, 1=する)
	POLY_FAR_PLANE     = (1 << 12) // Zクリップ遠面(+w方面)にハミ出るような木端なポリゴンを表示するかどうか
	POLY_1DOT          = (1 << 13)
	POLY_DEPTH_EQUAL   = (1 << 14)
)

POLYGON_ATTR (0x40004A4)

View Source
const (
	MODE_MODULATION = iota
	MODE_DECAL
	MODE_SHADED
	MODE_SHADOW
)

POLYGON_ATTR.4-5; 頂点カラーとテクスチャカラーの合成方法

View Source
const (
	TEX_NONE = iota
	TEX_A3I5
	TEX_2BPP
	TEX_4BPP
	TEX_8BPP
	TEX_TC4x4
	TEX_A5I3
	TEX_DIRECT
)

TEXIMAGE_PARAM.26-28

View Source
const KB = 1024
View Source
const RDLINES_COUNT = 46 // ラインバッファの空き容量

Variables

View Source
var PARAM_LENGTH = [256]int8{}/* 115 elements not displayed */

32bitのパラメータの数(なのでバイト数は PARAM_LENGTH[x] * 4)

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	Bank uint8 // 0 or 1
	// contains filtered or unexported fields
}

type EdgeFlag

type EdgeFlag = uint8 // edge's bit: 0:left, 1:right, 2:top, 3:bottom
const (
	EDGE_L EdgeFlag = 1 << 0
	EDGE_R EdgeFlag = 1 << 1
	EDGE_T EdgeFlag = 1 << 2
	EDGE_B EdgeFlag = 1 << 3
)

type GPU

type GPU struct {
	Timestamp int64 // 33MHz単位

	/*
		DISP3DCNT(0x0400_0060)
			bit0:    テクスチャマッピング
			bit1:    シェーディング(0: トゥーンシェーディング, 1: ハイライトシェーディング)
			bit2:    アルファテスト有効化
			bit3:    アルファブレンディング有効化
			bit4:    AA有効化
			bit5:    Edge-Marking有効化
			bit6:    Fog Color Mode
			bit7:    フォグ有効化
			bit8-11: Fog Depth Shift
			bit12:   Color Buffer RDLINES Underflow
			bit13:   Polygon/Vertex RAM Overflow
			bit14:	 Rear Plane Enable
	*/
	LCDC uint16

	// 昔のGPUなので、ジオメトリ処理とレンダリング処理でハードウェアが分かれている
	Gx *GeometryEngine
	R  *RenderingEngine

	RAM *Buffer // 2つのバッファ(ジオメトリエンジンとレンダリングエンジンで共有)

	FrameBuffer [2][SCR_W * SCR_H]color.NRGBA // 正確にはNDSはラインバッファ方式
	FrontBuffer uint8                         // FrameBuffer のどちらを画面に表示するか(0 or 1)
	BG0HOFS     uint16                        // BG0HOFS (0x0400_0010)

	Texture   [512 * KB]uint8 // 512KB; Texture RAM
	TexPal    [96 * KB]uint8  // 96KB; Texture Palette RAM
	ToonTable [32]uint16      // RGB555

	CC         uint32 // CLEAR_COLOR (4000350h)
	ClearDepth uint16 // CLEAR_DEPTH (4000354h)

	EdgeTable [8]uint16 // RGB555

	FogColor   uint32    // FOG_COLOR (0x4000358)
	FogOffset  uint16    // FOG_OFFSET (0x400035C)
	FogDensity [32]uint8 // FOG_TABLE (0x4000360)

	Thread *Thread
	// contains filtered or unexported fields
}

3Dエンジン, 左手座標系(Zが大きいほど奥)

Command FIFO
↓
Geometry Engine (GE)
↓
Polygon RAM, Vertex RAM
↓
Rendering Engine (RE)
↓
BG0

func New

func New(irq func(bool), dma func(cpu, mode uint8)) *GPU

func (*GPU) CyclesToRunFor

func (g *GPU) CyclesToRunFor() int64

func (*GPU) DISP3DCNT

func (g *GPU) DISP3DCNT() uint16

func (*GPU) GXSTAT

func (g *GPU) GXSTAT() uint32

func (*GPU) GetLine

func (g *GPU) GetLine(y int16, line *[SCR_W]color.NRGBA)

func (*GPU) IsCmdFIFOLessThanHalfFull

func (g *GPU) IsCmdFIFOLessThanHalfFull() bool

func (*GPU) Power

func (g *GPU) Power(geometry, rendering bool)

func (*GPU) Read16

func (g *GPU) Read16(addr uint32) uint16

func (*GPU) Read32

func (g *GPU) Read32(addr uint32) uint32

func (*GPU) Read8

func (g *GPU) Read8(addr uint32) uint8

func (*GPU) Render

func (g *GPU) Render()

func (*GPU) Reset

func (g *GPU) Reset()

func (*GPU) Run

func (g *GPU) Run(target33MHz int64)

func (*GPU) SetBG0HOFS

func (g *GPU) SetBG0HOFS(val uint16)

func (*GPU) Stall

func (g *GPU) Stall() bool

func (*GPU) SwapBuffers

func (g *GPU) SwapBuffers() bool

SwapBuffers は、ジオメトリエンジンの頂点RAM/ポリゴンRAM と レンダリングエンジンのそれ を入れ替える このとき ly = 192

func (*GPU) Write16

func (g *GPU) Write16(addr uint32, data uint16)

func (*GPU) Write32

func (g *GPU) Write32(addr uint32, data uint32)

func (*GPU) Write8

func (g *GPU) Write8(addr uint32, data uint8)

type GeometryEngine

type GeometryEngine struct {
	Enabled bool // POWCNT1.3

	/*
		GXSTAT (0x0400_0600)
			bit: Wがついている項目以外は読み取り専用
				0:     テストコマンド実行中
				1:     Boxテストの結果
				2-7:   不使用
				8-12:  モデルビュー行列スタックの使用数 (0..31)
				13:    投影行列スタックの使用数 (0 or 1)
				14:    行列スタックがビジーか (1ならPush/Popの処理中)
				15:    W 行列スタックがオーバーフロー/アンダーフローした
				16-24: GXFIFOの長さ (0..256)
				25:    GXFIFOが半分未満かどうか
				26:    GXFIFOが空かどうか
				27:    ジオメトリエンジンがビジーかどうか(ビジー: コマンド実行中)
				30-31: W GXFIFO IRQタイミング (0: なし, 1: 半分以下, 2: 空)
	*/
	Stat uint32

	/*
		MTX_MODE(=glMatrixMode(param))
			0: 投影変換行列(GL_PROJECTION)
			1: モデルビュー変換行列(GL_MODELVIEW)
			2: Position & Vector Simultaneous Set mode
			3: Texture Matrix (see DS 3D Texture Coordinates chapter)
	*/
	MtxMode uint8

	PolygonMode uint8 // BEGIN_VTXS(=glBegin(param))で設定する頂点座標のタイプ; 0: 三角形, 1: 四角形, 2: 三角形ストリップ, 3: 四角形ストリップ

	MVP        [4 * 4]int20x12 // mvpMatrix = modelviewMatrix * projectionMatrix
	MVPUpdated bool            // mvpMatrix は modelviewMatrix と projectionMatrix の積だが、どちらかが変更されたときに再計算する(ことで無駄な計算を省く, 実際にハードウェアでもそのようになっている)

	Viewport             Viewport
	TexParam, TexpalBase uint32 // TEXIMAGE_PARAM, PLTT_BASE

	VertexColor uint16 // RGB555

	// ライティング
	Lights                               [4]Light // 光源
	Diffuse, Ambient, Specular, Emission uint16   // RGB555; Diffuse: 拡散反射光, Ambient: 環境光, Specular: 鏡面反射光, Emission: エミッション

	Swap Swap

	VecTestResult [3]int1x12 // 整数部分はない [-1.0, +1.0)

	PushPopCount, TestCount int16 // まだ実行されていない テストコマンド,PushPopコマンド とそのパラメータ のエントリ数(GXSTAT.0, GXSTAT.14 のために必要)
	// contains filtered or unexported fields
}

func (*GeometryEngine) MVPMatrix

func (g *GeometryEngine) MVPMatrix() *[4 * 4]int32

type Light

type Light struct {
	X, Y, Z int16  // 9bit fraction
	Color   uint16 // RGB555
}

type Polygon

type Polygon struct {
	Vertices [10]*vertex // 頂点RAMのインデックス(0..6143), クリッピングによる分割を考慮して10個確保

	Z, W [10]int32 // 深度値(wは16bitに丸められてる, zを深度値として使う場合でもパースペクティブ補正にwが必要)

	/*
		POLYGON_ATTR
			bit0-3:   ライト0-3の有効化
			bit4-5:   モード (0: モジュレーション, 1: デカール, 2: トゥーン/ハイライトシェーディング, 3: シャドウ)
			bit6-7:   [Back, Front]
			bit11:    このポリゴンの半透明ピクセルを描画する時に深度バッファを更新するか (0=Keep Old, 1=Set New Depth)
			bit12:    Far-plane intersecting polygons       (0=Hide, 1=Render/clipped)
			bit13:    このポリゴンを描画したときに1ピクセルになる場合、描画するかどうか (0: しない, 1: する)
			bit14:    Depth Test, Draw Pixels with Depth    (0=Less, 1=Equal) (usually 0)
			bit15:    フォグ有効化
			bit16-20: アルファ値 (0: ワイヤーフレーム, 1..30: 半透明, 31: ソリッド)
			bit24-29: 0..63 (ポリゴンID)
	*/
	Attr uint32

	MinY, MaxY int32 // ポリゴンの最上部と最下部のY座標(スクリーン座標, 0..191, 0が上, 191が下)
	// contains filtered or unexported fields
}

クリッピングされる場合、複数のポリゴンに分割されるが、それら全部をこの構造体でまとめて扱う

func (*Polygon) UV

func (p *Polygon) UV(x, y int) (int, int)

type RenderingEngine

type RenderingEngine struct {
	Enabled          bool   // POWCNT1.2
	ClearImageOffset uint16 // CLRIMAGE_OFFSET (4000356h)

	BackColorBuffer [SCR_W * SCR_H]color.NRGBA // バックカラーバッファ (不透明ポリゴンの後ろの色を保持する, AAで使用)
	DepthBuffer     [SCR_W * SCR_H]uint32      // 深度バッファ
	/*
		ピクセルの属性を保持するバッファ(melonDSのものを参考にしている)

		bit:
			0-3:   そのピクセルがポリゴンのエッジかどうか (0: left, 1: right, 2: top, 3: bottom)
			4:     カメラから見てポリゴンが裏を向いているかどうか
			8-12:  AC (Anti-Aliasing factor, 0..31, 0に近いほどエッジが透明になる)
			15:    フォグが有効かどうか
			16-21: (半透明ピクセルの場合の)ポリゴンID
			22:    そのピクセルが半透明かどうか
			24-29: (不透明ピクセルの場合の)ポリゴンID (POLYGON_ATTRでポリゴンIDがbit24-29なので合わせてる)
	*/
	AttrBuffer    [SCR_W * SCR_H]uint32
	StencilBuffer [SCR_W * SCR_H]uint8 // ステンシルバッファ (影の描画でのみ)

	// レンダリング関連のレジスタは(SwapBuffersコマンド発行直後の)VBlank時に、GPUからコピーされる
	LCDC       uint16 // DISP3DCNT
	AlphaRef   uint8
	CC         uint32
	ClearDepth uint16

	EdgeTable  [8]([3]uint8) // RGB888
	FogColor   uint32
	FogOffset  uint32    // FOG_OFFSET * 0x200
	FogShift   uint8     // (DISP3DCNT >> 8) & 0xF; よく使うのでキャッシュしておく
	FogDensity [34]uint8 // 線形補間のため、(32 + 2)個

	// デバッグ用のフラグ
	Debug struct {
		Fog, EdgeMarking, AA bool // 各ポストプロセスを強制的に無効化するためのフラグ
	}
	// contains filtered or unexported fields
}

RenderingEngine (RE)

 Stage1
	Rasterize
	Texture-Mapping
	α-Blending
 ↓
 Stage2
	Edge-Marking
	Fog
 ↓
 Stage3
	Anti-Aliasing

func (*RenderingEngine) CalculateFogDensity

func (r *RenderingEngine) CalculateFogDensity(depth uint32) uint8

func (*RenderingEngine) PowerOff

func (r *RenderingEngine) PowerOff()

func (*RenderingEngine) Render

func (r *RenderingEngine) Render(frameBuffer *[SCR_W * SCR_H]color.NRGBA)

func (*RenderingEngine) Reset

func (r *RenderingEngine) Reset()

func (*RenderingEngine) Set

func (r *RenderingEngine) Set(x, y int, z uint32, c color.NRGBA, edge EdgeFlag)

edge's bit: 0:left, 1:right, 2:top, 3:bottom

type Swap

type Swap struct {
	Param uint8 // SWAP_BUFFERSのパラメータ(デバッグ用)

	Pending bool
	// contains filtered or unexported fields
}

type Thread

type Thread struct {
	// contains filtered or unexported fields
}

func NewThread

func NewThread(fn func()) *Thread

func (*Thread) Request

func (t *Thread) Request()

func (*Thread) Reset

func (t *Thread) Reset()

func (*Thread) Wait

func (t *Thread) Wait()

type Viewport

type Viewport struct {
	X, Y uint32 // (0, 0) = 左"上"の座標になることに注意 // (width, height) が右"下"
	// contains filtered or unexported fields
}

Directories

Path Synopsis
render

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL