mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
internal/graphicsdriver/directx: revert changes to remove GetCopyableFootprints
This reverts these commits *ccddd4ee20
*8e40c2ce7e
*2eb1efe3cc
Without GetCopyableFootprints, the test results on 32bit Windows seemed much more flaky. This didn't fix the memory issue like #2294 anyway.
This commit is contained in:
parent
ccddd4ee20
commit
cfdf59ef8d
@ -59,7 +59,6 @@ const (
|
|||||||
_D3D12_SDK_VERSION = (_D3D12_SDK_VERSION_MAJOR << 16) | _D3D12_SDK_VERSION_MINOR
|
_D3D12_SDK_VERSION = (_D3D12_SDK_VERSION_MAJOR << 16) | _D3D12_SDK_VERSION_MINOR
|
||||||
_D3D12_SDK_VERSION_MAJOR = 2
|
_D3D12_SDK_VERSION_MAJOR = 2
|
||||||
_D3D12_SDK_VERSION_MINOR = 4
|
_D3D12_SDK_VERSION_MINOR = 4
|
||||||
_D3D12_TEXTURE_DATA_PITCH_ALIGNMENT = 256
|
|
||||||
_D3D12XBOX_DEFAULT_SIZE_BYTES = 0xffffffff
|
_D3D12XBOX_DEFAULT_SIZE_BYTES = 0xffffffff
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1075,6 +1075,11 @@ func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
layouts, _, _, totalBytes := g.device.GetCopyableFootprints(&desc, 0, 1, 0)
|
||||||
|
if totalBytes == ^uint64(0) {
|
||||||
|
return nil, fmt.Errorf("directx: GetCopyableFootprints returned an invalid total bytes")
|
||||||
|
}
|
||||||
|
|
||||||
i := &Image{
|
i := &Image{
|
||||||
graphics: g,
|
graphics: g,
|
||||||
id: g.genNextImageID(),
|
id: g.genNextImageID(),
|
||||||
@ -1082,6 +1087,8 @@ func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) {
|
|||||||
height: height,
|
height: height,
|
||||||
texture: t,
|
texture: t,
|
||||||
states: [frameCount]_D3D12_RESOURCE_STATES{state},
|
states: [frameCount]_D3D12_RESOURCE_STATES{state},
|
||||||
|
layouts: layouts,
|
||||||
|
totalBytes: totalBytes,
|
||||||
}
|
}
|
||||||
g.addImage(i)
|
g.addImage(i)
|
||||||
return i, nil
|
return i, nil
|
||||||
@ -1453,6 +1460,8 @@ type Image struct {
|
|||||||
states [frameCount]_D3D12_RESOURCE_STATES
|
states [frameCount]_D3D12_RESOURCE_STATES
|
||||||
texture *_ID3D12Resource
|
texture *_ID3D12Resource
|
||||||
stencil *_ID3D12Resource
|
stencil *_ID3D12Resource
|
||||||
|
layouts _D3D12_PLACED_SUBRESOURCE_FOOTPRINT
|
||||||
|
totalBytes uint64
|
||||||
uploadingStagingBuffer *_ID3D12Resource
|
uploadingStagingBuffer *_ID3D12Resource
|
||||||
readingStagingBuffer *_ID3D12Resource
|
readingStagingBuffer *_ID3D12Resource
|
||||||
rtvDescriptorHeap *_ID3D12DescriptorHeap
|
rtvDescriptorHeap *_ID3D12DescriptorHeap
|
||||||
@ -1499,42 +1508,28 @@ func (*Image) IsInvalidated() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) ensureUploadingStagingBuffer() (*_ID3D12Resource, error) {
|
func (i *Image) ensureUploadingStagingBuffer() error {
|
||||||
// Unlike readingStagingBuffer, uploadingStagingBuffer's lifetime is not determined and cannot recreate easily.
|
|
||||||
// Then, keep using the same uploadingStagingBuffer for one image.
|
|
||||||
|
|
||||||
if i.uploadingStagingBuffer != nil {
|
if i.uploadingStagingBuffer != nil {
|
||||||
return i.uploadingStagingBuffer, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
var err error
|
||||||
iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
|
i.uploadingStagingBuffer, err = createBuffer(i.graphics.device, i.totalBytes, _D3D12_HEAP_TYPE_UPLOAD)
|
||||||
alignedWidth := align(4 * iw)
|
|
||||||
size := alignedWidth * ih
|
|
||||||
|
|
||||||
usb, err := createBuffer(i.graphics.device, uint64(size), _D3D12_HEAP_TYPE_UPLOAD)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
i.uploadingStagingBuffer = usb
|
return nil
|
||||||
return usb, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) ensureReadingStagingBuffer(size uint64) (*_ID3D12Resource, error) {
|
func (i *Image) ensureReadingStagingBuffer() error {
|
||||||
if i.readingStagingBuffer != nil && i.readingStagingBuffer.GetDesc().Width < size {
|
|
||||||
i.readingStagingBuffer.Release()
|
|
||||||
i.readingStagingBuffer = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if i.readingStagingBuffer != nil {
|
if i.readingStagingBuffer != nil {
|
||||||
return i.readingStagingBuffer, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
var err error
|
||||||
rsb, err := createBuffer(i.graphics.device, size, _D3D12_HEAP_TYPE_READBACK)
|
i.readingStagingBuffer, err = createBuffer(i.graphics.device, i.totalBytes, _D3D12_HEAP_TYPE_READBACK)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
i.readingStagingBuffer = rsb
|
return nil
|
||||||
return rsb, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) ReadPixels(buf []byte, x, y, width, height int) error {
|
func (i *Image) ReadPixels(buf []byte, x, y, width, height int) error {
|
||||||
@ -1546,9 +1541,7 @@ func (i *Image) ReadPixels(buf []byte, x, y, width, height int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
alignedWidth := align(4 * width)
|
if err := i.ensureReadingStagingBuffer(); err != nil {
|
||||||
rsb, err := i.ensureReadingStagingBuffer(uint64(alignedWidth * height))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1556,24 +1549,15 @@ func (i *Image) ReadPixels(buf []byte, x, y, width, height int) error {
|
|||||||
i.graphics.copyCommandList.ResourceBarrier([]_D3D12_RESOURCE_BARRIER_Transition{rb})
|
i.graphics.copyCommandList.ResourceBarrier([]_D3D12_RESOURCE_BARRIER_Transition{rb})
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := rsb.Map(0, &_D3D12_RANGE{0, 0})
|
m, err := i.readingStagingBuffer.Map(0, &_D3D12_RANGE{0, 0})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dst := _D3D12_TEXTURE_COPY_LOCATION_PlacedFootPrint{
|
dst := _D3D12_TEXTURE_COPY_LOCATION_PlacedFootPrint{
|
||||||
pResource: rsb,
|
pResource: i.readingStagingBuffer,
|
||||||
Type: _D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
|
Type: _D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
|
||||||
PlacedFootprint: _D3D12_PLACED_SUBRESOURCE_FOOTPRINT{
|
PlacedFootprint: i.layouts,
|
||||||
Offset: 0,
|
|
||||||
Footprint: _D3D12_SUBRESOURCE_FOOTPRINT{
|
|
||||||
Format: _DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
||||||
Width: uint32(width),
|
|
||||||
Height: uint32(height),
|
|
||||||
Depth: 1,
|
|
||||||
RowPitch: uint32(alignedWidth),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
src := _D3D12_TEXTURE_COPY_LOCATION_SubresourceIndex{
|
src := _D3D12_TEXTURE_COPY_LOCATION_SubresourceIndex{
|
||||||
pResource: i.texture,
|
pResource: i.texture,
|
||||||
@ -1598,14 +1582,14 @@ func (i *Image) ReadPixels(buf []byte, x, y, width, height int) error {
|
|||||||
var dstBytes []byte
|
var dstBytes []byte
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&dstBytes))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&dstBytes))
|
||||||
h.Data = uintptr(m)
|
h.Data = uintptr(m)
|
||||||
h.Len = alignedWidth * height
|
h.Len = int(i.totalBytes)
|
||||||
h.Cap = alignedWidth * height
|
h.Cap = int(i.totalBytes)
|
||||||
|
|
||||||
for j := 0; j < height; j++ {
|
for j := 0; j < height; j++ {
|
||||||
copy(buf[j*width*4:(j+1)*width*4], dstBytes[j*alignedWidth:])
|
copy(buf[j*width*4:(j+1)*width*4], dstBytes[j*int(i.layouts.Footprint.RowPitch):])
|
||||||
}
|
}
|
||||||
|
|
||||||
rsb.Unmap(0, nil)
|
i.readingStagingBuffer.Unmap(0, nil)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -1619,8 +1603,7 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
usb, err := i.ensureUploadingStagingBuffer()
|
if err := i.ensureUploadingStagingBuffer(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1628,46 +1611,32 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error {
|
|||||||
i.graphics.copyCommandList.ResourceBarrier([]_D3D12_RESOURCE_BARRIER_Transition{rb})
|
i.graphics.copyCommandList.ResourceBarrier([]_D3D12_RESOURCE_BARRIER_Transition{rb})
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err := usb.Map(0, &_D3D12_RANGE{0, 0})
|
m, err := i.uploadingStagingBuffer.Map(0, &_D3D12_RANGE{0, 0})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
i.graphics.needFlushCopyCommandList = true
|
i.graphics.needFlushCopyCommandList = true
|
||||||
|
|
||||||
iw, ih := graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
|
|
||||||
alignedWidth := align(4 * iw)
|
|
||||||
|
|
||||||
var srcBytes []byte
|
var srcBytes []byte
|
||||||
h := (*reflect.SliceHeader)(unsafe.Pointer(&srcBytes))
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&srcBytes))
|
||||||
h.Data = uintptr(m)
|
h.Data = uintptr(m)
|
||||||
h.Len = alignedWidth * ih
|
h.Len = int(i.totalBytes)
|
||||||
h.Cap = alignedWidth * ih
|
h.Cap = int(i.totalBytes)
|
||||||
for _, a := range args {
|
for _, a := range args {
|
||||||
for j := 0; j < a.Height; j++ {
|
for j := 0; j < a.Height; j++ {
|
||||||
copy(srcBytes[(a.Y+j)*alignedWidth+a.X*4:], a.Pixels[j*a.Width*4:(j+1)*a.Width*4])
|
copy(srcBytes[(a.Y+j)*int(i.layouts.Footprint.RowPitch)+a.X*4:], a.Pixels[j*a.Width*4:(j+1)*a.Width*4])
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, a := range args {
|
|
||||||
dst := _D3D12_TEXTURE_COPY_LOCATION_SubresourceIndex{
|
dst := _D3D12_TEXTURE_COPY_LOCATION_SubresourceIndex{
|
||||||
pResource: i.texture,
|
pResource: i.texture,
|
||||||
Type: _D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
|
Type: _D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
|
||||||
SubresourceIndex: 0,
|
SubresourceIndex: 0,
|
||||||
}
|
}
|
||||||
src := _D3D12_TEXTURE_COPY_LOCATION_PlacedFootPrint{
|
src := _D3D12_TEXTURE_COPY_LOCATION_PlacedFootPrint{
|
||||||
pResource: usb,
|
pResource: i.uploadingStagingBuffer,
|
||||||
Type: _D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
|
Type: _D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
|
||||||
PlacedFootprint: _D3D12_PLACED_SUBRESOURCE_FOOTPRINT{
|
PlacedFootprint: i.layouts,
|
||||||
Offset: 0,
|
|
||||||
Footprint: _D3D12_SUBRESOURCE_FOOTPRINT{
|
|
||||||
Format: _DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
||||||
Width: uint32(iw),
|
|
||||||
Height: uint32(ih),
|
|
||||||
Depth: 1,
|
|
||||||
RowPitch: uint32(alignedWidth),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
i.graphics.copyCommandList.CopyTextureRegion_SubresourceIndex_PlacedFootPrint(
|
i.graphics.copyCommandList.CopyTextureRegion_SubresourceIndex_PlacedFootPrint(
|
||||||
&dst, uint32(a.X), uint32(a.Y), 0, &src, &_D3D12_BOX{
|
&dst, uint32(a.X), uint32(a.Y), 0, &src, &_D3D12_BOX{
|
||||||
@ -1680,7 +1649,7 @@ func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
usb.Unmap(0, nil)
|
i.uploadingStagingBuffer.Unmap(0, nil)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -2046,7 +2015,3 @@ func (s *Shader) uniformsToFloat32s(uniforms [][]float32) []float32 {
|
|||||||
}
|
}
|
||||||
return fs
|
return fs
|
||||||
}
|
}
|
||||||
|
|
||||||
func align(x int) int {
|
|
||||||
return (((x - 1) / _D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) + 1) * _D3D12_TEXTURE_DATA_PITCH_ALIGNMENT
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user