zengqh / slimdx

Automatically exported from code.google.com/p/slimdx
0 stars 0 forks source link

Refactoring to reduce code duplication #329

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
SlimDX suffers severe problems with widespread code duplication across
overloads of functions. The reason for this is that the SlimDX design goals
are that you should be able to precisely control what is passed into native
D3D functions. For example, texture creation functions take an optional
pointer to a palette. SlimDX has two overloads, one that provides a palette
and one that doesn't. The one that does pins the submitted palette and
submits the pointer to D3D; the one that doesn't passes NULL. The current
functionality was basically created by intelligent copy paste, and is
vulnerable to that only appear in some overloads.

What we need are intermediate internal functions that perform marshaling
that is shared between all overloads, but takes pre-marshaled pointers as
parameters when those parameters are optional. Returning back to the
texture example, these are VolumeTexture::FromMemory overloads:
            static VolumeTexture^ FromMemory( SlimDX::Direct3D9::Device^ device,
array<System::Byte>^ memory, int width, int height, int depth, int
levelCount, Usage usage, Format format, Pool pool, Filter filter, Filter
mipFilter, int colorKey, [Out] ImageInformation% imageInformation, [Out]
array<PaletteEntry>^% palette );
            static VolumeTexture^ FromMemory( SlimDX::Direct3D9::Device^ device,
array<System::Byte>^ memory, int width, int height, int depth, int
levelCount, Usage usage, Format format, Pool pool, Filter filter, Filter
mipFilter, int colorKey, [Out] ImageInformation% imageInformation );
            static VolumeTexture^ FromMemory( SlimDX::Direct3D9::Device^ device,
array<System::Byte>^ memory, int width, int height, int depth, int
levelCount, Usage usage, Format format, Pool pool, Filter filter, Filter
mipFilter, int colorKey );

This is refactored by adding an internal function:
            static VolumeTexture^ Internal_FromMemory( SlimDX::Direct3D9::Device^
device, array<System::Byte>^ memory, int width, int height, int depth, int
levelCount, Usage usage, Format format, Pool pool, Filter filter, Filter
mipFilter, int colorKey, D3DXIMAGE_INFO* imageInformation, PALETTEENTRY*
palette );

This function allows the vast majority of the implementation to be shared,
letting the overloads just take care of overload specific details.

This needs to be done library wide, and should really be finished by our
November milestone. This refactoring should substantially improve our code
correctness. However, do NOT start doing this until the September milestone
is out the door.

Original issue reported on code.google.com by promit....@gmail.com on 29 Aug 2008 at 7:47

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Dropping the milestone tag.

Original comment by promit....@gmail.com on 6 Nov 2008 at 10:00

GoogleCodeExporter commented 9 years ago
Closing because this is an obvious general work item.

Original comment by josh.petrie on 9 Nov 2009 at 3:05