Learn about DirectX Problems

Source: Internet
Author: User
1 maxsimultaneoustextures texture simultaneously overlays the maximum number of devices-> getdevicecaps (& caps );
Int maxactivetextures = caps. maxsimultaneoustextures; that is, the maximum number of textures that can be set before the drawprimitive vertex. If your machine's video card is poor, this number is 2, that is, it just supports multiple textures.
As follows: Code As shown in, if maxsimultaneoustextures <4, the image cannot be output. Note: even if the texture is the same, the number is one. If the texture effect is greater than that of maxsimultaneoustextures, you can call the drawprimitive Function Multiple times to achieve the same effect. However, it may affect the speed. For example, quake3 can render up to 10 pd3ddevice-> settexture (0, g_pbackgroundtexture );
Pd3ddevice-> settexturestagestate (0, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (0, d3dtss_colodrop, d3dtop_selectarg1); pd3ddevice-> settexture (1, g_pwalltexture );
Pd3ddevice-> settexturestagestate (1, d3dtss_texcoordindex, 0 );
Pd3ddevice-> settexturestagestate (1, d3dtss_colodrop, d3dtop_modulate );
Pd3ddevice-> settexturestagestate (1, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (1, d3dtss_colorarg2, d3dta_current); pd3ddevice-> settexture (2, g_penvtexture );
Pd3ddevice-> settexturestagestate (2, d3dtss_texcoordindex, 0 );
Pd3ddevice-> settexturestagestate (2, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (2, d3dtss_colorarg2, d3dta_current );
Pd3ddevice-> settexturestagestate (2, d3dtss_colodrop, d3dtop_add); pd3ddevice-> settexture (3, g_penvtexture );
Pd3ddevice-> settexturestagestate (3, d3dtss_texcoordindex, 0 );
Pd3ddevice-> settexturestagestate (3, d3dtss_colorarg1, d3dta_texture );
Pd3ddevice-> settexturestagestate (3, d3dtss_colorarg2, d3dta_current );
Pd3ddevice-> settexturestagestate (3, d3dtss_colodrop, d3dtop_add); pd3ddevice-> setfvf (fvf );
Pd3ddevice-> setstreamsource (0, g_pvb, 0, sizeof (vertex ));
Pd3ddevice-> drawprimitive (d3dpt_trianglestrip, 0, 2); 2 the d3dpt_trianglestrip sign of the drawprimitive () function uses the first triangle of the vertex to clockwise, and the second to counterclockwise .. Iterations. For example, remember the UV sequence ()-> () vertex vertices [] = {
{0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f ,},
{Width, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f ,},
{0.0f, height, 0.5f, 1.0f, 0.0f, 1.0f,}, // X, Y, Z, rhw, tu, TV
{Width, height, 0.5f, 1.0f, 1.0f, 1.0f ,},
}; 3 get the window height and width idirect3dsurface9. Note that the idirect3dsurface9 height and width members are both 1. # Ifdef d3d_debug_info contains these members. The surface attribute should be obtained through getdesc, and the height and width in d3dsurface_desc are correct. Idirect3dsurface9 * m_d3dsdbackbuffer; pd3ddevice-> getbackbuffer (0, 0, d3dbackbuffer_type_mono, & m_d3dsdbackbuffer );
// Int Height = m_d3dsdbackbuffer-> height;
// Int width = m_d3dsdbackbuffer-> width; m_d3dsdbackbuffer-> getdesc (pbackbuffersurfacedesc)
Int Height = pbackbuffersurfacedesc-> height;
Int width = pbackbuffersurfacedesc-> width;
M_d3dsdbackbuffer-> release (); // you must release it. 4. Specify the d3dcreate_puredevice flag when creating a device. See d3dpresent_parameters parameter description 5. Invalid texture Area
By specifying invalid areas for the texture, applyProgramWe can optimize the subsets of the texture to be copied. Only those areas marked as invalid will be Idirect3ddevice9: updatetextureMethod update. When a texture is created, the entire texture is marked as invalid. Only the following operations can change the invalid texture status.

    • Add an invalid region to a texture.
    • lock some areas in the texture. This operation adds the locked area to the invalid area. If the application knows which areas are actually invalid, it can also disable automatic updates to the invalid area.
    • If the texture is updated as the target surface, the entire texture is marked as invalid.
    • call idirect3ddevice9 :: the updatetexture method clears all invalid regions of the texture.
    • device context.

ForIn terms of the mipmap texture, the invalid region is located on the top level texture. To minimize the number of bytes that need to be copied for each level of texture update in the mipmap texture,Idirect3ddevice9: updatetextureThe method can expand the invalid region and update the sub-texture along the mipmap chain. Note that the texture coordinates of invalid areas in the child level are rounded up, that is, their decimal points are rounded up to the nearest pixel in the texture.

Because each type of texture has different types of invalid areas, each type of texture has a corresponding method to indicate invalid areas. Two-dimensional textures use Rectangles and stereo texturesCubeBody.

    • idirect3dcubetexture9: adddirtyrect
    • idirect3dtexture9: adddirtyrect
    • idirect3dvolumetexture9: adddirtybox

set pdirtyrect or pdirtybox parameter is set to null, the invalid area is expanded and the entire texture is overwritten.
Each locking method has the d3dlock_no_dirty_update flag, which can be used to prevent changes to areas with invalid textures. For more information, see lock resources .
if you can obtain a complete set of changed regions during the lock operation, then the application should use the d3dlock_no_dirty_update flag. Note that a sub-level lock or copy operation (that is, the maximum level of the texture is not locked or copied) will not update the invalid region of the texture. When the application locks the sub-level of the texture but does not lock the highest level of the texture, it also has the responsibility to update invalid areas.

6. Why didn't setfvf be used for fixed pipelines?
I saw that setfvf is not used in dxquake3, but there is no vertex or pixel shader. In fact, fvf is converted to vertexdeclaration by DX internally, so use fvf or vertexdeclaration.Setvertexdeclaration and setvertexshader are used instead of setfvf. setvertexdeclaration declares the data storage format. setvertexshader provides the data processing method.. Setfvf is a new function of DX9 to simplify writing. The vertex format provided by setvertexdeclaration in dxquake3. function parameters are similar
D3dvertexelement9 Decl [] =
{
{0, 0, d3ddecltype_float3, d3ddeclmethod_default, d3ddeclusage_position, 0 },
{0, 12, d3ddecltype_d3dcolor, d3ddeclmethod_default, d3ddeclusage_color, 0 },
D3ddecl_end ()
};
However, setvertexshader is not used to set the shader. Compared with setfvf, setvertexdeclaration is more conducive to future expansion and implements programmable shader.

7. failed to call converttoblendedmesh using mesh. If you set d3dcreate_hardware_vertexprocessing when creating a device, it is likely that the vertex fails because it is not enough.
It is better to use the d3dcreate_mixed_vertexprocessing parameter (in the case of skinmesh noindex ).

8. Add d3dx9dt. lib instead of d3dx9. Lib. In this way, memory leakage will be reported for resources without release () (call _ crtsetdbgflag ). Only Memory leakage libraries such as VLD can detect objects without release.

9 key colors
When loading images:
D3dxcreatetexturefromfileex (m_pd3ddevice, "test.bmp", d3dx_default, d3dx_default, 1, 0, d3dfmt_unknown,
D3dpool_default, d3dx_filter_linear, d3dx_filter_linear, 0xffff00ff, null, null, & m_texture );
~~~~~~~~~~ Colorkey
During rendering:
Before rendering
M_pd3ddevice-> setrenderstate (d3drs_alphablendenable, true );
M_pd3ddevice-> setrenderstate (d3drs_srcblend, d3dblend_srcalpha); // use the loaded image srcalpha
M_pd3ddevice-> setrenderstate (d3drs_destblend, d3dblend_invsrcalpha); // use 1-srcalpha
Rendering
Render ()
Restore after rendering
M_pd3ddevice-> setrenderstate (d3drs_alphablendenable, false );
M_pd3ddevice-> setrenderstate (d3drs_srcblend, d3dblend_one );
M_pd3ddevice-> setrenderstate (d3drs_destblend, d3dblend_zero );
It is estimated that the color is the same when loading. Set the pixel Alpha to 0.

10
Lock vertexbuffer. Use the offset address to specify the value d3dlock_nooverwrite at the same time. In this way, only a small part of the update is required. Other parts will not be discarded. Of course, the default parameter 0 is also supported. However, it is inefficient to use 0.
For Dynamic Caching. Using an offset address does not increase efficiency. Because discard cannot be used during offset. Otherwise, unupdated parts will be discarded. However, it is often hoped that no update is retained. Update All cached locations without using the offset address. (Especially when a complete cache is updated, it can be unlocked, but it can be faster)

The above recognition is incorrect. Discard indicates that you no longer use the video memory. Give me a new one. The original position can be used by the device (if the vertex data is used up ). Data is not discarded. "Discard" means developers no longer use it. As for when to discard DirectX Management
Similarly, nooverwrite does not mean no overwrite. It only indicates that the developer thinks that the write location will not overwrite the vertex (draw) being used by dx, and does not need to wait until the video card draw ends (the default parameter 0 indicates waiting for the end ), overwrite the vertex data at the corresponding position directly (even if the vertex is being used for drawing, it will be forcibly overwritten ). The opposite is true.

If the cached data does not change. Do not update it. (Only updating changed data can speed up). A cache needs to be updated several times. You can start locking once. The last unlock is good. This does not seem to affect performance.

11 d3dxloadsurfacefrommemory. You can convert one format to another.
Idirect3dsurface9 * temp;
D3dok (m_atex [Tex]. m_ptex-> getsurfacelevel (Level, & temp ));

int W = get2powerhigh (SZ. CX); // that is, the pitch is aligned to the 32 position
// source cache size
rect srcrc = {0, 0, W, Sz. cy };< br> rect dstrc = RT;
// pixelformatbits obtains the number of bytes for each surface format. you can refer to the dxtex program such as d3dfmt_a8r8g8b8 to return 32
unsigned char bytes = pixelformatbits (PF)/8;
hresult hr;
// pitch = W * bytes must use the texture width. The width is not necessarily equal to the parameter width. The width of the fingerprint mechanism pitch rather than the buffer array
// idirect3dsurface9 is the surface of the target area, and the second can be null (the era of 256 colors has long passed ). RT is the region where the target Texture needs to be updated (the power of 2 can effectively prevent distortion)
// data is color buffered data. Each element (a8r8g8b8) represents a color value. PF data color format (for example, d3dfmt_a8r8g8b8)
// null is the color palette, and srcrc is a part of the buffer zone. D3dx_filter_none filtering mode
// The final parameter is the Key Color
If (failed (hR = d3dxloadsurfacefrommemory (temp, null, & RT, Data, PF, w * bytes, null, & srcrc, d3dx_filter_none, 0)
{< br> ri-> error (err_d3d, _ T ("failed to update texture and convert the surface format: % 0x \ n "), HR);
}

If (m_atex [Tex]. m_bmipmap)
{< br> d3dok (d3dxfiltertexture (m_atex [Tex]. m_ptex, null, 0, d3dx_default);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.