Avantage de directx 11 sur unity ?

Questions à propos du scripting Shader.
Palmier97
Messages : 59
Inscription : 16 Oct 2015 18:46

Avantage de directx 11 sur unity ?

Message par Palmier97 » 24 Jan 2016 11:12

Bonjour, c'est quoi l'avantage de directx 11 si je l'utilise dans unity, meilleur optimisation ou de jolie graphisme ? (En ce moment je suis sur directx 9 mais si je l'utilises le directx 9, elle compatible pour les pc d'aujourd'hui non ?)

Avatar de l’utilisateur
ZJP
Messages : 5745
Inscription : 15 Déc 2009 06:00

Re: Avantage de directx 11 sur unity ?

Message par ZJP » 27 Jan 2016 18:22

Palmier97 a écrit :Bonjour, c'est quoi l'avantage de directx 11 si je l'utilise dans unity, meilleur optimisation ou de jolie graphisme ? (En ce moment je suis sur directx 9 mais si je l'utilises le directx 9, elle compatible pour les pc d'aujourd'hui non ?)
- Dx9 : Fonctionne avec du matériel compatible Dx10, Dx11 et Dx12, y compris avec les "veilles" configurations. Mon choix. Je teste mes projets avec une configuration équipé d'une GeForce 9800GT. Celle-ci :
:mrgreen: :mrgreen:
Image

- Dx11 : la Tesselation !!! et les "Compute Shaders".


Pour..."simplifier". ;-)

Ah oui, en tant que développeur, l’accès direct aux pointeurs des textures via une DLL externe est aussi plus facile sous DX9. 8-) 8-)


