ErrorCode Shader::compileasm(std::string vert, std::string pixel) {
    LPD3DXBUFFER code;
    LPD3DXBUFFER err;

    HRESULT result;

    /**
     * Assemble Vertex Shader
     */

    result = D3DXAssembleShader(vert.c_str(), vert.length(), NULL, NULL, 0, &code, &err);

    if (FAILED(result))
    {
        ErrorHandleCritical(mamain->err, mamain->errCrit, ErrorCompileVertexShader, result, (char*) err->GetBufferPointer());
        err->Release();

        return ErrorCompileVertexShader;
    }

    result = mamain->d3ddev->CreateVertexShader((DWORD*) code->GetBufferPointer(), &VShader);
    code->Release();

    if (FAILED(result))
        return ErrorHandleCritical(mamain->err, mamain->errCrit, ErrorCreateVertexShader, result);

    /**
    * Assemble Pixel Shader
    */

    result = D3DXAssembleShader(pixel.c_str(), pixel.length(), NULL, NULL, 0, &code, &err);

    if (FAILED(result))
    {
        ErrorHandleCritical(mamain->err, mamain->errCrit, ErrorCompilePixelShader, result, (char*) err->GetBufferPointer());
        err->Release();

        VShader->Release();
        VShader = 0;

        return ErrorCompilePixelShader;
    }

    result = mamain->d3ddev->CreatePixelShader((DWORD*)code->GetBufferPointer(), &PShader);
    code->Release();

    if (FAILED(result))
    {
        ErrorHandleCritical(mamain->err, mamain->errCrit, ErrorCreatePixelShader, result);

        VShader->Release();
        VShader = 0;

        return ErrorCreatePixelShader;
    }

    return ErrorOk;
}
Exemple #2
0
  GPUContextDX9::VertexShaderHandle GPUContextDX9::createVertexShader( const char* inSource )
  {
    IDirect3DVertexShader9* shader;
    HRESULT result;
    LPD3DXBUFFER codeBuffer;
    LPD3DXBUFFER errorBuffer;

    result = D3DXAssembleShader( inSource, strlen(inSource), NULL, NULL, 0, &codeBuffer, &errorBuffer );
    if( errorBuffer != NULL )
    {
      const char* errorMessage = (const char*)errorBuffer->GetBufferPointer();
      GPUWARN << "Vertex shader failed to compile:\n" << errorMessage;
      return NULL;
    }
    else if( FAILED(result) )
    {
      GPUWARN << "Vertex shader failed to compile.";
      return NULL;
    }

    result = _device->CreateVertexShader( (DWORD*)codeBuffer->GetBufferPointer(), &shader );
    codeBuffer->Release();

    if( FAILED(result) )
    {
      DX9WARN << "Failed to allocate vertex shader.";
      return NULL;
    }

    return (VertexShaderHandle)shader;
  }