Un exemple des "trucs" sur lequel je "perds" du temps (quand j'en ai) :-D

Code : Tout sélectionner

// OPEN CV Unity (c) ZJP

#define EXPORT_API __declspec(dllexport)

#include <math.h>
#include <stdio.h>
#include <d3d9.h>

#include <cv.h>
#include <highgui.h>

#include "isl.h"

static float g_Time;

IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;

// declaration des textures ************************************
// declaration des textures ************************************
// WebCam Input !!!
static void*       Webcam_TexturePointer;
IDirect3DTexture9* WebCam_d3dtex;
D3DSURFACE_DESC    WebCam_desc;
D3DLOCKED_RECT     WebCam_lr;
// Texture 1 Input
static void*       Tex1_TexturePointer;
IDirect3DTexture9* Tex1_d3dtex;
D3DSURFACE_DESC    Tex1_desc;
D3DLOCKED_RECT     Tex1_lr;
// Texture 2 Input
static void*       Tex2_TexturePointer;
IDirect3DTexture9* Tex2_d3dtex;
D3DSURFACE_DESC    Tex2_desc;
D3DLOCKED_RECT     Tex2_lr;
// Texture 3 Input
static void*       Tex3_TexturePointer;
IDirect3DTexture9* Tex3_d3dtex;
D3DSURFACE_DESC    Tex3_desc;
D3DLOCKED_RECT     Tex3_lr;
// Texture 4 Input
static void*       Tex4_TexturePointer;
IDirect3DTexture9* Tex4_d3dtex;
D3DSURFACE_DESC    Tex4_desc;
D3DLOCKED_RECT     Tex4_lr;
// Texture 5 Input
static void*       Tex5_TexturePointer;
IDirect3DTexture9* Tex5_d3dtex;
D3DSURFACE_DESC    Tex5_desc;
D3DLOCKED_RECT     Tex5_lr;

// flag init des textures **********************
static int WebcamTex_init = 0;
static int Tex1_Init  = 0;
static int Tex2_Init  = 0;
static int Tex3_Init  = 0;
static int Tex4_Init  = 0;
static int Tex5_Init  = 0;
// declaration des textures ************************************
// declaration des textures ************************************

extern "C" int EXPORT_API OCV_InitTexture (void* texturePtr, int texNumber, int tWidth, int tHeight )
{
    if (texNumber == 0)
    {
        Webcam_TexturePointer = texturePtr;
        WebCam_d3dtex = (IDirect3DTexture9*)Webcam_TexturePointer;
        WebCam_d3dtex->GetLevelDesc (0, &WebCam_desc);
        WebcamTex_init = 1;

//****************************************************************************************************
// OPEN CV !!!
//****************************************************************************************************
        image = cvCreateImage(cvSize(tWidth,tHeight), IPL_DEPTH_8U, 4);
        cedge = cvCreateImage(cvSize(tWidth,tHeight), IPL_DEPTH_8U, 4);
        // Convert to grayscale
        gray = cvCreateImage(cvSize(tWidth,tHeight), IPL_DEPTH_8U, 1);
        edge = cvCreateImage(cvSize(tWidth,tHeight), IPL_DEPTH_8U, 1);
//****************************************************************************************************
//****************************************************************************************************
        /* Initialize ISL library */
        ISL_Initialize();
//****************************************************************************************************
//****************************************************************************************************
        return 0;
    }

    if (texNumber == 1)
    {
        Tex1_TexturePointer = texturePtr;
        Tex1_d3dtex = (IDirect3DTexture9*)Tex1_TexturePointer;
        Tex1_d3dtex->GetLevelDesc (0, &Tex1_desc);
        Tex1_Init = 1;
        return 0;
    }

    if (texNumber == 2)
    {
        Tex2_TexturePointer = texturePtr;
        Tex2_d3dtex = (IDirect3DTexture9*)Tex2_TexturePointer;
        Tex2_d3dtex->GetLevelDesc (0, &Tex2_desc);
        Tex2_Init = 1;
        return 0;
    }

    if (texNumber == 3)
    {
        Tex3_TexturePointer = texturePtr;
        Tex3_d3dtex = (IDirect3DTexture9*)Tex3_TexturePointer;
        Tex3_d3dtex->GetLevelDesc (0, &Tex3_desc);
        Tex3_Init = 1;
        return 0;
    }

    if (texNumber == 4)
    {
        Tex4_TexturePointer = texturePtr;
        Tex4_d3dtex = (IDirect3DTexture9*)Tex4_TexturePointer;
        Tex4_d3dtex->GetLevelDesc (0, &Tex4_desc);
        Tex4_Init = 1;
        return 0;
    }

    return 1; // error
}

extern "C" float EXPORT_API OCV_CopyTexture(int texOrigine, int texDestination, int directCopy)
{
	float t1 = cvGetTickCount();

	WebCam_d3dtex->LockRect (0, &WebCam_lr, NULL, 0);
	Tex1_d3dtex->LockRect (0, &Tex1_lr, NULL, 0);

//******************************************************************************************************************
//******************************************************************************************************************
    image->imageData = (char*) WebCam_lr.pBits;

    cvCvtColor(image, gray, CV_BGR2GRAY);
    cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
    cvNot( gray, edge );
    cvCanny(gray, edge, 20, 20*3, 3);
    cvZero( cedge );
    cvCopy( image, cedge, edge );

	// cvShowImage(wndname, cedge); // resultat !!!!
    int iwd   = cedge->widthStep;
    char *idt = cedge->imageData;
    int channels  = cedge->nChannels;


//**********************************************************
// ISL *****************************************************
//**********************************************************
    //cvCopy( image, cedge);
    /* Add "LINES" effect to the script */
    //ISL_AddEffect(ISL_INVERSION,true);
    //ISL_SetEffectValue(ISL_Effect1,ISL_Value4,20);

    //ISL_ExecuteScriptOnImage(cedge->imageData,WebCam_desc.Width,WebCam_desc.Height);
    //ISL_Finish();

//********************************************************************
//********************************************************************
// Direct COPY !!!!!
    if (directCopy)
    {

        unsigned char* ptrDes = (unsigned char*)Tex1_lr.pBits;
        memcpy(ptrDes, idt, WebCam_desc.Width * WebCam_desc.Height * 4 );

    } else
    {
        // Idividual ACCESS !!!!!
        for (int y = 0; y < WebCam_desc.Height; ++y)
        {
            unsigned char* ptrDes = (unsigned char*)Tex1_lr.pBits;
            for (int x = 0; x < WebCam_desc.Width; ++x)
            {
                ptrDes[0] = idt[y*iwd+x*channels];   // Pixel Bleu
                ptrDes[1] = idt[y*iwd+x*channels+1]; // Pixel Vert
                ptrDes[2] = idt[y*iwd+x*channels+2]; // Pixel Rouge
                ptrDes[3] = 255; // idt[y*iwd+x*channels+3]=255; // alpha
                // To next pixel (our pixels are 4 bpp 32B)
                ptrDes += 4;
            }
            Tex1_lr.pBits += WebCam_lr.Pitch; // Tex1_lr.pBits += Tex1_lr.Pitch;
        }
    }

	WebCam_d3dtex->UnlockRect (0);
	Tex1_d3dtex->UnlockRect (0);

	return (cvGetTickCount()-t1)/((float)cvGetTickFrequency()*1000.0);// temps en Ms
}

extern "C" float EXPORT_API OCV_PaurexTexture(int texOrigine, int texDestination)
{
	float t1 = cvGetTickCount();

	WebCam_d3dtex->LockRect (0, &WebCam_lr, NULL, 0);
	Tex1_d3dtex->LockRect (0, &Tex1_lr, NULL, 0);

    image->imageData = (char*) WebCam_lr.pBits;

    cvCopy( image, cedge);
    /* Add "LINES" effect to the script */
    ISL_AddEffect(ISL_INVERSION,true);
    //ISL_SetEffectValue(ISL_Effect1,ISL_Value4,20);

    ISL_ExecuteScriptOnImage(cedge->imageData,WebCam_desc.Width,WebCam_desc.Height);
    ISL_Finish();

    unsigned char* ptrDes = (unsigned char*)Tex1_lr.pBits;

    memcpy(ptrDes, cedge->imageData, WebCam_desc.Width * WebCam_desc.Height * 4);

	WebCam_d3dtex->UnlockRect (0);
	Tex1_d3dtex->UnlockRect (0);

	return (cvGetTickCount()-t1)/((float)cvGetTickFrequency()*1000.0);// temps en Ms
}

extern "C" void EXPORT_API OCV_ClearTexture()
{
    cvReleaseImage(&image);
    cvReleaseImage(&gray);
    cvReleaseImage(&cedge);
    cvReleaseImage(&edge);
}

ou

Code : Tout sélectionner

// *****************************************************************
// *****************************************************************
// FFMpeg to Unity3D DLL (c) ZJP
// *****************************************************************
// *****************************************************************
#define EXPORT_DLL __declspec(dllexport)

#ifdef __cplusplus
extern "C"
{
#endif
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
    //#include <libavutil/imgutils.h>
    //#include <libavutil/samplefmt.h>
    //#include <libavutil/timestamp.h>
#ifdef __cplusplus
}
#endif

#include <stdio.h>
#include <d3d9.h>

//#include <windows.h>

// compatibility with newer API
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
	#define av_frame_alloc avcodec_alloc_frame
	#define av_frame_free avcodec_free_frame
#endif

static void*       Tex1_TexturePointer;
IDirect3DTexture9* Tex1_d3dtex;
D3DSURFACE_DESC    Tex1_desc;
D3DLOCKED_RECT     Tex1_lr;
unsigned char*     ptrDes;

// Initalizing these to NULL prevents segfaults!
AVFormatContext   *pFormatCtx = NULL;
int               i, videoStream;
AVCodecContext    *pCodecCtxOrig = NULL;
AVCodecContext    *pCodecCtx = NULL;
AVCodec           *pCodec = NULL;
AVFrame           *pFrame = NULL;
AVFrame           *pFrameRGB = NULL;
AVPacket          packet;
int               frameFinished;
int               numBytes;
uint8_t           *buffer = NULL;
struct SwsContext *sws_ctx = NULL;

AVStream          *st;

int lecture;
int actualRead;

extern "C" int EXPORT_DLL FFMPEG_InitTexture (void* texturePtr, int tWidth, int tHeight )
{
    Tex1_TexturePointer = texturePtr;
    Tex1_d3dtex = (IDirect3DTexture9*)Tex1_TexturePointer;
    Tex1_d3dtex->GetLevelDesc (0, &Tex1_desc);

    return 0;
}


void FFMPEG_CopyTexture(AVFrame *pFrame, int width, int height)
{
	Tex1_d3dtex->LockRect (0, &Tex1_lr, NULL, 0);

    for (int y = 0; y < height; ++y)
    {
        unsigned char* ptrDes = (unsigned char*)Tex1_lr.pBits;
        for (int x = 0; x < width; ++x)
        {
            ptrDes[0] = pFrame->data[0][y * pFrame->linesize[0] + x * 3 + 0];
            ptrDes[1] = pFrame->data[0][y * pFrame->linesize[0] + x * 3 + 1];
            ptrDes[2] = pFrame->data[0][y * pFrame->linesize[0] + x * 3 + 2];
           // ptrDes[3] = 0; // alpha
            // To next pixel (our pixels are 4 bpp 32B)
            ptrDes += 4;
        }
        Tex1_lr.pBits += Tex1_lr.Pitch;
    }

	Tex1_d3dtex->UnlockRect (0);
}

extern "C" void EXPORT_DLL FFMPEG_CloseMovie()
{

   av_free_packet(&packet);
       // Free the RGB image
    av_free(buffer);
    av_frame_free(&pFrameRGB);

    // Free the YUV frame
    av_frame_free(&pFrame);

    // Close the codecs
    avcodec_close(pCodecCtx);
    avcodec_close(pCodecCtxOrig);

	// youpiiiiiii !!!!
    avcodec_close(st->codec);

    // Close the video file
    avformat_close_input(&pFormatCtx);

}


//extern "C" int EXPORT_DLL FFMPEG_PlayMovie(char *film)
extern "C" int EXPORT_DLL FFMPEG_OpenMovie(const char* movie)
{
	// Register all formats and codecs
	av_register_all();

	pFormatCtx = avformat_alloc_context();

	// Open video file
	if(avformat_open_input(&pFormatCtx, movie, NULL, NULL)!= 0 ) return 1; // Couldn't open file

	// Retrieve stream information
	if(avformat_find_stream_info(pFormatCtx, NULL) < 0) return 2; // Couldn't find stream information

	// Find the first video stream
	videoStream = -1;
	for(i = 0; i < pFormatCtx->nb_streams; i++)
	{
		if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
		{
			videoStream = i;
			break;
		}
	}
	if(videoStream == -1) return 3; // Didn't find a video stream

	// youpiiiiii
	st = pFormatCtx->streams[videoStream];

	// Get a pointer to the codec context for the video stream
	pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;

	// Find the decoder for the video stream
	pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id);
	if(pCodec == NULL) return 4; // Unsupported codec

	// Copy context
	pCodecCtx = avcodec_alloc_context3(pCodec);
	if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) return 5; // Couldn't copy codec context

	// Open codec
	if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0) return 6; // Could not open codec

	// Allocate video frame
	pFrame = av_frame_alloc();

	// Allocate an AVFrame structure
	pFrameRGB = av_frame_alloc();
	if(pFrameRGB == NULL) return 7;

	// Determine required buffer size and allocate buffer
	numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
	buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

	// Assign appropriate parts of buffer to image planes in pFrameRGB
	// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
	// of AVPicture
	avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);

	// initialize SWS context for software scaling
	sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BILINEAR,  NULL, NULL, NULL );

	return 0;
}