bool CD3D8ShaderMaterialRenderer::createPixelShader(const c8* pxsh)
{
	if (!pxsh)
		return true;

#if defined( _IRR_XBOX_PLATFORM_)
	return false;
#else
	// compile shader

	LPD3DXBUFFER code = 0;
	LPD3DXBUFFER errors = 0;

	#ifdef _IRR_D3D_NO_SHADER_DEBUGGING

		// compile shader without debug info
		D3DXAssembleShader(pxsh, (UINT)strlen(pxsh), 0, 0, &code, &errors);

	#else

		// compile shader and emitt some debug informations to
		// make it possible to debug the shader in visual studio

		static int irr_dbg_file_nr = 0;
		++irr_dbg_file_nr;
		char tmp[32];
		sprintf(tmp, "irr_d3d8_dbg_shader_%d.psh", irr_dbg_file_nr);

		FILE* f = fopen(tmp, "wb");
		fwrite(pxsh, strlen(pxsh), 1, f);
		fflush(f);
		fclose(f);

		D3DXAssembleShaderFromFile(tmp, D3DXASM_DEBUG, 0, &code, &errors);
	#endif
	if (errors)
	{
		// print out compilation errors.
		os::Printer::log("Pixel shader compilation failed:", ELL_ERROR);
		os::Printer::log((c8*)errors->GetBufferPointer(), ELL_ERROR);

		if (code)
			code->Release();

		errors->Release();
		return false;
	}

	if (FAILED(pID3DDevice->CreatePixelShader((DWORD*)code->GetBufferPointer(), &PixelShader)))
	{
		os::Printer::log("Could not create pixel shader.", ELL_ERROR);
		code->Release();
		return false;
	}

	code->Release();
	return true;
#endif

}
Exemple #4
0
void* LcD3D_BuildShader(PDEV pDev, char* sStrAssem, int iLen, char* sShader)
{
	HRESULT	hr;

	DWORD dFlag = 0;
	#if defined( _DEBUG ) || defined( DEBUG )
	dFlag |= D3DXSHADER_DEBUG;
	#endif

	LPD3DXBUFFER	pAsm = NULL;
	LPD3DXBUFFER	pErr = NULL;
	void*			pShd = NULL;

	hr = D3DXAssembleShader(
			sStrAssem
		,	iLen
		,	NULL
		,	NULL
		,	dFlag
		,	&pAsm
		,	&pErr);

	if( FAILED(hr) )
	{
		if(pErr)
		{
			char* sErr = (char*)pErr->GetBufferPointer();
			LcD3D_GetDxError(hr, sErr);
			pErr->Release();
		}
		else
			LcD3D_GetDxError(hr);

		return NULL;
	}

	if( 0 == _strnicmp(sShader, "vs", 2))
	{
		PDVS pVS = NULL;
		hr = pDev->CreateVertexShader( (DWORD*)pAsm->GetBufferPointer() , &pVS);
		pShd = pVS;
	}

	else if( 0 == _strnicmp(sShader, "ps", 2))
	{
		PDPS pPS	= NULL;
		hr = pDev->CreatePixelShader( (DWORD*)pAsm->GetBufferPointer() , &pPS);
		pShd = pPS;
	}

	pAsm->Release();

	if( FAILED(hr) )
	{
		LcD3D_GetDxError(hr);
		return NULL;
	}

	return pShd;
}
Exemple #5
0
bool Viewer::loadVertexShader()
{
  g_pD3DDevice->CreateVertexDeclaration( decl, &m_pVertexDeclaration );

  // Load bumpmapping vertex shader

  LPD3DXBUFFER pCode;
  LPD3DXBUFFER pError;

  if(FAILED(D3DXAssembleShader(vertexShaderStr,sizeof(vertexShaderStr),NULL, NULL, 0,  
                          &pCode, &pError)))
  {
    	  std::cout << "Error while assembling default vertex shader:\n " << (char *)pError->GetBufferPointer();
	      pError->Release();
		  return false;	  
  }	    

  if(FAILED(g_pD3DDevice->CreateVertexShader((DWORD*)pCode->GetBufferPointer(),
                                  &m_pVS)))
  {
      pCode->Release();
	  return false;
  }
  pCode->Release();
   
  return true;
}
Exemple #6
0
//-- Start --------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
bool ShadeWorm_c::Start()
{

	char contentPath[MAX_PATH];
	getDreamContentPath((char*)&contentPath);

	char particle[MAX_PATH];
	sprintf((char*)&particle, "%s\\%s", contentPath, "Resources\\Particle.bmp");

	if (D3D_OK != D3DXCreateTextureFromFile(m_device, (char *)&particle, &m_spriteTexture))
		return false;

	if (D3D_OK  != m_device->CreateTexture(512, 512, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_bufferTexture, NULL))
		return false;

	if (D3D_OK  != m_device->CreateTexture(1, 256, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_colMapTexture, NULL))
		return false;

	if (D3D_OK  != m_device->CreateVertexBuffer(4*sizeof(TEXVERTEX), NULL, D3DFVF_TEXVERTEX, D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL))
		return false;
	
	if (S_OK != D3DXAssembleShader(m_shaderColourMapSrc, (UINT)strlen(m_shaderColourMapSrc), 0, NULL, NULL, &m_pUcode, NULL)) 						
		return false;	

	if (D3D_OK  != m_device->CreatePixelShader((DWORD*)m_pUcode->GetBufferPointer(), &m_pShader))	
		return false;	

	LoadSettings(m_saverName);

	if (m_numColMaps == 0)
		return false;

	return true;

} // Start
Exemple #7
0
bool LTPixelShaderImp::Recreate()
{
    FreeDeviceObject();

    if (NULL == m_pByteCode)
    {
        return false;
    }

    HRESULT hr;

    // Should we assemble the shader? If this is true, then the byte code must be raw text.
    if (m_bCompileShader)
    {
        // Assemble the shader.
        LPD3DXBUFFER pCode = NULL;
        hr = D3DXAssembleShader((const TCHAR*)m_pByteCode, m_ByteCodeSize, NULL, NULL, 0, &pCode, NULL);
        if (FAILED(hr))
        {
            OUTPUT_D3D_ERROR(1, hr);
            Term();
            return false;
        }

        // Create the pixel shader.
        hr = PD3DDEVICE->CreatePixelShader((const DWORD*)pCode->GetBufferPointer(), &m_pShader);
        if (FAILED(hr))
        {
            OUTPUT_D3D_ERROR(1, hr);
            Term();
            return false;
        }
    }
    else
    {
        // Create the pixel shader directly from the byte code.
        hr = PD3DDEVICE->CreatePixelShader((const DWORD*)m_pByteCode, &m_pShader);
        if (FAILED(hr))
        {
            OUTPUT_D3D_ERROR(1, hr);
            Term();
            return false;
        }
    }

    // Try to install the pixel shader once for good measure.
    hr = PD3DDEVICE->SetPixelShader(m_pShader);
    if (FAILED(hr))
    {
        // Failed to setup the shader; it isn't supported.
        OUTPUT_D3D_ERROR(1, hr);
        Term();
        return false;
    }

    // We successfully installed it, so clear it out now.
    PD3DDEVICE->SetPixelShader(NULL);

    return true;
}
Exemple #8
0
int uPixelShader::LoadPS(char *filename, char *pixelprog)
{
	int er=0;
	// Create the pixel shader.
	LPD3DXBUFFER pCode;                             // assembled shader code
	LPD3DXBUFFER pError;
	if(hPixelShader!=NULL) pd->DeletePixelShader( hPixelShader);
	if(filename==NULL)
		D3DXAssembleShader( pixelprog, strlen(pixelprog), 0, NULL, &pCode, &pError );  // assemble shader code
	else
	{
		if( D3DXAssembleShaderFromFile( filename, 0, NULL, &pCode, &pError) != D3D_OK)er=1;  // assemble shader code
	}

	if(pError!=NULL)
	{
		char text[150];
		char* error_text = new char[pError->GetBufferSize()+1];
		memcpy(error_text,pError->GetBufferPointer(),pError->GetBufferSize());
		error_text[pError->GetBufferSize()]=NULL;
		pError->Release();

		if(filename!=NULL)
			sprintf( text, "ERROR in pixel shader: %s", filename);
		else
			sprintf( text, "ERROR in pixel shader: %s");

		MessageBox(hWnd, error_text, text, MB_OK);
		delete [] error_text;
		hPixelShader = NULL;
		return 0;
	}
	else if(er==1)
	{
		MessageBox(hWnd, filename, english ? "Couldn't find file for pixel shader":"Nenajdeny subor pre pixel shader", MB_OK);
		return 0;
	}

	if(	pd->CreatePixelShader( (DWORD*)pCode->GetBufferPointer(), &hPixelShader )
		== D3D_OK )
	{
		pCode->Release();
		return 1;
	}
	pCode->Release();
	MessageBox(hWnd, filename, english ? "ERROR in pixel shader":"Chyba pri nacitavani pixel shader", MB_OK);
	return 0;
}
Exemple #9
0
static int assemble_stdin()
{
	LPD3DXBUFFER shader;
	char *buf = NULL;
	const int block = 4096;
	size_t size = 0;

	_setmode(_fileno(stdin), O_BINARY);
	while (!feof(stdin)) {
		buf = (char*)realloc(buf, size + block);
		if (!buf)
			return 1;
		size += fread(buf + size, 1, block, stdin);
		if (ferror(stdin))
			return 1;
	}

	D3DXAssembleShader(buf, size, NULL, NULL, 0, &shader, NULL);

	_setmode(_fileno(stdout), O_BINARY);
	fwrite(shader->GetBufferPointer(), 1, shader->GetBufferSize(), stdout);

	return 0;
}
Exemple #10
0
HRESULT SampleShaderPluginVertexShader::InitValid(Mesh *mesh, INode *node) 
{
	
	HRESULT hr = S_OK;
	m_pINode = node;
	
    // Create a vertex shader for doing the effect
	
	LPD3DXBUFFER *ppCode = &pCode;
	
    LPD3DXBUFFER pBuffer = NULL;
	
	//! VertexShader Declarations
	
	
	//! VertexShader Constants
	Constants.SetCount(20);
	
	//! set up the material vertex color ... 
    D3DMATERIAL8 mtrl;
	ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) );
	
	mtrl.Diffuse.r = mtrl.Ambient.r = m_MtlColor.r;
	mtrl.Diffuse.g = mtrl.Ambient.g = m_MtlColor.g;
	mtrl.Diffuse.b = mtrl.Ambient.b = m_MtlColor.b;
	mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
    pd3dDevice->SetMaterial( &mtrl );
    pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
    pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );	
	/*!
	* D3DXAssembleShader
	* Assembles an ASCII description of a shader into binary form, where the shader source is in memory.
	*
	* @param pSrcData : [in] Pointer to the source code. 
	* @param SrcDataLen : [in] Size of the source code, in bytes. 
	* @param Flags : [in] A combination of the following flags, specifying assembly options. 
	* D3DXASM_DEBUG Inserts debugging information as comments in the assembled shader. 
	* D3DXASM_SKIPVALIDATION Do not validate the generated code against known capabilities and constraints. This option is recommended only when assembling a shader you know will function (that is, the shader has been assembled before without this option.) 
	* @param ppConstants : [out] Returns a pointer to an ID3DXBuffer interface, representing the returned constant declarations. These constants are returned as a vertex shader declaration fragment. It is up to the application to insert the contents of this buffer into their declaration. For pixel shaders this parameter is meaningless because constant declarations are included in the assembled shader. This parameter is ignored if it is NULL. 
	* @param ppCompiledShader : [out] Returns a pointer to an ID3DXBuffer interface, representing the returned compiled object code. This parameter is ignored if it is NULL. 
	* @param ppCompilationErrors : [out] Returns a pointer to an ID3DXBuffer interface, representing the returned ASCII error messages. This parameter is ignored if it is NULL. 
	*
	* @return HRESULT  : 
	
	  HRESULT D3DXAssembleShader(
	  LPCVOID pSrcData,
	  UINT SrcDataLen,
	  DWORD Flags,
	  LPD3DXBUFFER* ppConstants,
	  LPD3DXBUFFER* ppCompiledShader,
	  LPD3DXBUFFER* ppCompilationErrors
	  );
	  
	*/
    //! Specify the vertex format that the vertex shader will be using for doing the effect
    DWORD dwVertexFormatDefinition[] =
    {
        D3DVSD_STREAM( 0 ),	D3DVSD_REG( 0, D3DVSDT_FLOAT3 ),     // v0 = Position
		D3DVSD_REG( 1, D3DVSDT_FLOAT3 ),     // v1 = Normal
		D3DVSD_END()
    };	
	