extern "C" int EXPORT_DLL FFMPEG_PlayMovie()
{
    actualRead = av_read_frame(pFormatCtx, &packet);
    if (actualRead  >= 0)
    {
        // Is this a packet from the video stream?
        if(packet.stream_index == videoStream)
        {
            // Decode video frame
            avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

            // Did we get a video frame?
            if(frameFinished)
            {
                // Convert the image from its native format to RGB
                sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data,  pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
//                FFMPEG_CopyTexture(pFrameRGB, pCodecCtxOrig->width, pCodecCtxOrig->height);
                FFMPEG_CopyTexture(pFrameRGB, pCodecCtx->width, pCodecCtx->height);

            }
        }

        // Free the packet that was allocated by av_read_frame
        av_free_packet(&packet);
    }

    return actualRead;
}

extern "C" int EXPORT_DLL FFMPEG_GetWidth()
{
//    return pCodecCtxOrig->width;
    return pCodecCtx->width;
}

extern "C" int EXPORT_DLL FFMPEG_GetHeight()
{
 //   return pCodecCtxOrig->height;
    return pCodecCtx->height;
}

extern "C" int EXPORT_DLL FFMPEG_GetFrameNumber()
{
    return (int)st->nb_frames;
}

extern "C" float EXPORT_DLL FFMPEG_FrameRate()
{
    return st->r_frame_rate.num / (double)st->r_frame_rate.den;
}




extern "C" int EXPORT_DLL test_array(int n, float anArray[])
{
    for (int i = 0; i < n; i++) anArray[i] = 99.0 + i;
    return sizeof(anArray) * n;
}

Possible grace à DX9. Faisable bien sur sous DX11.

Palmier97
Messages : 59
Inscription : 16 Oct 2015 18:46

Re: Avantage de directx 11 sur unity ?

Message par Palmier97 » 27 Jan 2016 18:51

Ha oui la bonne GeForce 9800GT, mais je vais rester sur dx9 comme sa, sa tourne bien sur les vieux pc.

Répondre

Revenir vers « les Shaders »