#if 0
	TCHAR *vertexShaderPath = FindMapFile("ambient.njv");	
	hr = D3DXAssembleShaderFromFile(vertexShaderPath , 0, NULL, &pCode, NULL);
#else
	hr = D3DXAssembleShader( SurfaceShader , sizeof(SurfaceShader)-1 , 0 , NULL , &pCode , NULL ); 
#endif
	
	hr = pd3dDevice->CreateVertexShader(dwVertexFormatDefinition,(LPDWORD)pCode->GetBufferPointer(), &dwVertexShader, 0);
	
	initDone = true;
	
	return hr;
	
}
SDL_Renderer *
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
{
    SDL_VideoDisplay *display = window->display;
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
    SDL_Renderer *renderer;
    D3D_RenderData *data;
    HRESULT result;
    D3DPRESENT_PARAMETERS pparams;
    IDirect3DSwapChain9 *chain;
    D3DCAPS9 caps;

    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    if (!renderer) {
        SDL_OutOfMemory();
        return NULL;
    }

    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
    if (!data) {
        D3D_DestroyRenderer(renderer);
        SDL_OutOfMemory();
        return NULL;
    }
    data->d3d = videodata->d3d;

    videodata->render = RENDER_D3D;

    renderer->DisplayModeChanged = D3D_DisplayModeChanged;
    renderer->CreateTexture = D3D_CreateTexture;
    renderer->QueryTexturePixels = D3D_QueryTexturePixels;
    renderer->SetTexturePalette = D3D_SetTexturePalette;
    renderer->GetTexturePalette = D3D_GetTexturePalette;
    renderer->SetTextureColorMod = D3D_SetTextureColorMod;
    renderer->SetTextureAlphaMod = D3D_SetTextureAlphaMod;
    renderer->SetTextureBlendMode = D3D_SetTextureBlendMode;
    renderer->SetTextureScaleMode = D3D_SetTextureScaleMode;
    renderer->UpdateTexture = D3D_UpdateTexture;
    renderer->LockTexture = D3D_LockTexture;
    renderer->UnlockTexture = D3D_UnlockTexture;
    renderer->DirtyTexture = D3D_DirtyTexture;
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
    renderer->RenderDrawLines = D3D_RenderDrawLines;
    renderer->RenderDrawRects = D3D_RenderDrawRects;
    renderer->RenderFillRects = D3D_RenderFillRects;
    renderer->RenderCopy = D3D_RenderCopy;
    renderer->RenderReadPixels = D3D_RenderReadPixels;
    renderer->RenderWritePixels = D3D_RenderWritePixels;
    renderer->RenderPresent = D3D_RenderPresent;
    renderer->DestroyTexture = D3D_DestroyTexture;
    renderer->DestroyRenderer = D3D_DestroyRenderer;
    renderer->info = D3D_RenderDriver.info;
    renderer->window = window;
    renderer->driverdata = data;

    renderer->info.flags = SDL_RENDERER_ACCELERATED;

    SDL_zero(pparams);
    pparams.BackBufferWidth = window->w;
    pparams.BackBufferHeight = window->h;
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
        pparams.BackBufferFormat =
            PixelFormatToD3DFMT(window->fullscreen_mode.format);
    } else {
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
    }
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
        pparams.BackBufferCount = 2;
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
        pparams.BackBufferCount = 3;
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
    } else if (flags & SDL_RENDERER_PRESENTCOPY) {
        pparams.BackBufferCount = 1;
        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
    } else {
        pparams.BackBufferCount = 1;
        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    }
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
        pparams.Windowed = FALSE;
        pparams.FullScreen_RefreshRateInHz =
            window->fullscreen_mode.refresh_rate;
    } else {
        pparams.Windowed = TRUE;
        pparams.FullScreen_RefreshRateInHz = 0;
    }
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
    } else {
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    }

    data->adapter = D3D_FindAdapter(videodata->d3d, display);
    IDirect3D9_GetDeviceCaps(videodata->d3d, data->adapter,
                             D3DDEVTYPE_HAL, &caps);

    result = IDirect3D9_CreateDevice(videodata->d3d, data->adapter,
                                     D3DDEVTYPE_HAL,
                                     windowdata->hwnd,
                                     (caps.
                                      DevCaps &
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                     &pparams, &data->device);
    if (FAILED(result)) {
        D3D_DestroyRenderer(renderer);
        D3D_SetError("CreateDevice()", result);
        return NULL;
    }
    data->beginScene = SDL_TRUE;

    /* Get presentation parameters to fill info */
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
    if (FAILED(result)) {
        D3D_DestroyRenderer(renderer);
        D3D_SetError("GetSwapChain()", result);
        return NULL;
    }
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
    if (FAILED(result)) {
        IDirect3DSwapChain9_Release(chain);
        D3D_DestroyRenderer(renderer);
        D3D_SetError("GetPresentParameters()", result);
        return NULL;
    }
    IDirect3DSwapChain9_Release(chain);
    switch (pparams.SwapEffect) {
    case D3DSWAPEFFECT_COPY:
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
        break;
    case D3DSWAPEFFECT_FLIP:
        switch (pparams.BackBufferCount) {
        case 2:
            renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
            break;
        case 3:
            renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
            break;
        }
        break;
    case D3DSWAPEFFECT_DISCARD:
        renderer->info.flags |= SDL_RENDERER_PRESENTDISCARD;
        break;
    }
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
    }
    data->pparams = pparams;

    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
    renderer->info.max_texture_width = caps.MaxTextureWidth;
    renderer->info.max_texture_height = caps.MaxTextureHeight;

    /* Set up parameters for rendering */
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
    IDirect3DDevice9_SetFVF(data->device,
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
                                    D3DCULL_NONE);
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
    /* Enable color modulation by diffuse color */
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
                                          D3DTOP_MODULATE);
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
                                          D3DTA_TEXTURE);
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
                                          D3DTA_DIFFUSE);
    /* Enable alpha modulation by diffuse alpha */
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
                                          D3DTOP_MODULATE);
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
                                          D3DTA_TEXTURE);
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
                                          D3DTA_DIFFUSE);
    /* Disable second texture stage, since we're done */
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
                                          D3DTOP_DISABLE);
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
                                          D3DTOP_DISABLE);

    {
#ifdef ASSEMBLE_SHADER
        const char *shader_text =
"ps_1_1\n"
"def c0, 0, 0, 0, 0.496\n"
"def c1, 0, 0, 0, 1\n"
"def c2, 0, 0, 0, -1\n"
"tex t0\n"
"mul r1, t0, v0\n"
"add r0, r1, c0\n"
"cnd r0, r0.a, c1, c2\n"
"add r0, r0, r1\n";
        LPD3DXBUFFER pCode;         // buffer with the assembled shader code
        LPD3DXBUFFER pErrorMsgs;    // buffer with error messages
        LPDWORD shader_data;
        DWORD   shader_size;
        result = D3DXAssembleShader( shader_text, SDL_strlen(shader_text), NULL, NULL, 0, &pCode, &pErrorMsgs );
        if (FAILED(result)) {
            D3D_SetError("D3DXAssembleShader()", result);
        }
        shader_data = (DWORD*)pCode->lpVtbl->GetBufferPointer(pCode);
        shader_size = pCode->lpVtbl->GetBufferSize(pCode);
#else
        const DWORD shader_data[] = {
            0xffff0101,0x00000051,0xa00f0000,0x00000000,0x00000000,0x00000000,
            0x3efdf3b6,0x00000051,0xa00f0001,0x00000000,0x00000000,0x00000000,
            0x3f800000,0x00000051,0xa00f0002,0x00000000,0x00000000,0x00000000,
            0xbf800000,0x00000042,0xb00f0000,0x00000005,0x800f0001,0xb0e40000,
            0x90e40000,0x00000002,0x800f0000,0x80e40001,0xa0e40000,0x00000050,
            0x800f0000,0x80ff0000,0xa0e40001,0xa0e40002,0x00000002,0x800f0000,
            0x80e40000,0x80e40001,0x0000ffff
        };
#endif
        result = IDirect3DDevice9_CreatePixelShader(data->device, shader_data, &data->ps_mask);
        if (FAILED(result)) {
            D3D_SetError("CreatePixelShader()", result);
        }
    }

    return renderer;
}
HRESULT U2Dx9ShaderMgr::LoadAndCreateShaderFromMemory( const char * program_asm_code,	// ASCII assembly code
													 const TCHAR * shader_name,
													 U2Dx9ShaderDesc::ShaderType eShaderType,
													 ShaderIndex * outIndex )
{
	HRESULT hr = S_OK;
	FAIL_IF_NULL( program_asm_code );
	FAIL_IF_NULL( outIndex );

	//@@@@@ check if shader exists!

	U2Dx9ShaderDesc*	pDesc = U2_NEW U2Dx9ShaderDesc;
	pDesc->m_szShaderDefFile = TEXT("");
	pDesc->m_type					= eShaderType;
	pDesc->m_szShaderObjFile		= shader_name;

	//	typedef struct D3DXMACRO {
	//		LPCSTR Name;
	//		LPCSTR Definition;	};
	LPD3DXBUFFER pbufShader, pbufErrors;

	hr = D3DXAssembleShader( program_asm_code, 
		(UINT) strlen( program_asm_code ),
		NULL,		// D3DXMACRO preprocessor definitions
		NULL,		// include directives
		NULL,		// compile option flags
		& pbufShader,
		& pbufErrors );
	if( FAILED( hr ))
	{
		char * pfunc = "D3DXAssembleShader";
		switch( hr )
		{
		case D3DERR_INVALIDCALL : 
			FMsg("%s failed with HRESULT = D3DERR_INVALIDCALL\n", pfunc );
			break;
		case D3DXERR_INVALIDDATA : 
			FMsg("%s failed with HRESULT = D3DXERR_INVALIDDATA\n", pfunc );
			break;
		case E_OUTOFMEMORY : 
			FMsg("%s failed with HRESULT = E_OUTOFMEMORY\n", pfunc );
			break;
		default : 
			FMsg("Unknown HRESULT : %u\n", hr );
			break;
		}

		FMsg("U2Dx9ShaderMgr::D3DXAssembleShader Errors:\n");
		if( pbufErrors != NULL )
		{
			HandleErrorString( (TCHAR*) pbufErrors->GetBufferPointer() );
		}
		else
		{
			FMsg("Error buffer is NULL!\n");
		}
		SAFE_RELEASE( pbufShader );
		BREAK_AND_RET_VAL_IF_FAILED( hr );
	}

	hr = CreateShader( pbufShader, eShaderType, pDesc );

	// Add shader description to the array, set its index and return the index
	//@@@@ make function for this!!	
	m_shaders.push_back( *pDesc );
	* outIndex = (ShaderIndex) m_shaders.size() - 1;
	m_shaderIndices.push_back( *outIndex );
	return( hr );
}
HRESULT CD3D9ShaderMaterialRenderer::stubD3DXAssembleShader(LPCSTR pSrcData,
		UINT SrcDataLen, CONST D3DXMACRO* pDefines,
		LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXBUFFER* ppShader,
		LPD3DXBUFFER* ppErrorMsgs)
{
	// Because Irrlicht needs to be able to start up even without installed d3d dlls, it
	// needs to load external d3d dlls manually. examples for the dlls are:
	// SDK              dll name     D3DX_SDK_VERSION
	// Summer 2004:     no dll        22
	// February 2005:	d3dx9_24.dll  24
	// April 2005:		d3dx9_25.dll  25
	// June 2005:		d3dx9_26.dll  26
	// August 2005:		d3dx9_27.dll  27
	// October 2005,
	// December 2005:	d3dx9_28.dll  28

	#if ( D3DX_SDK_VERSION < 24 )
		// directly link functions, old d3d sdks didn't try to load external dlls
		// when linking to the d3dx9.lib
		#ifdef _MSC_VER
		#pragma comment (lib, "d3dx9.lib")
		#endif

		// invoke static linked function
		return D3DXAssembleShader(pSrcData, SrcDataLen, pDefines, pInclude,
						  Flags, ppShader, ppErrorMsgs);
	#else
	{
		// try to load shader functions from the dll and print error if failed.

		// D3DXAssembleShader signature
		typedef HRESULT (WINAPI *AssembleShaderFunction)(LPCSTR pSrcData,  UINT SrcDataLen,
					CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude,
					DWORD Flags, LPD3DXBUFFER* ppShader,
					LPD3DXBUFFER* ppErrorMsgs);

		static bool LoadFailed = false;
		static AssembleShaderFunction pFn = 0;

		if (!pFn && !LoadFailed)
		{
			// try to load dll
			io::path strDllName = "d3dx9_";
			strDllName += (int)D3DX_SDK_VERSION;
			strDllName += ".dll";

			HMODULE hMod = LoadLibrary(strDllName.c_str());
			if (hMod)
				 pFn = (AssembleShaderFunction)GetProcAddress(hMod, "D3DXAssembleShader");

			if (!pFn)
			{
				LoadFailed = true;
				os::Printer::log("Could not load shader function D3DXAssembleShader from dll, shaders disabled",
					strDllName.c_str(), ELL_ERROR);
			}
		}

		if (pFn)
		{
			// call already loaded function
			return (*pFn)(pSrcData, SrcDataLen, pDefines, pInclude, Flags, ppShader, ppErrorMsgs);
		}
	}
	#endif // D3DX_SDK_VERSION < 24

	return 0;
}
Exemple #14
0
bool InitVertexShader()
{
#if DIRECTX_VERSION == 8
	// shader decl
	DWORD decl[] =
	{
		D3DVSD_STREAM(0),
		D3DVSD_REG(0, D3DVSDT_FLOAT3 ), // D3DVSDE_POSITION
		D3DVSD_REG(1, D3DVSDT_FLOAT3 ), // D3DVSDE_NORMAL 
		D3DVSD_REG(2, D3DVSDT_D3DCOLOR ), // D3DVSDE_DIFFUSE 
		D3DVSD_REG(3, D3DVSDT_FLOAT2 ), // D3DVSDE_TEXCOORD0 
		D3DVSD_END()
	};
#else
	D3DVERTEXELEMENT9 decl[] = 
	{
		{ 0, 0,  D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0 },
		{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,  0 },
		{ 0, 28, D3DDECLTYPE_FLOAT2,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
		D3DDECL_END()
	};
#endif

	LPD3DXBUFFER pCode;
	LPD3DXBUFFER ppCompilationErrors;

	HRESULT res;

#ifdef _DEBUG
#if DIRECTX_VERSION == 8
	LPD3DXBUFFER ppConstants;
	res = D3DXAssembleShaderFromFile("shader.vsh", D3DXASM_DEBUG, &ppConstants, &pCode, &ppCompilationErrors);
#else
	res = D3DXAssembleShaderFromFileA("D:\\n64developing\\RiceVideo\\shaderdx9.vsh", NULL, NULL, D3DXSHADER_DEBUG, &pCode, &ppCompilationErrors);
	//res = D3DXAssembleShaderFromFileA("D:\\n64developing\\RiceVideo\\shaderdx9-2.vsh", NULL, NULL, D3DXSHADER_DEBUG, &pCode, &ppCompilationErrors);
#endif
#else
#if DIRECTX_VERSION == 8
	LPD3DXBUFFER ppConstants;
	res = D3DXAssembleShader(shaderstr, strlen(shaderstr), 0, &ppConstants, &pCode, &ppCompilationErrors);
#else
	res = D3DXAssembleShader(shaderstr2, strlen(shaderstr2), NULL, NULL, D3DXSHADER_DEBUG, &pCode, &ppCompilationErrors);
#endif
#endif


	if( CDXGraphicsContext::IsResultGood(res,true) )
	{
#if DIRECTX_VERSION == 8
		res = g_pD3DDev->CreateVertexShader( decl, (DWORD*)pCode->GetBufferPointer(), &gVertexShader, 0 );
#else
		res = g_pD3DDev->CreateVertexShader( (DWORD*)pCode->GetBufferPointer(), &gVertexShader );
		//FILE *fp = fopen("D:\\n64developing\\RiceVideo\\shaderdx9.vso","rb");
		//BYTE buf[4000];
		//int num = fread(buf, 1, 4000, fp);
		//res = g_pD3DDev->CreateVertexShader( (DWORD*)buf, &gVertexShader );
		//fclose(fp);
#endif
		pCode->Release();

		if( !CDXGraphicsContext::IsResultGood(res,true) )
			return false;

		res = g_pD3DDev->SetVertexShader( gVertexShader );
		InitVertexShaderConstants();
		return true;
	}
	else
	{
		if( ppCompilationErrors )
		{
			char* str = (char*)(ppCompilationErrors->GetBufferPointer());
			TRACE0(str);
		}
		return false;
	}
}
bool CD3D8ShaderMaterialRenderer::createVertexShader(const char* vtxsh, E_VERTEX_TYPE type)
{
	if (!vtxsh)
		return true;

	// compile shader
#if defined( _IRR_XBOX_PLATFORM_)
	return false;
#else

	LPD3DXBUFFER code = 0;
	LPD3DXBUFFER errors = 0;

	#ifdef _IRR_D3D_NO_SHADER_DEBUGGING

		// compile shader without debug info
		D3DXAssembleShader(vtxsh, (UINT)strlen(vtxsh), 0, 0, &code, &errors);

	#else

		// compile shader and emitt some debug informations to
		// make it possible to debug the shader in visual studio
		static int irr_dbg_file_nr = 0;
		++irr_dbg_file_nr;
		char tmp[32];
		sprintf(tmp, "irr_d3d8_dbg_shader_%d.vsh", irr_dbg_file_nr);

		FILE* f = fopen(tmp, "wb");
		fwrite(vtxsh, strlen(vtxsh), 1, f);
		fflush(f);
		fclose(f);

		D3DXAssembleShaderFromFile(tmp, D3DXASM_DEBUG, 0, &code, &errors);

	#endif


	if (errors)
	{
		// print out compilation errors.
		os::Printer::log("Vertex shader compilation failed:", ELL_ERROR);
		os::Printer::log((c8*)errors->GetBufferPointer(), ELL_ERROR);

		if (code)
			code->Release();

		errors->Release();
		return false;
	}

	DWORD* decl = 0;

	DWORD dwStdDecl[] =
	{
		D3DVSD_STREAM(0),
		D3DVSD_REG(0, D3DVSDT_FLOAT3),   // position 0
		D3DVSD_REG(1, D3DVSDT_FLOAT3),   // normal 1
		D3DVSD_REG(2, D3DVSDT_D3DCOLOR ),// color 2
		D3DVSD_REG(3, D3DVSDT_FLOAT2 ),  // tex1 3
		D3DVSD_REG(4, D3DVSDT_FLOAT2 ),  // tex2 4
		D3DVSD_END()
	};

	DWORD dwTngtDecl[] =
	{
		D3DVSD_STREAM(0),
		D3DVSD_REG(0 , D3DVSDT_FLOAT3),   // position 0
		D3DVSD_REG(1 , D3DVSDT_FLOAT3),   // normal 1
		D3DVSD_REG(2 , D3DVSDT_D3DCOLOR ),// color 2
		D3DVSD_REG(3 , D3DVSDT_FLOAT2 ),  // tex1 3
		D3DVSD_REG(4, D3DVSDT_FLOAT3 ),  // tangent 4
		D3DVSD_REG(5, D3DVSDT_FLOAT3 ),  // binormal 5
		D3DVSD_END()
	};

	if (type == EVT_TANGENTS)
		decl = dwTngtDecl;
	else
		decl = dwStdDecl;

	if (FAILED(pID3DDevice->CreateVertexShader(decl,
		(DWORD*)code->GetBufferPointer(), &VertexShader, 0)))
	{
		os::Printer::log("Could not create vertex shader.", ELL_ERROR);
		code->Release();
		return false;
	}

	code->Release();
	return true;
#endif
}
Exemple #16
0
int CDirectXPixelShaderCombiner::GeneratePixelShaderFromMux(void)
{
	char buf[100];

	/************************************************************************/
	/*
	/*    STEP 1: generate pixel shader text according to current MUX
	/*
	/* Ideas of implementation:
	/* - 8 arithmetic instructions are just enough to simulate N64 combiners
	/*   in such a way:
	/*      For color channel
	/*			r0.rgb = a1-b1
	/*      	r0.rgb = r0*c1+d1
	/*      	r0.rgb = a2-b2
	/*      	r0.rgb = r0*c2+dd
	/*      For alpha channel
	/*			r0.a = a1-b1
	/*      	r0.a = r0*c1+d1
	/*      	r0.a = a2-b2
	/*      	r0.a = r0*c2+dd
	/*
	/* - Constant definition:
	/*   c0: = 0	will never change
	/*   c1: = 1	will never change
	/*   c2: = MUX_PRIM
	/*   c3: = MUX_ENV
	/*   c4: = MUX_LOADFRAC
	/*   c5: = MUX_PRIMLOADPRAC
	/*
	/* - Will use register r0 as MUX_COMB, never changes
	/*
	/************************************************************************/

	DecodedMux &m = *m_pDecodedMux;

	m_textBuf[0] = 0;
	strcat(m_textBuf, "ps.1.1\n");
	strcat(m_textBuf, "def c0, 0.0, 0.0, 0.0, 0.0\n");
	strcat(m_textBuf, "def c1, 1.0, 1.0, 1.0, 1.0\n");
	if( m.m_bTexel0IsUsed ) strcat(m_textBuf, "tex t0\n");
	if( m.m_bTexel1IsUsed ) strcat(m_textBuf, "tex t1\n");

	// Color channel 1
	sprintf(buf, "sub r1.rgb,     %s, %s\n", MuxToC(m.aRGB0), MuxToC(m.bRGB0) );	strcat(m_textBuf, buf);
	sprintf(buf, "mad_sat r1.rgb, r1, %s, %s\n", MuxToC(m.cRGB0), MuxToC(m.dRGB0) );	strcat(m_textBuf, buf);

	// Alpha channel 1
	sprintf(buf, "sub r1.a,     %s, %s\n", MuxToA(m.aA0), MuxToA(m.bA0) );	strcat(m_textBuf, buf);
	sprintf(buf, "mad_sat r1.a, r1, %s, %s\n", MuxToA(m.cA0), MuxToA(m.dA0) );	strcat(m_textBuf, buf);

	// Color channel 2
	sprintf(buf, "sub r0.rgb,     %s, %s\n", MuxToC(m.aRGB1), MuxToC(m.bRGB1) );	strcat(m_textBuf, buf);
	sprintf(buf, "mad_sat r0.rgb, r0, %s, %s\n", MuxToC(m.cRGB1), MuxToC(m.dRGB1) );	strcat(m_textBuf, buf);

	// Alpha channel 2
	sprintf(buf, "sub r0.a,     %s, %s\n", MuxToA(m.aA1), MuxToA(m.bA1) );	strcat(m_textBuf, buf);
	sprintf(buf, "mad_sat r0.a, r0, %s, %s\n", MuxToA(m.cA1), MuxToA(m.dA1) );	strcat(m_textBuf, buf);

	// Step 2: Compile the shade text to generate a new pixel shader binary

	PixelShaderEntry newEntry;
	newEntry.mux64 = m_pD3DRender->m_Mux;

	HRESULT e = D3DXAssembleShader( m_textBuf, strlen(m_textBuf),  0, NULL, NULL, &(newEntry.pVS), NULL );
	if( e != S_OK )
	{
#ifdef _DEBUG
		TRACE0("Error to assemble shader");
		TRACE0(m_textBuf);
		DisplaySimpleMuxString();
#endif
	}

	e = g_pD3DDev->CreatePixelShader( (DWORD*)newEntry.pVS->GetBufferPointer(), &(newEntry.pShader) );

	if( e != S_OK )
	{
		TRACE0("Error to create shader");
		TRACE0(m_textBuf);
		newEntry.dwShaderID = 0;
		newEntry.pShader = NULL;
	}
	
#ifdef _DEBUG
	newEntry.pShaderText = new char[strlen(m_textBuf)+1];
	strcpy(newEntry.pShaderText, m_textBuf);
#else
	newEntry.pShaderText = NULL;
#endif

	m_pixelShaderList.push_back(newEntry);

	return m_pixelShaderList.size()-1;
}
Exemple #17
0
HRESULT CRacorX::RestoreDeviceObjects()
{
	IDirect3DDevice8* device;
	HRESULT hr = m_spD3D->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		m_hWnd,
		m_iVP,
		&m_dpps,
		&device);
	if (FAILED(hr)) {
		OutputDebugString(L"Create Device Failed! Error:\n");
		switch (hr)
		{
		case D3DERR_DEVICELOST:
			OutputDebugString(L"D3DERR_DEVICELOST\n");
			break;
		case D3DERR_INVALIDCALL:
			OutputDebugString(L"D3DERR_INVALIDCALL\n");
			break;
		case D3DERR_NOTAVAILABLE:
			OutputDebugString(L"D3DERR_NOTAVAILABLE\n");
			break;
		case D3DERR_OUTOFVIDEOMEMORY:
			OutputDebugString(L"D3DERR_OUTOFVIDEOMEMORY\n");
			break;;
		default:
			OutputDebugString(L"Unknown\n");
			break;
		}
		return S_FALSE;
	}
	m_spDevice.reset(device, [](IDirect3DDevice8* device){ device->Release(); });
	DWORD dwDecl0[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(0,D3DVSDT_FLOAT3),
		//D3DVSD_REG(5,D3DVSDT_D3DCOLOR),
		/*D3DVSD_CONST(0, 4),
		*(DWORD*)&m_mtWorld[0], *(DWORD*)&m_mtWorld[1], *(DWORD*)&m_mtWorld[2], *(DWORD*)&m_mtWorld[3],
		*(DWORD*)&m_mtWorld[4], *(DWORD*)&m_mtWorld[5], *(DWORD*)&m_mtWorld[6], *(DWORD*)&m_mtWorld[7],
		*(DWORD*)&m_mtWorld[8], *(DWORD*)&m_mtWorld[9], *(DWORD*)&m_mtWorld[10], *(DWORD*)&m_mtWorld[11],
		*(DWORD*)&m_mtWorld[12], *(DWORD*)&m_mtWorld[13], *(DWORD*)&m_mtWorld[14], *(DWORD*)&m_mtWorld[15],
		D3DVSD_CONST(0, 1),
		*(DWORD*)&m_fMaterial[0], *(DWORD*)&m_fMaterial[1], *(DWORD*)&m_fMaterial[2], *(DWORD*)&m_fMaterial[3],*/
		D3DVSD_END()
	};

	IDirect3DVertexBuffer8* vb;
	m_spDevice->CreateVertexBuffer(
		4 * sizeof (Vertex),
		D3DUSAGE_WRITEONLY,
		Vertex::FVF,
		D3DPOOL_MANAGED,
		&vb);
	m_spVB.reset(vb, [](IDirect3DVertexBuffer8* vb){ vb->Release(); });

	Vertex* vertices = 0;
	m_spVB->Lock(0, 0, reinterpret_cast<BYTE**>(&vertices), 0);
	vertices[0] = { -100.0f, -100.0f, 0.0f, };
	vertices[1] = { 100.0f, -100.0f, 0.0f, };
	vertices[2] = { 100.0f, 100.0f, 0.0f, };
	vertices[3] = { -100.0f, 100.0f, 0.0f, };
	/*
	vertices[0] = { -1.0f, -1.0f, 0.2f, };
	vertices[1] = { 1.0f, -1.0f, 0.2f, };
	vertices[2] = { 1.0f, 1.0f, 0.2f, };
	vertices[3] = { -1.0f, 1.0f, 0.2f, };
	*/
	m_spVB->Unlock();

	IDirect3DIndexBuffer8* ib;
	m_spDevice->CreateIndexBuffer(
		6 * sizeof(WORD),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&ib);
	m_spIB.reset(ib, [](IDirect3DIndexBuffer8* ib){ ib->Release(); });

	WORD *indices = 0;
	m_spIB->Lock(0, 0, reinterpret_cast<BYTE**>(&indices), 0);

	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;
	indices[3] = 0;
	indices[4] = 2;
	indices[5] = 3;

	m_spIB->Unlock();
	
	const char vsh[] = 
		"vs.1.1 \n" \
		"dp4 oPos.x, v0, c0 \n"\
		"dp4 oPos.y, v0, c1 \n"\
		"dp4 oPos.z, v0, c2 \n"\
		"dp4 oPos.w, v0, c3 \n"\
		"mov oD0, c4\n";
	/*
	const char vsh[] = 
		"vs.1.1 \n" \
		"mov oPos, v0 \n" \
		"mov oD0, c4 \n";
		*/
	ID3DXBuffer* pVBuffer;
	ID3DXBuffer* pErrors;
	HRESULT rc = D3DXAssembleShader(reinterpret_cast<LPCVOID>(vsh), sizeof(vsh) - 1, 0, NULL, &pVBuffer, &pErrors);
	if (FAILED(rc))
	{
		OutputDebugString(L"Failed to assemble the vertex shader, error:\n");
		OutputDebugStringA(reinterpret_cast<CHAR*>(pErrors->GetBufferPointer()));
		OutputDebugString(L"\n");
	}

	rc = m_spDevice->CreateVertexShader(dwDecl0, (DWORD*)pVBuffer->GetBufferPointer(), &m_dwVertexShader, 0);
	if (FAILED(rc))
	{
		OutputDebugString(L"Failed to create vertex shader, error:\n");
		WCHAR szBuffer[512] = { 0 };
		D3DXGetErrorString(rc, szBuffer, sizeof(szBuffer));
		OutputDebugString(szBuffer);
		OutputDebugString(L"\n");
	}
	m_spDevice->SetViewport(&m_Viewport);
	//m_spDevice->SetTransform(D3DTS_VIEW, &m_mtView);
	//m_spDevice->SetTransform(D3DTS_PROJECTION, &m_mtProj);
	m_spDevice->SetRenderState(D3DRS_ZENABLE, true);
	m_spDevice->SetRenderState(D3DRS_LIGHTING, false);
	//m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	return S_OK;
}
Exemple #18
0
static void assembleshader_test(void) {
    const char test1[] = {
        "vs.1.1\n"
        "mov DEF2, v0\n"
    };
    const char testincl[] = {
        "#define REGISTER r0\n"
        "vs.1.1\n"
    };
    const char testshader[] = {
        "#include \"incl.vsh\"\n"
        "mov REGISTER, v0\n"
    };
    const char testshader2[] = {
        "#include \"incl2.vsh\"\n"
        "mov REGISTER, v0\n"
    };
    const char testshader3[] = {
        "#include \"include/incl3.vsh\"\n"
        "mov REGISTER, v0\n"
    };
    const char testincl3[] = {
        "#include \"incl4.vsh\"\n"
    };
    const char testincl4_ok[] = {
        "#define REGISTER r0\n"
        "vs.1.1\n"
    };
    const char testincl4_wrong[] = {
        "#error \"wrong include\"\n"
    };
    HRESULT hr;
    ID3DXBuffer *shader, *messages;
    D3DXMACRO defines[] = {
        {
            "DEF1", "10 + 15"
        },
        {
            "DEF2", "r0"
        },
        {
            NULL, NULL
        }
    };
    struct D3DXIncludeImpl include;
    HRESULT shader_vsh_res;

    /* pDefines test */
    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShader(test1, strlen(test1),
                            defines, NULL, D3DXSHADER_SKIPVALIDATION,
                            &shader, &messages);
    ok(hr == D3D_OK, "pDefines test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    /* NULL messages test */
    shader = NULL;
    hr = D3DXAssembleShader(test1, strlen(test1),
                            defines, NULL, D3DXSHADER_SKIPVALIDATION,
                            &shader, NULL);
    ok(hr == D3D_OK, "NULL messages test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(shader) ID3DXBuffer_Release(shader);

    /* NULL shader test */
    messages = NULL;
    hr = D3DXAssembleShader(test1, strlen(test1),
                            defines, NULL, D3DXSHADER_SKIPVALIDATION,
                            NULL, &messages);
    ok(hr == D3D_OK, "NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }

    /* pInclude test */
    shader = NULL;
    messages = NULL;
    include.ID3DXInclude_iface.lpVtbl = &D3DXInclude_Vtbl;
    hr = D3DXAssembleShader(testshader, strlen(testshader), NULL, &include.ID3DXInclude_iface,
                            D3DXSHADER_SKIPVALIDATION, &shader, &messages);
    ok(hr == D3D_OK, "pInclude test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    /* "unexpected #include file from memory" test */
    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShader(testshader, strlen(testshader),
                            NULL, NULL, D3DXSHADER_SKIPVALIDATION,
                            &shader, &messages);
    ok(hr == D3DXERR_INVALIDDATA, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    /* recursive #include test */
    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShader(testshader2, strlen(testshader2), NULL, &include.ID3DXInclude_iface,
                            D3DXSHADER_SKIPVALIDATION, &shader, &messages);
    ok(hr == D3D_OK, "D3DXAssembleShader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("recursive D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    shader_vsh_res = create_file("shader.vsh", testshader, sizeof(testshader) - 1);
    if(SUCCEEDED(shader_vsh_res)) {
        create_file("incl.vsh", testincl, sizeof(testincl) - 1);

        /* D3DXAssembleShaderFromFile + #include test */
        shader = NULL;
        messages = NULL;
        hr = D3DXAssembleShaderFromFileA("shader.vsh",
                                         NULL, NULL, D3DXSHADER_SKIPVALIDATION,
                                         &shader, &messages);
        ok(hr == D3D_OK, "D3DXAssembleShaderFromFile test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
        if(messages) {
            trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
            ID3DXBuffer_Release(messages);
        }
        if(shader) ID3DXBuffer_Release(shader);

        /* D3DXAssembleShaderFromFile + pInclude test */
        shader = NULL;
        messages = NULL;
        hr = D3DXAssembleShaderFromFileA("shader.vsh", NULL, &include.ID3DXInclude_iface,
                                         D3DXSHADER_SKIPVALIDATION, &shader, &messages);
        ok(hr == D3D_OK, "D3DXAssembleShaderFromFile + pInclude test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
        if(messages) {
            trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
            ID3DXBuffer_Release(messages);
        }
        if(shader) ID3DXBuffer_Release(shader);

        create_file("shader3.vsh", testshader3, sizeof(testshader3) - 1);
        create_file("incl4.vsh", testincl4_wrong, sizeof(testincl4_wrong) - 1);
        if(CreateDirectoryA("include", NULL)) {
            create_file("include/incl3.vsh", testincl3, sizeof(testincl3) - 1);
            create_file("include/incl4.vsh", testincl4_ok, sizeof(testincl4_ok) - 1);

            /* path search #include test */
            shader = NULL;
            messages = NULL;
            hr = D3DXAssembleShaderFromFileA("shader3.vsh", NULL, NULL,
                                             D3DXSHADER_SKIPVALIDATION,
                                             &shader, &messages);
            ok(hr == D3D_OK, "D3DXAssembleShaderFromFile path search test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
            if(messages) {
                trace("D3DXAssembleShaderFromFile path search messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
                ID3DXBuffer_Release(messages);
            }
            if(shader) ID3DXBuffer_Release(shader);
        } else skip("Couldn't create \"include\" directory\n");
    } else skip("Couldn't create \"shader.vsh\"\n");

    /* NULL shader tests */
    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShader(NULL, 0,
                            NULL, NULL, D3DXSHADER_SKIPVALIDATION,
                            &shader, &messages);
    ok(hr == D3DXERR_INVALIDDATA, "NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShader messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShaderFromFileA("nonexistent.vsh",
                                     NULL, NULL, D3DXSHADER_SKIPVALIDATION,
                                     &shader, &messages);
    ok(hr == D3DXERR_INVALIDDATA || hr == E_FAIL, /* I get this on WinXP */
        "D3DXAssembleShaderFromFile nonexistent file test failed with error 0x%x - %d\n",
        hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShaderFromFile messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    /* D3DXAssembleShaderFromResource test */
    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShaderFromResourceA(NULL, MAKEINTRESOURCEA(IDB_ASMSHADER),
                                         NULL, NULL, D3DXSHADER_SKIPVALIDATION,
                                         &shader, &messages);
    ok(hr == D3D_OK, "D3DXAssembleShaderFromResource test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    /* D3DXAssembleShaderFromResource with missing shader resource test */
    shader = NULL;
    messages = NULL;
    hr = D3DXAssembleShaderFromResourceA(NULL, "notexisting",
                                         NULL, NULL, D3DXSHADER_SKIPVALIDATION,
                                         &shader, &messages);
    ok(hr == D3DXERR_INVALIDDATA, "D3DXAssembleShaderFromResource NULL shader test failed with error 0x%x - %d\n", hr, hr & 0x0000FFFF);
    if(messages) {
        trace("D3DXAssembleShaderFromResource messages:\n%s", (char *)ID3DXBuffer_GetBufferPointer(messages));
        ID3DXBuffer_Release(messages);
    }
    if(shader) ID3DXBuffer_Release(shader);

    /* cleanup */
    if(SUCCEEDED(shader_vsh_res)) {
        DeleteFileA("shader.vsh");
        DeleteFileA("incl.vsh");
        DeleteFileA("shader3.vsh");
        DeleteFileA("incl4.vsh");
        DeleteFileA("include/incl3.vsh");
        DeleteFileA("include/incl4.vsh");
        RemoveDirectoryA("include");
    }
}
Exemple #19
0
unsigned int CWinRenderer::PreInit()
{
  CSingleLock lock(g_graphicsContext);
  m_bConfigured = false;
  UnInit();
  m_iResolution = PAL_4x3;

  m_iOSDRenderBuffer = 0;
  m_iYV12RenderBuffer = 0;
  m_NumOSDBuffers = 0;
  m_NumYV12Buffers = 0;
  m_OSDHeight = m_OSDWidth = 0;
  m_OSDRendered = false;

  m_iOSDTextureWidth = 0;
  m_iOSDTextureHeight[0] = 0;
  m_iOSDTextureHeight[1] = 0;

  // setup the background colour
  m_clearColour = (g_advancedSettings.m_videoBlackBarColour & 0xff) * 0x010101;
  // low memory pixel shader
  if (!m_hLowMemShader)
  {
    // lowmem shader (not as accurate, but no need for interleaving of YUV)
    const char *lowmem =
      "ps.1.3\n"
      "def c0, 0      ,0.18664,0.96032,0\n"
      "def c1, 0.76120,0.38738,0      ,0\n"
      "def c2, 1,0,1,1\n"
      "def c3, 0.0625,0.0625,0.0625,0\n"
      "def c4, 0.58219,0.58219,0.58219,0.5\n"
      "def c5, 0.03639,0.03639,0.03639,0\n"
      "tex t0\n"
      "tex t1\n"
      "tex t2\n"
      //"xmma_x2 r0,r1,discard, t1_bias,c0, t2_bias,c1\n"
      "mul_x2 r0,t1_bias,c0\n"
      "mul_x2 r1,t2_bias,c1\n"
      //"xmma discard,discard,r0, r0,c2_bx2, r1,c2_bx2\n"
      "mul r0, r0,c2_bx2\n"
      "mad r0, r1, c2_bx2, r0\n"
      //"xmma_x2 discard,discard,r1, t0,c4, -c3,c4\n"
      "mul r1, t0,c4\n"
      "add_x2 r1, r1, -c5\n"
      "add_sat r0, r0,r1\n";

    LPD3DXBUFFER pShader, pError;
	  HRESULT hr;
	  hr = D3DXAssembleShader(lowmem, strlen(lowmem),  NULL, NULL, &pShader, &pError);
	  if (FAILED(hr))
    {
      CLog::Log(LOGERROR, "CWinRenderer::PreInit: Call to D3DXAssembleShader failed!" );
      CLog::Log(LOGERROR,  (char*)pError->GetBufferPointer());
      return 1;
    }
    //m_pD3DDevice->CreatePixelShader((D3DPIXELSHADERDEF*)pShader->GetBufferPointer(), &m_hLowMemShader);
    pShader->Release();
  }

  return 0;
}